<p>This is a quite wide subject which implies a lot of topics such as the platform or the compiler, but I think that it could be useful for everyone to know more about this.</p>
<p>A common mistake is to think that conditionals are bad when writing shader but the real bad thing is branching, right ?</p>
<p>I would love to know more about the difference between conditionals and branching with simple concrete examples and how to solve common issues.</p>
<p>Thanks</p>

CC BY-SA 4.0
2020-07-17T13:42:18.270Z
Pretty much any 'divergence' is going to cause a relative loss in peak shader efficiency: If A SIMD GPU has N parallel units but only M of them are doing 'useful' work, be that because of a 'mask bit' or branching, it's still only going to be achieving M/N efficiency.
Simon F
CC BY-SA 4.0
2020-07-17T16:02:40.483Z
This problem was discussed on stackoverflow link: https://stackoverflow.com/questions/37827216/do-conditional-statements-slow-down-shaders
Thomas
CC BY-SA 4.0
2020-07-17T16:05:20.337Z
one example would be: if(uniformVariable == 1)...  not branching, because every invocation is end up in the same condition.. but if(texture(tex,interpolatedCoordinate) == 1) this is branching, because some will go into the condition and other invocations not
Thomas
<opengl><rendering><shader><optimisation><directx>
Conditionals and branching in shaders
MaT
# Conditionals and branching in shaders

(Asked by: username_0 on 2020-07-17)

This is a quite wide subject which implies a lot of topics such as the platform or the compiler, but I think that it could be useful for everyone to know more about this.

A common mistake is to think that conditionals are bad when writing shader but the real bad thing is branching, right ?

I would love to know more about the difference between conditionals and branching with simple concrete examples and how to solve common issues.

Thanks

**Tags:** opengl, rendering, shader, optimisation, directx

> **Comments:**
> **username_1**: Pretty much any 'divergence' is going to cause a relative loss in peak shader efficiency: If A SIMD GPU has N parallel units but only M of them are doing 'useful' work, be that because of a 'mask bit' or branching, it's still only going to be achieving M/N efficiency.  
> **username_2**: This problem was discussed on stackoverflow link: https://stackoverflow.com/questions/37827216/do-conditional-statements-slow-down-shaders  
> **username_2**: one example would be: if(uniformVariable == 1)...  not branching, because every invocation is end up in the same condition.. but if(texture(tex,interpolatedCoordinate) == 1) this is branching, because some will go into the condition and other invocations not  

---

## 0 answers

