Sign in to follow this  
ThiEF_Jr

Uniform variable in HLSL

Recommended Posts

Hi All. I ve some problem (again), help me plz. What is difference between Uniform variable and constant Global varible in HLSL language ? and If i use uniform varible for mask some part of code, the shader will slower than separate code ?
OUTPUT PSLighting( INPUT, uniform bSpecular )
{
   float4 Color = Diffuse( INPUT ) * Texture;

   if( bSpecular ) Color += Specular( INPUT );

   return Color;
}

and
OUTPUT PSDiffuseLighting( INPUT )
{
   float4 Color = Diffuse( INPUT ) * Texture;
   return Color;
}

OUTPUT PSSpecLighting( INPUT )
{
   float4 Color = Diffuse( INPUT ) * Texture;
   Color += Specular( INPUT );
   return Color;
}

If i dont care harder code, which method is fastest ? note: This is an example, in real code it mask in more complex code Thx a lot -ThiEF

Share this post


Link to post
Share on other sites
Quote:
DXSDK Docs[/i]
Uniform data can be specified by two methods. The most common method is to declare global variables and use them within a shader. Any use of global variables within a shader will result in adding that variable to the list of uniform variables required by that shader. The second method is to mark an input parameter of the top-level shader function as uniform. This marking specifies that the given variable should be added to the list of uniform variables.

This means that it doesn't matter which way you declare your constants. Either way, it is compiled down to the same thing.

Share this post


Link to post
Share on other sites
To make it a little more clear which to choose, a uniform variable is specified when you compile a shader, while a global variable may change each time the shader is run.

for the first you have:
compile PixelShader psl = PSLighting(float4(1.0, 1.0, 0.5, 1.0));

for the second, you can simply change the global value each time you call DrawPrimitive. (Don't forget to call CommitChanges).

The first method is more useful for the scenario where you would normally need to have two shaders just to accomodate two different values. Instead, the shader programmer (which may be an artists) can specify two different shaders that will ALWAYS have the same (uniform) value.

eg:
compile PixelShader bright = PSLighting(float4(1.0, 1.0, 1.0, 1.0));
compile PixelShader dark = PSLighting(float4(0.2, 0.2, 0.2, 0.2));

For the second method, the programmer must know the shader well enough to specify the global values required to produce certain effects. Each scenario has its place.

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