CmD3D11Device.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "CmD3D11Device.h"
  2. #include "CmException.h"
  3. namespace CamelotEngine
  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. HRESULT hr = mD3D11Device->QueryInterface(__uuidof(ID3D11InfoQueue), (LPVOID*)&mInfoQueue);
  20. if (FAILED(hr))
  21. CM_EXCEPT(RenderingAPIException, "Unable to query D3D11InfoQueue");
  22. // If feature level is 11, create class linkage
  23. SAFE_RELEASE(mClassLinkage);
  24. if (mD3D11Device->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0)
  25. {
  26. HRESULT hr = mD3D11Device->CreateClassLinkage(&mClassLinkage);
  27. if (FAILED(hr))
  28. CM_EXCEPT(RenderingAPIException, "Unable to create class linkage.");
  29. }
  30. }
  31. }
  32. D3D11Device::~D3D11Device()
  33. {
  34. }
  35. void D3D11Device::shutdown()
  36. {
  37. // Clear state
  38. if (mImmediateContext)
  39. {
  40. mImmediateContext->Flush();
  41. mImmediateContext->ClearState();
  42. }
  43. SAFE_RELEASE(mInfoQueue);
  44. SAFE_RELEASE(mD3D11Device);
  45. SAFE_RELEASE(mImmediateContext);
  46. SAFE_RELEASE(mClassLinkage);
  47. }
  48. String D3D11Device::getErrorDescription(bool doClearErrors)
  49. {
  50. if (mD3D11Device == nullptr)
  51. return "NULL device";
  52. String res;
  53. if (mInfoQueue != nullptr)
  54. {
  55. UINT64 numStoredMessages = mInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
  56. for (UINT64 i = 0 ; i < numStoredMessages ; i++ )
  57. {
  58. // Get the size of the message
  59. SIZE_T messageLength = 0;
  60. mInfoQueue->GetMessage(i, nullptr, &messageLength);
  61. // Allocate space and get the message
  62. D3D11_MESSAGE* pMessage = (D3D11_MESSAGE*)malloc(messageLength);
  63. mInfoQueue->GetMessage(i, pMessage, &messageLength);
  64. res = res + pMessage->pDescription + "\n";
  65. free(pMessage);
  66. }
  67. }
  68. if(doClearErrors)
  69. clearError();
  70. return res;
  71. }
  72. bool D3D11Device::hasError() const
  73. {
  74. if (mInfoQueue != nullptr)
  75. {
  76. UINT64 numStoredMessages = mInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
  77. if (numStoredMessages > 0)
  78. return true;
  79. return false;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. void D3D11Device::clearError()
  87. {
  88. if (mD3D11Device != nullptr && mInfoQueue != nullptr)
  89. {
  90. mInfoQueue->ClearStoredMessages();
  91. }
  92. }
  93. void D3D11Device::setExceptionsErrorLevel(const CM_D3D11_ERROR_LEVEL exceptionsErrorLevel)
  94. {
  95. if(mInfoQueue == nullptr)
  96. return;
  97. mInfoQueue->ClearRetrievalFilter();
  98. mInfoQueue->ClearStorageFilter();
  99. D3D11_INFO_QUEUE_FILTER filter;
  100. ZeroMemory(&filter, sizeof(D3D11_INFO_QUEUE_FILTER));
  101. std::vector<D3D11_MESSAGE_SEVERITY> severityList;
  102. switch(exceptionsErrorLevel)
  103. {
  104. case D3D11ERR_NO_EXCEPTION:
  105. severityList.push_back(D3D11_MESSAGE_SEVERITY_CORRUPTION);
  106. case D3D11ERR_CORRUPTION:
  107. severityList.push_back(D3D11_MESSAGE_SEVERITY_ERROR);
  108. case D3D11ERR_ERROR:
  109. severityList.push_back(D3D11_MESSAGE_SEVERITY_WARNING);
  110. case D3D11ERR_WARNING:
  111. case D3D11ERR_INFO:
  112. severityList.push_back(D3D11_MESSAGE_SEVERITY_INFO);
  113. default:
  114. break;
  115. }
  116. if (severityList.size() > 0)
  117. {
  118. filter.DenyList.NumSeverities = (UINT)severityList.size();
  119. filter.DenyList.pSeverityList = &severityList[0];
  120. }
  121. mInfoQueue->AddStorageFilterEntries(&filter);
  122. mInfoQueue->AddRetrievalFilterEntries(&filter);
  123. }
  124. }