From 8ffa50a88e2e9624cf30e157233ed19bb23d86f0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 18 Apr 2011 12:35:19 +0000 Subject: [PATCH] Added IncrementalCompileOperation::compileAllForNextFrame(uint) method and supporting functionality that tell the IncrementalCompileOperation to compile all pending objects during next draw traversal, for specified number of frames. --- include/osg/Version | 2 +- include/osgUtil/IncrementalCompileOperation | 47 ++++++++++++++++----- src/osgUtil/IncrementalCompileOperation.cpp | 29 +++++++++---- src/osgViewer/CompositeViewer.cpp | 2 +- src/osgViewer/Viewer.cpp | 2 +- 5 files changed, 61 insertions(+), 21 deletions(-) diff --git a/include/osg/Version b/include/osg/Version index 0d1827f10..5cd6c9d1b 100644 --- a/include/osg/Version +++ b/include/osg/Version @@ -21,7 +21,7 @@ extern "C" { #define OPENSCENEGRAPH_MAJOR_VERSION 2 #define OPENSCENEGRAPH_MINOR_VERSION 9 #define OPENSCENEGRAPH_PATCH_VERSION 12 -#define OPENSCENEGRAPH_SOVERSION 72 +#define OPENSCENEGRAPH_SOVERSION 73 /* Convenience macro that can be used to decide whether a feature is present or not i.e. * #if OSG_MIN_VERSION_REQUIRED(2,9,5) diff --git a/include/osgUtil/IncrementalCompileOperation b/include/osgUtil/IncrementalCompileOperation index efeeec892..46688baa2 100644 --- a/include/osgUtil/IncrementalCompileOperation +++ b/include/osgUtil/IncrementalCompileOperation @@ -134,7 +134,26 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation /** Merge subgraphs that have been compiled.*/ - void mergeCompiledSubgraphs(); + void mergeCompiledSubgraphs(const osg::FrameStamp* frameStamp); + + /** Set the current frame number that the IncrementalCompileOperation should use as a reference + * value for calculations based on current frame number. + * Note, this value is set by the mergeCompiledSubgraphs(..) method so one won't normally need to call + * set the CurrentFrameNumber manually.*/ + void setCurrentFrameNumber(unsigned int fn) { _currentFrameNumber = fn; } + unsigned int getCurrentFrameNumber() const { return _currentFrameNumber; } + + /** tell the IncrementalCompileOperation to compile all pending objects during next draw traversal, + * for specified number of frames.*/ + void compileAllForNextFrame(unsigned int numFramesToDoCompileAll=1); + + /** tell the IncrementalCompileOperation to compile all pending objects during next draw traversal, + * till specified frame number.*/ + void setCompileAllTillFrameNumber(unsigned int fn) { _compileAllTillFrameNumber = fn; } + unsigned int getCompileAllTillFrameNumber() const { return _compileAllTillFrameNumber; } + + + virtual void operator () (osg::GraphicsContext* context); @@ -142,12 +161,19 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation { CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico); - double availableTime() { return allocatedTime - timer.elapsedTime(); } - + bool okToCompile(double estimatedTimeForCompile=0.0) const + { + if (compileAll) return true; + if (maxNumObjectsToCompile==0) return false; + return (allocatedTime - timer.elapsedTime()) >= estimatedTimeForCompile; + } + IncrementalCompileOperation* incrementalCompileOperation; + + bool compileAll; unsigned int maxNumObjectsToCompile; - osg::ElapsedTime timer; double allocatedTime; + osg::ElapsedTime timer; }; struct CompileOp : public osg::Referenced @@ -257,32 +283,33 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation /** Remove CompileSet from list.*/ void remove(CompileSet* compileSet); - OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; } CompileSets& getToCompile() { return _toCompile; } - + OpenThreads::Mutex* getCompiledMutex() { return &_compiledMutex; } CompileSets& getCompiled() { return _compiled; } protected: virtual ~IncrementalCompileOperation(); - - + double _targetFrameRate; double _minimumTimeAvailableForGLCompileAndDeletePerFrame; unsigned int _maximumNumOfObjectsToCompilePerFrame; double _flushTimeRatio; double _conservativeTimeRatio; + unsigned int _currentFrameNumber; + unsigned int _compileAllTillFrameNumber; + osg::ref_ptr _forceTextureDownloadGeometry; OpenThreads::Mutex _toCompileMutex; CompileSets _toCompile; - + OpenThreads::Mutex _compiledMutex; CompileSets _compiled; - + ContextSet _contexts; }; diff --git a/src/osgUtil/IncrementalCompileOperation.cpp b/src/osgUtil/IncrementalCompileOperation.cpp index 84d32c274..b240d86ed 100644 --- a/src/osgUtil/IncrementalCompileOperation.cpp +++ b/src/osgUtil/IncrementalCompileOperation.cpp @@ -301,7 +301,10 @@ bool IncrementalCompileOperation::CompileProgramOp::compile(CompileInfo& compile return true; } -IncrementalCompileOperation::CompileInfo::CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico) +IncrementalCompileOperation::CompileInfo::CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico): + compileAll(false), + maxNumObjectsToCompile(0), + allocatedTime(0) { setState(context->getState()); incrementalCompileOperation = ico; @@ -342,7 +345,7 @@ bool IncrementalCompileOperation::CompileList::compile(CompileInfo& compileInfo) //#define USE_TIME_ESTIMATES for(CompileOps::iterator itr = _compileOps.begin(); - itr != _compileOps.end() && compileInfo.availableTime()>0.0 && compileInfo.maxNumObjectsToCompile>0; + itr != _compileOps.end() && compileInfo.okToCompile(); ) { #ifdef USE_TIME_ESTIMATES @@ -444,7 +447,9 @@ bool IncrementalCompileOperation::CompileSet::compile(CompileInfo& compileInfo) IncrementalCompileOperation::IncrementalCompileOperation(): osg::GraphicsOperation("IncrementalCompileOperation",true), _flushTimeRatio(0.5), - _conservativeTimeRatio(0.5) + _conservativeTimeRatio(0.5), + _currentFrameNumber(0), + _compileAllTillFrameNumber(0) { _targetFrameRate = 100.0; _minimumTimeAvailableForGLCompileAndDeletePerFrame = 0.001; // 1ms. @@ -623,13 +628,15 @@ void IncrementalCompileOperation::remove(CompileSet* compileSet) } -void IncrementalCompileOperation::mergeCompiledSubgraphs() +void IncrementalCompileOperation::mergeCompiledSubgraphs(const osg::FrameStamp* frameStamp) { // OSG_INFO<<"IncrementalCompileOperation::mergeCompiledSubgraphs()"< compilded_lock(_compiledMutex); - - for(CompileSets::iterator itr = _compiled.begin(); + + if (frameStamp) _currentFrameNumber = frameStamp->getFrameNumber(); + + for(CompileSets::iterator itr = _compiled.begin(); itr != _compiled.end(); ++itr) { @@ -673,7 +680,6 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context) double flushTime = availableTime * _flushTimeRatio; double compileTime = availableTime - flushTime; - unsigned int maxNumOfObjectsToCompilePerFrame = _maximumNumOfObjectsToCompilePerFrame; #if 1 OSG_NOTIFY(level)<<"total availableTime = "< _currentFrameNumber); CompileSets toCompileCopy; { @@ -704,7 +711,7 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context) } for(CompileSets::iterator itr = toCompileCopy.begin(); - itr != toCompileCopy.end() && compileTime>0.0 && maxNumOfObjectsToCompilePerFrame>0; + itr != toCompileCopy.end() && compileInfo.okToCompile(); ++itr) { CompileSet* cs = itr->get(); @@ -738,4 +745,10 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context) //glFinish(); } +void IncrementalCompileOperation::compileAllForNextFrame(unsigned int numFramesToDoCompileAll) +{ + _compileAllTillFrameNumber = _currentFrameNumber+numFramesToDoCompileAll; +} + + } // end of namespace osgUtil diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 15ada12c8..d9859c7d1 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -1075,7 +1075,7 @@ void CompositeViewer::updateTraversal() if (_incrementalCompileOperation.valid()) { // merge subgraphs that have been compiled by the incremental compiler operation. - _incrementalCompileOperation->mergeCompiledSubgraphs(); + _incrementalCompileOperation->mergeCompiledSubgraphs(getFrameStamp()); } if (_updateOperations.valid()) diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index 1750ed8b8..10b71e248 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -983,7 +983,7 @@ void Viewer::updateTraversal() if (_incrementalCompileOperation.valid()) { // merge subgraphs that have been compiled by the incremental compiler operation. - _incrementalCompileOperation->mergeCompiledSubgraphs(); + _incrementalCompileOperation->mergeCompiledSubgraphs(getFrameStamp()); } {