BsD3D11InputLayoutManager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11InputLayoutManager.h"
  4. #include "BsD3D11Mappings.h"
  5. #include "BsD3D11RenderAPI.h"
  6. #include "BsD3D11Device.h"
  7. #include "BsD3D11GpuProgram.h"
  8. #include "Managers/BsHardwareBufferManager.h"
  9. #include "Profiling/BsRenderStats.h"
  10. #include "Debug/BsDebug.h"
  11. namespace bs { namespace ct
  12. {
  13. size_t D3D11InputLayoutManager::HashFunc::operator()
  14. (const D3D11InputLayoutManager::VertexDeclarationKey &key) const
  15. {
  16. size_t hash = 0;
  17. hash_combine(hash, key.vertxDeclId);
  18. hash_combine(hash, key.vertexProgramId);
  19. return hash;
  20. }
  21. bool D3D11InputLayoutManager::EqualFunc::operator()
  22. (const D3D11InputLayoutManager::VertexDeclarationKey &a, const D3D11InputLayoutManager::VertexDeclarationKey &b) const
  23. {
  24. if (a.vertxDeclId != b.vertxDeclId)
  25. return false;
  26. if(a.vertexProgramId != b.vertexProgramId)
  27. return false;
  28. return true;
  29. }
  30. D3D11InputLayoutManager::D3D11InputLayoutManager()
  31. :mLastUsedCounter(0), mWarningShown(false)
  32. {
  33. }
  34. D3D11InputLayoutManager::~D3D11InputLayoutManager()
  35. {
  36. while(mInputLayoutMap.begin() != mInputLayoutMap.end())
  37. {
  38. auto firstElem = mInputLayoutMap.begin();
  39. SAFE_RELEASE(firstElem->second->inputLayout);
  40. bs_delete(firstElem->second);
  41. mInputLayoutMap.erase(firstElem);
  42. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_InputLayout);
  43. }
  44. }
  45. ID3D11InputLayout* D3D11InputLayoutManager::retrieveInputLayout(const SPtr<VertexDeclaration>& vertexShaderDecl,
  46. const SPtr<VertexDeclaration>& vertexBufferDecl, D3D11GpuProgram& vertexProgram)
  47. {
  48. VertexDeclarationKey pair;
  49. pair.vertxDeclId = vertexBufferDecl->getId();
  50. pair.vertexProgramId = vertexProgram.getProgramId();
  51. auto iterFind = mInputLayoutMap.find(pair);
  52. if(iterFind == mInputLayoutMap.end())
  53. {
  54. if(mInputLayoutMap.size() >= DECLARATION_BUFFER_SIZE)
  55. removeLeastUsed(); // Prune so the buffer doesn't just infinitely grow
  56. addNewInputLayout(vertexShaderDecl, vertexBufferDecl, vertexProgram);
  57. iterFind = mInputLayoutMap.find(pair);
  58. if(iterFind == mInputLayoutMap.end()) // We failed to create input layout
  59. return nullptr;
  60. }
  61. iterFind->second->lastUsedIdx = ++mLastUsedCounter;
  62. return iterFind->second->inputLayout;
  63. }
  64. void D3D11InputLayoutManager::addNewInputLayout(const SPtr<VertexDeclaration>& vertexShaderDecl,
  65. const SPtr<VertexDeclaration>& vertexBufferDecl, D3D11GpuProgram& vertexProgram)
  66. {
  67. const VertexDeclarationProperties& bufferDeclProps = vertexBufferDecl->getProperties();
  68. const VertexDeclarationProperties& shaderDeclProps = vertexShaderDecl->getProperties();
  69. Vector<D3D11_INPUT_ELEMENT_DESC> declElements;
  70. const List<VertexElement>& bufferElems = bufferDeclProps.getElements();
  71. const List<VertexElement>& shaderElems = shaderDeclProps.getElements();
  72. INT32 maxStreamIdx = -1;
  73. for (auto iter = bufferElems.begin(); iter != bufferElems.end(); ++iter)
  74. {
  75. declElements.push_back(D3D11_INPUT_ELEMENT_DESC());
  76. D3D11_INPUT_ELEMENT_DESC& elementDesc = declElements.back();
  77. elementDesc.SemanticName = D3D11Mappings::get(iter->getSemantic());
  78. elementDesc.SemanticIndex = iter->getSemanticIdx();
  79. elementDesc.Format = D3D11Mappings::get(iter->getType());
  80. elementDesc.InputSlot = iter->getStreamIdx();
  81. elementDesc.AlignedByteOffset = static_cast<WORD>(iter->getOffset());
  82. if (iter->getInstanceStepRate() == 0)
  83. {
  84. elementDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  85. elementDesc.InstanceDataStepRate = 0;
  86. }
  87. else
  88. {
  89. elementDesc.InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
  90. elementDesc.InstanceDataStepRate = iter->getInstanceStepRate();
  91. }
  92. maxStreamIdx = std::max(maxStreamIdx, (INT32)iter->getStreamIdx());
  93. }
  94. // Find elements missing in buffer and add a dummy stream for them
  95. for (auto shaderIter = shaderElems.begin(); shaderIter != shaderElems.end(); ++shaderIter)
  96. {
  97. bool foundElement = false;
  98. for (auto bufferIter = bufferElems.begin(); bufferIter != bufferElems.end(); ++bufferIter)
  99. {
  100. if (shaderIter->getSemantic() == bufferIter->getSemantic() && shaderIter->getSemanticIdx() == bufferIter->getSemanticIdx())
  101. {
  102. foundElement = true;
  103. break;
  104. }
  105. }
  106. if (!foundElement)
  107. {
  108. declElements.push_back(D3D11_INPUT_ELEMENT_DESC());
  109. D3D11_INPUT_ELEMENT_DESC& elementDesc = declElements.back();
  110. elementDesc.SemanticName = D3D11Mappings::get(shaderIter->getSemantic());
  111. elementDesc.SemanticIndex = shaderIter->getSemanticIdx();
  112. elementDesc.Format = D3D11Mappings::get(shaderIter->getType());
  113. elementDesc.InputSlot = (UINT32)(maxStreamIdx + 1);
  114. elementDesc.AlignedByteOffset = 0;
  115. elementDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  116. elementDesc.InstanceDataStepRate = 0;
  117. }
  118. }
  119. D3D11RenderAPI* d3d11rs = static_cast<D3D11RenderAPI*>(RenderAPI::instancePtr());
  120. D3D11Device& device = d3d11rs->getPrimaryDevice();
  121. const HLSLMicroCode& microcode = vertexProgram.getMicroCode();
  122. InputLayoutEntry* newEntry = bs_new<InputLayoutEntry>();
  123. newEntry->lastUsedIdx = ++mLastUsedCounter;
  124. newEntry->inputLayout = nullptr;
  125. HRESULT hr = device.getD3D11Device()->CreateInputLayout(
  126. &declElements[0],
  127. (UINT32)declElements.size(),
  128. &microcode[0],
  129. microcode.size(),
  130. &newEntry->inputLayout);
  131. if (FAILED(hr)|| device.hasError())
  132. BS_EXCEPT(RenderingAPIException, "Unable to set D3D11 vertex declaration" + device.getErrorDescription());
  133. // Create key and add to the layout map
  134. VertexDeclarationKey pair;
  135. pair.vertxDeclId = vertexBufferDecl->getId();
  136. pair.vertexProgramId = vertexProgram.getProgramId();
  137. mInputLayoutMap[pair] = newEntry;
  138. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_InputLayout);
  139. }
  140. void D3D11InputLayoutManager::removeLeastUsed()
  141. {
  142. if(!mWarningShown)
  143. {
  144. LOGWRN("Input layout buffer is full, pruning last " + toString(NUM_ELEMENTS_TO_PRUNE) + " elements. This is probably okay unless you are creating a massive amount of input layouts" \
  145. " as they will get re-created every frame. In that case you should increase the layout buffer size. This warning won't be shown again.");
  146. mWarningShown = true;
  147. }
  148. Map<UINT32, VertexDeclarationKey> leastFrequentlyUsedMap;
  149. for(auto iter = mInputLayoutMap.begin(); iter != mInputLayoutMap.end(); ++iter)
  150. leastFrequentlyUsedMap[iter->second->lastUsedIdx] = iter->first;
  151. UINT32 elemsRemoved = 0;
  152. for(auto iter = leastFrequentlyUsedMap.begin(); iter != leastFrequentlyUsedMap.end(); ++iter)
  153. {
  154. auto inputLayoutIter = mInputLayoutMap.find(iter->second);
  155. SAFE_RELEASE(inputLayoutIter->second->inputLayout);
  156. bs_delete(inputLayoutIter->second);
  157. mInputLayoutMap.erase(inputLayoutIter);
  158. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_InputLayout);
  159. elemsRemoved++;
  160. if(elemsRemoved >= NUM_ELEMENTS_TO_PRUNE)
  161. break;
  162. }
  163. }
  164. }}