File: Renderer

package info (click to toggle)
openscenegraph 2.8.3-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,968 kB
  • ctags: 30,935
  • sloc: cpp: 287,169; ansic: 9,050; sh: 654; yacc: 548; objc: 374; makefile: 264; lex: 151; perl: 119
file content (180 lines) | stat: -rw-r--r-- 7,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * 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 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGVIEWER_RENDERER
#define OSGVIEWER_RENDERER 1

#include <osg/Timer>
#include <osgDB/DatabasePager>
#include <osgUtil/SceneView>
#include <osgViewer/Export>

namespace osgViewer {

class OSGVIEWER_EXPORT OpenGLQuerySupport
{
    public:
        OpenGLQuerySupport();

        typedef std::pair<GLuint, int> QueryFrameNumberPair;
        typedef std::list<QueryFrameNumberPair> QueryFrameNumberList;
        typedef std::vector<GLuint> QueryList;

        void setStartTick(osg::Timer_t startTick) { _startTick = startTick; }
        osg::Timer_t getStartTick() const { return _startTick; }

        void checkQuery(osg::Stats* stats);
        
        GLuint createQueryObject();
        void beginQuery(int frameNumber);
        void endQuery();
        void initialize(osg::State* state);

    protected:

        osg::Timer_t                                _startTick;
        bool                                        _initialized;
        bool                                        _timerQuerySupported;
        const osg::Drawable::Extensions*            _extensions;
        QueryFrameNumberList                        _queryFrameNumberList;
        QueryList                                   _availableQueryObjects;
        double                                      _previousQueryTime;

};

class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQuerySupport
{
    public:

        Renderer(osg::Camera* camera);

        osgUtil::SceneView* getSceneView(unsigned int i) { return _sceneView[i].get(); }

        void setDone(bool done) { _done = done; }
        bool getDone() { return _done; }

        void setGraphicsThreadDoesCull(bool flag);
        bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; }


        virtual void cull();
        virtual void draw();
        virtual void cull_draw();

        virtual void compile();
        
        void setCompileOnNextDraw(bool flag) { _compileOnNextDraw = flag; }
        bool getCompileOnNextDraw() const { return _compileOnNextDraw; }

        virtual void operator () (osg::Object* object);
        
        virtual void operator () (osg::GraphicsContext* context);

        virtual void release();
        

        /** Set the target frame rate that the DatabasePager 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 DatabasePager, but are should be used as a guide for how
          * long to set aside per frame for compiling and deleting OpenGL objects - ie. the value to use 
          * when calling DatabasePager::compileGLObjectgs(state,availableTime,). The longer amount of
          * time to set aside  cthe 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
          * on a modern mid to high end  PC. 
          * The way to compute the amount of available time use a scheme such as :
          *    availableTime = maximum(1.0/targetFrameRate - timeTakenDuringUpdateCullAndDraw, minimumTimeAvailableForGLCompileAndDeletePerFrame). 
          *
          * Note, the actual TargetFrameRate used is the minimum of this value and that set in the DatabasePager. */
        void setTargetFrameRate(double tfr) { _targetFrameRate = tfr; }

        /** Get the target frame rate that the DatabasePager should assume.*/
        double getTargetFrameRate() const { return _targetFrameRate; }
        
        /** Set the minimum amount of time (in seconds) that should be made available for compiling and delete OpenGL objects per frame.
          * Default value is 0.001 (1 millisecond). 
          * For usage see notes in setTargetFrameRate.
          *
          * Note, the actual TargetFrameRate used is the minimum of this value and that set in the DatabasePager. */
        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; }

        /** 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; }

    protected:
    
        virtual ~Renderer();

        virtual void updateSceneView(osgUtil::SceneView* sceneView);        
        virtual void flushAndCompile(double currentElapsedFrameTime, osgUtil::SceneView* sceneView, osgDB::DatabasePager* databasePager, osg::GraphicsThread* compileThread);

        double _targetFrameRate;
        double _minimumTimeAvailableForGLCompileAndDeletePerFrame;
        double _flushTimeRatio;
        double _conservativeTimeRatio;

        osg::observer_ptr<osg::Camera>                      _camera;
        
        bool                                                _done;
        bool                                                _graphicsThreadDoesCull;
        bool                                                _compileOnNextDraw;
        
        osg::ref_ptr<osgUtil::SceneView>                    _sceneView[2];
        
        osg::ref_ptr<osg::FlushDeletedGLObjectsOperation>   _flushOperation;
    

        struct OSGVIEWER_EXPORT ThreadSafeQueue
        {
            OpenThreads::Mutex _mutex;
            OpenThreads::Block _block;
            typedef std::list<osgUtil::SceneView*> SceneViewList;
            SceneViewList _queue;
            
            ThreadSafeQueue();
            ~ThreadSafeQueue();
            
            void release()
            {
                _block.release();
            }
            
            osgUtil::SceneView* takeFront();
            
            void add(osgUtil::SceneView* sv);
        };


        ThreadSafeQueue _availableQueue;
        ThreadSafeQueue _drawQueue;

};

}

#endif