Changeset 13041 for OpenSceneGraph/trunk/src/osgUtil/RenderStage.cpp
- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
-
OpenSceneGraph/trunk/src/osgUtil/RenderStage.cpp (modified) (55 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgUtil/RenderStage.cpp
r12597 r13041 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 5 * (at your option) any later version. The full license is in LICENSE file 6 6 * included with this distribution, and on the openscenegraph.org website. 7 * 7 * 8 8 * This library is distributed in the hope that it will be useful, 9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 11 * OpenSceneGraph Public License for more details. 12 12 */ … … 55 55 _cameraRequiresSetUp = false; 56 56 _camera = 0; 57 57 58 58 _level = 0; 59 59 _face = 0; 60 60 61 61 _imageReadPixelFormat = GL_RGBA; 62 62 _imageReadPixelDataType = GL_UNSIGNED_BYTE; … … 85 85 _cameraRequiresSetUp = false; 86 86 _camera = 0; 87 87 88 88 _level = 0; 89 89 _face = 0; 90 90 91 91 _imageReadPixelFormat = GL_RGBA; 92 92 _imageReadPixelDataType = GL_UNSIGNED_BYTE; … … 129 129 { 130 130 _stageDrawnThisFrame = false; 131 131 132 132 if (_renderStageLighting.valid()) _renderStageLighting->reset(); 133 133 … … 210 210 { 211 211 if (_preRenderList.empty()) return; 212 212 213 213 //cout << "Drawing prerendering stages "<<this<< " "<<_viewport->x()<<","<< _viewport->y()<<","<< _viewport->width()<<","<< _viewport->height()<<std::endl; 214 214 for(RenderStageList::iterator itr=_preRenderList.begin(); … … 227 227 228 228 if (!_camera) return; 229 229 230 230 osg::State& state = *renderInfo.getState(); 231 231 … … 250 250 depth = osg::maximum(depth,itr->second.depth()); 251 251 } 252 252 253 253 // OSG_NOTICE<<"RenderStage::runCameraSetUp viewport "<<_viewport->x()<<" "<<_viewport->y()<<" "<<_viewport->width()<<" "<<_viewport->height()<<std::endl; 254 254 // OSG_NOTICE<<"RenderStage::runCameraSetUp computed "<<width<<" "<<height<<" "<<depth<<std::endl; … … 280 280 _bufferAttachmentMap[itr->first]._image = image; 281 281 } 282 282 283 283 if (itr->second._texture.valid()) 284 284 { … … 336 336 } 337 337 } 338 338 339 339 if (renderTargetImplementation==osg::Camera::FRAME_BUFFER_OBJECT) 340 340 { 341 341 osg::FBOExtensions* fbo_ext = osg::FBOExtensions::instance(state.getContextID(),true); 342 342 bool fbo_supported = fbo_ext && fbo_ext->isSupported(); 343 343 344 344 if (fbo_supported) 345 345 { … … 350 350 osg::ref_ptr<osg::FrameBufferObject> fbo = new osg::FrameBufferObject; 351 351 osg::ref_ptr<osg::FrameBufferObject> fbo_multisample; 352 352 353 353 bool colorAttached = false; 354 354 bool depthAttached = false; … … 398 398 osg::Camera::BufferComponent buffer = itr->first; 399 399 osg::Camera::Attachment& attachment = itr->second; 400 400 401 401 if (attachment._texture.valid() || attachment._image.valid()) 402 402 fbo->setAttachment(buffer, osg::FrameBufferAttachment(attachment)); … … 436 436 samples, colorSamples))); 437 437 } 438 438 439 439 if (buffer==osg::Camera::DEPTH_BUFFER) depthAttached = true; 440 440 else if (buffer==osg::Camera::STENCIL_BUFFER) stencilAttached = true; … … 445 445 } 446 446 else if (buffer>=osg::Camera::COLOR_BUFFER) colorAttached = true; 447 447 448 448 } 449 449 450 450 if (!depthAttached) 451 451 { 452 // If doing MSFBO (and therefore need two FBOs, one for multisampled rendering and one for 453 // final resolve), then configure "fbo" as the resolve FBO, and When done 454 // configuring, swap it into "_resolveFbo" (see line 554). But, if not 452 // If doing MSFBO (and therefore need two FBOs, one for multisampled rendering and one for 453 // final resolve), then configure "fbo" as the resolve FBO, and When done 454 // configuring, swap it into "_resolveFbo" (see line 554). But, if not 455 455 // using MSFBO, then "fbo" is just the render fbo. 456 // If using MSFBO, then resolveBuffersMask 457 // is the value set by the app for the resolve buffers. But if not using 458 // MSFBO, then resolveBuffersMask is the value set by the app for render 456 // If using MSFBO, then resolveBuffersMask 457 // is the value set by the app for the resolve buffers. But if not using 458 // MSFBO, then resolveBuffersMask is the value set by the app for render 459 459 // buffers. In both cases, resolveBuffersMask is used to configure "fbo". 460 460 if( resolveBuffersMask & osg::Camera::IMPLICIT_DEPTH_BUFFER_ATTACHMENT ) … … 505 505 fbo->apply(state); 506 506 507 // If no color attachment make sure to set glDrawBuffer/glReadBuffer to none 507 // If no color attachment make sure to set glDrawBuffer/glReadBuffer to none 508 508 // otherwise glCheckFramebufferStatus will fail 509 // It has to be done after call to glBindFramebuffer (fbo->apply) 509 // It has to be done after call to glBindFramebuffer (fbo->apply) 510 510 // and before call to glCheckFramebufferStatus 511 511 if ( !colorAttached ) … … 529 529 fbo_ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId); 530 530 fbo = 0; 531 531 532 532 // clean up. 533 533 double availableTime = 100.0f; … … 535 535 osg::RenderBuffer::flushDeletedRenderBuffers(state.getContextID(),currentTime,availableTime); 536 536 osg::FrameBufferObject::flushDeletedFrameBufferObjects(state.getContextID(),currentTime,availableTime); 537 537 538 538 539 539 } … … 542 542 setDrawBuffer(GL_NONE, false ); 543 543 setReadBuffer(GL_NONE, false ); 544 544 545 545 _fbo = fbo; 546 546 … … 579 579 } 580 580 } 581 581 582 582 if (!fbo_supported) 583 583 { … … 588 588 } 589 589 } 590 590 591 591 // check whether PBuffer-RTT is supported or not 592 if (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT && 592 if (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT && 593 593 !osg::isGLExtensionSupported(state.getContextID(), "WGL_ARB_render_texture")) 594 { 594 { 595 595 if (renderTargetImplementation<renderTargetFallback) 596 596 renderTargetImplementation = renderTargetFallback; … … 600 600 601 601 // if any of the renderTargetImplementations require a separate graphics context such as with pbuffer try in turn to 602 // set up, but if each level fails then resort to the next level down. 602 // set up, but if each level fails then resort to the next level down. 603 603 while (!getGraphicsContext() && 604 604 (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT || … … 624 624 osg::Texture* pBufferTexture = 0; 625 625 GLenum bufferFormat = GL_NONE; 626 unsigned int level = 0; 627 unsigned int face = 0; 626 unsigned int level = 0; 627 unsigned int face = 0; 628 628 629 629 bool colorAttached = false; … … 710 710 711 711 if (!depthAttached) 712 { 712 { 713 713 traits->depth = 24; 714 714 } 715 715 716 716 if (!colorAttached) 717 { 717 { 718 718 if (bufferFormat == GL_NONE) bufferFormat = GL_RGB; 719 719 … … 728 728 { 729 729 traits->sharedContext = state.getGraphicsContext(); 730 730 731 731 const osg::GraphicsContext::Traits* sharedTraits = traits->sharedContext->getTraits(); 732 732 if (sharedTraits) … … 746 746 747 747 // successfully set up graphics context as requested, 748 // will assign this graphics context to the RenderStage and 748 // will assign this graphics context to the RenderStage and 749 749 // associated parameters. Setting the graphics context will 750 750 // single this while loop to exit successful. 751 751 setGraphicsContext(context.get()); 752 752 753 753 // how to do we detect that an attempt to set up RTT has failed?? 754 754 … … 770 770 { 771 771 OSG_INFO<<"Failed to acquire Graphics Context"<<std::endl; 772 772 773 773 if (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT) 774 774 { … … 779 779 renderTargetImplementation = osg::Camera::PIXEL_BUFFER; 780 780 } 781 else 781 else 782 782 { 783 783 renderTargetImplementation = osg::Camera::FRAME_BUFFER; … … 787 787 } 788 788 } 789 789 790 790 // finally if all else has failed, then the frame buffer fallback will come in to play. 791 791 if (renderTargetImplementation==osg::Camera::FRAME_BUFFER) … … 797 797 ++itr) 798 798 { 799 // assign the texture... 799 // assign the texture... 800 800 if (itr->second._texture.valid()) setTexture(itr->second._texture.get(), itr->second._level, itr->second._face); 801 801 } … … 857 857 { 858 858 // need to implement 859 texture3D->copyTexSubImage3D(state, 859 texture3D->copyTexSubImage3D(state, 860 860 static_cast<int>(_viewport->x()), 861 861 static_cast<int>(_viewport->y()), … … 869 869 { 870 870 // need to implement 871 textureCubeMap->copyTexSubImageCubeMap(state, _face, 871 textureCubeMap->copyTexSubImageCubeMap(state, _face, 872 872 static_cast<int>(_viewport->x()), 873 873 static_cast<int>(_viewport->y()), … … 908 908 909 909 bool using_multiple_render_targets = fbo_supported && _fbo->hasMultipleRenderingTargets(); 910 910 911 911 if (!using_multiple_render_targets) 912 912 { 913 913 #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) 914 915 if( getDrawBufferApplyMask() ) 914 915 if( getDrawBufferApplyMask() ) 916 916 glDrawBuffer(_drawBuffer); 917 917 918 if( getReadBufferApplyMask() ) 918 if( getReadBufferApplyMask() ) 919 919 glReadBuffer(_readBuffer); 920 920 921 921 #endif 922 922 } … … 927 927 } 928 928 929 // do the drawing itself. 929 // do the drawing itself. 930 930 RenderBin::draw(renderInfo,previous); 931 931 … … 953 953 GLbitfield blitMask = 0; 954 954 bool needToBlitColorBuffers = false; 955 955 956 956 //find which buffer types should be copied 957 957 for (FrameBufferObject::AttachmentMap::const_iterator … … 1020 1020 } 1021 1021 #endif 1022 1022 1023 1023 apply_read_fbo = true; 1024 1024 read_fbo = _resolveFbo.get(); … … 1044 1044 1045 1045 #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) 1046 1046 1047 1047 if (using_multiple_render_targets) 1048 1048 { … … 1069 1069 GLenum dataType = itr->second._image->getDataType(); 1070 1070 if (dataType==0) dataType = _imageReadPixelDataType; 1071 if (dataType==0) dataType = GL_UNSIGNED_BYTE; 1071 if (dataType==0) dataType = GL_UNSIGNED_BYTE; 1072 1072 1073 1073 itr->second._image->readPixels(static_cast<int>(_viewport->x()), 1074 1074 static_cast<int>(_viewport->y()), 1075 1075 static_cast<int>(_viewport->width()), 1076 static_cast<int>(_viewport->height()), 1076 static_cast<int>(_viewport->height()), 1077 1077 pixelFormat, dataType); 1078 1078 } … … 1099 1099 ++itr) 1100 1100 { 1101 if (itr->second._texture.valid() && itr->second._mipMapGeneration) 1101 if (itr->second._texture.valid() && itr->second._mipMapGeneration) 1102 1102 { 1103 1103 state.setActiveTextureUnit(0); … … 1111 1111 struct DrawInnerOperation : public osg::Operation 1112 1112 { 1113 DrawInnerOperation(RenderStage* stage, osg::RenderInfo& renderInfo) : 1113 DrawInnerOperation(RenderStage* stage, osg::RenderInfo& renderInfo) : 1114 1114 osg::Operation("DrawInnerStage",false), 1115 1115 _stage(stage), … … 1130 1130 } 1131 1131 } 1132 1132 1133 1133 RenderStage* _stage; 1134 1134 RenderInfo _renderInfo; … … 1142 1142 if(_initialViewMatrix.valid()) renderInfo.getState()->setInitialViewMatrix(_initialViewMatrix.get()); 1143 1143 1144 // push the stages camera so that drawing code can query it 1144 // push the stages camera so that drawing code can query it 1145 1145 if (_camera) renderInfo.pushCamera(_camera); 1146 1146 … … 1156 1156 // so there is no need to call it here. 1157 1157 drawPreRenderStages(renderInfo,previous); 1158 1158 1159 1159 if (_cameraRequiresSetUp) 1160 1160 { … … 1169 1169 osg::OperationThread* useThread = 0; 1170 1170 osg::RenderInfo useRenderInfo(renderInfo); 1171 1171 1172 1172 RenderLeaf* saved_previous = previous; 1173 1173 … … 1177 1177 // now as an experiment. 1178 1178 callingContext->releaseContext(); 1179 1179 1180 1180 // OSG_NOTICE<<" enclosing state before - "<<state.getStateSetStackSize()<<std::endl; 1181 1181 … … 1184 1184 useThread = useContext->getGraphicsThread(); 1185 1185 useRenderInfo.setState(useState); 1186 1186 1187 1187 // synchronize the frame stamps 1188 1188 useState->setFrameStamp(const_cast<osg::FrameStamp*>(state.getFrameStamp())); … … 1191 1191 useState->setDynamicObjectCount(state.getDynamicObjectCount()); 1192 1192 useState->setDynamicObjectRenderingCompletedCallback(state.getDynamicObjectRenderingCompletedCallback()); 1193 1194 if (!useThread) 1193 1194 if (!useThread) 1195 1195 { 1196 1196 previous = 0; 1197 1197 useContext->makeCurrent(); 1198 1198 1199 1199 // OSG_NOTICE<<" nested state before - "<<useState->getStateSetStackSize()<<std::endl; 1200 1200 } … … 1208 1208 (*(_camera->getPreDrawCallback()))(renderInfo); 1209 1209 } 1210 1211 bool doCopyTexture = _texture.valid() ? 1210 1211 bool doCopyTexture = _texture.valid() ? 1212 1212 (callingContext != useContext) : 1213 1213 false; … … 1217 1217 #if 1 1218 1218 ref_ptr<osg::BlockAndFlushOperation> block = new osg::BlockAndFlushOperation; 1219 1219 1220 1220 useThread->add(new DrawInnerOperation( this, renderInfo )); 1221 1221 1222 1222 useThread->add(block.get()); 1223 1223 1224 1224 // wait till the DrawInnerOperations is complete. 1225 1225 block->block(); 1226 1226 1227 1227 doCopyTexture = false; 1228 1228 1229 1229 #else 1230 1230 useThread->add(new DrawInnerOperation( this, renderInfo ), true); 1231 1231 1232 1232 doCopyTexture = false; 1233 #endif 1233 #endif 1234 1234 } 1235 1235 else 1236 1236 { 1237 1237 drawInner( useRenderInfo, previous, doCopyTexture); 1238 1238 1239 1239 if (useRenderInfo.getUserData() != renderInfo.getUserData()) 1240 1240 { 1241 1241 renderInfo.setUserData(useRenderInfo.getUserData()); 1242 1242 } 1243 1243 1244 1244 } 1245 1245 … … 1281 1281 // to ensure that textures are updated before main thread commenses. 1282 1282 glFlush(); 1283 1284 1283 1284 1285 1285 useContext->releaseContext(); 1286 1286 } … … 1290 1290 { 1291 1291 // restore the graphics context. 1292 1292 1293 1293 previous = saved_previous; 1294 1294 1295 1295 // OSG_NOTICE<<" nested state after - "<<useState->getStateSetStackSize()<<std::endl; 1296 1296 // OSG_NOTICE<<" enclosing state after - "<<state.getStateSetStackSize()<<std::endl; … … 1350 1350 glClearDepthf( _clearDepth); 1351 1351 #endif 1352 1352 1353 1353 glDepthMask ( GL_TRUE ); 1354 1354 state.haveAppliedAttribute( osg::StateAttribute::DEPTH ); … … 1368 1368 } 1369 1369 #endif 1370 1370 1371 1371 1372 1372 glClear( _clearMask ); 1373 1373 1374 1374 #ifdef USE_SCISSOR_TEST 1375 1375 glDisable( GL_SCISSOR_TEST ); … … 1379 1379 glMatrixMode( GL_MODELVIEW ); 1380 1380 glLoadIdentity(); 1381 #endif 1381 #endif 1382 1382 1383 1383 // apply the positional state. … … 1403 1403 { 1404 1404 if (_postRenderList.empty()) return; 1405 1405 1406 1406 //cout << "Drawing prerendering stages "<<this<< " "<<_viewport->x()<<","<< _viewport->y()<<","<< _viewport->width()<<","<< _viewport->height()<<std::endl; 1407 1407 for(RenderStageList::iterator itr=_postRenderList.begin(); … … 1438 1438 } 1439 1439 } 1440 1440 1441 1441 if (RenderBin::getStats(stats)) 1442 1442 { … … 1470 1470 count += post_itr->second->computeNumberOfDynamicRenderLeaves(); 1471 1471 } 1472 1472 1473 1473 return count; 1474 1474 }
