| | 32 | |
| | 33 | struct CompileOperation : public osg::GraphicsThread::Operation |
| | 34 | { |
| | 35 | CompileOperation(osg::Node* scene): |
| | 36 | osg::GraphicsThread::Operation("Compile",false), |
| | 37 | _scene(scene) |
| | 38 | { |
| | 39 | } |
| | 40 | |
| | 41 | virtual void operator () (osg::GraphicsContext* context) |
| | 42 | { |
| | 43 | std::cout<<"Compile"<<std::endl; |
| | 44 | |
| | 45 | osgUtil::GLObjectsVisitor compileVisitor; |
| | 46 | compileVisitor.setState(context->getState()); |
| | 47 | |
| | 48 | // do the compile traversal |
| | 49 | _scene->accept(compileVisitor); |
| | 50 | } |
| | 51 | |
| | 52 | osg::ref_ptr<osg::Node> _scene; |
| | 53 | }; |
| 165 | | // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) |
| | 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 |
| | 227 | for(gitr = graphicsContextSet.begin(); |
| | 228 | gitr != graphicsContextSet.end(); |
| | 229 | ++gitr) |
| | 230 | { |
| | 231 | osg::GraphicsContext* context = *gitr; |
| | 232 | context->getGraphicsThread()->add(frameEndBarrierOp.get(), false); |
| | 233 | context->getGraphicsThread()->add(preSwapBarrierOp.get(), false); |
| | 234 | context->getGraphicsThread()->add(swapOp.get(), false); |
| | 235 | } |
| | 236 | |
| | 237 | // main loop - update scene graph, dispatch frame, wait for frame done. |
| 181 | | // issue the frame for each camera. |
| 182 | | for(citr = cameraMap.begin(); |
| 183 | | citr != cameraMap.end(); |
| 184 | | ++citr) |
| 185 | | { |
| 186 | | osg::CameraNode* camera = const_cast<osg::CameraNode*>(citr->first.get()); |
| 187 | | camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false); |
| 188 | | } |
| 189 | | |
| 190 | | GraphicsContextSet::iterator gitr; |
| 191 | | for(gitr = graphicsContextSet.begin(); |
| 192 | | gitr != graphicsContextSet.end(); |
| 193 | | ++gitr) |
| 194 | | { |
| 195 | | osg::GraphicsContext* context = *gitr; |
| 196 | | context->getGraphicsThread()->add(frameEndBarrierOp.get(), false); |
| 197 | | context->getGraphicsThread()->add(preSwapBarrierOp.get(), false); |
| 198 | | } |
| 199 | | |
| 200 | | osg::notify(osg::INFO)<<"Join frameEndBarrierOp block "<<std::endl; |
| 201 | | osg::Timer_t before_tick = osg::Timer::instance()->tick(); |
| | 253 | // dispatch the frame. |
| | 254 | frameBeginBarrierOp->block(); |
| | 255 | |
| | 256 | // wait till the frame is done. |
| 203 | | osg::Timer_t after_tick = osg::Timer::instance()->tick(); |
| 204 | | osg::notify(osg::INFO)<<"Leave frameEndBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl; |
| 205 | | |
| 206 | | osg::notify(osg::INFO)<<"Join preSwapBarrierOp block "<<std::endl; |
| 207 | | before_tick = osg::Timer::instance()->tick(); |
| 208 | | after_tick = osg::Timer::instance()->tick(); |
| 209 | | osg::notify(osg::INFO)<<"Leave preSwapBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl; |
| 210 | | |
| 211 | | for(gitr = graphicsContextSet.begin(); |
| 212 | | gitr != graphicsContextSet.end(); |
| 213 | | ++gitr) |
| 214 | | { |
| 215 | | osg::GraphicsContext* context = *gitr; |
| 216 | | context->getGraphicsThread()->add(swapOp.get(), true); |
| 217 | | } |