root/OpenSceneGraph/trunk/include/osgUtil/GLObjectsVisitor @ 9868

Revision 9868, 7.7 kB (checked in by robert, 6 years ago)

Preliminary work on general purpose incremental compile support in osgViewer.

  • 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_GLOBJECTSVISITOR
15#define OSGUTIL_GLOBJECTSVISITOR 1
16
17#include <osg/NodeVisitor>
18#include <osg/Geode>
19#include <osg/State>
20
21#include <osgUtil/Export>
22
23namespace osgUtil {
24
25/** Visitor for traversing scene graph and setting each osg::Drawable's _useDisplayList flag,
26  * with option to immediately compile osg::Drawable OpenGL Display lists and
27  * osg::StateAttribute's.
28  */
29class OSGUTIL_EXPORT GLObjectsVisitor : public osg::NodeVisitor
30{
31    public:
32
33        /** Operation modes of the.*/
34        enum ModeValues
35        {
36            SWITCH_ON_DISPLAY_LISTS             = 0x1,
37            SWITCH_OFF_DISPLAY_LISTS            = 0x2,
38            COMPILE_DISPLAY_LISTS               = 0x4,
39            COMPILE_STATE_ATTRIBUTES            = 0x8,
40            RELEASE_DISPLAY_LISTS               = 0x10,
41            RELEASE_STATE_ATTRIBUTES            = 0x20,
42            SWITCH_ON_VERTEX_BUFFER_OBJECTS     = 0x40,
43            SWITCH_OFF_VERTEX_BUFFER_OBJECTS    = 0x80,
44            CHECK_BLACK_LISTED_MODES            = 0x100
45        };
46       
47        typedef unsigned int Mode;
48
49        /** Construct a GLObjectsVisitor to traverse all children, operating on
50          * node according to specified mode, such as to compile or release
51          * display list/texture objects etc. Default mode is to compile
52          * GL objects.
53          */
54        GLObjectsVisitor(Mode mode=COMPILE_DISPLAY_LISTS|COMPILE_STATE_ATTRIBUTES|CHECK_BLACK_LISTED_MODES);     
55 
56        META_NodeVisitor("osg","GLObjectsVisitor")
57       
58        virtual void reset()
59        {
60            _drawablesAppliedSet.clear();
61            _stateSetAppliedSet.clear();
62        }
63
64       
65        /** Set the operational mode of what operations to do on the scene graph.*/
66        void setMode(Mode mode) { _mode = mode; }
67
68        /** Get the operational mode.*/
69        Mode getMode() const { return _mode; }
70       
71
72        /** Set the State to use during traversal. */
73        void setState(osg::State* state)
74        {
75            _renderInfo.setState(state);
76        }
77       
78        osg::State* getState()
79        {
80            return _renderInfo.getState();
81        }
82
83        void setRenderInfo(osg::RenderInfo& renderInfo)
84        {
85            _renderInfo = renderInfo;
86        }
87       
88        osg::RenderInfo& getRenderInfo()
89        {
90            return _renderInfo;
91        }
92
93        /** Simply traverse using standard NodeVisitor traverse method.*/
94        virtual void apply(osg::Node& node);
95       
96        /** For each Geode visited set the display list usage according to the
97          * _displayListMode.
98          */
99        virtual void apply(osg::Geode& node);
100
101        void apply(osg::Drawable& drawable);
102        void apply(osg::StateSet& stateset);
103
104    protected:
105
106        typedef std::set<osg::Drawable*> DrawableAppliedSet;
107        typedef std::set<osg::StateSet*> StatesSetAppliedSet;
108
109        Mode                        _mode;
110        osg::RenderInfo             _renderInfo;
111        DrawableAppliedSet          _drawablesAppliedSet;
112        StatesSetAppliedSet         _stateSetAppliedSet;
113        osg::ref_ptr<osg::Program>  _lastCompiledProgram;
114
115};
116
117class OSGUTIL_EXPORT GLObjectsOperation : public osg::GraphicsOperation
118{
119    public:
120
121        GLObjectsOperation(GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);
122
123        GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);
124
125        virtual void operator () (osg::GraphicsContext* context);
126       
127    protected:
128   
129        osg::ref_ptr<osg::Node> _subgraph;
130        GLObjectsVisitor::Mode  _mode;
131};
132
133class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation
134{
135    public:
136
137        IncrementalCompileOperation();
138       
139        typedef std::vector<osg::GraphicsContext*> Contexts;
140        void assignContexts(Contexts& contexts);
141        void removeContexts(Contexts& contexts);
142
143        void addGraphicsContext(osg::GraphicsContext* gc);
144        void removeGraphicsContext(osg::GraphicsContext* gc);
145
146
147        /** Merge subgraphs that have been compiled.*/
148        void mergeCompiledSubgraphs();
149
150        virtual void operator () (osg::GraphicsContext* context);
151       
152
153
154        struct CompileData : public osg::Referenced
155        {
156            typedef std::list< osg::ref_ptr<osg::Texture> >  Textures;
157            typedef std::list< osg::ref_ptr<osg::Drawable> > Drawables;
158            typedef std::list< osg::ref_ptr<osg::Program> >  Programs;
159           
160            bool empty() const { return _textures.empty() && _drawables.empty() && _programs.empty(); }
161           
162            Textures    _textures;
163            Drawables   _drawables;
164            Programs    _programs;
165        };
166
167        class CompileSet;
168        typedef std::set<osg::GraphicsContext*> ContextSet;
169        typedef std::map<osg::GraphicsContext*, osg::ref_ptr<CompileData> >  CompileMap;
170       
171        struct CompileCompletedCallback : public osg::Referenced
172        {
173            virtual bool compileCompleted(CompileSet* compileSet) = 0;
174        };
175
176        class CompileSet : public osg::Referenced
177        {
178        public:
179       
180           
181            CompileSet() {}
182           
183            CompileSet(osg::Node*subgraphToCompile):
184                _subgraphToCompile(subgraphToCompile) {}
185
186            CompileSet(osg::Group* attachmentPoint, osg::Node*subgraphToCompile):
187                _attachmentPoint(attachmentPoint),
188                _subgraphToCompile(subgraphToCompile) {}
189       
190            void buildCompileMap(ContextSet& context);
191
192            osg::ref_ptr<osg::Group>                _attachmentPoint;
193            osg::ref_ptr<osg::Node>                 _subgraphToCompile;
194            osg::ref_ptr<CompileCompletedCallback>  _compileCompletedCallback;
195            CompileMap                              _compileMap;
196           
197        // protected:
198       
199            virtual ~CompileSet() {}
200        };
201       
202        typedef std::list< osg::ref_ptr<CompileSet> >  CompileSets;
203
204
205        /** Add a subgraph to be compiled.*/
206        void add(osg::Node* subgraphToCompile);
207
208        /** Add a subgraph to be compiled and add automatically to attachPoint on call to mergeCompiledSubgraphs.*/
209        void add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile);
210
211        /** Add a CompileSet to be compiled.*/
212        void add(CompileSet* compileSet, bool callBuildCompileMap=true);
213
214
215        OpenThreads::Mutex& getToCompiledMutex() { return _toCompileMutex; }
216        CompileSets& getToCompile() { return _compiled; }
217       
218        OpenThreads::Mutex& getCompiledMutex() { return _compiledMutex; }
219        CompileSets& getCompiled() { return _compiled; }
220
221    protected:
222
223        virtual ~IncrementalCompileOperation();
224
225   
226        OpenThreads::Mutex  _toCompileMutex;
227        CompileSets         _toCompile;
228       
229        OpenThreads::Mutex  _compiledMutex;
230        CompileSets         _compiled;
231       
232        ContextSet          _contexts;
233
234};
235
236
237}
238
239#endif
240
Note: See TracBrowser for help on using the browser.