gfxOcclusionQuery.h
Engine/source/gfx/gfxOcclusionQuery.h
Classes:
class
A geometry visibility query object.
class
Handle for GFXOcclusionQuery than store last valid state.
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#ifndef _GFXOCCLUSIONQUERY_H_ 25#define _GFXOCCLUSIONQUERY_H_ 26 27#ifndef _GFXDEVICE_H_ 28#include "gfx/gfxDevice.h" 29#endif 30 31 32/// A geometry visibility query object. 33/// @see GFXDevice::createOcclusionQuery 34class GFXOcclusionQuery : public GFXResource 35{ 36protected: 37 38 GFXDevice *mDevice; 39 40 GFXOcclusionQuery( GFXDevice *device ) 41 : mDevice( device ) 42 { 43 } 44 45public: 46 47 /// The states returned by getStatus() 48 /// If you modify this enum you should also modify statusToString() 49 enum OcclusionQueryStatus 50 { 51 Unset, ///< 52 Waiting, ///< 53 Error, ///< 54 Occluded, 55 NotOccluded 56 }; 57 58 virtual ~GFXOcclusionQuery() {} 59 60 /// Prepares the query returning true if the last query 61 /// has been processed and more geometry can be issued. 62 /// @see getStatus 63 virtual bool begin() = 0; 64 65 /// Called after your geometry is drawn to submit 66 /// the query for processing. 67 virtual void end() = 0; 68 69 /// Returns the status of the last submitted query. In general 70 /// you should avoid blocking for the result until the frame 71 /// following your query to keep from stalling the CPU. 72 /// @return Status 73 /// @param block If true CPU will block until the query finishes. 74 /// @param data Number of pixels rendered, valid only if status returned is NotOccluded. 75 virtual OcclusionQueryStatus getStatus( bool block, U32 *data = NULL ) = 0; 76 77 /// Returns a status string. 78 static String statusToString( OcclusionQueryStatus status ); 79 80 // GFXResource 81 virtual void zombify() = 0; 82 virtual void resurrect() = 0; 83 virtual const String describeSelf() const = 0; 84}; 85 86/// Handle for GFXOcclusionQuery than store last valid state 87class GFXOcclusionQueryHandle 88{ 89public: 90 91 GFXOcclusionQueryHandle() 92 : mLastStatus(GFXOcclusionQuery::Unset), mLastData(0), mWaiting(false) , mQuery(NULL) 93 {} 94 95 ~GFXOcclusionQueryHandle() 96 { 97 SAFE_DELETE(mQuery); 98 } 99 100 bool getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr = NULL, U32 *data = NULL ); 101 GFXOcclusionQuery* getQuery() const { return mQuery; } 102 103 void clearLastStatus() 104 { 105 mLastStatus = GFXOcclusionQuery::Unset; 106 mLastData = 0; 107 mWaiting = false; 108 109 if( !mQuery ) 110 return; 111 112 mQuery->begin(); 113 mQuery->end(); 114 } 115 116 bool isWaiting() const { return mWaiting; } 117protected: 118 GFXOcclusionQuery::OcclusionQueryStatus mLastStatus; 119 U32 mLastData; 120 bool mWaiting; 121 GFXOcclusionQuery *mQuery; 122}; 123 124inline bool GFXOcclusionQueryHandle::getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr, U32 *data ) 125{ 126 if( !mQuery ) 127 mQuery = GFX->createOcclusionQuery(); 128 129 GFXOcclusionQuery::OcclusionQueryStatus status = mQuery->getStatus( block, data ); 130 131 if( status == GFXOcclusionQuery::Waiting ) 132 { 133 mWaiting = true; 134 if( statusPtr ) 135 *statusPtr = mLastStatus; 136 if( data ) 137 *data = mLastData; 138 139 return true; 140 } 141 142 if( statusPtr ) 143 *statusPtr = status; 144 145 mWaiting = false; 146 mLastStatus = status; 147 mLastData = *data; 148 149 return true; 150} 151 152 153#endif // _GFXOCCLUSIONQUERY_H_ 154