3

Using OpenGL 4.3, I want to know how to bind shader buffer blocks using glShaderStorageBinding.

'binding' qualifiers works fine, but I don't want to use them.

I have written the following code:

    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];
  }

Expecting result as 1, but returning value as 0 in Obuffer.

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

1 回答 1

1

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

于 2013-10-03T11:28:53.233 回答