git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1742 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
chrisf
2007-10-03 22:12:31 +00:00
parent b5627d0bb2
commit 9354daed64
3 changed files with 43 additions and 55 deletions

View File

@@ -170,6 +170,10 @@
RelativePath="System.dll" RelativePath="System.dll"
AssemblyName="System, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" AssemblyName="System, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"
/> />
<AssemblyReference
RelativePath="..\..\IjwFramework\IjwFramework\bin\Debug\IjwFramework.dll"
AssemblyName="IjwFramework, Version=1.0.0.0, processorArchitecture=MSIL"
/>
</References> </References>
<Files> <Files>
<Filter <Filter

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
using namespace System::Collections::Generic; using namespace System::Collections::Generic;
using namespace IjwFramework::Collections;
using namespace IjwFramework::Delegates;
namespace BluntDirectX { namespace Direct3D namespace BluntDirectX { namespace Direct3D
{ {
@@ -11,12 +13,12 @@ namespace BluntDirectX { namespace Direct3D
High, High,
}; };
public ref class Effect public ref class Shader
{ {
private: private:
static ID3DXEffectPool* effectPool; static ID3DXEffectPool* effectPool;
static Effect() static Shader()
{ {
ID3DXEffectPool* e; ID3DXEffectPool* e;
D3DXCreateEffectPool( &e ); D3DXCreateEffectPool( &e );
@@ -25,12 +27,23 @@ namespace BluntDirectX { namespace Direct3D
ShaderQuality shaderQuality; ShaderQuality shaderQuality;
IntPtr GetHandle( String^ symbol )
{
array<unsigned char>^ chars = System::Text::Encoding::ASCII->GetBytes(symbol);
pin_ptr<const unsigned char> p = &chars[0];
IntPtr result = IntPtr((void*)effect->GetParameterByName(NULL, (char*)p));
return result;
}
internal: internal:
ID3DXEffect* effect; ID3DXEffect* effect;
Cache<String^,IntPtr>^ parameters;
public: public:
Effect(GraphicsDevice^ device, Stream^ data) Shader(GraphicsDevice^ device, Stream^ data)
{ {
parameters = gcnew Cache<String^,IntPtr>( gcnew Provider<IntPtr,String^>(this, &Shader::GetHandle));
ID3DXEffect* e; ID3DXEffect* e;
ID3DXBuffer* compilationErrors; ID3DXBuffer* compilationErrors;
HRESULT hr; HRESULT hr;
@@ -56,15 +69,7 @@ namespace BluntDirectX { namespace Direct3D
Quality = ShaderQuality::High; Quality = ShaderQuality::High;
} }
IntPtr GetHandle( String^ symbol ) ~Shader()
{
array<unsigned char>^ chars = System::Text::Encoding::ASCII->GetBytes(symbol);
pin_ptr<const unsigned char> p = &chars[0];
IntPtr result = IntPtr((void*)effect->GetParameterByName(NULL, (char*)p));
return result;
}
~Effect()
{ {
safe_release( effect ); safe_release( effect );
} }
@@ -100,37 +105,32 @@ namespace BluntDirectX { namespace Direct3D
effect->CommitChanges(); effect->CommitChanges();
} }
int Begin() void Render( IjwFramework::Delegates::Action^ action )
{ {
unsigned int passes; unsigned int passes;
effect->Begin( &passes, D3DXFX_DONOTSAVESTATE | D3DXFX_DONOTSAVESHADERSTATE ); effect->Begin( &passes, D3DXFX_DONOTSAVESTATE | D3DXFX_DONOTSAVESHADERSTATE );
return passes;
}
void End() for( unsigned int i = 0; i < passes; i++ )
{
effect->End();
}
void BeginPass(int pass)
{
effect->BeginPass( pass );
}
void EndPass()
{ {
effect->BeginPass( i );
action();
effect->EndPass(); effect->EndPass();
} }
effect->End();
}
generic< typename T> where T : value class 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<T> pvalue = &value; pin_ptr<T> pvalue = &value;
effect->SetValue( (D3DXHANDLE)handle.ToPointer(), pvalue, sizeof(T) ); 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 ); effect->SetTexture( (D3DXHANDLE)handle.ToPointer(), texture->texture );
} }

View File

@@ -6,21 +6,20 @@ using System.Drawing;
using BluntDirectX.Direct3D; using BluntDirectX.Direct3D;
using System.IO; using System.IO;
using OpenRa.FileFormats; using OpenRa.FileFormats;
using IjwFramework.Delegates;
namespace OpenRa.Game namespace OpenRa.Game
{ {
class Renderer class Renderer
{ {
readonly GraphicsDevice device; readonly GraphicsDevice device;
readonly Effect shader; readonly Shader shader;
readonly IntPtr r1Handle, r2Handle, baseTextureHandle, scrollHandle, paletteHandle;
const string shaderName = "diffuse.fx"; const string shaderName = "diffuse.fx";
public void SetPalette(HardwarePalette hp) public void SetPalette(HardwarePalette hp)
{ {
shader.SetTexture(paletteHandle, hp.Texture); shader.SetValue("Palette", hp.Texture);
} }
public Renderer(Control host, Size resolution, bool windowed) public Renderer(Control host, Size resolution, bool windowed)
@@ -29,14 +28,8 @@ namespace OpenRa.Game
device = GraphicsDevice.Create(host, device = GraphicsDevice.Create(host,
resolution.Width, resolution.Height, windowed, false); resolution.Width, resolution.Height, windowed, false);
shader = new Effect(device, FileSystem.Open(shaderName)); shader = new Shader(device, FileSystem.Open(shaderName));
shader.Quality = ShaderQuality.Low; 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; } } public GraphicsDevice Device { get { return device; } }
@@ -45,9 +38,9 @@ namespace OpenRa.Game
{ {
device.Begin(); device.Begin();
shader.SetValue(scrollHandle, scroll); shader.SetValue("Scroll", scroll);
shader.SetValue(r1Handle, r1); shader.SetValue("r1", r1);
shader.SetValue(r2Handle, r2); shader.SetValue("r2", r2);
} }
public void EndFrame() public void EndFrame()
@@ -56,26 +49,17 @@ namespace OpenRa.Game
device.Present(); device.Present();
} }
public void DrawWithShader(ShaderQuality quality, MethodInvoker task) public void DrawWithShader(ShaderQuality quality, Action task)
{ {
shader.Quality = quality; shader.Quality = quality;
shader.Render(task);
int passes = shader.Begin();
for (int pass = 0; pass < passes; pass++)
{
shader.BeginPass(pass);
task();
shader.EndPass();
}
shader.End();
} }
public void DrawBatch<T>(FvfVertexBuffer<T> vertices, IndexBuffer indices, public void DrawBatch<T>(FvfVertexBuffer<T> vertices, IndexBuffer indices,
Range<int> vertexRange, Range<int> indexRange, Texture texture) Range<int> vertexRange, Range<int> indexRange, Texture texture)
where T : struct where T : struct
{ {
shader.SetTexture(baseTextureHandle, texture); shader.SetValue("DiffuseTexture", texture);
shader.Commit(); shader.Commit();
vertices.Bind(0); vertices.Bind(0);