Extract vertex attributes
This commit is contained in:
committed by
Matthias Mailänder
parent
0a90c2a95e
commit
26b6118f50
@@ -50,15 +50,6 @@ namespace OpenRA.Platforms.Default
|
||||
OpenGL.glBindVertexArray(vao);
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
|
||||
OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glEnableVertexAttribArray(Shader.TintAttributeIndex);
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
|
||||
public IVertexBuffer<T> CreateVertexBuffer<T>(int size) where T : struct
|
||||
@@ -103,10 +94,10 @@ namespace OpenRA.Platforms.Default
|
||||
return new FrameBuffer(s, texture, clearColor);
|
||||
}
|
||||
|
||||
public IShader CreateShader(string name)
|
||||
public IShader CreateShader(IShaderBindings bindings)
|
||||
{
|
||||
VerifyThreadAffinity();
|
||||
return new Shader(name);
|
||||
return new Shader(bindings);
|
||||
}
|
||||
|
||||
public void EnableScissor(int x, int y, int width, int height)
|
||||
|
||||
@@ -18,24 +18,16 @@ namespace OpenRA.Platforms.Default
|
||||
{
|
||||
sealed class Shader : ThreadAffine, IShader
|
||||
{
|
||||
public const int VertexPosAttributeIndex = 0;
|
||||
public const int TexCoordAttributeIndex = 1;
|
||||
public const int TexMetadataAttributeIndex = 2;
|
||||
public const int TintAttributeIndex = 3;
|
||||
|
||||
readonly Dictionary<string, int> samplers = new();
|
||||
readonly Dictionary<int, int> legacySizeUniforms = new();
|
||||
readonly Dictionary<string, int> uniformCache = new();
|
||||
readonly Dictionary<int, ITexture> textures = new();
|
||||
readonly Queue<int> unbindTextures = new();
|
||||
readonly IShaderBindings bindings;
|
||||
readonly uint program;
|
||||
|
||||
static uint CompileShaderObject(int type, string name)
|
||||
static uint CompileShaderObject(int type, string code, string name)
|
||||
{
|
||||
var ext = type == OpenGL.GL_VERTEX_SHADER ? "vert" : "frag";
|
||||
var filename = Path.Combine(Platform.EngineDir, "glsl", name + "." + ext);
|
||||
var code = File.ReadAllText(filename);
|
||||
|
||||
var version = OpenGL.Profile == GLProfile.Embedded ? "300 es" :
|
||||
OpenGL.Profile == GLProfile.Legacy ? "120" : "140";
|
||||
|
||||
@@ -61,29 +53,29 @@ namespace OpenRA.Platforms.Default
|
||||
OpenGL.glGetShaderInfoLog(shader, len, out _, log);
|
||||
|
||||
Log.Write("graphics", $"GL Info Log:\n{log}");
|
||||
throw new InvalidProgramException($"Compile error in shader object '{filename}'");
|
||||
throw new InvalidProgramException($"Compile error in shader object {name}.");
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
public Shader(string name)
|
||||
public Shader(IShaderBindings bindings)
|
||||
{
|
||||
var vertexShader = CompileShaderObject(OpenGL.GL_VERTEX_SHADER, name);
|
||||
var fragmentShader = CompileShaderObject(OpenGL.GL_FRAGMENT_SHADER, name);
|
||||
var vertexShader = CompileShaderObject(OpenGL.GL_VERTEX_SHADER, bindings.VertexShaderCode, bindings.VertexShaderName);
|
||||
var fragmentShader = CompileShaderObject(OpenGL.GL_FRAGMENT_SHADER, bindings.FragmentShaderCode, bindings.FragmentShaderName);
|
||||
|
||||
// Assemble program
|
||||
program = OpenGL.glCreateProgram();
|
||||
OpenGL.CheckGLError();
|
||||
|
||||
OpenGL.glBindAttribLocation(program, VertexPosAttributeIndex, "aVertexPosition");
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glBindAttribLocation(program, TexCoordAttributeIndex, "aVertexTexCoord");
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glBindAttribLocation(program, TexMetadataAttributeIndex, "aVertexTexMetadata");
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glBindAttribLocation(program, TintAttributeIndex, "aVertexTint");
|
||||
OpenGL.CheckGLError();
|
||||
this.bindings = bindings;
|
||||
for (ushort i = 0; i < bindings.Attributes.Length; i++)
|
||||
{
|
||||
OpenGL.glEnableVertexAttribArray(i);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glBindAttribLocation(program, i, bindings.Attributes[i].Name);
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
|
||||
if (OpenGL.Profile == GLProfile.Modern)
|
||||
{
|
||||
@@ -107,7 +99,7 @@ namespace OpenRA.Platforms.Default
|
||||
var log = new StringBuilder(len);
|
||||
OpenGL.glGetProgramInfoLog(program, len, out _, log);
|
||||
Log.Write("graphics", $"GL Info Log:\n{log}");
|
||||
throw new InvalidProgramException($"Link error in shader program '{name}'");
|
||||
throw new InvalidProgramException($"Link error in shader program '{bindings.VertexShaderName}' and '{bindings.FragmentShaderName}'");
|
||||
}
|
||||
|
||||
OpenGL.glUseProgram(program);
|
||||
@@ -148,6 +140,16 @@ namespace OpenRA.Platforms.Default
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
for (ushort i = 0; i < bindings.Attributes.Length; i++)
|
||||
{
|
||||
var attribute = bindings.Attributes[i];
|
||||
OpenGL.glVertexAttribPointer(i, attribute.Components, OpenGL.GL_FLOAT, false, bindings.Stride, new IntPtr(attribute.Offset));
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
}
|
||||
|
||||
public void PrepareRender()
|
||||
{
|
||||
VerifyThreadAffinity();
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace OpenRA.Platforms.Default
|
||||
return new ThreadedFrameBuffer(this,
|
||||
context.CreateFrameBuffer(t.Item1, (ITextureInternal)CreateTexture(), t.Item2));
|
||||
};
|
||||
getCreateShader = name => new ThreadedShader(this, context.CreateShader((string)name));
|
||||
getCreateShader = bindings => new ThreadedShader(this, context.CreateShader((IShaderBindings)bindings));
|
||||
getCreateVertexBuffer =
|
||||
tuple =>
|
||||
{
|
||||
@@ -416,9 +416,9 @@ namespace OpenRA.Platforms.Default
|
||||
return Send(getCreateFrameBuffer, (s, clearColor));
|
||||
}
|
||||
|
||||
public IShader CreateShader(string name)
|
||||
public IShader CreateShader(IShaderBindings bindings)
|
||||
{
|
||||
return Send(getCreateShader, name);
|
||||
return Send(getCreateShader, bindings);
|
||||
}
|
||||
|
||||
public ITexture CreateTexture()
|
||||
@@ -742,10 +742,12 @@ namespace OpenRA.Platforms.Default
|
||||
readonly Action<object> setVec2;
|
||||
readonly Action<object> setVec3;
|
||||
readonly Action<object> setVec4;
|
||||
readonly Action bind;
|
||||
|
||||
public ThreadedShader(ThreadedGraphicsContext device, IShader shader)
|
||||
{
|
||||
this.device = device;
|
||||
bind = shader.Bind;
|
||||
prepareRender = shader.PrepareRender;
|
||||
setBool = tuple => { var t = ((string, bool))tuple; shader.SetBool(t.Item1, t.Item2); };
|
||||
setMatrix = tuple => { var t = ((string, float[]))tuple; shader.SetMatrix(t.Item1, t.Item2); };
|
||||
@@ -756,6 +758,11 @@ namespace OpenRA.Platforms.Default
|
||||
setVec4 = tuple => { var t = ((string, float, float, float))tuple; shader.SetVec(t.Item1, t.Item2, t.Item3, t.Item4); };
|
||||
}
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
device.Post(bind);
|
||||
}
|
||||
|
||||
public void PrepareRender()
|
||||
{
|
||||
device.Post(prepareRender);
|
||||
|
||||
@@ -89,15 +89,6 @@ namespace OpenRA.Platforms.Default
|
||||
{
|
||||
VerifyThreadAffinity();
|
||||
OpenGL.glBindBuffer(OpenGL.GL_ARRAY_BUFFER, buffer);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glVertexAttribPointer(Shader.VertexPosAttributeIndex, 3, OpenGL.GL_FLOAT, false, VertexSize, IntPtr.Zero);
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glVertexAttribPointer(Shader.TexCoordAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(12));
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28));
|
||||
OpenGL.CheckGLError();
|
||||
OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36));
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user