PipelineLayout.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Vulkan/PipelineLayout.h>
  6. namespace anki
  7. {
  8. void PipelineLayoutFactory::destroy()
  9. {
  10. while(!m_layouts.isEmpty())
  11. {
  12. auto it = m_layouts.getBegin();
  13. VkPipelineLayout handle = *it;
  14. m_layouts.erase(m_alloc, it);
  15. vkDestroyPipelineLayout(m_dev, handle, nullptr);
  16. }
  17. }
  18. Error PipelineLayoutFactory::newPipelineLayout(const WeakArray<DescriptorSetLayout>& dsetLayouts, U32 pushConstantsSize,
  19. PipelineLayout& layout)
  20. {
  21. U64 hash = computeHash(&pushConstantsSize, sizeof(pushConstantsSize));
  22. Array<VkDescriptorSetLayout, MAX_DESCRIPTOR_SETS> vkDsetLayouts;
  23. U32 dsetLayoutCount = 0;
  24. for(const DescriptorSetLayout& dl : dsetLayouts)
  25. {
  26. vkDsetLayouts[dsetLayoutCount++] = dl.getHandle();
  27. }
  28. if(dsetLayoutCount > 0)
  29. {
  30. hash = appendHash(&vkDsetLayouts[0], sizeof(vkDsetLayouts[0]) * dsetLayoutCount, hash);
  31. }
  32. LockGuard<Mutex> lock(m_layoutsMtx);
  33. auto it = m_layouts.find(hash);
  34. if(it != m_layouts.getEnd())
  35. {
  36. // Found it
  37. layout.m_handle = *it;
  38. }
  39. else
  40. {
  41. // Not found, create new
  42. VkPipelineLayoutCreateInfo ci = {};
  43. ci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  44. ci.pSetLayouts = &vkDsetLayouts[0];
  45. ci.setLayoutCount = dsetLayoutCount;
  46. VkPushConstantRange pushConstantRange;
  47. if(pushConstantsSize > 0)
  48. {
  49. pushConstantRange.offset = 0;
  50. pushConstantRange.size = pushConstantsSize;
  51. pushConstantRange.stageFlags = VK_SHADER_STAGE_ALL;
  52. ci.pushConstantRangeCount = 1;
  53. ci.pPushConstantRanges = &pushConstantRange;
  54. }
  55. VkPipelineLayout pplineLayHandle;
  56. ANKI_VK_CHECK(vkCreatePipelineLayout(m_dev, &ci, nullptr, &pplineLayHandle));
  57. m_layouts.emplace(m_alloc, hash, pplineLayHandle);
  58. layout.m_handle = pplineLayHandle;
  59. }
  60. return Error::NONE;
  61. }
  62. } // end namespace anki