Show
Ignore:
Timestamp:
08/31/05 12:55:58 (9 years ago)
Author:
robert
Message:

Seperated out the cull and draw traversals into their own operations,
and added a glFinishBarrierOp which can be placed between the cull and the
draw or after swap.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgcamera/osgcamera.cpp

    r4482 r4483  
    5454}; 
    5555 
    56 // Frame operation, that does a cull and draw on the scene graph. 
    57 struct FrameOperation : public osg::GraphicsThread::Operation 
     56// Cull operation, that does a cull on the scene graph. 
     57struct CullOperation : public osg::GraphicsThread::Operation 
    5858{ 
    59     FrameOperation(osg::CameraNode* camera, osg::FrameStamp* frameStamp): 
    60         osg::GraphicsThread::Operation("Frame",true), 
     59    CullOperation(osg::CameraNode* camera, osgUtil::SceneView* sceneView): 
     60        osg::GraphicsThread::Operation("Cull",true), 
    6161        _camera(camera), 
    62         _frameStamp(frameStamp) 
    63     { 
    64         _sceneView = new osgUtil::SceneView; 
    65         _sceneView->setDefaults(); 
    66         _sceneView->setFrameStamp(_frameStamp.get()); 
    67              
    68         if (camera->getNumChildren()>=1) 
    69         { 
    70             _sceneView->setSceneData(camera->getChild(0)); 
    71         } 
     62        _sceneView(sceneView) 
     63    { 
    7264    } 
    7365     
     
    8072         
    8173        _sceneView->cull(); 
     74    } 
     75     
     76    osg::ref_ptr<osg::CameraNode>    _camera; 
     77    osg::ref_ptr<osgUtil::SceneView> _sceneView; 
     78}; 
     79 
     80// Draw operation, that does a draw on the scene graph. 
     81struct DrawOperation : public osg::GraphicsThread::Operation 
     82{ 
     83    DrawOperation(osgUtil::SceneView* sceneView): 
     84        osg::GraphicsThread::Operation("Draw",true), 
     85        _sceneView(sceneView) 
     86    { 
     87    } 
     88     
     89    virtual void operator () (osg::GraphicsContext*) 
     90    { 
    8291        _sceneView->draw(); 
    8392    } 
    8493     
    85     osg::ref_ptr<osg::CameraNode>    _camera; 
    86     osg::ref_ptr<osg::FrameStamp>    _frameStamp; 
    8794    osg::ref_ptr<osgUtil::SceneView> _sceneView; 
    8895}; 
     
    218225    { 
    219226        osg::GraphicsContext* context = *gitr; 
    220         context->getGraphicsThread()->add(compileOp.get(), true); 
     227        context->getGraphicsThread()->add(compileOp.get(), false); 
    221228    } 
    222229 
     
    232239    } 
    233240 
     241    osg::ref_ptr<osg::BarrierOperation> glFinishBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FINISH); 
     242 
     243    // we can put a finish in to gate rendering throughput, so that each new frame starts with a clean sheet. 
     244    // you should only enable one of these, doFinishBeforeNewDraw will allow for the better parallism of the two finish approaches 
     245    bool doFinishBeforeNewDraw = true; 
     246    bool doFinishAfterSwap = false; 
     247 
    234248    // third add the frame for each camera. 
    235249    for(citr = cameraList.begin(); 
     
    238252    { 
    239253        osg::CameraNode* camera = citr->get(); 
    240         camera->getGraphicsContext()->getGraphicsThread()->add( new FrameOperation(camera, frameStamp.get()), false);  
     254         
     255        // create a scene view to do the cull and draw 
     256        osgUtil::SceneView* sceneView = new osgUtil::SceneView; 
     257        sceneView->setDefaults(); 
     258        sceneView->setFrameStamp(frameStamp.get()); 
     259             
     260        if (camera->getNumChildren()>=1) 
     261        { 
     262            sceneView->setSceneData(camera->getChild(0)); 
     263        } 
     264 
     265        // cull traversal operation 
     266        camera->getGraphicsContext()->getGraphicsThread()->add( new CullOperation(camera, sceneView), false);  
     267 
     268        // optionally add glFinish barrier to ensure that all OpenGL commands are completed before we start dispatching a new frame 
     269        if (doFinishBeforeNewDraw) camera->getGraphicsContext()->getGraphicsThread()->add( glFinishBarrierOp.get(), false);  
     270 
     271        // draw traversal operation. 
     272        camera->getGraphicsContext()->getGraphicsThread()->add( new DrawOperation(sceneView), false);  
    241273    } 
    242274 
     
    246278    // are held back until they have all dispatched their fifo to the graphics hardware.   
    247279    // 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. 
    249280    osg::ref_ptr<osg::BarrierOperation> frameEndBarrierOp = new osg::BarrierOperation(graphicsContextSet.size()+1, osg::BarrierOperation::NO_OPERATION); 
    250281    osg::ref_ptr<osg::BarrierOperation> preSwapBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FLUSH); 
    251282    osg::ref_ptr<osg::SwapBuffersOperation> swapOp = new osg::SwapBuffersOperation(); 
    252     osg::ref_ptr<osg::BarrierOperation> postSwapBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FINISH); 
    253     bool waitTillSwapBufferFinished = false; 
    254283    for(gitr = graphicsContextSet.begin(); 
    255284        gitr != graphicsContextSet.end(); 
     
    257286    { 
    258287        osg::GraphicsContext* context = *gitr; 
     288 
    259289        context->getGraphicsThread()->add(frameEndBarrierOp.get(), false); 
    260290        context->getGraphicsThread()->add(preSwapBarrierOp.get(), false); 
    261291        context->getGraphicsThread()->add(swapOp.get(), false); 
    262         if (waitTillSwapBufferFinished) context->getGraphicsThread()->add(postSwapBarrierOp.get(), false); 
     292         
     293        // optionally add finish barrier to ensure that we don't do any other graphics work till the current OpenGL commands are complete. 
     294        if (doFinishAfterSwap) context->getGraphicsThread()->add(glFinishBarrierOp.get(), false); 
    263295    } 
    264296