2
0

BsD3D11Device.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11Device.h"
  4. #include "Error/BsException.h"
  5. namespace bs { namespace ct
  6. {
  7. D3D11Device::D3D11Device()
  8. :mD3D11Device(nullptr), mImmediateContext(nullptr), mClassLinkage(nullptr)
  9. {
  10. }
  11. D3D11Device::D3D11Device(ID3D11Device* device)
  12. : mD3D11Device(device)
  13. , mImmediateContext(nullptr)
  14. , mInfoQueue(nullptr)
  15. , mClassLinkage(nullptr)
  16. {
  17. assert(device != nullptr);
  18. if (device)
  19. {
  20. device->GetImmediateContext(&mImmediateContext);
  21. #if BS_DEBUG_MODE
  22. // This interface is not available unless we created the device with debug layer
  23. HRESULT hr = mD3D11Device->QueryInterface(__uuidof(ID3D11InfoQueue), (LPVOID*)&mInfoQueue);
  24. if (FAILED(hr))
  25. BS_EXCEPT(RenderingAPIException, "Unable to query D3D11InfoQueue");
  26. setExceptionsErrorLevel(D3D11ERR_ERROR);
  27. #endif
  28. // If feature level is 11, create class linkage
  29. SAFE_RELEASE(mClassLinkage);
  30. if (mD3D11Device->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0)
  31. {
  32. HRESULT hr = mD3D11Device->CreateClassLinkage(&mClassLinkage);
  33. if (FAILED(hr))
  34. BS_EXCEPT(RenderingAPIException, "Unable to create class linkage.");
  35. }
  36. }
  37. }
  38. D3D11Device::~D3D11Device()
  39. {
  40. shutdown();
  41. }
  42. void D3D11Device::shutdown()
  43. {
  44. if (mImmediateContext)
  45. {
  46. mImmediateContext->Flush();
  47. mImmediateContext->ClearState();
  48. }
  49. SAFE_RELEASE(mInfoQueue);
  50. SAFE_RELEASE(mD3D11Device);
  51. SAFE_RELEASE(mImmediateContext);
  52. SAFE_RELEASE(mClassLinkage);
  53. }
  54. String D3D11Device::getErrorDescription(bool doClearErrors)
  55. {
  56. if (mD3D11Device == nullptr)
  57. return "Null device.";
  58. String res;
  59. if (mInfoQueue != nullptr)
  60. {
  61. UINT64 numStoredMessages = mInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
  62. for (UINT64 i = 0 ; i < numStoredMessages ; i++ )
  63. {
  64. // Get the size of the message
  65. SIZE_T messageLength = 0;
  66. mInfoQueue->GetMessage(i, nullptr, &messageLength);
  67. // Allocate space and get the message
  68. D3D11_MESSAGE* pMessage = (D3D11_MESSAGE*)malloc(messageLength);
  69. mInfoQueue->GetMessage(i, pMessage, &messageLength);
  70. res = res + pMessage->pDescription + "\n";
  71. free(pMessage);
  72. }
  73. }
  74. if(doClearErrors)
  75. clearErrors();
  76. return res;
  77. }
  78. bool D3D11Device::hasError() const
  79. {
  80. if (mInfoQueue != nullptr)
  81. {
  82. UINT64 numStoredMessages = mInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
  83. if (numStoredMessages > 0)
  84. return true;
  85. return false;
  86. }
  87. else
  88. {
  89. return false;
  90. }
  91. }
  92. void D3D11Device::clearErrors()
  93. {
  94. if (mD3D11Device != nullptr && mInfoQueue != nullptr)
  95. {
  96. mInfoQueue->ClearStoredMessages();
  97. }
  98. }
  99. void D3D11Device::setExceptionsErrorLevel(const BS_D3D11_ERROR_LEVEL exceptionsErrorLevel)
  100. {
  101. if(mInfoQueue == nullptr)
  102. return;
  103. mInfoQueue->ClearRetrievalFilter();
  104. mInfoQueue->ClearStorageFilter();
  105. D3D11_INFO_QUEUE_FILTER filter;
  106. ZeroMemory(&filter, sizeof(D3D11_INFO_QUEUE_FILTER));
  107. Vector<D3D11_MESSAGE_SEVERITY> severityList;
  108. switch(exceptionsErrorLevel)
  109. {
  110. case D3D11ERR_INFO:
  111. severityList.push_back(D3D11_MESSAGE_SEVERITY_INFO);
  112. case D3D11ERR_WARNING:
  113. severityList.push_back(D3D11_MESSAGE_SEVERITY_WARNING);
  114. case D3D11ERR_ERROR:
  115. severityList.push_back(D3D11_MESSAGE_SEVERITY_ERROR);
  116. case D3D11ERR_CORRUPTION:
  117. severityList.push_back(D3D11_MESSAGE_SEVERITY_CORRUPTION);
  118. case D3D11ERR_NO_EXCEPTION:
  119. default:
  120. break;
  121. }
  122. if (severityList.size() > 0)
  123. {
  124. filter.AllowList.NumSeverities = (UINT)severityList.size();
  125. filter.AllowList.pSeverityList = &severityList[0];
  126. }
  127. mInfoQueue->AddStorageFilterEntries(&filter);
  128. mInfoQueue->AddRetrievalFilterEntries(&filter);
  129. }
  130. }}