Changeset 6097
- Timestamp:
- 02/06/07 12:03:13
- Files:
-
- OpenSceneGraph/trunk/include/osgViewer/Viewer (modified) (4 diffs)
- OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
OpenSceneGraph/trunk/include/osgViewer/Viewer
r6088 r6097 134 134 void getCameras(Cameras& cameras, bool onlyActive=true); 135 135 136 typedef std::vector<OpenThreads::Thread*> Threads; 137 void getAllThreads(Threads& threads, bool onlyActive=true); 138 139 typedef std::vector<osg::OperationsThread*> OperationsThreads; 140 void getOperationsThreads(OperationsThreads& threads, bool onlyActive=true); 141 136 142 /** Set the graphics operation to call on realization of the viewers graphics windows.*/ 137 143 void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; } … … 145 151 /** Start any threads required by the viewer, as per viewers ThreadingModel.*/ 146 152 void startThreading(); 153 154 147 155 148 156 /** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/ … … 153 161 void checkWindowStatus(); 154 162 163 inline void makeCurrent(osg::GraphicsContext* gc) 164 { 165 if (_currentContext==gc) return; 166 167 releaseContext(); 168 169 if (gc && gc->makeCurrent()) _currentContext = gc; 170 } 171 172 inline void releaseContext() 173 { 174 if (_currentContext.valid()) 175 { 176 _currentContext->releaseContext(); 177 _currentContext = 0; 178 } 179 } 180 155 181 bool _firstFrame; 156 182 … … 184 210 185 211 osg::ref_ptr<osg::Operation> _realizeOperation; 212 213 osg::observer_ptr<osg::GraphicsContext> _currentContext; 186 214 }; 187 215 OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp
r6095 r6097 136 136 } 137 137 138 osg::Camera* getCamera() { return _sceneView->getCamera(); } 139 138 140 virtual void operator () (osg::Object*) 139 141 { … … 243 245 } 244 246 247 osg::Camera* getCamera() { return _sceneView[0]->getCamera(); } 248 245 249 void setGraphicsThreadDoesCull(bool flag) 246 250 { … … 491 495 { 492 496 osg::GraphicsContext* context = dynamic_cast<osg::GraphicsContext*>(object); 493 if (!context) return; 497 if (!context) 498 { 499 osg::Camera* camera = dynamic_cast<osg::Camera*>(object); 500 if (camera) cull(); 501 return; 502 } 494 503 495 504 //osg::notify(osg::NOTICE)<<"GraphicsCall "<<std::endl; … … 533 542 _keyEventSetsDone(osgGA::GUIEventAdapter::KEY_Escape), 534 543 _quitEventSetsDone(true), 535 //_threadingModel(SingleThreaded),536 _threadingModel(ThreadPerContext),544 _threadingModel(SingleThreaded), 545 // _threadingModel(ThreadPerContext), 537 546 // _threadingModel(ThreadPerCamera), 538 547 _threadsRunning(false), 539 _useMainThreadForRenderingTraversal( false),548 _useMainThreadForRenderingTraversal(true), 540 549 _endBarrierPosition(AfterSwapBuffers), 541 550 _numThreadsOnBarrier(0) … … 558 567 //osg::notify(osg::NOTICE)<<"Viewer::~Viewer()"<<std::endl; 559 568 569 570 Threads threads; 571 getAllThreads(threads); 572 573 osg::notify(osg::NOTICE)<<"start destrcutor getThreads = "<<threads.size()<<std::endl; 574 575 560 576 stopThreading(); 561 577 … … 584 600 //osg::notify(osg::NOTICE)<<"finish Viewer::~Viewer()"<<std::endl; 585 601 602 getAllThreads(threads); 603 604 osg::notify(osg::NOTICE)<<"end destrcutor getThreads = "<<threads.size()<<std::endl; 605 586 606 } 587 607 … … 715 735 if (!_threadsRunning) return; 716 736 717 osg::notify(osg:: INFO)<<"Viewer::stopThreading() - stopping threading"<<std::endl;737 osg::notify(osg::NOTICE)<<"Viewer::stopThreading() - stopping threading"<<std::endl; 718 738 719 739 Contexts contexts; 720 740 getContexts(contexts); 721 741 722 Contexts::iterator citr; 742 Cameras cameras; 743 getCameras(cameras); 744 745 Contexts::iterator gcitr; 746 Cameras::iterator citr; 723 747 724 748 // reset any double buffer graphics objects 725 for( citr = contexts.begin();726 citr != contexts.end();727 ++ citr)728 { 729 osg::GraphicsContext* gc = (* citr);749 for(gcitr = contexts.begin(); 750 gcitr != contexts.end(); 751 ++gcitr) 752 { 753 osg::GraphicsContext* gc = (*gcitr); 730 754 731 755 OpenThreads::ScopedLock<OpenThreads::Mutex> lock( *(gc->getOperationsMutex()) ); … … 745 769 746 770 // delete all the graphics threads. 747 for(Contexts::iterator itr = contexts.begin(); 748 itr != contexts.end(); 749 ++itr) 750 { 751 (*itr)->setGraphicsThread(0); 771 for(gcitr = contexts.begin(); 772 gcitr != contexts.end(); 773 ++gcitr) 774 { 775 (*gcitr)->setGraphicsThread(0); 776 } 777 778 // delete all the camera threads. 779 for(citr = cameras.begin(); 780 citr != cameras.end(); 781 ++citr) 782 { 783 (*citr)->setCameraThread(0); 752 784 } 753 785 754 786 // reset any double buffer graphics objects 755 for( citr = contexts.begin();756 citr != contexts.end();757 ++ citr)758 { 759 osg::GraphicsContext* gc = (* citr);787 for(gcitr = contexts.begin(); 788 gcitr != contexts.end(); 789 ++gcitr) 790 { 791 osg::GraphicsContext* gc = (*gcitr); 760 792 761 793 OpenThreads::ScopedLock<OpenThreads::Mutex> lock( *(gc->getOperationsMutex()) ); … … 780 812 _numThreadsOnBarrier = 0; 781 813 _endDynamicDrawBlock = 0; 782 osg::notify(osg:: INFO)<<"Viewer::stopThreading() - stopped threading :-)"<<std::endl;814 osg::notify(osg::NOTICE)<<"Viewer::stopThreading() - stopped threading :-)"<<std::endl; 783 815 } 784 816 … … 834 866 unsigned int Viewer::computeNumberOfThreadsIncludingMainRequired() 835 867 { 868 unsigned int numThreadsOnBarrier = 1; 869 switch(_threadingModel) 870 { 871 case(SingleThreaded): 872 numThreadsOnBarrier = 1; 873 break; 874 case(ThreadPerContext): 875 { 876 Contexts contexts; 877 getContexts(contexts); 878 numThreadsOnBarrier = contexts.size()+1; 879 break; 880 } 881 case(ThreadPerCamera): 882 { 883 Cameras cameras; 884 getCameras(cameras); 885 numThreadsOnBarrier = _useMainThreadForRenderingTraversal ? cameras.size() : cameras.size()+1; 886 break; 887 } 888 } 889 return numThreadsOnBarrier; 890 } 891 892 void Viewer::startThreading() 893 { 894 if (_threadsRunning) return; 895 896 osg::notify(osg::INFO)<<"Viewer::startThreading() - starting threading"<<std::endl; 897 898 // release any context held by the main thread. 899 releaseContext(); 900 836 901 Contexts contexts; 837 902 getContexts(contexts); 838 903 839 if (contexts.empty()) return 0; 840 841 if (_threadingModel==SingleThreaded) 842 { 843 return 1; 844 } 845 846 return _useMainThreadForRenderingTraversal ? contexts.size() : contexts.size()+1; 847 } 848 849 void Viewer::startThreading() 850 { 851 if (_threadsRunning) return; 852 853 osg::notify(osg::INFO)<<"Starting threading"<<std::endl; 854 855 unsigned int numThreadsIncludingMainThread = computeNumberOfThreadsIncludingMainRequired(); 856 857 // return if we don't need multiple threads. 858 // if (numThreadsIncludingMainThread <= 1) return; 859 860 // return if threading is already up and running 861 #if 0 862 if (numThreadsIncludingMainThread == _numThreadsOnBarrier) 863 { 864 osg::notify(osg::NOTICE)<<"Thread already started?"<<std::endl; 865 return; 866 } 867 868 if (_numThreadsOnBarrier!=0) 869 { 870 // we already have threads running but not the right number, so stop them and then create new threads. 871 stopThreading(); 872 } 873 #endif 874 875 osg::notify(osg::INFO)<<"Viewer::startThreading() - starting threading"<<std::endl; 904 Cameras cameras; 905 getCameras(cameras); 906 907 _numThreadsOnBarrier = 0; 908 switch(_threadingModel) 909 { 910 case(SingleThreaded): 911 _numThreadsOnBarrier = 1; 912 return; 913 case(ThreadPerContext): 914 _numThreadsOnBarrier = contexts.size()+1; 915 break; 916 case(ThreadPerCamera): 917 _numThreadsOnBarrier = _useMainThreadForRenderingTraversal ? cameras.size() : cameras.size()+1; 918 break; 919 } 876 920 877 921 // using multi-threading so make sure that new objects are allocated with thread safe ref/unref … … 888 932 getSceneData()->resizeGLObjectBuffers(osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts()); 889 933 } 890 891 892 Contexts contexts; 893 getContexts(contexts); 894 895 934 935 osg::notify(osg::NOTICE)<<"_numThreadsOnBarrier = "<<_numThreadsOnBarrier<<std::endl; 936 896 937 897 938 int numProcessors = OpenThreads::GetNumberOfProcessors(); 898 bool affinity = true; 899 900 bool firstContextAsMainThread = numThreadsIncludingMainThread==contexts.size(); 901 902 // osg::notify(osg::NOTICE)<<"numThreadsIncludingMainThread=="<<numThreadsIncludingMainThread<<std::endl; 903 904 _numThreadsOnBarrier = numThreadsIncludingMainThread; 905 906 939 bool affinity = false;//true; 940 907 941 Contexts::iterator citr; 908 942 … … 935 969 if (_threadingModel==ThreadPerContext) 936 970 { 937 _startRenderingBarrier = new osg::BarrierOperation(_numThreadsOnBarrier, osg::BarrierOperation::NO_OPERATION); 938 939 #if 1 940 _endRenderingDispatchBarrier = new osg::BarrierOperation(_numThreadsOnBarrier, osg::BarrierOperation::NO_OPERATION); 941 #else 942 _endRenderingDispatchBarrier = new osg::BarrierOperation(_numThreadsOnBarrier, osg::BarrierOperation::GL_FINISH); 943 #endif 971 _startRenderingBarrier = 0; 972 _endRenderingDispatchBarrier = 0; 944 973 _endDynamicDrawBlock = 0; 945 974 } … … 950 979 _endDynamicDrawBlock = new EndOfDynamicDrawBlock(numViewerDoubleBufferedRenderingOperation); 951 980 } 981 982 if (_numThreadsOnBarrier>1) 983 { 984 _startRenderingBarrier = new osg::BarrierOperation(_numThreadsOnBarrier, osg::BarrierOperation::NO_OPERATION); 985 _endRenderingDispatchBarrier = new osg::BarrierOperation(_numThreadsOnBarrier, osg::BarrierOperation::NO_OPERATION); 986 } 987 988 989 osg::ref_ptr<osg::BarrierOperation> swapReadyBarrier = contexts.empty() ? 0 : new osg::BarrierOperation(contexts.size(), osg::BarrierOperation::NO_OPERATION); 952 990 953 991 osg::ref_ptr<osg::SwapBuffersOperation> swapOp = new osg::SwapBuffersOperation(); 954 992 955 citr = contexts.begin(); 956 unsigned int processNum = 0; 957 958 if (firstContextAsMainThread) 959 { 960 ++processNum; 961 ++citr; 962 } 963 964 for(; 993 unsigned int processNum = 1; 994 for(citr = contexts.begin(); 965 995 citr != contexts.end(); 966 ++citr, 967 ++processNum) 996 ++citr, ++processNum) 968 997 { 969 998 osg::GraphicsContext* gc = (*citr); … … 974 1003 gc->createGraphicsThread(); 975 1004 1005 #if 1 976 1006 if (affinity) gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); 1007 #else 1008 if (affinity) gc->getGraphicsThread()->setProcessorAffinity(1); 1009 #endif 977 1010 978 1011 gc->getGraphicsThread()->add(new ViewerCompileOperation(getSceneData())); 979 1012 980 1013 // add the startRenderingBarrier 981 if (_ startRenderingBarrier.valid()) gc->getGraphicsThread()->add(_startRenderingBarrier.get());1014 if (_threadingModel==ThreadPerContext && _startRenderingBarrier.valid()) gc->getGraphicsThread()->add(_startRenderingBarrier.get()); 982 1015 983 1016 // add the rendering operation itself. 984 1017 gc->getGraphicsThread()->add(new ViewerRunOperations()); 985 1018 986 if (_ endBarrierPosition==BeforeSwapBuffers && _endRenderingDispatchBarrier.valid())1019 if (_threadingModel==ThreadPerContext && _endBarrierPosition==BeforeSwapBuffers && _endRenderingDispatchBarrier.valid()) 987 1020 { 988 1021 // add the endRenderingDispatchBarrier … … 990 1023 } 991 1024 1025 if (swapReadyBarrier.valid()) gc->getGraphicsThread()->add(swapReadyBarrier.get()); 1026 992 1027 // add the swap buffers 993 1028 gc->getGraphicsThread()->add(swapOp.get()); 994 1029 995 if (_ endBarrierPosition==AfterSwapBuffers && _endRenderingDispatchBarrier.valid())1030 if (_threadingModel==ThreadPerContext && _endBarrierPosition==AfterSwapBuffers && _endRenderingDispatchBarrier.valid()) 996 1031 { 997 1032 // add the endRenderingDispatchBarrier … … 999 1034 } 1000 1035 1001 } 1002 1003 if (firstContextAsMainThread) 1004 { 1005 if (affinity) OpenThreads::SetProcessorAffinityOfCurrentThread(0); 1006 } 1036 1037 1038 } 1039 1040 1041 if (_threadingModel==ThreadPerCamera && _numThreadsOnBarrier>1) 1042 { 1043 Cameras::iterator camItr = cameras.begin(); 1044 if (_useMainThreadForRenderingTraversal) ++camItr; 1045 1046 for(; 1047 camItr != cameras.end(); 1048 ++camItr, ++processNum) 1049 { 1050 osg::Camera* camera = *camItr; 1051 camera->createCameraThread(); 1052 1053 #if 0 1054 if (affinity) camera->getCameraThread()->setProcessorAffinity(processNum % numProcessors); 1055 #else 1056 if (affinity) camera->getCameraThread()->setProcessorAffinity(1); 1057 #endif 1058 osg::GraphicsContext* gc = camera->getGraphicsContext(); 1059 1060 // add the startRenderingBarrier 1061 if (_startRenderingBarrier.valid()) camera->getCameraThread()->add(_startRenderingBarrier.get()); 1062 1063 OpenThreads::ScopedLock<OpenThreads::Mutex> lock( *(gc->getOperationsMutex()) ); 1064 osg::GraphicsContext::OperationQueue& operations = gc->getOperationsQueue(); 1065 for(osg::GraphicsContext::OperationQueue::iterator oitr = operations.begin(); 1066 oitr != operations.end(); 1067 ++oitr) 1068 { 1069 ViewerDoubleBufferedRenderingOperation* vdbro = dynamic_cast<ViewerDoubleBufferedRenderingOperation*>(oitr->get()); 1070 if (vdbro && vdbro->getCamera()==camera) 1071 { 1072 camera->getCameraThread()->add(vdbro); 1073 } 1074 } 1075 1076 if (_endRenderingDispatchBarrier.valid()) 1077 { 1078 // add the endRenderingDispatchBarrier 1079 gc->getGraphicsThread()->add(_endRenderingDispatchBarrier.get()); 1080 } 1081 1082 } 1083 1084 for(camItr = cameras.begin(); 1085 camItr != cameras.end(); 1086 ++camItr, ++processNum) 1087 { 1088 osg::Camera* camera = *camItr; 1089 if (camera->getCameraThread() && !camera->getCameraThread()->isRunning()) 1090 { 1091 osg::notify(osg::INFO)<<" camera->getCameraThread()-> "<<camera->getCameraThread()<<std::endl; 1092 camera->getCameraThread()->startThread(); 1093 } 1094 } 1095 } 1096 1097 if (affinity) OpenThreads::SetProcessorAffinityOfCurrentThread(0); 1007 1098 1008 1099 for(citr = contexts.begin(); … … 1093 1184 cameras.clear(); 1094 1185 1095 if ( onlyActive || _camera->getGraphicsContext()) cameras.push_back(_camera.get());1186 if (!onlyActive || _camera->getGraphicsContext()) cameras.push_back(_camera.get()); 1096 1187 1097 1188 for(Slaves::iterator itr = _slaves.begin(); … … 1099 1190 ++itr) 1100 1191 { 1101 if (onlyActive || itr->_camera->getGraphicsContext()) cameras.push_back(itr->_camera.get()); 1102 } 1103 1192 if (!onlyActive || itr->_camera->getGraphicsContext()) cameras.push_back(itr->_camera.get()); 1193 } 1194 1195 } 1196 1197 void Viewer::getAllThreads(Threads& threads, bool onlyActive) 1198 { 1199 OperationsThreads operationsThreads; 1200 getOperationsThreads(operationsThreads); 1201 1202 for(OperationsThreads::iterator itr = operationsThreads.begin(); 1203 itr != operationsThreads.end(); 1204 ++itr) 1205 { 1206 threads.push_back(*itr); 1207 } 1208 1209 if (_scene.valid() && 1210 _scene->getDatabasePager() && 1211 (!onlyActive || _scene->getDatabasePager()->isRunning())) 1212 { 1213 threads.push_back(_scene->getDatabasePager()); 1214 } 1215 } 1216 1217 1218 void Viewer::getOperationsThreads(OperationsThreads& threads, bool onlyActive) 1219 { 1220 threads.clear(); 1221 1222 Contexts contexts; 1223 getContexts(contexts); 1224 for(Contexts::iterator gcitr = contexts.begin(); 1225 gcitr != contexts.end(); 1226 ++gcitr) 1227 { 1228 osg::GraphicsContext* gc = *gcitr; 1229 if (gc->getGraphicsThread() && 1230 (!onlyActive || gc->getGraphicsThread()->isRunning()) ) 1231 { 1232 threads.push_back(gc->getGraphicsThread()); 1233 } 1234 } 1235 1236 Cameras cameras; 1237 getCameras(cameras); 1238 for(Cameras::iterator citr = cameras.begin(); 1239 citr != cameras.end(); 1240 ++citr) 1241 { 1242 osg::Camera* camera = *citr; 1243 if (camera->getCameraThread() && 1244 (!onlyActive || camera->getCameraThread()->isRunning()) ) 1245 { 1246 threads.push_back(camera->getCameraThread()); 1247 } 1248 } 1249 1104 1250 } 1105 1251 … … 1741 1887 if (vdbro) 1742 1888 { 1743 if (!vdbro->getGraphicsThreadDoesCull() )1889 if (!vdbro->getGraphicsThreadDoesCull() && !(vdbro->getCamera()->getCameraThread())) 1744 1890 { 1745 1891 vdbro->cull(); … … 1747 1893 } 1748 1894 } 1749 1750 1895 } 1751 1896 … … 1757 1902 if (!((*itr)->getGraphicsThread())) 1758 1903 { 1759 (*itr)->makeCurrent();1904 makeCurrent(*itr); 1760 1905 (*itr)->runOperations(); 1761 (*itr)->releaseContext();1762 1906 } 1763 1907 } 1764 1908 1765 1909 // osg::notify(osg::NOTICE)<<"Joing _endRenderingDispatchBarrier block"<<std::endl; 1910 1911 // wait till the rendering dispatch is done. 1912 if (_endRenderingDispatchBarrier.valid()) _endRenderingDispatchBarrier->block(); 1913 1914 for(itr = contexts.begin(); 1915 itr != contexts.end(); 1916 ++itr) 1917 { 1918 if (_done) return; 1919 1920 if (!((*itr)->getGraphicsThread())) 1921 { 1922 makeCurrent(*itr); 1923 (*itr)->swapBuffers(); 1924 } 1925 } 1926 1927 if (dp) 1928 { 1929 dp->signalEndFrame(); 1930 } 1766 1931 1767 1932 // wait till the dynamic draw is complete. … … 1773 1938 } 1774 1939 1775 // wait till the rendering dispatch is done.1776 if (_endRenderingDispatchBarrier.valid()) _endRenderingDispatchBarrier->block();1777 1778 for(itr = contexts.begin();1779 itr != contexts.end();1780 ++itr)1781 {1782 if (_done) return;1783 1784 if (!((*itr)->getGraphicsThread()))1785 {1786 (*itr)->makeCurrent();1787 (*itr)->swapBuffers();1788 (*itr)->releaseContext();1789 }1790 }1791 1792 if (dp)1793 {1794 dp->signalEndFrame();1795 }1796 1940 1797 1941 if (getStats() && getStats()->collectStats("update"))
