OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IncrementalCompileOperation.h
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 
17 #include <osgUtil/GLObjectsVisitor>
18 #include <osg/Geometry>
19 
20 namespace osgUtil {
21 
22 
24 {
25  public:
26 
27  StateToCompile(GLObjectsVisitor::Mode mode, osg::Object* markerObject=0);
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 
34  DrawableSet _drawablesHandled;
35  StateSetSet _statesetsHandled;
36 
38  DrawableSet _drawables;
39  TextureSet _textures;
40  ProgramSet _programs;
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 
87  void setMinimumTimeAvailableForGLCompileAndDeletePerFrame(double ta) { _minimumTimeAvailableForGLCompileAndDeletePerFrame = ta; }
88 
91  double getMinimumTimeAvailableForGLCompileAndDeletePerFrame() const { return _minimumTimeAvailableForGLCompileAndDeletePerFrame; }
92 
96  void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num) { _maximumNumOfObjectsToCompilePerFrame = num; }
97 
99  unsigned int getMaximumNumOfObjectsToCompilePerFrame() const { return _maximumNumOfObjectsToCompilePerFrame; }
100 
101 
104  void setFlushTimeRatio(double ratio) { _flushTimeRatio = ratio; }
105  double getFlushTimeRatio() const { return _flushTimeRatio; }
106 
111  void setConservativeTimeRatio(double ratio) { _conservativeTimeRatio = ratio; }
112  double getConservativeTimeRatio() const { return _conservativeTimeRatio; }
113 
116  void assignForceTextureDownloadGeometry();
117 
120  void setForceTextureDownloadGeometry(osg::Geometry* geom) { _forceTextureDownloadGeometry = geom; }
121  osg::Geometry* getForceTextureDownloadGeometry() { return _forceTextureDownloadGeometry.get(); }
122  const osg::Geometry* getForceTextureDownloadGeometry() const { return _forceTextureDownloadGeometry.get(); }
123 
124  typedef std::vector<osg::GraphicsContext*> Contexts;
125  void assignContexts(Contexts& contexts);
126  void removeContexts(Contexts& contexts);
127 
128  void addGraphicsContext(osg::GraphicsContext* gc);
129  void removeGraphicsContext(osg::GraphicsContext* gc);
130 
131  typedef std::set<osg::GraphicsContext*> ContextSet;
132  ContextSet& getContextSet() { return _contexts; }
133  const ContextSet& getContextSet() const { return _contexts; }
134 
135 
137  void mergeCompiledSubgraphs(const osg::FrameStamp* frameStamp);
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 
152  void setCompileAllTillFrameNumber(unsigned int fn) { _compileAllTillFrameNumber = fn; }
153  unsigned int getCompileAllTillFrameNumber() const { return _compileAllTillFrameNumber; }
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 
179  struct CompileOp : public osg::Referenced
180  {
182  virtual double estimatedTimeForCompile(CompileInfo& compileInfo) const = 0;
184  virtual bool compile(CompileInfo& compileInfo) = 0;
185  };
186 
188  {
189  CompileDrawableOp(osg::Drawable* drawable);
190  double estimatedTimeForCompile(CompileInfo& compileInfo) const;
191  bool compile(CompileInfo& compileInfo);
193  };
194 
196  {
197  CompileTextureOp(osg::Texture* texture);
198  double estimatedTimeForCompile(CompileInfo& compileInfo) const;
199  bool compile(CompileInfo& compileInfo);
201  };
202 
204  {
205  CompileProgramOp(osg::Program* program);
206  double estimatedTimeForCompile(CompileInfo& compileInfo) const;
207  bool compile(CompileInfo& compileInfo);
209  };
210 
212  {
213  public:
214  CompileList();
215  ~CompileList();
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;
228  CompileOps _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 
261 
265 
266  typedef std::map<osg::GraphicsContext*, CompileList > CompileMap;
267  CompileMap _compileMap;
268 
269  protected:
270 
271  virtual ~CompileSet() {}
272  };
273 
274  typedef std::list< osg::ref_ptr<CompileSet> > CompileSets;
275 
277  void add(osg::Node* subgraphToCompile);
278 
280  void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile);
281 
283  void add(CompileSet* compileSet, bool callBuildCompileMap=true);
284 
286  void remove(CompileSet* compileSet);
287 
288  OpenThreads::Mutex* getToCompiledMutex() { return &_toCompileMutex; }
289  CompileSets& getToCompile() { return _toCompile; }
290 
291  OpenThreads::Mutex* getCompiledMutex() { return &_compiledMutex; }
292  CompileSets& getCompiled() { return _compiled; }
293 
294  void setMarkerObject(osg::Object* mo) { _markerObject = mo; }
295  osg::Object* getMarkerObject() { return _markerObject.get(); }
296  const osg::Object* getMarkerObject() const { return _markerObject.get(); }
297 
298  protected:
299 
300  virtual ~IncrementalCompileOperation();
301 
302  void compileSets(CompileSets& toCompile, CompileInfo& compileInfo);
303 
309 
310  unsigned int _currentFrameNumber;
312 
314 
316  CompileSets _toCompile;
317 
319  CompileSets _compiled;
320 
321  ContextSet _contexts;
322 
324 
325 };
326 
327 }
328 
329 #endif
330 
osg::ref_ptr< osg::PixelBufferObject > _pbo
std::set< osg::GraphicsContext * > ContextSet
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
std::set< osg::StateSet * > StateSetSet
osg::ref_ptr< CompileCompletedCallback > _compileCompletedCallback
std::set< osg::Program * > ProgramSet
std::map< osg::GraphicsContext *, CompileList > CompileMap
std::set< osg::Texture * > TextureSet
bool okToCompile(double estimatedTimeForCompile=0.0) const
This class provides an atomic increment and decrement operation.
Definition: Atomic.h:48
void setForceTextureDownloadGeometry(osg::Geometry *geom)
const osg::Geometry * getForceTextureDownloadGeometry() const
osg::ref_ptr< osg::Geometry > _forceTextureDownloadGeometry
unsigned int getMaximumNumOfObjectsToCompilePerFrame() const
std::vector< osg::GraphicsContext * > Contexts
Definition: Node.h:71
#define OSGUTIL_EXPORT
Definition: Export.h:40
std::list< osg::ref_ptr< CompileSet > > CompileSets
std::set< osg::Drawable * > DrawableSet
void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num)
osg::ref_ptr< osg::Object > _markerObject
CompileSet(osg::Group *attachmentPoint, osg::Node *subgraphToCompile)
Shader generator framework.
Definition: RenderInfo.h:20