BsD3D11InputLayoutManager.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "BsD3D11InputLayoutManager.h"
  2. #include "BsD3D11Mappings.h"
  3. #include "BsD3D11RenderSystem.h"
  4. #include "BsD3D11Device.h"
  5. #include "BsD3D11GpuProgram.h"
  6. #include "BsHardwareBufferManager.h"
  7. #include "BsDebug.h"
  8. #include "BsUtil.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<PoolAlloc>(firstElem->second);
  39. mInputLayoutMap.erase(firstElem);
  40. }
  41. }
  42. ID3D11InputLayout* D3D11InputLayoutManager::retrieveInputLayout(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl, D3D11GpuProgram& vertexProgram)
  43. {
  44. VertexDeclarationKey pair;
  45. pair.vertxDeclId = vertexBufferDecl->getInternalID();
  46. pair.vertexProgramId = vertexProgram.getProgramId();
  47. auto iterFind = mInputLayoutMap.find(pair);
  48. if(iterFind == mInputLayoutMap.end())
  49. {
  50. if(mInputLayoutMap.size() >= DECLARATION_BUFFER_SIZE)
  51. removeLeastUsed(); // Prune so the buffer doesn't just infinitely grow
  52. addNewInputLayout(vertexShaderDecl, vertexBufferDecl, vertexProgram);
  53. iterFind = mInputLayoutMap.find(pair);
  54. if(iterFind == mInputLayoutMap.end()) // We failed to create input layout
  55. return nullptr;
  56. }
  57. iterFind->second->lastUsedIdx = ++mLastUsedCounter;
  58. return iterFind->second->inputLayout;
  59. }
  60. void D3D11InputLayoutManager::addNewInputLayout(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl, D3D11GpuProgram& vertexProgram)
  61. {
  62. if(!areCompatible(vertexShaderDecl, vertexBufferDecl))
  63. return; // Error was already reported, so just quit here
  64. UINT32 numElements = vertexBufferDecl->getElementCount();
  65. D3D11_INPUT_ELEMENT_DESC* declElements = bs_newN<D3D11_INPUT_ELEMENT_DESC, ScratchAlloc>(numElements);
  66. ZeroMemory(declElements, sizeof(D3D11_INPUT_ELEMENT_DESC) * numElements);
  67. unsigned int idx = 0;
  68. for(auto iter = vertexBufferDecl->getElements().begin(); iter != vertexBufferDecl->getElements().end(); ++iter)
  69. {
  70. declElements[idx].SemanticName = D3D11Mappings::get(iter->getSemantic());
  71. declElements[idx].SemanticIndex = iter->getSemanticIdx();
  72. declElements[idx].Format = D3D11Mappings::get(iter->getType());
  73. declElements[idx].InputSlot = iter->getStreamIdx();
  74. declElements[idx].AlignedByteOffset = static_cast<WORD>(iter->getOffset());
  75. declElements[idx].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  76. declElements[idx].InstanceDataStepRate = 0;
  77. idx++;
  78. }
  79. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  80. D3D11Device& device = d3d11rs->getPrimaryDevice();
  81. const HLSLMicroCode& microcode = vertexProgram.getMicroCode();
  82. InputLayoutEntry* newEntry = bs_new<InputLayoutEntry, PoolAlloc>();
  83. newEntry->lastUsedIdx = ++mLastUsedCounter;
  84. newEntry->inputLayout = nullptr;
  85. HRESULT hr = device.getD3D11Device()->CreateInputLayout(
  86. declElements,
  87. numElements,
  88. &microcode[0],
  89. microcode.size(),
  90. &newEntry->inputLayout);
  91. bs_deleteN<ScratchAlloc>(declElements, numElements);
  92. if (FAILED(hr)|| device.hasError())
  93. BS_EXCEPT(RenderingAPIException, "Unable to set D3D11 vertex declaration" + device.getErrorDescription());
  94. // Create key and add to the layout map
  95. VertexDeclarationKey pair;
  96. pair.vertxDeclId = vertexBufferDecl->getInternalID();
  97. pair.vertexProgramId = vertexProgram.getProgramId();
  98. mInputLayoutMap[pair] = newEntry;
  99. }
  100. void D3D11InputLayoutManager::removeLeastUsed()
  101. {
  102. if(!mWarningShown)
  103. {
  104. 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" \
  105. " 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.");
  106. mWarningShown = true;
  107. }
  108. Map<UINT32, VertexDeclarationKey> leastFrequentlyUsedMap;
  109. for(auto iter = mInputLayoutMap.begin(); iter != mInputLayoutMap.end(); ++iter)
  110. leastFrequentlyUsedMap[iter->second->lastUsedIdx] = iter->first;
  111. UINT32 elemsRemoved = 0;
  112. for(auto iter = leastFrequentlyUsedMap.begin(); iter != leastFrequentlyUsedMap.end(); ++iter)
  113. {
  114. auto inputLayoutIter = mInputLayoutMap.find(iter->second);
  115. SAFE_RELEASE(inputLayoutIter->second->inputLayout);
  116. bs_delete<PoolAlloc>(inputLayoutIter->second);
  117. mInputLayoutMap.erase(inputLayoutIter);
  118. elemsRemoved++;
  119. if(elemsRemoved >= NUM_ELEMENTS_TO_PRUNE)
  120. break;
  121. }
  122. }
  123. bool D3D11InputLayoutManager::areCompatible(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl)
  124. {
  125. for(auto shaderIter = vertexShaderDecl->getElements().begin(); shaderIter != vertexShaderDecl->getElements().end(); ++shaderIter)
  126. {
  127. const VertexElement* foundElement = nullptr;
  128. for(auto bufferIter = vertexBufferDecl->getElements().begin(); bufferIter != vertexBufferDecl->getElements().end(); ++bufferIter)
  129. {
  130. if(shaderIter->getSemantic() == bufferIter->getSemantic() && shaderIter->getSemanticIdx() == bufferIter->getSemanticIdx())
  131. {
  132. foundElement = &(*bufferIter);
  133. break;
  134. }
  135. }
  136. if(foundElement == nullptr)
  137. {
  138. LOGWRN("Provided vertex buffer doesn't have a required input attribute: " + toString(shaderIter->getSemantic()) + toString(shaderIter->getSemanticIdx()));
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. }