2
0

BsD3D11OcclusionQuery.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11OcclusionQuery.h"
  4. #include "BsD3D11RenderAPI.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsD3D11CommandBuffer.h"
  7. #include "Profiling/BsRenderStats.h"
  8. #include "Math/BsMath.h"
  9. namespace bs { namespace ct
  10. {
  11. D3D11OcclusionQuery::D3D11OcclusionQuery(bool binary, UINT32 deviceIdx)
  12. :OcclusionQuery(binary), mContext(nullptr), mQuery(nullptr), mNumSamples(0), mFinalized(false), mQueryEndCalled(false)
  13. {
  14. assert(deviceIdx == 0 && "Multiple GPUs not supported natively on DirectX 11.");
  15. D3D11RenderAPI* rs = static_cast<D3D11RenderAPI*>(RenderAPI::instancePtr());
  16. D3D11Device& device = rs->getPrimaryDevice();
  17. D3D11_QUERY_DESC queryDesc;
  18. queryDesc.Query = mBinary ? D3D11_QUERY_OCCLUSION_PREDICATE : D3D11_QUERY_OCCLUSION;
  19. queryDesc.MiscFlags = 0;
  20. HRESULT hr = device.getD3D11Device()->CreateQuery(&queryDesc, &mQuery);
  21. if (hr != S_OK)
  22. BS_EXCEPT(RenderingAPIException, "Failed to create an occlusion query.");
  23. mContext = device.getImmediateContext();
  24. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Query);
  25. }
  26. D3D11OcclusionQuery::~D3D11OcclusionQuery()
  27. {
  28. if (mQuery != nullptr)
  29. mQuery->Release();
  30. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Query);
  31. }
  32. void D3D11OcclusionQuery::begin(const SPtr<CommandBuffer>& cb)
  33. {
  34. auto execute = [&]()
  35. {
  36. mContext->Begin(mQuery);
  37. mNumSamples = 0;
  38. mQueryEndCalled = false;
  39. setActive(true);
  40. };
  41. if (cb == nullptr)
  42. execute();
  43. else
  44. {
  45. SPtr<D3D11CommandBuffer> d3d11CB = std::static_pointer_cast<D3D11CommandBuffer>(cb);
  46. d3d11CB->queueCommand(execute);
  47. }
  48. }
  49. void D3D11OcclusionQuery::end(const SPtr<CommandBuffer>& cb)
  50. {
  51. auto execute = [&]()
  52. {
  53. mContext->End(mQuery);
  54. mQueryEndCalled = true;
  55. mFinalized = false;
  56. };
  57. if (cb == nullptr)
  58. execute();
  59. else
  60. {
  61. SPtr<D3D11CommandBuffer> d3d11CB = std::static_pointer_cast<D3D11CommandBuffer>(cb);
  62. d3d11CB->queueCommand(execute);
  63. }
  64. }
  65. bool D3D11OcclusionQuery::isReady() const
  66. {
  67. if (!mQueryEndCalled)
  68. return false;
  69. if (mBinary)
  70. {
  71. BOOL anySamples = FALSE;
  72. return mContext->GetData(mQuery, &anySamples, sizeof(anySamples), 0) == S_OK;
  73. }
  74. else
  75. {
  76. UINT64 numSamples = 0;
  77. return mContext->GetData(mQuery, &numSamples, sizeof(numSamples), 0) == S_OK;
  78. }
  79. }
  80. UINT32 D3D11OcclusionQuery::getNumSamples()
  81. {
  82. if (!mFinalized && isReady())
  83. {
  84. finalize();
  85. }
  86. return mNumSamples;
  87. }
  88. void D3D11OcclusionQuery::finalize()
  89. {
  90. mFinalized = true;
  91. if (mBinary)
  92. {
  93. BOOL anySamples = FALSE;
  94. mContext->GetData(mQuery, &anySamples, sizeof(anySamples), 0);
  95. mNumSamples = anySamples == TRUE ? 1 : 0;
  96. }
  97. else
  98. {
  99. UINT64 numSamples = 0;
  100. mContext->GetData(mQuery, &numSamples, sizeof(numSamples), 0);
  101. mNumSamples = (UINT32)numSamples;
  102. }
  103. }
  104. }}