Torque3D Documentation / _generateds / gfxD3D11OcclusionQuery.cpp

gfxD3D11OcclusionQuery.cpp

Engine/source/gfx/D3D11/gfxD3D11OcclusionQuery.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2015 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 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
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "gfx/D3D11/gfxD3D11Device.h"
 25#include "gfx/D3D11/gfxD3D11OcclusionQuery.h"
 26
 27#include "gui/3d/guiTSControl.h"
 28
 29#ifdef TORQUE_GATHER_METRICS
 30// For TickMs define
 31#include "T3D/gameBase/processList.h"
 32#endif
 33
 34GFXD3D11OcclusionQuery::GFXD3D11OcclusionQuery(GFXDevice *device)
 35 : GFXOcclusionQuery(device), 
 36   mQuery(NULL), 
 37   mTesting(false)   
 38{
 39#ifdef TORQUE_GATHER_METRICS
 40   mTimer = PlatformTimer::create();
 41   mTimer->getElapsedMs();
 42
 43   mTimeSinceEnd = 0;
 44   mBeginFrame = 0;
 45#endif
 46}
 47
 48GFXD3D11OcclusionQuery::~GFXD3D11OcclusionQuery()
 49{
 50   SAFE_RELEASE(mQuery);
 51
 52#ifdef TORQUE_GATHER_METRICS
 53   SAFE_DELETE(mTimer);
 54#endif
 55}
 56
 57bool GFXD3D11OcclusionQuery::begin()
 58{
 59   if(GFXDevice::getDisableOcclusionQuery())
 60      return true;
 61
 62   if (mQuery == NULL)
 63   {
 64      D3D11_QUERY_DESC queryDesc;
 65      queryDesc.Query = D3D11_QUERY_OCCLUSION;
 66      queryDesc.MiscFlags = 0;
 67
 68      HRESULT hRes = D3D11DEVICE->CreateQuery(&queryDesc, &mQuery);
 69
 70      if(FAILED(hRes))
 71      {
 72         AssertFatal(false, "GFXD3D11OcclusionQuery::begin - Hardware does not support D3D11 Occlusion-Queries, this should be caught before this type is created");
 73      }
 74
 75      AssertISV(hRes != E_OUTOFMEMORY, "GFXD3D11OcclusionQuery::begin - Out of memory");
 76   }
 77
 78   if (!mTesting)
 79   {
 80      D3D11DEVICECONTEXT->Begin(mQuery);
 81      mTesting = true;
 82   }
 83
 84#ifdef TORQUE_GATHER_METRICS
 85   mBeginFrame = GuiTSCtrl::getFrameCount();
 86#endif
 87
 88   return true;
 89}
 90
 91void GFXD3D11OcclusionQuery::end()
 92{
 93   if (GFXDevice::getDisableOcclusionQuery())
 94      return;
 95
 96   // Add an end marker to the command buffer queue.
 97   D3D11DEVICECONTEXT->End(mQuery);
 98   mTesting = false;
 99
100#ifdef TORQUE_GATHER_METRICS
101   AssertFatal( mBeginFrame == GuiTSCtrl::getFrameCount(), "GFXD3D11OcclusionQuery::end - ended query on different frame than begin!" );   
102   mTimer->getElapsedMs();
103   mTimer->reset();
104#endif
105}
106
107GFXD3D11OcclusionQuery::OcclusionQueryStatus GFXD3D11OcclusionQuery::getStatus(bool block, U32 *data)
108{
109   // If this ever shows up near the top of a profile then your system is 
110   // GPU bound or you are calling getStatus too soon after submitting it.
111   //
112   // To test if you are GPU bound resize your window very small and see if
113   // this profile no longer appears at the top.
114   //
115   // To test if you are calling getStatus to soon after submitting it,
116   // check the value of mTimeSinceEnd in a debug build. If it is < half the length
117   // of time to render an individual frame you could have problems.
118   PROFILE_SCOPE(GFXD3D11OcclusionQuery_getStatus);
119
120   if ( GFXDevice::getDisableOcclusionQuery() )
121      return NotOccluded;
122
123   if ( mQuery == NULL )
124      return Unset;
125
126#ifdef TORQUE_GATHER_METRICS
127   //AssertFatal( mBeginFrame < GuiTSCtrl::getFrameCount(), "GFXD3D11OcclusionQuery::getStatus - called on the same frame as begin!" );
128
129   //U32 mTimeSinceEnd = mTimer->getElapsedMs();
130   //AssertFatal( mTimeSinceEnd >= 5, "GFXD3DOcculsionQuery::getStatus - less than TickMs since called ::end!" );
131#endif
132
133   HRESULT hRes;
134   U64 dwOccluded = 0;
135
136   if ( block )
137   {      
138      while ((hRes = D3D11DEVICECONTEXT->GetData(mQuery, &dwOccluded, sizeof(U64), 0)) == S_FALSE);
139   }
140   else
141   {
142      hRes = D3D11DEVICECONTEXT->GetData(mQuery, &dwOccluded, sizeof(U64), 0);
143   }
144
145   if (hRes == S_OK)   
146   {
147      if (data != NULL)
148         *data = (U32)dwOccluded;
149
150      return dwOccluded > 0 ? NotOccluded : Occluded;   
151   }
152
153   if (hRes == S_FALSE)
154      return Waiting;
155
156   return Error;   
157}
158
159void GFXD3D11OcclusionQuery::zombify()
160{
161   SAFE_RELEASE( mQuery );
162}
163
164void GFXD3D11OcclusionQuery::resurrect()
165{
166   // Recreate the query 
167   if( mQuery == NULL ) 
168   { 
169      D3D11_QUERY_DESC queryDesc;
170      queryDesc.Query = D3D11_QUERY_OCCLUSION;
171      queryDesc.MiscFlags = 0;
172
173      HRESULT hRes = D3D11DEVICE->CreateQuery(&queryDesc, &mQuery); 
174   
175      AssertISV( hRes != E_OUTOFMEMORY, "GFXD3D11QueryFence::resurrect - Out of memory" ); 
176   } 
177}
178
179const String GFXD3D11OcclusionQuery::describeSelf() const
180{
181   // We've got nothing
182   return String();
183}
184