GpuMemoryManager.h 1.9 KB

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