让我们考虑以下GLSL示例:
1。
uniform sampler2D t;
...
void main() {
float val_r = texture2D(t, coords).r;
float val_g = texture2D(t, coords).g;
float val_b = texture2D(t, coords).b;
float val_a = texture2D(t, coords).a;
2。
uniform sampler2D t;
...
void main() {
vec4 data = texture2D(t, coords);
float val_r = data.r;
float val_g = data.g;
float val_b = data.b;
float val_a = data.a;
3。
uniform sampler2D t;
...
void main() {
vec4 data1 = texture2D(t, coords);
vec4 data2 = texture2D(t, coords);
vec4 data3 = texture2D(t, coords);
vec4 data4 = texture2D(t, coords);
float val_r = data1.r;
float val_g = data2.g;
float val_b = data3.b;
float val_a = data4.a;
2.比1.快吗?
还是由于明显的编译器优化而使它们相等?
那3。当然,这没有任何意义,但是为了我的着色器代码的清晰起见,可能会出现这种多余的纹理查找。我想知道它们是否经过优化/缓存,还是应该避免使用。
最佳答案
规范中没有提到GLSL编译器应该进行任何优化。这意味着更好的方法是避免过早的悲观化。此代码
uniform sampler2D t;
void main() {
vec4 val = texture2D(t, coords);
是最好的。没有理由创建float变量,因为您始终可以直接通过
val.x
等访问vector的组件。
关于optimization - GLSL-是否优化了纹理查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11647793/