GLSL:采样器上传仅在删除未使用的采样器时有效

GLSL: sampler upload only works when unused sampler is removed

我目前正在尝试在 OpenGL 中使用帧缓冲区实现延迟着色。帧缓冲区本身工作完美,但是当我不在着色器中使用所有统一采样器时,其中一个纹理停止工作。

这是我的片段着色器,帧缓冲区的测试演示:

#version 330 core

uniform sampler2D texCoordTex;
uniform usampler2D texIndexTex;
uniform sampler2D normalTex;
uniform sampler2D positionTex;
uniform sampler2D depthTex;

float linearizeDepth(float depth){
    float n = 0.1, f = 2000;
    return (2*n)/(f + n - depth*(f-n));
}

out vec4 out_Color;

uniform sampler2DArray texArray;

void main(){
    ivec2 texelCoord = ivec2(gl_FragCoord.xy);
    vec2 texCoord = texelFetch(texCoordTex,texelCoord,0).xy;
    uint texIndex = texelFetch(texIndexTex,texelCoord,0).x;
    vec3 normal = texelFetch(normalTex,texelCoord,0).xyz;
    //vec3 pos = texelFetch(positionTex,texelCoord,0).xyz;
    float depth = linearizeDepth(texelFetch(depthTex,texelCoord,0).x);

    out_Color = vec4(0,0,0,1);

    if(texelCoord.x < 512){
        if(texelCoord.y < 300){
            out_Color.xyz = 0.5*normal+0.5;     
        }else{
            out_Color.xyz = vec3(texCoord,texIndex/50.0);       
        }
    }else{
        if(texelCoord.y < 300){
            out_Color.xyz = texture(texArray,vec3(texCoord,texIndex)).xyz;      
        }else{
            out_Color.xyz = vec3(depth);        
        }
    }
}

请注意,未使用的行 vec3 pos = texelFetch(positionTex,texelCoord,0).xyz; 已被注释掉。
记录采样器统一上传:

13/05/2015 15:11:24,317 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = depthTex, unit = 1] uploaded to shader.
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:11:24,317 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texCoordTex, unit = 2] uploaded to shader.
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:11:24,317 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texIndexTex, unit = 3] uploaded to shader.
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:11:24,317 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = normalTex, unit = 4] uploaded to shader.
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = positionTex, unit = 0] not found in shader.
13/05/2015 15:11:24,317 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:11:24,318 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texArray, unit = 5] uploaded to shader.
13/05/2015 15:11:24,318 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE

positionTex当然找不到,因为没有在shader中使用。

Sponza 场景的结果图像:

如果我不注释行 vec3 pos = texelFetch(positionTex,texelCoord,0).xyz;texCoordTex 将不再起作用,统一上传会产生 GL_INVALID_OPERATION 错误。

结果看起来像这样,因为纹理坐标为零。

日志输出显示错误:

13/05/2015 15:20:26,162 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = depthTex, unit = 1] uploaded to shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:20:26,162 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texCoordTex, unit = 2] uploaded to shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:20:26,162 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texIndexTex, unit = 3] uploaded to shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:20:26,162 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = normalTex, unit = 4] uploaded to shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = positionTex, unit = 0] not found in shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_NONE
13/05/2015 15:20:26,162 INFO  [default] ShaderProgram::ShaderProgram: TextureUnit[ name = texArray, unit = 5] uploaded to shader.
13/05/2015 15:20:26,162 WARN  [default] ShaderProgram::ShaderProgram: glGetError() is GL_INVALID_OPERATION

我是不是忽略了什么或者这是一个驱动程序错误? 我使用 ATI HD 6870,我可以在 Linux 和 Windows.

上重现此行为

我发现了这个错误,它与着色器无关。在我的 ShaderProgram class 中,我使用 glGetActiveUniform 来查找有关统一变量的信息。后面我使用了统一索引作为location,但是必须调用glGetUniformLocation单独查询location。 this 博客 post 中描述了该错误。