Sign in to follow this  
schupf

HLSL: Shared constants?

Recommended Posts

schupf    221
Hi,

lets say I have 10 vertex shaders (each one stored in a .vsh file. Pure HLSL, No Effect framework!) and all of them
use some constants that are the same for all shaders for one frame (for example the view matrix, projection matrix, time etc.).

Is there a way to set these shared constants only once for all shaders?
So instead of this:
[code]
foreach shader in shaders
set view matrix
set proj matrix

set world matrix
set vs/ps
render
[/code]
this:
[code]
set view matrix
set proj matrix
foreach shader in shaders
set world matrix
set vs/ps
render
[/code]

Is this possible in DX9 and with pure HLSL shaders?

Share this post


Link to post
Share on other sites
Hodgman    51345
Yes, but you've got to use the register keyword to manually place them all in the same registers in each shader file.

Set[font="Courier New"]Pixel/Vertex[/font]ShaderConstantF modifies a single set of registers that are used by all shaders.

If you manually assign all your variables to certain registers, you can optimize how often you have to set their values

Share this post


Link to post
Share on other sites
schupf    221
[quote name='Hodgman' timestamp='1311414270' post='4839232']
Yes, but you've got to use the register keyword to manually place them all in the same registers in each shader file.

Set[font="Courier New"]Pixel/Vertex[/font]ShaderConstantF modifies a single set of registers that are used by all shaders.

If you manually assign all your variables to certain registers, you can optimize how often you have to set their values
[/quote]
Thanks!

So I guess setting 2 matrices would look like this:
[code]
// Vertex shader:
float4x4 viewMatrix : register(c0);
float4x4 projMatrix : register(c4);

// C++ Host Code:
float viewMat[16] = ...
float projMat[16] = ...
device->SetVertexShaderConstantF(0, viewMat, 4);
device->SetVertexShaderConstantF(4, viewMat, 4);
[/code]
Is this correct?

Can I additionally add some variables to my shaders that do NOT use the register keyword and set them per constant table?
For example:
[code]
// Vertex shader:
float myVar;
float4x4 viewMatrix : register(c0);
float anotherVar;
[/code]
Will those 2 variables automatically be assigned to some registers? Is the order of the variable declaration important?

Share this post


Link to post
Share on other sites
Hodgman    51345
Yep, that's all correct.
The compiler will assign registers to your other variables however it feels like it (they don't have to be listed in order).

You've just got to be careful if you've got shaders that don't use the viewMatrix variable (for example).

[code]// Vertex shader:
float myVar; //uh oh, might be put in c0
float anotherVar; //might be put in c1[/code]When you set myVar/anotherVar on the C++ side for this shader, you (might/will) be writing into c0/c1, which previously held your viewMatrix. If the next shader assumes that the viewMatrix is already loaded into c0-c3, then it'll be in for a rude surprise!

So if you're going to use manual-register-assignment for some variables, and automatic-register-assignment for other variables, make sure you always include the 'manual' variables in your shaders, even if they're not used (put them in a header or something).
[code]float4x4 viewMatrix : register(c0);//not used in this shader
float myVar;
float4 vs_main( float4 pos : POSITION ) : POSITION
{
return pos * myVar;
}[/code]As you can see, even though viewMatrix isn't used, it still influences the automatic register assignment, with myVar ending up in c4:[code]// fxc /Tvs_3_0 /Evs_main test.hlsl
//
// Parameters:
// float myVar;
//
// Registers:
// Name Reg Size
// ------------ ----- ----
// myVar c4 1
//
vs_3_0
dcl_position v0
dcl_position o0
mul o0, c4.x, v0[/code]

Share this post


Link to post
Share on other sites
schupf    221
Ok, so I'm gonna include a file with all common shader constants in every shader (even if not needed)

That was very helpful! Thank you!:)

Share this post


Link to post
Share on other sites
jischneider    253
Good explanation Hodgman!

A little question:

Let’s say that I have 20 shaders that uses the world matrixand 10 than not. What do you recommend me? To use the register keyword or not?

Thanks!!

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