18

是否有在 GLSL 中编写高效着色器的指南?编译器是否处理大部分优化?

4

2 回答 2

21

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.

于 2010-04-12T14:15:47.687 回答
2

虽然许多传统的 c 优化适用于 glsl,但确实存在一些针对 GLSL 的特定优化。如果您是着色器编程的新手,请不要在 optm 上花费太多,您的编译器可以为您完成非常高效的工作。当您深入了解图形编程时,您可以收集一些其他高级 optm 技术。祝你好运。

于 2010-04-13T04:17:48.640 回答