Moving float array between compute shader and OF

I have a hard time finding any basic information about this topic.

I would like to move a float array from OF to a compute shader, calculate something and return a float array, but how do I get the array from ofBufferObject?

My compute shader looks like this

#version 440

struct SomeType {
float a;
};

layout(local_size_x = 100) in;

layout (std430, binding = 0) buffer FloatInput {
readonly SomeType data[];
} floatInput;

layout (std430, binding = 1) buffer FloatOutput {
writeonly SomeType data[];
} floatOutput;

void main(){
uint ident = gl_GlobalInvocationID.x;

// Some random function for demonstration
floatOutput.data[ident].a = floatInput.data[ident].a * 0.5f;
}

This is how I load the data to the shader from OF

void ofApp::setup(){

compute.setupShaderFromFile(GL_COMPUTE_SHADER, “compute.glsl”);
compute.linkProgram();

shaderInput.resize(100);

for (auto & f : shaderInput) {
f.a = ofRandom(0.0f, 0.9f);
}

floatBufferInput.allocate(shaderInput, GL_FLOAT);
floatBufferOutput.allocate(shaderInput, GL_FLOAT);
floatBufferInput.bindBase(GL_SHADER_STORAGE_BUFFER, 0);
floatBufferOutput.bindBase(GL_SHADER_STORAGE_BUFFER, 1);
}

//--------------------------------------------------------------
void ofApp::update(){

compute.begin();
compute.dispatchCompute(100, 1, 1);
compute.end();

// Tried a lot of thing here :slight_smile:
}

to read back an ofBufferObject you can use map which will return a pointer to the buffer memory, copy it somewhere or use it and then useunmap when you are done

Can you give me a hint?

I am doing this:

SomeType* floatArray = floatBufferOutput.map(GL_SHADER_STORAGE_BUFFER);
floatBufferOutput.unmap();

But my pointer is null.

Haha it deletes my brackets :slightly_smiling:

THIS:

SomeType* floatArray = floatBufferOutput.map<SomeType>(GL_READ_ONLY);
floatBufferOutput.unmap();

SomeType* floatArray = floatBufferOutput.map<SomeType>(GL_READ_ONLY);
// do something with floatArray
floatBufferOutput.unmap();

Thanks Arturo

It turns out that I used the wrong enum for allocating the buffers

Documentation for allocating can be found here: https://www.opengl.org/sdk/docs/man4/html/glBufferData.xhtml