Preliminary work on general purpose incremental compile support in osgViewer.

This commit is contained in:
Robert Osfield
2009-03-08 12:00:36 +00:00
parent 43a081ee98
commit 7473b06275
8 changed files with 405 additions and 74 deletions

View File

@@ -317,7 +317,7 @@ GraphicsContext::GraphicsContexts GraphicsContext::getRegisteredGraphicsContexts
GraphicsContext* GraphicsContext::getOrCreateCompileContext(unsigned int contextID)
{
osg::notify(osg::INFO)<<"GraphicsContext::createCompileContext."<<std::endl;
osg::notify(osg::NOTICE)<<"GraphicsContext::createCompileContext."<<std::endl;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
@@ -349,7 +349,7 @@ GraphicsContext* GraphicsContext::getOrCreateCompileContext(unsigned int context
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
s_contextIDMap[contextID]._compileContext = gc;
osg::notify(osg::INFO)<<" succeeded GraphicsContext::createCompileContext."<<std::endl;
osg::notify(osg::NOTICE)<<" succeeded GraphicsContext::createCompileContext."<<std::endl;
return gc.release();
}
else

View File

@@ -13,10 +13,16 @@
#include <osgUtil/GLObjectsVisitor>
#include <osg/Drawable>
#include <osg/Notify>
#include <OpenThreads/ScopedLock>
using namespace osg;
using namespace osgUtil;
/////////////////////////////////////////////////////////////////
//
// GLObjectsVisitor
//
GLObjectsVisitor::GLObjectsVisitor(Mode mode)
{
setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
@@ -145,6 +151,11 @@ void GLObjectsVisitor::apply(osg::StateSet& stateset)
}
}
/////////////////////////////////////////////////////////////////
//
// GLObjectsVisitor
//
GLObjectsOperation::GLObjectsOperation(GLObjectsVisitor::Mode mode):
osg::GraphicsOperation("GLObjectOperation",false),
_mode(mode)
@@ -182,3 +193,156 @@ void GLObjectsOperation::operator () (osg::GraphicsContext* context)
}
// osg::notify(osg::NOTICE)<<"GLObjectsOperation::after >>>>>>>>>>> "<<std::endl;
}
/////////////////////////////////////////////////////////////////
//
// IncrementalCompileOperation
//
IncrementalCompileOperation::IncrementalCompileOperation():
osg::GraphicsOperation("IncrementalCompileOperation",true)
{
}
IncrementalCompileOperation::~IncrementalCompileOperation()
{
}
void IncrementalCompileOperation::assignContexts(Contexts& contexts)
{
for(Contexts::iterator itr = contexts.begin();
itr != contexts.end();
++itr)
{
osg::GraphicsContext* gc = *itr;
addGraphicsContext(gc);
}
}
void IncrementalCompileOperation::removeContexts(Contexts& contexts)
{
for(Contexts::iterator itr = contexts.begin();
itr != contexts.end();
++itr)
{
osg::GraphicsContext* gc = *itr;
removeGraphicsContext(gc);
}
}
void IncrementalCompileOperation::addGraphicsContext(osg::GraphicsContext* gc)
{
if (_contexts.count(gc)==0)
{
gc->add(this);
_contexts.insert(gc);
}
}
void IncrementalCompileOperation::removeGraphicsContext(osg::GraphicsContext* gc)
{
if (_contexts.count(gc)!=0)
{
gc->remove(this);
_contexts.erase(gc);
}
}
void IncrementalCompileOperation::add(osg::Node* subgraphToCompile)
{
osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add("<<subgraphToCompile<<")"<<std::endl;
add(new CompileSet(subgraphToCompile));
}
void IncrementalCompileOperation::add(osg::Group* attachmentPoint, osg::Node* subgraphToCompile)
{
osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add("<<attachmentPoint<<", "<<subgraphToCompile<<")"<<std::endl;
add(new CompileSet(attachmentPoint, subgraphToCompile));
}
void IncrementalCompileOperation::add(CompileSet* compileSet, bool callBuildCompileMap)
{
if (!compileSet) return;
if (callBuildCompileMap) compileSet->buildCompileMap(_contexts);
osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::add(CompileSet = "<<compileSet<<", "<<", "<<callBuildCompileMap<<")"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_toCompileMutex);
_toCompile.push_back(compileSet);
}
void IncrementalCompileOperation::mergeCompiledSubgraphs()
{
osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::mergeCompiledSubgraphs()"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex);
for(CompileSets::iterator itr = _compiled.begin();
itr != _compiled.end();
++itr)
{
CompileSet* cs = itr->get();
if (cs->_attachmentPoint.valid())
{
cs->_attachmentPoint->addChild(cs->_subgraphToCompile.get());
}
}
_compiled.clear();
}
void IncrementalCompileOperation::CompileSet::buildCompileMap(ContextSet& context)
{
}
void IncrementalCompileOperation::operator () (osg::GraphicsContext* context)
{
osg::notify(osg::NOTICE)<<"IncrementalCompileOperation::operator () ("<<context<<")"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> toCompile_lock(_toCompileMutex);
for(CompileSets::iterator itr = _toCompile.begin();
itr != _toCompile.end();
)
{
CompileSet* cs = itr->get();
CompileMap& cm = cs->_compileMap;
CompileData* cd = cm[context].get();
if (cd)
{
// compile textures
cd->_textures.clear();
// compile drawables
cd->_drawables.clear();
// compile programs
cd->_programs.clear();
}
if (!cd || cd->empty())
{
if (cs->_compileCompletedCallback.valid())
{
if (cs->_compileCompletedCallback->compileCompleted(cs))
{
// callback will handle merging of subgraph so no need to place CompileSet in merge.
}
else
{
OpenThreads::ScopedLock<OpenThreads::Mutex> compilded_lock(_compiledMutex);
_compiled.push_back(cs);
}
}
// remove from toCompileSet;
itr = _toCompile.erase(itr);
}
else
{
++itr;
}
}
}

View File

@@ -497,6 +497,10 @@ void CompositeViewer::realize()
}
}
// attach contexts to _incrementalCompileOperation if attached.
if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts);
bool grabFocus = true;
if (grabFocus)
{
@@ -994,6 +998,12 @@ void CompositeViewer::updateTraversal()
}
if (_incrementalCompileOperation.valid())
{
// merge subgraphs that have been compiled by the incremental compiler operation.
_incrementalCompileOperation->mergeCompiledSubgraphs();
}
if (_updateOperations.valid())
{
_updateOperations->runOperations(this);

View File

@@ -448,7 +448,10 @@ void Viewer::realize()
gc->releaseContext();
}
}
// attach contexts to _incrementalCompileOperation if attached.
if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts);
bool grabFocus = true;
if (grabFocus)
{
@@ -898,6 +901,13 @@ void Viewer::updateTraversal()
{
_updateOperations->runOperations(this);
}
if (_incrementalCompileOperation.valid())
{
// merge subgraphs that have been compiled by the incremental compiler operation.
_incrementalCompileOperation->mergeCompiledSubgraphs();
}
{
// call any camera update callbacks, but only traverse that callback, don't traverse its subgraph

View File

@@ -375,7 +375,7 @@ void ViewerBase::startThreading()
if (!gc->isRealized())
{
osg::notify(osg::INFO)<<"ViewerBase::startThreading() : Realizng window "<<gc<<std::endl;
osg::notify(osg::INFO)<<"ViewerBase::startThreading() : Realizing window "<<gc<<std::endl;
gc->realize();
}
@@ -551,6 +551,21 @@ void ViewerBase::removeUpdateOperation(osg::Operation* operation)
}
}
void ViewerBase::setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico)
{
if (_incrementalCompileOperation == ico) return;
Contexts contexts;
getContexts(contexts, false);
if (_incrementalCompileOperation.valid()) _incrementalCompileOperation->removeContexts(contexts);
// assign new operation
_incrementalCompileOperation = ico;
if (_incrementalCompileOperation) _incrementalCompileOperation->assignContexts(contexts);
}
int ViewerBase::run()
{
if (!isRealized())