GpuMemoryManager.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #pragma once
  6. #include <AnKi/Gr/Utils/ClassGpuAllocator.h>
  7. #include <AnKi/Gr/Vulkan/Common.h>
  8. namespace anki
  9. {
  10. /// @addtorgoup vulkan
  11. /// @{
  12. /// The handle that is returned from GpuMemoryManager's allocations.
  13. class GpuMemoryHandle
  14. {
  15. friend class GpuMemoryManager;
  16. public:
  17. VkDeviceMemory m_memory = VK_NULL_HANDLE;
  18. PtrSize m_offset = MAX_PTR_SIZE;
  19. explicit operator Bool() const
  20. {
  21. return m_memory != VK_NULL_HANDLE && m_offset < MAX_PTR_SIZE && m_memTypeIdx < MAX_U8;
  22. }
  23. private:
  24. ClassGpuAllocatorHandle m_classHandle;
  25. U8 m_memTypeIdx = MAX_U8;
  26. Bool m_linear = false;
  27. };
  28. /// Dynamic GPU memory allocator for all types.
  29. class GpuMemoryManager
  30. {
  31. public:
  32. GpuMemoryManager() = default;
  33. GpuMemoryManager(const GpuMemoryManager&) = delete; // Non-copyable
  34. ~GpuMemoryManager();
  35. GpuMemoryManager& operator=(const GpuMemoryManager&) = delete; // Non-copyable
  36. void init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress);
  37. void destroy();
  38. /// Allocate memory.
  39. void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource, GpuMemoryHandle& handle);
  40. /// Free memory.
  41. void freeMemory(GpuMemoryHandle& handle);
  42. /// Map memory.
  43. ANKI_USE_RESULT void* getMappedAddress(GpuMemoryHandle& handle);
  44. /// Find a suitable memory type.
  45. U32 findMemoryType(U32 resourceMemTypeBits, VkMemoryPropertyFlags preferFlags,
  46. VkMemoryPropertyFlags avoidFlags) const;
  47. /// Get some statistics.
  48. void getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory) const;
  49. private:
  50. class Memory;
  51. class Interface;
  52. class ClassAllocator;
  53. GrAllocator<U8> m_alloc;
  54. DynamicArray<Array<Interface, 2>> m_ifaces;
  55. DynamicArray<Array<ClassAllocator, 2>> m_callocs;
  56. VkPhysicalDeviceMemoryProperties m_memoryProperties;
  57. };
  58. /// @}
  59. } // end namespace anki