From 91c4179f0532c953843587f4cc8b70f1a1af5a1b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 21 Apr 2020 10:23:57 +0100 Subject: [PATCH] Split GLProfile from GLFeatures. --- OpenRA.Game/Graphics/PlatformInterfaces.cs | 6 ++++ OpenRA.Platforms.Default/FrameBuffer.cs | 2 +- OpenRA.Platforms.Default/OpenGL.cs | 35 ++++++++++++---------- OpenRA.Platforms.Default/Shader.cs | 2 +- OpenRA.Platforms.Default/Texture.cs | 4 +-- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/OpenRA.Game/Graphics/PlatformInterfaces.cs b/OpenRA.Game/Graphics/PlatformInterfaces.cs index 966240eb9d..40efb14167 100644 --- a/OpenRA.Game/Graphics/PlatformInterfaces.cs +++ b/OpenRA.Game/Graphics/PlatformInterfaces.cs @@ -15,6 +15,12 @@ using OpenRA.Primitives; namespace OpenRA { + public enum GLProfile + { + Modern, + Embedded, + } + public interface IPlatform { IPlatformWindow CreateWindow(Size size, WindowMode windowMode, float scaleModifier, int batchSize, int videoDisplay); diff --git a/OpenRA.Platforms.Default/FrameBuffer.cs b/OpenRA.Platforms.Default/FrameBuffer.cs index bc66dd718d..7a75ac6a56 100644 --- a/OpenRA.Platforms.Default/FrameBuffer.cs +++ b/OpenRA.Platforms.Default/FrameBuffer.cs @@ -50,7 +50,7 @@ namespace OpenRA.Platforms.Default 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(); diff --git a/OpenRA.Platforms.Default/OpenGL.cs b/OpenRA.Platforms.Default/OpenGL.cs index cac9ba785b..99ab42165b 100644 --- a/OpenRA.Platforms.Default/OpenGL.cs +++ b/OpenRA.Platforms.Default/OpenGL.cs @@ -29,12 +29,11 @@ namespace OpenRA.Platforms.Default 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; } @@ -495,7 +494,11 @@ namespace OpenRA.Platforms.Default 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) @@ -509,18 +512,12 @@ namespace OpenRA.Platforms.Default 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("glDebugMessageCallback" + suffix); glDebugMessageInsert = Bind("glDebugMessageInsert" + suffix); @@ -620,8 +617,9 @@ namespace OpenRA.Platforms.Default 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); @@ -641,13 +639,16 @@ namespace OpenRA.Platforms.Default 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; @@ -655,6 +656,8 @@ namespace OpenRA.Platforms.Default Features |= GLFeatures.DebugMessagesCallback; } catch (Exception) { } + + return hasValidConfiguration; } public static void WriteGraphicsLog(string message) diff --git a/OpenRA.Platforms.Default/Shader.cs b/OpenRA.Platforms.Default/Shader.cs index 7037822cf1..e2fc45c36c 100644 --- a/OpenRA.Platforms.Default/Shader.cs +++ b/OpenRA.Platforms.Default/Shader.cs @@ -33,7 +33,7 @@ namespace OpenRA.Platforms.Default 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); diff --git a/OpenRA.Platforms.Default/Texture.cs b/OpenRA.Platforms.Default/Texture.cs index b823b0d0e0..88d8c32d7f 100644 --- a/OpenRA.Platforms.Default/Texture.cs +++ b/OpenRA.Platforms.Default/Texture.cs @@ -75,7 +75,7 @@ namespace OpenRA.Platforms.Default 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(); @@ -119,7 +119,7 @@ namespace OpenRA.Platforms.Default 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;