dec*_*eck 4 c++ opengl glsl compiler-optimization
最近的GLSL编译器是否智能/优化?
换句话说,如果我没脑子并且写下面的内容,最近的编译器会节省我的时间并优化掉不必要的代码,或者我应该总是小心我写的东西?
// All of the values are constants
if (3.7 == 3.7) // Will the condition be executed or removed at build time?
x++;
// Will this whole block be entirely removed? (or should I use macros)
if (1 == 2)
x++;
for (i = 0; i < 0; ++i) // Remove this
x++;
for (i = 0; i < varA * varB; ++i) // Compute varA * varB once, outside the loop
x++;
vec3 v = vec3(0);
if (length(v) > 0) // Remove
x++;
float p = mix(varA, varB, 1); // p = varB
float p = 5;
p *= uniform * 0; // Just set a = 0 from the start
float p = 5;
p *= 1; // Remove that
Run Code Online (Sandbox Code Playgroud)
现在还有很多我无法解决的问题,但你应该明白这一点.
此外,最近的编译器可以自动检测不太明显的优化,如下所述:http: //www.opengl.org/wiki/GLSL_Optimizations
是否存在"编译时间"与"优化时间"之间的已知权衡,由实现者或规范设置?
GLSL编译器在浮点优化方面非常保守.您无法确定任何特定的优化.经验法则是:优化您可以做的任何事情,并且不希望GLSL编译器提供任何帮助.
阅读Emil Persson 的高级着色语言中的低级思维,了解有趣的细节和案例研究.
PS:这个答案可能听起来很悲观.但是,GLSL编译器仍然在优化代码方面做了很多工作.只是不要指望它并尽力而为.