我使用1D纹理来存储需要由片段着色器访问的单通道整数数据。从应用程序来看,整数数据类型是GLubyte,需要在着色器中作为无符号整数进行访问。以下是纹理的创建方式(请注意,之后还有其他纹理单元被绑定,我希望与此问题无关):
GLuint mTexture[2];
std::vector<GLubyte> data;
///... populate with 289 elements, all with value of 1
glActiveTexture(GL_TEXTURE0);
{
glGenTextures(1, &mTexture[0]);
glBindTexture(GL_TEXTURE_1D, mTexture[0]);
{
glTexImage1D(GL_TEXTURE_1D, 0, GL_R8UI, data.size(), 0,
GL_RED_INTEGER, GL_UNSIGNED_BYTE, &data[0]);
}
glBindTexture(GL_TEXTURE_1D, 0);
}
glActiveTexture(GL_TEXTURE1);
{
//Setup the other texture using mTexture[1]
}
片段着色器如下所示:
#version 420 core
smooth in vec2 tc;
out vec4 color;
layout (binding = 0) uniform usampler1D buffer;
layout (binding = 1) uniform sampler2DArray sampler;
uniform float spacing;
void main()
{
vec3 pos;
pos.x = tc.x;
pos.y = tc.y;
if (texelFetch(buffer, 0, 0).r == 1)
pos.z = 3.0;
else
pos.z = 0.0;
color = texture(sampler, pos);
}
在此示例中,texelFetch返回的值基本上决定了从2D数组中使用哪个纹理图层作为最终输出颜色。我希望它返回值1,但它总是返回0并命中片段着色器中的else子句。使用NVIDIA的Nsight工具,我可以看到纹理确实包含值1,289次: