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

Revision 9886, 9.3 kB (checked in by robert, 6 years ago)

From Roland Smeenk, "While working on the Collada plugin I noticed that all geometry created by the dae reader result in slow path geometry.
Because there already exists the option to convert slow path geometry to the fast path by computing an internal fast path alternative, I added a new optimizer option that automatically does this. To check the results I also made some changes to the statistics gathering and rendering.

Somewhat unrelated, but also part of the optimizer I disabled removal of CameraView? nodes during RemoveRedundantNodes? optimization.
As discussed on the ML, CameraViews? were removed from the scenegraph. This solves that issue.

Summary:
-Geometry::areFastPathsUsed now also looks at internalOptimizedGeometry
-Added Optimize option to make all slow path geometry compute their internal fast path alternative
-Added fast geometry counter to the statistics
-Disabled removel of CameraViews? in optimizer
"

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