Archived

This topic is now archived and is closed to further replies.

HLSL global constant setting problem

This topic is 5011 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

Hi peeps, Just switching over to using HLSL and would like to assign the view/projection matrix for the scene once only (at the moment it is set every time i switch to a new effect) In the shader i have something like:- float4x4 matWorldViewProj: register (c3); and the main code sends 4 vectors to the vertex constants just before the call to BeginScene(). Problem I''ve got is the constants are always zero''d when it gets to executing the vertex shader. Is this a state switch thing or more likely am I just thick? Any help would be much appreciated.

Share this post


Link to post
Share on other sites
Make sure that in all your shaders you set the world/view/proj matrix to use the same register. So all your shaders would have this at the top somewhere:

float4x4 matWorldViewProj: register (c3);

There could be problems if you don''t do that for all your shaders. The reason is that if you load another shader where you don''t specify that register c3 will be used for this matrix, the D3D runtime may assign some other constant to use this register.

Also, when you set the constant from your app code, use the constant table interface. The code would look something like this:

D3DXHANDLE handle;
pVertexTable->GetConstantByName( &handle, "matWorldViewProj" );
if( handle )
{
pVertexTable->SetMatrix(m_pd3dDevice, handle, &matWVP);
}

Hope this helps,
neneboricua

Share this post


Link to post
Share on other sites
Or better yet (debateable):


pVertexTable->SetMatrix(m_pd3dDevice, "matWorldViewProj", &matWVP);


It''s not well-documented, but you can send the name instead of a handle to Set* calls.

I like pie.

Share this post


Link to post
Share on other sites
Thanks to RenderTarget & neneboricua19 for your help,

Unfortunatly still can''t get it to work!

The problem with using handles is they are specific to one shader only, if i set my matrix for shader ''A'' when i switch to shader ''B'' it seems to relode the constants with whatever was last in the matrix variable for shader ''B''.

ie. Even though all shaders use the same variable name, each shader has its own copy of said variable.

This means I would have to go through each shader once per frame repeatadly setting the same matrix into the same variable, is this the normal way to do things?

Is there anyway to switch between shaders and have the shader not set up the constants again?

I suppose at the end of the day I''m trying to share some variables between all shaders and so only have to set them up once!

Once again thanks for any help received.


Share this post


Link to post
Share on other sites
Using HLSL and constant tables I believe you may need to set the constants up for each shader, unless you can coax it into putting all the constants in the same place for you.
If you were not using constant tables and managing the registers yourself, I think you would only need to do it once, because Set*ShaderConstant() leaves the constants shared across the whole device.

The only way I'm aware of sharing constants easily and using HLSL is to use the Effect file framework, (which are pretty damn cool). You can set up shared parameter pools with them that will handle the work for you.

Hope that helps,

-Mezz

[edited by - Mezz on March 30, 2004 5:52:01 AM]

Share this post


Link to post
Share on other sites