| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osg/OcclusionQueryNode> |
|---|
| 16 | #include <OpenThreads/ScopedLock> |
|---|
| 17 | #include <osg/Timer> |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | #include <osg/CopyOp> |
|---|
| 20 | #include <osg/Vec3> |
|---|
| 21 | #include <osg/MatrixTransform> |
|---|
| 22 | #include <osg/Group> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/Geometry> |
|---|
| 25 | #include <osg/BoundingBox> |
|---|
| 26 | #include <osg/BoundingSphere> |
|---|
| 27 | #include <osg/Referenced> |
|---|
| 28 | #include <osg/ComputeBoundsVisitor> |
|---|
| 29 | #include <osg/StateSet> |
|---|
| 30 | #include <osg/StateAttribute> |
|---|
| 31 | #include <osg/PolygonMode> |
|---|
| 32 | #include <osg/ColorMask> |
|---|
| 33 | #include <osg/PolygonOffset> |
|---|
| 34 | #include <osg/Depth> |
|---|
| 35 | #include <map> |
|---|
| 36 | #include <vector> |
|---|
| 37 | |
|---|
| 38 | #include <OpenThreads/Thread> |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | typedef osg::buffered_value< osg::ref_ptr< osg::Drawable::Extensions > > OcclusionQueryBufferedExtensions; |
|---|
| 42 | static OcclusionQueryBufferedExtensions s_OQ_bufferedExtensions; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | osg::StateSet* |
|---|
| 56 | initOQState() |
|---|
| 57 | { |
|---|
| 58 | osg::StateSet* state = new osg::StateSet; |
|---|
| 59 | |
|---|
| 60 | state->setRenderBinDetails( 9, "RenderBin" ); |
|---|
| 61 | |
|---|
| 62 | state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | |
|---|
| 63 | osg::StateAttribute::PROTECTED); |
|---|
| 64 | state->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::OFF | |
|---|
| 65 | osg::StateAttribute::PROTECTED); |
|---|
| 66 | state->setMode( GL_CULL_FACE, osg::StateAttribute::ON | |
|---|
| 67 | osg::StateAttribute::PROTECTED); |
|---|
| 68 | |
|---|
| 69 | osg::ColorMask* cm = new osg::ColorMask( false, false, false, false ); |
|---|
| 70 | state->setAttributeAndModes( cm, osg::StateAttribute::ON | |
|---|
| 71 | osg::StateAttribute::PROTECTED); |
|---|
| 72 | osg::Depth* d = new osg::Depth( osg::Depth::LEQUAL, 0.f, 1.f, false ); |
|---|
| 73 | state->setAttributeAndModes( d, osg::StateAttribute::ON | |
|---|
| 74 | osg::StateAttribute::PROTECTED); |
|---|
| 75 | osg::PolygonMode* pm = new osg::PolygonMode( |
|---|
| 76 | osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL ); |
|---|
| 77 | state->setAttributeAndModes( pm, osg::StateAttribute::ON | |
|---|
| 78 | osg::StateAttribute::PROTECTED); |
|---|
| 79 | |
|---|
| 80 | osg::PolygonOffset* po = new osg::PolygonOffset( -1., -1. ); |
|---|
| 81 | state->setAttributeAndModes( po, osg::StateAttribute::ON | |
|---|
| 82 | osg::StateAttribute::PROTECTED); |
|---|
| 83 | |
|---|
| 84 | return state; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osg::StateSet* |
|---|
| 89 | initOQDebugState() |
|---|
| 90 | { |
|---|
| 91 | osg::StateSet* debugState = new osg::StateSet; |
|---|
| 92 | |
|---|
| 93 | debugState->setMode( GL_LIGHTING, osg::StateAttribute::OFF | |
|---|
| 94 | osg::StateAttribute::PROTECTED); |
|---|
| 95 | debugState->setTextureMode( 0, GL_TEXTURE_2D, osg::StateAttribute::OFF | |
|---|
| 96 | osg::StateAttribute::PROTECTED); |
|---|
| 97 | debugState->setMode( GL_CULL_FACE, osg::StateAttribute::ON | |
|---|
| 98 | osg::StateAttribute::PROTECTED); |
|---|
| 99 | |
|---|
| 100 | osg::PolygonMode* pm = new osg::PolygonMode( |
|---|
| 101 | osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE ); |
|---|
| 102 | debugState->setAttributeAndModes( pm, osg::StateAttribute::ON | |
|---|
| 103 | osg::StateAttribute::PROTECTED); |
|---|
| 104 | |
|---|
| 105 | osg::PolygonOffset* po = new osg::PolygonOffset( -1., -1. ); |
|---|
| 106 | debugState->setAttributeAndModes( po, osg::StateAttribute::ON | |
|---|
| 107 | osg::StateAttribute::PROTECTED); |
|---|
| 108 | |
|---|
| 109 | return debugState; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class TestResult : public osg::Referenced |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | TestResult() : _init( false ), _id( 0 ), _contextID( 0 ), _active( false ), _numPixels( 0 ) {} |
|---|
| 123 | ~TestResult() {} |
|---|
| 124 | |
|---|
| 125 | bool _init; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | GLuint _id; |
|---|
| 129 | |
|---|
| 130 | unsigned int _contextID; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | mutable bool _active; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | GLint _numPixels; |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | class QueryGeometry : public osg::Geometry |
|---|
| 143 | { |
|---|
| 144 | public: |
|---|
| 145 | QueryGeometry( const std::string& oqnName=std::string("") ); |
|---|
| 146 | ~QueryGeometry(); |
|---|
| 147 | |
|---|
| 148 | void reset(); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | virtual void drawImplementation( osg::RenderInfo& renderInfo ) const; |
|---|
| 153 | |
|---|
| 154 | unsigned int getNumPixels( const osg::Camera* cam ); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | void releaseGLObjects( osg::State* state = 0 ); |
|---|
| 158 | static void deleteQueryObject( unsigned int contextID, GLuint handle ); |
|---|
| 159 | static void flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime ); |
|---|
| 160 | static void discardDeletedQueryObjects( unsigned int contextID ); |
|---|
| 161 | |
|---|
| 162 | protected: |
|---|
| 163 | typedef std::map< const osg::Camera*, TestResult > ResultMap; |
|---|
| 164 | mutable ResultMap _results; |
|---|
| 165 | mutable OpenThreads::Mutex _mapMutex; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | std::string _oqnName; |
|---|
| 169 | }; |
|---|
| 170 | |
|---|
| 171 | struct RetrieveQueriesCallback : public osg::Camera::DrawCallback |
|---|
| 172 | { |
|---|
| 173 | typedef std::vector<TestResult*> ResultsVector; |
|---|
| 174 | ResultsVector _results; |
|---|
| 175 | |
|---|
| 176 | RetrieveQueriesCallback( osg::Drawable::Extensions* ext=NULL ) |
|---|
| 177 | : _extensionsFallback( ext ) |
|---|
| 178 | { |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | RetrieveQueriesCallback( const RetrieveQueriesCallback&, const osg::CopyOp& ) {} |
|---|
| 182 | META_Object( osgOQ, RetrieveQueriesCallback ) |
|---|
| 183 | |
|---|
| 184 | virtual void operator() (const osg::Camera& camera) const |
|---|
| 185 | { |
|---|
| 186 | if (_results.empty()) |
|---|
| 187 | return; |
|---|
| 188 | |
|---|
| 189 | const osg::Timer& timer = *osg::Timer::instance(); |
|---|
| 190 | osg::Timer_t start_tick = timer.tick(); |
|---|
| 191 | double elapsedTime( 0. ); |
|---|
| 192 | int count( 0 ); |
|---|
| 193 | |
|---|
| 194 | osg::Drawable::Extensions* ext; |
|---|
| 195 | if (camera.getGraphicsContext()) |
|---|
| 196 | { |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | unsigned int contextID = camera.getGraphicsContext()->getState()->getContextID(); |
|---|
| 200 | RetrieveQueriesCallback* const_this = const_cast<RetrieveQueriesCallback*>( this ); |
|---|
| 201 | ext = const_this->getExtensions( contextID, true ); |
|---|
| 202 | } |
|---|
| 203 | else |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | osg::notify( osg::DEBUG_INFO ) << "osgOQ: RQCB: Using fallback path to obtain Extensions pointer." << std::endl; |
|---|
| 210 | ext = _extensionsFallback; |
|---|
| 211 | if (!ext) |
|---|
| 212 | { |
|---|
| 213 | osg::notify( osg::FATAL ) << "osgOQ: RQCB: Extensions pointer fallback is NULL." << std::endl; |
|---|
| 214 | return; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | ResultsVector::const_iterator it = _results.begin(); |
|---|
| 219 | while (it != _results.end()) |
|---|
| 220 | { |
|---|
| 221 | TestResult* tr = const_cast<TestResult*>( *it ); |
|---|
| 222 | |
|---|
| 223 | if (!tr->_active || !tr->_init) |
|---|
| 224 | { |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | it++; |
|---|
| 231 | continue; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | osg::notify( osg::DEBUG_INFO ) << |
|---|
| 235 | "osgOQ: RQCB: Retrieving..." << std::endl; |
|---|
| 236 | |
|---|
| 237 | #ifdef FORCE_QUERY_RESULT_AVAILABLE_BEFORE_RETRIEVAL |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | GLint ready( 0 ); |
|---|
| 246 | while( !ready ) |
|---|
| 247 | { |
|---|
| 248 | |
|---|
| 249 | OpenThreads::Thread::microSleep( 5 ); |
|---|
| 250 | ext->glGetQueryObjectiv( tr->_id, GL_QUERY_RESULT_AVAILABLE, &ready ); |
|---|
| 251 | }; |
|---|
| 252 | #endif |
|---|
| 253 | |
|---|
| 254 | ext->glGetQueryObjectiv( tr->_id, GL_QUERY_RESULT, &(tr->_numPixels) ); |
|---|
| 255 | if (tr->_numPixels < 0) |
|---|
| 256 | osg::notify( osg::WARN ) << "osgOQ: RQCB: " << |
|---|
| 257 | "glGetQueryObjectiv returned negative value (" << tr->_numPixels << ")." << std::endl; |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | tr->_active = false; |
|---|
| 262 | |
|---|
| 263 | it++; |
|---|
| 264 | count++; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 268 | osg::notify( osg::INFO ) << "osgOQ: RQCB: " << "Retrieved " << count << |
|---|
| 269 | " queries in " << elapsedTime << " seconds." << std::endl; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | void reset() |
|---|
| 273 | { |
|---|
| 274 | _results.clear(); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | void add( TestResult* tr ) |
|---|
| 278 | { |
|---|
| 279 | _results.push_back( tr ); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | osg::Drawable::Extensions* getExtensions( unsigned int contextID, bool createIfNotInitalized ) |
|---|
| 283 | { |
|---|
| 284 | if (!s_OQ_bufferedExtensions[ contextID ] && createIfNotInitalized) |
|---|
| 285 | s_OQ_bufferedExtensions[ contextID ] = new osg::Drawable::Extensions( contextID ); |
|---|
| 286 | return s_OQ_bufferedExtensions[ contextID ].get(); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | osg::Drawable::Extensions* _extensionsFallback; |
|---|
| 291 | }; |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | struct ClearQueriesCallback : public osg::Camera::DrawCallback |
|---|
| 297 | { |
|---|
| 298 | ClearQueriesCallback() : _rqcb( NULL ) {} |
|---|
| 299 | ClearQueriesCallback( const ClearQueriesCallback&, const osg::CopyOp& ) {} |
|---|
| 300 | META_Object( osgOQ, ClearQueriesCallback ) |
|---|
| 301 | |
|---|
| 302 | virtual void operator() (const osg::Camera&) const |
|---|
| 303 | { |
|---|
| 304 | if (!_rqcb) |
|---|
| 305 | { |
|---|
| 306 | osg::notify( osg::FATAL ) << "osgOQ: CQCB: Invalid RQCB." << std::endl; |
|---|
| 307 | return; |
|---|
| 308 | } |
|---|
| 309 | _rqcb->reset(); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | RetrieveQueriesCallback* _rqcb; |
|---|
| 313 | }; |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | typedef std::list< GLuint > QueryObjectList; |
|---|
| 320 | typedef osg::buffered_object< QueryObjectList > DeletedQueryObjectCache; |
|---|
| 321 | |
|---|
| 322 | static OpenThreads::Mutex s_mutex_deletedQueryObjectCache; |
|---|
| 323 | static DeletedQueryObjectCache s_deletedQueryObjectCache; |
|---|
| 324 | |
|---|
| 325 | QueryGeometry::QueryGeometry( const std::string& oqnName ) |
|---|
| 326 | : _oqnName( oqnName ) |
|---|
| 327 | { |
|---|
| 328 | |
|---|
| 329 | setUseDisplayList( false ); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | QueryGeometry::~QueryGeometry() |
|---|
| 333 | { |
|---|
| 334 | reset(); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | void |
|---|
| 339 | QueryGeometry::reset() |
|---|
| 340 | { |
|---|
| 341 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _mapMutex ); |
|---|
| 342 | |
|---|
| 343 | ResultMap::iterator it = _results.begin(); |
|---|
| 344 | while (it != _results.end()) |
|---|
| 345 | { |
|---|
| 346 | TestResult& tr = it->second; |
|---|
| 347 | if (tr._init) |
|---|
| 348 | QueryGeometry::deleteQueryObject( tr._contextID, tr._id ); |
|---|
| 349 | it++; |
|---|
| 350 | } |
|---|
| 351 | _results.clear(); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | void |
|---|
| 358 | QueryGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const |
|---|
| 359 | { |
|---|
| 360 | unsigned int contextID = renderInfo.getState()->getContextID(); |
|---|
| 361 | osg::Drawable::Extensions* ext = getExtensions( contextID, true ); |
|---|
| 362 | osg::Camera* cam = renderInfo.getCurrentCamera(); |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | if (!cam->getPostDrawCallback()) |
|---|
| 366 | { |
|---|
| 367 | RetrieveQueriesCallback* rqcb = new RetrieveQueriesCallback( ext ); |
|---|
| 368 | cam->setPostDrawCallback( rqcb ); |
|---|
| 369 | |
|---|
| 370 | ClearQueriesCallback* cqcb = new ClearQueriesCallback; |
|---|
| 371 | cqcb->_rqcb = rqcb; |
|---|
| 372 | cam->setPreDrawCallback( cqcb ); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | TestResult* tr; |
|---|
| 377 | { |
|---|
| 378 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _mapMutex ); |
|---|
| 379 | tr = &( _results[ cam ] ); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | RetrieveQueriesCallback* rqcb = dynamic_cast< |
|---|
| 384 | RetrieveQueriesCallback* >( cam->getPostDrawCallback() ); |
|---|
| 385 | if (!rqcb) |
|---|
| 386 | { |
|---|
| 387 | osg::notify( osg::FATAL ) << "osgOQ: QG: Invalid RQCB." << std::endl; |
|---|
| 388 | return; |
|---|
| 389 | } |
|---|
| 390 | rqcb->add( tr ); |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | if (!tr->_init) |
|---|
| 395 | { |
|---|
| 396 | ext->glGenQueries( 1, &(tr->_id) ); |
|---|
| 397 | tr->_contextID = contextID; |
|---|
| 398 | tr->_init = true; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | osg::notify( osg::DEBUG_INFO ) << |
|---|
| 402 | "osgOQ: QG: Querying for: " << _oqnName << std::endl; |
|---|
| 403 | |
|---|
| 404 | ext->glBeginQuery( GL_SAMPLES_PASSED_ARB, tr->_id ); |
|---|
| 405 | osg::Geometry::drawImplementation( renderInfo ); |
|---|
| 406 | ext->glEndQuery( GL_SAMPLES_PASSED_ARB ); |
|---|
| 407 | tr->_active = true; |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | osg::notify( osg::DEBUG_INFO ) << |
|---|
| 411 | "osgOQ: QG. OQNName: " << _oqnName << |
|---|
| 412 | ", Ctx: " << contextID << |
|---|
| 413 | ", ID: " << tr->_id << std::endl; |
|---|
| 414 | #ifdef _DEBUG |
|---|
| 415 | { |
|---|
| 416 | GLenum err; |
|---|
| 417 | if ((err = glGetError()) != GL_NO_ERROR) |
|---|
| 418 | osg::notify( osg::FATAL ) << |
|---|
| 419 | "osgOQ: QG: OpenGL error: " << err << "." << std::endl; |
|---|
| 420 | } |
|---|
| 421 | #endif |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | unsigned int |
|---|
| 428 | QueryGeometry::getNumPixels( const osg::Camera* cam ) |
|---|
| 429 | { |
|---|
| 430 | TestResult tr; |
|---|
| 431 | { |
|---|
| 432 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _mapMutex ); |
|---|
| 433 | tr = _results[ cam ]; |
|---|
| 434 | } |
|---|
| 435 | return tr._numPixels; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | void |
|---|
| 440 | QueryGeometry::releaseGLObjects( osg::State* state ) |
|---|
| 441 | { |
|---|
| 442 | if (!state) |
|---|
| 443 | |
|---|
| 444 | reset(); |
|---|
| 445 | |
|---|
| 446 | else |
|---|
| 447 | { |
|---|
| 448 | |
|---|
| 449 | unsigned int contextID = state->getContextID(); |
|---|
| 450 | ResultMap::iterator it = _results.begin(); |
|---|
| 451 | while (it != _results.end()) |
|---|
| 452 | { |
|---|
| 453 | TestResult& tr = it->second; |
|---|
| 454 | if (tr._contextID == contextID) |
|---|
| 455 | { |
|---|
| 456 | QueryGeometry::deleteQueryObject( contextID, tr._id ); |
|---|
| 457 | tr._init = false; |
|---|
| 458 | } |
|---|
| 459 | it++; |
|---|
| 460 | } |
|---|
| 461 | } |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | void |
|---|
| 465 | QueryGeometry::deleteQueryObject( unsigned int contextID, GLuint handle ) |
|---|
| 466 | { |
|---|
| 467 | if (handle!=0) |
|---|
| 468 | { |
|---|
| 469 | OpenThreads::ScopedLock< OpenThreads::Mutex > lock( s_mutex_deletedQueryObjectCache ); |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | s_deletedQueryObjectCache[contextID].push_back( handle ); |
|---|
| 473 | } |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | void |
|---|
| 478 | QueryGeometry::flushDeletedQueryObjects( unsigned int contextID, double , double& availableTime ) |
|---|
| 479 | { |
|---|
| 480 | |
|---|
| 481 | if (availableTime<=0.0) return; |
|---|
| 482 | |
|---|
| 483 | const osg::Timer& timer = *osg::Timer::instance(); |
|---|
| 484 | osg::Timer_t start_tick = timer.tick(); |
|---|
| 485 | double elapsedTime = 0.0; |
|---|
| 486 | |
|---|
| 487 | { |
|---|
| 488 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_mutex_deletedQueryObjectCache); |
|---|
| 489 | |
|---|
| 490 | const osg::Drawable::Extensions* extensions = getExtensions( contextID, true ); |
|---|
| 491 | |
|---|
| 492 | QueryObjectList& qol = s_deletedQueryObjectCache[contextID]; |
|---|
| 493 | |
|---|
| 494 | for(QueryObjectList::iterator titr=qol.begin(); |
|---|
| 495 | titr!=qol.end() && elapsedTime<availableTime; |
|---|
| 496 | ) |
|---|
| 497 | { |
|---|
| 498 | extensions->glDeleteQueries( 1L, &(*titr ) ); |
|---|
| 499 | titr = qol.erase(titr); |
|---|
| 500 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | availableTime -= elapsedTime; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | void |
|---|
| 508 | QueryGeometry::discardDeletedQueryObjects( unsigned int contextID ) |
|---|
| 509 | { |
|---|
| 510 | OpenThreads::ScopedLock< OpenThreads::Mutex > lock( s_mutex_deletedQueryObjectCache ); |
|---|
| 511 | QueryObjectList& qol = s_deletedQueryObjectCache[ contextID ]; |
|---|
| 512 | qol.clear(); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | namespace osg |
|---|
| 521 | { |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | OcclusionQueryNode::OcclusionQueryNode() |
|---|
| 525 | : _enabled( true ), |
|---|
| 526 | _visThreshold( 500 ), |
|---|
| 527 | _queryFrameCount( 5 ), |
|---|
| 528 | _debugBB( false ) |
|---|
| 529 | { |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | createSupportNodes(); |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | OcclusionQueryNode::~OcclusionQueryNode() |
|---|
| 537 | { |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | OcclusionQueryNode::OcclusionQueryNode( const OcclusionQueryNode& oqn, const osg::CopyOp& copyop ) |
|---|
| 541 | : Group( oqn, copyop ), |
|---|
| 542 | _passed( false ) |
|---|
| 543 | { |
|---|
| 544 | _enabled = oqn._enabled; |
|---|
| 545 | _visThreshold = oqn._visThreshold; |
|---|
| 546 | _queryFrameCount = oqn._queryFrameCount; |
|---|
| 547 | _debugBB = oqn._debugBB; |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | createSupportNodes(); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | bool |
|---|
| 555 | OcclusionQueryNode::getPassed( const osg::Camera* camera, float distanceToEyePoint ) |
|---|
| 556 | { |
|---|
| 557 | if ( !_enabled ) |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | return true; |
|---|
| 561 | |
|---|
| 562 | if (_queryGeode->getDrawable( 0 ) == NULL) |
|---|
| 563 | { |
|---|
| 564 | osg::notify( osg::FATAL ) << |
|---|
| 565 | "osgOQ: OcclusionQueryNode: No QueryGeometry." << std::endl; |
|---|
| 566 | |
|---|
| 567 | return true; |
|---|
| 568 | } |
|---|
| 569 | QueryGeometry* qg = static_cast< QueryGeometry* >( _queryGeode->getDrawable( 0 ) ); |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | const osg::BoundingSphere& bs = getBound(); |
|---|
| 575 | float distance = distanceToEyePoint - bs._radius; |
|---|
| 576 | _passed = ( distance <= 0.f ); |
|---|
| 577 | if (!_passed) |
|---|
| 578 | { |
|---|
| 579 | int result = qg->getNumPixels( camera ); |
|---|
| 580 | _passed = ( (unsigned int)(result) > _visThreshold ); |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | return _passed; |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | void |
|---|
| 587 | OcclusionQueryNode::traverseQuery( const osg::Camera* camera, osg::NodeVisitor& nv ) |
|---|
| 588 | { |
|---|
| 589 | bool issueQuery; |
|---|
| 590 | { |
|---|
| 591 | const int curFrame = nv.getTraversalNumber(); |
|---|
| 592 | |
|---|
| 593 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _frameCountMutex ); |
|---|
| 594 | int& lastQueryFrame = _frameCountMap[ camera ]; |
|---|
| 595 | issueQuery = (curFrame - lastQueryFrame >= _queryFrameCount); |
|---|
| 596 | if (issueQuery) |
|---|
| 597 | lastQueryFrame = curFrame; |
|---|
| 598 | } |
|---|
| 599 | if (issueQuery) |
|---|
| 600 | _queryGeode->accept( nv ); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | void |
|---|
| 604 | OcclusionQueryNode::traverseDebug( osg::NodeVisitor& nv ) |
|---|
| 605 | { |
|---|
| 606 | if (_debugBB) |
|---|
| 607 | |
|---|
| 608 | _debugGeode->accept( nv ); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | osg::BoundingSphere |
|---|
| 612 | OcclusionQueryNode::computeBound() const |
|---|
| 613 | { |
|---|
| 614 | { |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _computeBoundMutex ) ; |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | osg::OcclusionQueryNode* nonConstThis = const_cast<osg::OcclusionQueryNode*>( this ); |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | osg::ComputeBoundsVisitor cbv; |
|---|
| 626 | nonConstThis->accept( cbv ); |
|---|
| 627 | osg::BoundingBox bb = cbv.getBoundingBox(); |
|---|
| 628 | |
|---|
| 629 | osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array; |
|---|
| 630 | v->resize( 8 ); |
|---|
| 631 | (*v)[0] = osg::Vec3( bb._min.x(), bb._min.y(), bb._min.z() ); |
|---|
| 632 | (*v)[1] = osg::Vec3( bb._max.x(), bb._min.y(), bb._min.z() ); |
|---|
| 633 | (*v)[2] = osg::Vec3( bb._max.x(), bb._min.y(), bb._max.z() ); |
|---|
| 634 | (*v)[3] = osg::Vec3( bb._min.x(), bb._min.y(), bb._max.z() ); |
|---|
| 635 | (*v)[4] = osg::Vec3( bb._max.x(), bb._max.y(), bb._min.z() ); |
|---|
| 636 | (*v)[5] = osg::Vec3( bb._min.x(), bb._max.y(), bb._min.z() ); |
|---|
| 637 | (*v)[6] = osg::Vec3( bb._min.x(), bb._max.y(), bb._max.z() ); |
|---|
| 638 | (*v)[7] = osg::Vec3( bb._max.x(), bb._max.y(), bb._max.z() ); |
|---|
| 639 | |
|---|
| 640 | osg::Geometry* geom = static_cast< osg::Geometry* >( nonConstThis->_queryGeode->getDrawable( 0 ) ); |
|---|
| 641 | geom->setVertexArray( v.get() ); |
|---|
| 642 | |
|---|
| 643 | geom = static_cast< osg::Geometry* >( nonConstThis->_debugGeode->getDrawable( 0 ) ); |
|---|
| 644 | geom->setVertexArray( v.get() ); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | return Group::computeBound(); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | void |
|---|
| 653 | OcclusionQueryNode::setQueriesEnabled( bool enable ) |
|---|
| 654 | { |
|---|
| 655 | _enabled = enable; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | void |
|---|
| 660 | OcclusionQueryNode::setDebugDisplay( bool debug ) |
|---|
| 661 | { |
|---|
| 662 | _debugBB = debug; |
|---|
| 663 | } |
|---|
| 664 | bool |
|---|
| 665 | OcclusionQueryNode::getDebugDisplay() const |
|---|
| 666 | { |
|---|
| 667 | return _debugBB; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | void |
|---|
| 673 | OcclusionQueryNode::setQueryStateSet( osg::StateSet* ss ) |
|---|
| 674 | { |
|---|
| 675 | if (!_queryGeode) |
|---|
| 676 | { |
|---|
| 677 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid query support node." << std::endl; |
|---|
| 678 | return; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | _queryGeode->setStateSet( ss ); |
|---|
| 682 | } |
|---|
| 683 | osg::StateSet* |
|---|
| 684 | OcclusionQueryNode::getQueryStateSet() |
|---|
| 685 | { |
|---|
| 686 | if (!_queryGeode) |
|---|
| 687 | { |
|---|
| 688 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid query support node." << std::endl; |
|---|
| 689 | return NULL; |
|---|
| 690 | } |
|---|
| 691 | return _queryGeode->getStateSet(); |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | const osg::StateSet* |
|---|
| 695 | OcclusionQueryNode::getQueryStateSet() const |
|---|
| 696 | { |
|---|
| 697 | if (!_queryGeode) |
|---|
| 698 | { |
|---|
| 699 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid query support node." << std::endl; |
|---|
| 700 | return NULL; |
|---|
| 701 | } |
|---|
| 702 | return _queryGeode->getStateSet(); |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | void |
|---|
| 706 | OcclusionQueryNode::setDebugStateSet( osg::StateSet* ss ) |
|---|
| 707 | { |
|---|
| 708 | if (!_debugGeode) |
|---|
| 709 | { |
|---|
| 710 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid debug support node." << std::endl; |
|---|
| 711 | return; |
|---|
| 712 | } |
|---|
| 713 | _debugGeode->setStateSet( ss ); |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | osg::StateSet* |
|---|
| 717 | OcclusionQueryNode::getDebugStateSet() |
|---|
| 718 | { |
|---|
| 719 | if (!_debugGeode.valid()) |
|---|
| 720 | { |
|---|
| 721 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid debug support node." << std::endl; |
|---|
| 722 | return NULL; |
|---|
| 723 | } |
|---|
| 724 | return _debugGeode->getStateSet(); |
|---|
| 725 | } |
|---|
| 726 | const osg::StateSet* |
|---|
| 727 | OcclusionQueryNode::getDebugStateSet() const |
|---|
| 728 | { |
|---|
| 729 | if (!_debugGeode.valid()) |
|---|
| 730 | { |
|---|
| 731 | osg::notify( osg::WARN ) << "osgOQ: OcclusionQueryNode:: Invalid debug support node." << std::endl; |
|---|
| 732 | return NULL; |
|---|
| 733 | } |
|---|
| 734 | return _debugGeode->getStateSet(); |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | bool |
|---|
| 738 | OcclusionQueryNode::getPassed() const |
|---|
| 739 | { |
|---|
| 740 | return _passed; |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | void |
|---|
| 745 | OcclusionQueryNode::createSupportNodes() |
|---|
| 746 | { |
|---|
| 747 | GLushort indices[] = { 0, 1, 2, 3, 4, 5, 6, 7, |
|---|
| 748 | 0, 3, 6, 5, 2, 1, 4, 7, |
|---|
| 749 | 5, 4, 1, 0, 2, 7, 6, 3 }; |
|---|
| 750 | |
|---|
| 751 | { |
|---|
| 752 | |
|---|
| 753 | _queryGeode = new osg::Geode; |
|---|
| 754 | _queryGeode->setName( "OQTest" ); |
|---|
| 755 | _queryGeode->setDataVariance( osg::Object::DYNAMIC ); |
|---|
| 756 | |
|---|
| 757 | osg::ref_ptr< QueryGeometry > geom = new QueryGeometry( getName() ); |
|---|
| 758 | geom->setDataVariance( osg::Object::DYNAMIC ); |
|---|
| 759 | geom->addPrimitiveSet( new osg::DrawElementsUShort( |
|---|
| 760 | osg::PrimitiveSet::QUADS, 24, indices ) ); |
|---|
| 761 | |
|---|
| 762 | _queryGeode->addDrawable( geom.get() ); |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | { |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | _debugGeode = new osg::Geode; |
|---|
| 769 | _debugGeode->setName( "Debug" ); |
|---|
| 770 | _debugGeode->setDataVariance( osg::Object::DYNAMIC ); |
|---|
| 771 | |
|---|
| 772 | osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; |
|---|
| 773 | geom->setDataVariance( osg::Object::DYNAMIC ); |
|---|
| 774 | |
|---|
| 775 | osg::ref_ptr<osg::Vec4Array> ca = new osg::Vec4Array; |
|---|
| 776 | ca->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) ); |
|---|
| 777 | geom->setColorArray( ca.get() ); |
|---|
| 778 | geom->setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 779 | |
|---|
| 780 | geom->addPrimitiveSet( new osg::DrawElementsUShort( |
|---|
| 781 | osg::PrimitiveSet::QUADS, 24, indices ) ); |
|---|
| 782 | |
|---|
| 783 | _debugGeode->addDrawable( geom.get() ); |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | |
|---|
| 789 | setQueryStateSet( initOQState() ); |
|---|
| 790 | setDebugStateSet( initOQDebugState() ); |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | void |
|---|
| 795 | OcclusionQueryNode::releaseGLObjects( osg::State* state ) const |
|---|
| 796 | { |
|---|
| 797 | if(_queryGeode->getDrawable( 0 ) != NULL) |
|---|
| 798 | { |
|---|
| 799 | |
|---|
| 800 | OcclusionQueryNode* nonConstThis = const_cast< OcclusionQueryNode* >( this ); |
|---|
| 801 | QueryGeometry* qg = static_cast< QueryGeometry* >( nonConstThis->_queryGeode->getDrawable( 0 ) ); |
|---|
| 802 | qg->releaseGLObjects( state ); |
|---|
| 803 | } |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | void |
|---|
| 807 | OcclusionQueryNode::flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime ) |
|---|
| 808 | { |
|---|
| 809 | |
|---|
| 810 | QueryGeometry::flushDeletedQueryObjects( contextID, currentTime, availableTime ); |
|---|
| 811 | } |
|---|
| 812 | |
|---|
| 813 | void |
|---|
| 814 | OcclusionQueryNode::discardDeletedQueryObjects( unsigned int contextID ) |
|---|
| 815 | { |
|---|
| 816 | |
|---|
| 817 | QueryGeometry::discardDeletedQueryObjects( contextID ); |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | } |
|---|