diff --git a/OpenRa.Game/Graphics/Renderer.cs b/OpenRa.Game/Graphics/Renderer.cs index 66ae9f65d5..68a87b7ff6 100644 --- a/OpenRa.Game/Graphics/Renderer.cs +++ b/OpenRa.Game/Graphics/Renderer.cs @@ -48,11 +48,19 @@ namespace OpenRa.Graphics device.Begin(); device.Clear(Color.Black); - SpriteShader.SetValue("Palette", PaletteTexture); - SpriteShader.SetValue("Scroll", scroll.X, scroll.Y); - SpriteShader.SetValue("r1", r1.X, r1.Y); - SpriteShader.SetValue("r2", r2.X, r2.Y); - SpriteShader.Commit(); + SetShaderParams( SpriteShader, r1, r2, scroll ); + SetShaderParams( LineShader, r1, r2, scroll ); + SetShaderParams( RgbaSpriteShader, r1, r2, scroll ); + SetShaderParams( WorldSpriteShader, r1, r2, scroll ); + } + + private void SetShaderParams( Shader s, float2 r1, float2 r2, float2 scroll ) + { + s.SetValue( "Palette", PaletteTexture ); + s.SetValue( "Scroll", scroll.X, scroll.Y ); + s.SetValue( "r1", r1.X, r1.Y ); + s.SetValue( "r2", r2.X, r2.Y ); + s.Commit(); } public void EndFrame() diff --git a/OpenRa.Game/Graphics/SpriteRenderer.cs b/OpenRa.Game/Graphics/SpriteRenderer.cs index 06bf302d2b..e4cf7958ce 100644 --- a/OpenRa.Game/Graphics/SpriteRenderer.cs +++ b/OpenRa.Game/Graphics/SpriteRenderer.cs @@ -37,6 +37,7 @@ namespace OpenRa.Graphics if (sprites > 0) { shader.Quality = quality; + shader.SetValue( "DiffuseTexture", currentSheet.Texture ); shader.Render(() => { vertexBuffer.SetData(vertices); diff --git a/OpenRa.Game/Graphics/TerrainRenderer.cs b/OpenRa.Game/Graphics/TerrainRenderer.cs index 2ff9fe440d..dd2828dfd3 100644 --- a/OpenRa.Game/Graphics/TerrainRenderer.cs +++ b/OpenRa.Game/Graphics/TerrainRenderer.cs @@ -79,6 +79,7 @@ namespace OpenRa.Graphics } renderer.SpriteShader.Quality = ShaderQuality.Low; + renderer.SpriteShader.SetValue( "DiffuseTexture", terrainSheet.Texture ); renderer.SpriteShader.Render(() => renderer.DrawBatch(vertexBuffer, indexBuffer, new Range(verticesPerRow * firstRow, verticesPerRow * lastRow), diff --git a/OpenRa.Gl/GraphicsDevice.cs b/OpenRa.Gl/GraphicsDevice.cs index 4f71653832..4024c7eb22 100644 --- a/OpenRa.Gl/GraphicsDevice.cs +++ b/OpenRa.Gl/GraphicsDevice.cs @@ -50,7 +50,7 @@ namespace OpenRa.GlRenderer Wgl.wglMakeCurrent(dc, rc); cgContext = Cg.cgCreateContext(); - //Cg.cgSetErrorCallback(CgErrorCallback); + Cg.cgSetErrorCallback(CgErrorCallback); CgGl.cgGLRegisterStates(cgContext); CgGl.cgGLSetManageTextureParameters(cgContext, true); @@ -59,20 +59,22 @@ namespace OpenRa.GlRenderer Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY); CheckGlError(); - Gl.glEnableClientState(Gl.GL_INDEX_ARRAY); + Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY); CheckGlError(); } - void CgErrorCallback() - { - var err = Cg.cgGetError(); - var str = Cg.cgGetErrorString(err); - throw new InvalidOperationException( - string.Format("CG Error: {0}: {1}", err, str)); - } + static Cg.CGerrorCallbackFuncDelegate CgErrorCallback = () => + { + var err = Cg.cgGetError(); + var str = Cg.cgGetErrorString( err ); + throw new InvalidOperationException( + string.Format( "CG Error: {0}: {1}", err, str ) ); + }; public void EnableScissor(int left, int top, int width, int height) { + if( width < 0 ) width = 0; + if( height < 0 ) height = 0; Gl.glScissor(left, top, width, height); CheckGlError(); Gl.glEnable(Gl.GL_SCISSOR_TEST); @@ -90,7 +92,7 @@ namespace OpenRa.GlRenderer public void Clear(Color c) { - Gl.glClearColor(1, 1, 1, 1); + Gl.glClearColor(0, 0, 0, 0); CheckGlError(); Gl.glClear(Gl.GL_COLOR_BUFFER_BIT); CheckGlError(); @@ -110,9 +112,20 @@ namespace OpenRa.GlRenderer public void DrawIndexedPrimitives(PrimitiveType pt, int numVerts, int numPrimitives) { - Gl.glDrawElements((int)pt, numPrimitives, Gl.GL_UNSIGNED_SHORT, IntPtr.Zero); + Gl.glDrawElements((int)pt, numPrimitives * IndicesPerPrimitive( pt ), Gl.GL_UNSIGNED_SHORT, IntPtr.Zero); CheckGlError(); } + + static int IndicesPerPrimitive( PrimitiveType pt ) + { + switch( pt ) + { + case PrimitiveType.PointList: return 1; + case PrimitiveType.LineList: return 2; + case PrimitiveType.TriangleList: return 3; + } + throw new NotImplementedException(); + } } public struct Range @@ -135,7 +148,7 @@ namespace OpenRa.GlRenderer { Bind(); Gl.glBufferData(Gl.GL_ARRAY_BUFFER, - new IntPtr(Marshal.SizeOf(typeof(T))), data, Gl.GL_DYNAMIC_DRAW); + new IntPtr(Marshal.SizeOf(typeof(T))*data.Length), data, Gl.GL_DYNAMIC_DRAW); GraphicsDevice.CheckGlError(); } @@ -209,7 +222,9 @@ namespace OpenRa.GlRenderer public Shader(GraphicsDevice dev, Stream s) { this.dev = dev; - var code = new StreamReader(s).ReadToEnd(); + string code; + using (var file = new StreamReader(s)) + code = file.ReadToEnd(); effect = Cg.cgCreateEffect(dev.cgContext, code, null); if (effect == IntPtr.Zero) @@ -228,9 +243,14 @@ namespace OpenRa.GlRenderer if (highTechnique != IntPtr.Zero && 0 == Cg.cgValidateTechnique(highTechnique)) highTechnique = IntPtr.Zero; - if (highTechnique == IntPtr.Zero && lowTechnique == IntPtr.Zero) + if (highTechnique == IntPtr.Zero && lowTechnique == IntPtr.Zero) throw new InvalidOperationException("No valid techniques"); - } + + if( highTechnique == IntPtr.Zero ) + highTechnique = lowTechnique; + if( lowTechnique == IntPtr.Zero ) + lowTechnique = highTechnique; + } public ShaderQuality Quality { get; set; } @@ -255,14 +275,16 @@ namespace OpenRa.GlRenderer public void SetValue(string name, Texture texture) { - var param = Cg.cgGetNamedEffectParameter(effect, name); - CgGl.cgGLSetupSampler(param, texture.texture); + var param = Cg.cgGetNamedEffectParameter( effect, name ); + if( param != IntPtr.Zero && texture != null ) + CgGl.cgGLSetupSampler( param, texture.texture ); } public void SetValue(string name, float x, float y) { var param = Cg.cgGetNamedEffectParameter(effect, name); - CgGl.cgGLSetParameter2f(param, x, y); + if( param != IntPtr.Zero ) + CgGl.cgGLSetParameter2f(param, x, y); } public void Commit() { } @@ -281,6 +303,9 @@ namespace OpenRa.GlRenderer public void SetData(Bitmap bitmap) { + Gl.glBindTexture( Gl.GL_TEXTURE_2D, texture ); + GraphicsDevice.CheckGlError(); + var bits = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, @@ -291,7 +316,7 @@ namespace OpenRa.GlRenderer Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_LEVEL, 0); GraphicsDevice.CheckGlError(); Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height, - 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides + 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides GraphicsDevice.CheckGlError(); bitmap.UnlockBits(bits); diff --git a/chrome-rgba.fx b/chrome-rgba.fx index c3549974bd..8fbcc903d0 100644 --- a/chrome-rgba.fx +++ b/chrome-rgba.fx @@ -32,9 +32,8 @@ VertexOut Simple_vp(VertexIn v) { } float4 Simple_fp(FragmentIn f) : COLOR0 { - return float4( 1,0,0,1 ); -// float4 r = tex2D(DiffuseTexture, f.Tex0); -// return r; + float4 r = tex2D(DiffuseTexture, f.Tex0); + return r; } technique high_quality { @@ -44,8 +43,8 @@ technique high_quality { // CullFace = NONE; // CullMode = None; // FillMode = Solid; - VertexProgram = compile arbvp1 Simple_vp(); - FragmentProgram = compile arbfp1 Simple_fp(); + VertexProgram = compile latest Simple_vp(); + FragmentProgram = compile latest Simple_fp(); //SrcBlend = SrcAlpha; //DestBlend = InvSrcAlpha; diff --git a/chrome-shp.fx b/chrome-shp.fx index d689f71f54..72e3af8cb4 100644 --- a/chrome-shp.fx +++ b/chrome-shp.fx @@ -60,7 +60,7 @@ technique low_quality { BlendEnable = false; DepthTestEnable = false; CullFaceEnable = false; - VertexProgram = compile arbvp1 Simple_vp(); - FragmentProgram = compile arbfp1 Palette_fp(); + VertexProgram = compile latest Simple_vp(); + FragmentProgram = compile latest Palette_fp(); } } diff --git a/line.fx b/line.fx index f4a7451379..71e69cd9e5 100644 --- a/line.fx +++ b/line.fx @@ -34,8 +34,8 @@ technique high_quality { DepthTestEnable = false; //CullMode = None; //FillMode = Wireframe; - VertexProgram = compile arbvp1 Simple_vp(); - FragmentProgram = compile arbfp1 Simple_fp(); + VertexProgram = compile latest Simple_vp(); + FragmentProgram = compile latest Simple_fp(); //SrcBlend = SrcAlpha; //DestBlend = InvSrcAlpha; diff --git a/world-shp.fx b/world-shp.fx index 5aee4721c5..f59685bcfb 100644 --- a/world-shp.fx +++ b/world-shp.fx @@ -57,10 +57,13 @@ float4 Palette_fp(VertexOut f) : COLOR0 { technique low_quality { pass p0 { - BlendEnable = false; + BlendEnable = true; DepthTestEnable = false; CullFaceEnable = false; - VertexProgram = compile arbvp1 Simple_vp(); - FragmentProgram = compile arbfp1 Palette_fp(); + VertexProgram = compile latest Simple_vp(); + FragmentProgram = compile latest Palette_fp(); + + BlendEquation = FuncAdd; + BlendFunc = int2( SrcAlpha, OneMinusSrcAlpha ); } }