让我们考虑以下 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.呢?当然这没有任何意义,但是为了我的着色器代码的清晰,可能会出现这种冗余的纹理查找。我想知道它们是否以某种方式优化/缓存,或者我是否应该避免它们。