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_STATISTICS |
---|
15 | #define OSGUTIL_STATISTICS 1 |
---|
16 | |
---|
17 | #include <osgUtil/Export> |
---|
18 | |
---|
19 | #include <osg/PrimitiveSet> |
---|
20 | #include <osg/Drawable> |
---|
21 | #include <osg/NodeVisitor> |
---|
22 | #include <osg/Geode> |
---|
23 | #include <osg/LOD> |
---|
24 | #include <osg/Switch> |
---|
25 | #include <osg/Geometry> |
---|
26 | #include <osg/Transform> |
---|
27 | |
---|
28 | #include <map> |
---|
29 | #include <set> |
---|
30 | #include <ostream> |
---|
31 | |
---|
32 | namespace 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 | |
---|
47 | class OSGUTIL_EXPORT Statistics : public osg::PrimitiveFunctor |
---|
48 | { |
---|
49 | public: |
---|
50 | |
---|
51 | typedef std::pair<unsigned int,unsigned int> PrimitivePair; |
---|
52 | typedef std::map<GLenum,PrimitivePair> PrimitiveValueMap; |
---|
53 | typedef std::map<GLenum, unsigned int> PrimitiveCountMap; |
---|
54 | |
---|
55 | |
---|
56 | Statistics(); |
---|
57 | |
---|
58 | enum StatsType |
---|
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 | |
---|
70 | void reset(); |
---|
71 | |
---|
72 | void setType(StatsType t) {stattype=t;} |
---|
73 | |
---|
74 | virtual void setVertexArray(unsigned int count,const osg::Vec3*) { _vertexCount += count; } |
---|
75 | virtual void setVertexArray(unsigned int count,const osg::Vec2*) { _vertexCount += count; } |
---|
76 | virtual void setVertexArray(unsigned int count,const osg::Vec4*) { _vertexCount += count; } |
---|
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; } |
---|
80 | |
---|
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*); |
---|
85 | |
---|
86 | virtual void begin(GLenum mode); |
---|
87 | |
---|
88 | inline void vertex() |
---|
89 | { |
---|
90 | PrimitivePair& prim = _primitiveCount[_currentPrimitiveFunctorMode]; |
---|
91 | ++prim.second; |
---|
92 | _number_of_vertexes++; |
---|
93 | } |
---|
94 | |
---|
95 | virtual void vertex(float,float,float) { vertex(); } |
---|
96 | virtual void vertex(const osg::Vec3&) { vertex(); } |
---|
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(); } |
---|
101 | |
---|
102 | virtual void end(); |
---|
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 | |
---|
112 | void setBinNo(int n) { _binNo=n;} |
---|
113 | |
---|
114 | void add(const Statistics& stats); |
---|
115 | |
---|
116 | public: |
---|
117 | |
---|
118 | PrimitiveCountMap& getPrimitiveCountMap() { return _primitives_count; } |
---|
119 | const PrimitiveCountMap& getPrimitiveCountMap() const { return _primitives_count; } |
---|
120 | |
---|
121 | /// deprecated |
---|
122 | PrimitiveCountMap::iterator GetPrimitivesBegin() { return _primitives_count.begin(); } |
---|
123 | /// deprecated |
---|
124 | PrimitiveCountMap::iterator GetPrimitivesEnd() { return _primitives_count.end(); } |
---|
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; |
---|
130 | StatsType stattype; |
---|
131 | int nimpostor; // number of impostors rendered |
---|
132 | |
---|
133 | unsigned int _vertexCount; |
---|
134 | PrimitiveValueMap _primitiveCount; |
---|
135 | GLenum _currentPrimitiveFunctorMode; |
---|
136 | |
---|
137 | private: |
---|
138 | PrimitiveCountMap _primitives_count; |
---|
139 | |
---|
140 | unsigned int _total_primitives_count; |
---|
141 | unsigned int _number_of_vertexes; |
---|
142 | |
---|
143 | inline unsigned int _calculate_primitives_number_by_mode(GLenum, GLsizei); |
---|
144 | }; |
---|
145 | |
---|
146 | inline unsigned int Statistics::_calculate_primitives_number_by_mode(GLenum mode, GLsizei count) |
---|
147 | { |
---|
148 | switch (mode) |
---|
149 | { |
---|
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; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | /** StatsVisitor for collecting statistics about scene graph.*/ |
---|
165 | class OSGUTIL_EXPORT StatsVisitor : public osg::NodeVisitor |
---|
166 | { |
---|
167 | public: |
---|
168 | |
---|
169 | typedef std::set<osg::Node*> NodeSet; |
---|
170 | typedef std::set<osg::Drawable*> DrawableSet; |
---|
171 | typedef std::set<osg::StateSet*> StateSetSet; |
---|
172 | |
---|
173 | StatsVisitor(); |
---|
174 | |
---|
175 | META_NodeVisitor("osgUtil","StatsVisitor") |
---|
176 | |
---|
177 | void reset(); |
---|
178 | |
---|
179 | void apply(osg::Node& node); |
---|
180 | |
---|
181 | void apply(osg::Group& node); |
---|
182 | |
---|
183 | void apply(osg::Transform& node); |
---|
184 | |
---|
185 | void apply(osg::LOD& node); |
---|
186 | |
---|
187 | void apply(osg::Switch& node); |
---|
188 | |
---|
189 | void apply(osg::Geode& node); |
---|
190 | |
---|
191 | void apply(osg::Drawable& drawable); |
---|
192 | |
---|
193 | void totalUpStats(); |
---|
194 | |
---|
195 | void print(std::ostream& out); |
---|
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; |
---|
204 | unsigned int _numInstancedFastGeometry; |
---|
205 | unsigned int _numInstancedStateSet; |
---|
206 | |
---|
207 | NodeSet _groupSet; |
---|
208 | NodeSet _transformSet; |
---|
209 | NodeSet _lodSet; |
---|
210 | NodeSet _switchSet; |
---|
211 | NodeSet _geodeSet; |
---|
212 | DrawableSet _drawableSet; |
---|
213 | DrawableSet _geometrySet; |
---|
214 | DrawableSet _fastGeometrySet; |
---|
215 | StateSetSet _statesetSet; |
---|
216 | |
---|
217 | osgUtil::Statistics _uniqueStats; |
---|
218 | osgUtil::Statistics _instancedStats; |
---|
219 | }; |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | #endif |
---|