C语言 如何使用 glShaderStorageBinding 绑定着色器缓冲区块


使用

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

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

我编写了以下代码:

    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);

计算着色器:

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

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

相关内容

  • 没有找到相关文章