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.
This commit is contained in:
Robert Osfield 2011-04-18 12:35:19 +00:00
parent a74289614d
commit 8ffa50a88e
5 changed files with 61 additions and 21 deletions

View File

@ -21,7 +21,7 @@ extern "C" {
#define OPENSCENEGRAPH_MAJOR_VERSION 2 #define OPENSCENEGRAPH_MAJOR_VERSION 2
#define OPENSCENEGRAPH_MINOR_VERSION 9 #define OPENSCENEGRAPH_MINOR_VERSION 9
#define OPENSCENEGRAPH_PATCH_VERSION 12 #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. /* 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) * #if OSG_MIN_VERSION_REQUIRED(2,9,5)

View File

@ -134,7 +134,26 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
/** Merge subgraphs that have been compiled.*/ /** 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); virtual void operator () (osg::GraphicsContext* context);
@ -142,12 +161,19 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
{ {
CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico); 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; IncrementalCompileOperation* incrementalCompileOperation;
bool compileAll;
unsigned int maxNumObjectsToCompile; unsigned int maxNumObjectsToCompile;
osg::ElapsedTime timer;
double allocatedTime; double allocatedTime;
osg::ElapsedTime timer;
}; };
struct CompileOp : public osg::Referenced struct CompileOp : public osg::Referenced
@ -257,7 +283,6 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
/** Remove CompileSet from list.*/ /** Remove CompileSet from list.*/
void remove(CompileSet* compileSet); void remove(CompileSet* compileSet);
OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; } OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; }
CompileSets& getToCompile() { return _toCompile; } CompileSets& getToCompile() { return _toCompile; }
@ -268,13 +293,15 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
virtual ~IncrementalCompileOperation(); virtual ~IncrementalCompileOperation();
double _targetFrameRate; double _targetFrameRate;
double _minimumTimeAvailableForGLCompileAndDeletePerFrame; double _minimumTimeAvailableForGLCompileAndDeletePerFrame;
unsigned int _maximumNumOfObjectsToCompilePerFrame; unsigned int _maximumNumOfObjectsToCompilePerFrame;
double _flushTimeRatio; double _flushTimeRatio;
double _conservativeTimeRatio; double _conservativeTimeRatio;
unsigned int _currentFrameNumber;
unsigned int _compileAllTillFrameNumber;
osg::ref_ptr<osg::Geometry> _forceTextureDownloadGeometry; osg::ref_ptr<osg::Geometry> _forceTextureDownloadGeometry;
OpenThreads::Mutex _toCompileMutex; OpenThreads::Mutex _toCompileMutex;

View File

@ -301,7 +301,10 @@ bool IncrementalCompileOperation::CompileProgramOp::compile(CompileInfo& compile
return true; 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()); setState(context->getState());
incrementalCompileOperation = ico; incrementalCompileOperation = ico;
@ -342,7 +345,7 @@ bool IncrementalCompileOperation::CompileList::compile(CompileInfo& compileInfo)
//#define USE_TIME_ESTIMATES //#define USE_TIME_ESTIMATES
for(CompileOps::iterator itr = _compileOps.begin(); 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 #ifdef USE_TIME_ESTIMATES
@ -444,7 +447,9 @@ bool IncrementalCompileOperation::CompileSet::compile(CompileInfo& compileInfo)
IncrementalCompileOperation::IncrementalCompileOperation(): IncrementalCompileOperation::IncrementalCompileOperation():
osg::GraphicsOperation("IncrementalCompileOperation",true), osg::GraphicsOperation("IncrementalCompileOperation",true),
_flushTimeRatio(0.5), _flushTimeRatio(0.5),
_conservativeTimeRatio(0.5) _conservativeTimeRatio(0.5),
_currentFrameNumber(0),
_compileAllTillFrameNumber(0)
{ {
_targetFrameRate = 100.0; _targetFrameRate = 100.0;
_minimumTimeAvailableForGLCompileAndDeletePerFrame = 0.001; // 1ms. _minimumTimeAvailableForGLCompileAndDeletePerFrame = 0.001; // 1ms.
@ -623,12 +628,14 @@ void IncrementalCompileOperation::remove(CompileSet* compileSet)
} }
void IncrementalCompileOperation::mergeCompiledSubgraphs() void IncrementalCompileOperation::mergeCompiledSubgraphs(const osg::FrameStamp* frameStamp)
{ {
// OSG_INFO<<"IncrementalCompileOperation::mergeCompiledSubgraphs()"<<std::endl; // OSG_INFO<<"IncrementalCompileOperation::mergeCompiledSubgraphs()"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex); OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex);
if (frameStamp) _currentFrameNumber = frameStamp->getFrameNumber();
for(CompileSets::iterator itr = _compiled.begin(); for(CompileSets::iterator itr = _compiled.begin();
itr != _compiled.end(); itr != _compiled.end();
++itr) ++itr)
@ -673,7 +680,6 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context)
double flushTime = availableTime * _flushTimeRatio; double flushTime = availableTime * _flushTimeRatio;
double compileTime = availableTime - flushTime; double compileTime = availableTime - flushTime;
unsigned int maxNumOfObjectsToCompilePerFrame = _maximumNumOfObjectsToCompilePerFrame;
#if 1 #if 1
OSG_NOTIFY(level)<<"total availableTime = "<<availableTime*1000.0<<std::endl; OSG_NOTIFY(level)<<"total availableTime = "<<availableTime*1000.0<<std::endl;
@ -696,6 +702,7 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context)
CompileInfo compileInfo(context, this); CompileInfo compileInfo(context, this);
compileInfo.maxNumObjectsToCompile = _maximumNumOfObjectsToCompilePerFrame; compileInfo.maxNumObjectsToCompile = _maximumNumOfObjectsToCompilePerFrame;
compileInfo.allocatedTime = compileTime; compileInfo.allocatedTime = compileTime;
compileInfo.compileAll = (_compileAllTillFrameNumber > _currentFrameNumber);
CompileSets toCompileCopy; CompileSets toCompileCopy;
{ {
@ -704,7 +711,7 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context)
} }
for(CompileSets::iterator itr = toCompileCopy.begin(); for(CompileSets::iterator itr = toCompileCopy.begin();
itr != toCompileCopy.end() && compileTime>0.0 && maxNumOfObjectsToCompilePerFrame>0; itr != toCompileCopy.end() && compileInfo.okToCompile();
++itr) ++itr)
{ {
CompileSet* cs = itr->get(); CompileSet* cs = itr->get();
@ -738,4 +745,10 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context)
//glFinish(); //glFinish();
} }
void IncrementalCompileOperation::compileAllForNextFrame(unsigned int numFramesToDoCompileAll)
{
_compileAllTillFrameNumber = _currentFrameNumber+numFramesToDoCompileAll;
}
} // end of namespace osgUtil } // end of namespace osgUtil

View File

@ -1075,7 +1075,7 @@ void CompositeViewer::updateTraversal()
if (_incrementalCompileOperation.valid()) if (_incrementalCompileOperation.valid())
{ {
// merge subgraphs that have been compiled by the incremental compiler operation. // merge subgraphs that have been compiled by the incremental compiler operation.
_incrementalCompileOperation->mergeCompiledSubgraphs(); _incrementalCompileOperation->mergeCompiledSubgraphs(getFrameStamp());
} }
if (_updateOperations.valid()) if (_updateOperations.valid())

View File

@ -983,7 +983,7 @@ void Viewer::updateTraversal()
if (_incrementalCompileOperation.valid()) if (_incrementalCompileOperation.valid())
{ {
// merge subgraphs that have been compiled by the incremental compiler operation. // merge subgraphs that have been compiled by the incremental compiler operation.
_incrementalCompileOperation->mergeCompiledSubgraphs(); _incrementalCompileOperation->mergeCompiledSubgraphs(getFrameStamp());
} }
{ {