diff --git a/OpenRA.Renderer.Gl/Shader.cs b/OpenRA.Renderer.Gl/Shader.cs index a4d12fb7fe..8f39d77aac 100644 --- a/OpenRA.Renderer.Gl/Shader.cs +++ b/OpenRA.Renderer.Gl/Shader.cs @@ -23,6 +23,7 @@ namespace OpenRA.Renderer.Glsl { int program; readonly Dictionary samplers = new Dictionary(); + readonly Dictionary textures = new Dictionary(); public Shader(GraphicsDevice dev, string type) { @@ -85,7 +86,7 @@ namespace OpenRA.Renderer.Glsl Gl.glGetObjectParameterivARB( program, Gl.GL_ACTIVE_UNIFORMS, out numUniforms ); ErrorHandler.CheckGlError(); - int nextTexUnit = 1; + int nextTexUnit = 0; for( int i = 0 ; i < numUniforms ; i++ ) { int uLen, uSize, uType, loc; @@ -103,11 +104,22 @@ namespace OpenRA.Renderer.Glsl ++nextTexUnit; } } + } public void Render(Action a) { Gl.glUseProgramObjectARB(program); + + /* bind the textures */ + + foreach (var kv in textures) + { + Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB + kv.Key ); + Gl.glBindTexture( Gl.GL_TEXTURE_2D, ((Texture)kv.Value).texture ); + } + + /* configure blend state */ ErrorHandler.CheckGlError(); // Todo: Only enable alpha blending if we need it Gl.glEnable(Gl.GL_BLEND); @@ -123,18 +135,9 @@ namespace OpenRA.Renderer.Glsl public void SetValue(string name, ITexture t) { if( t == null ) return; - Gl.glUseProgramObjectARB(program); - ErrorHandler.CheckGlError(); - var texture = (Texture)t; int texUnit; if( samplers.TryGetValue( name, out texUnit ) ) - { - Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB + texUnit ); - ErrorHandler.CheckGlError(); - Gl.glBindTexture( Gl.GL_TEXTURE_2D, texture.texture ); - ErrorHandler.CheckGlError(); - Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB ); - } + textures[texUnit] = t; } public void SetValue(string name, float x, float y)