Extract vertex attributes
This commit is contained in:
committed by
Matthias Mailänder
parent
0a90c2a95e
commit
26b6118f50
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user