gfxOcclusionQuery.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFXOCCLUSIONQUERY_H_
  23. #define _GFXOCCLUSIONQUERY_H_
  24. #ifndef _GFXDEVICE_H_
  25. #include "gfx/gfxDevice.h"
  26. #endif
  27. /// A geometry visibility query object.
  28. /// @see GFXDevice::createOcclusionQuery
  29. class GFXOcclusionQuery : public GFXResource
  30. {
  31. protected:
  32. GFXDevice *mDevice;
  33. GFXOcclusionQuery( GFXDevice *device )
  34. : mDevice( device )
  35. {
  36. }
  37. public:
  38. /// The states returned by getStatus()
  39. /// If you modify this enum you should also modify statusToString()
  40. enum OcclusionQueryStatus
  41. {
  42. Unset, ///<
  43. Waiting, ///<
  44. Error, ///<
  45. Occluded,
  46. NotOccluded
  47. };
  48. virtual ~GFXOcclusionQuery() {}
  49. /// Prepares the query returning true if the last query
  50. /// has been processed and more geometry can be issued.
  51. /// @see getStatus
  52. virtual bool begin() = 0;
  53. /// Called after your geometry is drawn to submit
  54. /// the query for processing.
  55. virtual void end() = 0;
  56. /// Returns the status of the last submitted query. In general
  57. /// you should avoid blocking for the result until the frame
  58. /// following your query to keep from stalling the CPU.
  59. /// @return Status
  60. /// @param block If true CPU will block until the query finishes.
  61. /// @param data Number of pixels rendered, valid only if status returned is NotOccluded.
  62. virtual OcclusionQueryStatus getStatus( bool block, U32 *data = NULL ) = 0;
  63. /// Returns a status string.
  64. static String statusToString( OcclusionQueryStatus status );
  65. // GFXResource
  66. virtual void zombify() = 0;
  67. virtual void resurrect() = 0;
  68. virtual const String describeSelf() const = 0;
  69. };
  70. /// Handle for GFXOcclusionQuery than store last valid state
  71. class GFXOcclusionQueryHandle
  72. {
  73. public:
  74. GFXOcclusionQueryHandle()
  75. : mLastStatus(GFXOcclusionQuery::Unset), mLastData(0), mWaiting(false) , mQuery(NULL)
  76. {}
  77. ~GFXOcclusionQueryHandle()
  78. {
  79. SAFE_DELETE(mQuery);
  80. }
  81. bool getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr = NULL, U32 *data = NULL );
  82. GFXOcclusionQuery* getQuery() const { return mQuery; }
  83. void clearLastStatus()
  84. {
  85. mLastStatus = GFXOcclusionQuery::Unset;
  86. mLastData = 0;
  87. mWaiting = false;
  88. if( !mQuery )
  89. return;
  90. mQuery->begin();
  91. mQuery->end();
  92. }
  93. bool isWaiting() const { return mWaiting; }
  94. protected:
  95. GFXOcclusionQuery::OcclusionQueryStatus mLastStatus;
  96. U32 mLastData;
  97. bool mWaiting;
  98. GFXOcclusionQuery *mQuery;
  99. };
  100. inline bool GFXOcclusionQueryHandle::getLastStatus( bool block, GFXOcclusionQuery::OcclusionQueryStatus *statusPtr, U32 *data )
  101. {
  102. if( !mQuery )
  103. mQuery = GFX->createOcclusionQuery();
  104. GFXOcclusionQuery::OcclusionQueryStatus status = mQuery->getStatus( block, data );
  105. if( status == GFXOcclusionQuery::Waiting )
  106. {
  107. mWaiting = true;
  108. if( statusPtr )
  109. *statusPtr = mLastStatus;
  110. if( data )
  111. *data = mLastData;
  112. return true;
  113. }
  114. if( statusPtr )
  115. *statusPtr = status;
  116. mWaiting = false;
  117. mLastStatus = status;
  118. mLastData = *data;
  119. return true;
  120. }
  121. #endif // _GFXOCCLUSIONQUERY_H_