I’m trying to work out how to get buffer data back out of a compute shader once it has been run.
I think i’m doing the basics right. I’m using Kha’s Shader Storage Buffer Object (SSBO) to modify some data in the compute shader, then after the compute call has completed I’m wondering how to access the changed value(s)…
ssbo = new ShaderStorageBuffer(100,VertexData.Float1);
Compute.setShader(Shaders.test_comp);
Compute.setBuffer(ssbo,0);
Compute.compute(1,1,1);
and the computer shader:
layout(binding=0) buffer Pos{
float Position[];
};
layout (local_size_x = 1, local_size_y = 1) in;
void main() {
Position[0] = 42.0;
}
Debugging the buffer in Xcode's frame capture show's the value 42.0 has been set. I then tried locking the SSBO to get a reference to the array but the value seems to be unchanged.
I believe in OpenGL there’d be something like glMapBuffer or glBufferData/subBufferData to get the data back out, but looking at the Kha API (and the underlying code) it doesn’t seem to use those functions for retrieval. The API for SSBOs supports lock
and unlock
but I assume that is only for putting the data onto the GPU?
Also, i’m working on a Mac so we’re really talking about Metal (graphics4 on top of graphics5) so those OpenGL calls wouldn't apply everywhere anyway, a whole other implementation of course…
I know I could write a results to a texture but i’m actually interested in modifying data buffers or using them like OpenGL’s Transform Feedback or generally poking around...
So the question is, how should I deal with SSBOs in the CPU side? How do I read data back out after the Compute Shader has manipulated it?
Also, I know the term SSBO is not really applicable when it comes to Metal, but for Kha’s purposes the name is used for consistency across implementations. Ideally what i’d like to be able to do is use an SSBO as a VertexBuffer in a subsequent render call - so what would be the correct (most efficient) way of dealing with the two buffer types interchangeably?
Any help is greatly appreciated.