我目前正在尝试在OpenGL中使用framebuffers实现延迟着色。帧缓冲区本身工作得很好,但是当我在着色器中不使用所有统一采样器时,其中一个纹理就会停止工作。
这是我的片段着色器,帧缓冲的测试演示:
#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
,因为它没有在着色器中使用。
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上重现这种行为。
答案 0 :(得分:0)
我发现了这个错误,它与着色器无关。在我的ShaderProgram
课程中,我使用glGetActiveUniform
查找有关统一变量的信息。我稍后使用统一索引作为位置,但必须通过调用glGetUniformLocation
单独查询位置。该问题在this博文中有所描述。