PipelineLayout.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. void PipelineLayoutFactory::destroy()
  8. {
  9. while(!m_layouts.isEmpty())
  10. {
  11. auto it = m_layouts.getBegin();
  12. VkPipelineLayout handle = *it;
  13. m_layouts.erase(m_alloc, it);
  14. vkDestroyPipelineLayout(m_dev, handle, nullptr);
  15. }
  16. }
  17. Error PipelineLayoutFactory::newPipelineLayout(const WeakArray<DescriptorSetLayout>& dsetLayouts, U32 pushConstantsSize,
  18. PipelineLayout& layout)
  19. {
  20. U64 hash = computeHash(&pushConstantsSize, sizeof(pushConstantsSize));
  21. Array<VkDescriptorSetLayout, MAX_DESCRIPTOR_SETS> vkDsetLayouts;
  22. U32 dsetLayoutCount = 0;
  23. for(const DescriptorSetLayout& dl : dsetLayouts)
  24. {
  25. vkDsetLayouts[dsetLayoutCount++] = dl.getHandle();
  26. }
  27. if(dsetLayoutCount > 0)
  28. {
  29. hash = appendHash(&vkDsetLayouts[0], sizeof(vkDsetLayouts[0]) * dsetLayoutCount, hash);
  30. }
  31. LockGuard<Mutex> lock(m_layoutsMtx);
  32. auto it = m_layouts.find(hash);
  33. if(it != m_layouts.getEnd())
  34. {
  35. // Found it
  36. layout.m_handle = *it;
  37. }
  38. else
  39. {
  40. // Not found, create new
  41. VkPipelineLayoutCreateInfo ci = {};
  42. ci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  43. ci.pSetLayouts = &vkDsetLayouts[0];
  44. ci.setLayoutCount = dsetLayoutCount;
  45. VkPushConstantRange pushConstantRange;
  46. if(pushConstantsSize > 0)
  47. {
  48. pushConstantRange.offset = 0;
  49. pushConstantRange.size = pushConstantsSize;
  50. pushConstantRange.stageFlags = VK_SHADER_STAGE_ALL;
  51. ci.pushConstantRangeCount = 1;
  52. ci.pPushConstantRanges = &pushConstantRange;
  53. }
  54. VkPipelineLayout pplineLayHandle;
  55. ANKI_VK_CHECK(vkCreatePipelineLayout(m_dev, &ci, nullptr, &pplineLayHandle));
  56. m_layouts.emplace(m_alloc, hash, pplineLayHandle);
  57. layout.m_handle = pplineLayHandle;
  58. }
  59. return Error::NONE;
  60. }
  61. } // end namespace anki