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?

  • 1
    $\begingroup$ possible duplicate of Why is this conditional in my fragment shader so slow? $\endgroup$ Commented Aug 18, 2015 at 8:46
  • $\begingroup$ 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. $\endgroup$ Commented Aug 18, 2015 at 8:47
  • $\begingroup$ But what about shaders different from fragment? $\endgroup$ Commented Aug 18, 2015 at 8:58
  • 2
    $\begingroup$ I believe vertices get assembled into warps or wavefronts just the same. $\endgroup$ Commented Aug 18, 2015 at 9:01
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Aug 18, 2015 at 13:29

1 Answer 1


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

in pseudo code:

   res = ...
   res = ...


    res1 = ...
    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).

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.

  • 4
    $\begingroup$ 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. $\endgroup$ Commented Aug 21, 2015 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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