Jump to content

  • Log In with Google      Sign In   
  • Create Account

HLSL: Shared constants?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 schupf   Members   -  Reputation: 216

Like
0Likes
Like

Posted 23 July 2011 - 02:54 AM

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:
foreach shader in shaders
   set view matrix
   set proj matrix

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

Is this possible in DX9 and with pure HLSL shaders?

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 30415

Like
0Likes
Like

Posted 23 July 2011 - 03:44 AM

Yes, but you've got to use the register keyword to manually place them all in the same registers in each shader file.

SetPixel/VertexShaderConstantF 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

#3 schupf   Members   -  Reputation: 216

Like
0Likes
Like

Posted 23 July 2011 - 09:53 AM

Yes, but you've got to use the register keyword to manually place them all in the same registers in each shader file.

SetPixel/VertexShaderConstantF 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

Thanks!

So I guess setting 2 matrices would look like this:
// 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);
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:
// Vertex shader:
float myVar;
float4x4 viewMatrix : register(c0);
float anotherVar;
Will those 2 variables automatically be assigned to some registers? Is the order of the variable declaration important?

#4 Hodgman   Moderators   -  Reputation: 30415

Like
3Likes
Like

Posted 23 July 2011 - 09:47 PM

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).

// Vertex shader:
float myVar; //uh oh, might be put in c0
float anotherVar; //might be put in c1
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).
float4x4 viewMatrix : register(c0);//not used in this shader
float myVar;
float4 vs_main( float4 pos : POSITION ) : POSITION
{
  return pos * myVar;
}
As you can see, even though viewMatrix isn't used, it still influences the automatic register assignment, with myVar ending up in c4:
//   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


#5 schupf   Members   -  Reputation: 216

Like
0Likes
Like

Posted 24 July 2011 - 02:46 AM

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!:)

#6 jischneider   Members   -  Reputation: 252

Like
0Likes
Like

Posted 05 August 2011 - 01:32 PM

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!!


Project page: < XNA FINAL Engine >





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS