Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips for efficient GLSL code [closed]

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

like image 892
May Oakes Avatar asked Apr 10 '10 19:04

May Oakes


1 Answers

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.

like image 68
GhassanPL Avatar answered Sep 28 '22 02:09

GhassanPL