From c346f5b94355cc7777cda6c5ac21025211a0870f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 2 Aug 2007 11:02:47 +0000 Subject: [PATCH] Introduce new osgViewer::Renderer class for implementing of the rendering of cameras in viewers --- include/osg/Camera | 19 +- include/osg/GraphicsThread | 11 + include/osg/State | 3 + include/osg/View | 3 + include/osgViewer/CompositeViewer | 7 - include/osgViewer/Renderer | 103 ++ include/osgViewer/View | 11 +- include/osgViewer/Viewer | 6 - src/osg/Camera.cpp | 8 +- src/osg/GraphicsContext.cpp | 8 + src/osg/GraphicsThread.cpp | 6 + src/osg/View.cpp | 17 +- src/osgViewer/CMakeLists.txt | 4 +- src/osgViewer/CompositeViewer.cpp | 200 +--- src/osgViewer/HelpHandler.cpp | 20 +- src/osgViewer/Renderer.cpp | 573 +++++++++++ src/osgViewer/StatsHandler.cpp | 19 +- src/osgViewer/View.cpp | 12 + src/osgViewer/Viewer.cpp | 913 +----------------- src/osgWrappers/osg/Camera.cpp | 37 +- src/osgWrappers/osg/GraphicsThread.cpp | 8 + src/osgWrappers/osg/State.cpp | 9 +- src/osgWrappers/osg/View.cpp | 6 + src/osgWrappers/osgViewer/CompositeViewer.cpp | 5 - src/osgWrappers/osgViewer/Renderer.cpp | 152 +++ src/osgWrappers/osgViewer/View.cpp | 12 +- src/osgWrappers/osgViewer/Viewer.cpp | 5 - 27 files changed, 1040 insertions(+), 1137 deletions(-) create mode 100644 include/osgViewer/Renderer create mode 100644 src/osgViewer/Renderer.cpp create mode 100644 src/osgWrappers/osgViewer/Renderer.cpp diff --git a/include/osg/Camera b/include/osg/Camera index 48cd47bd6..a2e1d082e 100644 --- a/include/osg/Camera +++ b/include/osg/Camera @@ -373,13 +373,23 @@ class OSG_EXPORT Camera : public Transform, public CullSettings /** Set the Rendering object that is used to implement rendering of the subgraph.*/ - void setRenderer(osg::Object* rc) { _renderer = rc; } + void setRenderer(osg::GraphicsOperation* rc) { _renderer = rc; } /** Get the Rendering object that is used to implement rendering of the subgraph.*/ - osg::Object* getRenderer() { return _renderer.get(); } + osg::GraphicsOperation* getRenderer() { return _renderer.get(); } /** Get the const Rendering object that is used to implement rendering of the subgraph.*/ - const osg::Object* getRenderer() const { return _renderer.get(); } + const osg::GraphicsOperation* getRenderer() const { return _renderer.get(); } + + + /** Set the Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ + void setRenderingCache(osg::Object* rc) { _renderingCache = rc; } + + /** Get the Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ + osg::Object* getRenderingCache() { return _renderingCache.get(); } + + /** Get the const Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ + const osg::Object* getRenderingCache() const { return _renderingCache.get(); } /** Draw callback for custom operations.*/ @@ -471,7 +481,8 @@ class OSG_EXPORT Camera : public Transform, public CullSettings ref_ptr _graphicsContext; - ref_ptr _renderer; + ref_ptr _renderer; + ref_ptr _renderingCache; ref_ptr _preDrawCallback; ref_ptr _postDrawCallback; diff --git a/include/osg/GraphicsThread b/include/osg/GraphicsThread index d202abf6a..c11a7e30f 100644 --- a/include/osg/GraphicsThread +++ b/include/osg/GraphicsThread @@ -106,6 +106,17 @@ struct OSG_EXPORT FlushDeletedGLObjectsOperation : public GraphicsOperation double _availableTime; }; +class OSG_EXPORT RunOperations : public osg::GraphicsOperation +{ +public: + + RunOperations(): + osg::GraphicsOperation("RunOperation",true) {} + + virtual void operator () (osg::GraphicsContext* context); + +}; + } #endif diff --git a/include/osg/State b/include/osg/State index d25ff91f5..2d7f63c37 100644 --- a/include/osg/State +++ b/include/osg/State @@ -957,6 +957,9 @@ class OSG_EXPORT State : public Referenced inline void setFrameStamp(FrameStamp* fs) { _frameStamp = fs; } /** Get the frame stamp for the current frame.*/ + inline FrameStamp* getFrameStamp() { return _frameStamp.get(); } + + /** Get the const frame stamp for the current frame.*/ inline const FrameStamp* getFrameStamp() const { return _frameStamp.get(); } diff --git a/include/osg/View b/include/osg/View index 797716410..b2f102c65 100644 --- a/include/osg/View +++ b/include/osg/View @@ -127,10 +127,13 @@ class OSG_EXPORT View : public osg::Object void updateSlave(unsigned int i); + protected : virtual ~View(); + virtual osg::GraphicsOperation* createRenderer(osg::Camera* camera) {} + osg::ref_ptr _stats; LightingMode _lightingMode; diff --git a/include/osgViewer/CompositeViewer b/include/osgViewer/CompositeViewer index f88242251..3ab689a1f 100644 --- a/include/osgViewer/CompositeViewer +++ b/include/osgViewer/CompositeViewer @@ -158,10 +158,6 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Referenced /** Start any threads required by the viewer, as per viewers ThreadingModel.*/ void startThreading(); - /** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/ - void setUpRenderingSupport(); - - protected: void constructorInit(); @@ -191,9 +187,6 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Referenced unsigned int _numThreadsOnBarrier; - typedef std::map, osg::ref_ptr > CameraSceneViewMap; - CameraSceneViewMap _cameraSceneViewMap; - osg::Timer_t _startTick; osg::ref_ptr _frameStamp; diff --git a/include/osgViewer/Renderer b/include/osgViewer/Renderer new file mode 100644 index 000000000..289ec466d --- /dev/null +++ b/include/osgViewer/Renderer @@ -0,0 +1,103 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 OSGVIEWER_RENDERER +#define OSGVIEWER_RENDERER 1 + +#include +#include +#include + +namespace osgViewer { + +class OpenGLQuerySupport +{ + public: + OpenGLQuerySupport(); + + typedef std::pair QueryFrameNumberPair; + typedef std::list QueryFrameNumberList; + typedef std::vector QueryList; + + void setStartTick(osg::Timer_t startTick) { _startTick = startTick; } + osg::Timer_t getStartTick() const { return _startTick; } + + void checkQuery(osg::Stats* stats); + + GLuint createQueryObject(); + void beginQuery(int frameNumber); + inline void endQuery(); + void initialize(osg::State* state); + + protected: + + osg::Timer_t _startTick; + bool _initialized; + bool _timerQuerySupported; + const osg::Drawable::Extensions* _extensions; + QueryFrameNumberList _queryFrameNumberList; + QueryList _availableQueryObjects; + double _previousQueryTime; + +}; + +class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQuerySupport +{ + public: + + Renderer(osg::Camera* camera); + + osgUtil::SceneView* getSceneView(unsigned int i) { return _sceneView[i].get(); } + + void setDone(bool done) { _done = done; } + bool getDone() { return _done; } + + void setGraphicsThreadDoesCull(bool flag); + bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; } + + void cull(); + void draw(); + void cull_draw(); + + virtual void operator () (osg::Object* object); + + virtual void operator () (osg::GraphicsContext* context); + + virtual void release(); + + protected: + + void updateSceneView(osgUtil::SceneView* sceneView); + + virtual ~Renderer(); + + osg::observer_ptr _camera; + + bool _done; + bool _graphicsThreadDoesCull; + unsigned int _currentCull; + unsigned int _currentDraw; + + OpenThreads::Mutex _mutex[2]; + bool _lockHeld[2]; + osg::ref_ptr _sceneView[2]; + int _frameNumber[2]; + + osg::ref_ptr _flushOperation; + + +}; + +} + +#endif diff --git a/include/osgViewer/View b/include/osgViewer/View index cb3dfe925..9a2cdf96a 100644 --- a/include/osgViewer/View +++ b/include/osgViewer/View @@ -122,13 +122,13 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter void computeActiveCoordinateSystemNodePath(); - /** Set the DsplaySettings object associated with this view.*/ + /** Set the DisplaySettings object associated with this view.*/ void setDisplaySettings(osg::DisplaySettings* ds) { _displaySettings = ds; } - /** Set the DsplaySettings object associated with this view.*/ + /** Set the DisplaySettings object associated with this view.*/ osg::DisplaySettings* getDisplaySettings() { return _displaySettings.get(); } - /** Set the DsplaySettings object associated with this view.*/ + /** Set the DisplaySettings object associated with this view.*/ const osg::DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); } /** Set the FusionDistanceMode and Value. Note, is used only when working in stereo.*/ @@ -176,8 +176,6 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter virtual void requestRedraw(); virtual void requestContinuousUpdate(bool needed=true); virtual void requestWarpPointer(float x,float y); - - public: @@ -186,9 +184,10 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter protected: - virtual ~View(); + virtual osg::GraphicsOperation* createRenderer(osg::Camera* camera); + osg::ref_ptr _scene; osg::ref_ptr _eventQueue; osg::ref_ptr _cameraManipulator; diff --git a/include/osgViewer/Viewer b/include/osgViewer/Viewer index b55cb6a6c..defa2bc16 100644 --- a/include/osgViewer/Viewer +++ b/include/osgViewer/Viewer @@ -172,9 +172,6 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View /** Start any threads required by the viewer.*/ void startThreading(); - /** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/ - void setUpRenderingSupport(); - /** Get the keyboard and mouse usage of this viewer.*/ virtual void getUsage(osg::ApplicationUsage& usage) const; @@ -221,9 +218,6 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View unsigned int _numWindowsOpenAtLastSetUpThreading; - typedef std::list< osg::ref_ptr > SceneViews; - SceneViews _sceneViews; - osg::Timer_t _startTick; osg::ref_ptr _frameStamp; diff --git a/src/osg/Camera.cpp b/src/osg/Camera.cpp index 82f11c93c..a3c3be585 100644 --- a/src/osg/Camera.cpp +++ b/src/osg/Camera.cpp @@ -251,9 +251,9 @@ void Camera::detach(BufferComponent buffer) void Camera::resizeGLObjectBuffers(unsigned int maxSize) { - if (_renderer.valid()) + if (_renderingCache.valid()) { - const_cast(this)->_renderer->resizeGLObjectBuffers(maxSize); + const_cast(this)->_renderingCache->resizeGLObjectBuffers(maxSize); } Transform::resizeGLObjectBuffers(maxSize); @@ -261,9 +261,9 @@ void Camera::resizeGLObjectBuffers(unsigned int maxSize) void Camera::releaseGLObjects(osg::State* state) const { - if (_renderer.valid()) + if (_renderingCache.valid()) { - const_cast(this)->_renderer->releaseGLObjects(state); + const_cast(this)->_renderingCache->releaseGLObjects(state); } Transform::releaseGLObjects(state); diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index c31643783..cc5e63139 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -579,6 +579,14 @@ void GraphicsContext::removeAllOperations() void GraphicsContext::runOperations() { + for(osg::GraphicsContext::Cameras::iterator itr = _cameras.begin(); + itr != _cameras.end(); + ++itr) + { + osg::Camera* camera = *itr; + if (camera->getRenderer()) (*(camera->getRenderer()))(this); + } + for(OperationQueue::iterator itr = _operations.begin(); itr != _operations.end(); ) diff --git a/src/osg/GraphicsThread.cpp b/src/osg/GraphicsThread.cpp index f32e6c87d..02dad51ae 100644 --- a/src/osg/GraphicsThread.cpp +++ b/src/osg/GraphicsThread.cpp @@ -127,3 +127,9 @@ void FlushDeletedGLObjectsOperation::operator () (GraphicsContext* context) flushDeletedGLObjects(contextID, currentTime, availableTime); } + + +void RunOperations::operator () (osg::GraphicsContext* context) +{ + context->runOperations(); +} diff --git a/src/osg/View.cpp b/src/osg/View.cpp index 01b0aad16..ec15ec232 100644 --- a/src/osg/View.cpp +++ b/src/osg/View.cpp @@ -23,19 +23,15 @@ View::View() setLightingMode(HEADLIGHT); - setCamera(new osg::Camera); + _camera = new osg::Camera; + _camera->setView(this); -#if 1 double height = osg::DisplaySettings::instance()->getScreenHeight(); double width = osg::DisplaySettings::instance()->getScreenWidth(); double distance = osg::DisplaySettings::instance()->getScreenDistance(); - double vfov = osg::RadiansToDegrees(atan2(height/2.0f,distance)*2.0); _camera->setProjectionMatrixAsPerspective( vfov, width/height, 1.0f,10000.0f); -#else - _camera->setProjectionMatrixAsFrustum(-0.325, 0.325, -0.26, 0.26, 1.0f,10000.0f); -#endif _camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f)); @@ -107,7 +103,12 @@ void View::setCamera(osg::Camera* camera) _camera = camera; - if (_camera.valid()) _camera->setView(this); + if (_camera.valid()) + { + _camera->setView(this); + + _camera->setRenderer(createRenderer(camera)); + } } void View::updateSlaves() @@ -145,6 +146,8 @@ bool View::addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, co _slaves.push_back(Slave(camera, projectionOffset, viewOffset, useMastersSceneData)); updateSlave(i); + + camera->setRenderer(createRenderer(camera)); return true; } diff --git a/src/osgViewer/CMakeLists.txt b/src/osgViewer/CMakeLists.txt index 10778a006..79e8a7bbf 100644 --- a/src/osgViewer/CMakeLists.txt +++ b/src/osgViewer/CMakeLists.txt @@ -12,6 +12,7 @@ SET(LIB_PUBLIC_HEADERS ${HEADER_PATH}/CompositeViewer ${HEADER_PATH}/Export ${HEADER_PATH}/GraphicsWindow + ${HEADER_PATH}/Renderer ${HEADER_PATH}/Scene ${HEADER_PATH}/Version ${HEADER_PATH}/View @@ -21,9 +22,10 @@ SET(LIB_PUBLIC_HEADERS SET(LIB_COMMON_FILES CompositeViewer.cpp + HelpHandler.cpp + Renderer.cpp Scene.cpp StatsHandler.cpp - HelpHandler.cpp Version.cpp View.cpp Viewer.cpp diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 74ff947aa..37b143b9c 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -105,7 +106,6 @@ void CompositeViewer::addView(osgViewer::View* view) _views.push_back(view); - setUpRenderingSupport(); if (threadsWereRuinning) startThreading(); } @@ -122,7 +122,6 @@ void CompositeViewer::removeView(osgViewer::View* view) _views.erase(itr); - setUpRenderingSupport(); if (threadsWereRuinning) startThreading(); return; @@ -254,22 +253,6 @@ void CompositeViewer::stopThreading() _numThreadsOnBarrier = 0; } -// Draw operation, that does a draw on the scene graph. -struct CompositeViewerRunOperations : public osg::Operation -{ - CompositeViewerRunOperations(): - osg::Operation("RunOperation",true) - { - } - - virtual void operator () (osg::Object* object) - { - osg::GraphicsContext* context = dynamic_cast(object); - if (!context) return; - - context->runOperations(); - } -}; unsigned int CompositeViewer::computeNumberOfThreadsIncludingMainRequired() { @@ -373,7 +356,7 @@ void CompositeViewer::startThreading() gc->getGraphicsThread()->add(_startRenderingBarrier.get()); // add the rendering operation itself. - gc->getGraphicsThread()->add(new CompositeViewerRunOperations()); + gc->getGraphicsThread()->add(new osg::RunOperations()); if (_endBarrierPosition==BeforeSwapBuffers) { @@ -521,183 +504,6 @@ void CompositeViewer::getScenes(Scenes& scenes, bool onlyValid) } } - -// Draw operation, that does a draw on the scene graph. -struct CompositeViewerRenderingOperation : public osg::Operation -{ - CompositeViewerRenderingOperation(osgUtil::SceneView* sceneView, osgDB::DatabasePager* databasePager): - osg::Operation("Render",true), - _sceneView(sceneView), - _databasePager(databasePager) - { - _sceneView->getCullVisitor()->setDatabaseRequestHandler(_databasePager.get()); - - _flushOperation = new osg::FlushDeletedGLObjectsOperation(0.1); - } - - virtual void operator () (osg::Object*) - { - if (!_sceneView) return; - - - // osg::notify(osg::NOTICE)<<"RenderingOperation"<(_sceneView->getCamera()->getView()); - if (view) _sceneView->setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); - - osg::GraphicsContext* compileContext = osg::GraphicsContext::getCompileContext(_sceneView->getState()->getContextID()); - osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; - - _sceneView->inheritCullSettings(*(_sceneView->getCamera())); - _sceneView->cull(); - _sceneView->draw(); - - double availableTime = 0.004; // 4 ms - - if (_databasePager.valid() && _databasePager->requiresExternalCompileGLObjects(_sceneView->getState()->getContextID())) - { - _databasePager->compileGLObjects(*(_sceneView->getState()), availableTime); - } - - if (compileThread) - { - compileThread->add(_flushOperation.get()); - } - else - { - _sceneView->flushDeletedGLObjects(availableTime); - } - } - - osg::observer_ptr _sceneView; - osg::observer_ptr _databasePager; - osg::ref_ptr _flushOperation; -}; - -void CompositeViewer::setUpRenderingSupport() -{ -#if 1 - _cameraSceneViewMap.clear(); - - Contexts contexts; - getContexts(contexts); - - osg::FrameStamp* frameStamp = getFrameStamp(); - - for(Contexts::iterator gcitr = contexts.begin(); - gcitr != contexts.end(); - ++gcitr) - { - (*gcitr)->removeAllOperations(); - - osg::GraphicsContext* gc = *gcitr; - osg::GraphicsContext::Cameras& cameras = gc->getCameras(); - osg::State* state = gc->getState(); - - for(osg::GraphicsContext::Cameras::iterator citr = cameras.begin(); - citr != cameras.end(); - ++citr) - { - osg::Camera* camera = *citr; - osgViewer::View* view = dynamic_cast(camera->getView()); - osgViewer::Scene* scene = view ? view->getScene() : 0; - - osg::DisplaySettings* ds = view ? view->getDisplaySettings() : 0; - if (!ds) ds = osg::DisplaySettings::instance(); - - osgDB::DatabasePager* dp = scene ? scene->getDatabasePager() : 0; - - camera->setStats(new osg::Stats("Camera")); - - osgUtil::SceneView* sceneView = new osgUtil::SceneView; - _cameraSceneViewMap[camera] = sceneView; - - sceneView->setGlobalStateSet(view ? view->getCamera()->getStateSet() : 0); - sceneView->setDefaults(); - sceneView->setDisplaySettings(camera->getDisplaySettings()!=0 ? camera->getDisplaySettings() : ds); - sceneView->setCamera(camera); - sceneView->setState(state); - sceneView->setFrameStamp(frameStamp); - - if (dp) dp->setCompileGLObjectsForContextID(state->getContextID(), true); - - gc->add(new CompositeViewerRenderingOperation(sceneView, dp)); - - } - } - -#else - osg::FrameStamp* frameStamp = getFrameStamp(); - - // what should we do with the old sceneViews? - _cameraSceneViewMap.clear(); - - Contexts contexts; - getContexts(contexts); - - // clear out all the previously assigned operations - for(Contexts::iterator citr = contexts.begin(); - citr != contexts.end(); - ++citr) - { - (*citr)->removeAllOperations(); - } - - for(Views::iterator itr = _views.begin(); - itr != _views.end(); - ++itr) - { - osgViewer::View* view = itr->get(); - osg::DisplaySettings* ds = view->getDisplaySettings() ? view->getDisplaySettings() : osg::DisplaySettings::instance(); - - osgDB::DatabasePager* dp = view->getScene() ? view->getScene()->getDatabasePager() : 0; - - if (view->getCamera() && view->getCamera()->getGraphicsContext()) - { - osgUtil::SceneView* sceneView = new osgUtil::SceneView; - _cameraSceneViewMap[view->getCamera()] = sceneView; - - sceneView->setGlobalStateSet(view->getCamera()->getStateSet()); - sceneView->setDefaults(); - sceneView->setDisplaySettings(view->getCamera()->getDisplaySettings()!=0 ? view->getCamera()->getDisplaySettings() : ds); - sceneView->setCamera(view->getCamera()); - sceneView->setState(view->getCamera()->getGraphicsContext()->getState()); - sceneView->setSceneData(view->getSceneData()); - sceneView->setFrameStamp(frameStamp); - - if (dp) dp->setCompileGLObjectsForContextID(view->getCamera()->getGraphicsContext()->getState()->getContextID(), true); - - view->getCamera()->getGraphicsContext()->add(new CompositeViewerRenderingOperation(sceneView, dp)); - } - - for(unsigned i=0; igetNumSlaves(); ++i) - { - View::Slave& slave = view->getSlave(i); - if (slave._camera.valid() && slave._camera->getGraphicsContext()) - { - osgUtil::SceneView* sceneView = new osgUtil::SceneView; - _cameraSceneViewMap[slave._camera] = sceneView; - - sceneView->setGlobalStateSet(view->getCamera()->getStateSet()); - sceneView->setDefaults(); - sceneView->setCamera(slave._camera.get()); - sceneView->setDisplaySettings(slave._camera->getDisplaySettings()!=0 ? slave._camera->getDisplaySettings() : ds); - sceneView->setState(slave._camera->getGraphicsContext()->getState()); - sceneView->setSceneData(view->getSceneData()); - sceneView->setFrameStamp(frameStamp); - - if (dp) dp->setCompileGLObjectsForContextID(slave._camera->getGraphicsContext()->getState()->getContextID(), true); - - slave._camera->getGraphicsContext()->add(new CompositeViewerRenderingOperation(sceneView, dp)); - } - } - - } -#endif -} - - void CompositeViewer::realize() { //osg::notify(osg::INFO)<<"CompositeViewer::realize()"< +#include #include @@ -26,6 +27,7 @@ HelpHandler::HelpHandler(osg::ApplicationUsage* au): _initialized(false) { _camera = new osg::Camera; + _camera->setRenderer(new Renderer(_camera.get())); } @@ -76,14 +78,20 @@ bool HelpHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapt void HelpHandler::setUpHUDCamera(osgViewer::Viewer* viewer) { - osgViewer::Viewer::Windows windows; - viewer->getWindows(windows); + osgViewer::GraphicsWindow* window = dynamic_cast(_camera->getGraphicsContext()); - if (windows.empty()) return; + if (!window) + { + osgViewer::Viewer::Windows windows; + viewer->getWindows(windows); - osgViewer::GraphicsWindow* window = windows.front(); + if (windows.empty()) return; + + window = windows.front(); + + _camera->setGraphicsContext(window); + } - _camera = new osg::Camera; _camera->setGraphicsContext(window); _camera->setViewport(0, 0, window->getTraits()->width, window->getTraits()->height); @@ -94,8 +102,6 @@ void HelpHandler::setUpHUDCamera(osgViewer::Viewer* viewer) // only clear the depth buffer _camera->setClearMask(0); - viewer->setUpRenderingSupport(); - _initialized = true; } diff --git a/src/osgViewer/Renderer.cpp b/src/osgViewer/Renderer.cpp new file mode 100644 index 000000000..717dddaf4 --- /dev/null +++ b/src/osgViewer/Renderer.cpp @@ -0,0 +1,573 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 + +#include + +#include +#include + +#include +#include +#include + +#include + +#include + +#include + +using namespace osgViewer; + +//#define DEBUG_MESSAGE osg::notify(osg::NOTICE) +#define DEBUG_MESSAGE osg::notify(osg::INFO) + + +OpenGLQuerySupport::OpenGLQuerySupport(): + _startTick(0), + _initialized(false), + _timerQuerySupported(false), + _extensions(0), + _previousQueryTime(0.0) +{ +} + +void OpenGLQuerySupport::checkQuery(osg::Stats* stats) +{ + for(QueryFrameNumberList::iterator itr = _queryFrameNumberList.begin(); + itr != _queryFrameNumberList.end(); + ) + { + GLuint query = itr->first; + GLint available = 0; + _extensions->glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &available); + if (available) + { + GLuint64EXT timeElapsed = 0; + _extensions->glGetQueryObjectui64v(query, GL_QUERY_RESULT, &timeElapsed); + + double timeElapsedSeconds = double(timeElapsed)*1e-9; + double currentTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); + double estimatedEndTime = (_previousQueryTime + currentTime) * 0.5; + double estimatedBeginTime = estimatedEndTime - timeElapsedSeconds; + + stats->setAttribute(itr->second, "GPU draw begin time", estimatedBeginTime); + stats->setAttribute(itr->second, "GPU draw end time", estimatedEndTime); + stats->setAttribute(itr->second, "GPU draw time taken", timeElapsedSeconds); + + + itr = _queryFrameNumberList.erase(itr); + _availableQueryObjects.push_back(query); + } + else + { + ++itr; + } + + } + _previousQueryTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); +} + +GLuint OpenGLQuerySupport::createQueryObject() +{ + if (_availableQueryObjects.empty()) + { + GLuint query; + _extensions->glGenQueries(1, &query); + return query; + } + else + { + GLuint query = _availableQueryObjects.back(); + _availableQueryObjects.pop_back(); + return query; + } +} + +void OpenGLQuerySupport::beginQuery(int frameNumber) +{ + GLuint query = createQueryObject(); + _extensions->glBeginQuery(GL_TIME_ELAPSED, query); + _queryFrameNumberList.push_back(QueryFrameNumberPair(query, frameNumber)); +} + +void OpenGLQuerySupport::endQuery() +{ + _extensions->glEndQuery(GL_TIME_ELAPSED); +} + +void OpenGLQuerySupport::initialize(osg::State* state) +{ + if (_initialized) return; + + _initialized = true; + _extensions = osg::Drawable::getExtensions(state->getContextID(),true); + _timerQuerySupported = _extensions && _extensions->isTimerQuerySupported(); + _previousQueryTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// Renderer + +Renderer::Renderer(osg::Camera* camera): + osg::GraphicsOperation("Renderer",true), + OpenGLQuerySupport(), + _camera(camera), + _done(false), + _graphicsThreadDoesCull(true) +{ + + DEBUG_MESSAGE<<"Render::Render() "<getView() ? _camera->getView()->getCamera() : camera; + osg::StateSet* stateset = masterCamera->getOrCreateStateSet(); + osgViewer::View* view = dynamic_cast(_camera->getView()); + + osg::DisplaySettings* ds = _camera->getDisplaySettings() ? _camera->getDisplaySettings() : + ((view &&view->getDisplaySettings()) ? view->getDisplaySettings() : osg::DisplaySettings::instance()); + + _sceneView[0]->setGlobalStateSet(stateset); + _sceneView[1]->setGlobalStateSet(stateset); + + _sceneView[0]->setDefaults(sceneViewOptions); + _sceneView[1]->setDefaults(sceneViewOptions); + + _sceneView[0]->setDisplaySettings(ds); + _sceneView[1]->setDisplaySettings(ds); + + _sceneView[0]->setCamera(_camera.get()); + _sceneView[1]->setCamera(_camera.get()); + + _currentCull = 0; + _currentDraw = 0; + + // lock the mutex for the current cull SceneView to + // prevent the draw traversal from reading from it before the cull traversal has been completed. + if (!_graphicsThreadDoesCull) + { + _mutex[_currentCull].lock(); + _lockHeld[_currentCull] = true; + } + + _flushOperation = new osg::FlushDeletedGLObjectsOperation(0.1); +} + +Renderer::~Renderer() +{ + DEBUG_MESSAGE<<"Render::~Render() "<getView() ? _camera->getView()->getCamera() : _camera.get(); + osg::StateSet* stateset = masterCamera->getOrCreateStateSet(); + + if (sceneView->getGlobalStateSet()!=stateset) + { + sceneView->setGlobalStateSet(stateset); + } + + osg::GraphicsContext* context = _camera->getGraphicsContext(); + osg::State* state = context ? context->getState() : 0; + if (sceneView->getState()!=state) + { + sceneView->setState(state); + } + + osgViewer::View* view = dynamic_cast(_camera->getView()); + osgViewer::Viewer* viewer = dynamic_cast(view); + Scene* scene = view ? view->getScene() : 0; + osgDB::DatabasePager* databasePager = scene ? scene->getDatabasePager() : 0; + sceneView->getCullVisitor()->setDatabaseRequestHandler(databasePager); + + sceneView->setFrameStamp(scene ? scene->getFrameStamp() : state->getFrameStamp()); + + if (databasePager) databasePager->setCompileGLObjectsForContextID(state->getContextID(), true); + + osg::DisplaySettings* ds = _camera->getDisplaySettings() ? _camera->getDisplaySettings() : + ((view &&view->getDisplaySettings()) ? view->getDisplaySettings() : osg::DisplaySettings::instance()); + + sceneView->setDisplaySettings(ds); + + if (viewer) _startTick = viewer->getStartTick(); +} + + +void Renderer::cull() +{ + DEBUG_MESSAGE<<"cull()"<(sceneView->getCamera()->getView()); + if (view) sceneView->setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); + + osg::Stats* stats = sceneView->getCamera()->getStats(); + osg::State* state = sceneView->getState(); + const osg::FrameStamp* fs = state->getFrameStamp(); + int frameNumber = fs ? fs->getFrameNumber() : 0; + + _frameNumber[_currentCull] = frameNumber; + + // do cull taversal + osg::Timer_t beforeCullTick = osg::Timer::instance()->tick(); + + sceneView->inheritCullSettings(*(sceneView->getCamera())); + sceneView->cull(); + + osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); + +#if 0 + if (sceneView->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) + { + // osg::notify(osg::NOTICE)<<"Completed in cull"<getDynamicObjectRenderingCompletedCallback()->completed(state); + } +#endif + if (stats && stats->collectStats("rendering")) + { + DEBUG_MESSAGE<<"Collecting rendering stats"<setAttribute(frameNumber, "Cull traversal begin 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)); + } + } + + + // relase the mutex associated with this cull traversal, let the draw commence. + _lockHeld[_currentCull] = false; + _mutex[_currentCull].unlock(); + + // swap which SceneView we need to do cull traversal on next. + _currentCull = 1 - _currentCull; + + // aquire the lock for it for the new cull traversal + _mutex[_currentCull].lock(); + _lockHeld[_currentCull] = true; + + DEBUG_MESSAGE<<"end cull() "<getState()->getContextID()); + osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; + + if (sceneView || _done) + { + OpenThreads::ScopedLock lock(_mutex[_currentDraw]); + + osgViewer::View* view = dynamic_cast(_camera->getView()); + Scene* scene = view ? view->getScene() : 0; + osgDB::DatabasePager* databasePager = scene ? scene->getDatabasePager() : 0; + + // osg::notify(osg::NOTICE)<<"Drawing buffer "<<_currentDraw<getCamera()->getStats(); + osg::State* state = sceneView->getState(); + int frameNumber = _frameNumber[_currentDraw]; + + if (!_initialized) + { + initialize(state); + } + + state->setDynamicObjectCount(sceneView->getDynamicObjectCount()); + + if (sceneView->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) + { + // osg::notify(osg::NOTICE)<<"Completed in cull"<getDynamicObjectRenderingCompletedCallback()->completed(state); + } + + osg::Timer_t beforeDrawTick = osg::Timer::instance()->tick(); + + bool aquireGPUStats = stats && _timerQuerySupported && stats->collectStats("gpu"); + + if (aquireGPUStats) + { + checkQuery(stats); + } + + // do draw traveral + if (aquireGPUStats) + { + checkQuery(stats); + beginQuery(frameNumber); + } + + sceneView->draw(); + + double availableTime = 0.004; // 4 ms + if (databasePager && databasePager->requiresExternalCompileGLObjects(sceneView->getState()->getContextID())) + { + databasePager->compileGLObjects(*(sceneView->getState()), availableTime); + } + + if (compileThread) + { + compileThread->add(_flushOperation.get()); + } + else + { + sceneView->flushDeletedGLObjects(availableTime); + } + + if (aquireGPUStats) + { + endQuery(); + checkQuery(stats); + } + + glFlush(); + + + osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); + + if (stats && stats->collectStats("rendering")) + { + stats->setAttribute(frameNumber, "Draw traversal begin time", osg::Timer::instance()->delta_s(_startTick, beforeDrawTick)); + 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(beforeDrawTick, afterDrawTick)); + } + } + + _currentDraw = 1-_currentDraw; + + DEBUG_MESSAGE<<"end draw() "<(_camera->getView()); + Scene* scene = view ? view->getScene() : 0; + osgDB::DatabasePager* databasePager = scene ? scene->getDatabasePager() : 0; + + osg::GraphicsContext* compileContext = osg::GraphicsContext::getCompileContext(sceneView->getState()->getContextID()); + osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; + + OpenThreads::ScopedLock lock(_mutex[_currentDraw]); + + if (_done) + { + osg::notify(osg::INFO)<<"Render::release() causing cull_draw to exit"<setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); + + osg::Stats* stats = sceneView->getCamera()->getStats(); + osg::State* state = sceneView->getState(); + const osg::FrameStamp* fs = state->getFrameStamp(); + int frameNumber = fs ? fs->getFrameNumber() : 0; + + if (!_initialized) + { + initialize(state); + } + + bool aquireGPUStats = stats && _timerQuerySupported && stats->collectStats("gpu"); + + if (aquireGPUStats) + { + checkQuery(stats); + } + + // do cull taversal + osg::Timer_t beforeCullTick = osg::Timer::instance()->tick(); + + sceneView->inheritCullSettings(*(sceneView->getCamera())); + sceneView->cull(); + + osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); + +#if 0 + if (state->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) + { + state->getDynamicObjectRenderingCompletedCallback()->completed(state); + } +#endif + + // do draw traveral + if (aquireGPUStats) + { + checkQuery(stats); + beginQuery(frameNumber); + } + + sceneView->draw(); + + double availableTime = 0.004; // 4 ms + if (databasePager && databasePager->requiresExternalCompileGLObjects(sceneView->getState()->getContextID())) + { + databasePager->compileGLObjects(*(sceneView->getState()), availableTime); + } + + if (compileThread) + { + compileThread->add(_flushOperation.get()); + } + else + { + sceneView->flushDeletedGLObjects(availableTime); + } + + if (aquireGPUStats) + { + endQuery(); + checkQuery(stats); + } + + osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); + + if (stats && stats->collectStats("rendering")) + { + DEBUG_MESSAGE<<"Collecting rendering stats"<setAttribute(frameNumber, "Cull traversal begin 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 begin 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)); + } + + DEBUG_MESSAGE<<"end cull_draw() "<(object); + if (context) operator()(context); + + osg::Camera* camera = dynamic_cast(object); + if (camera) cull(); +} + +void Renderer::operator () (osg::GraphicsContext* context) +{ + if (_graphicsThreadDoesCull) + { + cull_draw(); + } + else + { + draw(); + } +} + +void Renderer::release() +{ + osg::notify(osg::INFO)<<"Renderer::release()"< #include +#include #include #include @@ -34,6 +35,7 @@ StatsHandler::StatsHandler(): _blockMultiplier(10000.0) { _camera = new osg::Camera; + _camera->setRenderer(new Renderer(_camera.get())); } bool StatsHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) @@ -224,7 +226,7 @@ void StatsHandler::setUpHUDCamera(osgViewer::Viewer* viewer) // only clear the depth buffer _camera->setClearMask(0); - viewer->setUpRenderingSupport(); + _camera->setRenderer(new Renderer(_camera.get())); _initialized = true; } @@ -479,13 +481,13 @@ void StatsHandler::setUpScene(osgViewer::Viewer* viewer) // collect all the relevant camers typedef std::vector Cameras; Cameras cameras; - if (viewer->getCamera()->getStats()) + if (viewer->getCamera()->getStats() && viewer->getCamera()->getGraphicsContext()) { cameras.push_back(viewer->getCamera()); } for(unsigned int si=0; sigetNumSlaves(); ++si) { - if (viewer->getSlave(si)._camera->getStats()) + if (viewer->getSlave(si)._camera->getStats() && viewer->getSlave(si)._camera->getGraphicsContext()) { cameras.push_back(viewer->getSlave(si)._camera.get()); } @@ -497,11 +499,14 @@ void StatsHandler::setUpScene(osgViewer::Viewer* viewer) citr != cameras.end(); ++citr) { - unsigned int contextID = (*citr)->getGraphicsContext()->getState()->getContextID(); - const osg::Drawable::Extensions* extensions = osg::Drawable::getExtensions(contextID, false); - if (extensions && extensions->isTimerQuerySupported()) + if ((*citr)->getGraphicsContext()) { - ++numCamrasWithTimerQuerySupport; + unsigned int contextID = (*citr)->getGraphicsContext()->getState()->getContextID(); + const osg::Drawable::Extensions* extensions = osg::Drawable::getExtensions(contextID, false); + if (extensions && extensions->isTimerQuerySupported()) + { + ++numCamrasWithTimerQuerySupport; + } } } diff --git a/src/osgViewer/View.cpp b/src/osgViewer/View.cpp index 325af1bb1..bad1ebf6d 100644 --- a/src/osgViewer/View.cpp +++ b/src/osgViewer/View.cpp @@ -11,6 +11,7 @@ * OpenSceneGraph Public License for more details. */ +#include #include #include @@ -126,6 +127,9 @@ View::View(): // make sure View is safe to reference multi-threaded. setThreadSafeRefUnref(true); + + // need to attach a Renderer to the maaster camera which has been default constructed + getCamera()->setRenderer(createRenderer(getCamera())); setEventQueue(new osgGA::EventQueue); } @@ -144,6 +148,14 @@ View::~View() // osg::notify(osg::NOTICE)<<"Destructing osgViewer::View"<setStats(new osg::Stats("Camera")); + return render; +} + + void View::init() { osg::notify(osg::INFO)<<"View::init()"< -#include #include +#include + #include #include #include #include -#include -#include +#include +#include + #include using namespace osgViewer; - -class ViewerQuerySupport -{ -public: - ViewerQuerySupport(osg::Timer_t startTick): - _startTick(startTick), - _initialized(false), - _timerQuerySupported(false), - _extensions(0), - _previousQueryTime(0.0) - { - } - - typedef std::pair QueryFrameNumberPair; - typedef std::list QueryFrameNumberList; - typedef std::vector QueryList; - - inline void checkQuery(osg::Stats* stats) - { - for(QueryFrameNumberList::iterator itr = _queryFrameNumberList.begin(); - itr != _queryFrameNumberList.end(); - ) - { - GLuint query = itr->first; - GLint available = 0; - _extensions->glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &available); - if (available) - { - GLuint64EXT timeElapsed = 0; - _extensions->glGetQueryObjectui64v(query, GL_QUERY_RESULT, &timeElapsed); - - double timeElapsedSeconds = double(timeElapsed)*1e-9; - double currentTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); - double estimatedEndTime = (_previousQueryTime + currentTime) * 0.5; - double estimatedBeginTime = estimatedEndTime - timeElapsedSeconds; - - stats->setAttribute(itr->second, "GPU draw begin time", estimatedBeginTime); - stats->setAttribute(itr->second, "GPU draw end time", estimatedEndTime); - stats->setAttribute(itr->second, "GPU draw time taken", timeElapsedSeconds); - - - itr = _queryFrameNumberList.erase(itr); - _availableQueryObjects.push_back(query); - } - else - { - ++itr; - } - - } - _previousQueryTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); - } - - inline GLuint createQueryObject() - { - if (_availableQueryObjects.empty()) - { - GLuint query; - _extensions->glGenQueries(1, &query); - return query; - } - else - { - GLuint query = _availableQueryObjects.back(); - _availableQueryObjects.pop_back(); - return query; - } - } - - inline void beginQuery(int frameNumber) - { - GLuint query = createQueryObject(); - _extensions->glBeginQuery(GL_TIME_ELAPSED, query); - _queryFrameNumberList.push_back(QueryFrameNumberPair(query, frameNumber)); - } - - inline void endQuery() - { - _extensions->glEndQuery(GL_TIME_ELAPSED); - } - - void initialize(osg::State* state) - { - if (_initialized) return; - - _initialized = true; - _extensions = osg::Drawable::getExtensions(state->getContextID(),true); - _timerQuerySupported = _extensions && _extensions->isTimerQuerySupported(); - _previousQueryTime = osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); - } - - osg::Timer_t _startTick; - bool _initialized; - bool _timerQuerySupported; - const osg::Drawable::Extensions* _extensions; - QueryFrameNumberList _queryFrameNumberList; - QueryList _availableQueryObjects; - double _previousQueryTime; - -}; - - -// Draw operation, that does a draw on the scene graph. -struct ViewerRenderingOperation : public osg::GraphicsOperation, public ViewerQuerySupport -{ - ViewerRenderingOperation(osgUtil::SceneView* sceneView, osgDB::DatabasePager* databasePager, osg::Timer_t startTick): - osg::GraphicsOperation("Render",true), - ViewerQuerySupport(startTick), - _sceneView(sceneView), - _databasePager(databasePager) - { - _sceneView->getCullVisitor()->setDatabaseRequestHandler(_databasePager.get()); - - _flushOperation = new osg::FlushDeletedGLObjectsOperation(0.1); - } - - osg::Camera* getCamera() { return _sceneView->getCamera(); } - - virtual void operator () (osg::GraphicsContext*) - { - if (!_sceneView) return; - - // osg::notify(osg::NOTICE)<<"RenderingOperation"<getCamera()->getStats(); - osg::State* state = _sceneView->getState(); - const osg::FrameStamp* fs = state->getFrameStamp(); - int frameNumber = fs ? fs->getFrameNumber() : 0; - - if (!_initialized) - { - initialize(state); - } - - bool aquireGPUStats = stats && _timerQuerySupported && stats->collectStats("gpu"); - - if (aquireGPUStats) - { - checkQuery(stats); - } - - // do cull taversal - osg::Timer_t beforeCullTick = osg::Timer::instance()->tick(); - - // pass on the fusion distance settings from the View to the SceneView - osgViewer::View* view = dynamic_cast(_sceneView->getCamera()->getView()); - if (view) _sceneView->setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); - - _sceneView->inheritCullSettings(*(_sceneView->getCamera())); - - _sceneView->cull(); - - - osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); - -#if 0 - if (_sceneView->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) - { - osg::notify(osg::NOTICE)<<"Completed in ViewerRenderingOperation"<getDynamicObjectRenderingCompletedCallback()->completed(state); - } -#endif - - state->setDynamicObjectCount(_sceneView->getDynamicObjectCount()); - - // do draw traveral - if (aquireGPUStats) - { - checkQuery(stats); - beginQuery(frameNumber); - } - - _sceneView->draw(); - - double availableTime = 0.004; // 4 ms - if (_databasePager.valid() && _databasePager->requiresExternalCompileGLObjects(_sceneView->getState()->getContextID())) - { - _databasePager->compileGLObjects(*(_sceneView->getState()), availableTime); - } - - osg::GraphicsContext* compileContext = osg::GraphicsContext::getCompileContext(_sceneView->getState()->getContextID()); - osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; - - if (compileThread) - { - compileThread->add(_flushOperation.get()); - } - else - { - _sceneView->flushDeletedGLObjects(availableTime); - } - - if (aquireGPUStats) - { - endQuery(); - checkQuery(stats); - } - - osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); - - if (stats && stats->collectStats("rendering")) - { - stats->setAttribute(frameNumber, "Cull traversal begin 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 begin 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::ref_ptr _flushOperation; - -}; - - -// Draw operation, that does a draw on the scene graph. -struct ViewerDoubleBufferedRenderingOperation : public osg::Operation, public ViewerQuerySupport -{ - ViewerDoubleBufferedRenderingOperation(bool graphicsThreadDoesCull, osgUtil::SceneView* sv0, osgUtil::SceneView* sv1, osgDB::DatabasePager* databasePager, osg::Timer_t startTick): - osg::Operation("Render",true), - ViewerQuerySupport(startTick), - _graphicsThreadDoesCull(graphicsThreadDoesCull), - _done(false), - _databasePager(databasePager) - { - _lockHeld[0] = false; - _lockHeld[1] = false; - - _sceneView[0] = sv0; - _sceneView[0]->getCullVisitor()->setDatabaseRequestHandler(_databasePager.get()); - - _sceneView[1] = sv1; - _sceneView[1]->getCullVisitor()->setDatabaseRequestHandler(_databasePager.get()); - - _currentCull = 0; - _currentDraw = 0; - - // lock the mutex for the current cull SceneView to - // prevent the draw traversal from reading from it before the cull traversal has been completed. - if (!_graphicsThreadDoesCull) - { - _mutex[_currentCull].lock(); - _lockHeld[_currentCull] = true; - } - - - _flushOperation = new osg::FlushDeletedGLObjectsOperation(0.1); - - // osg::notify(osg::NOTICE)<<"constructed"<getCamera(); } - - void setGraphicsThreadDoesCull(bool flag) - { - if (_graphicsThreadDoesCull==flag) return; - - _graphicsThreadDoesCull = flag; - - _currentCull = 0; - _currentDraw = 0; - - if (_graphicsThreadDoesCull) - { - // need to disable any locks held by the cull - if (_lockHeld[0]) - { - _lockHeld[0] = false; - _mutex[0].unlock(); - } - - if (_lockHeld[1]) - { - _lockHeld[1] = false; - _mutex[1].unlock(); - } - } - else - { - // need to set a lock for cull - _mutex[_currentCull].lock(); - _lockHeld[_currentCull] = true; - } - } - - bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; } - - void cull() - { - // osg::notify(osg::NOTICE)<<"cull()"<(sceneView->getCamera()->getView()); - if (view) sceneView->setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); - - osg::Stats* stats = sceneView->getCamera()->getStats(); - osg::State* state = sceneView->getState(); - const osg::FrameStamp* fs = state->getFrameStamp(); - int frameNumber = fs ? fs->getFrameNumber() : 0; - - _frameNumber[_currentCull] = frameNumber; - - // do cull taversal - osg::Timer_t beforeCullTick = osg::Timer::instance()->tick(); - - sceneView->inheritCullSettings(*(sceneView->getCamera())); - sceneView->cull(); - - osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); - -#if 0 - if (sceneView->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) - { - // osg::notify(osg::NOTICE)<<"Completed in cull"<getDynamicObjectRenderingCompletedCallback()->completed(state); - } -#endif - if (stats && stats->collectStats("rendering")) - { - stats->setAttribute(frameNumber, "Cull traversal begin 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)); - } - } - - - // relase the mutex associated with this cull traversal, let the draw commence. - _lockHeld[_currentCull] = false; - _mutex[_currentCull].unlock(); - - // swap which SceneView we need to do cull traversal on next. - _currentCull = 1 - _currentCull; - - // aquire the lock for it for the new cull traversal - _mutex[_currentCull].lock(); - _lockHeld[_currentCull] = true; - } - - void draw() - { - // osg::notify(osg::NOTICE)<<"draw()"<getState()->getContextID()); - osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; - - if (sceneView || _done) - { - OpenThreads::ScopedLock lock(_mutex[_currentDraw]); - - // osg::notify(osg::NOTICE)<<"Drawing buffer "<<_currentDraw<getCamera()->getStats(); - osg::State* state = sceneView->getState(); - int frameNumber = _frameNumber[_currentDraw]; - - if (!_initialized) - { - initialize(state); - } - - state->setDynamicObjectCount(sceneView->getDynamicObjectCount()); - - if (sceneView->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) - { - // osg::notify(osg::NOTICE)<<"Completed in cull"<getDynamicObjectRenderingCompletedCallback()->completed(state); - } - - osg::Timer_t beforeDrawTick = osg::Timer::instance()->tick(); - - bool aquireGPUStats = stats && _timerQuerySupported && stats->collectStats("gpu"); - - if (aquireGPUStats) - { - checkQuery(stats); - } - - // do draw traveral - if (aquireGPUStats) - { - checkQuery(stats); - beginQuery(frameNumber); - } - - sceneView->draw(); - - double availableTime = 0.004; // 4 ms - if (_databasePager.valid() && _databasePager->requiresExternalCompileGLObjects(sceneView->getState()->getContextID())) - { - _databasePager->compileGLObjects(*(sceneView->getState()), availableTime); - } - - if (compileThread) - { - compileThread->add(_flushOperation.get()); - } - else - { - sceneView->flushDeletedGLObjects(availableTime); - } - - if (aquireGPUStats) - { - endQuery(); - checkQuery(stats); - } - - glFlush(); - - - osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); - - if (stats && stats->collectStats("rendering")) - { - stats->setAttribute(frameNumber, "Draw traversal begin time", osg::Timer::instance()->delta_s(_startTick, beforeDrawTick)); - 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(beforeDrawTick, afterDrawTick)); - } - } - - _currentDraw = 1-_currentDraw; - - } - - void cull_draw() - { - osgUtil::SceneView* sceneView = _sceneView[_currentDraw].get(); - if (!sceneView || _done) return; - - osg::GraphicsContext* compileContext = osg::GraphicsContext::getCompileContext(sceneView->getState()->getContextID()); - osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; - - OpenThreads::ScopedLock lock(_mutex[_currentDraw]); - - if (_done) - { - osg::notify(osg::INFO)<<"ViewerDoubleBufferedRenderingOperation::release() causing cull_draw to exit"<(sceneView->getCamera()->getView()); - if (view) sceneView->setFusionDistance(view->getFusionDistanceMode(), view->getFusionDistanceValue()); - - osg::Stats* stats = sceneView->getCamera()->getStats(); - osg::State* state = sceneView->getState(); - const osg::FrameStamp* fs = state->getFrameStamp(); - int frameNumber = fs ? fs->getFrameNumber() : 0; - - if (!_initialized) - { - initialize(state); - } - - bool aquireGPUStats = stats && _timerQuerySupported && stats->collectStats("gpu"); - - if (aquireGPUStats) - { - checkQuery(stats); - } - - // do cull taversal - osg::Timer_t beforeCullTick = osg::Timer::instance()->tick(); - - sceneView->inheritCullSettings(*(sceneView->getCamera())); - sceneView->cull(); - - osg::Timer_t afterCullTick = osg::Timer::instance()->tick(); - - if (state->getDynamicObjectCount()==0 && state->getDynamicObjectRenderingCompletedCallback()) - { - state->getDynamicObjectRenderingCompletedCallback()->completed(state); - } - - // do draw traveral - if (aquireGPUStats) - { - checkQuery(stats); - beginQuery(frameNumber); - } - - sceneView->draw(); - - double availableTime = 0.004; // 4 ms - if (_databasePager.valid() && _databasePager->requiresExternalCompileGLObjects(sceneView->getState()->getContextID())) - { - _databasePager->compileGLObjects(*(sceneView->getState()), availableTime); - } - - if (compileThread) - { - compileThread->add(_flushOperation.get()); - } - else - { - sceneView->flushDeletedGLObjects(availableTime); - } - - if (aquireGPUStats) - { - endQuery(); - checkQuery(stats); - } - - osg::Timer_t afterDrawTick = osg::Timer::instance()->tick(); - - if (stats && stats->collectStats("rendering")) - { - stats->setAttribute(frameNumber, "Cull traversal begin 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 begin 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)); - } - } - - virtual void operator () (osg::Object* object) - { - osg::GraphicsContext* context = dynamic_cast(object); - if (!context) - { - osg::Camera* camera = dynamic_cast(object); - if (camera) cull(); - return; - } - - //osg::notify(osg::NOTICE)<<"GraphicsCall "< _sceneView[2]; - int _frameNumber[2]; - osg::observer_ptr _databasePager; - - osg::ref_ptr _flushOperation; - -}; - - Viewer::Viewer() { constructorInit(); @@ -853,7 +248,6 @@ void Viewer::setSceneData(osg::Node* node) setReferenceTime(0.0); assignSceneDataToCameras(); - setUpRenderingSupport(); } GraphicsWindowEmbedded* Viewer::setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height) @@ -948,26 +342,13 @@ void Viewer::stopThreading() Contexts::iterator gcitr; Cameras::iterator citr; - // reset any double buffer graphics objects - for(gcitr = contexts.begin(); - gcitr != contexts.end(); - ++gcitr) + for(Cameras::iterator camItr = cameras.begin(); + camItr != cameras.end(); + ++camItr) { - osg::GraphicsContext* gc = (*gcitr); - - OpenThreads::ScopedLock lock( *(gc->getOperationsMutex()) ); - osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); - for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); - oitr != operations.end(); - ++oitr) - { - ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast(oitr->get()); - if (vdbro) - { - vdbro->release(); - } - } - + osg::Camera* camera = *camItr; + Renderer* renderer = dynamic_cast(camera->getRenderer()); + if (renderer) renderer->release(); } // delete all the graphics threads. @@ -986,29 +367,20 @@ void Viewer::stopThreading() (*citr)->setCameraThread(0); } - // reset any double buffer graphics objects - for(gcitr = contexts.begin(); - gcitr != contexts.end(); - ++gcitr) + for(Cameras::iterator camItr = cameras.begin(); + camItr != cameras.end(); + ++camItr) { - osg::GraphicsContext* gc = (*gcitr); - - OpenThreads::ScopedLock lock( *(gc->getOperationsMutex()) ); - osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); - for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); - oitr != operations.end(); - ++oitr) + osg::Camera* camera = *camItr; + Renderer* renderer = dynamic_cast(camera->getRenderer()); + if (renderer) { - ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast(oitr->get()); - if (vdbro) - { - vdbro->setGraphicsThreadDoesCull( true ); - vdbro->_done = false; - } + renderer->setGraphicsThreadDoesCull( true ); + renderer->setDone(false); } - } + int numProcessors = OpenThreads::GetNumberOfProcessors(); bool affinity = numProcessors>1; if (affinity) @@ -1029,20 +401,6 @@ void Viewer::stopThreading() osg::notify(osg::INFO)<<"Viewer::stopThreading() - stopped threading."<runOperations(); - } -}; - static osg::ApplicationUsageProxy Viewer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING ","Set the threading model using by Viewer, can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext."); static osg::ApplicationUsageProxy Viewer_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN ","Set the default screen that windows should open up on."); static osg::ApplicationUsageProxy Viewer_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WINDOW x y width height","Set the default window dimensions that windows should open up on."); @@ -1064,11 +422,9 @@ Viewer::ThreadingModel Viewer::suggestBestThreadingModel() if (contexts.empty()) return SingleThreaded; #if 0 -#ifdef _WIN32 // temporary hack to disable multi-threading under Windows till we find good solutions for // crashes that users are seeing. return SingleThreaded; -#endif #endif Cameras cameras; @@ -1090,18 +446,14 @@ Viewer::ThreadingModel Viewer::suggestBestThreadingModel() return CullThreadPerCameraDrawThreadPerContext; } -#if 1 - return DrawThreadPerContext; -#else - return CullDrawThreadPerContext; -#endif + return DrawThreadPerContext; } void Viewer::startThreading() { if (_threadsRunning) return; - // osg::notify(osg::NOTICE)<<"Viewer::startThreading() - starting threading"< lock( *(gc->getOperationsMutex()) ); - osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); - for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); - oitr != operations.end(); - ++oitr) - { - ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast(oitr->get()); - if (vdbro) - { - vdbro->setGraphicsThreadDoesCull( graphicsThreadsDoesCull ); - vdbro->_done = false; - ++numViewerDoubleBufferedRenderingOperation; - } - } - + osg::Camera* camera = *camItr; + Renderer* renderer = dynamic_cast(camera->getRenderer()); + renderer->setGraphicsThreadDoesCull(graphicsThreadsDoesCull); + renderer->setDone(false); + ++numViewerDoubleBufferedRenderingOperation; } if (_threadingModel==CullDrawThreadPerContext) @@ -1240,7 +579,7 @@ void Viewer::startThreading() if (_threadingModel==CullDrawThreadPerContext && _startRenderingBarrier.valid()) gc->getGraphicsThread()->add(_startRenderingBarrier.get()); // add the rendering operation itself. - gc->getGraphicsThread()->add(new ViewerRunOperations()); + gc->getGraphicsThread()->add(new osg::RunOperations()); if (_threadingModel==CullDrawThreadPerContext && _endBarrierPosition==BeforeSwapBuffers && _endRenderingDispatchBarrier.valid()) { @@ -1259,11 +598,8 @@ void Viewer::startThreading() gc->getGraphicsThread()->add(_endRenderingDispatchBarrier.get()); } - - } - if (_threadingModel==CullThreadPerCameraDrawThreadPerContext && numThreadsOnBarrier>1) { Cameras::iterator camItr = cameras.begin(); @@ -1284,18 +620,9 @@ void Viewer::startThreading() // add the startRenderingBarrier if (_startRenderingBarrier.valid()) camera->getCameraThread()->add(_startRenderingBarrier.get()); - OpenThreads::ScopedLock lock( *(gc->getOperationsMutex()) ); - osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); - for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); - oitr != operations.end(); - ++oitr) - { - ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast(oitr->get()); - if (vdbro && vdbro->getCamera()==camera) - { - camera->getCameraThread()->add(vdbro); - } - } + Renderer* renderer = dynamic_cast(camera->getRenderer()); + renderer->setGraphicsThreadDoesCull(true); + camera->getCameraThread()->add(renderer); if (_endRenderingDispatchBarrier.valid()) { @@ -1324,7 +651,7 @@ void Viewer::startThreading() if (_scene.valid() && _scene->getDatabasePager()) { #if 0 - //_scene->getDatabasePager()->setProcessorAffinity(1); + _scene->getDatabasePager()->setProcessorAffinity(1); #else _scene->getDatabasePager()->setProcessorAffinity(0); #endif @@ -1526,145 +853,6 @@ void Viewer::getOperationThreads(OperationThreads& threads, bool onlyActive) } -void Viewer::setUpRenderingSupport() -{ - bool threadsRunningBeforeSetUpRenderingSupport = _threadsRunning; - if (_threadsRunning) stopThreading(); - - _sceneViews.clear(); - - Contexts contexts; - getContexts(contexts); - - osg::FrameStamp* frameStamp = getFrameStamp(); - osg::DisplaySettings* ds = _displaySettings.valid() ? _displaySettings.get() : osg::DisplaySettings::instance(); - osgDB::DatabasePager* dp = _scene.valid() ? _scene->getDatabasePager() : 0; - - bool graphicsThreadDoesCull = _threadingModel!=CullThreadPerCameraDrawThreadPerContext; - unsigned int numViewerDoubleBufferedRenderingOperation = 0; - - Cameras localCameras; - getCameras(localCameras); - - unsigned int sceneViewOptions = osgUtil::SceneView::HEADLIGHT; - - if (true)//(_threadingModel==CullThreadPerCameraDrawThreadPerContext) - { - for(Contexts::iterator gcitr = contexts.begin(); - gcitr != contexts.end(); - ++gcitr) - { - (*gcitr)->removeAllOperations(); - - osg::GraphicsContext* gc = *gcitr; - osg::GraphicsContext::Cameras& cameras = gc->getCameras(); - osg::State* state = gc->getState(); - - if (dp) dp->setCompileGLObjectsForContextID(state->getContextID(), true); - - for(osg::GraphicsContext::Cameras::iterator citr = cameras.begin(); - citr != cameras.end(); - ++citr) - { - osg::Camera* camera = *citr; - if (!camera->getStats()) camera->setStats(new osg::Stats("Camera")); - - bool localCamera = std::find(localCameras.begin(),localCameras.end(),camera) != localCameras.end(); - if (localCamera) - { - osgUtil::SceneView* sceneViewList[2]; - - for(int i=0; i<2; ++i) - { - osgUtil::SceneView* sceneView = new osgUtil::SceneView; - - _sceneViews.push_back(sceneView); - sceneViewList[i] = sceneView; - - sceneView->setGlobalStateSet(_camera->getStateSet()); - sceneView->setDefaults(sceneViewOptions); - sceneView->setDisplaySettings(camera->getDisplaySettings()!=0 ? camera->getDisplaySettings() : ds); - sceneView->setCamera(camera); - sceneView->setState(state); - sceneView->setFrameStamp(frameStamp); - } - - - // osg::notify(osg::NOTICE)<<"localCamera "<getName()<add(vdbro); - ++numViewerDoubleBufferedRenderingOperation; - } - else - { - // osg::notify(osg::NOTICE)<<"non local Camera"<setGlobalStateSet(_camera->getStateSet()); - sceneView->setDefaults(sceneViewOptions); - sceneView->setDisplaySettings(ds); - sceneView->setCamera(camera); - sceneView->setDisplaySettings(camera->getDisplaySettings()!=0 ? camera->getDisplaySettings() : ds); - sceneView->setState(state); - sceneView->setFrameStamp(frameStamp); - - ViewerRenderingOperation* vro = new ViewerRenderingOperation(sceneView, dp, _startTick); - gc->add(vro); - } - - } - } - } - else - { - for(Contexts::iterator gcitr = contexts.begin(); - gcitr != contexts.end(); - ++gcitr) - { - (*gcitr)->removeAllOperations(); - - osg::GraphicsContext* gc = *gcitr; - osg::GraphicsContext::Cameras& cameras = gc->getCameras(); - osg::State* state = gc->getState(); - - for(osg::GraphicsContext::Cameras::iterator citr = cameras.begin(); - citr != cameras.end(); - ++citr) - { - osg::Camera* camera = *citr; - - if (!camera->getStats()) camera->setStats(new osg::Stats("Camera")); - - osgUtil::SceneView* sceneView = new osgUtil::SceneView; - _sceneViews.push_back(sceneView); - - sceneView->setGlobalStateSet(_camera->getStateSet()); - sceneView->setDefaults(sceneViewOptions); - sceneView->setDisplaySettings(camera->getDisplaySettings()!=0 ? camera->getDisplaySettings() : ds); - sceneView->setCamera(camera); - sceneView->setState(state); - sceneView->setFrameStamp(frameStamp); - - if (dp) dp->setCompileGLObjectsForContextID(state->getContextID(), true); - - gc->add(new ViewerRenderingOperation(sceneView, dp, _startTick)); - - ++numViewerDoubleBufferedRenderingOperation; - } - } - } - - if (_endDynamicDrawBlock.valid()) - { - _endDynamicDrawBlock->setNumOfBlocks(numViewerDoubleBufferedRenderingOperation); - } - - if (threadsRunningBeforeSetUpRenderingSupport) startThreading(); -} - void Viewer::realize() { @@ -1720,8 +908,6 @@ void Viewer::realize() return; } - setUpRenderingSupport(); - for(Contexts::iterator citr = contexts.begin(); citr != contexts.end(); ++citr) @@ -2221,6 +1407,9 @@ void Viewer::renderingTraversals() Contexts contexts; getContexts(contexts); + + Cameras cameras; + getCameras(cameras); Contexts::iterator itr; @@ -2233,25 +1422,17 @@ void Viewer::renderingTraversals() } // reset any double buffer graphics objects - for(itr = contexts.begin(); - itr != contexts.end(); - ++itr) + for(Cameras::iterator camItr = cameras.begin(); + camItr != cameras.end(); + ++camItr) { - osg::GraphicsContext* gc = (*itr); - - OpenThreads::ScopedLock lock( *(gc->getOperationsMutex()) ); - osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); - for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); - oitr != operations.end(); - ++oitr) + osg::Camera* camera = *camItr; + Renderer* renderer = dynamic_cast(camera->getRenderer()); + if (renderer) { - ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast(oitr->get()); - if (vdbro) + if (!renderer->getGraphicsThreadDoesCull() && !(camera->getCameraThread())) { - if (!vdbro->getGraphicsThreadDoesCull() && !(vdbro->getCamera()->getCameraThread())) - { - vdbro->cull(); - } + renderer->cull(); } } } diff --git a/src/osgWrappers/osg/Camera.cpp b/src/osgWrappers/osg/Camera.cpp index 531774fa7..c8916c18f 100644 --- a/src/osgWrappers/osg/Camera.cpp +++ b/src/osgWrappers/osg/Camera.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -476,21 +477,36 @@ BEGIN_OBJECT_REFLECTOR(osg::Camera) __C5_GraphicsContext_P1__getGraphicsContext, "Get the const GraphicsContext. ", ""); - I_Method1(void, setRenderer, IN, osg::Object *, rc, + I_Method1(void, setRenderer, IN, osg::GraphicsOperation *, rc, Properties::NON_VIRTUAL, - __void__setRenderer__osg_Object_P1, + __void__setRenderer__osg_GraphicsOperation_P1, "Set the Rendering object that is used to implement rendering of the subgraph. ", ""); - I_Method0(osg::Object *, getRenderer, + I_Method0(osg::GraphicsOperation *, getRenderer, Properties::NON_VIRTUAL, - __osg_Object_P1__getRenderer, + __osg_GraphicsOperation_P1__getRenderer, "Get the Rendering object that is used to implement rendering of the subgraph. ", ""); - I_Method0(const osg::Object *, getRenderer, + I_Method0(const osg::GraphicsOperation *, getRenderer, Properties::NON_VIRTUAL, - __C5_osg_Object_P1__getRenderer, + __C5_osg_GraphicsOperation_P1__getRenderer, "Get the const Rendering object that is used to implement rendering of the subgraph. ", ""); + I_Method1(void, setRenderingCache, IN, osg::Object *, rc, + Properties::NON_VIRTUAL, + __void__setRenderingCache__osg_Object_P1, + "Set the Rendering cache that is used for cached objects associated with rendering of subgraphs. ", + ""); + I_Method0(osg::Object *, getRenderingCache, + Properties::NON_VIRTUAL, + __osg_Object_P1__getRenderingCache, + "Get the Rendering cache that is used for cached objects associated with rendering of subgraphs. ", + ""); + I_Method0(const osg::Object *, getRenderingCache, + Properties::NON_VIRTUAL, + __C5_osg_Object_P1__getRenderingCache, + "Get the const Rendering cache that is used for cached objects associated with rendering of subgraphs. ", + ""); I_Method1(void, setPreDrawCallback, IN, osg::Camera::DrawCallback *, cb, Properties::NON_VIRTUAL, __void__setPreDrawCallback__DrawCallback_P1, @@ -606,9 +622,12 @@ BEGIN_OBJECT_REFLECTOR(osg::Camera) I_SimpleProperty(osg::Camera::RenderTargetImplementation, RenderTargetImplementation, __RenderTargetImplementation__getRenderTargetImplementation, __void__setRenderTargetImplementation__RenderTargetImplementation); - I_SimpleProperty(osg::Object *, Renderer, - __osg_Object_P1__getRenderer, - __void__setRenderer__osg_Object_P1); + I_SimpleProperty(osg::GraphicsOperation *, Renderer, + __osg_GraphicsOperation_P1__getRenderer, + __void__setRenderer__osg_GraphicsOperation_P1); + I_SimpleProperty(osg::Object *, RenderingCache, + __osg_Object_P1__getRenderingCache, + __void__setRenderingCache__osg_Object_P1); I_SimpleProperty(osg::Stats *, Stats, __osg_Stats_P1__getStats, __void__setStats__osg_Stats_P1); diff --git a/src/osgWrappers/osg/GraphicsThread.cpp b/src/osgWrappers/osg/GraphicsThread.cpp index d5e6594e7..c165f68eb 100644 --- a/src/osgWrappers/osg/GraphicsThread.cpp +++ b/src/osgWrappers/osg/GraphicsThread.cpp @@ -105,6 +105,14 @@ BEGIN_OBJECT_REFLECTOR(osg::ReleaseContext_Block_MakeCurrentOperation) ""); END_REFLECTOR +BEGIN_OBJECT_REFLECTOR(osg::RunOperations) + I_DeclaringFile("osg/GraphicsThread"); + I_BaseType(osg::GraphicsOperation); + I_Constructor0(____RunOperations, + "", + ""); +END_REFLECTOR + BEGIN_OBJECT_REFLECTOR(osg::SwapBuffersOperation) I_DeclaringFile("osg/GraphicsThread"); I_BaseType(osg::GraphicsOperation); diff --git a/src/osgWrappers/osg/State.cpp b/src/osgWrappers/osg/State.cpp index ac5e47caa..f7567d8a3 100644 --- a/src/osgWrappers/osg/State.cpp +++ b/src/osgWrappers/osg/State.cpp @@ -621,10 +621,15 @@ BEGIN_OBJECT_REFLECTOR(osg::State) __void__setFrameStamp__FrameStamp_P1, "Set the frame stamp for the current frame. ", ""); + I_Method0(osg::FrameStamp *, getFrameStamp, + Properties::NON_VIRTUAL, + __FrameStamp_P1__getFrameStamp, + "Get the frame stamp for the current frame. ", + ""); I_Method0(const osg::FrameStamp *, getFrameStamp, Properties::NON_VIRTUAL, __C5_FrameStamp_P1__getFrameStamp, - "Get the frame stamp for the current frame. ", + "Get the const frame stamp for the current frame. ", ""); I_Method1(void, setDisplaySettings, IN, osg::DisplaySettings *, vs, Properties::NON_VIRTUAL, @@ -795,7 +800,7 @@ BEGIN_OBJECT_REFLECTOR(osg::State) 0, __void__setFogCoordPointer__C5_Array_P1); I_SimpleProperty(osg::FrameStamp *, FrameStamp, - 0, + __FrameStamp_P1__getFrameStamp, __void__setFrameStamp__FrameStamp_P1); I_SimpleProperty(const osg::StateAttribute *, GlobalDefaultAttribute, 0, diff --git a/src/osgWrappers/osg/View.cpp b/src/osgWrappers/osg/View.cpp index e842a438e..e880f06fb 100644 --- a/src/osgWrappers/osg/View.cpp +++ b/src/osgWrappers/osg/View.cpp @@ -169,6 +169,12 @@ BEGIN_OBJECT_REFLECTOR(osg::View) __void__updateSlave__unsigned_int, "", ""); + I_ProtectedMethod1(osg::GraphicsOperation *, createRenderer, IN, osg::Camera *, camera, + Properties::VIRTUAL, + Properties::NON_CONST, + __osg_GraphicsOperation_P1__createRenderer__osg_Camera_P1, + "", + ""); I_SimpleProperty(osg::Camera *, Camera, __osg_Camera_P1__getCamera, __void__setCamera__osg_Camera_P1); diff --git a/src/osgWrappers/osgViewer/CompositeViewer.cpp b/src/osgWrappers/osgViewer/CompositeViewer.cpp index 14928e853..128028b89 100644 --- a/src/osgWrappers/osgViewer/CompositeViewer.cpp +++ b/src/osgWrappers/osgViewer/CompositeViewer.cpp @@ -277,11 +277,6 @@ BEGIN_OBJECT_REFLECTOR(osgViewer::CompositeViewer) __void__startThreading, "Start any threads required by the viewer, as per viewers ThreadingModel. ", ""); - I_Method0(void, setUpRenderingSupport, - Properties::NON_VIRTUAL, - __void__setUpRenderingSupport, - "Set up the Operations to render the various viewer cameras on the viewers graphics windows. ", - ""); I_ProtectedMethod0(void, constructorInit, Properties::NON_VIRTUAL, Properties::NON_CONST, diff --git a/src/osgWrappers/osgViewer/Renderer.cpp b/src/osgWrappers/osgViewer/Renderer.cpp new file mode 100644 index 000000000..53e6a10bc --- /dev/null +++ b/src/osgWrappers/osgViewer/Renderer.cpp @@ -0,0 +1,152 @@ +// *************************************************************************** +// +// Generated automatically by genwrapper. +// Please DO NOT EDIT this file! +// +// *************************************************************************** + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +// Must undefine IN and OUT macros defined in Windows headers +#ifdef IN +#undef IN +#endif +#ifdef OUT +#undef OUT +#endif + +TYPE_NAME_ALIAS(std::pair< GLuint COMMA int >, osgViewer::OpenGLQuerySupport::QueryFrameNumberPair) + +TYPE_NAME_ALIAS(std::list< osgViewer::OpenGLQuerySupport::QueryFrameNumberPair >, osgViewer::OpenGLQuerySupport::QueryFrameNumberList) + +TYPE_NAME_ALIAS(std::vector< GLuint >, osgViewer::OpenGLQuerySupport::QueryList) + +BEGIN_VALUE_REFLECTOR(osgViewer::OpenGLQuerySupport) + I_DeclaringFile("osgViewer/Renderer"); + I_Constructor0(____OpenGLQuerySupport, + "", + ""); + I_Method1(void, setStartTick, IN, osg::Timer_t, startTick, + Properties::NON_VIRTUAL, + __void__setStartTick__osg_Timer_t, + "", + ""); + I_Method0(osg::Timer_t, getStartTick, + Properties::NON_VIRTUAL, + __osg_Timer_t__getStartTick, + "", + ""); + I_Method1(void, checkQuery, IN, osg::Stats *, stats, + Properties::NON_VIRTUAL, + __void__checkQuery__osg_Stats_P1, + "", + ""); + I_Method0(GLuint, createQueryObject, + Properties::NON_VIRTUAL, + __GLuint__createQueryObject, + "", + ""); + I_Method1(void, beginQuery, IN, int, frameNumber, + Properties::NON_VIRTUAL, + __void__beginQuery__int, + "", + ""); + I_Method0(void, endQuery, + Properties::NON_VIRTUAL, + __void__endQuery, + "", + ""); + I_Method1(void, initialize, IN, osg::State *, state, + Properties::NON_VIRTUAL, + __void__initialize__osg_State_P1, + "", + ""); + I_SimpleProperty(osg::Timer_t, StartTick, + __osg_Timer_t__getStartTick, + __void__setStartTick__osg_Timer_t); +END_REFLECTOR + +BEGIN_OBJECT_REFLECTOR(osgViewer::Renderer) + I_DeclaringFile("osgViewer/Renderer"); + I_BaseType(osg::GraphicsOperation); + I_BaseType(osgViewer::OpenGLQuerySupport); + I_Constructor1(IN, osg::Camera *, camera, + Properties::NON_EXPLICIT, + ____Renderer__osg_Camera_P1, + "", + ""); + I_Method1(osgUtil::SceneView *, getSceneView, IN, unsigned int, i, + Properties::NON_VIRTUAL, + __osgUtil_SceneView_P1__getSceneView__unsigned_int, + "", + ""); + I_Method1(void, setDone, IN, bool, done, + Properties::NON_VIRTUAL, + __void__setDone__bool, + "", + ""); + I_Method0(bool, getDone, + Properties::NON_VIRTUAL, + __bool__getDone, + "", + ""); + I_Method1(void, setGraphicsThreadDoesCull, IN, bool, flag, + Properties::NON_VIRTUAL, + __void__setGraphicsThreadDoesCull__bool, + "", + ""); + I_Method0(bool, getGraphicsThreadDoesCull, + Properties::NON_VIRTUAL, + __bool__getGraphicsThreadDoesCull, + "", + ""); + I_Method0(void, cull, + Properties::NON_VIRTUAL, + __void__cull, + "", + ""); + I_Method0(void, draw, + Properties::NON_VIRTUAL, + __void__draw, + "", + ""); + I_Method0(void, cull_draw, + Properties::NON_VIRTUAL, + __void__cull_draw, + "", + ""); + I_Method0(void, release, + Properties::VIRTUAL, + __void__release, + "if this operation is a barrier then release it. ", + ""); + I_ProtectedMethod1(void, updateSceneView, IN, osgUtil::SceneView *, sceneView, + Properties::NON_VIRTUAL, + Properties::NON_CONST, + __void__updateSceneView__osgUtil_SceneView_P1, + "", + ""); + I_SimpleProperty(bool, Done, + __bool__getDone, + __void__setDone__bool); + I_SimpleProperty(bool, GraphicsThreadDoesCull, + __bool__getGraphicsThreadDoesCull, + __void__setGraphicsThreadDoesCull__bool); +END_REFLECTOR + +STD_LIST_REFLECTOR(std::list< osgViewer::OpenGLQuerySupport::QueryFrameNumberPair >) + +STD_PAIR_REFLECTOR(std::pair< GLuint COMMA int >) + diff --git a/src/osgWrappers/osgViewer/View.cpp b/src/osgWrappers/osgViewer/View.cpp index acf76f66b..7e8d5d56a 100644 --- a/src/osgWrappers/osgViewer/View.cpp +++ b/src/osgWrappers/osgViewer/View.cpp @@ -201,17 +201,17 @@ BEGIN_OBJECT_REFLECTOR(osgViewer::View) I_Method1(void, setDisplaySettings, IN, osg::DisplaySettings *, ds, Properties::NON_VIRTUAL, __void__setDisplaySettings__osg_DisplaySettings_P1, - "Set the DsplaySettings object associated with this view. ", + "Set the DisplaySettings object associated with this view. ", ""); I_Method0(osg::DisplaySettings *, getDisplaySettings, Properties::NON_VIRTUAL, __osg_DisplaySettings_P1__getDisplaySettings, - "Set the DsplaySettings object associated with this view. ", + "Set the DisplaySettings object associated with this view. ", ""); I_Method0(const osg::DisplaySettings *, getDisplaySettings, Properties::NON_VIRTUAL, __C5_osg_DisplaySettings_P1__getDisplaySettings, - "Set the DsplaySettings object associated with this view. ", + "Set the DisplaySettings object associated with this view. ", ""); I_MethodWithDefaults2(void, setFusionDistance, IN, osgUtil::SceneView::FusionDistanceMode, mode, , IN, float, value, 1.0f, Properties::NON_VIRTUAL, @@ -288,6 +288,12 @@ BEGIN_OBJECT_REFLECTOR(osgViewer::View) __void__init, "", ""); + I_ProtectedMethod1(osg::GraphicsOperation *, createRenderer, IN, osg::Camera *, camera, + Properties::VIRTUAL, + Properties::NON_CONST, + __osg_GraphicsOperation_P1__createRenderer__osg_Camera_P1, + "", + ""); I_SimpleProperty(osgGA::MatrixManipulator *, CameraManipulator, __osgGA_MatrixManipulator_P1__getCameraManipulator, __void__setCameraManipulator__osgGA_MatrixManipulator_P1); diff --git a/src/osgWrappers/osgViewer/Viewer.cpp b/src/osgWrappers/osgViewer/Viewer.cpp index 3f05a3e2a..3375c3e33 100644 --- a/src/osgWrappers/osgViewer/Viewer.cpp +++ b/src/osgWrappers/osgViewer/Viewer.cpp @@ -274,11 +274,6 @@ BEGIN_OBJECT_REFLECTOR(osgViewer::Viewer) __void__startThreading, "Start any threads required by the viewer. ", ""); - I_Method0(void, setUpRenderingSupport, - Properties::NON_VIRTUAL, - __void__setUpRenderingSupport, - "Set up the Operations to render the various viewer cameras on the viewers graphics windows. ", - ""); I_Method1(void, getUsage, IN, osg::ApplicationUsage &, usage, Properties::VIRTUAL, __void__getUsage__osg_ApplicationUsage_R1,