如何使用glShaderStorageBinding绑定着色器缓冲区块?

时间:2013-10-01 06:36:22

标签: c opengl glsl opengl-4 compute-shader

使用OpenGL 4.3,我想知道如何使用glShaderStorageBinding绑定着色器缓冲区块。

'binding'限定符工作正常,但我不想使用它们。

我写了以下代码:

    GLint i=0, j=0, Ibuffer[32], *Obuffer;
    GLuint shader_buffer[2], buffer_index;

   for(i=0; i<32; i++)  
   {
       Ibuffer[i]=1;

   }

    Obuffer=(GLint*) malloc(32*sizeof(GLint));

glGenBuffers(2, shader_buffer, nResult);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, shader_buffer[0]);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(Ibuffer), Ibuffer, GL_DYNAMIC_DRAW);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, shader_buffer[1], nResult);
glBufferData( GL_SHADER_STORAGE_BUFFER, 32*sizeof(GLint), NULL, GL_DYNAMIC_READ);
GLint x;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &x);
glUseProgram(computeshaderprogObj);
buffer_index=glGetProgramResourceIndex(computeshaderprogObj, GL_SHADER_STORAGE_BLOCK, "iB");
printf("buffer_index = %d GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS = %d", buffer_index, x);
glShaderStorageBlockBinding(computeshaderprogObj, buffer_index, 0);

buffer_index=glGetProgramResourceIndex(computeshaderprogObj, GL_SHADER_STORAGE_BLOCK, "oB");
printf("buffer_index = %d", buffer_index);
glShaderStorageBlockBinding(computeshaderprogObj, buffer_index, 1);

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, shader_buffer[0]);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, shader_buffer[1]);

 glDispatchCompute(4, 1, 1);
 glBindBuffer(GL_SHADER_STORAGE_BUFFER, shader_buffer[1]);
 Obuffer = (GLint*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint)*32, GL_MAP_READ_BIT); 
 // Reading the buffer
 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

Compute Shader:

layout (local_size_x = 32, local_size_y = 1, local_size_z = 1) in;
layout(std140) buffer iB {
   int ibuf[];
}g_in;

layout(std140) buffer oB {
  int obuf[];
}g_out;
void main() {
uint LocalInvocationIndex = gl_LocalInvocationIndex;
g_out.obuf[LocalInvocationIndex] = g_in.ibuf[LocalInvocationIndex];
  }

期望结果为1,但在Obuffer中将值返回为0。

output:
buffer_index = 0 GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS = 96
buffer_index = 1
value at Obuffer[1]=0

1 个答案:

答案 0 :(得分:0)

我找到了答案,由于'std140'布局,它给出了错误。它保存结果为1000,1000,1000等。因此将布局限定符更改为已打包。 感谢。