gfxD3D11OcclusionQuery.cpp 5.2 KB

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