Are there any guidelines for writing efficient shaders in GLSL? Does the compiler handle most of the optimization?
A few tips are here: Common mistakes in GLSL
Also, avoid branching whenever possible. That is, if
and while
statements, and for
statements that have a comparison with a variable, for example:
for (int i=0; i<n; i++) {}
will be slow. However,
for (int i=0; i<10; i++) {}
should be much faster, because most of the time the loop is unrolled, and when it's not all the shading units are still executing the same code at the same time, so there is no performance penalty.
Instead of branching, try using conditional compilation using the preprocessor.
Also, check out nVidia and ATI specific #pragma
s to tweak the efficiency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With