7

Doing some research into antialiasing methods, I stumbled upon this piece of code (grabbed from Nvidia's FXAA shader):

if(!pairN) lumaN = lumaS; if(!pairN) gradientN = gradientS; if(!pairN) lengthSign *= -1.0; 

Is there a good reason it's not written as the following instead?

if (!pairN) { lumaN = lumaS; gradientN = gradientS; lengthSign *= -1.0; } 

I assume it's an optimization of some sort? Just seems really counterintuitive...

1 Answer 1

7

It is trivial for a GLSL compiler to convert this code:

if(!pairN) lumaN = lumaS; if(!pairN) gradientN = gradientS; if(!pairN) lengthSign *= -1.0; 

into this branchless code:

lumaN = pair ? lumaN : lumaS; gradientN = pair ? gradientN : gradientS; lengthSign *= pair ? 1.0 : -1.0; 

It is branchless in the sense it can be expressed as:

float a = float( pair ); float b = 1.0 - a; lumaN = a * lumaN + b * lumaS; gradientN = a * gradientN + b * gradientS; lengthSign *= a * 1.0 + b * -1.0; 

This is somewhat efficient optimization on mobile GPUs.

Automatically optimizing an entire if block is not so trivial.

Sign up to request clarification or add additional context in comments.

3 Comments

Would modern GPU compilers not optimize the intuitive version then?
It looks like they are unable to optimize a large if block.
@skiwi If it's one thing I've learned about GLSL implementations over the years... the compiler does a lot of unexpected stuff. Some optimizations blow my mind and others I can hardly believe were missed. Trying both ways and looking at the intermediate output via glGetProgramBinary is as good as you can get.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.