Reputation: 1009
In a fragment shader, which of these is likely to be faster?
vec3 val = vec3(0);
if (bool(uform)) {
val = texture(tex, texcoord).rgb;
}
or
vec3 val = texture(tex, texcoord).rgb * uform;
"uform" is an int that will be either 0 or 1.
I've been using the first one, but I've heard plenty of times that branching should be avoided. On my system, (nvidia 680) I can't really notice a difference between the two, but on most recent systems, which one would likely be faster?
Upvotes: 0
Views: 709
Reputation: 48196
The shader compiler is free to optimize the first to
vec3 val = texture(tex, texcoord).rgb * sign(abs(uform));
which is also branch-less. However it is not guaranteed to do so.
So on a sufficiently optimizing driver the difference will be minimal.
Though some compiler can optimize the flow if it is all dependent on uniforms. What will happen is the the driver will recompile and reoptimize the program with all uniforms inlined. This usually happens to eliminate branches but is fully implementation defined.
Upvotes: 1