pixel shader without sampler

Started by
2 comments, last by S-Rave 14 years, 5 months ago
Hey, I'm trying to use a pixel shader on a quad and change the color based on the uv coordinates (without a sample texture). On all the examples I've seen this should work but all it returns is the color blue:

float4 GradPS(float2 tex : TEXCOORD0) : COLOR
{
	float4 c = float4(1,1,1,1);
	c.r = tex[0];
	c.g = 0;
	return c;
}
I'm guessing that I'm not sending any TEXCOORD0, only I've set it in my vertex declaration:

// This looks okay to me.
D3DVERTEXELEMENT9 VertexPNTElements[] = 
{
  {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
  {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
  {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
  D3DDECL_END()
};	
HR(gd3dDevice->CreateVertexDeclaration(VertexPNTElements, &VertexPNT::Decl));
It might be the declaration of the plane vertex buffer and index buffer, only it shows the quad. In the VertexPNT, first 3 are position, next 3 are normal (aren't really used in the shader), last 2 are uv.

HR(gd3dDevice->CreateVertexBuffer(4 * sizeof(VertexPNT), D3DUSAGE_WRITEONLY,
		0, D3DPOOL_MANAGED,	&mPlaneVB, 0));

	VertexPNT* v = 0;
	HR(mPlaneVB->Lock(0,0,(void**)&v, 0));

	v[0] = VertexPNT(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
	v[1] = VertexPNT(-1.0f,  1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
	v[2] = VertexPNT( 1.0f,  1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
	v[3] = VertexPNT( 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

	HR(mPlaneVB->Unlock());
	
	// Create the vertex buffer.
	HR(gd3dDevice->CreateIndexBuffer(6 * sizeof(WORD),	D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,	D3DPOOL_MANAGED, &mPlaneIB, 0));

	// Write box indices to the index buffer.
	WORD* i = 0;
	HR(mPlaneIB->Lock(0, 0, (void**)&i, 0));

	// Fill in the front face index data
	i[0] = 0; i[1] = 1; i[2] = 2;
	i[3] = 0; i[4] = 2; i[5] = 3;
	
	HR(mPlaneIB->Unlock());
Here is my rendering:

HR(mPlaneFX->SetTechnique(mhPlaneTech));
HR(mPlaneFX->SetMatrix(mhPlaneWVP, &(mView*mProj*mWorld)))
HR(mPlaneFX->SetFloat(mhPlaneTime, fTime));
	
HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
HR(gd3dDevice->SetStreamSource(0, mPlaneVB, 0, sizeof(VertexPNT)));
HR(gd3dDevice->SetIndices(mPlaneIB));

UINT numPasses = 0;
HR(mPlaneFX->Begin(&numPasses, 0));
for(UINT i = 0; i < numPasses; ++i)
{
  HR(mPlaneFX->BeginPass(i));
  HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2));
  HR(mPlaneFX->EndPass());
}
HR(mPlaneFX->End());
I've googled for 5 hours and checked my directx book but I just can't figure it out. Anyone got any clue to why it doesn't work? Or maybe it's not supposed to work like this.
Advertisement
I don't do much pixel shading, so this is a guess.

Try-

c.r = tex.x; // not tex[0]. tex is not an array

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Does your vertex shader actually pass the UV coordinates to PS? Also, what Buckeye said.

Niko Suni

Buckeye:
I've already tried with tex.x (had it that way before), didn't work. Tried again now, same results. :

Nik02:
How do I pass the uv from the vertex shader?


Edit: Aah... send the texcood to the vertex shader output structure. Thanks, it've got it now. :)

This topic is closed to new replies.

Advertisement