I want a compute shader that writes 1's in the output buffer. I compile the shader and attach it to the program without problem, then I call glDispatchCompute() function and I wait until compute shader ends. But when I see the array, there are only 0's.
Can anyone tell me where is the mistake?
This is my compute shader code:
#version 430 core layout (local_size_x = 2) in; layout(std430, binding=0) writeonly buffer Pos{ float Position[]; }; void main(){ Position[gl_GlobalInvocationID.x] = 1.0f; }
And this is some of my main:
GLuint program_compute = 0, SSBO = 0; //...(Create, compile and link the program with the shader)... vector<GLfloat> initPos; int num_numeros = 12; for (int i = 0; i < num_numeros; i++){ initPos.push_back(0.0f); } glUseProgram(program_compute); glGenBuffers(1, &SSBO); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO); glBufferData(GL_SHADER_STORAGE_BUFFER, num_numeros * sizeof(GLfloat), &initPos, GL_DYNAMIC_DRAW); glDispatchCompute(num_numeros/2, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0); for (int i = 0; i < num_numeros; i++){ cout << "p" << i << ": " << initPos[i] << endl; } cout << endl;
EDIT: Finally it works. Thank you BDL.
As BDL has said, I've forgotten read the buffer back from GPU memory. Now it works. This is the new code:
GLuint program_compute = 0, SSBO = 0; // ...The same code as above glDispatchCompute(num_numeros/2, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO); GLfloat *ptr; ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY); initPos.clear(); for (int i = 0; i < num_numeros; i++){ initPos.push_back(ptr[i]); } glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); for (int i = 0; i < num_numeros; i++){ cout << "p" << i << ": " << initPos[i] << endl; } cout << endl;
EDIT: Thank you Andon M. Coleman.
I read from a write only buffer. Here is the line fixed:
ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);