From 47269b5c4fa21d1a8f52c30c2bcfc2be4c550142 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Mon, 13 Jan 2025 22:38:44 +0200 Subject: [PATCH] Glversion (#2186) * rename _version * GLVersion * use GLVersion --- .../.GL.Android/ConcreteGraphicsAdapter.cs | 15 +++-- .../ConcreteGraphicsContext.Android.cs | 9 ++- .../.GL.SDL/ConcreteGraphicsAdapter.cs | 19 +++---- .../.GL.SDL/ConcreteGraphicsContext.SDL.cs | 5 +- .../.GL.iOS/ConcreteGraphicsContext.iOS.cs | 9 ++- .../.GL/ConcreteGraphicsCapabilities.cs | 24 ++++---- .../Graphics/.GL/ConcreteGraphicsContext.cs | 3 +- Platforms/Graphics/.GL/ConcreteTexture.cs | 2 +- Platforms/Graphics/.GL/GLVersion.cs | 57 +++++++++++++++++++ .../Kni.Platform.Android.GL.Xamarin.csproj | 1 + Platforms/Kni.Platform.Android.GL.csproj | 1 + Platforms/Kni.Platform.Cardboard.GL.csproj | 1 + Platforms/Kni.Platform.Oculus.GL.csproj | 1 + Platforms/Kni.Platform.SDL2.GL.csproj | 1 + Platforms/Kni.Platform.iOS.GL.csproj | 1 + Platforms/MonoGame.Framework.DesktopGL.csproj | 1 + 16 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 Platforms/Graphics/.GL/GLVersion.cs diff --git a/Platforms/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs b/Platforms/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs index dab02a18f97..099773812f7 100644 --- a/Platforms/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs +++ b/Platforms/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs @@ -118,8 +118,7 @@ public override GraphicsBackend Backend EGLDisplay _eglDisplay; EGLConfig[] _eglConfigs; - int _eglMajorVersion; - int _eglMinorVersion; + GLVersion _eglVersion; int _maxTextureSize; int _maxVertexBufferSlots; int _maxMultiSampleCount; @@ -139,10 +138,12 @@ internal ConcreteGraphicsAdapter() #if CARDBOARD _eglDisplay = _ogl.Egl.EglGetCurrentDisplay(); - _ogl.GetInteger(GetParamName.MajorVersion, out _eglMajorVersion); + _ogl.GetInteger(GetParamName.MajorVersion, out int eglMajorVersion); _ogl.CheckGLError(); - _ogl.GetInteger(GetParamName.MinorVersion, out _eglMinorVersion); + _ogl.GetInteger(GetParamName.MinorVersion, out int eglMinorVersion); _ogl.CheckGLError(); + _eglVersion = new GLVersion(eglMajorVersion, eglMinorVersion); + _ogl.GetInteger(GetParamName.MaxTextureSize, out _maxTextureSize); _ogl.CheckGLError(); _ogl.GetInteger(GetParamName.MaxVertexAttribs, out _maxVertexBufferSlots); @@ -185,10 +186,12 @@ internal ConcreteGraphicsAdapter() _ogl.Egl.EglMakeCurrent(EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, eglContext); _ogl.CheckGLError(); - _ogl.GetInteger(GetParamName.MajorVersion, out _eglMajorVersion); + _ogl.GetInteger(GetParamName.MajorVersion, out int eglMajorVersion); _ogl.CheckGLError(); - _ogl.GetInteger(GetParamName.MinorVersion, out _eglMinorVersion); + _ogl.GetInteger(GetParamName.MinorVersion, out int eglMinorVersion); _ogl.CheckGLError(); + _eglVersion = new GLVersion(eglMajorVersion, eglMinorVersion); + _ogl.GetInteger(GetParamName.MaxTextureSize, out _maxTextureSize); _ogl.CheckGLError(); _ogl.GetInteger(GetParamName.MaxVertexAttribs, out _maxVertexBufferSlots); diff --git a/Platforms/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs b/Platforms/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs index eb07f614f08..07b30007790 100644 --- a/Platforms/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs +++ b/Platforms/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs @@ -86,20 +86,19 @@ internal ConcreteGraphicsContext(GraphicsContext context) else // if it fails, we assume to be on a 1.1 context version = "1.1"; - _glMajorVersion = Convert.ToInt32(version.Substring(0, 1)); - _glMinorVersion = Convert.ToInt32(version.Substring(2, 1)); + _glVersion.Major = Convert.ToInt16(version.Substring(0, 1)); + _glVersion.Minor = Convert.ToInt16(version.Substring(2, 1)); } catch (FormatException) { // if it fails, we assume to be on a 1.1 context - _glMajorVersion = 1; - _glMinorVersion = 1; + _glVersion = new GLVersion(1,1); } base._capabilities = new ConcreteGraphicsCapabilities(); ((ConcreteGraphicsCapabilities)base._capabilities).PlatformInitialize( this, ((IPlatformGraphicsContext)this.Context).DeviceStrategy, - _glMajorVersion, _glMinorVersion); + _glVersion); base.Initialize(this.Capabilities); diff --git a/Platforms/Graphics/.GL.SDL/ConcreteGraphicsAdapter.cs b/Platforms/Graphics/.GL.SDL/ConcreteGraphicsAdapter.cs index 4e1f9dcbe1c..628fd435945 100644 --- a/Platforms/Graphics/.GL.SDL/ConcreteGraphicsAdapter.cs +++ b/Platforms/Graphics/.GL.SDL/ConcreteGraphicsAdapter.cs @@ -138,9 +138,8 @@ public override GraphicsBackend Backend private OGL _gl; - private string _glVersion; - private int _glMajorVersion = 0; - private int _glMinorVersion = 0; + private string _version; + private GLVersion _glVersion; int _capMaxTextureSize; int _capMaxMultiSampleCount; @@ -150,8 +149,7 @@ public override GraphicsBackend Backend int _capMaxDrawBuffers; internal OGL GL { get { return _gl; } } - internal int glMajorVersion { get { return _glMajorVersion; } } - internal int glMinorVersion { get { return _glMinorVersion; } } + internal GLVersion glVersion { get { return _glVersion; } } internal ConcreteGraphicsAdapter() @@ -186,20 +184,19 @@ internal ConcreteGraphicsAdapter() // GL_MAJOR_VERSION and GL_MINOR_VERSION are GL 3.0+ only, so we need to rely on GL_VERSION string. try { - _glVersion = _gl.GetString(StringName.Version); - if (string.IsNullOrEmpty(_glVersion)) + _version = _gl.GetString(StringName.Version); + if (string.IsNullOrEmpty(_version)) throw new NoSuitableGraphicsDeviceException("Unable to retrieve OpenGL version"); // for OpenGL, the GL_VERSION string always starts with the version number in the "major.minor" format, // optionally followed by multiple vendor specific characters - _glMajorVersion = Convert.ToInt32(_glVersion.Substring(0, 1)); - _glMinorVersion = Convert.ToInt32(_glVersion.Substring(2, 1)); + _glVersion.Major = Convert.ToInt16(_version.Substring(0, 1)); + _glVersion.Minor = Convert.ToInt16(_version.Substring(2, 1)); } catch (FormatException) { // if it fails, we assume to be on a 1.1 context - _glMajorVersion = 1; - _glMinorVersion = 1; + _glVersion = new GLVersion(1, 1); } _description = _gl.GetString(StringName.Renderer); diff --git a/Platforms/Graphics/.GL.SDL/ConcreteGraphicsContext.SDL.cs b/Platforms/Graphics/.GL.SDL/ConcreteGraphicsContext.SDL.cs index 001bd1850a6..645a6936c0e 100644 --- a/Platforms/Graphics/.GL.SDL/ConcreteGraphicsContext.SDL.cs +++ b/Platforms/Graphics/.GL.SDL/ConcreteGraphicsContext.SDL.cs @@ -52,13 +52,12 @@ internal ConcreteGraphicsContext(GraphicsContext context) // get the GL version. - _glMajorVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete().glMajorVersion; - _glMinorVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete().glMinorVersion; + _glVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete().glVersion; base._capabilities = new ConcreteGraphicsCapabilities(); ((ConcreteGraphicsCapabilities)base._capabilities).PlatformInitialize( this, ((IPlatformGraphicsContext)this.Context).DeviceStrategy, - _glMajorVersion, _glMinorVersion); + _glVersion); base.Initialize(this.Capabilities); diff --git a/Platforms/Graphics/.GL.iOS/ConcreteGraphicsContext.iOS.cs b/Platforms/Graphics/.GL.iOS/ConcreteGraphicsContext.iOS.cs index ec8d1920829..a33cc9bdbc1 100644 --- a/Platforms/Graphics/.GL.iOS/ConcreteGraphicsContext.iOS.cs +++ b/Platforms/Graphics/.GL.iOS/ConcreteGraphicsContext.iOS.cs @@ -64,20 +64,19 @@ internal ConcreteGraphicsContext(GraphicsContext context) else // if it fails, we assume to be on a 1.1 context version = "1.1"; - _glMajorVersion = Convert.ToInt32(version.Substring(0, 1)); - _glMinorVersion = Convert.ToInt32(version.Substring(2, 1)); + _glVersion.Major = Convert.ToInt16(version.Substring(0, 1)); + _glVersion.Minor = Convert.ToInt16(version.Substring(2, 1)); } catch (FormatException) { // if it fails, we assume to be on a 1.1 context - _glMajorVersion = 1; - _glMinorVersion = 1; + _glVersion = new GLVersion(1,1); } base._capabilities = new ConcreteGraphicsCapabilities(); ((ConcreteGraphicsCapabilities)base._capabilities).PlatformInitialize( this, ((IPlatformGraphicsContext)this.Context).DeviceStrategy, - _glMajorVersion, _glMinorVersion); + _glVersion); base.Initialize(this.Capabilities); diff --git a/Platforms/Graphics/.GL/ConcreteGraphicsCapabilities.cs b/Platforms/Graphics/.GL/ConcreteGraphicsCapabilities.cs index 0cd076f821b..0b2a09507ba 100644 --- a/Platforms/Graphics/.GL/ConcreteGraphicsCapabilities.cs +++ b/Platforms/Graphics/.GL/ConcreteGraphicsCapabilities.cs @@ -34,7 +34,7 @@ internal sealed class ConcreteGraphicsCapabilities : GraphicsCapabilities internal int MaxDrawBuffers { get { return _maxDrawBuffers; } } - internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, GraphicsDeviceStrategy deviceStrategy, int majorVersion, int minorVersion) + internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, GraphicsDeviceStrategy deviceStrategy, GLVersion version) { var GL = cgraphicsContext.GL; @@ -88,13 +88,13 @@ internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, Gra if (GL.BoundApi == OGL.RenderApi.ES) { - SupportsEtc2 = majorVersion >= 3; + SupportsEtc2 = version >= new GLVersion(3, 0); } // Framebuffer objects #if GLES - SupportsFramebufferObjectARB = GL.BoundApi == OGL.RenderApi.ES && (majorVersion >= 2 || GL.Extensions.Contains("GL_ARB_framebuffer_object")); // always supported on GLES 2.0+ + SupportsFramebufferObjectARB = GL.BoundApi == OGL.RenderApi.ES && (version >= new GLVersion(2, 0) || GL.Extensions.Contains("GL_ARB_framebuffer_object")); // always supported on GLES 2.0+ SupportsFramebufferObjectEXT = GL.Extensions.Contains("GL_EXT_framebuffer_object");; SupportsFramebufferObjectIMG = GL.Extensions.Contains("GL_IMG_multisampled_render_to_texture") | GL.Extensions.Contains("GL_APPLE_framebuffer_multisample") | @@ -103,7 +103,7 @@ internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, Gra #elif DESKTOPGL // if we're on GL 3.0+, frame buffer extensions are guaranteed to be present, but extensions may be missing // it is then safe to assume that GL_ARB_framebuffer_object is present so that the standard function are loaded - SupportsFramebufferObjectARB = majorVersion >= 3 || GL.Extensions.Contains("GL_ARB_framebuffer_object"); + SupportsFramebufferObjectARB = version >= new GLVersion(3, 0) || GL.Extensions.Contains("GL_ARB_framebuffer_object"); SupportsFramebufferObjectEXT = GL.Extensions.Contains("GL_EXT_framebuffer_object"); #endif // Anisotropic filtering @@ -117,15 +117,15 @@ internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, Gra // sRGB #if GLES - SupportsSRgb = GL.Extensions.Contains("GL_EXT_sRGB"); - SupportsFloatTextures = GL.BoundApi == OGL.RenderApi.ES && (majorVersion >= 3 || GL.Extensions.Contains("GL_EXT_color_buffer_float")); - SupportsHalfFloatTextures = GL.BoundApi == OGL.RenderApi.ES && (majorVersion >= 3 || GL.Extensions.Contains("GL_EXT_color_buffer_half_float")); - SupportsNormalized = GL.BoundApi == OGL.RenderApi.ES && (majorVersion >= 3 && GL.Extensions.Contains("GL_EXT_texture_norm16")); + SupportsSRgb = GL.Extensions.Contains("GL_EXT_sRGB"); + SupportsFloatTextures = GL.BoundApi == OGL.RenderApi.ES && (version >= new GLVersion(3,0) || GL.Extensions.Contains("GL_EXT_color_buffer_float")); + SupportsHalfFloatTextures = GL.BoundApi == OGL.RenderApi.ES && (version >= new GLVersion(3,0) || GL.Extensions.Contains("GL_EXT_color_buffer_half_float")); + SupportsNormalized = GL.BoundApi == OGL.RenderApi.ES && (version >= new GLVersion(3,0) && GL.Extensions.Contains("GL_EXT_texture_norm16")); #elif DESKTOPGL SupportsSRgb = GL.Extensions.Contains("GL_EXT_texture_sRGB") && GL.Extensions.Contains("GL_EXT_framebuffer_sRGB"); - SupportsFloatTextures = GL.BoundApi == OGL.RenderApi.GL && (majorVersion >= 3 || GL.Extensions.Contains("GL_ARB_texture_float")); - SupportsHalfFloatTextures = GL.BoundApi == OGL.RenderApi.GL && (majorVersion >= 3 || GL.Extensions.Contains("GL_ARB_half_float_pixel"));; - SupportsNormalized = GL.BoundApi == OGL.RenderApi.GL && (majorVersion >= 3 || GL.Extensions.Contains("GL_EXT_texture_norm16"));; + SupportsFloatTextures = GL.BoundApi == OGL.RenderApi.GL && (version >= new GLVersion(3,0) || GL.Extensions.Contains("GL_ARB_texture_float")); + SupportsHalfFloatTextures = GL.BoundApi == OGL.RenderApi.GL && (version >= new GLVersion(3,0) || GL.Extensions.Contains("GL_ARB_half_float_pixel"));; + SupportsNormalized = GL.BoundApi == OGL.RenderApi.GL && (version >= new GLVersion(3,0) || GL.Extensions.Contains("GL_EXT_texture_norm16"));; #endif // TODO: Implement OpenGL support for texture arrays @@ -160,7 +160,7 @@ internal void PlatformInitialize(ConcreteGraphicsContextGL cgraphicsContext, Gra #if GLES SupportsSeparateBlendStates = false; #elif DESKTOPGL - SupportsSeparateBlendStates = majorVersion >= 4 || GL.Extensions.Contains("GL_ARB_draw_buffers_blend"); + SupportsSeparateBlendStates = version >= new GLVersion(4,0) || GL.Extensions.Contains("GL_ARB_draw_buffers_blend"); #endif GL.GetInteger(GetParamName.MaxDrawBuffers, out _maxDrawBuffers); diff --git a/Platforms/Graphics/.GL/ConcreteGraphicsContext.cs b/Platforms/Graphics/.GL/ConcreteGraphicsContext.cs index bda8e5ba71f..c215981cda1 100644 --- a/Platforms/Graphics/.GL/ConcreteGraphicsContext.cs +++ b/Platforms/Graphics/.GL/ConcreteGraphicsContext.cs @@ -64,8 +64,7 @@ internal abstract class ConcreteGraphicsContextGL : GraphicsContextStrategy internal bool FramebufferRequireFlippedY { get { return (base.RenderTargetCount > 0 && !(CurrentRenderTargetBindings[0].RenderTarget is RenderTargetSwapChain)); } } - internal int _glMajorVersion = 0; - internal int _glMinorVersion = 0; + internal GLVersion _glVersion; public override Viewport Viewport diff --git a/Platforms/Graphics/.GL/ConcreteTexture.cs b/Platforms/Graphics/.GL/ConcreteTexture.cs index dee3afc0bae..32586fae193 100644 --- a/Platforms/Graphics/.GL/ConcreteTexture.cs +++ b/Platforms/Graphics/.GL/ConcreteTexture.cs @@ -62,7 +62,7 @@ internal static void ToGLSurfaceFormat(SurfaceFormat format, bool supportsFloat = contextStrategy.Capabilities.SupportsFloatTextures; bool supportsHalfFloat = contextStrategy.Capabilities.SupportsHalfFloatTextures; bool supportsNormalized = contextStrategy.Capabilities.SupportsNormalized; - bool isGLES2 = GL.BoundApi == OGL.RenderApi.ES && ((ConcreteGraphicsContextGL)contextStrategy)._glMajorVersion == 2; + bool isGLES2 = GL.BoundApi == OGL.RenderApi.ES && ((ConcreteGraphicsContextGL)contextStrategy)._glVersion.Major == 2; switch (format) { diff --git a/Platforms/Graphics/.GL/GLVersion.cs b/Platforms/Graphics/.GL/GLVersion.cs new file mode 100644 index 00000000000..09e2ad2c816 --- /dev/null +++ b/Platforms/Graphics/.GL/GLVersion.cs @@ -0,0 +1,57 @@ +// Copyright (C)2025 Nick Kastellanos + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace Microsoft.Xna.Framework.Graphics +{ + [StructLayout(LayoutKind.Explicit)] + internal struct GLVersion + { + [FieldOffset(0)] + public short Major; + [FieldOffset(2)] + public short Minor; + + [FieldOffset(0)] + public int PackedValue; + + public GLVersion(short major, short minor) : this() + { + this.Major = major; + this.Minor = minor; + } + + public GLVersion(int major, int minor) : this() + { + this.Major = (short)major; + this.Minor = (short)minor; + } + + public static bool operator <(GLVersion l, GLVersion r) + { + return l.PackedValue < r.PackedValue; + } + + public static bool operator >(GLVersion l, GLVersion r) + { + return l.PackedValue > r.PackedValue; + } + + public static bool operator <=(GLVersion l, GLVersion r) + { + return l.PackedValue <= r.PackedValue; + } + + public static bool operator >=(GLVersion l, GLVersion r) + { + return l.PackedValue >= r.PackedValue; + } + + public override string ToString() + { + return String.Format("{0}.{1}", Major, Minor); + } + } +} diff --git a/Platforms/Kni.Platform.Android.GL.Xamarin.csproj b/Platforms/Kni.Platform.Android.GL.Xamarin.csproj index c19eaf6ad6d..dc5910a584b 100644 --- a/Platforms/Kni.Platform.Android.GL.Xamarin.csproj +++ b/Platforms/Kni.Platform.Android.GL.Xamarin.csproj @@ -162,6 +162,7 @@ + diff --git a/Platforms/Kni.Platform.Android.GL.csproj b/Platforms/Kni.Platform.Android.GL.csproj index 5040a819d2e..e2f143936d7 100644 --- a/Platforms/Kni.Platform.Android.GL.csproj +++ b/Platforms/Kni.Platform.Android.GL.csproj @@ -176,6 +176,7 @@ + diff --git a/Platforms/Kni.Platform.Cardboard.GL.csproj b/Platforms/Kni.Platform.Cardboard.GL.csproj index 5951be7d139..02127cc8629 100644 --- a/Platforms/Kni.Platform.Cardboard.GL.csproj +++ b/Platforms/Kni.Platform.Cardboard.GL.csproj @@ -211,6 +211,7 @@ + diff --git a/Platforms/Kni.Platform.Oculus.GL.csproj b/Platforms/Kni.Platform.Oculus.GL.csproj index f51fd8527d4..0d4e542d4f4 100644 --- a/Platforms/Kni.Platform.Oculus.GL.csproj +++ b/Platforms/Kni.Platform.Oculus.GL.csproj @@ -176,6 +176,7 @@ + diff --git a/Platforms/Kni.Platform.SDL2.GL.csproj b/Platforms/Kni.Platform.SDL2.GL.csproj index d02cb809228..bffa81a8697 100644 --- a/Platforms/Kni.Platform.SDL2.GL.csproj +++ b/Platforms/Kni.Platform.SDL2.GL.csproj @@ -150,6 +150,7 @@ + diff --git a/Platforms/Kni.Platform.iOS.GL.csproj b/Platforms/Kni.Platform.iOS.GL.csproj index 81eb25f5108..fc5c74486e4 100644 --- a/Platforms/Kni.Platform.iOS.GL.csproj +++ b/Platforms/Kni.Platform.iOS.GL.csproj @@ -166,6 +166,7 @@ + diff --git a/Platforms/MonoGame.Framework.DesktopGL.csproj b/Platforms/MonoGame.Framework.DesktopGL.csproj index 005b6bd3b0e..94f5bbaac13 100644 --- a/Platforms/MonoGame.Framework.DesktopGL.csproj +++ b/Platforms/MonoGame.Framework.DesktopGL.csproj @@ -166,6 +166,7 @@ +