Added new osg::Stats class for collecting frame stats of different sorts.

Added s/getStats() to osg::View and osg::Camera.
Added population of View::getStats() with frame stats in osgViewer/Viewer.
Added Basic StatsHandler to osgviewer example.
This commit is contained in:
Robert Osfield 2007-01-19 19:53:23 +00:00
parent a90206bcbb
commit f0e2404541
8 changed files with 351 additions and 16 deletions

View File

@ -524,6 +524,10 @@ SOURCE=..\..\src\osg\StateSet.cpp
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\src\osg\Stats.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\Stencil.cpp SOURCE=..\..\src\osg\Stencil.cpp
# End Source File # End Source File
# Begin Source File # Begin Source File
@ -1040,6 +1044,10 @@ SOURCE=..\..\Include\Osg\StateSet
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\Include\Osg\Stats
# End Source File
# Begin Source File
SOURCE=..\..\Include\Osg\Stencil SOURCE=..\..\Include\Osg\Stencil
# End Source File # End Source File
# Begin Source File # Begin Source File

View File

@ -200,6 +200,39 @@ public:
bool _done; bool _done;
}; };
class StatsHandler : public osgGA::GUIEventHandler
{
public:
StatsHandler() {}
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (!viewer) return false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYUP):
{
if (ea.getKey()=='s')
{
if (viewer->getStats())
{
viewer->getStats()->report(osg::notify(osg::NOTICE));
}
return true;
}
}
default: break;
}
return false;
}
bool _done;
};
int main_osgViewer(osg::ArgumentParser& arguments) int main_osgViewer(osg::ArgumentParser& arguments)
{ {
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
@ -273,9 +306,10 @@ int main_osgViewer(osg::ArgumentParser& arguments)
} }
// add the thread model handler // add the thread model handler
{ viewer.addEventHandler(new ThreadingHandler);
viewer.addEventHandler(new ThreadingHandler);
} // add the stats handler
viewer.addEventHandler(new StatsHandler);
// load the data // load the data
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);

View File

@ -21,6 +21,7 @@
#include <osg/Texture> #include <osg/Texture>
#include <osg/Image> #include <osg/Image>
#include <osg/GraphicsContext> #include <osg/GraphicsContext>
#include <osg/Stats>
#include <OpenThreads/Mutex> #include <OpenThreads/Mutex>
@ -53,6 +54,16 @@ class OSG_EXPORT Camera : public Transform, public CullSettings
const View* getView() const { return _view; } const View* getView() const { return _view; }
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
void setStats(osg::Stats* stats) { _stats = stats; }
/** Get the Stats object.*/
osg::Stats* getStats() { return _stats.get(); }
/** Get the const Stats object.*/
const osg::Stats* getStats() const { return _stats.get(); }
/** Sets the clear color. */ /** Sets the clear color. */
inline void setClearColor(const Vec4& color) { _clearColor = color; } inline void setClearColor(const Vec4& color) { _clearColor = color; }
@ -80,8 +91,6 @@ class OSG_EXPORT Camera : public Transform, public CullSettings
ColorMask* getColorMask() { return _colorMask.get(); } ColorMask* getColorMask() { return _colorMask.get(); }
/** Set the viewport of the camera to use specified osg::Viewport. */ /** Set the viewport of the camera to use specified osg::Viewport. */
void setViewport(osg::Viewport* viewport); void setViewport(osg::Viewport* viewport);
@ -377,6 +386,8 @@ class OSG_EXPORT Camera : public Transform, public CullSettings
View* _view; View* _view;
osg::ref_ptr<osg::Stats> _stats;
Vec4 _clearColor; Vec4 _clearColor;
GLbitfield _clearMask; GLbitfield _clearMask;
ref_ptr<ColorMask> _colorMask; ref_ptr<ColorMask> _colorMask;

80
include/osg/Stats Normal file
View File

@ -0,0 +1,80 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_STATS
#define OSG_STATS 1
#include <osg/Referenced>
#include <string>
#include <map>
#include <vector>
#include <ostream>
namespace osg {
class Stats : public osg::Referenced
{
public:
Stats(const std::string& name, unsigned int numberOfFrames=100);
void setName(const std::string& name) { _name = name; }
const std::string& getName() const { return _name; }
void allocate(unsigned int numberOfFrames);
int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - _attributeMapList.size() + 1; }
int getLatestFrameNumber() const { return _latestFrameNumber; }
typedef std::map<std::string, double> AttributeMap;
typedef std::vector<AttributeMap> AttributeMapList;
bool setAttribute(int frameNumber, const std::string& attributeName, double value);
bool getAttribute(int frameNumber, const std::string& attributeName, double& value) const;
AttributeMap& getAttributeMap(int frameNumber);
const AttributeMap& getAttributeMap(int frameNumber) const;
void report(std::ostream& out);
protected:
virtual ~Stats() {}
int getIndex(int frameNumber) const
{
// reject frame that are in the future
if (frameNumber > _latestFrameNumber) return -1;
// reject frames that are too early
if (frameNumber < getEarliestFrameNumber()) return -1;
if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
else return _attributeMapList.size() - (_baseFrameNumber-frameNumber);
}
std::string _name;
int _baseFrameNumber;
int _latestFrameNumber;
AttributeMapList _attributeMapList;
AttributeMap _invalidAttributeMap;
};
}
#endif

View File

@ -31,6 +31,17 @@ class OSG_EXPORT View : public virtual osg::Referenced
View(); View();
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
void setStats(osg::Stats* stats) { _stats = stats; }
/** Get the Stats object.*/
osg::Stats* getStats() { return _stats.get(); }
/** Get the const Stats object.*/
const osg::Stats* getStats() const { return _stats.get(); }
/** Set the master camera of the view. */ /** Set the master camera of the view. */
void setCamera(osg::Camera* camera) { _camera = camera; } void setCamera(osg::Camera* camera) { _camera = camera; }
@ -84,6 +95,8 @@ class OSG_EXPORT View : public virtual osg::Referenced
virtual ~View(); virtual ~View();
osg::ref_ptr<osg::Stats> _stats;
osg::ref_ptr<osg::Camera> _camera; osg::ref_ptr<osg::Camera> _camera;
typedef std::vector<Slave> Slaves; typedef std::vector<Slave> Slaves;

View File

@ -92,6 +92,7 @@ CXXFILES =\
Shape.cpp\ Shape.cpp\
ShapeDrawable.cpp\ ShapeDrawable.cpp\
State.cpp\ State.cpp\
Stats.cpp\
StateAttribute.cpp\ StateAttribute.cpp\
StateSet.cpp\ StateSet.cpp\
Stencil.cpp\ Stencil.cpp\

114
src/osg/Stats.cpp Normal file
View File

@ -0,0 +1,114 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Stats>
#include <osg/Notify>
using namespace osg;
Stats::Stats(const std::string& name, unsigned int numberOfFrames):
_name(name)
{
allocate(numberOfFrames);
}
void Stats::allocate(unsigned int numberOfFrames)
{
_baseFrameNumber = 0;
_latestFrameNumber = 0;
_attributeMapList.clear();
_attributeMapList.resize(numberOfFrames);
}
bool Stats::setAttribute(int frameNumber, const std::string& attributeName, double value)
{
if (frameNumber<getEarliestFrameNumber()) return false;
if (frameNumber>_latestFrameNumber)
{
// need to advance
// first clear the entries up to and including the new frameNumber
for(int i = _latestFrameNumber+1; i<= frameNumber; ++i)
{
int index = (i - _baseFrameNumber) % _attributeMapList.size();
_attributeMapList[index].clear();
}
if ( (frameNumber-_baseFrameNumber) >= static_cast<int>(_attributeMapList.size()))
{
_baseFrameNumber = (frameNumber/_attributeMapList.size())*_attributeMapList.size();
}
_latestFrameNumber = frameNumber;
}
int index = getIndex(frameNumber);
if (index<0)
{
osg::notify(osg::NOTICE)<<"Failed to assing valid index for Stats::setAttribute("<<frameNumber<<","<<attributeName<<","<<value<<")"<<std::endl;
return false;
}
AttributeMap& attributeMap = _attributeMapList[index];
attributeMap[attributeName] = value;
return true;
}
bool Stats::getAttribute(int frameNumber, const std::string& attributeName, double& value) const
{
int index = getIndex(frameNumber);
if (index<0) return false;
const AttributeMap& attributeMap = _attributeMapList[index];
AttributeMap::const_iterator itr = attributeMap.find(attributeName);
if (itr != attributeMap.end()) return false;
value = itr->second;
return true;
}
Stats::AttributeMap& Stats::getAttributeMap(int frameNumber)
{
int index = getIndex(frameNumber);
if (index<0) return _invalidAttributeMap;
return _attributeMapList[index];
}
const Stats::AttributeMap& Stats::getAttributeMap(int frameNumber) const
{
int index = getIndex(frameNumber);
if (index<0) return _invalidAttributeMap;
return _attributeMapList[index];
}
void Stats::report(std::ostream& out)
{
out<<"Stats "<<_name<<std::endl;
for(int i = getEarliestFrameNumber(); i<= getLatestFrameNumber(); ++i)
{
out<<" FrameNumber "<<i<<std::endl;
osg::Stats::AttributeMap& attributes = getAttributeMap(i);
for(osg::Stats::AttributeMap::iterator itr = attributes.begin();
itr != attributes.end();
++itr)
{
out<<" "<<itr->first<<"\t"<<itr->second<<std::endl;
}
}
}

View File

@ -6,10 +6,7 @@
* included with this distribution, and on the openscenegraph.org website. * included with this distribution, and on the openscenegraph.org website.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY * but WITHOUT ANY WARRANTY; without even the implied warranty of
{
}
without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details. * OpenSceneGraph Public License for more details.
*/ */
@ -38,6 +35,8 @@ Viewer::Viewer():
_eventVisitor = new osgGA::EventVisitor; _eventVisitor = new osgGA::EventVisitor;
_eventVisitor->setActionAdapter(this); _eventVisitor->setActionAdapter(this);
setStats(new osg::Stats("Viewer"));
} }
Viewer::~Viewer() Viewer::~Viewer()
@ -460,15 +459,72 @@ struct ViewerRenderingOperation : public osg::GraphicsOperation
// osg::notify(osg::NOTICE)<<"RenderingOperation"<<std::endl; // osg::notify(osg::NOTICE)<<"RenderingOperation"<<std::endl;
_sceneView->cull(); _sceneView->cull();
_sceneView->draw();
double availableTime = 0.004; // 4 ms
if (_databasePager.valid()) #if 0
osg::State* state = _sceneView->getState();
osg::Stats* stats = _sceneView->getCamera()->getStats();
const osg::Drawable::Extensions* extensions = stats ? osg::Drawable::getExtensions(state->getContextID(),true) : 0;
bool aquireGPUStats = extensions && extensions->isTimerQuerySupported();
if (aquireGPUStats)
{ {
_databasePager->compileGLObjects(*(_sceneView->getState()), availableTime); const osg::Drawable::Extensions* extensions = osg::Drawable::getExtensions(state->getContextID(),true);
bool aquireGPUStats = extensions->isTimerQuerySupported() && stats;
GLuint queries[2];
GLint available = 0;
// Create a query object.
extensions->glGenQueries(2, queries);
extensions->glBeginQuery(GL_TIME_ELAPSED, queries[0]);
_sceneView->draw();
extensions->glEndQuery(GL_TIME_ELAPSED);
extensions->glBeginQuery(GL_TIME_ELAPSED, queries[1]);
double availableTime = 0.004; // 4 ms
if (_databasePager.valid())
{
_databasePager->compileGLObjects(*(_sceneView->getState()), availableTime);
}
_sceneView->flushDeletedGLObjects(availableTime);
extensions->glEndQuery(GL_TIME_ELAPSED);
// Wait for all results to become available
while (!available) {
extensions->glGetQueryObjectiv(queries[1], GL_QUERY_RESULT_AVAILABLE, &available);
}
GLuint64EXT timeElapsed = 0;
extensions->glGetQueryObjectui64v(queries[0], GL_QUERY_RESULT, &timeElapsed);
stats->setAttribute(state->getFrameStamp()->getFrameNumber(), "GPU draw elapsed time", double(timeElapsed)*1e-9);
extensions->glGetQueryObjectui64v(queries[1], GL_QUERY_RESULT, &timeElapsed);
stats->setAttribute(state->getFrameStamp()->getFrameNumber(), "GPU compile & flush elapsed time", double(timeElapsed)*1e-9);
} }
_sceneView->flushDeletedGLObjects(availableTime); else
#endif
{
_sceneView->draw();
double availableTime = 0.004; // 4 ms
if (_databasePager.valid())
{
_databasePager->compileGLObjects(*(_sceneView->getState()), availableTime);
}
_sceneView->flushDeletedGLObjects(availableTime);
}
} }
osg::observer_ptr<osgUtil::SceneView> _sceneView; osg::observer_ptr<osgUtil::SceneView> _sceneView;
@ -498,6 +554,8 @@ void Viewer::setUpRenderingSupport()
if (_camera.valid() && _camera->getGraphicsContext()) if (_camera.valid() && _camera->getGraphicsContext())
{ {
_camera->setStats(new osg::Stats("Viewer"));
osgUtil::SceneView* sceneView = new osgUtil::SceneView; osgUtil::SceneView* sceneView = new osgUtil::SceneView;
_cameraSceneViewMap[_camera] = sceneView; _cameraSceneViewMap[_camera] = sceneView;
@ -519,6 +577,8 @@ void Viewer::setUpRenderingSupport()
Slave& slave = getSlave(i); Slave& slave = getSlave(i);
if (slave._camera.valid() && slave._camera->getGraphicsContext()) if (slave._camera.valid() && slave._camera->getGraphicsContext())
{ {
_camera->setStats(new osg::Stats("Slave Camera"));
osgUtil::SceneView* sceneView = new osgUtil::SceneView; osgUtil::SceneView* sceneView = new osgUtil::SceneView;
_cameraSceneViewMap[slave._camera] = sceneView; _cameraSceneViewMap[slave._camera] = sceneView;
@ -626,8 +686,22 @@ void Viewer::advance()
{ {
if (_done) return; if (_done) return;
double prevousReferenceTime = _frameStamp->getReferenceTime();
int previousFrameNumber = _frameStamp->getFrameNumber();
_frameStamp->setReferenceTime( osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()) ); _frameStamp->setReferenceTime( osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()) );
_frameStamp->setFrameNumber(_frameStamp->getFrameNumber()+1); _frameStamp->setFrameNumber(_frameStamp->getFrameNumber()+1);
if (getStats())
{
// update previous frame stats
double deltaFrameTime = _frameStamp->getReferenceTime() - prevousReferenceTime;
getStats()->setAttribute(previousFrameNumber, "Frame duration", deltaFrameTime);
getStats()->setAttribute(previousFrameNumber, "Frame rate", 1.0/deltaFrameTime);
// update current frames stats
getStats()->setAttribute(_frameStamp->getFrameNumber(), "Reference time", _frameStamp->getReferenceTime());
}
} }
void Viewer::eventTraversal() void Viewer::eventTraversal()