Skip to content

Commit

Permalink
Glversion (#2186)
Browse files Browse the repository at this point in the history
* rename _version

* GLVersion

* use GLVersion
  • Loading branch information
nkast authored Jan 13, 2025
1 parent d69c5d7 commit 47269b5
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 45 deletions.
15 changes: 9 additions & 6 deletions Platforms/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public override GraphicsBackend Backend
EGLDisplay _eglDisplay;
EGLConfig[] _eglConfigs;

int _eglMajorVersion;
int _eglMinorVersion;
GLVersion _eglVersion;
int _maxTextureSize;
int _maxVertexBufferSlots;
int _maxMultiSampleCount;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
19 changes: 8 additions & 11 deletions Platforms/Graphics/.GL.SDL/ConcreteGraphicsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions Platforms/Graphics/.GL.SDL/ConcreteGraphicsContext.SDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ internal ConcreteGraphicsContext(GraphicsContext context)


// get the GL version.
_glMajorVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>().glMajorVersion;
_glMinorVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>().glMinorVersion;
_glVersion = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>().glVersion;

base._capabilities = new ConcreteGraphicsCapabilities();
((ConcreteGraphicsCapabilities)base._capabilities).PlatformInitialize(
this, ((IPlatformGraphicsContext)this.Context).DeviceStrategy,
_glMajorVersion, _glMinorVersion);
_glVersion);

base.Initialize(this.Capabilities);

Expand Down
9 changes: 4 additions & 5 deletions Platforms/Graphics/.GL.iOS/ConcreteGraphicsContext.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 12 additions & 12 deletions Platforms/Graphics/.GL/ConcreteGraphicsCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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") |
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions Platforms/Graphics/.GL/ConcreteGraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Platforms/Graphics/.GL/ConcreteTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
57 changes: 57 additions & 0 deletions Platforms/Graphics/.GL/GLVersion.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.Android.GL.Xamarin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />
<Compile Include="Graphics\RenderTargetSwapChain.OpenGL.cs" />
Expand Down
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.Android.GL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />

Expand Down
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.Cardboard.GL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />

Expand Down
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.Oculus.GL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />

Expand Down
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.SDL2.GL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />
<Compile Include="Graphics\RenderTargetSwapChain.OpenGL.cs" />
Expand Down
1 change: 1 addition & 0 deletions Platforms/Kni.Platform.iOS.GL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />

Expand Down
1 change: 1 addition & 0 deletions Platforms/MonoGame.Framework.DesktopGL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<Compile Include="Graphics\.GL\ConcreteSamplerStateCollection.cs" />
<Compile Include="Graphics\.GL\ConcreteTextureCollection.cs" />
<Compile Include="Graphics\.GL\GLExtensions.cs" />
<Compile Include="Graphics\.GL\GLVersion.cs" />
<Compile Include="Graphics\.GL\IRenderTargetStrategyGL.cs" />
<Compile Include="Graphics\.GL\OpenGL.cs" />

Expand Down

0 comments on commit 47269b5

Please sign in to comment.