From be29ec53427a71b63ddfbe03c57a2a53882a3cd5 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 19 Dec 2015 23:57:52 +0000 Subject: [PATCH] Use explicit shader attributes. --- OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs | 4 ++-- OpenRA.Platforms.Default/Shader.cs | 9 +++++++++ OpenRA.Platforms.Default/VertexBuffer.cs | 4 ++-- glsl/color.frag | 4 +++- glsl/color.vert | 11 ++++++++--- glsl/rgba.frag | 4 +++- glsl/rgba.vert | 8 ++++++-- glsl/shp.frag | 14 +++++++------- glsl/shp.vert | 16 +++++++++------- glsl/vxl.frag | 12 ++++++------ glsl/vxl.vert | 16 +++++++++------- 11 files changed, 64 insertions(+), 38 deletions(-) diff --git a/OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs b/OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs index b7483e9f82..60dd8e72d3 100644 --- a/OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs +++ b/OpenRA.Platforms.Default/Sdl2GraphicsDevice.cs @@ -91,9 +91,9 @@ namespace OpenRA.Platforms.Default 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(); - GL.EnableClientState(ArrayCap.TextureCoordArray); + GL.EnableVertexAttribArray(Shader.TexCoordAttributeIndex); ErrorHandler.CheckGlError(); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); diff --git a/OpenRA.Platforms.Default/Shader.cs b/OpenRA.Platforms.Default/Shader.cs index 6e8653e1bd..78d2295c02 100644 --- a/OpenRA.Platforms.Default/Shader.cs +++ b/OpenRA.Platforms.Default/Shader.cs @@ -18,6 +18,9 @@ namespace OpenRA.Platforms.Default { class Shader : ThreadAffine, IShader { + public const int VertexPosAttributeIndex = 0; + public const int TexCoordAttributeIndex = 1; + readonly Dictionary samplers = new Dictionary(); readonly Dictionary textures = new Dictionary(); readonly int program; @@ -62,6 +65,12 @@ namespace OpenRA.Platforms.Default // Assemble program program = GL.CreateProgram(); ErrorHandler.CheckGlError(); + + GL.BindAttribLocation(program, VertexPosAttributeIndex, "aVertexPosition"); + ErrorHandler.CheckGlError(); + GL.BindAttribLocation(program, TexCoordAttributeIndex, "aVertexTexCoord"); + ErrorHandler.CheckGlError(); + GL.AttachShader(program, vertexShader); ErrorHandler.CheckGlError(); GL.AttachShader(program, fragmentShader); diff --git a/OpenRA.Platforms.Default/VertexBuffer.cs b/OpenRA.Platforms.Default/VertexBuffer.cs index 1126fff3d0..f3e44400e6 100644 --- a/OpenRA.Platforms.Default/VertexBuffer.cs +++ b/OpenRA.Platforms.Default/VertexBuffer.cs @@ -63,9 +63,9 @@ namespace OpenRA.Platforms.Default VerifyThreadAffinity(); GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); ErrorHandler.CheckGlError(); - GL.VertexPointer(3, VertexPointerType.Float, VertexSize, IntPtr.Zero); + GL.VertexAttribPointer(Shader.VertexPosAttributeIndex, 3, VertexAttribPointerType.Float, false, VertexSize, IntPtr.Zero); 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(); } diff --git a/glsl/color.frag b/glsl/color.frag index 3f45d895ff..88c1daa512 100644 --- a/glsl/color.frag +++ b/glsl/color.frag @@ -1,4 +1,6 @@ +varying vec4 vColor; + void main() { - gl_FragColor = gl_Color; + gl_FragColor = vColor; } \ No newline at end of file diff --git a/glsl/color.vert b/glsl/color.vert index e38ef97d14..ffc17d90e1 100644 --- a/glsl/color.vert +++ b/glsl/color.vert @@ -1,8 +1,13 @@ uniform vec2 Scroll; -uniform vec2 r1, r2; // matrix elements +uniform vec2 r1, r2; + +attribute vec4 aVertexPosition; +attribute vec4 aVertexTexCoord; +varying vec4 vColor; + 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_FrontColor = gl_MultiTexCoord0; + vColor = aVertexTexCoord; } diff --git a/glsl/rgba.frag b/glsl/rgba.frag index 7f52d6d4ac..b01e69da10 100644 --- a/glsl/rgba.frag +++ b/glsl/rgba.frag @@ -1,5 +1,7 @@ uniform sampler2D DiffuseTexture; +varying vec4 vTexCoord; + void main() { - gl_FragColor = texture2D(DiffuseTexture,gl_TexCoord[0].st); + gl_FragColor = texture2D(DiffuseTexture, vTexCoord.st); } \ No newline at end of file diff --git a/glsl/rgba.vert b/glsl/rgba.vert index 89ffea0e96..f5d8177526 100644 --- a/glsl/rgba.vert +++ b/glsl/rgba.vert @@ -1,9 +1,13 @@ uniform vec2 Scroll; uniform vec2 r1, r2; +attribute vec4 aVertexPosition; +attribute vec4 aVertexTexCoord; +varying vec4 vTexCoord; + 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_TexCoord[0] = gl_MultiTexCoord0; + vTexCoord = aVertexTexCoord; } diff --git a/glsl/shp.frag b/glsl/shp.frag index 43972d45b6..c7e1c1189f 100644 --- a/glsl/shp.frag +++ b/glsl/shp.frag @@ -2,23 +2,23 @@ uniform sampler2D DiffuseTexture, Palette; uniform bool EnableDepthPreview; -varying vec4 TexCoord; -varying vec4 ChannelMask; -varying vec4 DepthMask; +varying vec4 vTexCoord; +varying vec4 vChannelMask; +varying vec4 vDepthMask; void main() { - vec4 x = texture2D(DiffuseTexture, TexCoord.st); - vec2 p = vec2(dot(x, ChannelMask), TexCoord.p); + vec4 x = texture2D(DiffuseTexture, vTexCoord.st); + vec2 p = vec2(dot(x, vChannelMask), vTexCoord.p); vec4 c = texture2D(Palette, p); // Discard any transparent fragments (both color and depth) if (c.a == 0.0) 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); } else diff --git a/glsl/shp.vert b/glsl/shp.vert index 18fd603a77..c59498a96b 100644 --- a/glsl/shp.vert +++ b/glsl/shp.vert @@ -1,9 +1,11 @@ uniform vec2 Scroll; uniform vec2 r1,r2; // matrix elements -varying vec4 TexCoord; -varying vec4 ChannelMask; -varying vec4 DepthMask; +attribute vec4 aVertexPosition; +attribute vec4 aVertexTexCoord; +varying vec4 vTexCoord; +varying vec4 vChannelMask; +varying vec4 vDepthMask; vec4 DecodeChannelMask(float x) { @@ -34,9 +36,9 @@ vec4 DecodeDepthChannelMask(float x) 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); - TexCoord = gl_MultiTexCoord0; - ChannelMask = DecodeChannelMask(gl_MultiTexCoord0.w); - DepthMask = DecodeDepthChannelMask(gl_MultiTexCoord0.w); + vTexCoord = aVertexTexCoord; + vChannelMask = DecodeChannelMask(aVertexTexCoord.w); + vDepthMask = DecodeDepthChannelMask(aVertexTexCoord.w); } diff --git a/glsl/vxl.frag b/glsl/vxl.frag index c29828ea69..c9373d5fb1 100644 --- a/glsl/vxl.frag +++ b/glsl/vxl.frag @@ -4,18 +4,18 @@ uniform vec2 PaletteRows; uniform vec4 LightDirection; uniform vec3 AmbientLight, DiffuseLight; -varying vec4 TexCoord; -varying vec4 ChannelMask; -varying vec4 NormalsMask; +varying vec4 vTexCoord; +varying vec4 vChannelMask; +varying vec4 vNormalsMask; void main() { - vec4 x = texture2D(DiffuseTexture, TexCoord.st); - vec4 color = texture2D(Palette, vec2(dot(x, ChannelMask), PaletteRows.x)); + vec4 x = texture2D(DiffuseTexture, vTexCoord.st); + vec4 color = texture2D(Palette, vec2(dot(x, vChannelMask), PaletteRows.x)); if (color.a < 0.01) 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); gl_FragColor = vec4(intensity * color.rgb, color.a); } diff --git a/glsl/vxl.vert b/glsl/vxl.vert index e349fd4a2a..07a84997b2 100644 --- a/glsl/vxl.vert +++ b/glsl/vxl.vert @@ -1,9 +1,11 @@ uniform mat4 View; uniform mat4 TransformMatrix; -varying vec4 TexCoord; -varying vec4 ChannelMask; -varying vec4 NormalsMask; +attribute vec4 aVertexPosition; +attribute vec4 aVertexTexCoord; +varying vec4 vTexCoord; +varying vec4 vChannelMask; +varying vec4 vNormalsMask; vec4 DecodeMask(float x) { @@ -15,8 +17,8 @@ vec4 DecodeMask(float x) void main() { - gl_Position = View*TransformMatrix*gl_Vertex; - TexCoord = gl_MultiTexCoord0; - ChannelMask = DecodeMask(gl_MultiTexCoord0.z); - NormalsMask = DecodeMask(gl_MultiTexCoord0.w); + gl_Position = View*TransformMatrix*aVertexPosition; + vTexCoord = aVertexTexCoord; + vChannelMask = DecodeMask(aVertexTexCoord.z); + vNormalsMask = DecodeMask(aVertexTexCoord.w); }