OpenSceneGraph 3.6.5
IncrementalCompileOperation
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGUTIL_INCREMENTALCOMPILEOPERATOR
15#define OSGUTIL_INCREMENTALCOMPILEOPERATOR
16
18#include <osg/Geometry>
19
20namespace osgUtil {
21
22
24{
25 public:
26
28
29 typedef std::set<osg::Drawable*> DrawableSet;
30 typedef std::set<osg::StateSet*> StateSetSet;
31 typedef std::set<osg::Texture*> TextureSet;
32 typedef std::set<osg::Program*> ProgramSet;
33
36
44
45 bool empty() const { return _textures.empty() && _programs.empty() && _drawables.empty(); }
46
47 virtual void apply(osg::Node& node);
48
49 virtual void apply(osg::Drawable& drawable);
50 virtual void apply(osg::StateSet& stateset);
51 virtual void apply(osg::Texture& texture);
52
53};
54
56{
57 public:
58
60
62 bool isActive() const { return !_contexts.empty(); }
63
64 bool requiresCompile(StateToCompile& stateToCompile);
65
79 void setTargetFrameRate(double tfr) { _targetFrameRate = tfr; }
80
82 double getTargetFrameRate() const { return _targetFrameRate; }
83
88
92
97
100
101
104 void setFlushTimeRatio(double ratio) { _flushTimeRatio = ratio; }
105 double getFlushTimeRatio() const { return _flushTimeRatio; }
106
111 void setConservativeTimeRatio(double ratio) { _conservativeTimeRatio = ratio; }
113
117
123
124 typedef std::vector<osg::GraphicsContext*> Contexts;
125 void assignContexts(Contexts& contexts);
126 void removeContexts(Contexts& contexts);
127
130
131 typedef std::set<osg::GraphicsContext*> ContextSet;
133 const ContextSet& getContextSet() const { return _contexts; }
134
135
138
143 void setCurrentFrameNumber(unsigned int fn) { _currentFrameNumber = fn; }
144 unsigned int getCurrentFrameNumber() const { return _currentFrameNumber; }
145
148 void compileAllForNextFrame(unsigned int numFramesToDoCompileAll=1);
149
154
155
156
157
158 virtual void operator () (osg::GraphicsContext* context);
159
161 {
163
164 bool okToCompile(double estimatedTimeForCompile=0.0) const
165 {
166 if (compileAll) return true;
167 if (maxNumObjectsToCompile==0) return false;
168 return (allocatedTime - timer.elapsedTime()) >= estimatedTimeForCompile;
169 }
170
172
177 };
178
180 {
182 virtual double estimatedTimeForCompile(CompileInfo& compileInfo) const = 0;
184 virtual bool compile(CompileInfo& compileInfo) = 0;
185 };
186
194
202
210
212 {
213 public:
216
217 bool empty() const { return _compileOps.empty(); }
218 void add(CompileOp* compileOp);
219 void add(osg::Drawable* drawable) { add(new CompileDrawableOp(drawable)); }
220 void add(osg::Texture* texture) { add(new CompileTextureOp(texture)); }
221 void add(osg::Program* program) { add(new CompileProgramOp(program)); }
222
223 double estimatedTimeForCompile(CompileInfo& compileInfo) const;
224 bool compile(CompileInfo& compileInfo);
225
226
227 typedef std::list< osg::ref_ptr<CompileOp> > CompileOps;
229 };
230
231
232 class CompileSet;
233
235 {
238 virtual bool compileCompleted(CompileSet* compileSet) = 0;
239 };
240
242 {
243 public:
245
246 CompileSet(osg::Node*subgraphToCompile):
247 _subgraphToCompile(subgraphToCompile) {}
248
249 CompileSet(osg::Group* attachmentPoint, osg::Node* subgraphToCompile):
250 _attachmentPoint(attachmentPoint),
251 _subgraphToCompile(subgraphToCompile) {}
252
253 void buildCompileMap(ContextSet& contexts, StateToCompile& stateToCompile);
255
256 bool compile(CompileInfo& compileInfo);
257
258 bool compiled() const { return _numberCompileListsToCompile==0; }
259
260 OpenThreads::Atomic _numberCompileListsToCompile;
261
265
266 typedef std::map<osg::GraphicsContext*, CompileList > CompileMap;
268
270
271 protected:
272
273 virtual ~CompileSet() {}
274 };
275
276 typedef std::list< osg::ref_ptr<CompileSet> > CompileSets;
277
279 void add(osg::Node* subgraphToCompile);
280
282 void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile);
283
285 void add(CompileSet* compileSet, bool callBuildCompileMap=true);
286
288 void remove(CompileSet* compileSet);
289
290 OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; }
292
293 OpenThreads::Mutex* getCompiledMutex() { return &_compiledMutex; }
295
298 const osg::Object* getMarkerObject() const { return _markerObject.get(); }
299
300 protected:
301
303
304 void compileSets(CompileSets& toCompile, CompileInfo& compileInfo);
305
311
314
316
317 OpenThreads::Mutex _toCompileMutex;
319
320 OpenThreads::Mutex _compiledMutex;
322
324
326
327};
328
329}
330
331#endif
332
The osgUtil library provides general purpose utility classes such as update, cull and draw traverses,...
Definition NodeVisitor:25
Pure virtual base class for drawable geometry.
Definition Drawable:89
Class which encapsulates the frame number, reference time and calendar time of specific frame,...
Definition FrameStamp:35
Definition Geometry:31
Base class for providing Windowing API agnostic access to creating and managing graphics context.
Definition GraphicsContext:30
Definition GraphicsThread:36
General group node which maintains a list of children.
Definition Group:29
Base class for all internal nodes in the scene graph.
Definition Node:72
Visitor for type safe operations on osg::Nodes.
Definition NodeVisitor:82
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
Smart pointer for observed objects, that automatically set pointers to them to null when they are del...
Definition observer_ptr:39
osg::Program is an application-level abstraction of an OpenGL glProgram.
Definition Program:52
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
Definition RenderInfo:28
Stores a set of modes and attributes which represent a set of OpenGL state.
Definition StateSet:46
Texture pure virtual base class that encapsulates OpenGL texture functionality common to the various ...
Definition Texture:422
Helper class for timing sections of code.
Definition Timer:81
@ COMPILE_STATE_ATTRIBUTES
Definition GLObjectsVisitor:40
@ COMPILE_DISPLAY_LISTS
Definition GLObjectsVisitor:39
unsigned int Mode
Definition GLObjectsVisitor:48
Definition IncrementalCompileOperation:24
std::set< osg::Drawable * > DrawableSet
Definition IncrementalCompileOperation:29
std::set< osg::StateSet * > StateSetSet
Definition IncrementalCompileOperation:30
osg::ref_ptr< osg::PixelBufferObject > _pbo
Definition IncrementalCompileOperation:42
StateToCompile(GLObjectsVisitor::Mode mode, osg::Object *markerObject)
ProgramSet _programs
Definition IncrementalCompileOperation:40
bool empty() const
Definition IncrementalCompileOperation:45
virtual void apply(osg::Texture &texture)
virtual void apply(osg::Node &node)
DrawableSet _drawablesHandled
Definition IncrementalCompileOperation:34
virtual void apply(osg::Drawable &drawable)
bool _assignPBOToImages
Definition IncrementalCompileOperation:41
GLObjectsVisitor::Mode _mode
Definition IncrementalCompileOperation:37
osg::ref_ptr< osg::Object > _markerObject
Definition IncrementalCompileOperation:43
std::set< osg::Program * > ProgramSet
Definition IncrementalCompileOperation:32
DrawableSet _drawables
Definition IncrementalCompileOperation:38
virtual void apply(osg::StateSet &stateset)
TextureSet _textures
Definition IncrementalCompileOperation:39
StateSetSet _statesetsHandled
Definition IncrementalCompileOperation:35
std::set< osg::Texture * > TextureSet
Definition IncrementalCompileOperation:31
unsigned int _compileAllTillFrameNumber
Definition IncrementalCompileOperation:313
unsigned int _maximumNumOfObjectsToCompilePerFrame
Definition IncrementalCompileOperation:308
void mergeCompiledSubgraphs(const osg::FrameStamp *frameStamp)
Merge subgraphs that have been compiled.
double getTargetFrameRate() const
Get the target frame rate that the IncrementalCompileOperation should assume.
Definition IncrementalCompileOperation:82
double getMinimumTimeAvailableForGLCompileAndDeletePerFrame() const
Get the minimum amount of time that should be made available for compiling and delete OpenGL objects ...
Definition IncrementalCompileOperation:91
bool isActive() const
Return true if the IncrementCompileOperation is active.
Definition IncrementalCompileOperation:62
void addGraphicsContext(osg::GraphicsContext *gc)
void add(osg::Node *subgraphToCompile)
Add a subgraph to be compiled.
ContextSet _contexts
Definition IncrementalCompileOperation:323
OpenThreads::Mutex * getToCompiledMutex()
Definition IncrementalCompileOperation:290
unsigned int getCompileAllTillFrameNumber() const
Definition IncrementalCompileOperation:153
CompileSets _compiled
Definition IncrementalCompileOperation:321
std::list< osg::ref_ptr< CompileSet > > CompileSets
Definition IncrementalCompileOperation:276
ContextSet & getContextSet()
Definition IncrementalCompileOperation:132
const osg::Object * getMarkerObject() const
Definition IncrementalCompileOperation:298
void assignForceTextureDownloadGeometry()
Assign a geometry and associated StateSet than is applied after each texture compile to atttempt to f...
void setCurrentFrameNumber(unsigned int fn)
Set the current frame number that the IncrementalCompileOperation should use as a reference value for...
Definition IncrementalCompileOperation:143
void setFlushTimeRatio(double ratio)
FlushTimeRatio governs how much of the spare time in each frame is used for flushing deleted OpenGL o...
Definition IncrementalCompileOperation:104
OpenThreads::Mutex * getCompiledMutex()
Definition IncrementalCompileOperation:293
osg::Geometry * getForceTextureDownloadGeometry()
Definition IncrementalCompileOperation:121
void setMinimumTimeAvailableForGLCompileAndDeletePerFrame(double ta)
Set the minimum amount of time (in seconds) that should be made available for compiling and delete Op...
Definition IncrementalCompileOperation:87
bool requiresCompile(StateToCompile &stateToCompile)
osg::ref_ptr< osg::Object > _markerObject
Definition IncrementalCompileOperation:325
void add(osg::Group *attachmentPoint, osg::Node *subgraphToCompile)
Add a subgraph to be compiled and add automatically to attachPoint on call to mergeCompiledSubgraphs.
void setTargetFrameRate(double tfr)
Set the target frame rate that the IncrementalCompileOperation should assume.
Definition IncrementalCompileOperation:79
unsigned int _currentFrameNumber
Definition IncrementalCompileOperation:312
double _targetFrameRate
Definition IncrementalCompileOperation:306
double _conservativeTimeRatio
Definition IncrementalCompileOperation:310
void setCompileAllTillFrameNumber(unsigned int fn)
tell the IncrementalCompileOperation to compile all pending objects during next draw traversal,...
Definition IncrementalCompileOperation:152
void setForceTextureDownloadGeometry(osg::Geometry *geom)
Set the osg::Geometry to apply after each texture compile to atttempt to force the OpenGL drive to do...
Definition IncrementalCompileOperation:120
void compileAllForNextFrame(unsigned int numFramesToDoCompileAll=1)
tell the IncrementalCompileOperation to compile all pending objects during next draw traversal,...
double getFlushTimeRatio() const
Definition IncrementalCompileOperation:105
std::set< osg::GraphicsContext * > ContextSet
Definition IncrementalCompileOperation:131
osg::ref_ptr< osg::Geometry > _forceTextureDownloadGeometry
Definition IncrementalCompileOperation:315
OpenThreads::Mutex _toCompileMutex
Definition IncrementalCompileOperation:317
osg::Object * getMarkerObject()
Definition IncrementalCompileOperation:297
void remove(CompileSet *compileSet)
Remove CompileSet from list.
void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num)
Set the maximum number of OpenGL objects that the page should attempt to compile per frame.
Definition IncrementalCompileOperation:96
void setMarkerObject(osg::Object *mo)
Definition IncrementalCompileOperation:296
CompileSets _toCompile
Definition IncrementalCompileOperation:318
double _minimumTimeAvailableForGLCompileAndDeletePerFrame
Definition IncrementalCompileOperation:307
void setConservativeTimeRatio(double ratio)
ConservativeTimeRatio governs how much of the measured spare time in each frame is used for flushing ...
Definition IncrementalCompileOperation:111
void removeContexts(Contexts &contexts)
std::vector< osg::GraphicsContext * > Contexts
Definition IncrementalCompileOperation:124
double _flushTimeRatio
Definition IncrementalCompileOperation:309
void assignContexts(Contexts &contexts)
const osg::Geometry * getForceTextureDownloadGeometry() const
Definition IncrementalCompileOperation:122
const ContextSet & getContextSet() const
Definition IncrementalCompileOperation:133
unsigned int getCurrentFrameNumber() const
Definition IncrementalCompileOperation:144
unsigned int getMaximumNumOfObjectsToCompilePerFrame() const
Get the maximum number of OpenGL objects that the page should attempt to compile per frame.
Definition IncrementalCompileOperation:99
OpenThreads::Mutex _compiledMutex
Definition IncrementalCompileOperation:320
CompileSets & getCompiled()
Definition IncrementalCompileOperation:294
void compileSets(CompileSets &toCompile, CompileInfo &compileInfo)
void add(CompileSet *compileSet, bool callBuildCompileMap=true)
Add a CompileSet to be compiled.
void removeGraphicsContext(osg::GraphicsContext *gc)
double getConservativeTimeRatio() const
Definition IncrementalCompileOperation:112
CompileSets & getToCompile()
Definition IncrementalCompileOperation:291
Definition IncrementalCompileOperation:161
IncrementalCompileOperation * incrementalCompileOperation
Definition IncrementalCompileOperation:171
CompileInfo(osg::GraphicsContext *context, IncrementalCompileOperation *ico)
unsigned int maxNumObjectsToCompile
Definition IncrementalCompileOperation:174
bool okToCompile(double estimatedTimeForCompile=0.0) const
Definition IncrementalCompileOperation:164
osg::ElapsedTime timer
Definition IncrementalCompileOperation:176
double allocatedTime
Definition IncrementalCompileOperation:175
bool compileAll
Definition IncrementalCompileOperation:173
Definition IncrementalCompileOperation:180
virtual double estimatedTimeForCompile(CompileInfo &compileInfo) const =0
return an estimate for how many seconds the compile will take.
virtual bool compile(CompileInfo &compileInfo)=0
compile associated objects, return true if object as been fully compiled and this CompileOp can be re...
Definition IncrementalCompileOperation:188
osg::ref_ptr< osg::Drawable > _drawable
Definition IncrementalCompileOperation:192
bool compile(CompileInfo &compileInfo)
compile associated objects, return true if object as been fully compiled and this CompileOp can be re...
double estimatedTimeForCompile(CompileInfo &compileInfo) const
return an estimate for how many seconds the compile will take.
Definition IncrementalCompileOperation:196
bool compile(CompileInfo &compileInfo)
compile associated objects, return true if object as been fully compiled and this CompileOp can be re...
double estimatedTimeForCompile(CompileInfo &compileInfo) const
return an estimate for how many seconds the compile will take.
osg::ref_ptr< osg::Texture > _texture
Definition IncrementalCompileOperation:200
Definition IncrementalCompileOperation:204
double estimatedTimeForCompile(CompileInfo &compileInfo) const
return an estimate for how many seconds the compile will take.
bool compile(CompileInfo &compileInfo)
compile associated objects, return true if object as been fully compiled and this CompileOp can be re...
osg::ref_ptr< osg::Program > _program
Definition IncrementalCompileOperation:208
std::list< osg::ref_ptr< CompileOp > > CompileOps
Definition IncrementalCompileOperation:227
void add(osg::Program *program)
Definition IncrementalCompileOperation:221
void add(osg::Drawable *drawable)
Definition IncrementalCompileOperation:219
bool empty() const
Definition IncrementalCompileOperation:217
CompileOps _compileOps
Definition IncrementalCompileOperation:228
void add(osg::Texture *texture)
Definition IncrementalCompileOperation:220
double estimatedTimeForCompile(CompileInfo &compileInfo) const
Definition IncrementalCompileOperation:235
virtual bool compileCompleted(CompileSet *compileSet)=0
return true if the callback assumes responsibility for merging any associated subgraphs with the main...
Definition IncrementalCompileOperation:242
std::map< osg::GraphicsContext *, CompileList > CompileMap
Definition IncrementalCompileOperation:266
osg::ref_ptr< osg::Node > _subgraphToCompile
Definition IncrementalCompileOperation:263
OpenThreads::Atomic _numberCompileListsToCompile
Definition IncrementalCompileOperation:260
CompileSet(osg::Group *attachmentPoint, osg::Node *subgraphToCompile)
Definition IncrementalCompileOperation:249
CompileSet(osg::Node *subgraphToCompile)
Definition IncrementalCompileOperation:246
osg::observer_ptr< osg::Group > _attachmentPoint
Definition IncrementalCompileOperation:262
void buildCompileMap(ContextSet &contexts, GLObjectsVisitor::Mode mode=GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES)
CompileSet()
Definition IncrementalCompileOperation:244
bool compile(CompileInfo &compileInfo)
osg::ref_ptr< osg::Object > _markerObject
Definition IncrementalCompileOperation:269
void buildCompileMap(ContextSet &contexts, StateToCompile &stateToCompile)
bool compiled() const
Definition IncrementalCompileOperation:258
osg::ref_ptr< CompileCompletedCallback > _compileCompletedCallback
Definition IncrementalCompileOperation:264
virtual ~CompileSet()
Definition IncrementalCompileOperation:273
CompileMap _compileMap
Definition IncrementalCompileOperation:267
#define OSGUTIL_EXPORT
Definition Export:40

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.