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"
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>
<Files>
<Filter

View File

@@ -1,6 +1,8 @@
#pragma once
using namespace System::Collections::Generic;
using namespace IjwFramework::Collections;
using namespace IjwFramework::Delegates;
namespace BluntDirectX { namespace Direct3D
{
@@ -11,12 +13,12 @@ namespace BluntDirectX { namespace Direct3D
High,
};
public ref class Effect
public ref class Shader
{
private:
static ID3DXEffectPool* effectPool;
static Effect()
static Shader()
{
ID3DXEffectPool* e;
D3DXCreateEffectPool( &e );
@@ -25,12 +27,23 @@ namespace BluntDirectX { namespace Direct3D
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:
ID3DXEffect* effect;
Cache<String^,IntPtr>^ parameters;
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;
ID3DXBuffer* compilationErrors;
HRESULT hr;
@@ -56,15 +69,7 @@ namespace BluntDirectX { namespace Direct3D
Quality = ShaderQuality::High;
}
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;
}
~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()
{
effect->End();
}
void BeginPass(int pass)
{
effect->BeginPass( pass );
}
void EndPass()
for( unsigned int i = 0; i < passes; i++ )
{
effect->BeginPass( i );
action();
effect->EndPass();
}
effect->End();
}
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;
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 );
}

View File

@@ -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<T>(FvfVertexBuffer<T> vertices, IndexBuffer indices,
Range<int> vertexRange, Range<int> indexRange, Texture texture)
where T : struct
{
shader.SetTexture(baseTextureHandle, texture);
shader.SetValue("DiffuseTexture", texture);
shader.Commit();
vertices.Bind(0);