BsVulkanDescriptorManager.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsVulkanPrerequisites.h"
  5. #include "BsVulkanDescriptorLayout.h"
  6. namespace bs
  7. {
  8. /** Used as a key in a hash map containing VulkanDescriptorLayout%s. */
  9. struct VulkanLayoutKey
  10. {
  11. VulkanLayoutKey(VkDescriptorSetLayoutBinding* bindings, UINT32 numBindings);
  12. /** Compares two descriptor layouts. */
  13. bool operator==(const VulkanLayoutKey& rhs) const;
  14. UINT32 numBindings;
  15. VkDescriptorSetLayoutBinding* bindings;
  16. VulkanDescriptorLayout* layout;
  17. };
  18. /** Used as a key in a hash map containing pipeline layouts. */
  19. struct VulkanPipelineLayoutKey
  20. {
  21. VulkanPipelineLayoutKey(VulkanDescriptorLayout** layouts, UINT32 numLayouts);
  22. /** Compares two pipeline layouts. */
  23. bool operator==(const VulkanPipelineLayoutKey& rhs) const;
  24. /** Calculates a has value for the provided descriptor layouts. */
  25. size_t calculateHash() const;
  26. UINT32 numLayouts;
  27. VulkanDescriptorLayout** layouts;
  28. };
  29. }
  30. /** @cond STDLIB */
  31. /** @addtogroup Vulkan
  32. * @{
  33. */
  34. namespace std
  35. {
  36. /** Hash value generator for VulkanLayoutKey. */
  37. template<>
  38. struct hash<bs::VulkanLayoutKey>
  39. {
  40. size_t operator()(const bs::VulkanLayoutKey& value) const
  41. {
  42. if (value.layout != nullptr)
  43. return value.layout->getHash();
  44. return bs::VulkanDescriptorLayout::calculateHash(value.bindings, value.numBindings);
  45. }
  46. };
  47. /** Hash value generator for VulkanPipelineLayoutKey. */
  48. template<>
  49. struct hash<bs::VulkanPipelineLayoutKey>
  50. {
  51. size_t operator()(const bs::VulkanPipelineLayoutKey& value) const
  52. {
  53. return value.calculateHash();
  54. }
  55. };
  56. }
  57. /** @} */
  58. /** @endcond */
  59. namespace bs
  60. {
  61. /** @addtogroup Vulkan
  62. * @{
  63. */
  64. /** Manages allocation of descriptor layouts and sets for a single Vulkan device. */
  65. class VulkanDescriptorManager
  66. {
  67. public:
  68. VulkanDescriptorManager(VulkanDevice& device);
  69. ~VulkanDescriptorManager();
  70. /** Attempts to find an existing one, or allocates a new descriptor set layout from the provided set of bindings. */
  71. VulkanDescriptorLayout* getLayout(VkDescriptorSetLayoutBinding* bindings, UINT32 numBindings);
  72. /** Allocates a new empty descriptor set matching the provided layout. */
  73. VulkanDescriptorSet* createSet(VulkanDescriptorLayout* layout);
  74. /** Attempts to find an existing one, or allocates a new pipeline layout based on the provided descriptor layouts. */
  75. VkPipelineLayout getPipelineLayout(VulkanDescriptorLayout** layouts, UINT32 numLayouts);
  76. protected:
  77. VulkanDevice& mDevice;
  78. UnorderedSet<VulkanLayoutKey> mLayouts;
  79. UnorderedMap<VulkanPipelineLayoutKey, VkPipelineLayout> mPipelineLayouts;
  80. Vector<VulkanDescriptorPool*> mPools;
  81. };
  82. /** @} */
  83. }