From d57a16e023d0fb57b607d8be544ea7c66809d471 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 11 Aug 2007 14:49:14 +0000 Subject: [PATCH] Added DisplaySetting::s/getCompileContextHint() and use of this hint in osgViewer::Viewer/CompositeViewer. Removed the CustomViewer in osgterrain example as the above now removes the need for it. --- applications/osgviewer/osgviewer.cpp | 27 ----------------- examples/osgterrain/osgterrain.cpp | 40 +++---------------------- include/osg/DisplaySettings | 5 ++++ src/osg/DisplaySettings.cpp | 29 +++++++++++++++++- src/osgDB/Registry.cpp | 2 +- src/osgViewer/CompositeViewer.cpp | 21 +++++++++++++ src/osgViewer/Viewer.cpp | 21 +++++++++++++ src/osgWrappers/osg/DisplaySettings.cpp | 13 ++++++++ 8 files changed, 93 insertions(+), 65 deletions(-) diff --git a/applications/osgviewer/osgviewer.cpp b/applications/osgviewer/osgviewer.cpp index 38f9cf4b4..350b7588a 100644 --- a/applications/osgviewer/osgviewer.cpp +++ b/applications/osgviewer/osgviewer.cpp @@ -74,12 +74,6 @@ int main(int argc, char** argv) return 1; } - bool createBackgroundContextForCompiling = false; - while (arguments.read("--bc")) { createBackgroundContextForCompiling = true; } - - bool createBackgroundThreadsForCompiling = false; - while (arguments.read("--bt")) { createBackgroundContextForCompiling = true; createBackgroundThreadsForCompiling = true; } - // set up the camera manipulators. { osg::ref_ptr keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; @@ -151,27 +145,6 @@ int main(int argc, char** argv) viewer.realize(); - 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; - } - } - } - viewer.run(); } diff --git a/examples/osgterrain/osgterrain.cpp b/examples/osgterrain/osgterrain.cpp index 8e1daee66..1d0d7bc39 100644 --- a/examples/osgterrain/osgterrain.cpp +++ b/examples/osgterrain/osgterrain.cpp @@ -574,47 +574,12 @@ protected: osg::observer_ptr _layer; }; -class CustomViewer : public osgViewer::Viewer -{ -public: - CustomViewer(osg::ArgumentParser& arguments): - Viewer(arguments) {} - - // override the realize to create the compile graphics contexts + threads for us. - virtual void realize() - { - Viewer::realize(); - - - int numProcessors = OpenThreads::GetNumberOfProcessors(); - int processNum = (getThreadingModel()==osgViewer::Viewer::SingleThreaded) ? 1 : 0; - - for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) - { - osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); - - if (gc) - { - gc->createGraphicsThread(); - gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); - gc->getGraphicsThread()->startThread(); - - ++processNum; - } - } - } - -protected: - -}; - - int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc, argv); // construct the viewer. - CustomViewer viewer(arguments); + osgViewer::Viewer viewer(arguments); // set up the camera manipulators. { @@ -934,6 +899,9 @@ int main(int argc, char** argv) } viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); + + // enable the use of compile contexts and associated threads. + osg::DisplaySettings::instance()->setCompileContextsHint(true); // realize the graphics windows. viewer.realize(); diff --git a/include/osg/DisplaySettings b/include/osg/DisplaySettings index b42a458c4..70f4716aa 100644 --- a/include/osg/DisplaySettings +++ b/include/osg/DisplaySettings @@ -174,6 +174,9 @@ class OSG_EXPORT DisplaySettings : public osg::Referenced void setNumMultiSamples(unsigned int samples) { _numMultiSamples = samples; } unsigned int getNumMultiSamples() const { return _numMultiSamples; } bool getMultiSamples() const { return _numMultiSamples!=0; } + + void setCompileContextsHint(bool useCompileContexts) { _compileContextsHint = useCompileContexts; } + bool getCompileContextsHint() const { return _compileContextsHint; } protected: @@ -207,6 +210,8 @@ class OSG_EXPORT DisplaySettings : public osg::Referenced unsigned int _maxNumOfGraphicsContexts; unsigned int _numMultiSamples; + + bool _compileContextsHint; }; diff --git a/src/osg/DisplaySettings.cpp b/src/osg/DisplaySettings.cpp index 40e4ba4c5..ab448dcbf 100644 --- a/src/osg/DisplaySettings.cpp +++ b/src/osg/DisplaySettings.cpp @@ -70,6 +70,8 @@ void DisplaySettings::setDisplaySettings(const DisplaySettings& vs) _maxNumOfGraphicsContexts = vs._maxNumOfGraphicsContexts; _numMultiSamples = vs._numMultiSamples; + + _compileContextsHint = vs._compileContextsHint; } void DisplaySettings::merge(const DisplaySettings& vs) @@ -85,6 +87,9 @@ void DisplaySettings::merge(const DisplaySettings& vs) if (vs._minimumNumberAlphaBits>_minimumNumberAlphaBits) _minimumNumberAlphaBits = vs._minimumNumberAlphaBits; if (vs._minimumNumberStencilBits>_minimumNumberStencilBits) _minimumNumberStencilBits = vs._minimumNumberStencilBits; if (vs._numMultiSamples>_numMultiSamples) _numMultiSamples = vs._numMultiSamples; + + if (vs._compileContextsHint) _compileContextsHint = vs._compileContextsHint; + } void DisplaySettings::setDefaults() @@ -123,6 +128,8 @@ void DisplaySettings::setDefaults() // switch on anti-aliasing by default, just in case we have an Onyx :-) _numMultiSamples = 4; #endif + + _compileContextsHint = false; } void DisplaySettings::setMaxNumberOfGraphicsContexts(unsigned int num) @@ -157,7 +164,7 @@ static ApplicationUsageProxy DisplaySetting_e9(ApplicationUsage::ENVIRONMENTAL_V static ApplicationUsageProxy DisplaySetting_e10(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_AUTO_ADJUST_ASPECT_RATIO ","OFF | ON Default to ON to compenstate for the compression of the aspect ratio when viewing in split screen stereo. Note, if you are setting fovx and fovy explicityly OFF should be used."); static ApplicationUsageProxy DisplaySetting_e11(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_VERTICAL_SEPARATION ","number of pixels between viewports"); static ApplicationUsageProxy DisplaySetting_e12(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MAX_NUMBER_OF_GRAPHICS_CONTEXTS ","maximum number of graphics contexts to be used with applications."); - +static ApplicationUsageProxy DisplaySetting_e13(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_COMPIlE_CONTEXTS ","OFF | ON Enable/disable the use a backgrouind compile contexts and threads."); void DisplaySettings::readEnvironmentalVariables() { const char* ptr = 0; @@ -308,6 +315,19 @@ void DisplaySettings::readEnvironmentalVariables() { _maxNumOfGraphicsContexts = atoi(ptr); } + + if( (ptr = getenv("OSG_COMPIlE_CONTEXTS")) != 0) + { + if (strcmp(ptr,"OFF")==0) + { + _compileContextsHint = false; + } + else + if (strcmp(ptr,"ON")==0) + { + _compileContextsHint = true; + } + } } void DisplaySettings::readCommandLine(ArgumentParser& arguments) @@ -324,6 +344,7 @@ void DisplaySettings::readCommandLine(ArgumentParser& arguments) arguments.getApplicationUsage()->addCommandLineOption("--accum-rgb","Request a rgb accumulator buffer visual"); arguments.getApplicationUsage()->addCommandLineOption("--accum-rgba","Request a rgb accumulator buffer visual"); arguments.getApplicationUsage()->addCommandLineOption("--samples ","Request a multisample visual"); + arguments.getApplicationUsage()->addCommandLineOption("--cc","Request use of compile contexts and threads"); } std::string str; @@ -376,4 +397,10 @@ void DisplaySettings::readCommandLine(ArgumentParser& arguments) { _numMultiSamples = atoi(str.c_str()); } + + while(arguments.read("--cc")) + { + _compileContextsHint = true; + } + } diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index 84012bd2b..c2078ea80 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -160,7 +160,7 @@ Registry::Registry() } addFileExtensionAlias("shadow", "osgShadow"); - + addFileExtensionAlias("terrain", "osgTerrain"); addFileExtensionAlias("view", "osgViewer"); addFileExtensionAlias("sgi", "rgb"); diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index adad489b8..8147a7c17 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -597,6 +597,27 @@ void CompositeViewer::realize() // pass on the start tick to all the associated eventqueues setStartTick(osg::Timer::instance()->getStartTick()); + + if (osg::DisplaySettings::instance()->getCompileContextsHint()) + { + 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) + { + gc->createGraphicsThread(); + gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); + gc->getGraphicsThread()->startThread(); + + ++processNum; + } + } + } + } diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index 733e5260d..96d3a583f 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -935,6 +935,27 @@ void Viewer::realize() setStartTick(osg::Timer::instance()->getStartTick()); setUpThreading(); + + if (osg::DisplaySettings::instance()->getCompileContextsHint()) + { + 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) + { + gc->createGraphicsThread(); + gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); + gc->getGraphicsThread()->startThread(); + + ++processNum; + } + } + } + } diff --git a/src/osgWrappers/osg/DisplaySettings.cpp b/src/osgWrappers/osg/DisplaySettings.cpp index 98b8e9647..77519b8e9 100644 --- a/src/osgWrappers/osg/DisplaySettings.cpp +++ b/src/osgWrappers/osg/DisplaySettings.cpp @@ -329,6 +329,16 @@ BEGIN_OBJECT_REFLECTOR(osg::DisplaySettings) __bool__getMultiSamples, "", ""); + I_Method1(void, setCompileContextsHint, IN, bool, useCompileContexts, + Properties::NON_VIRTUAL, + __void__setCompileContextsHint__bool, + "", + ""); + I_Method0(bool, getCompileContextsHint, + Properties::NON_VIRTUAL, + __bool__getCompileContextsHint, + "", + ""); I_StaticMethod0(osg::DisplaySettings *, instance, __DisplaySettings_P1__instance_S, "Maintain a DisplaySettings singleton for objects to query at runtime. ", @@ -339,6 +349,9 @@ BEGIN_OBJECT_REFLECTOR(osg::DisplaySettings) I_SimpleProperty(bool, AlphaBuffer, __bool__getAlphaBuffer, 0); + I_SimpleProperty(bool, CompileContextsHint, + __bool__getCompileContextsHint, + __void__setCompileContextsHint__bool); I_SimpleProperty(bool, DepthBuffer, __bool__getDepthBuffer, __void__setDepthBuffer__bool);