Statistics 7.7 KB

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