我尝试使用图像加载/存储功能。我需要几个图像,如果它们被编入索引,它便于编码,所以我想使用image2DArray uniform.But它似乎只有image2DArray的第一层换句话说,iimage2DArray的表现就像一个iimage。
片段着色器(仅由fs使用):
#version 420
coherent uniform layout(r32i) iimage2DArray sigmal;
out vec4 FragColor;
void main()
{
ivec2 coord = ivec2(gl_FragCoord.xy);
int increment=100;
int v=imageAtomicAdd(sigmal,ivec3(coord,**1**),increment); //the promlem is that it seems
//the coordinate z has no influence
FragColor =vec4(coord/screenSize,v/1000.0f,1);
}
c ++代码: 结合:
glGenTextures(1,&image);
glBindTexture(GL_TEXTURE_2D_ARRAY,image);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,0);
//Allocate the storage.
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32I, width, height, layerCount);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, layerCount, GL_RED_INTEGER, GL_INT, clearData); //
glBindImageTexture(0, image, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32I);
//Always set reasonable texture parameters
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
渲染:
glBindTexture(GL_TEXTURE_2D_ARRAY,image);
shader.use();
shader.setUniform("sigmal",0);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
shader.unuse();
glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);
int *data=new int[viewport[2]*viewport[3]*layerCount];
glBindTexture(GL_TEXTURE_2D_ARRAY,image);
glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB,0);
glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 , GL_RED_INTEGER, GL_INT,data );
printf("%d %d\n",data[0],data[viewport[2]*viewport[3]]);
printf结果是数据[0],我认为是第一层逐帧增加的第一个纹素,而数据[viewport [2] * viewport [3]]我认为是第一个纹素第二层没有变化。我认为合理的结果是相反的。