Changeset 9868
- Timestamp:
- 03/08/09 13:00:36 (4 years ago)
- Location:
- OpenSceneGraph/trunk
- Files:
-
- 8 modified
-
examples/osgterrain/osgterrain.cpp (modified) (12 diffs)
-
include/osgUtil/GLObjectsVisitor (modified) (2 diffs)
-
include/osgViewer/ViewerBase (modified) (3 diffs)
-
src/osg/GraphicsContext.cpp (modified) (2 diffs)
-
src/osgUtil/GLObjectsVisitor.cpp (modified) (3 diffs)
-
src/osgViewer/CompositeViewer.cpp (modified) (2 diffs)
-
src/osgViewer/Viewer.cpp (modified) (2 diffs)
-
src/osgViewer/ViewerBase.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgterrain/osgterrain.cpp
r9618 r9868 58 58 typedef std::vector< osg::ref_ptr<osg::GraphicsThread> > GraphicsThreads; 59 59 60 struct ReleaseBlockOnCompileCompleted : public osgUtil::IncrementalCompileOperation::CompileCompletedCallback 61 { 62 63 ReleaseBlockOnCompileCompleted(osg::RefBlockCount* block): 64 _block(block) {} 65 66 virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet) 67 { 68 if (_block.valid()) _block->completed(); 69 70 // tell IncrementalCompileOperation that it's now safe to remove the compileSet 71 72 osg::notify(osg::NOTICE)<<"compileCompleted("<<compileSet<<")"<<std::endl; 73 74 return true; 75 } 76 77 osg::ref_ptr<osg::RefBlockCount> _block; 78 }; 60 79 61 80 … … 64 83 public: 65 84 66 LoadAndCompileOperation(const std::string& filename, GraphicsThreads& graphicsThreads, osg::RefBlockCount* block):85 LoadAndCompileOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico , osg::RefBlockCount* block): 67 86 Operation("Load and compile Operation", false), 68 87 _filename(filename), 69 _ graphicsThreads(graphicsThreads),88 _incrementalCompileOperation(ico), 70 89 _block(block) {} 71 90 … … 75 94 76 95 _loadedModel = osgDB::readNodeFile(_filename); 77 if (_loadedModel.valid() && !_graphicsThreads.empty())78 {79 osg::ref_ptr<osgUtil::GLObjectsOperation> compileOperation = new osgUtil::GLObjectsOperation(_loadedModel.get());80 81 for(GraphicsThreads::iterator gitr = _graphicsThreads.begin();82 gitr != _graphicsThreads.end();83 ++gitr)84 {85 (*gitr)->add( compileOperation.get());86 // requiresBarrier = true;87 }88 }89 90 if (_block.valid()) _block->completed();96 97 if (_loadedModel.valid() && _incrementalCompileOperation.valid()) 98 { 99 osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = 100 new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel); 101 102 compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_block.get()); 103 104 _incrementalCompileOperation->add(compileSet.get()); 105 } 106 else 107 { 108 if (_block.valid()) _block->completed(); 109 } 91 110 92 111 // osg::notify(osg::NOTICE)<<"done LoadAndCompileOperation "<<_filename<<std::endl; 93 112 } 94 113 95 std::string _filename;96 GraphicsThreads _graphicsThreads;97 osg::ref_ptr<osg ::Node> _loadedModel;98 osg::ref_ptr<osg::RefBlockCount> _block;114 std::string _filename; 115 osg::ref_ptr<osg::Node> _loadedModel; 116 osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; 117 osg::ref_ptr<osg::RefBlockCount> _block; 99 118 100 119 }; … … 109 128 110 129 111 MasterOperation(const std::string& filename ):130 MasterOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico): 112 131 Operation("Master reading operation",true), 113 _filename(filename) 132 _filename(filename), 133 _incrementalCompileOperation(ico) 114 134 { 115 135 } … … 256 276 if (gt) threads.push_back(gt); 257 277 } 258 259 bool requiresBarrier = false;260 261 278 262 279 if (_operationQueue.valid()) … … 277 294 // osg::notify(osg::NOTICE)<<"Adding LoadAndCompileOperation "<<*nitr<<std::endl; 278 295 279 osg::ref_ptr<LoadAndCompileOperation> loadAndCompile = new LoadAndCompileOperation( *nitr, threads, _endOfLoadBlock.get() );296 osg::ref_ptr<LoadAndCompileOperation> loadAndCompile = new LoadAndCompileOperation( *nitr, _incrementalCompileOperation.get(), _endOfLoadBlock.get() ); 280 297 loadAndCompileList.push_back(loadAndCompile); 281 298 _operationQueue->add( loadAndCompile.get() ); … … 301 318 { 302 319 nodesToAdd[(*litr)->_filename] = (*litr)->_loadedModel; 303 requiresBarrier = true;304 320 } 305 321 } … … 309 325 else 310 326 { 327 328 _endOfLoadBlock = new osg::RefBlockCount(newFiles.size()); 329 330 _endOfLoadBlock->reset(); 331 311 332 for(Files::iterator nitr = newFiles.begin(); 312 333 nitr != newFiles.end(); … … 319 340 nodesToAdd[*nitr] = loadedModel; 320 341 321 osg::ref_ptr<osgUtil::GLObjectsOperation> compileOperation = new osgUtil::GLObjectsOperation(loadedModel.get()); 322 323 for(GraphicsThreads::iterator gitr = threads.begin(); 324 gitr != threads.end(); 325 ++gitr) 342 if (_incrementalCompileOperation.valid()) 326 343 { 327 (*gitr)->add( compileOperation.get() ); 328 requiresBarrier = true; 344 osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = 345 new osgUtil::IncrementalCompileOperation::CompileSet(loadedModel.get()); 346 347 compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_endOfLoadBlock.get()); 348 349 _incrementalCompileOperation->add(compileSet.get()); 350 } 351 else 352 { 353 _endOfLoadBlock->completed(); 329 354 } 330 355 } 331 } 356 else 357 { 358 _endOfLoadBlock->completed(); 359 } 360 } 361 362 _endOfLoadBlock->block(); 363 332 364 } 333 365 334 if (requiresBarrier)335 {336 _endOfCompilebarrier = new osg::BarrierOperation(threads.size()+1);337 _endOfCompilebarrier->setKeep(false);338 339 for(GraphicsThreads::iterator gitr = threads.begin();340 gitr != threads.end();341 ++gitr)342 {343 (*gitr)->add(_endOfCompilebarrier.get());344 }345 346 // osg::notify(osg::NOTICE)<<"Waiting for Compile to complete"<<std::endl;347 348 // wait for the graphics threads to complete.349 _endOfCompilebarrier->block();350 351 // osg::notify(osg::NOTICE)<<"done ... Waiting for Compile to complete"<<std::endl;352 }353 366 } 354 367 … … 455 468 OpenThreads::Block _updatesMergedBlock; 456 469 470 osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; 457 471 osg::ref_ptr<osg::BarrierOperation> _endOfCompilebarrier; 458 472 osg::ref_ptr<osg::RefBlockCount> _endOfLoadBlock; … … 619 633 viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); 620 634 635 // attach an IncrementaCompileOperation to allow the master loading 636 // to be handled with an incremental compile to avoid frame drops when large objects are added. 637 viewer.setIncrementalCompileOperation(new osgUtil::IncrementalCompileOperation()); 621 638 622 639 double x = 0.0; … … 632 649 while(arguments.read("-m",masterFilename)) 633 650 { 634 masterOperation = new MasterOperation(masterFilename );651 masterOperation = new MasterOperation(masterFilename, viewer.getIncrementalCompileOperation()); 635 652 } 636 653 -
OpenSceneGraph/trunk/include/osgUtil/GLObjectsVisitor
r9377 r9868 124 124 125 125 virtual void operator () (osg::GraphicsContext* context); 126 126 127 127 protected: 128 128 … … 131 131 }; 132 132 133 class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation 134 { 135 public: 136 137 IncrementalCompileOperation(); 138 139 typedef std::vector<osg::GraphicsContext*> Contexts; 140 void assignContexts(Contexts& contexts); 141 void removeContexts(Contexts& contexts); 142 143 void addGraphicsContext(osg::GraphicsContext* gc); 144 void removeGraphicsContext(osg::GraphicsContext* gc); 145 146 147 /** Merge subgraphs that have been compiled.*/ 148 void mergeCompiledSubgraphs(); 149 150 virtual void operator () (osg::GraphicsContext* context); 151 152 153 154 struct CompileData : public osg::Referenced 155 { 156 typedef std::list< osg::ref_ptr<osg::Texture> > Textures; 157 typedef std::list< osg::ref_ptr<osg::Drawable> > Drawables; 158 typedef std::list< osg::ref_ptr<osg::Program> > Programs; 159 160 bool empty() const { return _textures.empty() && _drawables.empty() && _programs.empty(); } 161 162 Textures _textures; 163 Drawables _drawables; 164 Programs _programs; 165 }; 166 167 class CompileSet; 168 typedef std::set<osg::GraphicsContext*> ContextSet; 169 typedef std::map<osg::GraphicsContext*, osg::ref_ptr<CompileData> > CompileMap; 170 171 struct CompileCompletedCallback : public osg::Referenced 172 { 173 virtual bool compileCompleted(CompileSet* compileSet) = 0; 174 }; 175 176 class CompileSet : public osg::Referenced 177 { 178 public: 179 180 181 CompileSet() {} 182 183 CompileSet(osg::Node*subgraphToCompile): 184 _subgraphToCompile(subgraphToCompile) {} 185 186 CompileSet(osg::Group* attachmentPoint, osg::Node*subgraphToCompile): 187 _attachmentPoint(attachmentPoint), 188 _subgraphToCompile(subgraphToCompile) {} 189 190 void buildCompileMap(ContextSet& context); 191 192 osg::ref_ptr<osg::Group> _attachmentPoint; 193 osg::ref_ptr<osg::Node> _subgraphToCompile; 194 osg::ref_ptr<CompileCompletedCallback> _compileCompletedCallback; 195 CompileMap _compileMap; 196 197 // protected: 198 199 virtual ~CompileSet() {} 200 }; 201 202 typedef std::list< osg::ref_ptr<CompileSet> > CompileSets; 203 204 205 /** Add a subgraph to be compiled.*/ 206 void add(osg::Node* subgraphToCompile); 207 208 /** Add a subgraph to be compiled and add automatically to attachPoint on call to mergeCompiledSubgraphs.*/ 209 void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile); 210 211 /** Add a CompileSet to be compiled.*/ 212 void add(CompileSet* compileSet, bool callBuildCompileMap=true); 213 214 215 OpenThreads::Mutex& getToCompiledMutex() { return _toCompileMutex; } 216 CompileSets& getToCompile() { return _compiled; } 217 218 OpenThreads::Mutex& getCompiledMutex() { return _compiledMutex; } 219 CompileSets& getCompiled() { return _compiled; } 220 221 protected: 222 223 virtual ~IncrementalCompileOperation(); 224 225 226 OpenThreads::Mutex _toCompileMutex; 227 CompileSets _toCompile; 228 229 OpenThreads::Mutex _compiledMutex; 230 CompileSets _compiled; 231 232 ContextSet _contexts; 233 234 }; 235 236 133 237 } 134 238 -
OpenSceneGraph/trunk/include/osgViewer/ViewerBase
r9554 r9868 18 18 19 19 #include <osgUtil/UpdateVisitor> 20 #include <osgUtil/GLObjectsVisitor> 20 21 21 22 #include <osgGA/MatrixManipulator> … … 185 186 /** Get the graphics operation to call on realization of the viewers graphics windows.*/ 186 187 osg::Operation* getRealizeOperation() { return _realizeOperation.get(); } 188 189 190 /** Set the incremental compile operation. 191 * Used to manage the OpenGL object compilation and merging of subgraphs in a way that avoids overloading 192 * the rendering of frame with too many new objects in one frame. */ 193 void setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico); 194 195 /** Get the incremental compile operation. */ 196 osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); } 187 197 188 198 … … 261 271 virtual void viewerInit() = 0; 262 272 263 bool _firstFrame; 264 bool _done; 265 int _keyEventSetsDone; 266 bool _quitEventSetsDone; 267 bool _releaseContextAtEndOfFrameHint; 268 269 ThreadingModel _threadingModel; 270 bool _threadsRunning; 271 272 BarrierPosition _endBarrierPosition; 273 274 osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier; 275 osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier; 276 osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock; 277 278 osg::ref_ptr<osgGA::EventVisitor> _eventVisitor; 279 280 osg::ref_ptr<osg::OperationQueue> _updateOperations; 281 osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor; 282 283 osg::ref_ptr<osg::Operation> _realizeOperation; 284 285 osg::observer_ptr<osg::GraphicsContext> _currentContext; 273 bool _firstFrame; 274 bool _done; 275 int _keyEventSetsDone; 276 bool _quitEventSetsDone; 277 bool _releaseContextAtEndOfFrameHint; 278 279 ThreadingModel _threadingModel; 280 bool _threadsRunning; 281 282 BarrierPosition _endBarrierPosition; 283 284 osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier; 285 osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier; 286 osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock; 287 288 osg::ref_ptr<osgGA::EventVisitor> _eventVisitor; 289 290 osg::ref_ptr<osg::OperationQueue> _updateOperations; 291 osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor; 292 293 osg::ref_ptr<osg::Operation> _realizeOperation; 294 osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; 295 296 osg::observer_ptr<osg::GraphicsContext> _currentContext; 286 297 }; 287 298 -
OpenSceneGraph/trunk/src/osg/GraphicsContext.cpp
r8872 r9868 318 318 GraphicsContext* GraphicsContext::getOrCreateCompileContext(unsigned int contextID) 319 319 { 320 osg::notify(osg:: INFO)<<"GraphicsContext::createCompileContext."<<std::endl;320 osg::notify(osg::NOTICE)<<"GraphicsContext::createCompileContext."<<std::endl; 321 321 322 322 { … … 350 350 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex); 351 351 s_contextIDMap[contextID]._compileContext = gc; 352 osg::notify(osg:: INFO)<<" succeeded GraphicsContext::createCompileContext."<<std::endl;352 osg::notify(osg::NOTICE)<<" succeeded GraphicsContext::createCompileContext."<<std::endl; 353 353 return gc.release(); 354 354 } -
OpenSceneGraph/trunk/src/osgUtil/GLObjectsVisitor.cpp
r8360 r9868 14 14 #include <osg/Drawable> 15 15 #include <osg/Notify> 16 #include <OpenThreads/ScopedLock> 16 17 17 18 using namespace osg; 18 19 using namespace osgUtil; 19 20 21 22 ///////////////////////////////////////////////////////////////// 23 // 24 // GLObjectsVisitor 25 // 20 26 GLObjectsVisitor::GLObjectsVisitor(Mode mode) 21 27 { … … 146 152 } 147 153 154 ///////////////////////////////////////////////////////////////// 155 // 156 // GLObjectsVisitor 157 // 158 148 159 GLObjectsOperation::GLObjectsOperation(GLObjectsVisitor::Mode mode): 149 160 osg::GraphicsOperation("GLObjectOperation",false), … … 183 194 // osg::notify(osg::NOTICE)<<"GLObjectsOperation::after >>>>>>>>>>> "<<std::endl; 184 195 } 196 197 ///////////////////////////////////////////////////////////////// 198 // 199 // IncrementalCompileOperation 200 // 201 IncrementalCompileOperation::IncrementalCompileOperation(): 202 osg::GraphicsOperation("IncrementalCompileOperation",true) 203 { 204 } 205 206 IncrementalCompileOperation::~IncrementalCompileOperation() 207 { 208 } 209 210 void IncrementalCompileOperation::assignContexts(Contexts& contexts) 211 { 212 for(Contexts::iterator itr = contexts.begin(); 213 itr != contexts.end(); 214 ++itr) 215 { 216 osg::GraphicsContext* gc = *itr; 217 addGraphicsContext(gc); 218 } 219 } 220 221 void IncrementalCompileOperation::removeContexts(Contexts& contexts) 222 { 223 for(Contexts::iterator itr = contexts.begin(); 224 itr != contexts.end(); 225 ++itr) 226 { 227 osg::GraphicsContext* gc = *itr; 228 removeGraphicsContext(gc); 229 } 230 } 231 232 233 void IncrementalCompileOperation::addGraphicsContext(osg::GraphicsContext* gc) 234 { 235 if (_contexts.count(gc)==0) 236 { 237 gc->add(this); 238 _contexts.insert(gc); 239 } 240 } 241 242 void IncrementalCompileOperation::removeGraphicsContext(osg::GraphicsContext* gc) 243 { 244 if (_contexts.count(gc)!=0) 245 { 246 gc->remove(this); 247 _contexts.erase(gc); 248 } 249 } 250 251 void IncrementalCompileOperation::add(osg::Node* subgraphToCompile) 252 { 253 osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add("<<subgraphToCompile<<")"<<std::endl; 254 add(new CompileSet(subgraphToCompile)); 255 } 256 257 void IncrementalCompileOperation::add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile) 258 { 259 osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add("<<attachmentPoint<<", "<<subgraphToCompile<<")"<<std::endl; 260 add(new CompileSet(attachmentPoint, subgraphToCompile)); 261 } 262 263 264 void IncrementalCompileOperation::add(CompileSet* compileSet, bool callBuildCompileMap) 265 { 266 if (!compileSet) return; 267 268 if (callBuildCompileMap) compileSet->buildCompileMap(_contexts); 269 270 osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add(CompileSet = "<<compileSet<<", "<<", "<<callBuildCompileMap<<")"<<std::endl; 271 272 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_toCompileMutex); 273 _toCompile.push_back(compileSet); 274 } 275 276 void IncrementalCompileOperation::mergeCompiledSubgraphs() 277 { 278 osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::mergeCompiledSubgraphs()"<<std::endl; 279 280 OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex); 281 282 for(CompileSets::iterator itr = _compiled.begin(); 283 itr != _compiled.end(); 284 ++itr) 285 { 286 CompileSet* cs = itr->get(); 287 if (cs->_attachmentPoint.valid()) 288 { 289 cs->_attachmentPoint->addChild(cs->_subgraphToCompile.get()); 290 } 291 } 292 293 _compiled.clear(); 294 } 295 296 void IncrementalCompileOperation::CompileSet::buildCompileMap(ContextSet& context) 297 { 298 } 299 300 void IncrementalCompileOperation::operator () (osg::GraphicsContext* context) 301 { 302 osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::operator () ("<<context<<")"<<std::endl; 303 304 OpenThreads::ScopedLock<OpenThreads::Mutex> toCompile_lock(_toCompileMutex); 305 for(CompileSets::iterator itr = _toCompile.begin(); 306 itr != _toCompile.end(); 307 ) 308 { 309 CompileSet* cs = itr->get(); 310 CompileMap& cm = cs->_compileMap; 311 CompileData* cd = cm[context].get(); 312 313 if (cd) 314 { 315 // compile textures 316 cd->_textures.clear(); 317 318 // compile drawables 319 cd->_drawables.clear(); 320 321 // compile programs 322 cd->_programs.clear(); 323 } 324 325 if (!cd || cd->empty()) 326 { 327 if (cs->_compileCompletedCallback.valid()) 328 { 329 if (cs->_compileCompletedCallback->compileCompleted(cs)) 330 { 331 // callback will handle merging of subgraph so no need to place CompileSet in merge. 332 } 333 else 334 { 335 OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex); 336 _compiled.push_back(cs); 337 } 338 } 339 340 // remove from toCompileSet; 341 itr = _toCompile.erase(itr); 342 } 343 else 344 { 345 ++itr; 346 } 347 } 348 } -
OpenSceneGraph/trunk/src/osgViewer/CompositeViewer.cpp
r9554 r9868 498 498 } 499 499 500 // attach contexts to _incrementalCompileOperation if attached. 501 if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts); 502 503 500 504 bool grabFocus = true; 501 505 if (grabFocus) … … 995 999 } 996 1000 1001 if (_incrementalCompileOperation.valid()) 1002 { 1003 // merge subgraphs that have been compiled by the incremental compiler operation. 1004 _incrementalCompileOperation->mergeCompiledSubgraphs(); 1005 } 1006 997 1007 if (_updateOperations.valid()) 998 1008 { -
OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp
r9554 r9868 449 449 } 450 450 } 451 451 452 // attach contexts to _incrementalCompileOperation if attached. 453 if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts); 454 452 455 bool grabFocus = true; 453 456 if (grabFocus) … … 899 902 _updateOperations->runOperations(this); 900 903 } 904 905 if (_incrementalCompileOperation.valid()) 906 { 907 // merge subgraphs that have been compiled by the incremental compiler operation. 908 _incrementalCompileOperation->mergeCompiledSubgraphs(); 909 } 910 901 911 902 912 { -
OpenSceneGraph/trunk/src/osgViewer/ViewerBase.cpp
r9591 r9868 376 376 if (!gc->isRealized()) 377 377 { 378 osg::notify(osg::INFO)<<"ViewerBase::startThreading() : Realiz ng window "<<gc<<std::endl;378 osg::notify(osg::INFO)<<"ViewerBase::startThreading() : Realizing window "<<gc<<std::endl; 379 379 gc->realize(); 380 380 } … … 550 550 _updateOperations->remove(operation); 551 551 } 552 } 553 554 void ViewerBase::setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico) 555 { 556 if (_incrementalCompileOperation == ico) return; 557 558 Contexts contexts; 559 getContexts(contexts, false); 560 561 if (_incrementalCompileOperation.valid()) _incrementalCompileOperation->removeContexts(contexts); 562 563 // assign new operation 564 _incrementalCompileOperation = ico; 565 566 if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts); 552 567 } 553 568
