root/OpenSceneGraph/trunk/src/osgUtil/Statistics.cpp @ 6461

Revision 6461, 8.1 kB (checked in by robert, 7 years ago)

From Eric Wing, pedantic warning fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osgUtil/Statistics>
15
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
25#include <map>
26#include <set>
27#include <ostream>
28
29using namespace osgUtil;
30
31Statistics::Statistics()
32{
33    reset();
34}
35
36void Statistics::reset()
37{
38    numDrawables=0;
39    nummat=0;
40    depth=0;
41    stattype=STAT_NONE;
42    nlights=0;
43    nbins=0;
44    nimpostor=0;
45
46    _vertexCount=0;
47    _primitiveCount.clear();           
48
49    _currentPrimitiveFunctorMode=0;
50
51    _primitives_count.clear();
52    _total_primitives_count=0;
53    _number_of_vertexes=0;
54}
55
56void Statistics::drawArrays(GLenum mode,GLint,GLsizei count)
57{
58    PrimitivePair& prim = _primitiveCount[mode];
59    ++prim.first;
60    prim.second+=count;
61    _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
62}
63
64void Statistics::drawElements(GLenum mode,GLsizei count,const GLubyte*)
65{
66    PrimitivePair& prim = _primitiveCount[mode];
67    ++prim.first;
68    prim.second+=count;
69    _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
70}
71
72void Statistics::drawElements(GLenum mode,GLsizei count,const GLushort*)
73{
74    PrimitivePair& prim = _primitiveCount[mode];
75    ++prim.first;
76    prim.second+=count;
77    _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
78}
79
80void Statistics::drawElements(GLenum mode,GLsizei count,const GLuint*)
81{
82    PrimitivePair& prim = _primitiveCount[mode];
83    ++prim.first;
84    prim.second+=count;
85    _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
86}
87
88
89void Statistics::begin(GLenum mode)
90{
91    _currentPrimitiveFunctorMode=mode;
92    PrimitivePair& prim = _primitiveCount[mode];
93    ++prim.first;
94    _number_of_vertexes = 0;
95}
96
97void Statistics::end()
98{
99   _primitives_count[_currentPrimitiveFunctorMode] +=
100    _calculate_primitives_number_by_mode(_currentPrimitiveFunctorMode, _number_of_vertexes);
101
102   _vertexCount += _number_of_vertexes;
103}
104
105void Statistics::add(const Statistics& stats)
106{
107    numDrawables += stats.numDrawables;
108    nummat += stats.nummat;
109    depth += stats.depth;
110    nlights += stats.nlights;
111    nbins += stats.nbins;
112    nimpostor += stats.nimpostor;
113
114    _vertexCount += stats._vertexCount;
115    // _primitiveCount += stats._primitiveCount;   
116    for(PrimitiveValueMap::const_iterator pitr = stats._primitiveCount.begin();
117        pitr != stats._primitiveCount.end();
118        ++pitr)
119    {
120        _primitiveCount[pitr->first].first += pitr->second.first;
121        _primitiveCount[pitr->first].second += pitr->second.second;
122    }
123
124    _currentPrimitiveFunctorMode += stats._currentPrimitiveFunctorMode;
125
126    for(PrimitiveCountMap::const_iterator citr = stats._primitives_count.begin();
127        citr != stats._primitives_count.end();
128        ++citr)
129    {
130        _primitives_count[citr->first] += citr->second;
131    }
132
133    _total_primitives_count += stats._total_primitives_count;
134    _number_of_vertexes += stats._number_of_vertexes;
135}
136
137StatsVisitor::StatsVisitor():
138    osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
139    _numInstancedGroup(0),
140    _numInstancedSwitch(0),
141    _numInstancedLOD(0),
142    _numInstancedTransform(0),
143    _numInstancedGeode(0),
144    _numInstancedDrawable(0),
145    _numInstancedGeometry(0),
146    _numInstancedStateSet(0)
147{}
148
149void StatsVisitor::reset()
150{
151    _numInstancedGroup = 0;
152    _numInstancedSwitch = 0;
153    _numInstancedLOD = 0;
154    _numInstancedTransform = 0;
155    _numInstancedGeode = 0;
156    _numInstancedDrawable = 0;
157    _numInstancedGeometry = 0;
158    _numInstancedStateSet = 0;
159
160    _groupSet.clear();
161    _transformSet.clear();
162    _lodSet.clear();
163    _switchSet.clear();
164    _geodeSet.clear();
165    _drawableSet.clear();
166    _geometrySet.clear();
167    _statesetSet.clear();
168
169    _uniqueStats.reset();
170    _instancedStats.reset();
171}   
172
173void StatsVisitor::apply(osg::Node& node)
174{
175    if (node.getStateSet())
176    {
177        ++_numInstancedStateSet;
178        _statesetSet.insert(node.getStateSet());
179    }
180    traverse(node);
181}
182
183void StatsVisitor::apply(osg::Group& node)
184{
185    if (node.getStateSet())
186    {
187        ++_numInstancedStateSet;
188        _statesetSet.insert(node.getStateSet());
189    }
190
191    ++_numInstancedGroup;
192    _groupSet.insert(&node);
193    traverse(node);
194}
195
196void StatsVisitor::apply(osg::Transform& node)
197{
198    if (node.getStateSet())
199    {
200        ++_numInstancedStateSet;
201        _statesetSet.insert(node.getStateSet());
202    }
203
204    ++_numInstancedTransform;
205    _transformSet.insert(&node);
206    traverse(node);
207}
208
209void StatsVisitor::apply(osg::LOD& node)
210{
211    if (node.getStateSet())
212    {
213        ++_numInstancedStateSet;
214        _statesetSet.insert(node.getStateSet());
215    }
216
217    ++_numInstancedLOD;
218    _lodSet.insert(&node);
219    traverse(node);
220}
221
222void StatsVisitor::apply(osg::Switch& node)
223{
224    if (node.getStateSet())
225    {
226        ++_numInstancedStateSet;
227        _statesetSet.insert(node.getStateSet());
228    }
229
230    ++_numInstancedSwitch;
231    _switchSet.insert(&node);
232    traverse(node);
233}
234
235void StatsVisitor::apply(osg::Geode& node)
236{
237    if (node.getStateSet())
238    {
239        ++_numInstancedStateSet;
240        _statesetSet.insert(node.getStateSet());
241    }
242
243    ++_numInstancedGeode;
244    _geodeSet.insert(&node);
245
246    for(unsigned int i=0; i<node.getNumDrawables();++i)
247    {
248        apply(*node.getDrawable(i));
249    }
250
251    traverse(node);
252}
253
254void StatsVisitor::apply(osg::Drawable& drawable)
255{
256    if (drawable.getStateSet())
257    {
258        ++_numInstancedStateSet;
259        _statesetSet.insert(drawable.getStateSet());
260    }
261
262    ++_numInstancedDrawable;
263
264    drawable.accept(_instancedStats);
265
266    _drawableSet.insert(&drawable);
267
268    osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(&drawable);
269    if (geometry)
270    {
271        ++_numInstancedGeometry;
272        _geometrySet.insert(geometry);
273    }
274}
275
276void StatsVisitor::totalUpStats()
277{
278    _uniqueStats.reset();
279
280    for(DrawableSet::iterator itr = _drawableSet.begin();
281        itr != _drawableSet.end();
282        ++itr)
283    {
284        (*itr)->accept(_uniqueStats);
285    }
286}
287
288void StatsVisitor::print(std::ostream& out)
289{
290
291    unsigned int unique_primitives = 0;
292    osgUtil::Statistics::PrimitiveCountMap::iterator pcmitr;
293    for(pcmitr = _uniqueStats.GetPrimitivesBegin();
294        pcmitr != _uniqueStats.GetPrimitivesEnd();
295        ++pcmitr)
296    {
297        unique_primitives += pcmitr->second;
298    }
299
300    unsigned int instanced_primitives = 0;
301    for(pcmitr = _instancedStats.GetPrimitivesBegin();
302        pcmitr != _instancedStats.GetPrimitivesEnd();
303        ++pcmitr)
304    {
305        instanced_primitives += pcmitr->second;
306    }
307
308    out<<"Object Type\t#Unique\t#Instanced"<<std::endl;
309    out<<"StateSet      \t"<<_statesetSet.size()<<"\t"<<_numInstancedStateSet<<std::endl;
310    out<<"Group      \t"<<_groupSet.size()<<"\t"<<_numInstancedGroup<<std::endl;
311    out<<"Transform  \t"<<_transformSet.size()<<"\t"<<_numInstancedTransform<<std::endl;
312    out<<"LOD        \t"<<_lodSet.size()<<"\t"<<_numInstancedLOD<<std::endl;
313    out<<"Switch     \t"<<_switchSet.size()<<"\t"<<_numInstancedSwitch<<std::endl;
314    out<<"Geode      \t"<<_geodeSet.size()<<"\t"<<_numInstancedGeode<<std::endl;
315    out<<"Drawable   \t"<<_drawableSet.size()<<"\t"<<_numInstancedDrawable<<std::endl;
316    out<<"Geometry   \t"<<_geometrySet.size()<<"\t"<<_numInstancedGeometry<<std::endl;
317    out<<"Vertices   \t"<<_uniqueStats._vertexCount<<"\t"<<_instancedStats._vertexCount<<std::endl;
318    out<<"Primitives \t"<<unique_primitives<<"\t"<<instanced_primitives<<std::endl;
319}
320   
Note: See TracBrowser for help on using the browser.