From 04c1dee7a2cb7f2867345a59717bdd84b0878bbb Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 17 Jul 2007 10:54:17 +0000 Subject: [PATCH] Fixed GraphcicsContext::getMaxContextID so it properly returns the current max contextID. Fixed the osgviewer's compile context code to account for the above fix. Added compile context support into osgterrain example. --- applications/osgviewer/osgviewer.cpp | 4 +- examples/osgterrain/osgterrain.cpp | 65 +++++++++++++++++++++++----- src/osg/GraphicsContext.cpp | 29 +++++++++---- 3 files changed, 76 insertions(+), 22 deletions(-) diff --git a/applications/osgviewer/osgviewer.cpp b/applications/osgviewer/osgviewer.cpp index bc888df35..38f9cf4b4 100644 --- a/applications/osgviewer/osgviewer.cpp +++ b/applications/osgviewer/osgviewer.cpp @@ -157,7 +157,7 @@ int main(int argc, char** argv) int numProcessors = OpenThreads::GetNumberOfProcessors(); int processNum = 0; - for(unsigned int i=0; i #include +#include + #include #include @@ -119,6 +121,8 @@ public: virtual void operator () (osg::Object* object) { + // osg::notify(osg::NOTICE)<<"void operator ()"< loadedModel = osgDB::readNodeFile(*nitr); - if (loadedModel.get()) nodesToAdd[*nitr] = loadedModel; - } + + if (loadedModel.get()) + { + osg::notify(osg::NOTICE)<<"Loadinged file "<<*nitr< compileOperation = new osgUtil::GLObjectsOperation(loadedModel.get()); +#if 1 + for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) + { + osg::GraphicsContext* gc = osg::GraphicsContext::getCompileContext(i); + osg::GraphicsThread* gt = gc ? gc->getGraphicsThread() : 0; + if (gt) + { + osg::notify(osg::NOTICE)<<"Adding compile op. to compile context "<add( compileOperation.get() ); + } + } +#endif + } + } // swap the data. { @@ -180,8 +197,6 @@ public: void update(osg::Group* scene) { - // osg::notify(osg::NOTICE)<<"update"< lock(_mutex); if (!_nodesToRemove.empty()) @@ -403,6 +418,13 @@ int main(int argc, char** argv) double w = 1.0; double h = 1.0; + bool createBackgroundContextForCompiling = false; + while (arguments.read("--bc")) { createBackgroundContextForCompiling = true; } + + bool createBackgroundThreadsForCompiling = false; + while (arguments.read("--bt")) { createBackgroundContextForCompiling = true; createBackgroundThreadsForCompiling = true; } + + osg::ref_ptr masterOperation; std::string masterFilename; while(arguments.read("-m",masterFilename)) @@ -430,6 +452,27 @@ int main(int argc, char** argv) operationThread->add(masterOperation.get()); } + if (createBackgroundContextForCompiling) + { + + int numProcessors = OpenThreads::GetNumberOfProcessors(); + int processNum = 0; + + for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) + { + osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); + + if (gc && createBackgroundThreadsForCompiling) + { + gc->createGraphicsThread(); + gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); + gc->getGraphicsThread()->startThread(); + + ++processNum; + } + } + } + while (!viewer.done()) { viewer.advance(); diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index d650589bf..cd3dc1e32 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -147,8 +147,14 @@ unsigned int GraphicsContext::createNewContextID() unsigned int GraphicsContext::getMaxContextID() { OpenThreads::ScopedLock lock(s_contextIDMapMutex); - - return s_contextIDMap.size(); + unsigned int maxContextID = 0; + for(ContextIDMap::iterator itr = s_contextIDMap.begin(); + itr != s_contextIDMap.end(); + ++itr) + { + if (itr->first > maxContextID) maxContextID = itr->first; + } + return maxContextID; } @@ -260,16 +266,19 @@ GraphicsContext* GraphicsContext::getOrCreateCompileContext(unsigned int context traits->sharedContext = src_gc; traits->pbuffer = true; - osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits); - gc->realize(); - + osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits); + if (gc.valid() && gc->realize()) { OpenThreads::ScopedLock lock(s_contextIDMapMutex); s_contextIDMap[contextID]._compileContext = gc; + osg::notify(osg::INFO)<<" succeded GraphicsContext::createCompileContext."< lock(s_contextIDMapMutex); - return s_contextIDMap[contextID]._compileContext.get(); + ContextIDMap::iterator itr = s_contextIDMap.find(contextID); + if (itr != s_contextIDMap.end()) return itr->second._compileContext.get(); + else return 0; }