gfxOcclusionQuery.h 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef _GFXOCCLUSIONQUERY_H_
#define _GFXOCCLUSIONQUERY_H_

#ifndef _GFXDEVICE_H_
#include "gfx/gfxDevice.h"
#endif


/// A geometry visibility query object.
/// @see GFXDevice::createOcclusionQuery
class GFXOcclusionQuery : public GFXResource
{
protected:

   GFXDevice *mDevice;
   
   GFXOcclusionQuery( GFXDevice *device ) 
      : mDevice( device )
   {
   }   

public:

   /// The states returned by getStatus()
   /// If you modify this enum you should also modify statusToString()
   enum OcclusionQueryStatus
   {
      Unset,         ///<      
      Waiting,       ///< 
      Error,         ///< 
      Occluded,
      NotOccluded
   };   
      
   virtual ~GFXOcclusionQuery() {}
   
   /// Prepares the query returning true if the last query
   /// has been processed and more geometry can be issued.
   /// @see getStatus
   virtual bool begin() = 0;
   
   /// Called after your geometry is drawn to submit
   /// the query for processing.
   virtual void end() = 0;   
   
   /// Returns the status of the last submitted query.  In general
   /// you should avoid blocking for the result until the frame 
   /// following your query to keep from stalling the CPU.
   /// @return       Status    
   /// @param block  If true CPU will block until the query finishes.
   /// @param data   Number of pixels rendered, valid only if status returned is NotOccluded.
   virtual OcclusionQueryStatus getStatus( bool block, U32 *data = NULL ) = 0;
   
   /// Returns a status string.
   static String statusToString( OcclusionQueryStatus status );

   // GFXResource
   virtual void zombify() = 0;   
   virtual void resurrect() = 0;
   virtual const String describeSelf() const = 0;
};

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/// Handle for GFXOcclusionQuery than store last valid state
class GFXOcclusionQueryHandle
{
public:

    GFXOcclusionQueryHandle() 
        : mLastStatus(GFXOcclusionQuery::Unset), mLastData(0), mQuery(NULL), mWaiting(false) 
    {}

    ~GFXOcclusionQueryHandle()
    {
        SAFE_DELETE(mQuery);
    }

    bool getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr = NULL, U32 *data = NULL );
    GFXOcclusionQuery* getQuery() const { return mQuery; }

    void clearLastStatus()
    {
        mLastStatus = GFXOcclusionQuery::Unset;
        mLastData = 0;
        mWaiting = false;

        if( !mQuery )
            return;

        mQuery->begin();
        mQuery->end();
    }

    bool isWaiting() const { return mWaiting; }
protected:
    GFXOcclusionQuery::OcclusionQueryStatus mLastStatus;
    U32 mLastData;
    bool mWaiting;
    GFXOcclusionQuery *mQuery;
};

inline bool GFXOcclusionQueryHandle::getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr, U32 *data )
{
    if( !mQuery )
        mQuery = GFX->createOcclusionQuery();

    GFXOcclusionQuery::OcclusionQueryStatus status = mQuery->getStatus( block, data );

    if( status == GFXOcclusionQuery::Waiting )
    {
        mWaiting = true;
        if( statusPtr )
            *statusPtr = mLastStatus;
        if( data )
            *data = mLastData;

        return true;
    }

    if( statusPtr )
        *statusPtr = status;

    mWaiting = false;
    mLastStatus = status;
    mLastData = *data;

    return true;
}


152
#endif // _GFXOCCLUSIONQUERY_H_