openglopengl-4compute-shadershader-storage-buffer

Getting data back from compute shader


I'm fairly new to opengl and I found myself in situation where I need to get data from compute shader but as I miss some critical knowledge I can't get it to work. So I came here, so that maybe you can give me some hints.

Say I have a compute shader like this:

#version 430 core
struct rmTriangle
{
  vec4 probeCenter;
  vec4 triangles[3];
};

layout(std430, binding=9) buffer TriangleBuffer {
  rmTriangle triangles[];
}trBuffer;

//other uniforms, variables and stuff

void main()
{
  //here I make some computations and assign values to the
  //trBuffer's triangles array
}

Now I would like to use the trBuffer's data in my application. I was told to make a shader storage buffer So this is what I did:

private int ssbo;
gl.glGenBuffers(1, &ssbo);
gl.glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
//just allocate enough amount of memory
gl.glBufferData(GL_SHADER_STORAGE_BUFFER, MAX_TRIANGLES * SIZEOF_TRIANGLE, null, GL_DYNAMIC_READ);

Then this:

int blockIndex = gl.glGetProgramResourceIndex(program,GL_SHADER_STORAGE_BLOCK, name.getBytes(), 0);
if (blockIndex != GL_INVALID_INDEX) {
    gl.glShaderStorageBlockBinding(program, blockIndex, index);
} else {
    System.err.println("Warning: binding " + name + " not found");
}

where name = "TriangleBuffer" and index = 9

I know how to access the ssbo I created in my application. What I don't know is how to assign/transfer the TriangeBuffer data into my ssbo.


Solution

  • Add glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, ssbo);

    Also, when I fetch data from SSBOs then I do glMapBufferRange and memcpy the stuff I need.