I'm having trouble with writing a basic vertex shader using D3D9 - I was hoping somebody could help me find the problem.
First of all here's the FX file I'm using:
//-----------------------------------------------------------------------------
// Effect File Variables
//-----------------------------------------------------------------------------
float4 LightDir = {0.5f, 0.5f, -0.5f,1.0f};
float4 LightColor = {0.7f, 0.7f, 0.7f, 1.0f};
float4x4 gWorldViewProj : WORLDVIEWPROJ;
float4x4 gWorldMatrix : WORLD;
float4x4 gViewMatrix : VIEW;
float4x4 gProjMatrix : PROJECTION;
float3 gViewPos : CAMERAPOSITION;
//-----------------------------------------------------------------------------
// Vertex Definitions
//-----------------------------------------------------------------------------
struct VS_INPUT
{
float3 vPos : POSITION;
};
struct VS_OUTPUT
{
float4 vPos : POSITION;
};
//-----------------------------------------------------------------------------
// Simple Vertex Shader
//-----------------------------------------------------------------------------
void myvs( float3 vPos : POSITION, out VS_OUTPUT OUT )
{
OUT.vPos = mul( gWorldViewProj, float4(vPos,1.0f) );
}
//-----------------------------------------------------------------------------
// Simple Pixel Shader
//-----------------------------------------------------------------------------
float4 myps( in VS_OUTPUT IN ) : COLOR
{
return LightColor;
}
//-----------------------------------------------------------------------------
// Simple Effect (1 technique with 1 pass)
//-----------------------------------------------------------------------------
technique lol
{
pass P0
{
VertexShader = compile vs_2_0 myvs();
PixelShader = compile ps_2_0 myps();
//ZEnable = false;
}
}
As you can see, it's nothing special. Before I show you the code for rendering with the shader, let me say that if I remove the vertex shader and only render pixel shader it works fine. So I can only assume that my matrices are incorrect.
Here's how I render with the shader:
if( !m_shader->SetTechnique( "lol" ) )
DEBUG_INFO( DEBUG_FATAL, "Failed to set effect technique" );
m_shader->SendRenderData( &matWorld );
unsigned int iPasses = 0;
if( !m_shader->Begin( &iPasses ) )
DEBUG_INFO( DEBUG_FATAL, "Failed to set effect technique" );
for( unsigned int i = 0; i < iPasses; i++ )
{
if( !m_shader->Pass( i ) )
DEBUG_INFO( DEBUG_FATAL, "Failed to start pass %i", i );
//m_device->SetTransform( D3DTS_WORLD, &matWorld );
m_device->SetVertexDeclaration( Application::Get()->GetRenderer()->GetVertexDeclaration() );
m_device->SetFVF( VOXEL_VERTEXFORMAT );
m_device->SetStreamSource(0, m_vertexBuffer, 0, sizeof(VoxelVertex));
m_device->SetIndices( m_indexBuffer );
HRESULT hr = m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_vertices.size(), 0, m_indices.size()/3 );
if( FAILED( hr ) )
DEBUG_INFO( DEBUG_FATAL, "Failed to draw chunk 0x%x", hr );
if( !m_shader->EndPass( ) )
DEBUG_INFO( DEBUG_FATAL, "Failed to end pass %i", i );
}
m_shader->End( );
No errors are returned. At any point. I just see nothing geometry on the screen.
Where I send my data to the shader:
void Shader::SendRenderData( D3DXMATRIX * world )
{
if( !m_application )
return;
if( !m_effect )
return;
Renderer * pDevice = m_application->GetRenderer( );
if( !pDevice )
return;
D3DXMATRIX matWorld = world ? *world : pDevice->GetWorldTransform( );
D3DXMATRIX matView = pDevice->GetViewTransform( );
D3DXMATRIX matProj = pDevice->GetProjectionTransform( );
D3DXMATRIX matWorldViewProj = matWorld * matView * matProj;
m_effect->SetMatrix( "gWorldMatrix", &matWorld );
m_effect->SetMatrix( "gViewMatrix", &matView );
m_effect->SetMatrix( "gProjMatrix", &matProj );
m_effect->SetMatrix( "gWorldViewProj", &matWorldViewProj );
//m_effect->SetVector( "gViewPos", &pDevice->GetViewVector( ) );
D3DXMATRIX mwvp;
m_effect->GetMatrix( "gWorldViewProj", &mwvp );
m_effect->CommitChanges( );
}
I'm not very experienced with shaders in particular, however I can't see any obvious problems.
I stepped through with a debugger and the final WVP matrix is valid and looks something like this:
_11 0.50682366 float
_12 -0.75278497 float
_13 -0.81174475 float
_14 -0.81171763 float
_21 0.00000000 float
_22 -1.5269465 float
_23 0.47204468 float
_24 0.47202891 float
_31 -1.1960893 float
_32 -0.31898057 float
_33 -0.34396380 float
_34 -0.34395233 float
_41 0.00000000 float
_42 1368.1440 float
_43 -423.05203 float
_44 -422.93790 float
I look forward to your responses.
Thanks