root/OpenSceneGraph/trunk/include/osgUtil/Statistics @ 9552

Revision 9552, 7.2 kB (checked in by robert, 5 years ago)

Ported onscreen camera stats across to using thread safe stats collection

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[5328]1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
[2029]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_STATISTICS
15#define OSGUTIL_STATISTICS 1
16
[5879]17#include <osgUtil/Export>
18
[3819]19#include <osg/PrimitiveSet>
[2029]20#include <osg/Drawable>
[5289]21#include <osg/NodeVisitor>
22#include <osg/Geode>
23#include <osg/LOD>
24#include <osg/Switch>
25#include <osg/Geometry>
26#include <osg/Transform>
[2029]27
28#include <map>
[5289]29#include <set>
30#include <ostream>
[2029]31
32namespace osgUtil {
33
34/**
35 * Statistics base class. Used to extract primitive information from
36 * the renderBin(s).  Add a case of getStats(osgUtil::Statistics *stat)
37 * for any new drawable (or drawable derived class) that you generate
38 * (eg see Geometry.cpp).  There are 20 types of drawable counted - actually only
39 * 14 cases can occur in reality.  these represent sets of GL_POINTS, GL_LINES
40 * GL_LINESTRIPS, LOOPS, TRIANGLES, TRI-fans, tristrips, quads, quadstrips etc
41 * The number of triangles rendered is inferred:
42 * each triangle = 1 triangle (number of vertices/3)
43 * each quad = 2 triangles (nverts/2)
44 * each trifan or tristrip = (length-2) triangles and so on.
45 */
46
[5879]47class OSGUTIL_EXPORT Statistics : public osg::PrimitiveFunctor
[2029]48{
49    public:
50
51        typedef std::pair<unsigned int,unsigned int>    PrimitivePair;
[4556]52        typedef std::map<GLenum,PrimitivePair>          PrimitiveValueMap;
53        typedef std::map<GLenum, unsigned int>          PrimitiveCountMap;
[2029]54
55
[5863]56        Statistics();
[2029]57
[5369]58        enum StatsType
[2029]59        {
60            STAT_NONE, // default
61            STAT_FRAMERATE,
62            STAT_GRAPHS,
63            STAT_PRIMS,
64            STAT_PRIMSPERVIEW,
65            STAT_PRIMSPERBIN,
66            STAT_DC,
67            STAT_RESTART // hint to restart the stats
68        };
69       
[5863]70        void reset();
[5286]71
[5369]72        void setType(StatsType t) {stattype=t;}
[2029]73       
74        virtual void setVertexArray(unsigned int count,const osg::Vec3*) { _vertexCount += count; }
[2148]75        virtual void setVertexArray(unsigned int count,const osg::Vec2*) { _vertexCount += count; }
76        virtual void setVertexArray(unsigned int count,const osg::Vec4*) { _vertexCount += count; }
[7601]77        virtual void setVertexArray(unsigned int count,const osg::Vec3d*) { _vertexCount += count; }
78        virtual void setVertexArray(unsigned int count,const osg::Vec2d*) { _vertexCount += count; }
79        virtual void setVertexArray(unsigned int count,const osg::Vec4d*) { _vertexCount += count; }
[2029]80
[5863]81        virtual void drawArrays(GLenum mode,GLint,GLsizei count);
82        virtual void drawElements(GLenum mode,GLsizei count,const GLubyte*);
83        virtual void drawElements(GLenum mode,GLsizei count,const GLushort*);
84        virtual void drawElements(GLenum mode,GLsizei count,const GLuint*);
[2029]85
[5863]86        virtual void begin(GLenum mode);
[2148]87
88        inline void vertex()
[2029]89        {
[4556]90            PrimitivePair& prim = _primitiveCount[_currentPrimitiveFunctorMode];
[2029]91            ++prim.second;
92           _number_of_vertexes++;
93        }
[5863]94       
[2148]95        virtual void vertex(float,float,float) { vertex(); }
96        virtual void vertex(const osg::Vec3&)  { vertex(); }
[2300]97        virtual void vertex(const osg::Vec2&)  { vertex(); }
98        virtual void vertex(const osg::Vec4&)  { vertex(); }
99        virtual void vertex(float,float)   { vertex(); }
100        virtual void vertex(float,float,float,float)  { vertex(); }
[2148]101
[5863]102        virtual void end();
[2029]103       
104        void addDrawable() { numDrawables++;}
105        void addMatrix() { nummat++;}
106        void addLight(int np) { nlights+=np;}
107        void addImpostor(int np) { nimpostor+= np; }
108        inline int getBins() { return nbins;}
109        void setDepth(int d) { depth=d; }
110        void addBins(int np) { nbins+= np; }
111
[5369]112        void setBinNo(int n) { _binNo=n;}
113       
[5863]114        void add(const Statistics& stats);
[2029]115               
116    public:
117               
[9552]118        PrimitiveCountMap& getPrimitiveCountMap() { return _primitives_count; }
119        const PrimitiveCountMap& getPrimitiveCountMap() const { return _primitives_count; }
120       
121        /// deprecated
[4556]122        PrimitiveCountMap::iterator GetPrimitivesBegin() { return _primitives_count.begin(); }
[9552]123        /// deprecated
[4556]124        PrimitiveCountMap::iterator GetPrimitivesEnd() { return _primitives_count.end(); }
[2029]125
126        int numDrawables, nummat, nbins;
127        int nlights;
128        int depth; // depth into bins - eg 1.1,1.2,1.3 etc
129        int _binNo;
[5369]130        StatsType stattype;
[2029]131        int nimpostor; // number of impostors rendered
132       
133        unsigned int        _vertexCount;
[4556]134        PrimitiveValueMap    _primitiveCount;
135        GLenum              _currentPrimitiveFunctorMode;
[2029]136
137    private:
[4556]138        PrimitiveCountMap                     _primitives_count;
[2029]139
140        unsigned int                         _total_primitives_count;
141        unsigned int                         _number_of_vertexes;
142
[2219]143        inline unsigned int _calculate_primitives_number_by_mode(GLenum, GLsizei);
[2029]144};
145
146inline unsigned int Statistics::_calculate_primitives_number_by_mode(GLenum mode, GLsizei count)
147{
[5863]148    switch (mode)
[2029]149    {
[5863]150        case GL_POINTS:
151        case GL_LINE_LOOP:
152        case GL_POLYGON:  return count;
153        case GL_LINES: return count / 2;
154        case GL_LINE_STRIP: return count - 1;
155        case GL_TRIANGLES: return count / 3;
156        case GL_TRIANGLE_STRIP:
157        case GL_TRIANGLE_FAN: return count - 2;
158        case GL_QUADS: return count / 4;
159        case GL_QUAD_STRIP: return count / 2 - 1;
160        default: return 0;
[2029]161    }
162}
163
[5289]164/** StatsVisitor for collecting statistics about scene graph.*/
[5925]165class OSGUTIL_EXPORT StatsVisitor : public osg::NodeVisitor
[5289]166{
167public:
168
169    typedef std::set<osg::Node*> NodeSet;
170    typedef std::set<osg::Drawable*> DrawableSet;
[5488]171    typedef std::set<osg::StateSet*> StateSetSet;
[5289]172
[5863]173    StatsVisitor();
[5289]174       
[9377]175    META_NodeVisitor("osgUtil","StatsVisitor")
176
[5863]177    void reset();
[5289]178   
[5863]179    void apply(osg::Node& node);
[5488]180
[5863]181    void apply(osg::Group& node);
[5289]182
[5863]183    void apply(osg::Transform& node);
[5289]184
[5863]185    void apply(osg::LOD& node);
[5289]186
[5863]187    void apply(osg::Switch& node);
[5289]188
[5863]189    void apply(osg::Geode& node);
[5289]190
[5863]191    void apply(osg::Drawable& drawable);
[5488]192
[5863]193    void totalUpStats();
[5289]194   
[5863]195    void print(std::ostream& out);
[5289]196   
197    unsigned int _numInstancedGroup;
198    unsigned int _numInstancedSwitch;
199    unsigned int _numInstancedLOD;
200    unsigned int _numInstancedTransform;
201    unsigned int _numInstancedGeode;
202    unsigned int _numInstancedDrawable;
203    unsigned int _numInstancedGeometry;
[5488]204    unsigned int _numInstancedStateSet;
[5289]205
206    NodeSet _groupSet;
207    NodeSet _transformSet;
208    NodeSet _lodSet;
209    NodeSet _switchSet;
210    NodeSet _geodeSet;
211    DrawableSet _drawableSet;
212    DrawableSet _geometrySet;
[5488]213    StateSetSet _statesetSet;
[5289]214
215    osgUtil::Statistics _uniqueStats;
216    osgUtil::Statistics _instancedStats;
217};
218
[2029]219}
220
221#endif
Note: See TracBrowser for help on using the browser.