我正在使用https://www.opengl.org/wiki/Image_Load_Store编写Win32应用程序 - 我想从附加到Pixel着色器的纹理中读取一个值(一旦它正在工作,我将改变要写入的操作)。我正在使用GLFW和GLEW,支持我的gfx卡的OGL版本是4.2。相关的纹理设置代码如下所示:
typedef unsigned int BufferType;
int buf_w = 3;
int buf_h = 3;
int buf_size = buf_w * buf_h;
BufferType * bufferCounter = new BufferType[buf_size];
for (int i = 0; i < buf_size; ++i)
bufferCounter[i] = 200;
glActiveTexture( GL_TEXTURE0 );
glEnable( GL_TEXTURE_2D );
glGenTextures( 1, &tex2d );
glBindTexture( GL_TEXTURE_2D, tex2d );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0 );
glTexImage2D( GL_TEXTURE_2D, 0, GL_R32UI, buf_w, buf_h, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, bufferCounter );
主循环:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program);
glBindVertexArray(vao);
glBindTexture( GL_TEXTURE_2D, tex2d );
glBindImageTexture( 0, tex2d, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI ); // bind 2d tex to read_tex
glDrawArrays(GL_TRIANGLES, 0, 3);
顶点着色器无关紧要,只是gl_Position = vec4(vertex_position,1.0);片段着色器:
#version 430
#extension ARB_shader_image_load_store : enable
in vec3 color;
out vec4 frag_color;
readonly layout(r8ui) uniform uimage2D read_tex;
void main()
{
frag_color = imageLoad(read_tex, ivec2(0,0) ); // read from attached tex
}
我用glGetError检查 - 没有错误。着色器也可以编译和链接。屏幕上看不到三角形(但如果我将颜色设置为常数,则为)。关于可能出错的任何想法?我尝试在主循环函数loc = glGetUniformLocation( shader_program, "read_tex" ); glUniform1i( loc, tex2d );
中使用,但它们没有帮助。谢谢你的帮助。不幸的是,我找不到任何关于如何使用这个OGL功能的例子,而且文档很少。