Jump to content
  • Advertisement
Sign in to follow this  

Passing valid verts to shader, but shader has garbage values

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 )

	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 ) );
		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.
	float x, y,	z;		// Position
	float tu, tv;		// Texcoord

	const static D3DVERTEXELEMENT9 Decl[3];


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!