gfxD3D9OcclusionQuery.cpp 5.2 KB

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