Yes, conditionals are pure evil in the shaders kingdom. Everything is fancy and happy as long as you stand far from conditionals. Why? Because conditionals evokes the worst nightmare for GPU processing: Branches.

However, you don't came here looking for an advise against conditionals, you come here to learn how to make a conditional without create branches. An ancient knowledge lost in time and keep in secret by real-time lords that will be unlocked for you in the next lines.

Some of those secrets really depends of the smartness of you GPU board, but I'll try to summarize these aspects in order to provide a powerful recipe to be used in order to avoid branches.

The first thing to know is that branches only happens if something after the branches depends on thing into these branches.

If your target is a mobile platform, you can have even more problems as it is usually rendered in chuncks and a freezed chunk can impact all the others chunks.

Step

Saturate

Floor

vec4 when_eq(vec4 x, vec4 y) {

return 1.0 - abs(sign(x - y));

}

vec4 when_neq(vec4 x, vec4 y) {

return abs(sign(x - y));

}

vec4 when_gt(vec4 x, vec4 y) {

return max(sign(x - y), 0.0);

}

vec4 when_lt(vec4 x, vec4 y) {

return max(sign(y - x), 0.0);

}

vec4 when_ge(vec4 x, vec4 y) {

return 1.0 - when_lt(x, y);

}

vec4 when_le(vec4 x, vec4 y) {

return 1.0 - when_gt(x, y);

}

vec4 and(vec4 a, vec4 b) {

return a * b;

}

vec4 or(vec4 a, vec4 b) {

return min(a + b, 1.0);

}

vec4 xor(vec4 a, vec4 b) {

return (a + b) % 2.0;

}

vec4 not(vec4 a) {

return 1.0 - a;

}