openglopengl-esglslhlslglsles

How to force GLSL branching?


I'm working on a fragment shader. It works, but it still needs some optimisation.

As far as I know, most of the cases branches in GLSL are flatten, so both cases are executed. I've eliminated most of the if-else conditions, but there are some of them, which have to stay as they are, because both branches are expensive to execute. I know, that in HLSL there is a [branch] keyword for this problem. But how is it possible to solve it in GLSL?

My code looks like this (the conditions are not uniform, their results depend on calculations in the shader):

if( condition ) {
    expensive calculations...
}

if( condition2 ) {
    expensive calculations...
}

if( condition3 ) {
    expensive calculations...
}

...

One "expensive calculation" can modify the variables, on which a condition will depend. It is possible, that more than one calculation is executed.

I know, that there are older or mobile GPU-s, which does not support branching at all. In that case, there is nothing to do with this issue


Solution

  • GLSL has no mechanism to enforce branching (or to enforce flattening a branch).