<p>On modern hardware if all invocations in a group follow the same path then the unused path doesn't get evaluated.</p>

<p>in pseudo code:</p>

<pre><code>if(cond){
   res = ...
}else{
   res = ...
}
</code></pre>

<p>becomes</p>

<pre><code>if(anyInvocationARB(cond)){
    res1 = ...
}
if(anyInvocationARB(!cond)){
    res2 = ...
}
res = cond?res1:res2;
</code></pre>

<p>Where <code>anyInvocationARB</code> will be true if any invocation of the shader will have true as <code>cond</code> (from the opengl extension <a href="https://www.opengl.org/registry/specs/ARB/shader_group_vote.txt" rel="nofollow">ARB_shader_group_vote</a>). </p>

<p>If cond is derivable from uniforms alone then the driver may optimize and evaluate the condition before starting the render and replace the if with a goto to the correct branch. OpenGL has a feature called uniform subroutines that makes it explicit.</p>

CC BY-SA 3.0
2015-08-21T06:16:53.847Z
This is true, but it is *not* the only thing you need to consider for performance. GPUs still statically schedule resources per shader, so this may still resources as though you were executing both branches, which may hurt occupancy.
John Calsbeek
CC BY-SA 3.0
2015-08-18T09:16:17.933Z
2016-01-21T10:12:02.363Z
2016-01-21T10:12:02.363Z
ratchet freak
<p>In general, branching in shaders is not a good idea. But now I have a shader with a condition that is constant with respect to the entire draw call. So the branch that is executed is always the same for one draw call.</p>

<p>Is such kind of branching still more costly than having multiple shaders without these branches and switch between them?</p>

CC BY-SA 3.0
2015-08-18T08:46:09.777Z
possible duplicate of [Why is this conditional in my fragment shader so slow?](http://computergraphics.stackexchange.com/questions/259/why-is-this-conditional-in-my-fragment-shader-so-slow)
Martin Ender
CC BY-SA 3.0
2015-08-18T08:47:44.803Z
As the answer explains on my question, the fragments get grouped into "warps" or "wavefronts" and if all fragments in such a group use the same branch, only that branch is executed.
Martin Ender
CC BY-SA 3.0
2015-08-18T08:58:20.317Z
But what about shaders different from fragment?
nikitablack
CC BY-SA 3.0
2015-08-18T09:01:05.100Z
I believe [vertices get assembled into warps or wavefronts just the same](https://fgiesen.wordpress.com/2011/10/09/a-trip-through-the-graphics-pipeline-2011-part-13/).
Martin Ender
CC BY-SA 3.0
2015-08-18T09:02:36.593Z
The result being that if all threads follow the same path there will have less "divergence" and the shader will run faster. E.g. I don't think putting `if (true) { ... }` around the entire program would measurably alter the performance (assuming it's not optimized it out, which it would be).
jozxyqk
CC BY-SA 3.0
2015-08-18T09:04:51.867Z
@jozxyqk I think the OP is more interested in wrapping the entire program in `if (false) { ... }` but yes. :)
Martin Ender
CC BY-SA 3.0
2015-08-18T13:29:55.827Z
I suspect this is not a duplicate, but it needs to be edited to make it clear what is being asked before that can be determined. Some example code or an explanation of the two options being compared would help a lot.
trichoplax is on Codidact now
CC BY-SA 3.0
2015-08-18T13:31:58.820Z
`Is condition anyway will be executed every time?` Are you asking whether the condition is calculated for each pixel/vertex/fragment, or whether the condition is recalculated each time the shader is applied?
trichoplax is on Codidact now
CC BY-SA 3.0
2015-08-20T17:59:09.763Z
If you add some example code to your question, we can more easily tell you if it is a compile time constant or not (:
Alan Wolfe
<performance><shader>
Is a constant condition more costly than switching shaders?
nikitablack
# Is a constant condition more costly than switching shaders?

(Asked by: username_0 on 2015-08-18)

In general, branching in shaders is not a good idea. But now I have a shader with a condition that is constant with respect to the entire draw call. So the branch that is executed is always the same for one draw call.

Is such kind of branching still more costly than having multiple shaders without these branches and switch between them?

**Tags:** performance, shader

> **Comments:**
> **username_1**: possible duplicate of [Why is this conditional in my fragment shader so slow?](http://computergraphics.stackexchange.com/questions/259/why-is-this-conditional-in-my-fragment-shader-so-slow)  
> **username_1**: As the answer explains on my question, the fragments get grouped into "warps" or "wavefronts" and if all fragments in such a group use the same branch, only that branch is executed.  
> **username_0**: But what about shaders different from fragment?  
> **username_1**: I believe [vertices get assembled into warps or wavefronts just the same](https://fgiesen.wordpress.com/2011/10/09/a-trip-through-the-graphics-pipeline-2011-part-13/).  
> **username_2**: The result being that if all threads follow the same path there will have less "divergence" and the shader will run faster. E.g. I don't think putting `if (true) { ... }` around the entire program would measurably alter the performance (assuming it's not optimized it out, which it would be).  
> **username_1**: @username_2 I think the OP is more interested in wrapping the entire program in `if (false) { ... }` but yes. :)  
> **username_3**: I suspect this is not a duplicate, but it needs to be edited to make it clear what is being asked before that can be determined. Some example code or an explanation of the two options being compared would help a lot.  
> **username_3**: `Is condition anyway will be executed every time?` Are you asking whether the condition is calculated for each pixel/vertex/fragment, or whether the condition is recalculated each time the shader is applied?  
> **username_4**: If you add some example code to your question, we can more easily tell you if it is a compile time constant or not (:  

---

## 1 answers

### Answer by username_5 on 2015-08-18. Score: 13 | Accepted answer

On modern hardware if all invocations in a group follow the same path then the unused path doesn't get evaluated.

in pseudo code:

```
if(cond){
   res = ...
}else{
   res = ...
}

```

becomes

```
if(anyInvocationARB(cond)){
    res1 = ...
}
if(anyInvocationARB(!cond)){
    res2 = ...
}
res = cond?res1:res2;

```

Where `anyInvocationARB` will be true if any invocation of the shader will have true as `cond` (from the opengl extension [ARB\_shader\_group\_vote](https://www.opengl.org/registry/specs/ARB/shader_group_vote.txt)).

If cond is derivable from uniforms alone then the driver may optimize and evaluate the condition before starting the render and replace the if with a goto to the correct branch. OpenGL has a feature called uniform subroutines that makes it explicit.

> **Comments:**
> **username_6**: This is true, but it is *not* the only thing you need to consider for performance. GPUs still statically schedule resources per shader, so this may still resources as though you were executing both branches, which may hurt occupancy.  

---

