Sign in to follow this  
helix

Passing valid verts to shader, but shader has garbage values

Recommended Posts

I'm debugging a shader I wrote (HLSL using DXEffects) and it looks fine to me but somewhere along the way, the valid vertex data I render gets hosed and my shader has a bunch of uninitialized data in it. Why? Here's the shader code (just default rendering -- no wind motion, etc):
// Simply renders the grass with no sinusoidal motion or pixel shader
technique RenderGrassDefault
{
	pass p0
	{
		VertexShader = compile vs_1_1 RenderGrassDefaultVS();

		AlphaBlendEnable = true;
		SrcBlend = ONE;
		DestBlend = ONE;
		
		PixelShader = compile ps_2_0 RenderGrassPS();	// ps_1_4??
		
		ZEnable	= true;
	}
}

// Grass -- no sinusoidal motion or anything
void RenderGrassDefaultVS(	float4 vPos : POSITION,
				float2 vTex : TEXCOORD0,
				out float4 oCol : COLOR0,
				out float4 oPos : POSITION,
				out float2 oTex : TEXCOORD0 )
{
	// Transform the position from object space to homogeneous projection space
	oPos = mul( vPos, g_mWorldViewProjection );

	// Set the grass' color
	oCol = g_vGrassColor;
	
	// Just copy the tex coord through
	oTex = vTex;
}

//
// Grass pixel shader
//	Combine the color and the texture and multiply by two
//
void RenderGrassPS(  float4 vCol : COLOR0,
			float2	vTex : TEXCOORD0,
			out float4	oColor : COLOR0 )
{
	// Output pixel	color
	oColor = tex2D( texture1Sampler, vTex ) * vCol * 2.0f;
}


Here's where the grass verts are getting rendered (note: I've stepped through this and all the variables and data are exactly what I'm expecting to see):
HRESULT GrassContainer::Render( IDirect3DDevice9* pd3dDevice )
{
	HRESULT hr;

	int nNumPrimitives = GetNumQuads() * 2;
	int nNumVerts = GetNumVerts();

	assert( pd3dDevice );
	assert( nNumVerts > 0 && nNumPrimitives > 0 );

	D3DXMATRIXA16 mTerrainOffset;
	D3DXMatrixTranslation( &mTerrainOffset, m_vTerrainOffset.x, m_vTerrainOffset.y, m_vTerrainOffset.z );
	D3DXMATRIXA16 mWorld = *(g_pApp->GetWorldMatrix()) * mTerrainOffset;
	D3DXMATRIXA16 mView = *(g_pApp->GetCamera()->GetViewMatrix());
	D3DXMATRIXA16 mProj = *(g_pApp->GetCamera()->GetProjMatrix());
	D3DXMATRIXA16 mWorldView = mWorld * mView;
	D3DXMATRIXA16 mWorldViewProjection = mWorldView * mProj;

	// Set effect's shared global vars
	V( m_pEffect->SetFloat( "g_fTime", (float)m_dTime ) );
	V( m_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) );

	// Set the grass textures
	LPDIRECT3DBASETEXTURE9* pTex = &(m_pTexture[0]);
	if( pTex )
		V( m_pEffect->SetTexture( "g_texture1", *pTex ) );

	V( pd3dDevice->SetVertexDeclaration( g_pApp->GetScene()->GetGrassDecl() ) );

	// Render the grass patches
	UINT cPass;
	if( m_bNoMotion )
	{
		V( m_pEffect->SetTechnique( m_hTRenderGrassDefault ) );
	}
	else
	{
		V( m_pEffect->SetTechnique( m_hTRenderGrass ) );
	}

	V( m_pEffect->Begin( &cPass, 0 ) );
	for( UINT p = 0; p < cPass; ++p )
	{
		V( m_pEffect->BeginPass( p ) );
		V( m_pEffect->CommitChanges() );

		// Now actually render the grass
		GRASSVERT** data = m_arVerts.GetData(); // This is a growable array from DX's utility code.
		V( pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLELIST, nNumPrimitives, data, sizeof(GRASSVERT) ) );

		V( m_pEffect->EndPass() );
	}
	V( m_pEffect->End() );

	return S_OK;
}


And here is my vertex declarations
// This	is the vertex format used for the grass.
struct GRASSVERT
{
	float x, y,	z;		// Position
	float tu, tv;		// Texcoord

	const static D3DVERTEXELEMENT9 Decl[3];
};

// GRASSVERT
const D3DVERTEXELEMENT9 GRASSVERT::Decl[] =
{
    { 0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    D3DDECL_END()
};


Can you see anything wrong with this? Why would uninitted values be coming into my shader? Thanks!

Share this post


Link to post
Share on other sites
Ok, figured it out. My array of verts was an array of GRASSVERT* instead of GRASSVERT which is what DrawPrimitiveUP was looking for. :\ That and my alpha blending arguments were not even close which all led to some strange looking results.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this