From 2d1aaedb9c55adc90ce79448860d44a33ad3ad7a Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Thu, 24 Aug 2017 14:15:09 +0200 Subject: [PATCH] fix osggpucull to fit both new BIB and MDI --- examples/osggpucull/osggpucull.cpp | 48 ++++++++++++++---------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/examples/osggpucull/osggpucull.cpp b/examples/osggpucull/osggpucull.cpp index 906a0f59f..78650cc16 100644 --- a/examples/osggpucull/osggpucull.cpp +++ b/examples/osggpucull/osggpucull.cpp @@ -37,12 +37,13 @@ #include #include #include + #include "ShapeToGeometry.h" #include "AggregateGeometryVisitor.h" -#include "DrawIndirectPrimitiveSet.h" #include "GpuCullShaders.h" + // each instance type may have max 8 LODs ( if you change // this value, don't forget to change it in vertex shaders accordingly ) const unsigned int OSGGPUCULL_MAXIMUM_LOD_NUMBER = 8; @@ -176,20 +177,17 @@ struct IndirectTarget IndirectTarget() : maxTargetQuantity(0) { - indirectCommands = new osg::DrawArraysIndirectCommandArray ; + indirectCommands = new osg::DefaultIndirectCommandDrawArrays; + indirectCommands->getBufferObject()->setUsage(GL_DYNAMIC_DRAW); } IndirectTarget( AggregateGeometryVisitor* agv, osg::Program* program ) : geometryAggregator(agv), drawProgram(program), maxTargetQuantity(0) { - indirectCommands = new osg::DrawArraysIndirectCommandArray; + indirectCommands = new osg::DefaultIndirectCommandDrawArrays; + indirectCommands->getBufferObject()->setUsage(GL_DYNAMIC_DRAW); } void endRegister(unsigned int index, unsigned int rowsPerInstance, GLenum pixelFormat, GLenum type, GLint internalFormat, bool useMultiDrawArraysIndirect ) { - - osg::DrawIndirectBufferObject * indirectCommandbuffer=new osg::DrawIndirectBufferObject(); - indirectCommandbuffer->setUsage(GL_DYNAMIC_DRAW); - indirectCommands->setBufferObject(indirectCommandbuffer); - indirectCommandTextureBuffer = new osg::TextureBuffer(indirectCommands); indirectCommandTextureBuffer->setInternalFormat( GL_R32I ); indirectCommandTextureBuffer->bindToImageUnit(index, osg::Texture::READ_WRITE); @@ -199,28 +197,27 @@ struct IndirectTarget // add proper primitivesets to geometryAggregators if( !useMultiDrawArraysIndirect ) // use glDrawArraysIndirect() { - std::vector newPrimitiveSets; + std::vector newPrimitiveSets; - for(unsigned int j=0;jsize(); ++j) - newPrimitiveSets.push_back( new DrawArraysIndirect( GL_TRIANGLES, j*sizeof( osg::DrawArraysIndirectCommand ), indirectCommands ) ); + for(unsigned int j=0;jsize(); ++j){ + osg::DrawArraysIndirect *ipr=new osg::DrawArraysIndirect( GL_TRIANGLES, j ); + ipr->setIndirectCommandArray( indirectCommands); + newPrimitiveSets.push_back(ipr); + } geometryAggregator->getAggregatedGeometry()->removePrimitiveSet(0,geometryAggregator->getAggregatedGeometry()->getNumPrimitiveSets() ); for(unsigned int j=0;jsize(); ++j) geometryAggregator->getAggregatedGeometry()->addPrimitiveSet( newPrimitiveSets[j] ); - /*DrawIndirectBufferBinding should be deprecated - ///attach a DrawIndirect buffer binding to the stateset for non builtin primset DrawArraysIndirect - osg::ref_ptr bb=new osg::DrawIndirectBufferBinding(); - bb->setBufferObject(indirectCommandbuffer ); - geometryAggregator->getAggregatedGeometry()->getOrCreateStateSet()->setAttribute(bb );*/ + + } else // use glMultiDrawArraysIndirect() { - osg::MultiDrawArraysIndirect * mdi=new osg::MultiDrawArraysIndirect(GL_TRIANGLES); - mdi->setIndirectCommandArray(indirectCommands); + osg::MultiDrawArraysIndirect *ipr=new osg::MultiDrawArraysIndirect( GL_TRIANGLES ); + ipr->setIndirectCommandArray( indirectCommands ); geometryAggregator->getAggregatedGeometry()->removePrimitiveSet(0,geometryAggregator->getAggregatedGeometry()->getNumPrimitiveSets() ); - geometryAggregator->getAggregatedGeometry()->addPrimitiveSet(mdi ); - + geometryAggregator->getAggregatedGeometry()->addPrimitiveSet( ipr ); } geometryAggregator->getAggregatedGeometry()->setUseDisplayList(false); @@ -266,7 +263,7 @@ struct IndirectTarget stateset->setAttributeAndModes( drawProgram.get(), osg::StateAttribute::ON ); } - osg::ref_ptr< osg::DrawArraysIndirectCommandArray > indirectCommands; + osg::ref_ptr< osg::DefaultIndirectCommandDrawArrays > indirectCommands; osg::ref_ptr indirectCommandTextureBuffer; osg::ref_ptr< AggregateGeometryVisitor > geometryAggregator; osg::ref_ptr drawProgram; @@ -316,7 +313,7 @@ struct GPUCullData // AggregateGeometryVisitor creates single osg::Geometry from all objects used by specific indirect target AggregateGeometryVisitor::AddObjectResult aoResult = target->second.geometryAggregator->addObject( node , typeID, lodNumber ); // Information about first vertex and a number of vertices is stored for later primitiveset creation - target->second.indirectCommands->push_back( osg::DrawArraysIndirectCommand( aoResult.count,1,aoResult.first ) ); + target->second.indirectCommands->push_back( osg::DrawArraysIndirectCommand( aoResult.count,1, aoResult.first ) ); osg::ComputeBoundsVisitor cbv; node->accept(cbv); @@ -725,9 +722,9 @@ struct ResetTexturesCallback : public osg::StateSet::Callback osg::TextureBuffer* tex = dynamic_cast( stateset->getTextureAttribute(*it,osg::StateAttribute::TEXTURE) ); if(tex==NULL) continue; - osg::BufferData* drawcmds = tex->getBufferData(); - if(drawcmds!=NULL) - drawcmds->dirty(); + osg::BufferData* img =const_cast(tex->getBufferData()); + if(img!=NULL) + img->dirty(); } for(it=texUnitsDirtyParams.begin(), eit=texUnitsDirtyParams.end(); it!=eit; ++it) { @@ -1563,3 +1560,4 @@ int main( int argc, char **argv ) return viewer.run(); } +