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

时间:2021-06-17 06:54:22

Im currently trying to implement deferred shading with framebuffers in OpenGL. The framebuffer itself works perfectly, but when I dont use all uniform samplers in the shader, one of the textures stop working.

我目前正试图在OpenGL中使用framebuffers实现延迟着色。帧缓冲区本身运行良好,但是当我在着色器中不使用所有均匀采样器时,其中一个纹理停止工作。

This is my fragment shader, a test demonstration of the framebuffer:

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

#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);        
        }
    }
}

Note that the unused line vec3 pos = texelFetch(positionTex,texelCoord,0).xyz; is commented out.
The sampler uniform uploads are logged:

注意未使用的行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 is of course not found, because it isnt used in the shader.

当然没有找到positionTex,因为它没有在着色器中使用。

The result image for the Sponza scene: GLSL:采样器上传仅在删除未使用的采样器时有效

Sponza场景的结果图像:

If I dont comment out the line vec3 pos = texelFetch(positionTex,texelCoord,0).xyz;, texCoordTex doesnt work anymore and the uniform upload creates a GL_INVALID_OPERATION error.

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

The result looks like this, because the texture coordinates are zero. GLSL:采样器上传仅在删除未使用的采样器时有效

结果如下所示,因为纹理坐标为零。

The log output shows the errors:

日志输出显示错误:

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

Am I overlooking something or is this a driver bug? I use a ATI HD 6870, and I can reproduce this behavior on Linux and Windows.

我忽略了什么或这是一个驱动程序错误?我使用的是ATI HD 6870,我可以在Linux和Windows上重现这种行为。

1 个解决方案

#1


I found the bug, it was unrelated to the shader. In my ShaderProgram class I use glGetActiveUniform to find information about the uniform variables. I used the uniform index as location later, but the location must be queried separately by calling glGetUniformLocation. The bug is described in this blog post.

我发现了这个错误,它与着色器无关。在我的ShaderProgram类中,我使用glGetActiveUniform来查找有关统一变量的信息。我稍后使用统一索引作为位置,但必须通过调用glGetUniformLocation单独查询位置。该博客文章中描述了该错误。

#1


I found the bug, it was unrelated to the shader. In my ShaderProgram class I use glGetActiveUniform to find information about the uniform variables. I used the uniform index as location later, but the location must be queried separately by calling glGetUniformLocation. The bug is described in this blog post.

我发现了这个错误,它与着色器无关。在我的ShaderProgram类中,我使用glGetActiveUniform来查找有关统一变量的信息。我稍后使用统一索引作为位置,但必须通过调用glGetUniformLocation单独查询位置。该博客文章中描述了该错误。