gfxD3D11OcclusionQuery.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2015 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. #include "gfx/D3D11/gfxD3D11Device.h"
  23. #include "gfx/D3D11/gfxD3D11OcclusionQuery.h"
  24. #include "gui/3d/guiTSControl.h"
  25. #ifdef TORQUE_GATHER_METRICS
  26. // For TickMs define
  27. #include "T3D/gameBase/processList.h"
  28. #endif
  29. GFXD3D11OcclusionQuery::GFXD3D11OcclusionQuery(GFXDevice *device)
  30. : GFXOcclusionQuery(device),
  31. mQuery(NULL),
  32. mTesting(false)
  33. {
  34. #ifdef TORQUE_GATHER_METRICS
  35. mTimer = PlatformTimer::create();
  36. mTimer->getElapsedMs();
  37. mTimeSinceEnd = 0;
  38. mBeginFrame = 0;
  39. #endif
  40. }
  41. GFXD3D11OcclusionQuery::~GFXD3D11OcclusionQuery()
  42. {
  43. SAFE_RELEASE(mQuery);
  44. #ifdef TORQUE_GATHER_METRICS
  45. SAFE_DELETE(mTimer);
  46. #endif
  47. }
  48. bool GFXD3D11OcclusionQuery::begin()
  49. {
  50. if(GFXDevice::getDisableOcclusionQuery())
  51. return true;
  52. if (mQuery == NULL)
  53. {
  54. D3D11_QUERY_DESC queryDesc;
  55. queryDesc.Query = D3D11_QUERY_OCCLUSION;
  56. queryDesc.MiscFlags = 0;
  57. HRESULT hRes = D3D11DEVICE->CreateQuery(&queryDesc, &mQuery);
  58. if(FAILED(hRes))
  59. {
  60. AssertFatal(false, "GFXD3D11OcclusionQuery::begin - Hardware does not support D3D11 Occlusion-Queries, this should be caught before this type is created");
  61. }
  62. AssertISV(hRes != E_OUTOFMEMORY, "GFXD3D11OcclusionQuery::begin - Out of memory");
  63. }
  64. if (!mTesting)
  65. {
  66. D3D11DEVICECONTEXT->Begin(mQuery);
  67. mTesting = true;
  68. }
  69. #ifdef TORQUE_GATHER_METRICS
  70. mBeginFrame = GuiTSCtrl::getFrameCount();
  71. #endif
  72. return true;
  73. }
  74. void GFXD3D11OcclusionQuery::end()
  75. {
  76. if (GFXDevice::getDisableOcclusionQuery())
  77. return;
  78. // Add an end marker to the command buffer queue.
  79. D3D11DEVICECONTEXT->End(mQuery);
  80. mTesting = false;
  81. #ifdef TORQUE_GATHER_METRICS
  82. AssertFatal( mBeginFrame == GuiTSCtrl::getFrameCount(), "GFXD3D11OcclusionQuery::end - ended query on different frame than begin!" );
  83. mTimer->getElapsedMs();
  84. mTimer->reset();
  85. #endif
  86. }
  87. GFXD3D11OcclusionQuery::OcclusionQueryStatus GFXD3D11OcclusionQuery::getStatus(bool block, U32 *data)
  88. {
  89. // If this ever shows up near the top of a profile then your system is
  90. // GPU bound or you are calling getStatus too soon after submitting it.
  91. //
  92. // To test if you are GPU bound resize your window very small and see if
  93. // this profile no longer appears at the top.
  94. //
  95. // To test if you are calling getStatus to soon after submitting it,
  96. // check the value of mTimeSinceEnd in a debug build. If it is < half the length
  97. // of time to render an individual frame you could have problems.
  98. PROFILE_SCOPE(GFXD3D11OcclusionQuery_getStatus);
  99. if ( GFXDevice::getDisableOcclusionQuery() )
  100. return NotOccluded;
  101. if ( mQuery == NULL )
  102. return Unset;
  103. #ifdef TORQUE_GATHER_METRICS
  104. //AssertFatal( mBeginFrame < GuiTSCtrl::getFrameCount(), "GFXD3D11OcclusionQuery::getStatus - called on the same frame as begin!" );
  105. //U32 mTimeSinceEnd = mTimer->getElapsedMs();
  106. //AssertFatal( mTimeSinceEnd >= 5, "GFXD3DOcculsionQuery::getStatus - less than TickMs since called ::end!" );
  107. #endif
  108. HRESULT hRes;
  109. U64 dwOccluded = 0;
  110. if ( block )
  111. {
  112. while ((hRes = D3D11DEVICECONTEXT->GetData(mQuery, &dwOccluded, sizeof(U64), 0)) == S_FALSE);
  113. }
  114. else
  115. {
  116. hRes = D3D11DEVICECONTEXT->GetData(mQuery, &dwOccluded, sizeof(U64), 0);
  117. }
  118. if (hRes == S_OK)
  119. {
  120. if (data != NULL)
  121. *data = (U32)dwOccluded;
  122. return dwOccluded > 0 ? NotOccluded : Occluded;
  123. }
  124. if (hRes == S_FALSE)
  125. return Waiting;
  126. return Error;
  127. }
  128. void GFXD3D11OcclusionQuery::zombify()
  129. {
  130. SAFE_RELEASE( mQuery );
  131. }
  132. void GFXD3D11OcclusionQuery::resurrect()
  133. {
  134. // Recreate the query
  135. if( mQuery == NULL )
  136. {
  137. D3D11_QUERY_DESC queryDesc;
  138. queryDesc.Query = D3D11_QUERY_OCCLUSION;
  139. queryDesc.MiscFlags = 0;
  140. HRESULT hRes = D3D11DEVICE->CreateQuery(&queryDesc, &mQuery);
  141. AssertISV( hRes != E_OUTOFMEMORY, "GFXD3D11QueryFence::resurrect - Out of memory" );
  142. }
  143. }
  144. const String GFXD3D11OcclusionQuery::describeSelf() const
  145. {
  146. // We've got nothing
  147. return String();
  148. }