Hi ,
I want to use glShaderStorageBinding to bind my buffer in compute shader. I am using 2 buffers , one to write data and one to read data, and binding the buffer objects using glShaderStorageBinding. But getting wrong result. below is my 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];
}
Binding buffers as :
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);
using opengl 4.3.
Default binding in shader works and return correct result as 1, but I dont want to use the default binding in shader.
The shader and code is compiling properly. Also it also returning buffer index as 0 and 1 respectively. But it is always returning 0 value after reading the Obuffer. Do we need to do something else to bind the buffers?