BsD3D11InputLayoutManager.cpp 6.5 KB

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