OgreD3D9HardwareOcclusionQuery.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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 FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreD3D9HardwareOcclusionQuery.h"
  25. #include "CmRenderSystemCapabilities.h"
  26. #include "OgreException.h"
  27. #include "OgreD3D9RenderSystem.h"
  28. namespace CamelotEngine {
  29. /**
  30. * This is a class that is the DirectX9 implementation of
  31. * hardware occlusion testing.
  32. *
  33. * @author Lee Sandberg
  34. *
  35. * Updated on 12/7/2004 by Chris McGuirk
  36. * Updated on 4/8/2005 by Tuan Kuranes email: [email protected]
  37. */
  38. /**
  39. * Default object constructor
  40. */
  41. D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery()
  42. {
  43. }
  44. /**
  45. * Object destructor
  46. */
  47. D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery()
  48. {
  49. DeviceToQueryIterator it = mMapDeviceToQuery.begin();
  50. while (it != mMapDeviceToQuery.end())
  51. {
  52. SAFE_RELEASE(it->second);
  53. ++it;
  54. }
  55. mMapDeviceToQuery.clear();
  56. }
  57. //------------------------------------------------------------------
  58. // Occlusion query functions (see base class documentation for this)
  59. //--
  60. void D3D9HardwareOcclusionQuery::beginOcclusionQuery()
  61. {
  62. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  63. DeviceToQueryIterator it = mMapDeviceToQuery.find(pCurDevice);
  64. // No resource exits for current device -> create it.
  65. if (it == mMapDeviceToQuery.end() || it->second == NULL)
  66. createQuery(pCurDevice);
  67. // Grab the query of the current device.
  68. IDirect3DQuery9* pOccQuery = mMapDeviceToQuery[pCurDevice];
  69. if (pOccQuery != NULL)
  70. {
  71. pOccQuery->Issue(D3DISSUE_BEGIN);
  72. mIsQueryResultStillOutstanding = true;
  73. mPixelCount = 0;
  74. }
  75. }
  76. void D3D9HardwareOcclusionQuery::endOcclusionQuery()
  77. {
  78. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  79. DeviceToQueryIterator it = mMapDeviceToQuery.find(pCurDevice);
  80. if (it == mMapDeviceToQuery.end())
  81. {
  82. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  83. "End occlusion called without matching begin call !!",
  84. "D3D9HardwareOcclusionQuery::endOcclusionQuery" );
  85. }
  86. IDirect3DQuery9* pOccQuery = mMapDeviceToQuery[pCurDevice];
  87. if (pOccQuery != NULL)
  88. pOccQuery->Issue(D3DISSUE_END);
  89. }
  90. //------------------------------------------------------------------
  91. bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments )
  92. {
  93. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  94. DeviceToQueryIterator it = mMapDeviceToQuery.find(pCurDevice);
  95. if (it == mMapDeviceToQuery.end())
  96. return false;
  97. if (it->second == NULL)
  98. return false;
  99. // in case you didn't check if query arrived and want the result now.
  100. if (mIsQueryResultStillOutstanding)
  101. {
  102. // Loop until the data becomes available
  103. DWORD pixels;
  104. const size_t dataSize = sizeof( DWORD );
  105. while (1)
  106. {
  107. const HRESULT hr = it->second->GetData((void *)&pixels, dataSize, D3DGETDATA_FLUSH);
  108. if (hr == S_FALSE)
  109. continue;
  110. if (hr == S_OK)
  111. {
  112. mPixelCount = pixels;
  113. *NumOfFragments = pixels;
  114. break;
  115. }
  116. if (hr == D3DERR_DEVICELOST)
  117. {
  118. *NumOfFragments = 0;
  119. mPixelCount = 0;
  120. SAFE_RELEASE(it->second);
  121. break;
  122. }
  123. }
  124. mIsQueryResultStillOutstanding = false;
  125. }
  126. else
  127. {
  128. // we already stored result from last frames.
  129. *NumOfFragments = mPixelCount;
  130. }
  131. return true;
  132. }
  133. //------------------------------------------------------------------
  134. unsigned int D3D9HardwareOcclusionQuery::getLastQuerysPixelcount()
  135. {
  136. return mPixelCount;
  137. }
  138. //------------------------------------------------------------------
  139. bool D3D9HardwareOcclusionQuery::isStillOutstanding(void)
  140. {
  141. // in case you already asked for this query
  142. if (!mIsQueryResultStillOutstanding)
  143. return false;
  144. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  145. DeviceToQueryIterator it = mMapDeviceToQuery.find(pCurDevice);
  146. if (it == mMapDeviceToQuery.end())
  147. return false;
  148. if (it->second == NULL)
  149. return false;
  150. DWORD pixels;
  151. const HRESULT hr = it->second->GetData( (void *) &pixels, sizeof( DWORD ), 0);
  152. if (hr == S_FALSE)
  153. return true;
  154. if (hr == D3DERR_DEVICELOST)
  155. {
  156. mPixelCount = 100000;
  157. SAFE_RELEASE(it->second);
  158. }
  159. mPixelCount = pixels;
  160. mIsQueryResultStillOutstanding = false;
  161. return false;
  162. }
  163. //------------------------------------------------------------------
  164. void D3D9HardwareOcclusionQuery::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  165. {
  166. }
  167. //------------------------------------------------------------------
  168. void D3D9HardwareOcclusionQuery::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  169. {
  170. releaseQuery(d3d9Device);
  171. }
  172. //------------------------------------------------------------------
  173. void D3D9HardwareOcclusionQuery::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
  174. {
  175. releaseQuery(d3d9Device);
  176. }
  177. //------------------------------------------------------------------
  178. void D3D9HardwareOcclusionQuery::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
  179. {
  180. }
  181. //------------------------------------------------------------------
  182. void D3D9HardwareOcclusionQuery::createQuery(IDirect3DDevice9* d3d9Device)
  183. {
  184. HRESULT hr;
  185. // Check if query supported.
  186. hr = d3d9Device->CreateQuery(D3DQUERYTYPE_OCCLUSION, NULL);
  187. if (FAILED(hr))
  188. {
  189. mMapDeviceToQuery[d3d9Device] = NULL;
  190. return;
  191. }
  192. // create the occlusion query.
  193. IDirect3DQuery9* pCurQuery;
  194. hr = d3d9Device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pCurQuery);
  195. mMapDeviceToQuery[d3d9Device] = pCurQuery;
  196. }
  197. //------------------------------------------------------------------
  198. void D3D9HardwareOcclusionQuery::releaseQuery(IDirect3DDevice9* d3d9Device)
  199. {
  200. DeviceToQueryIterator it = mMapDeviceToQuery.find(d3d9Device);
  201. // Remove from query resource map.
  202. if (it != mMapDeviceToQuery.end())
  203. {
  204. SAFE_RELEASE(it->second);
  205. mMapDeviceToQuery.erase(it);
  206. }
  207. }
  208. }