From 861b417911c533bb4d9c2b2d9c034c69f2d0e4c3 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Fri, 8 May 2020 15:26:05 +0300 Subject: [PATCH] Moved non-mandatory gl functions to a demo-specific gl interface --- .../ControlCatalog/Pages/OpenGlPage.xaml.cs | 42 ++++++++++++++++--- src/Avalonia.OpenGL/GlInterface.cs | 27 +----------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/samples/ControlCatalog/Pages/OpenGlPage.xaml.cs b/samples/ControlCatalog/Pages/OpenGlPage.xaml.cs index 1698b69678..6c13a5ac22 100644 --- a/samples/ControlCatalog/Pages/OpenGlPage.xaml.cs +++ b/samples/ControlCatalog/Pages/OpenGlPage.xaml.cs @@ -7,6 +7,7 @@ using System.Runtime.InteropServices; using Avalonia; using Avalonia.Controls; using Avalonia.OpenGL; +using Avalonia.Platform.Interop; using Avalonia.Threading; using static Avalonia.OpenGL.GlConsts; // ReSharper disable StringLiteralTypo @@ -88,6 +89,7 @@ namespace ControlCatalog.Pages private int _vertexBufferObject; private int _indexBufferObject; private int _vertexArrayObject; + private GlExtrasInterface _glExt; private string GetShader(bool fragment, string shader) { @@ -255,6 +257,7 @@ namespace ControlCatalog.Pages protected unsafe override void OnOpenGlInit(GlInterface GL, int fb) { CheckError(GL); + _glExt = new GlExtrasInterface(GL); Info = $"Renderer: {GL.GetString(GL_RENDERER)} Version: {GL.GetString(GL_VERSION)}"; @@ -294,8 +297,8 @@ namespace ControlCatalog.Pages GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, new IntPtr(_indices.Length * sizeof(ushort)), new IntPtr(pdata), GL_STATIC_DRAW); CheckError(GL); - _vertexArrayObject = GL.GenVertexArray(); - GL.BindVertexArray(_vertexArrayObject); + _vertexArrayObject = _glExt.GenVertexArray(); + _glExt.BindVertexArray(_vertexArrayObject); CheckError(GL); GL.VertexAttribPointer(positionLocation, 3, GL_FLOAT, 0, vertexSize, IntPtr.Zero); @@ -312,12 +315,12 @@ namespace ControlCatalog.Pages // Unbind everything GL.BindBuffer(GL_ARRAY_BUFFER, 0); GL.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - GL.BindVertexArray(0); + _glExt.BindVertexArray(0); GL.UseProgram(0); // Delete all resources. GL.DeleteBuffers(2, new[] { _vertexBufferObject, _indexBufferObject }); - GL.DeleteVertexArrays(1, new[] { _vertexArrayObject }); + _glExt.DeleteVertexArrays(1, new[] { _vertexArrayObject }); GL.DeleteProgram(_shaderProgram); GL.DeleteShader(_fragmentShader); GL.DeleteShader(_vertexShader); @@ -334,7 +337,7 @@ namespace ControlCatalog.Pages GL.BindBuffer(GL_ARRAY_BUFFER, _vertexBufferObject); GL.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObject); - GL.BindVertexArray(_vertexArrayObject); + _glExt.BindVertexArray(_vertexArrayObject); GL.UseProgram(_shaderProgram); CheckError(GL); var projection = @@ -365,5 +368,34 @@ namespace ControlCatalog.Pages if (_disco > 0.01) Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background); } + + class GlExtrasInterface : GlInterfaceBase + { + public GlExtrasInterface(GlInterface gl) : base(gl.GetProcAddress, gl.ContextInfo) + { + } + + public delegate void GlDeleteVertexArrays(int count, int[] buffers); + [GlMinVersionEntryPoint("glDeleteVertexArrays", 3,0)] + [GlExtensionEntryPoint("glDeleteVertexArraysOES", "GL_OES_vertex_array_object")] + public GlDeleteVertexArrays DeleteVertexArrays { get; } + + public delegate void GlBindVertexArray(int array); + [GlMinVersionEntryPoint("glBindVertexArray", 3,0)] + [GlExtensionEntryPoint("glBindVertexArrayOES", "GL_OES_vertex_array_object")] + public GlBindVertexArray BindVertexArray { get; } + public delegate void GlGenVertexArrays(int n, int[] rv); + + [GlMinVersionEntryPoint("glGenVertexArrays",3,0)] + [GlExtensionEntryPoint("glGenVertexArraysOES", "GL_OES_vertex_array_object")] + public GlGenVertexArrays GenVertexArrays { get; } + + public int GenVertexArray() + { + var rv = new int[1]; + GenVertexArrays(1, rv); + return rv[0]; + } + } } } diff --git a/src/Avalonia.OpenGL/GlInterface.cs b/src/Avalonia.OpenGL/GlInterface.cs index 3d8532c5e7..d4356ebb9a 100644 --- a/src/Avalonia.OpenGL/GlInterface.cs +++ b/src/Avalonia.OpenGL/GlInterface.cs @@ -37,6 +37,7 @@ namespace Avalonia.OpenGL private GlInterface(GlContextInfo info, Func getProcAddress) : base(getProcAddress, info) { + ContextInfo = info; Version = GetString(GlConsts.GL_VERSION); Renderer = GetString(GlConsts.GL_RENDERER); Vendor = GetString(GlConsts.GL_VENDOR); @@ -307,25 +308,6 @@ namespace Avalonia.OpenGL [GlEntryPoint("glDrawElements")] public GlDrawElements DrawElements { get; } - - public delegate void GlGenVertexArrays(int n, int[] rv); - - [GlMinVersionEntryPoint("glGenVertexArrays",3,0)] - [GlExtensionEntryPoint("glGenVertexArraysOES", "GL_OES_vertex_array_object")] - public GlGenVertexArrays GenVertexArrays { get; } - - public int GenVertexArray() - { - var rv = new int[1]; - GenVertexArrays(1, rv); - return rv[0]; - } - - public delegate void GlBindVertexArray(int array); - [GlMinVersionEntryPoint("glBindVertexArray", 3,0)] - [GlExtensionEntryPoint("glBindVertexArrayOES", "GL_OES_vertex_array_object")] - public GlBindVertexArray BindVertexArray { get; } - public delegate int GlGetUniformLocation(int program, IntPtr name); [GlEntryPoint("glGetUniformLocation")] public GlGetUniformLocation GetUniformLocation { get; } @@ -353,11 +335,6 @@ namespace Avalonia.OpenGL [GlEntryPoint("glDeleteBuffers")] public GlDeleteBuffers DeleteBuffers { get; } - public delegate void GlDeleteVertexArrays(int count, int[] buffers); - [GlMinVersionEntryPoint("glDeleteVertexArrays", 3,0)] - [GlExtensionEntryPoint("glDeleteVertexArraysOES", "GL_OES_vertex_array_object")] - public GlDeleteVertexArrays DeleteVertexArrays { get; } - public delegate void GlDeleteProgram(int program); [GlEntryPoint("glDeleteProgram")] public GlDeleteProgram DeleteProgram { get; } @@ -365,8 +342,6 @@ namespace Avalonia.OpenGL public delegate void GlDeleteShader(int shader); [GlEntryPoint("glDeleteShader")] public GlDeleteShader DeleteShader { get; } - - // ReSharper restore UnassignedGetOnlyAutoProperty } }