Skip to content

Commit

Permalink
Split GLProfile from GLFeatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and abcdefg30 committed Apr 25, 2020
1 parent a4b427b commit 91c4179
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
6 changes: 6 additions & 0 deletions OpenRA.Game/Graphics/PlatformInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

namespace OpenRA
{
public enum GLProfile
{
Modern,
Embedded,
}

public interface IPlatform
{
IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int batchSize, int videoDisplay);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Platforms.Default/FrameBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public FrameBuffer(Size size, ITextureInternal texture, Color clearColor)
OpenGL.glBindRenderbuffer(OpenGL.GL_RENDERBUFFER, depth);
OpenGL.CheckGLError();

var glDepth = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_DEPTH_COMPONENT16 : OpenGL.GL_DEPTH_COMPONENT;
var glDepth = OpenGL.Profile == GLProfile.Embedded ? OpenGL.GL_DEPTH_COMPONENT16 : OpenGL.GL_DEPTH_COMPONENT;
OpenGL.glRenderbufferStorage(OpenGL.GL_RENDERBUFFER, glDepth, size.Width, size.Height);
OpenGL.CheckGLError();

Expand Down
35 changes: 19 additions & 16 deletions OpenRA.Platforms.Default/OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ internal static class OpenGL
public enum GLFeatures
{
None = 0,
Core = 1,
GLES = 2,
DebugMessagesCallback = 4,
ESReadFormatBGRA = 8
DebugMessagesCallback = 1,
ESReadFormatBGRA = 2,
}

public static GLProfile Profile { get; private set; }
public static GLFeatures Features { get; private set; }

public static string Version { get; private set; }
Expand Down Expand Up @@ -495,7 +494,11 @@ public static void Initialize()
throw new InvalidProgramException("Failed to initialize low-level OpenGL bindings. GPU information is not available.", e);
}

DetectGLFeatures();
if (!DetectGLFeatures())
{
WriteGraphicsLog("Unsupported OpenGL version: " + glGetString(GL_VERSION));
throw new InvalidProgramException("OpenGL Version Error: See graphics.log for details.");
}

// Allow users to force-disable the debug message callback feature to work around driver bugs
if (Features.HasFlag(GLFeatures.DebugMessagesCallback) && Game.Settings.Graphics.DisableGLDebugMessageCallback)
Expand All @@ -509,18 +512,12 @@ public static void Initialize()
Features ^= GLFeatures.DebugMessagesCallback;
}

if (!Features.HasFlag(GLFeatures.Core))
{
WriteGraphicsLog("Unsupported OpenGL version: " + glGetString(GL_VERSION));
throw new InvalidProgramException("OpenGL Version Error: See graphics.log for details.");
}

// Setup the debug message callback handler
if (Features.HasFlag(GLFeatures.DebugMessagesCallback))
{
try
{
var suffix = Features.HasFlag(GLFeatures.GLES) ? "KHR" : "";
var suffix = Profile == GLProfile.Embedded ? "KHR" : "";
glDebugMessageCallback = Bind<DebugMessageCallback>("glDebugMessageCallback" + suffix);
glDebugMessageInsert = Bind<DebugMessageInsert>("glDebugMessageInsert" + suffix);

Expand Down Expand Up @@ -620,8 +617,9 @@ static T Bind<T>(string name)
return (T)(object)Marshal.GetDelegateForFunctionPointer(SDL.SDL_GL_GetProcAddress(name), typeof(T));
}

public static void DetectGLFeatures()
public static bool DetectGLFeatures()
{
var hasValidConfiguration = false;
try
{
Version = glGetString(GL_VERSION);
Expand All @@ -641,20 +639,25 @@ public static void DetectGLFeatures()
var hasBGRA = SDL.SDL_GL_ExtensionSupported("GL_EXT_texture_format_BGRA8888") == SDL.SDL_bool.SDL_TRUE;
if (Version.Contains(" ES") && hasBGRA && major >= 3)
{
Features = GLFeatures.Core | GLFeatures.GLES;

hasValidConfiguration = true;
Profile = GLProfile.Embedded;
if (SDL.SDL_GL_ExtensionSupported("GL_EXT_read_format_bgra") == SDL.SDL_bool.SDL_TRUE)
Features |= GLFeatures.ESReadFormatBGRA;
}
else if (major > 3 || (major == 3 && minor >= 2))
Features = GLFeatures.Core;
{
hasValidConfiguration = true;
Profile = GLProfile.Modern;
}

// Debug callbacks were introduced in GL 4.3
var hasDebugMessagesCallback = SDL.SDL_GL_ExtensionSupported("GL_KHR_debug") == SDL.SDL_bool.SDL_TRUE;
if (hasDebugMessagesCallback)
Features |= GLFeatures.DebugMessagesCallback;
}
catch (Exception) { }

return hasValidConfiguration;
}

public static void WriteGraphicsLog(string message)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Platforms.Default/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected uint CompileShaderObject(int type, string name)
var filename = Path.Combine(Platform.GameDir, "glsl", name + "." + ext);
var code = File.ReadAllText(filename);

var version = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? "300 es" : "140";
var version = OpenGL.Profile == GLProfile.Embedded ? "300 es" : "140";
code = code.Replace("{VERSION}", version);

var shader = OpenGL.glCreateShader(type);
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Platforms.Default/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void PrepareTexture()
void SetData(IntPtr data, int width, int height)
{
PrepareTexture();
var glInternalFormat = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
var glInternalFormat = OpenGL.Profile == GLProfile.Embedded ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, glInternalFormat, width, height,
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data);
OpenGL.CheckGLError();
Expand Down Expand Up @@ -119,7 +119,7 @@ public byte[] GetData()
var data = new byte[4 * Size.Width * Size.Height];

// GLES doesn't support glGetTexImage so data must be read back via a frame buffer
if (OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES))
if (OpenGL.Profile == GLProfile.Embedded)
{
// Query the active framebuffer so we can restore it afterwards
int lastFramebuffer;
Expand Down

0 comments on commit 91c4179

Please sign in to comment.