| [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 | |
|---|
| 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 | |
|---|
| [5879] | 47 | class 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 | |
|---|
| [4556] | 118 | PrimitiveCountMap::iterator GetPrimitivesBegin() { return _primitives_count.begin(); } |
|---|
| 119 | PrimitiveCountMap::iterator GetPrimitivesEnd() { return _primitives_count.end(); } |
|---|
| [2029] | 120 | |
|---|
| 121 | int numDrawables, nummat, nbins; |
|---|
| 122 | int nlights; |
|---|
| 123 | int depth; // depth into bins - eg 1.1,1.2,1.3 etc |
|---|
| 124 | int _binNo; |
|---|
| [5369] | 125 | StatsType stattype; |
|---|
| [2029] | 126 | int nimpostor; // number of impostors rendered |
|---|
| 127 | |
|---|
| 128 | unsigned int _vertexCount; |
|---|
| [4556] | 129 | PrimitiveValueMap _primitiveCount; |
|---|
| 130 | GLenum _currentPrimitiveFunctorMode; |
|---|
| [2029] | 131 | |
|---|
| 132 | private: |
|---|
| [4556] | 133 | PrimitiveCountMap _primitives_count; |
|---|
| [2029] | 134 | |
|---|
| 135 | unsigned int _total_primitives_count; |
|---|
| 136 | unsigned int _number_of_vertexes; |
|---|
| 137 | |
|---|
| [2219] | 138 | inline unsigned int _calculate_primitives_number_by_mode(GLenum, GLsizei); |
|---|
| [2029] | 139 | }; |
|---|
| 140 | |
|---|
| 141 | inline unsigned int Statistics::_calculate_primitives_number_by_mode(GLenum mode, GLsizei count) |
|---|
| 142 | { |
|---|
| [5863] | 143 | switch (mode) |
|---|
| [2029] | 144 | { |
|---|
| [5863] | 145 | case GL_POINTS: |
|---|
| 146 | case GL_LINE_LOOP: |
|---|
| 147 | case GL_POLYGON: return count; |
|---|
| 148 | case GL_LINES: return count / 2; |
|---|
| 149 | case GL_LINE_STRIP: return count - 1; |
|---|
| 150 | case GL_TRIANGLES: return count / 3; |
|---|
| 151 | case GL_TRIANGLE_STRIP: |
|---|
| 152 | case GL_TRIANGLE_FAN: return count - 2; |
|---|
| 153 | case GL_QUADS: return count / 4; |
|---|
| 154 | case GL_QUAD_STRIP: return count / 2 - 1; |
|---|
| 155 | default: return 0; |
|---|
| [2029] | 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| [5289] | 159 | /** StatsVisitor for collecting statistics about scene graph.*/ |
|---|
| [5925] | 160 | class OSGUTIL_EXPORT StatsVisitor : public osg::NodeVisitor |
|---|
| [5289] | 161 | { |
|---|
| 162 | public: |
|---|
| 163 | |
|---|
| 164 | typedef std::set<osg::Node*> NodeSet; |
|---|
| 165 | typedef std::set<osg::Drawable*> DrawableSet; |
|---|
| [5488] | 166 | typedef std::set<osg::StateSet*> StateSetSet; |
|---|
| [5289] | 167 | |
|---|
| [5863] | 168 | StatsVisitor(); |
|---|
| [5289] | 169 | |
|---|
| [9377] | 170 | META_NodeVisitor("osgUtil","StatsVisitor") |
|---|
| 171 | |
|---|
| [5863] | 172 | void reset(); |
|---|
| [5289] | 173 | |
|---|
| [5863] | 174 | void apply(osg::Node& node); |
|---|
| [5488] | 175 | |
|---|
| [5863] | 176 | void apply(osg::Group& node); |
|---|
| [5289] | 177 | |
|---|
| [5863] | 178 | void apply(osg::Transform& node); |
|---|
| [5289] | 179 | |
|---|
| [5863] | 180 | void apply(osg::LOD& node); |
|---|
| [5289] | 181 | |
|---|
| [5863] | 182 | void apply(osg::Switch& node); |
|---|
| [5289] | 183 | |
|---|
| [5863] | 184 | void apply(osg::Geode& node); |
|---|
| [5289] | 185 | |
|---|
| [5863] | 186 | void apply(osg::Drawable& drawable); |
|---|
| [5488] | 187 | |
|---|
| [5863] | 188 | void totalUpStats(); |
|---|
| [5289] | 189 | |
|---|
| [5863] | 190 | void print(std::ostream& out); |
|---|
| [5289] | 191 | |
|---|
| 192 | unsigned int _numInstancedGroup; |
|---|
| 193 | unsigned int _numInstancedSwitch; |
|---|
| 194 | unsigned int _numInstancedLOD; |
|---|
| 195 | unsigned int _numInstancedTransform; |
|---|
| 196 | unsigned int _numInstancedGeode; |
|---|
| 197 | unsigned int _numInstancedDrawable; |
|---|
| 198 | unsigned int _numInstancedGeometry; |
|---|
| [5488] | 199 | unsigned int _numInstancedStateSet; |
|---|
| [5289] | 200 | |
|---|
| 201 | NodeSet _groupSet; |
|---|
| 202 | NodeSet _transformSet; |
|---|
| 203 | NodeSet _lodSet; |
|---|
| 204 | NodeSet _switchSet; |
|---|
| 205 | NodeSet _geodeSet; |
|---|
| 206 | DrawableSet _drawableSet; |
|---|
| 207 | DrawableSet _geometrySet; |
|---|
| [5488] | 208 | StateSetSet _statesetSet; |
|---|
| [5289] | 209 | |
|---|
| 210 | osgUtil::Statistics _uniqueStats; |
|---|
| 211 | osgUtil::Statistics _instancedStats; |
|---|
| 212 | }; |
|---|
| 213 | |
|---|
| [2029] | 214 | } |
|---|
| 215 | |
|---|
| 216 | #endif |
|---|