Restore legacy OpenGL 2.1 support.

This commit is contained in:
Paul Chote
2020-04-22 00:13:23 +01:00
committed by abcdefg30
parent 839be24053
commit dac1f270ce
10 changed files with 224 additions and 39 deletions

View File

@@ -478,7 +478,7 @@ namespace OpenRA.Platforms.Default
#endregion
public static void Initialize()
public static void Initialize(bool preferLegacyProfile)
{
try
{
@@ -488,13 +488,14 @@ namespace OpenRA.Platforms.Default
glGetError = Bind<GetError>("glGetError");
glGetStringInternal = Bind<GetString>("glGetString");
glGetStringiInternal = Bind<GetStringi>("glGetStringi");
glGetIntegerv = Bind<GetIntegerv>("glGetIntegerv");
}
catch (Exception e)
{
throw new InvalidProgramException("Failed to initialize low-level OpenGL bindings. GPU information is not available.", e);
}
if (!DetectGLFeatures())
if (!DetectGLFeatures(preferLegacyProfile))
{
WriteGraphicsLog("Unsupported OpenGL version: " + glGetString(GL_VERSION));
throw new InvalidProgramException("OpenGL Version Error: See graphics.log for details.");
@@ -542,7 +543,6 @@ namespace OpenRA.Platforms.Default
glViewport = Bind<Viewport>("glViewport");
glClear = Bind<Clear>("glClear");
glClearColor = Bind<ClearColor>("glClearColor");
glGetIntegerv = Bind<GetIntegerv>("glGetIntegerv");
glFinish = Bind<Finish>("glFinish");
glCreateProgram = Bind<CreateProgram>("glCreateProgram");
glUseProgram = Bind<UseProgram>("glUseProgram");
@@ -571,10 +571,7 @@ namespace OpenRA.Platforms.Default
glBufferData = Bind<BufferData>("glBufferData");
glBufferSubData = Bind<BufferSubData>("glBufferSubData");
glDeleteBuffers = Bind<DeleteBuffers>("glDeleteBuffers");
glGenVertexArrays = Bind<GenVertexArrays>("glGenVertexArrays");
glBindVertexArray = Bind<BindVertexArray>("glBindVertexArray");
glBindAttribLocation = Bind<BindAttribLocation>("glBindAttribLocation");
glBindFragDataLocation = Bind<BindFragDataLocation>("glBindFragDataLocation");
glVertexAttribPointer = Bind<VertexAttribPointer>("glVertexAttribPointer");
glEnableVertexAttribArray = Bind<EnableVertexAttribArray>("glEnableVertexAttribArray");
glDisableVertexAttribArray = Bind<DisableVertexAttribArray>("glDisableVertexAttribArray");
@@ -594,16 +591,39 @@ namespace OpenRA.Platforms.Default
glGetTexImage = Bind<GetTexImage>("glGetTexImage");
glTexParameteri = Bind<TexParameteri>("glTexParameteri");
glTexParameterf = Bind<TexParameterf>("glTexParameterf");
glGenFramebuffers = Bind<GenFramebuffers>("glGenFramebuffers");
glBindFramebuffer = Bind<BindFramebuffer>("glBindFramebuffer");
glFramebufferTexture2D = Bind<FramebufferTexture2D>("glFramebufferTexture2D");
glDeleteFramebuffers = Bind<DeleteFramebuffers>("glDeleteFramebuffers");
glGenRenderbuffers = Bind<GenRenderbuffers>("glGenRenderbuffers");
glBindRenderbuffer = Bind<BindRenderbuffer>("glBindRenderbuffer");
glRenderbufferStorage = Bind<RenderbufferStorage>("glRenderbufferStorage");
glDeleteRenderbuffers = Bind<DeleteRenderbuffers>("glDeleteRenderbuffers");
glFramebufferRenderbuffer = Bind<FramebufferRenderbuffer>("glFramebufferRenderbuffer");
glCheckFramebufferStatus = Bind<CheckFramebufferStatus>("glCheckFramebufferStatus");
if (Profile != GLProfile.Legacy)
{
glGenVertexArrays = Bind<GenVertexArrays>("glGenVertexArrays");
glBindVertexArray = Bind<BindVertexArray>("glBindVertexArray");
glBindFragDataLocation = Bind<BindFragDataLocation>("glBindFragDataLocation");
glGenFramebuffers = Bind<GenFramebuffers>("glGenFramebuffers");
glBindFramebuffer = Bind<BindFramebuffer>("glBindFramebuffer");
glFramebufferTexture2D = Bind<FramebufferTexture2D>("glFramebufferTexture2D");
glDeleteFramebuffers = Bind<DeleteFramebuffers>("glDeleteFramebuffers");
glGenRenderbuffers = Bind<GenRenderbuffers>("glGenRenderbuffers");
glBindRenderbuffer = Bind<BindRenderbuffer>("glBindRenderbuffer");
glRenderbufferStorage = Bind<RenderbufferStorage>("glRenderbufferStorage");
glDeleteRenderbuffers = Bind<DeleteRenderbuffers>("glDeleteRenderbuffers");
glFramebufferRenderbuffer = Bind<FramebufferRenderbuffer>("glFramebufferRenderbuffer");
glCheckFramebufferStatus = Bind<CheckFramebufferStatus>("glCheckFramebufferStatus");
}
else
{
glGenVertexArrays = null;
glBindVertexArray = null;
glBindFragDataLocation = null;
glGenFramebuffers = Bind<GenFramebuffers>("glGenFramebuffersEXT");
glBindFramebuffer = Bind<BindFramebuffer>("glBindFramebufferEXT");
glFramebufferTexture2D = Bind<FramebufferTexture2D>("glFramebufferTexture2DEXT");
glDeleteFramebuffers = Bind<DeleteFramebuffers>("glDeleteFramebuffersEXT");
glGenRenderbuffers = Bind<GenRenderbuffers>("glGenRenderbuffersEXT");
glBindRenderbuffer = Bind<BindRenderbuffer>("glBindRenderbufferEXT");
glRenderbufferStorage = Bind<RenderbufferStorage>("glRenderbufferStorageEXT");
glDeleteRenderbuffers = Bind<DeleteRenderbuffers>("glDeleteRenderbuffersEXT");
glFramebufferRenderbuffer = Bind<FramebufferRenderbuffer>("glFramebufferRenderbufferEXT");
glCheckFramebufferStatus = Bind<CheckFramebufferStatus>("glCheckFramebufferStatusEXT");
}
}
catch (Exception e)
{
@@ -617,7 +637,7 @@ namespace OpenRA.Platforms.Default
return (T)(object)Marshal.GetDelegateForFunctionPointer(SDL.SDL_GL_GetProcAddress(name), typeof(T));
}
public static bool DetectGLFeatures()
public static bool DetectGLFeatures(bool preferLegacyProfile)
{
var hasValidConfiguration = false;
try
@@ -654,6 +674,15 @@ namespace OpenRA.Platforms.Default
var hasDebugMessagesCallback = SDL.SDL_GL_ExtensionSupported("GL_KHR_debug") == SDL.SDL_bool.SDL_TRUE;
if (hasDebugMessagesCallback)
Features |= GLFeatures.DebugMessagesCallback;
if (preferLegacyProfile || (major == 2 && minor == 1) || (major == 3 && minor < 2))
{
if (SDL.SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object") == SDL.SDL_bool.SDL_TRUE)
{
hasValidConfiguration = true;
Profile = GLProfile.Legacy;
}
}
}
catch (Exception) { }
@@ -680,10 +709,15 @@ namespace OpenRA.Platforms.Default
Log.Write("graphics", "Shader Version: {0}", glGetString(GL_SHADING_LANGUAGE_VERSION));
Log.Write("graphics", "Available extensions:");
int extensionCount;
glGetIntegerv(GL_NUM_EXTENSIONS, out extensionCount);
for (var i = 0; i < extensionCount; i++)
Log.Write("graphics", glGetStringi(GL_EXTENSIONS, (uint)i));
if (Profile != GLProfile.Legacy)
{
int extensionCount;
glGetIntegerv(GL_NUM_EXTENSIONS, out extensionCount);
for (var i = 0; i < extensionCount; i++)
Log.Write("graphics", glGetStringi(GL_EXTENSIONS, (uint)i));
}
else
Log.Write("graphics", glGetString(GL_EXTENSIONS));
}
public static void CheckGLError()

View File

@@ -35,14 +35,17 @@ namespace OpenRA.Platforms.Default
if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window.Window, context) < 0)
throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError()));
OpenGL.Initialize();
OpenGL.Initialize(window.GLProfile == GLProfile.Legacy);
OpenGL.CheckGLError();
uint vao;
OpenGL.CheckGLError();
OpenGL.glGenVertexArrays(1, out vao);
OpenGL.CheckGLError();
OpenGL.glBindVertexArray(vao);
OpenGL.CheckGLError();
if (OpenGL.Profile != GLProfile.Legacy)
{
uint vao;
OpenGL.glGenVertexArrays(1, out vao);
OpenGL.CheckGLError();
OpenGL.glBindVertexArray(vao);
OpenGL.CheckGLError();
}
OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex);
OpenGL.CheckGLError();

View File

@@ -151,9 +151,9 @@ namespace OpenRA.Platforms.Default
// We first need to query the available profiles on Windows/Linux.
// On macOS, known/consistent OpenGL support is provided by the OS.
if (Platform.CurrentPlatform == PlatformType.OSX)
supportedProfiles = new[] { GLProfile.Modern };
supportedProfiles = new[] { GLProfile.Modern, GLProfile.Legacy };
else
supportedProfiles = new[] { GLProfile.Modern, GLProfile.Embedded }
supportedProfiles = new[] { GLProfile.Modern, GLProfile.Embedded, GLProfile.Legacy }
.Where(CanCreateGLWindow)
.ToArray();
@@ -487,6 +487,10 @@ namespace OpenRA.Platforms.Default
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES);
break;
case GLProfile.Legacy:
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 1);
break;
}
}

View File

@@ -23,6 +23,7 @@ namespace OpenRA.Platforms.Default
public const int TexMetadataAttributeIndex = 2;
readonly Dictionary<string, int> samplers = new Dictionary<string, int>();
readonly Dictionary<int, int> legacySizeUniforms = new Dictionary<int, int>();
readonly Dictionary<int, ITexture> textures = new Dictionary<int, ITexture>();
readonly Queue<int> unbindTextures = new Queue<int>();
readonly uint program;
@@ -33,7 +34,9 @@ namespace OpenRA.Platforms.Default
var filename = Path.Combine(Platform.GameDir, "glsl", name + "." + ext);
var code = File.ReadAllText(filename);
var version = OpenGL.Profile == GLProfile.Embedded ? "300 es" : "140";
var version = OpenGL.Profile == GLProfile.Embedded ? "300 es" :
OpenGL.Profile == GLProfile.Legacy ? "120" : "140";
code = code.Replace("{VERSION}", version);
var shader = OpenGL.glCreateShader(type);
@@ -80,8 +83,12 @@ namespace OpenRA.Platforms.Default
OpenGL.CheckGLError();
OpenGL.glBindAttribLocation(program, TexMetadataAttributeIndex, "aVertexTexMetadata");
OpenGL.CheckGLError();
OpenGL.glBindFragDataLocation(program, 0, "fragColor");
OpenGL.CheckGLError();
if (OpenGL.Profile != GLProfile.Legacy)
{
OpenGL.glBindFragDataLocation(program, 0, "fragColor");
OpenGL.CheckGLError();
}
OpenGL.glAttachShader(program, vertexShader);
OpenGL.CheckGLError();
@@ -132,6 +139,13 @@ namespace OpenRA.Platforms.Default
OpenGL.glUniform1i(loc, nextTexUnit);
OpenGL.CheckGLError();
if (OpenGL.Profile == GLProfile.Legacy)
{
var sizeLoc = OpenGL.glGetUniformLocation(program, sampler + "Size");
if (sizeLoc >= 0)
legacySizeUniforms.Add(nextTexUnit, sizeLoc);
}
nextTexUnit++;
}
}
@@ -146,13 +160,21 @@ namespace OpenRA.Platforms.Default
// bind the textures
foreach (var kv in textures)
{
var id = ((ITextureInternal)kv.Value).ID;
var texture = (ITextureInternal)kv.Value;
// Evict disposed textures from the cache
if (OpenGL.glIsTexture(id))
if (OpenGL.glIsTexture(texture.ID))
{
OpenGL.glActiveTexture(OpenGL.GL_TEXTURE0 + kv.Key);
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, id);
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture.ID);
// Work around missing textureSize GLSL function by explicitly tracking sizes in a uniform
int param;
if (OpenGL.Profile == GLProfile.Legacy && legacySizeUniforms.TryGetValue(kv.Key, out param))
{
OpenGL.glUniform2f(param, texture.Size.Width, texture.Size.Height);
OpenGL.CheckGLError();
}
}
else
unbindTextures.Enqueue(kv.Key);