gfxGLOcclusionQuery.cpp
Engine/source/gfx/gl/gfxGLOcclusionQuery.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "gfx/gl/gfxGLOcclusionQuery.h" 26#include "gfx/gl/tGL/tGL.h" 27 28GFXGLOcclusionQuery::GFXGLOcclusionQuery(GFXDevice* device) : 29 GFXOcclusionQuery(device), 30 mQuery(-1), 31 mTesting(false) 32{ 33 34} 35 36GFXGLOcclusionQuery::~GFXGLOcclusionQuery() 37{ 38 glDeleteQueries(1, &mQuery); 39} 40 41bool GFXGLOcclusionQuery::begin() 42{ 43 if (GFXDevice::getDisableOcclusionQuery()) 44 return true; 45 46 if (!glIsQuery(mQuery)) 47 glGenQueries(1, &mQuery); 48 49 if (!mTesting) 50 { 51 glBeginQuery(GL_SAMPLES_PASSED, mQuery); 52 mTesting = true; 53 } 54 return true; 55} 56 57void GFXGLOcclusionQuery::end() 58{ 59 if (GFXDevice::getDisableOcclusionQuery()) 60 return; 61 62 if (!glIsQuery(mQuery)) 63 return; 64 glEndQuery(GL_SAMPLES_PASSED); 65 mTesting = false; 66} 67 68GFXOcclusionQuery::OcclusionQueryStatus GFXGLOcclusionQuery::getStatus(bool block, U32* data) 69{ 70 // If this ever shows up near the top of a profile 71 // then your system is GPU bound. 72 PROFILE_SCOPE(GFXGLOcclusionQuery_getStatus); 73 74 if (GFXDevice::getDisableOcclusionQuery()) 75 return NotOccluded; 76 77 if (!glIsQuery(mQuery)) 78 return NotOccluded; 79 80 GLint numPixels = 0; 81 GLint queryDone = false; 82 83 if (block) 84 { 85 while (!queryDone) 86 { 87 //If we're stalled out, proceed with worst-case scenario -BJR 88 if (GFX->mFrameTime->getElapsedMs()>4) 89 { 90 this->begin(); 91 this->end(); 92 return NotOccluded; 93 } 94 glGetQueryObjectiv(mQuery, GL_QUERY_RESULT_AVAILABLE, &queryDone); 95 } 96 } 97 else 98 { 99 glGetQueryObjectiv(mQuery, GL_QUERY_RESULT_AVAILABLE, &queryDone); 100 } 101 102 if (queryDone) 103 glGetQueryObjectiv(mQuery, GL_QUERY_RESULT, &numPixels); 104 else 105 return Waiting; 106 107 if (data) 108 *data = numPixels; 109 110 return numPixels > 0 ? NotOccluded : Occluded; 111} 112 113void GFXGLOcclusionQuery::zombify() 114{ 115 glDeleteQueries(1, &mQuery); 116 mQuery = 0; 117} 118 119void GFXGLOcclusionQuery::resurrect() 120{ 121 glGenQueries(1, &mQuery); 122} 123 124const String GFXGLOcclusionQuery::describeSelf() const 125{ 126 // We've got nothing 127 return String(); 128} 129