Do conditional statements slow down shaders?
Asked Answered
B

2

114

I want to know if "if-statements" inside shaders (vertex / fragment / pixel...) are really slowing down the shader performance. For example:

Is it better to use this:

vec3 output;
output = input*enable + input2*(1-enable);

instead of using this:

vec3 output;
if(enable == 1)
{
    output = input;
}
else
{
    output = input2;
}

in another forum there was a talk about that (2013): https://mcmap.net/q/10592/unity-shader-if-else-performance Here the guys are saying, that the If-statements are really bad for the performance of the shader.

Also here they are talking about how much is inside the if/else statements (2012): https://www.opengl.org/discussion_boards/showthread.php/177762-Performance-alternative-for-if-(-)

maybe the hardware or the shadercompiler are better now and they fix somehow this (maybe not existing) performance issue.

EDIT:

What is with this case, here lets say enable is a uniform variable and it is always set to 0:

if(enable == 1) //never happens
{
    output = vec4(0,0,0,0);
}
else  //always happens
{
    output = calcPhong(normal, lightDir);
}

I think here we have a branch inside the shader which slows the shader down. Is that correct?

Does it make more sense to make 2 different shaders like one for the else and the other for the if part?

Bani answered 15/6, 2016 at 5:59 Comment(6)
I believe the if statement just reduces the calculation needed to be done by the shader. The first example forces the shader to calculate both cases...while the if statement reduces it. And no, your if statement will not be consuming processing time....as it comes out as branches in the assembly code at the end once compiledOnionskin
thanks for your fast answer! I edited the question. Can you pls answer also the edited part? thx!!!Bani
I don't think you should have 2 shaders. I believe this is starting to get into premature optimization. As @246tNt pointed out, the compiler will deal with it if it's a uniform. But other than that, I really don't think you should worry this much about the performance. First get it done. Then if see the shader is the bottleneck, then look into the algorithm. Stuff like these shouldn't hurt muchOnionskin
I only asked this question, because I want to know if I can achieve better performance with this or not. If you use a shader for a iterating calculation and you have to run it 100 times before you render your picture, then every ms is importantBani
Unless you profile and determine this is a bottleneck empirically, always go with the code which has the most clear intentions. In this case, the if statement. The reasons for this are two-fold: you don't end up with convoluted code constructs with no evidence to support them being better, and, more importantly, the compiler is more likely to recognize your intentions and optimize the problem away.Mirabel
@DanBechard It really depends on your experience. I'm not going to code every application in immediate mode OpenGL, then profile and realize i need shaders. Things like scene graphs are a similar decision. You could render everything inline in your code, or make the decision to put all draw calls into one spot. It's easy to know ahead of time without worrying about premature optimization.Poly
D
234

What is it about shaders that even potentially makes if statements performance problems? It has to do with how shaders get executed and where GPUs get their massive computing performance from.

Separate shader invocations are usually executed in parallel, executing the same instructions at the same time. They're simply executing them on different sets of input values; they share uniforms, but they have different internal registers. One term for a group of shaders all executing the same sequence of operations is "wavefront".

The potential problem with any form of conditional branching is that it can screw all that up. It causes different invocations within the wavefront to have to execute different sequences of code. That is a very expensive process, whereby a new wavefront has to be created, data copied over to it, etc.

Unless... it doesn't.

For example, if the condition is one that is taken by every invocation in the wavefront, then no runtime divergence is needed. As such, the cost of the if is just the cost of checking a condition.

So, let's say you have a conditional branch, and let's assume that all of the invocations in the wavefront will take the same branch. There are three possibilities for the nature of the expression in that condition:

  • Compile-time static. The conditional expression is entirely based off of compile-time constants. As such, you know from looking at the code which branches will be taken. Pretty much any compiler handles this as part of basic optimization.
  • Statically uniform branching. The condition is based off of expressions involving things which are known at compile-time to be constant (specifically, constants and uniform values). But the value of the expression will not be known at compile-time. So the compiler can statically be certain that wavefronts will never be broken by this if, but the compiler cannot know which branch will be taken.
  • Dynamic branching. The conditional expression contains terms other than constants and uniforms. Here, a compiler cannot tell a priori if a wavefront will be broken up or not. Whether that will need to happen depends on the runtime evaluation of the condition expression.

Different hardware can handle different branching types without divergence.

Also, even if a condition is taken by different wavefronts, the compiler could restructure the code to not require actual branching. You gave a fine example: output = input*enable + input2*(1-enable); is functionally equivalent to the if statement. A compiler could detect that an if is being used to set a variable, and thus execute both sides. This is frequently done for cases of dynamic conditions where the bodies of the branches are small.

Pretty much all hardware can handle var = bool ? val1 : val2 without having to diverge. This was possible way back in 2002.

Since this is very hardware-dependent, it... depends on the hardware. There are however certain epochs of hardware that can be looked at:

Desktop, Pre-D3D10

There, it's kinda the wild west. NVIDIA's compiler for such hardware was notorious for detecting such conditions and actually recompiling your shader whenever you changed uniforms that affected such conditions.

In general, this era is where about 80% of the "never use if statements" comes from. But even here, it's not necessarily true.

You can expect optimization of static branching. You can hope that statically uniform branching won't cause any additional slowdown (though the fact that NVIDIA thought recompilation would be faster than executing it makes it unlikely at least for their hardware). But dynamic branching is going to cost you something, even if all of the invocations take the same branch.

Compilers of this era do their best to optimize shaders so that simple conditions can be executed simply. For example, your output = input*enable + input2*(1-enable); is something that a decent compiler could generate from your equivalent if statement.

Desktop, Post-D3D10

Hardware of this era is generally capable of handling statically uniform branches statements with little slowdown. For dynamic branching, you may or may not encounter slowdown.

Desktop, D3D11+

Hardware of this era is pretty much guaranteed to be able to handle dynamically uniform conditions with little performance issues. Indeed, it doesn't even have to be dynamically uniform; so long as all of the invocations within the same wavefront take the same path, you won't see any significant performance loss.

Note that some hardware from the previous epoch probably could do this as well. But this is the one where it's almost certain to be true.

Mobile, ES 2.0

Welcome back to the wild west. Though unlike Pre-D3D10 desktop, this is mainly due to the huge variance of ES 2.0-caliber hardware. There's such a huge amount of stuff that can handle ES 2.0, and they all work very differently from each other.

Static branching will likely be optimized. But whether you get good performance from statically uniform branching is very hardware-dependent.

Mobile, ES 3.0+

Hardware here is rather more mature and capable than ES 2.0. As such, you can expect statically uniform branches to execute reasonably well. And some hardware can probably handle dynamic branches the way modern desktop hardware does.

Dunaville answered 15/6, 2016 at 13:35 Comment(5)
Safe to assume compilers optimize built-in conditional functions like min and step such that they don't diverge, right?Veldaveleda
@Veldaveleda Yes, they either use dedicated instructions or a conditional assignment instruction, which as mentioned does not cause divergence. As a side note, there's in my opinion no good reason to use the step function. You can always replace it with the much more intuitive conditional assignment: step(a, b) * c -> (a >= b) ? c : 0Enlistee
How about WebGL? As it is based on ES 2.0, does it work similarly to that, or because the implementation eg. on Windows is using ANGLE it is more mature regarding branch execution?Daberath
@Andrew: "How about WebGL?" What about it? We're talking about the behavior of hardware, not software. The interface isn't that important; what matters is how the underlying hardware reacts. I only mention API versions because the highest API version supported on some hardware is a rough estimate of its capabilities.Dunaville
A lot of post processing examples I've run across in Unity define two passes, each configured slightly differently. They then supply the number to define which pass of a shader to process during the Blit call. This looks to me like branching, except the branch determination itself occurs in the C# code. As I know little on the subject of shader performance, what's your opinion on the practice?Gigue
J
17

It's highly dependent on the hardware and on the condition.

If your condition is a uniform : don't bother, let the compiler deal with it. If your condition is something dynamic (like a value computed from attribute or fetched from texture or something), then it's more complicated.

For that latter case you'll pretty much have to test and benchmark because it'll depend on the complexity of the code in each branches and how 'consistent' the branch decision is.

For instance if one of the branch is taken 99% of the case and discards the fragment, then most likely you want to keep the conditional. But OTOH in your simple example above if enable is some dynamic condition, the arithmetic select might be better.

Unless you have a clear cut case like the above, or unless you're optimizing for one fixed known architecture, you're probably better off the compiler figure that out for you.

Jeramey answered 15/6, 2016 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.