Migrate rendering to OpenGL 3.2 / OpenGL ES 3.0.
This commit is contained in:
committed by
RoosterDragon
parent
91c63034d3
commit
ce8112fb5a
@@ -160,6 +160,9 @@ namespace OpenRA
|
|||||||
[Desc("Disable separate OpenGL render thread on Windows operating systems.")]
|
[Desc("Disable separate OpenGL render thread on Windows operating systems.")]
|
||||||
public bool DisableWindowsRenderThread = true;
|
public bool DisableWindowsRenderThread = true;
|
||||||
|
|
||||||
|
[Desc("Use OpenGL ES if both ES and regular OpenGL are available.")]
|
||||||
|
public bool PreferGLES = false;
|
||||||
|
|
||||||
public int BatchSize = 8192;
|
public int BatchSize = 8192;
|
||||||
public int SheetSize = 2048;
|
public int SheetSize = 2048;
|
||||||
|
|
||||||
|
|||||||
@@ -33,31 +33,32 @@ namespace OpenRA.Platforms.Default
|
|||||||
|
|
||||||
OpenGL.glGenFramebuffers(1, out framebuffer);
|
OpenGL.glGenFramebuffers(1, out framebuffer);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glBindFramebuffer(OpenGL.FRAMEBUFFER_EXT, framebuffer);
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebuffer);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
// Color
|
// Color
|
||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
texture.SetEmpty(size.Width, size.Height);
|
texture.SetEmpty(size.Width, size.Height);
|
||||||
OpenGL.glFramebufferTexture2D(OpenGL.FRAMEBUFFER_EXT, OpenGL.COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, texture.ID, 0);
|
OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture.ID, 0);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
// Depth
|
// Depth
|
||||||
OpenGL.glGenRenderbuffers(1, out depth);
|
OpenGL.glGenRenderbuffers(1, out depth);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
OpenGL.glBindRenderbuffer(OpenGL.RENDERBUFFER_EXT, depth);
|
OpenGL.glBindRenderbuffer(OpenGL.GL_RENDERBUFFER, depth);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
OpenGL.glRenderbufferStorage(OpenGL.RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT, size.Width, size.Height);
|
var glDepth = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_DEPTH_COMPONENT16 : OpenGL.GL_DEPTH_COMPONENT;
|
||||||
|
OpenGL.glRenderbufferStorage(OpenGL.GL_RENDERBUFFER, glDepth, size.Width, size.Height);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
OpenGL.glFramebufferRenderbuffer(OpenGL.FRAMEBUFFER_EXT, OpenGL.DEPTH_ATTACHMENT_EXT, OpenGL.RENDERBUFFER_EXT, depth);
|
OpenGL.glFramebufferRenderbuffer(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_DEPTH_ATTACHMENT, OpenGL.GL_RENDERBUFFER, depth);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
// Test for completeness
|
// Test for completeness
|
||||||
var status = OpenGL.glCheckFramebufferStatus(OpenGL.FRAMEBUFFER_EXT);
|
var status = OpenGL.glCheckFramebufferStatus(OpenGL.GL_FRAMEBUFFER);
|
||||||
if (status != OpenGL.FRAMEBUFFER_COMPLETE_EXT)
|
if (status != OpenGL.GL_FRAMEBUFFER_COMPLETE)
|
||||||
{
|
{
|
||||||
var error = "Error creating framebuffer: {0}\n{1}".F(status, new StackTrace());
|
var error = "Error creating framebuffer: {0}\n{1}".F(status, new StackTrace());
|
||||||
OpenGL.WriteGraphicsLog(error);
|
OpenGL.WriteGraphicsLog(error);
|
||||||
@@ -65,19 +66,14 @@ namespace OpenRA.Platforms.Default
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restore default buffer
|
// Restore default buffer
|
||||||
OpenGL.glBindFramebuffer(OpenGL.FRAMEBUFFER_EXT, 0);
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, 0);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int[] ViewportRectangle()
|
static int[] ViewportRectangle()
|
||||||
{
|
{
|
||||||
var v = new int[4];
|
var v = new int[4];
|
||||||
unsafe
|
OpenGL.glGetIntegerv(OpenGL.GL_VIEWPORT, out v[0]);
|
||||||
{
|
|
||||||
fixed (int* ptr = &v[0])
|
|
||||||
OpenGL.glGetIntegerv(OpenGL.GL_VIEWPORT, ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@@ -92,7 +88,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
|
|
||||||
OpenGL.glFlush();
|
OpenGL.glFlush();
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glBindFramebuffer(OpenGL.FRAMEBUFFER_EXT, framebuffer);
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebuffer);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glViewport(0, 0, size.Width, size.Height);
|
OpenGL.glViewport(0, 0, size.Width, size.Height);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
@@ -107,7 +103,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
VerifyThreadAffinity();
|
VerifyThreadAffinity();
|
||||||
OpenGL.glFlush();
|
OpenGL.glFlush();
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glBindFramebuffer(OpenGL.FRAMEBUFFER_EXT, 0);
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, 0);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glViewport(cv[0], cv[1], cv[2], cv[3]);
|
OpenGL.glViewport(cv[0], cv[1], cv[2], cv[3]);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|||||||
@@ -24,11 +24,12 @@ namespace OpenRA.Platforms.Default
|
|||||||
Justification = "C-style naming is kept for consistency with the underlying native API.")]
|
Justification = "C-style naming is kept for consistency with the underlying native API.")]
|
||||||
internal static class OpenGL
|
internal static class OpenGL
|
||||||
{
|
{
|
||||||
|
[Flags]
|
||||||
public enum GLFeatures
|
public enum GLFeatures
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
GL2OrGreater = 1,
|
Core = 1,
|
||||||
FramebufferExt = 4,
|
GLES = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GLFeatures Features { get; private set; }
|
public static GLFeatures Features { get; private set; }
|
||||||
@@ -105,6 +106,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
public const int GL_DYNAMIC_DRAW = 0x88E8;
|
public const int GL_DYNAMIC_DRAW = 0x88E8;
|
||||||
|
|
||||||
public const int GL_TEXTURE0 = 0x84C0;
|
public const int GL_TEXTURE0 = 0x84C0;
|
||||||
|
public const int GL_DEPTH_COMPONENT16 = 0x81A5;
|
||||||
|
|
||||||
// OpenGL 2
|
// OpenGL 2
|
||||||
public const int GL_FRAGMENT_SHADER = 0x8B30;
|
public const int GL_FRAGMENT_SHADER = 0x8B30;
|
||||||
@@ -127,14 +129,17 @@ namespace OpenRA.Platforms.Default
|
|||||||
public const int GL_RENDERER = 0x1F01;
|
public const int GL_RENDERER = 0x1F01;
|
||||||
public const int GL_VERSION = 0x1F02;
|
public const int GL_VERSION = 0x1F02;
|
||||||
public const int GL_EXTENSIONS = 0x1F03;
|
public const int GL_EXTENSIONS = 0x1F03;
|
||||||
|
public const int GL_NUM_EXTENSIONS = 0x821D;
|
||||||
|
|
||||||
public const int GL_SHADING_LANGUAGE_VERSION = 0x8B8C;
|
public const int GL_SHADING_LANGUAGE_VERSION = 0x8B8C;
|
||||||
|
|
||||||
// Framebuffers
|
// Framebuffers
|
||||||
public const int FRAMEBUFFER_EXT = 0x8D40;
|
public const int GL_FRAMEBUFFER = 0x8D40;
|
||||||
public const int RENDERBUFFER_EXT = 0x8D41;
|
public const int GL_RENDERBUFFER = 0x8D41;
|
||||||
public const int COLOR_ATTACHMENT0_EXT = 0x8CE0;
|
public const int GL_COLOR_ATTACHMENT0 = 0x8CE0;
|
||||||
public const int DEPTH_ATTACHMENT_EXT = 0x8D00;
|
public const int GL_DEPTH_ATTACHMENT = 0x8D00;
|
||||||
public const int FRAMEBUFFER_COMPLETE_EXT = 0x8CD5;
|
public const int GL_FRAMEBUFFER_COMPLETE = 0x8CD5;
|
||||||
|
public const int GL_FRAMEBUFFER_BINDING = 0x8CA6;
|
||||||
|
|
||||||
public delegate void Flush();
|
public delegate void Flush();
|
||||||
public static Flush glFlush { get; private set; }
|
public static Flush glFlush { get; private set; }
|
||||||
@@ -162,7 +167,18 @@ namespace OpenRA.Platforms.Default
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe delegate int GetIntegerv(int pname, int* param);
|
delegate IntPtr GetStringi(int name, uint index);
|
||||||
|
static GetStringi glGetStringiInternal;
|
||||||
|
|
||||||
|
public static string glGetStringi(int name, uint index)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
return new string((sbyte*)glGetStringiInternal(name, index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public unsafe delegate int GetIntegerv(int pname, out int param);
|
||||||
public static GetIntegerv glGetIntegerv { get; private set; }
|
public static GetIntegerv glGetIntegerv { get; private set; }
|
||||||
|
|
||||||
public delegate void Finish();
|
public delegate void Finish();
|
||||||
@@ -241,6 +257,12 @@ namespace OpenRA.Platforms.Default
|
|||||||
public delegate void BindBuffer(int target, uint buffer);
|
public delegate void BindBuffer(int target, uint buffer);
|
||||||
public static BindBuffer glBindBuffer { get; private set; }
|
public static BindBuffer glBindBuffer { get; private set; }
|
||||||
|
|
||||||
|
public delegate void GenVertexArrays(int n, out uint buffers);
|
||||||
|
public static GenVertexArrays glGenVertexArrays { get; private set; }
|
||||||
|
|
||||||
|
public delegate void BindVertexArray(uint buffer);
|
||||||
|
public static BindVertexArray glBindVertexArray { get; private set; }
|
||||||
|
|
||||||
public delegate void BufferData(int target, IntPtr size, IntPtr data, int usage);
|
public delegate void BufferData(int target, IntPtr size, IntPtr data, int usage);
|
||||||
public static BufferData glBufferData { get; private set; }
|
public static BufferData glBufferData { get; private set; }
|
||||||
|
|
||||||
@@ -253,6 +275,9 @@ namespace OpenRA.Platforms.Default
|
|||||||
public delegate void BindAttribLocation(uint program, int index, string name);
|
public delegate void BindAttribLocation(uint program, int index, string name);
|
||||||
public static BindAttribLocation glBindAttribLocation { get; private set; }
|
public static BindAttribLocation glBindAttribLocation { get; private set; }
|
||||||
|
|
||||||
|
public delegate void BindFragDataLocation(uint program, int colorNumber, string name);
|
||||||
|
public static BindFragDataLocation glBindFragDataLocation { get; private set; }
|
||||||
|
|
||||||
public delegate void VertexAttribPointer(int index, int size, int type, bool normalized,
|
public delegate void VertexAttribPointer(int index, int size, int type, bool normalized,
|
||||||
int stride, IntPtr pointer);
|
int stride, IntPtr pointer);
|
||||||
public static VertexAttribPointer glVertexAttribPointer { get; private set; }
|
public static VertexAttribPointer glVertexAttribPointer { get; private set; }
|
||||||
@@ -361,6 +386,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
{
|
{
|
||||||
glGetError = Bind<GetError>("glGetError");
|
glGetError = Bind<GetError>("glGetError");
|
||||||
glGetStringInternal = Bind<GetString>("glGetString");
|
glGetStringInternal = Bind<GetString>("glGetString");
|
||||||
|
glGetStringiInternal = Bind<GetStringi>("glGetStringi");
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
@@ -368,7 +394,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
}
|
}
|
||||||
|
|
||||||
DetectGLFeatures();
|
DetectGLFeatures();
|
||||||
if (!Features.HasFlag(GLFeatures.GL2OrGreater) || !Features.HasFlag(GLFeatures.FramebufferExt))
|
if (!Features.HasFlag(GLFeatures.Core))
|
||||||
{
|
{
|
||||||
WriteGraphicsLog("Unsupported OpenGL version: " + glGetString(GL_VERSION));
|
WriteGraphicsLog("Unsupported OpenGL version: " + glGetString(GL_VERSION));
|
||||||
throw new InvalidProgramException("OpenGL Version Error: See graphics.log for details.");
|
throw new InvalidProgramException("OpenGL Version Error: See graphics.log for details.");
|
||||||
@@ -411,7 +437,10 @@ namespace OpenRA.Platforms.Default
|
|||||||
glBufferData = Bind<BufferData>("glBufferData");
|
glBufferData = Bind<BufferData>("glBufferData");
|
||||||
glBufferSubData = Bind<BufferSubData>("glBufferSubData");
|
glBufferSubData = Bind<BufferSubData>("glBufferSubData");
|
||||||
glDeleteBuffers = Bind<DeleteBuffers>("glDeleteBuffers");
|
glDeleteBuffers = Bind<DeleteBuffers>("glDeleteBuffers");
|
||||||
|
glGenVertexArrays = Bind<GenVertexArrays>("glGenVertexArrays");
|
||||||
|
glBindVertexArray = Bind<BindVertexArray>("glBindVertexArray");
|
||||||
glBindAttribLocation = Bind<BindAttribLocation>("glBindAttribLocation");
|
glBindAttribLocation = Bind<BindAttribLocation>("glBindAttribLocation");
|
||||||
|
glBindFragDataLocation = Bind<BindFragDataLocation>("glBindFragDataLocation");
|
||||||
glVertexAttribPointer = Bind<VertexAttribPointer>("glVertexAttribPointer");
|
glVertexAttribPointer = Bind<VertexAttribPointer>("glVertexAttribPointer");
|
||||||
glEnableVertexAttribArray = Bind<EnableVertexAttribArray>("glEnableVertexAttribArray");
|
glEnableVertexAttribArray = Bind<EnableVertexAttribArray>("glEnableVertexAttribArray");
|
||||||
glDisableVertexAttribArray = Bind<DisableVertexAttribArray>("glDisableVertexAttribArray");
|
glDisableVertexAttribArray = Bind<DisableVertexAttribArray>("glDisableVertexAttribArray");
|
||||||
@@ -433,16 +462,16 @@ namespace OpenRA.Platforms.Default
|
|||||||
glGetTexImage = Bind<GetTexImage>("glGetTexImage");
|
glGetTexImage = Bind<GetTexImage>("glGetTexImage");
|
||||||
glTexParameteri = Bind<TexParameteri>("glTexParameteri");
|
glTexParameteri = Bind<TexParameteri>("glTexParameteri");
|
||||||
glTexParameterf = Bind<TexParameterf>("glTexParameterf");
|
glTexParameterf = Bind<TexParameterf>("glTexParameterf");
|
||||||
glGenFramebuffers = Bind<GenFramebuffers>("glGenFramebuffersEXT");
|
glGenFramebuffers = Bind<GenFramebuffers>("glGenFramebuffers");
|
||||||
glBindFramebuffer = Bind<BindFramebuffer>("glBindFramebufferEXT");
|
glBindFramebuffer = Bind<BindFramebuffer>("glBindFramebuffer");
|
||||||
glFramebufferTexture2D = Bind<FramebufferTexture2D>("glFramebufferTexture2DEXT");
|
glFramebufferTexture2D = Bind<FramebufferTexture2D>("glFramebufferTexture2D");
|
||||||
glDeleteFramebuffers = Bind<DeleteFramebuffers>("glDeleteFramebuffersEXT");
|
glDeleteFramebuffers = Bind<DeleteFramebuffers>("glDeleteFramebuffers");
|
||||||
glGenRenderbuffers = Bind<GenRenderbuffers>("glGenRenderbuffersEXT");
|
glGenRenderbuffers = Bind<GenRenderbuffers>("glGenRenderbuffers");
|
||||||
glBindRenderbuffer = Bind<BindRenderbuffer>("glBindRenderbufferEXT");
|
glBindRenderbuffer = Bind<BindRenderbuffer>("glBindRenderbuffer");
|
||||||
glRenderbufferStorage = Bind<RenderbufferStorage>("glRenderbufferStorageEXT");
|
glRenderbufferStorage = Bind<RenderbufferStorage>("glRenderbufferStorage");
|
||||||
glDeleteRenderbuffers = Bind<DeleteRenderbuffers>("glDeleteRenderbuffersEXT");
|
glDeleteRenderbuffers = Bind<DeleteRenderbuffers>("glDeleteRenderbuffers");
|
||||||
glFramebufferRenderbuffer = Bind<FramebufferRenderbuffer>("glFramebufferRenderbufferEXT");
|
glFramebufferRenderbuffer = Bind<FramebufferRenderbuffer>("glFramebufferRenderbuffer");
|
||||||
glCheckFramebufferStatus = Bind<CheckFramebufferStatus>("glCheckFramebufferStatusEXT");
|
glCheckFramebufferStatus = Bind<CheckFramebufferStatus>("glCheckFramebufferStatus");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -461,22 +490,24 @@ namespace OpenRA.Platforms.Default
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Version = glGetString(GL_VERSION);
|
Version = glGetString(GL_VERSION);
|
||||||
var version = Version.Contains(" ") ? Version.Split(' ')[0].Split('.') : Version.Split('.');
|
|
||||||
|
|
||||||
var major = 0;
|
var major = 0;
|
||||||
if (version.Length > 0)
|
|
||||||
int.TryParse(version[0], out major);
|
|
||||||
|
|
||||||
var minor = 0;
|
var minor = 0;
|
||||||
if (version.Length > 1)
|
|
||||||
int.TryParse(version[1], out minor);
|
|
||||||
|
|
||||||
if (major >= 2 && minor >= 0)
|
// Assume that the first numeric token corresponds to the GL version
|
||||||
Features |= GLFeatures.GL2OrGreater;
|
foreach (var t in Version.Split())
|
||||||
|
{
|
||||||
|
var split = t.Split('.');
|
||||||
|
if (split.Length >= 2 && int.TryParse(split[0], out major) && int.TryParse(split[1], out minor))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
var hasFramebufferExt = SDL.SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object") == SDL.SDL_bool.SDL_TRUE;
|
// Core features are defined as the shared feature set of GL 3.2 and (GLES 3 + BGRA extension)
|
||||||
if (hasFramebufferExt)
|
var hasBGRA = SDL.SDL_GL_ExtensionSupported("GL_EXT_texture_format_BGRA8888") == SDL.SDL_bool.SDL_TRUE;
|
||||||
Features |= GLFeatures.FramebufferExt;
|
if (Version.Contains(" ES") && hasBGRA && major >= 3)
|
||||||
|
Features = GLFeatures.Core | GLFeatures.GLES;
|
||||||
|
else if (major > 3 || (major == 3 && minor >= 2))
|
||||||
|
Features = GLFeatures.Core;
|
||||||
}
|
}
|
||||||
catch (Exception) { }
|
catch (Exception) { }
|
||||||
}
|
}
|
||||||
@@ -516,7 +547,11 @@ namespace OpenRA.Platforms.Default
|
|||||||
Log.Write("graphics", "GL Version: {0}", glGetString(GL_VERSION));
|
Log.Write("graphics", "GL Version: {0}", glGetString(GL_VERSION));
|
||||||
Log.Write("graphics", "Shader Version: {0}", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
Log.Write("graphics", "Shader Version: {0}", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||||
Log.Write("graphics", "Available extensions:");
|
Log.Write("graphics", "Available extensions:");
|
||||||
Log.Write("graphics", glGetString(GL_EXTENSIONS));
|
|
||||||
|
int extensionCount;
|
||||||
|
glGetIntegerv(GL_NUM_EXTENSIONS, out extensionCount);
|
||||||
|
for (var i = 0; i < extensionCount; i++)
|
||||||
|
Log.Write("graphics", glGetStringi(GL_EXTENSIONS, (uint)i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,13 @@ namespace OpenRA.Platforms.Default
|
|||||||
|
|
||||||
OpenGL.Initialize();
|
OpenGL.Initialize();
|
||||||
|
|
||||||
|
uint vao;
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
OpenGL.glGenVertexArrays(1, out vao);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
OpenGL.glBindVertexArray(vao);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex);
|
OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex);
|
OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex);
|
||||||
|
|||||||
@@ -75,8 +75,6 @@ namespace OpenRA.Platforms.Default
|
|||||||
|
|
||||||
public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int batchSize)
|
public Sdl2PlatformWindow(Size requestWindowSize, WindowMode windowMode, int batchSize)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Using SDL 2 with OpenGL renderer");
|
|
||||||
|
|
||||||
// Lock the Window/Surface properties until initialization is complete
|
// Lock the Window/Surface properties until initialization is complete
|
||||||
lock (syncObject)
|
lock (syncObject)
|
||||||
{
|
{
|
||||||
@@ -93,6 +91,25 @@ namespace OpenRA.Platforms.Default
|
|||||||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
|
||||||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, 0);
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, 0);
|
||||||
|
|
||||||
|
// Decide between OpenGL and OpenGL ES rendering
|
||||||
|
// Test whether we can use the preferred renderer and fall back to the other if that fails
|
||||||
|
// If neither works we will throw a graphics error later when trying to create the real window
|
||||||
|
bool useGLES;
|
||||||
|
if (Game.Settings.Graphics.PreferGLES)
|
||||||
|
useGLES = CanCreateGLWindow(3, 0, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES);
|
||||||
|
else
|
||||||
|
useGLES = !CanCreateGLWindow(3, 2, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
|
||||||
|
var glMajor = 3;
|
||||||
|
var glMinor = useGLES ? 0 : 2;
|
||||||
|
var glProfile = useGLES ? SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES : SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE;
|
||||||
|
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, glMajor);
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, glMinor);
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)glProfile);
|
||||||
|
|
||||||
|
Console.WriteLine("Using SDL 2 with OpenGL{0} renderer", useGLES ? " ES" : "");
|
||||||
|
|
||||||
SDL.SDL_DisplayMode display;
|
SDL.SDL_DisplayMode display;
|
||||||
SDL.SDL_GetCurrentDisplayMode(0, out display);
|
SDL.SDL_GetCurrentDisplayMode(0, out display);
|
||||||
|
|
||||||
@@ -366,5 +383,33 @@ namespace OpenRA.Platforms.Default
|
|||||||
VerifyThreadAffinity();
|
VerifyThreadAffinity();
|
||||||
return input.SetClipboardText(text);
|
return input.SetClipboardText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool CanCreateGLWindow(int major, int minor, SDL.SDL_GLprofile profile)
|
||||||
|
{
|
||||||
|
// Implementation inspired by TestIndividualGLVersion from Veldrid
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, major);
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, minor);
|
||||||
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)profile);
|
||||||
|
|
||||||
|
var flags = SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL;
|
||||||
|
var window = SDL.SDL_CreateWindow("", 0, 0, 1, 1, flags);
|
||||||
|
if (window == IntPtr.Zero || !string.IsNullOrEmpty(SDL.SDL_GetError()))
|
||||||
|
{
|
||||||
|
SDL.SDL_ClearError();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var context = SDL.SDL_GL_CreateContext(window);
|
||||||
|
if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0)
|
||||||
|
{
|
||||||
|
SDL.SDL_ClearError();
|
||||||
|
SDL.SDL_DestroyWindow(window);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL.SDL_GL_DeleteContext(context);
|
||||||
|
SDL.SDL_DestroyWindow(window);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ namespace OpenRA.Platforms.Default
|
|||||||
var filename = Path.Combine(Platform.GameDir, "glsl", name + "." + ext);
|
var filename = Path.Combine(Platform.GameDir, "glsl", name + "." + ext);
|
||||||
var code = File.ReadAllText(filename);
|
var code = File.ReadAllText(filename);
|
||||||
|
|
||||||
|
var version = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? "300 es" : "140";
|
||||||
|
code = code.Replace("{VERSION}", version);
|
||||||
|
|
||||||
var shader = OpenGL.glCreateShader(type);
|
var shader = OpenGL.glCreateShader(type);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
unsafe
|
unsafe
|
||||||
@@ -77,6 +80,9 @@ namespace OpenRA.Platforms.Default
|
|||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glBindAttribLocation(program, TexMetadataAttributeIndex, "aVertexTexMetadata");
|
OpenGL.glBindAttribLocation(program, TexMetadataAttributeIndex, "aVertexTexMetadata");
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
OpenGL.glBindFragDataLocation(program, 0, "fragColor");
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
OpenGL.glAttachShader(program, vertexShader);
|
OpenGL.glAttachShader(program, vertexShader);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
OpenGL.glAttachShader(program, fragmentShader);
|
OpenGL.glAttachShader(program, fragmentShader);
|
||||||
@@ -135,6 +141,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
{
|
{
|
||||||
VerifyThreadAffinity();
|
VerifyThreadAffinity();
|
||||||
OpenGL.glUseProgram(program);
|
OpenGL.glUseProgram(program);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
// bind the textures
|
// bind the textures
|
||||||
foreach (var kv in textures)
|
foreach (var kv in textures)
|
||||||
|
|||||||
@@ -85,7 +85,8 @@ namespace OpenRA.Platforms.Default
|
|||||||
{
|
{
|
||||||
var intPtr = new IntPtr((void*)ptr);
|
var intPtr = new IntPtr((void*)ptr);
|
||||||
PrepareTexture();
|
PrepareTexture();
|
||||||
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, width, height,
|
var glInternalFormat = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
|
||||||
|
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, glInternalFormat, width, height,
|
||||||
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
|
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
}
|
}
|
||||||
@@ -109,7 +110,8 @@ namespace OpenRA.Platforms.Default
|
|||||||
{
|
{
|
||||||
var intPtr = new IntPtr((void*)ptr);
|
var intPtr = new IntPtr((void*)ptr);
|
||||||
PrepareTexture();
|
PrepareTexture();
|
||||||
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, width, height,
|
var glInternalFormat = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
|
||||||
|
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, glInternalFormat, width, height,
|
||||||
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
|
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
}
|
}
|
||||||
@@ -121,7 +123,37 @@ namespace OpenRA.Platforms.Default
|
|||||||
VerifyThreadAffinity();
|
VerifyThreadAffinity();
|
||||||
var data = new byte[4 * Size.Width * Size.Height];
|
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))
|
||||||
|
{
|
||||||
|
// Query the active framebuffer so we can restore it afterwards
|
||||||
|
int lastFramebuffer;
|
||||||
|
OpenGL.glGetIntegerv(OpenGL.GL_FRAMEBUFFER_BINDING, out lastFramebuffer);
|
||||||
|
|
||||||
|
uint framebuffer;
|
||||||
|
OpenGL.glGenFramebuffers(1, out framebuffer);
|
||||||
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebuffer);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
|
OpenGL.glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER, OpenGL.GL_COLOR_ATTACHMENT0, OpenGL.GL_TEXTURE_2D, texture, 0);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (byte* ptr = &data[0])
|
||||||
|
{
|
||||||
|
var intPtr = new IntPtr((void*)ptr);
|
||||||
|
OpenGL.glReadPixels(0, 0, Size.Width, Size.Height, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, intPtr);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGL.glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, (uint)lastFramebuffer);
|
||||||
|
OpenGL.glDeleteFramebuffers(1, ref framebuffer);
|
||||||
|
OpenGL.CheckGLError();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
|
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture);
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
@@ -134,6 +166,8 @@ namespace OpenRA.Platforms.Default
|
|||||||
}
|
}
|
||||||
|
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +179,8 @@ namespace OpenRA.Platforms.Default
|
|||||||
|
|
||||||
Size = new Size(width, height);
|
Size = new Size(width, height);
|
||||||
PrepareTexture();
|
PrepareTexture();
|
||||||
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, width, height,
|
var glInternalFormat = OpenGL.Features.HasFlag(OpenGL.GLFeatures.GLES) ? OpenGL.GL_BGRA : OpenGL.GL_RGBA8;
|
||||||
|
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, glInternalFormat, width, height,
|
||||||
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, IntPtr.Zero);
|
0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, IntPtr.Zero);
|
||||||
OpenGL.CheckGLError();
|
OpenGL.CheckGLError();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
#version {VERSION}
|
||||||
|
#ifdef GL_ES
|
||||||
|
precision mediump float;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
in vec4 vColor;
|
||||||
|
|
||||||
uniform sampler2D Texture0;
|
uniform sampler2D Texture0;
|
||||||
uniform sampler2D Texture1;
|
uniform sampler2D Texture1;
|
||||||
uniform sampler2D Texture2;
|
uniform sampler2D Texture2;
|
||||||
@@ -10,15 +17,17 @@ uniform sampler2D Palette;
|
|||||||
uniform bool EnableDepthPreview;
|
uniform bool EnableDepthPreview;
|
||||||
uniform float DepthTextureScale;
|
uniform float DepthTextureScale;
|
||||||
|
|
||||||
varying vec4 vTexCoord;
|
in vec4 vTexCoord;
|
||||||
varying vec2 vTexMetadata;
|
in vec2 vTexMetadata;
|
||||||
varying vec4 vChannelMask;
|
in vec4 vChannelMask;
|
||||||
varying vec4 vDepthMask;
|
in vec4 vDepthMask;
|
||||||
varying vec2 vTexSampler;
|
in vec2 vTexSampler;
|
||||||
|
|
||||||
varying vec4 vColorFraction;
|
in vec4 vColorFraction;
|
||||||
varying vec4 vRGBAFraction;
|
in vec4 vRGBAFraction;
|
||||||
varying vec4 vPalettedFraction;
|
in vec4 vPalettedFraction;
|
||||||
|
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
float jet_r(float x)
|
float jet_r(float x)
|
||||||
{
|
{
|
||||||
@@ -38,26 +47,26 @@ float jet_b(float x)
|
|||||||
vec4 Sample(float samplerIndex, vec2 pos)
|
vec4 Sample(float samplerIndex, vec2 pos)
|
||||||
{
|
{
|
||||||
if (samplerIndex < 0.5)
|
if (samplerIndex < 0.5)
|
||||||
return texture2D(Texture0, pos);
|
return texture(Texture0, pos);
|
||||||
else if (samplerIndex < 1.5)
|
else if (samplerIndex < 1.5)
|
||||||
return texture2D(Texture1, pos);
|
return texture(Texture1, pos);
|
||||||
else if (samplerIndex < 2.5)
|
else if (samplerIndex < 2.5)
|
||||||
return texture2D(Texture2, pos);
|
return texture(Texture2, pos);
|
||||||
else if (samplerIndex < 3.5)
|
else if (samplerIndex < 3.5)
|
||||||
return texture2D(Texture3, pos);
|
return texture(Texture3, pos);
|
||||||
else if (samplerIndex < 4.5)
|
else if (samplerIndex < 4.5)
|
||||||
return texture2D(Texture4, pos);
|
return texture(Texture4, pos);
|
||||||
else if (samplerIndex < 5.5)
|
else if (samplerIndex < 5.5)
|
||||||
return texture2D(Texture5, pos);
|
return texture(Texture5, pos);
|
||||||
|
|
||||||
return texture2D(Texture6, pos);
|
return texture(Texture6, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 x = Sample(vTexSampler.s, vTexCoord.st);
|
vec4 x = Sample(vTexSampler.s, vTexCoord.st);
|
||||||
vec2 p = vec2(dot(x, vChannelMask), vTexMetadata.s);
|
vec2 p = vec2(dot(x, vChannelMask), vTexMetadata.s);
|
||||||
vec4 c = vPalettedFraction * texture2D(Palette, p) + vRGBAFraction * x + vColorFraction * vTexCoord;
|
vec4 c = vPalettedFraction * texture(Palette, p) + vRGBAFraction * x + vColorFraction * vTexCoord;
|
||||||
|
|
||||||
// Discard any transparent fragments (both color and depth)
|
// Discard any transparent fragments (both color and depth)
|
||||||
if (c.a == 0.0)
|
if (c.a == 0.0)
|
||||||
@@ -79,8 +88,8 @@ void main()
|
|||||||
float r = clamp(jet_r(x), 0.0, 1.0);
|
float r = clamp(jet_r(x), 0.0, 1.0);
|
||||||
float g = clamp(jet_g(x), 0.0, 1.0);
|
float g = clamp(jet_g(x), 0.0, 1.0);
|
||||||
float b = clamp(jet_b(x), 0.0, 1.0);
|
float b = clamp(jet_b(x), 0.0, 1.0);
|
||||||
gl_FragColor = vec4(r, g, b, 1.0);
|
fragColor = vec4(r, g, b, 1.0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
gl_FragColor = c;
|
fragColor = c;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
|
#version {VERSION}
|
||||||
|
|
||||||
uniform vec3 Scroll;
|
uniform vec3 Scroll;
|
||||||
uniform vec3 r1, r2;
|
uniform vec3 r1, r2;
|
||||||
|
|
||||||
attribute vec4 aVertexPosition;
|
in vec4 aVertexPosition;
|
||||||
attribute vec4 aVertexTexCoord;
|
in vec4 aVertexTexCoord;
|
||||||
attribute vec2 aVertexTexMetadata;
|
in vec2 aVertexTexMetadata;
|
||||||
|
|
||||||
varying vec4 vTexCoord;
|
out vec4 vTexCoord;
|
||||||
varying vec2 vTexMetadata;
|
out vec2 vTexMetadata;
|
||||||
varying vec4 vChannelMask;
|
out vec4 vChannelMask;
|
||||||
varying vec4 vDepthMask;
|
out vec4 vDepthMask;
|
||||||
varying vec2 vTexSampler;
|
out vec2 vTexSampler;
|
||||||
|
|
||||||
varying vec4 vColorFraction;
|
out vec4 vColorFraction;
|
||||||
varying vec4 vRGBAFraction;
|
out vec4 vRGBAFraction;
|
||||||
varying vec4 vPalettedFraction;
|
out vec4 vPalettedFraction;
|
||||||
|
|
||||||
vec4 UnpackChannelAttributes(float x)
|
vec4 UnpackChannelAttributes(float x)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,22 +1,28 @@
|
|||||||
|
#version {VERSION}
|
||||||
|
#ifdef GL_ES
|
||||||
|
precision mediump float;
|
||||||
|
#endif
|
||||||
|
|
||||||
uniform sampler2D Palette, DiffuseTexture;
|
uniform sampler2D Palette, DiffuseTexture;
|
||||||
uniform vec2 PaletteRows;
|
uniform vec2 PaletteRows;
|
||||||
|
|
||||||
uniform vec4 LightDirection;
|
uniform vec4 LightDirection;
|
||||||
uniform vec3 AmbientLight, DiffuseLight;
|
uniform vec3 AmbientLight, DiffuseLight;
|
||||||
|
|
||||||
varying vec4 vTexCoord;
|
in vec4 vTexCoord;
|
||||||
varying vec4 vChannelMask;
|
in vec4 vChannelMask;
|
||||||
varying vec4 vNormalsMask;
|
in vec4 vNormalsMask;
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 x = texture2D(DiffuseTexture, vTexCoord.st);
|
vec4 x = texture(DiffuseTexture, vTexCoord.st);
|
||||||
vec4 color = texture2D(Palette, vec2(dot(x, vChannelMask), PaletteRows.x));
|
vec4 color = texture(Palette, vec2(dot(x, vChannelMask), PaletteRows.x));
|
||||||
if (color.a < 0.01)
|
if (color.a < 0.01)
|
||||||
discard;
|
discard;
|
||||||
|
|
||||||
vec4 y = texture2D(DiffuseTexture, vTexCoord.pq);
|
vec4 y = texture(DiffuseTexture, vTexCoord.pq);
|
||||||
vec4 normal = (2.0 * texture2D(Palette, vec2(dot(y, vNormalsMask), PaletteRows.y)) - 1.0);
|
vec4 normal = (2.0 * texture(Palette, vec2(dot(y, vNormalsMask), PaletteRows.y)) - 1.0);
|
||||||
vec3 intensity = AmbientLight + DiffuseLight * max(dot(normal, LightDirection), 0.0);
|
vec3 intensity = AmbientLight + DiffuseLight * max(dot(normal, LightDirection), 0.0);
|
||||||
gl_FragColor = vec4(intensity * color.rgb, color.a);
|
fragColor = vec4(intensity * color.rgb, color.a);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
|
#version {VERSION}
|
||||||
|
|
||||||
uniform mat4 View;
|
uniform mat4 View;
|
||||||
uniform mat4 TransformMatrix;
|
uniform mat4 TransformMatrix;
|
||||||
|
|
||||||
attribute vec4 aVertexPosition;
|
in vec4 aVertexPosition;
|
||||||
attribute vec4 aVertexTexCoord;
|
in vec4 aVertexTexCoord;
|
||||||
attribute vec2 aVertexTexMetadata;
|
in vec2 aVertexTexMetadata;
|
||||||
varying vec4 vTexCoord;
|
out vec4 vTexCoord;
|
||||||
varying vec4 vChannelMask;
|
out vec4 vChannelMask;
|
||||||
varying vec4 vNormalsMask;
|
out vec4 vNormalsMask;
|
||||||
|
|
||||||
vec4 DecodeMask(float x)
|
vec4 DecodeMask(float x)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user