views:

294

answers:

2

Are there any guidelines for writing efficient shaders in GLSL? Does the compiler handle most of the optimization?

+3  A: 

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 #pragmas to tweak the efficiency.

Kronikarz
+1  A: 

While many traditional c optimizations work for glsl, there does exist some specific optimizations for GLSL. If you are new to shader programming, dont spend too much with optm, your compiler can do extremely efficient jobs for you. You can gather Some other advanced optm techniques as you dive deeper into graphics programming. good luck.

dex