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

Luca Bianconi lucabianco92 at gmail.com
Wed May 11 06:12:39 PDT 2016


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








More information about the osg-users mailing list