How to read SSBO data into CPU in LWJGL

407 Views Asked by At

I want a compute shader that writes 1's in the output buffer. I compile the shader and attach it to the program, then I call glDispatchCompute() function and I wait until compute shader ends. The only problem I have now is that I am not sure how to convert the outputted SSBO into a float array.

Retrieval code

public float[] useProgram(int x_size, int y_size, int z_size){
        
        this.group_x_size = (int)Math.ceil(x_size/100);
        this.group_y_size = (int)Math.ceil(y_size/100);
        this.group_z_size = (int)Math.ceil(z_size/100);
        float[] data = new float[x_size];
        FloatBuffer buffer = BufferUtils.createFloatBuffer(x_size);
        buffer.put(data);
        int ssbo = GL15.glGenBuffers();// creates empty VBO as an object and stores it in OpenGl memory
        GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, ssbo);
        GL43.glBindBufferBase(GL43.GL_SHADER_STORAGE_BUFFER, 0, ssbo);
        GL15.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER,buffer,GL15.GL_DYNAMIC_DRAW);
        GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, 0);
        
        GL20.glUseProgram(computeProgram);//start the shader program
        GL43.glDispatchCompute(group_x_size, 1, 1);
        GL43.glMemoryBarrier(GL43.GL_BUFFER_UPDATE_BARRIER_BIT );
        GL43.glBindBufferBase(GL43.GL_SHADER_STORAGE_BUFFER, 0, 0);

        GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, ssbo);

        float[] values;

        //Convert SSBO to a float array here

        System.out.println(Arrays.toString(values));
        GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, 0);
        
        return values;
    }

Compute Shader

#version 430 core

layout  (local_size_x  =  100)  in;

layout(std430, binding=0) buffer Pos{
    float Position[];
};

void main(){
    Position[gl_GlobalInvocationID.x] = 1.0;
}

I have been trying to figure this out for hours now and I still can't find any threads that answers how to convert glMapBuffer to a float[]. I'm not sure if it because my code is wrong or if there is a better way to do it. This is written in java using LWJGL.

0

There are 0 best solutions below