【发布时间】:2014-04-18 06:24:56
【问题描述】:
最近的 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
在“编译时间”与“优化时间”之间是否存在已知的权衡,由实现者或规范设置?
【问题讨论】:
-
你应该阅读humus.name/index.php?page=Articles&ID=6。简短的回答是“你不能确定”
-
这个读数令人沮丧。大文件通过。我想我仍然需要三思而后行我在写什么。然而 GLSL 似乎为一些优化留出了更少的空间(没有饱和,rcp,rsqrt,sincos...)
-
这也很大程度上取决于平台。例如,移动平台在编译着色器时没有太多时间用于优化。
标签: c++ opengl glsl compiler-optimization