I have written a GLSL fragment shader and pretty quickly found out that if (performance_needed) branching_is_something_to_avoid
So I wanted to be smart. My code is a flow simulation, reading inputs from sampler2D's. Among other data that are saved in those textures, there is a float value named "obst" that is 0.0 for "no obstacle" and 1.0 for "obstacle":
read = texture2D(Texture3, gl_TexCoord.st);
obst = read.x;
Now, further down in main() there is an if-clause:
fOut = fIn - omega * (fIn-fEq); // works perfectly,
if (obst==1.0) fOut = fIn[opp]; // but is slow!
So, in order to avoid that if-clause, I made it a sum, abusing the "obst" value to switch of the term to be neglegted:
fOut = (1.0-obst)*(fIn - omega * (fIn-fEq)) + fIn[opp]*obst;
And here comes the catch: the above line does _not_ work. I had a hard time finding out that I have to switch the position of "obst":
... + fIn[opp]*obst; // original code, wont work, is equal to ... + 0.0
... + obst*fIn[opp]; // works very well
Is this by design or is it a feature?
I am on Ubuntu 10.04, Intel Core 2 Duo, ATI Mobility HD 2600