[osg-users] Read back data from a storage buffer object

219 views
Skip to first unread message

Luca Bianconi

unread,
May 11, 2016, 6:37:01 AM5/11/16
to osg-...@lists.openscenegraph.org
Hi all,
for an university project I'm trying to set up a compute shader to execute some computation writing results to a shader storage buffer object.
I tried to run a trivial shader code where the data passed to the shader is a float array of 10 elements initialiazed at 1. At execution time one element, for example position 0, is changed to another value.
I don't know to read back data from ssbo into my application code where I need the data computed by the shader.
I'm pretty sure the shader is correctly executed and the ssbo correctly passed to it.
...

Thank you!

Cheers,
Luca

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=67072#67072





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Sebastian Messerschmidt

unread,
May 11, 2016, 6:49:55 AM5/11/16
to osg-...@lists.openscenegraph.org
Hi Luca,

Take a look at the osgSSBO example. The ShaderStorageBufferCallback can be used to retrieve the results.
You might have to add a
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

in order to retrieve the result a the correct moment. See the osggpucull example for the barrier usage. 

Luca Bianconi

unread,
May 11, 2016, 9:05:15 AM5/11/16
to osg-...@lists.openscenegraph.org
Hi Sebastian,
thank you for your answer.

I'm trying to use ShaderStorageBufferCallback by osgSSBO example in a minimal piece of code which is:


Code:


#include <osgViewer/Viewer>
#include <osg/BufferIndexBinding>

using namespace osg;


class ShaderStorageBufferCallback : public osg::StateAttributeCallback
{
public:
void operator() (osg::StateAttribute* attr, osg::NodeVisitor* nv)
{
//if you need to process the data in your app-code , better leaving it on GPU and processing there, uploading per frame will make it slow

osg::ShaderStorageBufferBinding* ssbb = static_cast<osg::ShaderStorageBufferBinding*>(attr);
osg::ShaderStorageBufferObject* ssbo
= static_cast<osg::ShaderStorageBufferObject*>(ssbb->getBufferObject());

osg::FloatArray* array = static_cast<osg::FloatArray*>(ssbo->getBufferData(0));

float someValue = array->at(0);
osg::notify(INFO) << "someValue now: " << someValue << std::endl;

}
};

int mymain(){

FloatArray * dati = new FloatArray;
dati->resize(10, 1.0);

ShaderStorageBufferObject *ssbo = new osg::ShaderStorageBufferObject;
dati->setBufferObject(ssbo);

ShaderStorageBufferBinding * ssbb = new ShaderStorageBufferBinding(2, ssbo, 0, 10 * sizeof(GLfloat));
ssbb->setUpdateCallback(new ShaderStorageBufferCallback);

Shader * shader = new osg::Shader(osg::Shader::COMPUTE);
shader->loadShaderSourceFromFile("shaders/lightpixels.cs");
osg::ref_ptr<osg::Program> computeProg = new osg::Program;
computeProg->setComputeGroups(512 / 16, 1, 1);
shader->setType(Shader::COMPUTE);
computeProg->addShader(shader);

osg::Geometry * geom = createTexturedQuadGeometry(Vec3(-0.5, 0, -0.5), Vec3(1.0, 0, 0.0), Vec3(0, 0, 1.0));
geom->getOrCreateStateSet()->setAttributeAndModes(computeProg, osg::StateAttribute::ON);
geom->getOrCreateStateSet()->setAttributeAndModes(ssbb, osg::StateAttribute::ON);

osgViewer::Viewer viewer;
viewer.setSceneData(geom);
viewer.setUpViewInWindow(100, 100, 640, 480);
viewer.realize();
while (!viewer.done()){
viewer.frame();
}

return 0;

}




shader code is


Code:

#version 430
layout (local_size_x = 16, local_size_y = 16) in;

//layout (binding=0, rgba32f) uniform image2D targetImage;
layout(std140, binding = 2) buffer dati{float dataBuffer[];};

void main() {
dataBuffer[0] = 348.0;
}




The callback is triggered but it print '1' as value. Like if buffer data was not changed. How do i use glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)?

Thank
Luca

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=67074#67074
Reply all
Reply to author
Forward
0 new messages