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.
This commit is contained in:
Robert Osfield 2007-07-17 10:54:17 +00:00
parent 610a76b210
commit 04c1dee7a2
3 changed files with 76 additions and 22 deletions

View File

@ -157,7 +157,7 @@ int main(int argc, char** argv)
int numProcessors = OpenThreads::GetNumberOfProcessors(); int numProcessors = OpenThreads::GetNumberOfProcessors();
int processNum = 0; int processNum = 0;
for(unsigned int i=0; i<osg::GraphicsContext::getMaxContextID(); ++i) for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i)
{ {
osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i);
@ -172,6 +172,6 @@ int main(int argc, char** argv)
} }
} }
viewer.run(); viewer.run();
} }

View File

@ -29,6 +29,8 @@
#include <osgDB/FileUtils> #include <osgDB/FileUtils>
#include <osgDB/ReadFile> #include <osgDB/ReadFile>
#include <osgUtil/GLObjectsVisitor>
#include <osgText/FadeText> #include <osgText/FadeText>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
@ -119,6 +121,8 @@ public:
virtual void operator () (osg::Object* object) virtual void operator () (osg::Object* object)
{ {
// osg::notify(osg::NOTICE)<<"void operator ()"<<std::endl;
Files files; Files files;
readMasterFile(files); readMasterFile(files);
@ -145,6 +149,8 @@ public:
} }
} }
} }
if (newFiles.empty())
// first load the files. // first load the files.
FilenameNodeMap nodesToAdd; FilenameNodeMap nodesToAdd;
@ -152,18 +158,29 @@ public:
nitr != newFiles.end(); nitr != newFiles.end();
++nitr) ++nitr)
{ {
osg::notify(osg::NOTICE)<<"loading files "<<*nitr<<std::endl;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(*nitr); osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(*nitr);
if (loadedModel.get()) nodesToAdd[*nitr] = loadedModel;
} if (loadedModel.get())
{
osg::notify(osg::NOTICE)<<"Loadinged file "<<*nitr<<std::endl;
for(Files::iterator ritr = removedFiles.begin(); nodesToAdd[*nitr] = loadedModel;
ritr != removedFiles.end(); osg::ref_ptr<osgUtil::GLObjectsOperation> compileOperation = new osgUtil::GLObjectsOperation(loadedModel.get());
++ritr)
{
osg::notify(osg::NOTICE)<<"Removed files "<<*ritr<<std::endl;
}
#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 "<<i<<std::endl;
gt->add( compileOperation.get() );
}
}
#endif
}
}
// swap the data. // swap the data.
{ {
@ -180,8 +197,6 @@ public:
void update(osg::Group* scene) void update(osg::Group* scene)
{ {
// osg::notify(osg::NOTICE)<<"update"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (!_nodesToRemove.empty()) if (!_nodesToRemove.empty())
@ -403,6 +418,13 @@ int main(int argc, char** argv)
double w = 1.0; double w = 1.0;
double h = 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> masterOperation; osg::ref_ptr<MasterOperation> masterOperation;
std::string masterFilename; std::string masterFilename;
while(arguments.read("-m",masterFilename)) while(arguments.read("-m",masterFilename))
@ -430,6 +452,27 @@ int main(int argc, char** argv)
operationThread->add(masterOperation.get()); 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()) while (!viewer.done())
{ {
viewer.advance(); viewer.advance();

View File

@ -147,8 +147,14 @@ unsigned int GraphicsContext::createNewContextID()
unsigned int GraphicsContext::getMaxContextID() unsigned int GraphicsContext::getMaxContextID()
{ {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex); OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
unsigned int maxContextID = 0;
return s_contextIDMap.size(); 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->sharedContext = src_gc;
traits->pbuffer = true; traits->pbuffer = true;
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits); osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits);
gc->realize(); if (gc.valid() && gc->realize())
{ {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex); OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
s_contextIDMap[contextID]._compileContext = gc; s_contextIDMap[contextID]._compileContext = gc;
osg::notify(osg::INFO)<<" succeded GraphicsContext::createCompileContext."<<std::endl;
return gc.release();
}
else
{
return 0;
} }
osg::notify(osg::INFO)<<" succeded GraphicsContext::createCompileContext."<<std::endl;
return gc;
} }
void GraphicsContext::setCompileContext(unsigned int contextID, GraphicsContext* gc) void GraphicsContext::setCompileContext(unsigned int contextID, GraphicsContext* gc)
@ -280,9 +289,11 @@ void GraphicsContext::setCompileContext(unsigned int contextID, GraphicsContext*
GraphicsContext* GraphicsContext::getCompileContext(unsigned int contextID) GraphicsContext* GraphicsContext::getCompileContext(unsigned int contextID)
{ {
//osg::notify(osg::NOTICE)<<"GraphicsContext::getCompileContext "<<contextID<<std::endl; // osg::notify(osg::NOTICE)<<"GraphicsContext::getCompileContext "<<contextID<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex); OpenThreads::ScopedLock<OpenThreads::Mutex> 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;
} }