Jump to content
  • Advertisement
Sign in to follow this  
ThiEF_Jr

Uniform variable in HLSL

This topic is 4785 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 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
Advertisement
Quote:
DXSDK Docs
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!