BsD3D11Device.cpp 3.5 KB

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