diff --git a/applications/osgviewer/osgviewer.cpp b/applications/osgviewer/osgviewer.cpp index 71d97aac7..07aa39baa 100644 --- a/applications/osgviewer/osgviewer.cpp +++ b/applications/osgviewer/osgviewer.cpp @@ -167,7 +167,41 @@ public: { if (viewer->getStats()) { - viewer->getStats()->report(osg::notify(osg::NOTICE)); + osg::notify(osg::NOTICE)< StatsList; + StatsList statsList; + statsList.push_back(viewer->getStats()); + + osgViewer::Viewer::Contexts contexts; + viewer->getContexts(contexts); + for(osgViewer::Viewer::Contexts::iterator gcitr = contexts.begin(); + gcitr != contexts.end(); + ++gcitr) + { + osg::GraphicsContext::Cameras& cameras = (*gcitr)->getCameras(); + for(osg::GraphicsContext::Cameras::iterator itr = cameras.begin(); + itr != cameras.end(); + ++itr) + { + if ((*itr)->getStats()) + { + statsList.push_back((*itr)->getStats()); + } + } + } + + for(int i = viewer->getStats()->getEarliestFrameNumber(); i<= viewer->getStats()->getLatestFrameNumber()-1; ++i) + { + for(StatsList::iterator itr = statsList.begin(); + itr != statsList.end(); + ++itr) + { + if (itr==statsList.begin()) (*itr)->report(osg::notify(osg::NOTICE), i); + else (*itr)->report(osg::notify(osg::NOTICE), i, " "); + } + osg::notify(osg::NOTICE)<first<<"\t"<second<first<<"\t"<second<getCullVisitor()->setDatabaseRequestHandler(_databasePager.get()); } @@ -455,38 +457,65 @@ struct ViewerRenderingOperation : public osg::GraphicsOperation virtual void operator () (osg::GraphicsContext*) { if (!_sceneView) return; - // osg::notify(osg::NOTICE)<<"RenderingOperation"<cull(); - - -#if 0 - osg::State* state = _sceneView->getState(); osg::Stats* stats = _sceneView->getCamera()->getStats(); + osg::State* state = _sceneView->getState(); + const osg::FrameStamp* fs = state->getFrameStamp(); + int frameNumber = fs ? fs->getFrameNumber() : 0; + + // osg::notify(osg::NOTICE)<<"Frame number "<tick(); + + _sceneView->cull(); + + osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); + + const osg::Drawable::Extensions* extensions = stats ? osg::Drawable::getExtensions(state->getContextID(),true) : 0; bool aquireGPUStats = extensions && extensions->isTimerQuerySupported(); - if (aquireGPUStats) { - const osg::Drawable::Extensions* extensions = osg::Drawable::getExtensions(state->getContextID(),true); - bool aquireGPUStats = extensions->isTimerQuerySupported() && stats; + if (_checkForQuery) + { + // Wait for all results to become available + GLint available = 0; + extensions->glGetQueryObjectiv(_query, GL_QUERY_RESULT_AVAILABLE, &available); - GLuint queries[2]; - GLint available = 0; + if (!available) + { + osg::notify(osg::INFO)<<"Stats not available - forcing a glFinish"<glGetQueryObjectiv(_query, GL_QUERY_RESULT_AVAILABLE, &available); + } - // Create a query object. - extensions->glGenQueries(2, queries); + if (available) + { + GLuint64EXT timeElapsed = 0; + extensions->glGetQueryObjectui64v(_query, GL_QUERY_RESULT, &timeElapsed); + stats->setAttribute(frameNumber-1, "GPU draw time taken", double(timeElapsed)*1e-9); + } + else + { + osg::notify(osg::INFO)<<"Stats not available - query has been lost."<glGenQueries(1, &_query); + } + } + else + { + // Create a query object. + extensions->glGenQueries(1, &_query); + } - extensions->glBeginQuery(GL_TIME_ELAPSED, queries[0]); + extensions->glBeginQuery(GL_TIME_ELAPSED, _query); _sceneView->draw(); - extensions->glEndQuery(GL_TIME_ELAPSED); - extensions->glBeginQuery(GL_TIME_ELAPSED, queries[1]); - double availableTime = 0.004; // 4 ms if (_databasePager.valid()) { @@ -495,40 +524,42 @@ struct ViewerRenderingOperation : public osg::GraphicsOperation _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); - + + _checkForQuery = true; } else -#endif - { _sceneView->draw(); double availableTime = 0.004; // 4 ms if (_databasePager.valid()) { - _databasePager->compileGLObjects(*(_sceneView->getState()), availableTime); + _databasePager->compileGLObjects(*(state), availableTime); } _sceneView->flushDeletedGLObjects(availableTime); } + osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); + if (stats) + { + stats->setAttribute(frameNumber, "Cull traversal start time", osg::Timer::instance()->delta_s(_startTick, beforeCullTick)); + stats->setAttribute(frameNumber, "Cull traversal end time", osg::Timer::instance()->delta_s(_startTick, afterCullTick)); + stats->setAttribute(frameNumber, "Cull traversal time taken", osg::Timer::instance()->delta_s(beforeCullTick, afterCullTick)); + + stats->setAttribute(frameNumber, "Draw traversal start time", osg::Timer::instance()->delta_s(_startTick, afterCullTick)); + stats->setAttribute(frameNumber, "Draw traversal end time", osg::Timer::instance()->delta_s(_startTick, afterDrawTick)); + stats->setAttribute(frameNumber, "Draw traversal time taken", osg::Timer::instance()->delta_s(afterCullTick, afterDrawTick)); + } } - osg::observer_ptr _sceneView; - osg::observer_ptr _databasePager; + osg::observer_ptr _sceneView; + osg::observer_ptr _databasePager; + + osg::Timer_t _startTick; + bool _checkForQuery; + GLuint _query; }; void Viewer::setUpRenderingSupport() @@ -573,7 +604,7 @@ void Viewer::setUpRenderingSupport() if (dp) dp->setCompileGLObjectsForContextID(state->getContextID(), true); - gc->add(new ViewerRenderingOperation(sceneView, dp)); + gc->add(new ViewerRenderingOperation(sceneView, dp, _startTick)); } }