VkGpuMemoryManager.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Vulkan/VkCommon.h>
  7. #include <AnKi/Util/ClassAllocatorBuilder.h>
  8. #include <AnKi/Util/List.h>
  9. #include <AnKi/Util/WeakArray.h>
  10. namespace anki {
  11. // Forward
  12. class GpuMemoryManager;
  13. /// @addtogroup vulkan
  14. /// @{
  15. class GpuMemoryManagerClassInfo
  16. {
  17. public:
  18. PtrSize m_suballocationSize;
  19. PtrSize m_chunkSize;
  20. };
  21. /// Implements the interface required by ClassAllocatorBuilder.
  22. /// @memberof GpuMemoryManager
  23. class GpuMemoryManagerChunk : public IntrusiveListEnabled<GpuMemoryManagerChunk>
  24. {
  25. public:
  26. VkDeviceMemory m_handle = VK_NULL_HANDLE;
  27. void* m_mappedAddress = nullptr;
  28. SpinLock m_mappedAddressMtx;
  29. PtrSize m_size = 0;
  30. // Bellow is the interface of ClassAllocatorBuilder
  31. BitSet<128, U64> m_inUseSuballocations = {false};
  32. U32 m_suballocationCount;
  33. void* m_class;
  34. };
  35. /// Implements the interface required by ClassAllocatorBuilder.
  36. /// @memberof GpuMemoryManager
  37. class GpuMemoryManagerInterface
  38. {
  39. public:
  40. GpuMemoryManager* m_parent = nullptr;
  41. U8 m_memTypeIdx = kMaxU8;
  42. Bool m_exposesBufferGpuAddress = false;
  43. Bool m_isDeviceMemory = false;
  44. PtrSize m_allocatedMemory = 0;
  45. PtrSize m_usedMemory = 0;
  46. ConstWeakArray<GpuMemoryManagerClassInfo> m_classInfos;
  47. // Bellow is the interface of ClassAllocatorBuilder
  48. U32 getClassCount() const
  49. {
  50. return m_classInfos.getSize();
  51. }
  52. void getClassInfo(U32 classIdx, PtrSize& chunkSize, PtrSize& suballocationSize) const
  53. {
  54. chunkSize = m_classInfos[classIdx].m_chunkSize;
  55. suballocationSize = m_classInfos[classIdx].m_suballocationSize;
  56. }
  57. Error allocateChunk(U32 classIdx, GpuMemoryManagerChunk*& chunk);
  58. void freeChunk(GpuMemoryManagerChunk* out);
  59. };
  60. /// The handle that is returned from GpuMemoryManager's allocations.
  61. class GpuMemoryHandle
  62. {
  63. friend class GpuMemoryManager;
  64. public:
  65. VkDeviceMemory m_memory = VK_NULL_HANDLE;
  66. PtrSize m_offset = kMaxPtrSize;
  67. explicit operator Bool() const
  68. {
  69. return m_memory != VK_NULL_HANDLE && m_offset < kMaxPtrSize && m_memTypeIdx < kMaxU8;
  70. }
  71. private:
  72. GpuMemoryManagerChunk* m_chunk = nullptr;
  73. PtrSize m_size = kMaxPtrSize;
  74. U8 m_memTypeIdx = kMaxU8;
  75. Bool isDedicated() const
  76. {
  77. return m_chunk == nullptr;
  78. }
  79. };
  80. /// Dynamic GPU memory allocator for all types.
  81. class GpuMemoryManager : public MakeSingleton<GpuMemoryManager>
  82. {
  83. friend class GpuMemoryManagerInterface;
  84. public:
  85. GpuMemoryManager()
  86. {
  87. init();
  88. }
  89. GpuMemoryManager(const GpuMemoryManager&) = delete; // Non-copyable
  90. ~GpuMemoryManager()
  91. {
  92. destroy();
  93. }
  94. GpuMemoryManager& operator=(const GpuMemoryManager&) = delete; // Non-copyable
  95. void getImageMemoryRequirements(VkImage image, VkMemoryDedicatedRequirementsKHR& dedicatedRequirements, VkMemoryRequirements2& requirements);
  96. /// Allocate memory.
  97. void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, GpuMemoryHandle& handle);
  98. void allocateMemoryDedicated(U32 memTypeIdx, PtrSize size, VkImage image, GpuMemoryHandle& handle);
  99. /// Free memory.
  100. void freeMemory(GpuMemoryHandle& handle);
  101. /// Map memory.
  102. [[nodiscard]] void* getMappedAddress(GpuMemoryHandle& handle);
  103. /// Find a suitable memory type.
  104. U32 findMemoryType(U32 resourceMemTypeBits, VkMemoryPropertyFlags preferFlags, VkMemoryPropertyFlags avoidFlags) const;
  105. void updateStats() const;
  106. private:
  107. using ClassAllocator = ClassAllocatorBuilder<GpuMemoryManagerChunk, GpuMemoryManagerInterface, Mutex, SingletonMemoryPoolWrapper<GrMemoryPool>>;
  108. GrDynamicArray<ClassAllocator> m_callocs;
  109. VkPhysicalDeviceMemoryProperties m_memoryProperties = {};
  110. U32 m_bufferImageGranularity = 0;
  111. // Dedicated allocation stats
  112. Atomic<PtrSize> m_dedicatedAllocatedMemory = {0};
  113. Atomic<U32> m_dedicatedAllocationCount = {0};
  114. void init();
  115. void destroy();
  116. };
  117. /// @}
  118. } // end namespace anki