GpuMemoryManager.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 : public NonCopyable
  30. {
  31. public:
  32. GpuMemoryManager() = default;
  33. ~GpuMemoryManager();
  34. void init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress);
  35. void destroy();
  36. /// Allocate memory.
  37. void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource, GpuMemoryHandle& handle);
  38. /// Free memory.
  39. void freeMemory(GpuMemoryHandle& handle);
  40. /// Map memory.
  41. ANKI_USE_RESULT void* getMappedAddress(GpuMemoryHandle& handle);
  42. /// Find a suitable memory type.
  43. U32 findMemoryType(U32 resourceMemTypeBits, VkMemoryPropertyFlags preferFlags,
  44. VkMemoryPropertyFlags avoidFlags) const;
  45. /// Get some statistics.
  46. void getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory) const;
  47. private:
  48. class Memory;
  49. class Interface;
  50. class ClassAllocator;
  51. GrAllocator<U8> m_alloc;
  52. DynamicArray<Array<Interface, 2>> m_ifaces;
  53. DynamicArray<Array<ClassAllocator, 2>> m_callocs;
  54. VkPhysicalDeviceMemoryProperties m_memoryProperties;
  55. };
  56. /// @}
  57. } // end namespace anki