How to retrieve data from Compute Shader to CPU?

Computer Graphics Asked by Ethan Ma on November 15, 2021

I want to retrieve data that has created by the compute shader but have no clue how it works. After researching a few forums and articles on how it should work and this is what I came up with.

Compute Shader Code

#version 430 core

layout  (local_size_x  =  100)  in;

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

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

Program 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);
        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.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, 0);
        GL20.glUseProgram(computeProgram);//start the shader program
        GL43.glDispatchCompute(group_x_size, 1, 1);

        GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, ssbo);
        String values = GL43.glMapBuffer(GL43.GL_SHADER_STORAGE_BUFFER, GL43.GL_WRITE_ONLY).asFloatBuffer().toString();
        return null;

However this code just simply will not work the way I want it to. When I go to print out the Values it comes up null. Is there something wrong with the code or is there a better way to do it. I’ve looked through many forums but most of them aren’t written in java.

Add your own answers!

Related Questions

BRDF for point lights should not return values over 1

1  Asked on August 27, 2021 by emil-kabirov


Dynamic Array in GLSL

3  Asked on January 13, 2021 by archmede


Combine box shadow with a signed distance field

0  Asked on January 2, 2021 by weichsem


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir