Skip to content

Commit

Permalink
Moved compile setup from osgViewer::ViewerBase into osgViewer::Render…
Browse files Browse the repository at this point in the history
…er to

avoid threading issues associated with compile running in a parallel with 
update/cull on the first frame.

Also added automatic recompile when a new SceneData is applied to a View.
  • Loading branch information
robertosfield committed May 10, 2008
1 parent a8426b8 commit 1a57bf7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions include/osgViewer/Renderer
Expand Up @@ -66,10 +66,16 @@ class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQu
void setGraphicsThreadDoesCull(bool flag);
bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; }


virtual void cull();
virtual void draw();
virtual void cull_draw();

virtual void compile();

void setCompileOnNextDraw(bool flag) { _compileOnNextDraw = flag; }
bool getCompileOnNextDraw() const { return _compileOnNextDraw; }

virtual void operator () (osg::Object* object);

virtual void operator () (osg::GraphicsContext* context);
Expand Down Expand Up @@ -136,6 +142,7 @@ class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQu

bool _done;
bool _graphicsThreadDoesCull;
bool _compileOnNextDraw;

osg::ref_ptr<osgUtil::SceneView> _sceneView[2];

Expand Down
31 changes: 29 additions & 2 deletions src/osgViewer/Renderer.cpp
Expand Up @@ -167,7 +167,8 @@ Renderer::Renderer(osg::Camera* camera):
_conservativeTimeRatio(0.5),
_camera(camera),
_done(false),
_graphicsThreadDoesCull(true)
_graphicsThreadDoesCull(true),
_compileOnNextDraw(true)
{

DEBUG_MESSAGE<<"Render::Render() "<<this<<std::endl;
Expand Down Expand Up @@ -251,6 +252,22 @@ void Renderer::updateSceneView(osgUtil::SceneView* sceneView)
if (view) _startTick = view->getStartTick();
}

void Renderer::compile()
{
DEBUG_MESSAGE<<"Renderer::compile()"<<std::endl;

_compileOnNextDraw = false;

osgUtil::SceneView* sceneView = _sceneView[0].get();
if (!sceneView || _done) return;

if (sceneView->getSceneData())
{
osgUtil::GLObjectsVisitor glov;
glov.setState(sceneView->getState());
sceneView->getSceneData()->accept(glov);
}
}

void Renderer::cull()
{
Expand Down Expand Up @@ -322,8 +339,13 @@ void Renderer::draw()
osg::GraphicsContext* compileContext = sceneView ? osg::GraphicsContext::getCompileContext(sceneView->getState()->getContextID()) : 0;
osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0;

if (sceneView || _done)
if (sceneView && !_done)
{
if (_compileOnNextDraw)
{
compile();
}

osgViewer::View* view = dynamic_cast<osgViewer::View*>(_camera->getView());
osgDB::DatabasePager* databasePager = view ? view->getDatabasePager() : 0;

Expand Down Expand Up @@ -431,6 +453,11 @@ void Renderer::cull_draw()
osgUtil::SceneView* sceneView = _sceneView[0].get();
if (!sceneView || _done) return;

if (_compileOnNextDraw)
{
compile();
}

updateSceneView(sceneView);

osgViewer::View* view = dynamic_cast<osgViewer::View*>(_camera->getView());
Expand Down
7 changes: 7 additions & 0 deletions src/osgViewer/View.cpp
Expand Up @@ -1634,6 +1634,10 @@ void View::assignSceneDataToCameras()
{
_camera->removeChildren(0,_camera->getNumChildren());
if (sceneData) _camera->addChild(sceneData);

Renderer* renderer = dynamic_cast<Renderer*>(_camera->getRenderer());
if (renderer) renderer->setCompileOnNextDraw(true);

}

for(unsigned i=0; i<getNumSlaves(); ++i)
Expand All @@ -1643,6 +1647,9 @@ void View::assignSceneDataToCameras()
{
slave._camera->removeChildren(0,slave._camera->getNumChildren());
if (sceneData) slave._camera->addChild(sceneData);

Renderer* renderer = dynamic_cast<Renderer*>(slave._camera->getRenderer());
if (renderer) renderer->setCompileOnNextDraw(true);
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/osgViewer/ViewerBase.cpp
Expand Up @@ -26,8 +26,6 @@

#include <osgUtil/Optimizer>
#include <osgUtil/IntersectionVisitor>
#include <osgUtil/GLObjectsVisitor>
#include <osgUtil/RenderLeaf>

static osg::ApplicationUsageProxy ViewerBase_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_CONFIG_FILE <filename>","Specify a viewer configuration file to load by default.");
static osg::ApplicationUsageProxy ViewerBase_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING <value>","Set the threading model using by Viewer, <value> can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext.");
Expand Down Expand Up @@ -281,7 +279,6 @@ void ViewerBase::startThreading()
// using multi-threading so make sure that new objects are allocated with thread safe ref/unref
osg::Referenced::setThreadSafeReferenceCounting(true);


Scenes scenes;
getScenes(scenes);
for(Scenes::iterator scitr = scenes.begin();
Expand Down Expand Up @@ -375,8 +372,6 @@ void ViewerBase::startThreading()
if (affinity) gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors);
threadAffinityMap[gc->getGraphicsThread()] = processNum % numProcessors;

gc->getGraphicsThread()->add(new osgUtil::GLObjectsOperation());

// add the startRenderingBarrier
if (_threadingModel==CullDrawThreadPerContext && _startRenderingBarrier.valid()) gc->getGraphicsThread()->add(_startRenderingBarrier.get());

Expand Down

0 comments on commit 1a57bf7

Please sign in to comment.