diff --git a/BluntDx/BluntDx.vcproj b/BluntDx/BluntDx.vcproj index 686e1f0636..47e66c6b2e 100644 --- a/BluntDx/BluntDx.vcproj +++ b/BluntDx/BluntDx.vcproj @@ -170,6 +170,10 @@ RelativePath="System.dll" AssemblyName="System, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" /> + ^ chars = System::Text::Encoding::ASCII->GetBytes(symbol); + pin_ptr p = &chars[0]; + IntPtr result = IntPtr((void*)effect->GetParameterByName(NULL, (char*)p)); + return result; + } + internal: ID3DXEffect* effect; + Cache^ parameters; public: - Effect(GraphicsDevice^ device, Stream^ data) + Shader(GraphicsDevice^ device, Stream^ data) { + parameters = gcnew Cache( gcnew Provider(this, &Shader::GetHandle)); + ID3DXEffect* e; ID3DXBuffer* compilationErrors; HRESULT hr; @@ -56,15 +69,7 @@ namespace BluntDirectX { namespace Direct3D Quality = ShaderQuality::High; } - IntPtr GetHandle( String^ symbol ) - { - array^ chars = System::Text::Encoding::ASCII->GetBytes(symbol); - pin_ptr p = &chars[0]; - IntPtr result = IntPtr((void*)effect->GetParameterByName(NULL, (char*)p)); - return result; - } - - ~Effect() + ~Shader() { safe_release( effect ); } @@ -100,37 +105,32 @@ namespace BluntDirectX { namespace Direct3D effect->CommitChanges(); } - int Begin() + void Render( IjwFramework::Delegates::Action^ action ) { unsigned int passes; effect->Begin( &passes, D3DXFX_DONOTSAVESTATE | D3DXFX_DONOTSAVESHADERSTATE ); - return passes; - } - void End() - { + for( unsigned int i = 0; i < passes; i++ ) + { + effect->BeginPass( i ); + action(); + effect->EndPass(); + } + effect->End(); } - void BeginPass(int pass) - { - effect->BeginPass( pass ); - } - - void EndPass() - { - effect->EndPass(); - } - generic< typename T> where T : value class - void SetValue( IntPtr handle, T value ) + void SetValue( String^ name, T value ) { + IntPtr handle = parameters[name]; pin_ptr pvalue = &value; effect->SetValue( (D3DXHANDLE)handle.ToPointer(), pvalue, sizeof(T) ); } - void SetTexture( IntPtr handle, Texture^ texture ) + void SetValue( String^ name, Texture^ texture ) { + IntPtr handle = parameters[name]; effect->SetTexture( (D3DXHANDLE)handle.ToPointer(), texture->texture ); } diff --git a/OpenRa.Game/Renderer.cs b/OpenRa.Game/Renderer.cs index 774fb277d0..695d086689 100644 --- a/OpenRa.Game/Renderer.cs +++ b/OpenRa.Game/Renderer.cs @@ -6,21 +6,20 @@ using System.Drawing; using BluntDirectX.Direct3D; using System.IO; using OpenRa.FileFormats; +using IjwFramework.Delegates; namespace OpenRa.Game { class Renderer { readonly GraphicsDevice device; - readonly Effect shader; - - readonly IntPtr r1Handle, r2Handle, baseTextureHandle, scrollHandle, paletteHandle; + readonly Shader shader; const string shaderName = "diffuse.fx"; public void SetPalette(HardwarePalette hp) { - shader.SetTexture(paletteHandle, hp.Texture); + shader.SetValue("Palette", hp.Texture); } public Renderer(Control host, Size resolution, bool windowed) @@ -29,14 +28,8 @@ namespace OpenRa.Game device = GraphicsDevice.Create(host, resolution.Width, resolution.Height, windowed, false); - shader = new Effect(device, FileSystem.Open(shaderName)); + shader = new Shader(device, FileSystem.Open(shaderName)); shader.Quality = ShaderQuality.Low; - - baseTextureHandle = shader.GetHandle("DiffuseTexture"); - scrollHandle = shader.GetHandle("Scroll"); - r1Handle = shader.GetHandle("r1"); - r2Handle = shader.GetHandle("r2"); - paletteHandle = shader.GetHandle("Palette"); } public GraphicsDevice Device { get { return device; } } @@ -45,9 +38,9 @@ namespace OpenRa.Game { device.Begin(); - shader.SetValue(scrollHandle, scroll); - shader.SetValue(r1Handle, r1); - shader.SetValue(r2Handle, r2); + shader.SetValue("Scroll", scroll); + shader.SetValue("r1", r1); + shader.SetValue("r2", r2); } public void EndFrame() @@ -56,26 +49,17 @@ namespace OpenRa.Game device.Present(); } - public void DrawWithShader(ShaderQuality quality, MethodInvoker task) + public void DrawWithShader(ShaderQuality quality, Action task) { shader.Quality = quality; - - int passes = shader.Begin(); - for (int pass = 0; pass < passes; pass++) - { - shader.BeginPass(pass); - task(); - shader.EndPass(); - } - - shader.End(); + shader.Render(task); } public void DrawBatch(FvfVertexBuffer vertices, IndexBuffer indices, Range vertexRange, Range indexRange, Texture texture) where T : struct { - shader.SetTexture(baseTextureHandle, texture); + shader.SetValue("DiffuseTexture", texture); shader.Commit(); vertices.Bind(0);