For quite some time, I've been avoiding branching in my shader code by, instead of
float invert_value(in float value)
{
if(value == 0.0)
return 0.0;
else
return 1.0 / value;
}
writing 'clever' code like this
float invert_value_ifless(in float value)
{
float sign_value = sign(value);
float sign_value_squared = sign_value*sign_value;
return sign_value_squared / ( value + sign_value_squared - 1.0);
}
This returns exactly what the first function does and has no branches, thus it is faster.
Or is it? Am I fighting with ghosts here?
How to profile graphics shaders for speed? I am most interested in recent mobile platforms (Android) but any advice on graphics profiling in general would be welcome!