root/OpenSceneGraph/trunk/include/osgUtil/IncrementalCompileOperation @ 13041

Revision 13041, 14.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
20namespace osgUtil {
21
22
23class OSGUTIL_EXPORT StateToCompile : public osg::NodeVisitor
24{
25    public:
26
27        StateToCompile(GLObjectsVisitor::Mode mode);
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
37        GLObjectsVisitor::Mode _mode;
38        DrawableSet _drawables;
39        TextureSet  _textures;
40        ProgramSet  _programs;
41        bool        _assignPBOToImages;
42        osg::ref_ptr<osg::PixelBufferObject> _pbo;
43
44        bool empty() const { return _textures.empty() &&  _programs.empty() && _drawables.empty(); }
45
46        virtual void apply(osg::Node& node);
47        virtual void apply(osg::Geode& 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
55class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
56{
57    public:
58
59        IncrementalCompileOperation();
60
61        /** Return true if the IncrementCompileOperation is active.*/
62        bool isActive() const { return !_contexts.empty(); }
63
64        bool requiresCompile(StateToCompile& stateToCompile);
65
66        /** Set the target frame rate that the IncrementalCompileOperation should assume.
67          * Typically one would set this to the value refresh rate of your display system i.e. 60Hz.
68          * Default value is 100.
69          * Usage notes.  The TargetFrameRate and the MinimumTimeAvailableForGLCompileAndDeletePerFrame
70          * parameters are not directly used by IncrementalCompileOperation, but are should be used as a guide for how
71          * long to set aside per frame for compiling and deleting OpenGL objects. The longer amount of
72          * time to set aside  the faster databases will be paged in but with increased chance of frame drops,
73          * the lower the amount of time the set aside the slower databases will paged it but with better
74          * chance of avoid any frame drops.  The default values are chosen to achieve the later when running
75          * on a modern mid to high end  PC.
76          * The way to compute the amount of available time use a scheme such as :
77          *    availableTime = maximum(1.0/targetFrameRate - timeTakenDuringUpdateCullAndDraw, minimumTimeAvailableForGLCompileAndDeletePerFrame).
78          */
79        void setTargetFrameRate(double tfr) { _targetFrameRate = tfr; }
80
81        /** Get the target frame rate that the IncrementalCompileOperation should assume.*/
82        double getTargetFrameRate() const { return _targetFrameRate; }
83
84        /** Set the minimum amount of time (in seconds) that should be made available for compiling and delete OpenGL objects per frame.
85          * Default value is 0.001 (1 millisecond).
86          * For usage see notes in setTargetFrameRate.*/
87        void setMinimumTimeAvailableForGLCompileAndDeletePerFrame(double ta) { _minimumTimeAvailableForGLCompileAndDeletePerFrame = ta; }
88
89        /** Get the minimum amount of time that should be made available for compiling and delete OpenGL objects per frame.
90          * For usage see notes in setTargetFrameRate.*/
91        double getMinimumTimeAvailableForGLCompileAndDeletePerFrame() const { return _minimumTimeAvailableForGLCompileAndDeletePerFrame; }
92
93        /** Set the maximum number of OpenGL objects that the page should attempt to compile per frame.
94          * Note, Lower values reduces chances of a frame drop but lower the rate that database will be paged in at.
95          * Default value is 8. */
96        void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num) { _maximumNumOfObjectsToCompilePerFrame = num; }
97
98        /** Get the maximum number of OpenGL objects that the page should attempt to compile per frame.*/
99        unsigned int getMaximumNumOfObjectsToCompilePerFrame() const { return _maximumNumOfObjectsToCompilePerFrame; }
100
101
102        /** FlushTimeRatio governs how much of the spare time in each frame is used for flushing deleted OpenGL objects.
103          * Default value is 0.5, valid range is 0.1 to 0.9.*/
104        void setFlushTimeRatio(double ratio) { _flushTimeRatio = ratio; }
105        double getFlushTimeRatio() const { return _flushTimeRatio; }
106
107        /** ConservativeTimeRatio governs how much of the measured spare time in each frame is used for flushing deleted and compile new OpenGL objects.
108          * Default value is 0.5, valid range is 0.1 to 1.0.
109          * A ratio near 1.0 will lead to paged databases being compiled and merged quicker but increase the chances of frame drop.
110          * A ratio near 0.1 will lead to paged databases being compiled and merged closer but reduse the chances of frame drop.*/
111        void setConservativeTimeRatio(double ratio) { _conservativeTimeRatio = ratio; }
112        double getConservativeTimeRatio() const { return _conservativeTimeRatio; }
113
114        /** Assign a geometry and associated StateSet than is applied after each texture compile to atttempt to force the OpenGL
115          * drive to download the texture object to OpenGL graphics card.*/
116        void assignForceTextureDownloadGeometry();
117
118        /** Set the osg::Geometry to apply after each texture compile to atttempt to force the OpenGL
119          * drive to download the texture object to OpenGL graphics card.*/
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
136        /** Merge subgraphs that have been compiled.*/
137        void mergeCompiledSubgraphs(const osg::FrameStamp* frameStamp);
138
139        /** Set the current frame number that the IncrementalCompileOperation should use as a reference
140         * value for calculations based on current frame number.
141         * Note, this value is set by the mergeCompiledSubgraphs(..) method so one won't normally need to call
142         * set the CurrentFrameNumber manually.*/
143        void setCurrentFrameNumber(unsigned int fn) { _currentFrameNumber = fn; }
144        unsigned int getCurrentFrameNumber() const { return _currentFrameNumber; }
145
146        /** tell the IncrementalCompileOperation to compile all pending objects during next draw traversal,
147         *  for specified number of frames.*/
148        void compileAllForNextFrame(unsigned int numFramesToDoCompileAll=1);
149
150        /** tell the IncrementalCompileOperation to compile all pending objects during next draw traversal,
151         * till specified frame number.*/
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
160        struct OSGUTIL_EXPORT CompileInfo : public osg::RenderInfo
161        {
162            CompileInfo(osg::GraphicsContext* context, IncrementalCompileOperation* ico);
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
171            IncrementalCompileOperation*        incrementalCompileOperation;
172
173            bool                                compileAll;
174            unsigned int                        maxNumObjectsToCompile;
175            double                              allocatedTime;
176            osg::ElapsedTime                    timer;
177        };
178
179        struct CompileOp : public osg::Referenced
180        {
181            /** return an estimate for how many seconds the compile will take.*/
182            virtual double estimatedTimeForCompile(CompileInfo& compileInfo) const = 0;
183            /** compile associated objects, return true if object as been fully compiled and this CompileOp can be removed from the to compile list.*/
184            virtual bool compile(CompileInfo& compileInfo) = 0;
185        };
186
187        struct OSGUTIL_EXPORT CompileDrawableOp : public CompileOp
188        {
189            CompileDrawableOp(osg::Drawable* drawable);
190            double estimatedTimeForCompile(CompileInfo& compileInfo) const;
191            bool compile(CompileInfo& compileInfo);
192            osg::ref_ptr<osg::Drawable> _drawable;
193        };
194
195        struct OSGUTIL_EXPORT CompileTextureOp : public CompileOp
196        {
197            CompileTextureOp(osg::Texture* texture);
198            double estimatedTimeForCompile(CompileInfo& compileInfo) const;
199            bool compile(CompileInfo& compileInfo);
200            osg::ref_ptr<osg::Texture> _texture;
201        };
202
203        struct OSGUTIL_EXPORT CompileProgramOp : public CompileOp
204        {
205            CompileProgramOp(osg::Program* program);
206            double estimatedTimeForCompile(CompileInfo& compileInfo) const;
207            bool compile(CompileInfo& compileInfo);
208            osg::ref_ptr<osg::Program> _program;
209        };
210
211        class OSGUTIL_EXPORT CompileList
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
234        struct CompileCompletedCallback : public virtual osg::Referenced
235        {
236            /// return true if the callback assumes responsibility for merging any associated subgraphs with the main scene graph
237            /// return false if callback doesn't handle the merge, and instead requires the IncrementalCompileOperation to handle this for us
238            virtual bool compileCompleted(CompileSet* compileSet) = 0;
239        };
240
241        class OSGUTIL_EXPORT CompileSet : public osg::Referenced
242        {
243        public:
244            CompileSet() {}
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);
254            void buildCompileMap(ContextSet& contexts, GLObjectsVisitor::Mode mode=GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES);
255
256            bool compile(CompileInfo& compileInfo);
257
258            bool compiled() const { return _numberCompileListsToCompile==0; }
259
260            OpenThreads::Atomic                     _numberCompileListsToCompile;
261
262            osg::observer_ptr<osg::Group>           _attachmentPoint;
263            osg::ref_ptr<osg::Node>                 _subgraphToCompile;
264            osg::ref_ptr<CompileCompletedCallback>  _compileCompletedCallback;
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
276        /** Add a subgraph to be compiled.*/
277        void add(osg::Node* subgraphToCompile);
278
279        /** Add a subgraph to be compiled and add automatically to attachPoint on call to mergeCompiledSubgraphs.*/
280        void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile);
281
282        /** Add a CompileSet to be compiled.*/
283        void add(CompileSet* compileSet, bool callBuildCompileMap=true);
284
285        /** Remove CompileSet from list.*/
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    protected:
295
296        virtual ~IncrementalCompileOperation();
297
298        void compileSets(CompileSets& toCompile, CompileInfo compileInfo);
299
300        double                              _targetFrameRate;
301        double                              _minimumTimeAvailableForGLCompileAndDeletePerFrame;
302        unsigned int                        _maximumNumOfObjectsToCompilePerFrame;
303        double                              _flushTimeRatio;
304        double                              _conservativeTimeRatio;
305
306        unsigned int                        _currentFrameNumber;
307        unsigned int                        _compileAllTillFrameNumber;
308
309        osg::ref_ptr<osg::Geometry>         _forceTextureDownloadGeometry;
310
311        OpenThreads::Mutex                  _toCompileMutex;
312        CompileSets                         _toCompile;
313
314        OpenThreads::Mutex                  _compiledMutex;
315        CompileSets                         _compiled;
316
317        ContextSet                          _contexts;
318
319};
320
321}
322
323#endif
324
Note: See TracBrowser for help on using the browser.