Use explicit shader attributes.

This commit is contained in:
Paul Chote
2015-12-19 23:57:52 +00:00
parent 788def1c31
commit be29ec5342
11 changed files with 64 additions and 38 deletions

View File

@@ -91,9 +91,9 @@ namespace OpenRA.Platforms.Default
throw new InvalidProgramException("Missing OpenGL extension GL_EXT_framebuffer_object. See graphics.log for details."); throw new InvalidProgramException("Missing OpenGL extension GL_EXT_framebuffer_object. See graphics.log for details.");
} }
GL.EnableClientState(ArrayCap.VertexArray); GL.EnableVertexAttribArray(Shader.VertexPosAttributeIndex);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.EnableClientState(ArrayCap.TextureCoordArray); GL.EnableVertexAttribArray(Shader.TexCoordAttributeIndex);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE);

View File

@@ -18,6 +18,9 @@ namespace OpenRA.Platforms.Default
{ {
class Shader : ThreadAffine, IShader class Shader : ThreadAffine, IShader
{ {
public const int VertexPosAttributeIndex = 0;
public const int TexCoordAttributeIndex = 1;
readonly Dictionary<string, int> samplers = new Dictionary<string, int>(); readonly Dictionary<string, int> samplers = new Dictionary<string, int>();
readonly Dictionary<int, ITexture> textures = new Dictionary<int, ITexture>(); readonly Dictionary<int, ITexture> textures = new Dictionary<int, ITexture>();
readonly int program; readonly int program;
@@ -62,6 +65,12 @@ namespace OpenRA.Platforms.Default
// Assemble program // Assemble program
program = GL.CreateProgram(); program = GL.CreateProgram();
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.BindAttribLocation(program, VertexPosAttributeIndex, "aVertexPosition");
ErrorHandler.CheckGlError();
GL.BindAttribLocation(program, TexCoordAttributeIndex, "aVertexTexCoord");
ErrorHandler.CheckGlError();
GL.AttachShader(program, vertexShader); GL.AttachShader(program, vertexShader);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.AttachShader(program, fragmentShader); GL.AttachShader(program, fragmentShader);

View File

@@ -63,9 +63,9 @@ namespace OpenRA.Platforms.Default
VerifyThreadAffinity(); VerifyThreadAffinity();
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.VertexPointer(3, VertexPointerType.Float, VertexSize, IntPtr.Zero); GL.VertexAttribPointer(Shader.VertexPosAttributeIndex, 3, VertexAttribPointerType.Float, false, VertexSize, IntPtr.Zero);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.TexCoordPointer(4, TexCoordPointerType.Float, VertexSize, new IntPtr(12)); GL.VertexAttribPointer(Shader.TexCoordAttributeIndex, 4, VertexAttribPointerType.Float, false, VertexSize, new IntPtr(12));
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
} }

View File

@@ -1,4 +1,6 @@
varying vec4 vColor;
void main() void main()
{ {
gl_FragColor = gl_Color; gl_FragColor = vColor;
} }

View File

@@ -1,8 +1,13 @@
uniform vec2 Scroll; uniform vec2 Scroll;
uniform vec2 r1, r2; // matrix elements uniform vec2 r1, r2;
attribute vec4 aVertexPosition;
attribute vec4 aVertexTexCoord;
varying vec4 vColor;
void main() void main()
{ {
vec2 p = (gl_Vertex.xy - Scroll.xy)*r1 + r2; vec2 p = (aVertexPosition.xy - Scroll.xy)*r1 + r2;
gl_Position = vec4(p.x,p.y,0,1); gl_Position = vec4(p.x,p.y,0,1);
gl_FrontColor = gl_MultiTexCoord0; vColor = aVertexTexCoord;
} }

View File

@@ -1,5 +1,7 @@
uniform sampler2D DiffuseTexture; uniform sampler2D DiffuseTexture;
varying vec4 vTexCoord;
void main() void main()
{ {
gl_FragColor = texture2D(DiffuseTexture,gl_TexCoord[0].st); gl_FragColor = texture2D(DiffuseTexture, vTexCoord.st);
} }

View File

@@ -1,9 +1,13 @@
uniform vec2 Scroll; uniform vec2 Scroll;
uniform vec2 r1, r2; uniform vec2 r1, r2;
attribute vec4 aVertexPosition;
attribute vec4 aVertexTexCoord;
varying vec4 vTexCoord;
void main() void main()
{ {
vec2 p = (gl_Vertex.xy - Scroll.xy)*r1 + r2; vec2 p = (aVertexPosition.xy - Scroll.xy)*r1 + r2;
gl_Position = vec4(p.x,p.y,0,1); gl_Position = vec4(p.x,p.y,0,1);
gl_TexCoord[0] = gl_MultiTexCoord0; vTexCoord = aVertexTexCoord;
} }

View File

@@ -2,23 +2,23 @@ uniform sampler2D DiffuseTexture, Palette;
uniform bool EnableDepthPreview; uniform bool EnableDepthPreview;
varying vec4 TexCoord; varying vec4 vTexCoord;
varying vec4 ChannelMask; varying vec4 vChannelMask;
varying vec4 DepthMask; varying vec4 vDepthMask;
void main() void main()
{ {
vec4 x = texture2D(DiffuseTexture, TexCoord.st); vec4 x = texture2D(DiffuseTexture, vTexCoord.st);
vec2 p = vec2(dot(x, ChannelMask), TexCoord.p); vec2 p = vec2(dot(x, vChannelMask), vTexCoord.p);
vec4 c = texture2D(Palette, p); vec4 c = texture2D(Palette, p);
// 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)
discard; discard;
if (EnableDepthPreview && length(DepthMask) > 0.0) if (EnableDepthPreview && length(vDepthMask) > 0.0)
{ {
float depth = dot(x, DepthMask); float depth = dot(x, vDepthMask);
gl_FragColor = vec4(depth, depth, depth, 1); gl_FragColor = vec4(depth, depth, depth, 1);
} }
else else

View File

@@ -1,9 +1,11 @@
uniform vec2 Scroll; uniform vec2 Scroll;
uniform vec2 r1,r2; // matrix elements uniform vec2 r1,r2; // matrix elements
varying vec4 TexCoord; attribute vec4 aVertexPosition;
varying vec4 ChannelMask; attribute vec4 aVertexTexCoord;
varying vec4 DepthMask; varying vec4 vTexCoord;
varying vec4 vChannelMask;
varying vec4 vDepthMask;
vec4 DecodeChannelMask(float x) vec4 DecodeChannelMask(float x)
{ {
@@ -34,9 +36,9 @@ vec4 DecodeDepthChannelMask(float x)
void main() void main()
{ {
vec2 p = (gl_Vertex.xy - Scroll.xy) * r1 + r2; vec2 p = (aVertexPosition.xy - Scroll.xy) * r1 + r2;
gl_Position = vec4(p.x,p.y,0,1); gl_Position = vec4(p.x,p.y,0,1);
TexCoord = gl_MultiTexCoord0; vTexCoord = aVertexTexCoord;
ChannelMask = DecodeChannelMask(gl_MultiTexCoord0.w); vChannelMask = DecodeChannelMask(aVertexTexCoord.w);
DepthMask = DecodeDepthChannelMask(gl_MultiTexCoord0.w); vDepthMask = DecodeDepthChannelMask(aVertexTexCoord.w);
} }

View File

@@ -4,18 +4,18 @@ uniform vec2 PaletteRows;
uniform vec4 LightDirection; uniform vec4 LightDirection;
uniform vec3 AmbientLight, DiffuseLight; uniform vec3 AmbientLight, DiffuseLight;
varying vec4 TexCoord; varying vec4 vTexCoord;
varying vec4 ChannelMask; varying vec4 vChannelMask;
varying vec4 NormalsMask; varying vec4 vNormalsMask;
void main() void main()
{ {
vec4 x = texture2D(DiffuseTexture, TexCoord.st); vec4 x = texture2D(DiffuseTexture, vTexCoord.st);
vec4 color = texture2D(Palette, vec2(dot(x, ChannelMask), PaletteRows.x)); vec4 color = texture2D(Palette, vec2(dot(x, vChannelMask), PaletteRows.x));
if (color.a < 0.01) if (color.a < 0.01)
discard; discard;
vec4 normal = (2.0 * texture2D(Palette, vec2(dot(x, NormalsMask), PaletteRows.y)) - 1.0); vec4 normal = (2.0 * texture2D(Palette, vec2(dot(x, 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); gl_FragColor = vec4(intensity * color.rgb, color.a);
} }

View File

@@ -1,9 +1,11 @@
uniform mat4 View; uniform mat4 View;
uniform mat4 TransformMatrix; uniform mat4 TransformMatrix;
varying vec4 TexCoord; attribute vec4 aVertexPosition;
varying vec4 ChannelMask; attribute vec4 aVertexTexCoord;
varying vec4 NormalsMask; varying vec4 vTexCoord;
varying vec4 vChannelMask;
varying vec4 vNormalsMask;
vec4 DecodeMask(float x) vec4 DecodeMask(float x)
{ {
@@ -15,8 +17,8 @@ vec4 DecodeMask(float x)
void main() void main()
{ {
gl_Position = View*TransformMatrix*gl_Vertex; gl_Position = View*TransformMatrix*aVertexPosition;
TexCoord = gl_MultiTexCoord0; vTexCoord = aVertexTexCoord;
ChannelMask = DecodeMask(gl_MultiTexCoord0.z); vChannelMask = DecodeMask(aVertexTexCoord.z);
NormalsMask = DecodeMask(gl_MultiTexCoord0.w); vNormalsMask = DecodeMask(aVertexTexCoord.w);
} }