GpuMemoryManager.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2009-2022, 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/Common.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 = MAX_U8;
  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 = MAX_PTR_SIZE;
  67. explicit operator Bool() const
  68. {
  69. return m_memory != VK_NULL_HANDLE && m_offset < MAX_PTR_SIZE && m_memTypeIdx < MAX_U8;
  70. }
  71. private:
  72. GpuMemoryManagerChunk* m_chunk = nullptr;
  73. PtrSize m_size = MAX_PTR_SIZE;
  74. U8 m_memTypeIdx = MAX_U8;
  75. Bool isDedicated() const
  76. {
  77. return m_chunk == nullptr;
  78. }
  79. };
  80. /// @memberof GpuMemoryManager
  81. class GpuMemoryManagerStats
  82. {
  83. public:
  84. PtrSize m_deviceMemoryAllocated;
  85. PtrSize m_deviceMemoryInUse;
  86. U32 m_deviceMemoryAllocationCount;
  87. PtrSize m_hostMemoryAllocated;
  88. PtrSize m_hostMemoryInUse;
  89. U32 m_hostMemoryAllocationCount;
  90. };
  91. /// Dynamic GPU memory allocator for all types.
  92. class GpuMemoryManager
  93. {
  94. friend class GpuMemoryManagerInterface;
  95. public:
  96. GpuMemoryManager() = default;
  97. GpuMemoryManager(const GpuMemoryManager&) = delete; // Non-copyable
  98. ~GpuMemoryManager();
  99. GpuMemoryManager& operator=(const GpuMemoryManager&) = delete; // Non-copyable
  100. void init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress);
  101. void destroy();
  102. /// Allocate memory.
  103. void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, GpuMemoryHandle& handle);
  104. void allocateMemoryDedicated(U32 memTypeIdx, PtrSize size, VkImage image, GpuMemoryHandle& handle);
  105. /// Free memory.
  106. void freeMemory(GpuMemoryHandle& handle);
  107. /// Map memory.
  108. [[nodiscard]] void* getMappedAddress(GpuMemoryHandle& handle);
  109. /// Find a suitable memory type.
  110. U32 findMemoryType(U32 resourceMemTypeBits, VkMemoryPropertyFlags preferFlags,
  111. VkMemoryPropertyFlags avoidFlags) const;
  112. /// Get some statistics.
  113. void getStats(GpuMemoryManagerStats& stats) const;
  114. private:
  115. using ClassAllocator = ClassAllocatorBuilder<GpuMemoryManagerChunk, GpuMemoryManagerInterface, Mutex>;
  116. GrAllocator<U8> m_alloc;
  117. VkDevice m_dev = VK_NULL_HANDLE;
  118. DynamicArray<ClassAllocator> m_callocs;
  119. VkPhysicalDeviceMemoryProperties m_memoryProperties;
  120. U32 m_bufferImageGranularity = 0;
  121. // Dedicated allocation stats
  122. Atomic<PtrSize> m_dedicatedAllocatedMemory = {0};
  123. Atomic<U32> m_dedicatedAllocationCount = {0};
  124. };
  125. /// @}
  126. } // end namespace anki