2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2009-03-12 23:21:04 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2009-03-12 23:21:04 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2009-03-12 23:21:04 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-03-12 23:21:04 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGUTIL_INCREMENTALCOMPILEOPERATOR
|
|
|
|
#define OSGUTIL_INCREMENTALCOMPILEOPERATOR
|
|
|
|
|
|
|
|
#include <osgUtil/GLObjectsVisitor>
|
2011-02-03 20:22:09 +08:00
|
|
|
#include <osg/Geometry>
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
namespace osgUtil {
|
|
|
|
|
2010-10-22 00:29:23 +08:00
|
|
|
|
2011-01-18 01:17:19 +08:00
|
|
|
class OSGUTIL_EXPORT StateToCompile : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
StateToCompile(GLObjectsVisitor::Mode mode);
|
|
|
|
|
|
|
|
typedef std::set<osg::Drawable*> DrawableSet;
|
|
|
|
typedef std::set<osg::StateSet*> StateSetSet;
|
|
|
|
typedef std::set<osg::Texture*> TextureSet;
|
|
|
|
typedef std::set<osg::Program*> ProgramSet;
|
|
|
|
|
|
|
|
DrawableSet _drawablesHandled;
|
|
|
|
StateSetSet _statesetsHandled;
|
|
|
|
|
|
|
|
GLObjectsVisitor::Mode _mode;
|
|
|
|
DrawableSet _drawables;
|
2011-02-01 20:18:22 +08:00
|
|
|
TextureSet _textures;
|
|
|
|
ProgramSet _programs;
|
|
|
|
bool _assignPBOToImages;
|
2011-02-07 22:19:58 +08:00
|
|
|
osg::ref_ptr<osg::PixelBufferObject> _pbo;
|
2011-01-18 01:17:19 +08:00
|
|
|
|
|
|
|
bool empty() const { return _textures.empty() && _programs.empty() && _drawables.empty(); }
|
|
|
|
|
|
|
|
virtual void apply(osg::Node& node);
|
|
|
|
virtual void apply(osg::Geode& node);
|
|
|
|
|
|
|
|
virtual void apply(osg::Drawable& drawable);
|
|
|
|
virtual void apply(osg::StateSet& stateset);
|
|
|
|
virtual void apply(osg::Texture& texture);
|
2011-02-07 22:19:58 +08:00
|
|
|
|
2011-01-18 01:17:19 +08:00
|
|
|
};
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
IncrementalCompileOperation();
|
2011-01-18 01:17:19 +08:00
|
|
|
|
|
|
|
/** Return true if the IncrementCompileOperation is active.*/
|
|
|
|
bool isActive() const { return !_contexts.empty(); }
|
|
|
|
|
|
|
|
bool requiresCompile(StateToCompile& stateToCompile);
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
/** Set the target frame rate that the IncrementalCompileOperation should assume.
|
|
|
|
* Typically one would set this to the value refresh rate of your display system i.e. 60Hz.
|
|
|
|
* Default value is 100.
|
|
|
|
* Usage notes. The TargetFrameRate and the MinimumTimeAvailableForGLCompileAndDeletePerFrame
|
|
|
|
* parameters are not directly used by IncrementalCompileOperation, but are should be used as a guide for how
|
|
|
|
* long to set aside per frame for compiling and deleting OpenGL objects. The longer amount of
|
|
|
|
* time to set aside the faster databases will be paged in but with increased chance of frame drops,
|
|
|
|
* the lower the amount of time the set aside the slower databases will paged it but with better
|
|
|
|
* chance of avoid any frame drops. The default values are chosen to achieve the later when running
|
2012-03-22 01:36:20 +08:00
|
|
|
* on a modern mid to high end PC.
|
2009-03-12 23:21:04 +08:00
|
|
|
* The way to compute the amount of available time use a scheme such as :
|
2012-03-22 01:36:20 +08:00
|
|
|
* availableTime = maximum(1.0/targetFrameRate - timeTakenDuringUpdateCullAndDraw, minimumTimeAvailableForGLCompileAndDeletePerFrame).
|
2009-03-12 23:21:04 +08:00
|
|
|
*/
|
|
|
|
void setTargetFrameRate(double tfr) { _targetFrameRate = tfr; }
|
|
|
|
|
|
|
|
/** Get the target frame rate that the IncrementalCompileOperation should assume.*/
|
|
|
|
double getTargetFrameRate() const { return _targetFrameRate; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
/** Set the minimum amount of time (in seconds) that should be made available for compiling and delete OpenGL objects per frame.
|
2012-03-22 01:36:20 +08:00
|
|
|
* Default value is 0.001 (1 millisecond).
|
2009-03-12 23:21:04 +08:00
|
|
|
* For usage see notes in setTargetFrameRate.*/
|
|
|
|
void setMinimumTimeAvailableForGLCompileAndDeletePerFrame(double ta) { _minimumTimeAvailableForGLCompileAndDeletePerFrame = ta; }
|
|
|
|
|
|
|
|
/** Get the minimum amount of time that should be made available for compiling and delete OpenGL objects per frame.
|
|
|
|
* For usage see notes in setTargetFrameRate.*/
|
|
|
|
double getMinimumTimeAvailableForGLCompileAndDeletePerFrame() const { return _minimumTimeAvailableForGLCompileAndDeletePerFrame; }
|
|
|
|
|
|
|
|
/** Set the maximum number of OpenGL objects that the page should attempt to compile per frame.
|
|
|
|
* Note, Lower values reduces chances of a frame drop but lower the rate that database will be paged in at.
|
|
|
|
* Default value is 8. */
|
|
|
|
void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num) { _maximumNumOfObjectsToCompilePerFrame = num; }
|
|
|
|
|
|
|
|
/** Get the maximum number of OpenGL objects that the page should attempt to compile per frame.*/
|
|
|
|
unsigned int getMaximumNumOfObjectsToCompilePerFrame() const { return _maximumNumOfObjectsToCompilePerFrame; }
|
|
|
|
|
|
|
|
|
|
|
|
/** FlushTimeRatio governs how much of the spare time in each frame is used for flushing deleted OpenGL objects.
|
|
|
|
* Default value is 0.5, valid range is 0.1 to 0.9.*/
|
|
|
|
void setFlushTimeRatio(double ratio) { _flushTimeRatio = ratio; }
|
|
|
|
double getFlushTimeRatio() const { return _flushTimeRatio; }
|
|
|
|
|
|
|
|
/** ConservativeTimeRatio governs how much of the measured spare time in each frame is used for flushing deleted and compile new OpenGL objects.
|
|
|
|
* Default value is 0.5, valid range is 0.1 to 1.0.
|
|
|
|
* A ratio near 1.0 will lead to paged databases being compiled and merged quicker but increase the chances of frame drop.
|
|
|
|
* A ratio near 0.1 will lead to paged databases being compiled and merged closer but reduse the chances of frame drop.*/
|
|
|
|
void setConservativeTimeRatio(double ratio) { _conservativeTimeRatio = ratio; }
|
|
|
|
double getConservativeTimeRatio() const { return _conservativeTimeRatio; }
|
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
/** Assign a geometry and associated StateSet than is applied after each texture compile to atttempt to force the OpenGL
|
|
|
|
* drive to download the texture object to OpenGL graphics card.*/
|
|
|
|
void assignForceTextureDownloadGeometry();
|
|
|
|
|
|
|
|
/** Set the osg::Geometry to apply after each texture compile to atttempt to force the OpenGL
|
|
|
|
* drive to download the texture object to OpenGL graphics card.*/
|
|
|
|
void setForceTextureDownloadGeometry(osg::Geometry* geom) { _forceTextureDownloadGeometry = geom; }
|
|
|
|
osg::Geometry* getForceTextureDownloadGeometry() { return _forceTextureDownloadGeometry.get(); }
|
|
|
|
const osg::Geometry* getForceTextureDownloadGeometry() const { return _forceTextureDownloadGeometry.get(); }
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
typedef std::vector<osg::GraphicsContext*> Contexts;
|
|
|
|
void assignContexts(Contexts& contexts);
|
|
|
|
void removeContexts(Contexts& contexts);
|
|
|
|
|
|
|
|
void addGraphicsContext(osg::GraphicsContext* gc);
|
|
|
|
void removeGraphicsContext(osg::GraphicsContext* gc);
|
|
|
|
|
2011-01-18 01:17:19 +08:00
|
|
|
typedef std::set<osg::GraphicsContext*> ContextSet;
|
|
|
|
ContextSet& getContextSet() { return _contexts; }
|
|
|
|
const ContextSet& getContextSet() const { return _contexts; }
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
/** Merge subgraphs that have been compiled.*/
|
2011-04-18 20:35:19 +08:00
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
virtual void operator () (osg::GraphicsContext* context);
|
2010-12-25 03:19:48 +08:00
|
|
|
|
|
|
|
struct OSGUTIL_EXPORT CompileInfo : public osg::RenderInfo
|
|
|
|
{
|
|
|
|
CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico);
|
|
|
|
|
2011-04-18 20:35:19 +08:00
|
|
|
bool okToCompile(double estimatedTimeForCompile=0.0) const
|
|
|
|
{
|
|
|
|
if (compileAll) return true;
|
|
|
|
if (maxNumObjectsToCompile==0) return false;
|
|
|
|
return (allocatedTime - timer.elapsedTime()) >= estimatedTimeForCompile;
|
|
|
|
}
|
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
IncrementalCompileOperation* incrementalCompileOperation;
|
2011-04-18 20:35:19 +08:00
|
|
|
|
|
|
|
bool compileAll;
|
2010-12-25 03:19:48 +08:00
|
|
|
unsigned int maxNumObjectsToCompile;
|
|
|
|
double allocatedTime;
|
2011-04-18 20:35:19 +08:00
|
|
|
osg::ElapsedTime timer;
|
2010-12-25 03:19:48 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
struct CompileOp : public osg::Referenced
|
|
|
|
{
|
|
|
|
/** return an estimate for how many seconds the compile will take.*/
|
|
|
|
virtual double estimatedTimeForCompile(CompileInfo& compileInfo) const = 0;
|
|
|
|
/** compile associated objects, return true if object as been fully compiled and this CompileOp can be removed from the to compile list.*/
|
|
|
|
virtual bool compile(CompileInfo& compileInfo) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OSGUTIL_EXPORT CompileDrawableOp : public CompileOp
|
|
|
|
{
|
|
|
|
CompileDrawableOp(osg::Drawable* drawable);
|
|
|
|
double estimatedTimeForCompile(CompileInfo& compileInfo) const;
|
|
|
|
bool compile(CompileInfo& compileInfo);
|
|
|
|
osg::ref_ptr<osg::Drawable> _drawable;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OSGUTIL_EXPORT CompileTextureOp : public CompileOp
|
|
|
|
{
|
|
|
|
CompileTextureOp(osg::Texture* texture);
|
|
|
|
double estimatedTimeForCompile(CompileInfo& compileInfo) const;
|
|
|
|
bool compile(CompileInfo& compileInfo);
|
|
|
|
osg::ref_ptr<osg::Texture> _texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OSGUTIL_EXPORT CompileProgramOp : public CompileOp
|
|
|
|
{
|
|
|
|
CompileProgramOp(osg::Program* program);
|
|
|
|
double estimatedTimeForCompile(CompileInfo& compileInfo) const;
|
|
|
|
bool compile(CompileInfo& compileInfo);
|
|
|
|
osg::ref_ptr<osg::Program> _program;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OSGUTIL_EXPORT CompileList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CompileList();
|
|
|
|
~CompileList();
|
|
|
|
|
|
|
|
bool empty() const { return _compileOps.empty(); }
|
|
|
|
void add(CompileOp* compileOp);
|
|
|
|
void add(osg::Drawable* drawable) { add(new CompileDrawableOp(drawable)); }
|
|
|
|
void add(osg::Texture* texture) { add(new CompileTextureOp(texture)); }
|
|
|
|
void add(osg::Program* program) { add(new CompileProgramOp(program)); }
|
|
|
|
|
|
|
|
double estimatedTimeForCompile(CompileInfo& compileInfo) const;
|
|
|
|
bool compile(CompileInfo& compileInfo);
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::list< osg::ref_ptr<CompileOp> > CompileOps;
|
|
|
|
CompileOps _compileOps;
|
|
|
|
};
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CompileSet;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-10-22 20:19:22 +08:00
|
|
|
struct CompileCompletedCallback : public virtual osg::Referenced
|
2009-03-12 23:21:04 +08:00
|
|
|
{
|
2010-10-15 02:16:03 +08:00
|
|
|
/// return true if the callback assumes responsibility for merging any associated subgraphs with the main scene graph
|
|
|
|
/// return false if callback doesn't handle the merge, and instead requires the IncrementalCompileOperation to handle this for us
|
2009-03-12 23:21:04 +08:00
|
|
|
virtual bool compileCompleted(CompileSet* compileSet) = 0;
|
|
|
|
};
|
|
|
|
|
2010-02-19 22:30:01 +08:00
|
|
|
class OSGUTIL_EXPORT CompileSet : public osg::Referenced
|
2009-03-12 23:21:04 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CompileSet() {}
|
2010-12-25 03:19:48 +08:00
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
CompileSet(osg::Node*subgraphToCompile):
|
|
|
|
_subgraphToCompile(subgraphToCompile) {}
|
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
CompileSet(osg::Group* attachmentPoint, osg::Node* subgraphToCompile):
|
2009-03-12 23:21:04 +08:00
|
|
|
_attachmentPoint(attachmentPoint),
|
|
|
|
_subgraphToCompile(subgraphToCompile) {}
|
2010-12-25 03:19:48 +08:00
|
|
|
|
2011-01-18 01:17:19 +08:00
|
|
|
void buildCompileMap(ContextSet& contexts, StateToCompile& stateToCompile);
|
|
|
|
void buildCompileMap(ContextSet& contexts, GLObjectsVisitor::Mode mode=GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES);
|
2009-03-12 23:21:04 +08:00
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
bool compile(CompileInfo& compileInfo);
|
|
|
|
|
2011-06-16 02:31:11 +08:00
|
|
|
bool compiled() const { return _numberCompileListsToCompile==0; }
|
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
OpenThreads::Atomic _numberCompileListsToCompile;
|
2009-03-12 23:21:04 +08:00
|
|
|
|
2010-12-10 23:27:19 +08:00
|
|
|
osg::observer_ptr<osg::Group> _attachmentPoint;
|
2009-03-12 23:21:04 +08:00
|
|
|
osg::ref_ptr<osg::Node> _subgraphToCompile;
|
|
|
|
osg::ref_ptr<CompileCompletedCallback> _compileCompletedCallback;
|
2010-12-25 03:19:48 +08:00
|
|
|
|
|
|
|
typedef std::map<osg::GraphicsContext*, CompileList > CompileMap;
|
2009-03-12 23:21:04 +08:00
|
|
|
CompileMap _compileMap;
|
2010-10-22 00:29:23 +08:00
|
|
|
|
2010-12-25 03:19:48 +08:00
|
|
|
protected:
|
2010-10-22 00:29:23 +08:00
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
virtual ~CompileSet() {}
|
|
|
|
};
|
|
|
|
|
2010-10-22 00:29:23 +08:00
|
|
|
typedef std::list< osg::ref_ptr<CompileSet> > CompileSets;
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
/** Add a subgraph to be compiled.*/
|
|
|
|
void add(osg::Node* subgraphToCompile);
|
|
|
|
|
|
|
|
/** Add a subgraph to be compiled and add automatically to attachPoint on call to mergeCompiledSubgraphs.*/
|
|
|
|
void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile);
|
|
|
|
|
|
|
|
/** Add a CompileSet to be compiled.*/
|
|
|
|
void add(CompileSet* compileSet, bool callBuildCompileMap=true);
|
|
|
|
|
2010-12-10 23:27:19 +08:00
|
|
|
/** Remove CompileSet from list.*/
|
|
|
|
void remove(CompileSet* compileSet);
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; }
|
2010-12-10 23:27:19 +08:00
|
|
|
CompileSets& getToCompile() { return _toCompile; }
|
2011-04-18 20:35:19 +08:00
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
OpenThreads::Mutex* getCompiledMutex() { return &_compiledMutex; }
|
|
|
|
CompileSets& getCompiled() { return _compiled; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~IncrementalCompileOperation();
|
2011-04-18 20:35:19 +08:00
|
|
|
|
2011-06-16 02:31:11 +08:00
|
|
|
void compileSets(CompileSets& toCompile, CompileInfo compileInfo);
|
|
|
|
|
2011-01-27 00:47:40 +08:00
|
|
|
double _targetFrameRate;
|
|
|
|
double _minimumTimeAvailableForGLCompileAndDeletePerFrame;
|
|
|
|
unsigned int _maximumNumOfObjectsToCompilePerFrame;
|
|
|
|
double _flushTimeRatio;
|
|
|
|
double _conservativeTimeRatio;
|
2010-10-13 23:03:02 +08:00
|
|
|
|
2011-04-18 20:35:19 +08:00
|
|
|
unsigned int _currentFrameNumber;
|
|
|
|
unsigned int _compileAllTillFrameNumber;
|
|
|
|
|
2011-01-27 00:47:40 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> _forceTextureDownloadGeometry;
|
2010-10-13 23:03:02 +08:00
|
|
|
|
2011-01-27 00:47:40 +08:00
|
|
|
OpenThreads::Mutex _toCompileMutex;
|
|
|
|
CompileSets _toCompile;
|
2011-04-18 20:35:19 +08:00
|
|
|
|
2011-01-27 00:47:40 +08:00
|
|
|
OpenThreads::Mutex _compiledMutex;
|
|
|
|
CompileSets _compiled;
|
2011-04-18 20:35:19 +08:00
|
|
|
|
2011-01-27 00:47:40 +08:00
|
|
|
ContextSet _contexts;
|
2009-03-12 23:21:04 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|