BsD3D11Device.cpp 3.9 KB

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