3
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks! I will hate DX11 because of that (in XNA/DX9 there was no problems with same shader code). I will follow your advices. \$\endgroup\$
    – Loryan55
    Aug 1, 2013 at 0:56
  • \$\begingroup\$ @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. \$\endgroup\$ Aug 1, 2013 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .