Sign in to follow this  
Halsafar

HLSL -- Cache parameters handles

Recommended Posts

I need to SetValue() many parameters each frame for a fx shader. I have read that it is faster to get a handle to the variable so you don't have to do a name look-up each frame. How do I go about this?

Share this post


Link to post
Share on other sites
When you call D3DXCompileShaderFromFile() the last argument of that function, if used, will return a pointer to a ID3DXConstantTable interface which can be used to access shader constants.
After you get that interface you can call ID3DXConstantTable::GetConstantByName() to get an handle to the constant like this:

D3DXHANDLE hConstant = m_pConstantTable->GetConstantByName( NULL, "myConstant" );


After that, to use the constant handle you call ID3DXConstantTable::Set* methods and pass the handle to those methods.

p.s. I don't know if that's how you do it with d3dx effects.

Share this post


Link to post
Share on other sites
With the effects framework, it's even easier. You have the following functions at your disposal:

GetParameter()
GetParameterByName()
GetParameterBySemantic()


GetParameter() is useful for retrieving individual elements of an array. There are also maching functions for retrieving technique handles, like:

GetTechnique()
GetTechniqueByName()



Now, you can just do:

D3DXHANDLE param = effect->GetParameterByName( NULL, "diffuseColor" )
effect->SetVector( param, &color );

Share this post


Link to post
Share on other sites
Nice site. Decent FX Article there.

Curious, why use GetParameterBySemantic when it seems identical to GetParameterByName, at least in the article you have on your site the use between the 2 is indistinguishable, in both case's your getting the handle via the parameter name...

Aren't semantics the way you connect stuff to the pipeline?
float4 texCoordDiffuse0 : TEXCOORD0

So wouldn't the use of GetBySemantic be used to get texCoordDiffuse0 via TEXCOORD0...

Share this post


Link to post
Share on other sites
Quote:
Original post by Halsafar
Curious, why use GetParameterBySemantic when it seems identical to GetParameterByName, at least in the article you have on your site the use between the 2 is indistinguishable, in both case's your getting the handle via the parameter name...

Aren't semantics the way you connect stuff to the pipeline?
float4 texCoordDiffuse0 : TEXCOORD0

So wouldn't the use of GetBySemantic be used to get texCoordDiffuse0 via TEXCOORD0...

Exactly, that is correct. I find that polling parameters by semantic is much better than polling by their name, because it is a pain to have to write shaders with the exact same variable names every time. If you produce a standard set of semantics for your project (much like DXSAS), then writing shaders and integrating them with your engine will be much easier.

Share this post


Link to post
Share on other sites
Quote:
Original post by circlesoft
Quote:
Original post by Halsafar
Curious, why use GetParameterBySemantic when it seems identical to GetParameterByName, at least in the article you have on your site the use between the 2 is indistinguishable, in both case's your getting the handle via the parameter name...

Aren't semantics the way you connect stuff to the pipeline?
float4 texCoordDiffuse0 : TEXCOORD0

So wouldn't the use of GetBySemantic be used to get texCoordDiffuse0 via TEXCOORD0...

Exactly, that is correct. I find that polling parameters by semantic is much better than polling by their name, because it is a pain to have to write shaders with the exact same variable names every time. If you produce a standard set of semantics for your project (much like DXSAS), then writing shaders and integrating them with your engine will be much easier.

Just to add something... my understanding of the standard semantics stuff is that it also helps with the overall production pipeline (esp. the bits you might buy/licence and not actually create yourself). If your content creation tools, debugging tools and runtime engine all use the same names you get a lot of interoperability for free [smile]

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Just to add something... my understanding of the standard semantics stuff is that it also helps with the overall production pipeline (esp. the bits you might buy/licence and not actually create yourself). If your content creation tools, debugging tools and runtime engine all use the same names you get a lot of interoperability for free [smile]

Ah, right. The last time I heard, both ATI's RenderMonkey and Nvidia's FX Composer supported DXSAS (and are probably adding more). SAS does take into account many different types of data binds (ie Sas.EnvironmentMap and Sas.Time.Now), but many engines may require their own specialized symantics. However, I think you will see *a lot* more of this with the emergence of XNA and hence, a completely unified pipeline.

Share this post


Link to post
Share on other sites
Quote:
Original post by Halsafar
Whoa, DXSAS... A standard use of semantics, I'd like to read up on this.


[DirectX SDK Documentation]
DirectX Graphics ->
Reference ->
Effect Reference ->
DirectX Standard Annotation and Semantics Reference


[DXSAS == DirectX Standard Annotation and Semantics]

Share this post


Link to post
Share on other sites
Also, the Nvidia SDK has a DXSAS sample that displays the inner-workings of a SAS-compliant engine. The code structure and style itself is pretty bad, but it serves as a pretty good reference. Some of the ID3DXEffect functionality, like retrieving annotations, can be confusing if you don't have a code example in front of you [smile]

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