Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

One of lines in pixel shader produces a lag (lower fps). I can't understand why.

if (TextureEnabled)
    color *= texDiffuse.Sample(texDiffuseSam, input.UV);

Interesting fact is: TextureEnabled is always false. I have already checked.

// they are null
Texture2D texDiffuse : register(t0);
sampler texDiffuseSam : register(s0);

Is it possible, or i have mistake in other place(?), or is it HLSL so slow?

share|improve this question

1 Answer

up vote 3 down vote accepted

Even if the condition is always false, you're still adding an if-statement to every single pixel you draw with this shader, and that's going to take a nonzero amount of time.

I'm guessing you're confused because you've read statements to the effect that shader dynamic branching is slow when different pixels take different branches. It's especially slow in that case, but it's not free even when all the pixels take the same branch. There is a cost just to have the branch in there.

Typically, the way people handle flags like this is to generate multiple versions of the shader, e.g. one with the texturing code enabled and one with it disabled. This is known as the "ubershader" approach. It can be done using #defines, or it can also be done using uniform bool variables and different entry points. People often write shader-generators to create all the different combinations automatically, or add the combinations as different techniques in an effect file.

Another approach is to write a shader that always samples the texture, and just set it to a 1x1 white texture when the texture is not used. This will be quite fast to sample, although not as fast as removing the texture altogether, and it simplifies the code.

share|improve this answer
 
Thanks! I will hate DX11 because of that (in XNA/DX9 there was no problems with same shader code). I will follow your advices. –  Loryan55 Aug 1 at 0:56
 
@Loryan55 Yeah, D3D9 had a "static branching" feature that, AFAIK, is no longer supported. I'm guessing that graphics drivers internally converted static branching to multiple shaders anyway, so they removed it when they simplified/re-wrote the API for D3D10/11. –  Nathan Reed Aug 1 at 1:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.