GLSL编译器是否得到了很好的优化?

Are GLSL compilers well optimized

本文关键字:很好 优化 编译器 是否 GLSL      更新时间:2023-10-16

最近的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

现在还有很多事情我说不出来,但你应该明白这一点。

同样,最近的编译器可以自动检测不太明显的优化吗?http://www.opengl.org/wiki/GLSL_Optimizations

在"编译时间"answers"优化时间"之间是否存在已知的权衡,由实现者或规范设置?

GLSL编译器在浮点优化方面非常保守。您无法确定任何特定的优化。经验法则是:尽你所能优化,不要指望GLSL编译器的任何帮助。

阅读Emil Persson在高级着色语言中的低级思维,了解有趣的细节和案例研究。

p。S:这个答案可能听起来很悲观。然而,GLSL编译器仍然在优化代码方面做了很多出色的工作。别指望了,尽力而为吧。

相关文章: