| | 90 | |
| | 91 | // main does the following steps to create a multi-thread, multiple camera/graphics context view of a scene graph. |
| | 92 | // |
| | 93 | // 1) load the scene graph |
| | 94 | // |
| | 95 | // 2) create a list of camera, each with their own graphis context, with a graphics thread for each context. |
| | 96 | // |
| | 97 | // 3) set up the graphic threads so that the do an initial compile OpenGL objects operation, this is done once, and then this compile op is disgarded |
| | 98 | // |
| | 99 | // 4) set up the graphics thread so that it has all the graphics ops required for the main loop, these ops are: |
| | 100 | // 4.a) frame begin barrair, syncronizes all the waiting graphic threads so they don't run while update is occuring |
| | 101 | // 4.b) frame operation - the cull and draw for each camera |
| | 102 | // 4.c) frame end barrier, releases the update thread once all graphic threads have dispatched all their OpenGL commands |
| | 103 | // 4.d) pre swap barrier, barrier which ensures that all graphics threads have sent their data down to the gfx card. |
| | 104 | // 4.e) swap buffers, do the swap buffers on all the graphics contexts. |
| | 105 | // |
| | 106 | // 5. The main loop: |
| | 107 | // 5.a) update |
| | 108 | // 5.b) join the frame begin barrrier, releasing all the graphics threads to do their stuff |
| | 109 | // 5.c) block on the frame end barrier, waiting till all the graphics threads have done their cull/draws. |
| | 110 | // 5.d) check to see if any of the windows has been closed. |
| | 111 | // |
| | 226 | for(gitr = graphicsContextSet.begin(); |
| | 227 | gitr != graphicsContextSet.end(); |
| | 228 | ++gitr) |
| | 229 | { |
| | 230 | osg::GraphicsContext* context = *gitr; |
| | 231 | context->getGraphicsThread()->add(frameBeginBarrierOp.get(), false); |
| | 232 | } |
| | 233 | |
| | 234 | // third add the frame for each camera. |
| | 235 | for(citr = cameraList.begin(); |
| | 236 | citr != cameraList.end(); |
| | 237 | ++citr) |
| | 238 | { |
| | 239 | osg::CameraNode* camera = citr->get(); |
| | 240 | camera->getGraphicsContext()->getGraphicsThread()->add( new FrameOperation(camera, frameStamp.get()), false); |
| | 241 | } |
| | 242 | |
| | 243 | // fourth add the frame end barrier, the pre swap barrier and finally the swap buffers to each graphics thread. |
| | 244 | // The frame end barrier tells the main thead that the draw dispatch/read phase of the scene graph is complete. |
| | 245 | // The pre swap barrier is an optional extra, which does a flush before joining the barrier, using this all graphics threads |
| | 246 | // are held back until they have all dispatched their fifo to the graphics hardware. |
| | 247 | // The swapOp just issues a swap buffers for each of the graphics contexts. |
| | 248 | // The post swap barrier is an optional extra which does a glFinish after the swap buffers. |
| 188 | | |
| 189 | | std::cout<<"nubmer of gfx."<<graphicsContextSet.size()<<std::endl; |
| 190 | | |
| 191 | | // record the timer tick at the start of rendering. |
| 192 | | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
| 193 | | osg::Timer_t previous_tick = start_tick; |
| 194 | | |
| 195 | | bool done = false; |
| 196 | | |
| 197 | | // first the compile of the GL Objects, do it syncronously. |
| 198 | | GraphicsContextSet::iterator gitr; |
| 199 | | for(gitr = graphicsContextSet.begin(); |
| 200 | | gitr != graphicsContextSet.end(); |
| 201 | | ++gitr) |
| 202 | | { |
| 203 | | osg::GraphicsContext* context = *gitr; |
| 204 | | context->getGraphicsThread()->add(compileOp.get(), true); |
| 205 | | } |
| 206 | | |
| 207 | | |
| 208 | | // second the begin frame barrier to all graphics threads |
| 209 | | for(gitr = graphicsContextSet.begin(); |
| 210 | | gitr != graphicsContextSet.end(); |
| 211 | | ++gitr) |
| 212 | | { |
| 213 | | osg::GraphicsContext* context = *gitr; |
| 214 | | context->getGraphicsThread()->add(frameBeginBarrierOp.get(), false); |
| 215 | | } |
| 216 | | |
| 217 | | // third add the frame for each camera. |
| 218 | | for(citr = cameraMap.begin(); |
| 219 | | citr != cameraMap.end(); |
| 220 | | ++citr) |
| 221 | | { |
| 222 | | osg::CameraNode* camera = const_cast<osg::CameraNode*>(citr->first.get()); |
| 223 | | camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false); |
| 224 | | } |
| 225 | | |
| 226 | | // fourth add the frame end barrier, the pre swap barrier and finally the swap buffers to each graphics thread |
| | 252 | osg::ref_ptr<osg::BarrierOperation> postSwapBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FINISH); |
| | 253 | bool waitTillSwapBufferFinished = false; |