GpuMemoryPools.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/Core/Common.h>
  7. #include <AnKi/Gr/Buffer.h>
  8. #include <AnKi/Gr/Utils/FrameGpuAllocator.h>
  9. #include <AnKi/Util/BuddyAllocatorBuilder.h>
  10. namespace anki {
  11. // Forward
  12. class ConfigSet;
  13. /// @addtogroup core
  14. /// @{
  15. /// Manages vertex and index memory for the whole application.
  16. class VertexGpuMemoryPool
  17. {
  18. public:
  19. VertexGpuMemoryPool() = default;
  20. VertexGpuMemoryPool(const VertexGpuMemoryPool&) = delete; // Non-copyable
  21. ~VertexGpuMemoryPool();
  22. VertexGpuMemoryPool& operator=(const VertexGpuMemoryPool&) = delete; // Non-copyable
  23. Error init(GenericMemoryPoolAllocator<U8> alloc, GrManager* gr, const ConfigSet& cfg);
  24. Error allocate(PtrSize size, PtrSize& offset);
  25. void free(PtrSize size, PtrSize offset);
  26. BufferPtr getVertexBuffer() const
  27. {
  28. return m_vertBuffer;
  29. }
  30. void getMemoryStats(BuddyAllocatorBuilderStats& stats) const
  31. {
  32. m_buddyAllocator.getStats(stats);
  33. }
  34. private:
  35. GrManager* m_gr = nullptr;
  36. BufferPtr m_vertBuffer;
  37. BuddyAllocatorBuilder<32, Mutex> m_buddyAllocator;
  38. };
  39. enum class StagingGpuMemoryType : U8
  40. {
  41. UNIFORM,
  42. STORAGE,
  43. VERTEX,
  44. TEXTURE,
  45. COUNT
  46. };
  47. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(StagingGpuMemoryType)
  48. /// Token that gets returned when requesting for memory to write to a resource.
  49. class StagingGpuMemoryToken
  50. {
  51. public:
  52. BufferPtr m_buffer;
  53. PtrSize m_offset = 0;
  54. PtrSize m_range = 0;
  55. StagingGpuMemoryType m_type = StagingGpuMemoryType::COUNT;
  56. StagingGpuMemoryToken() = default;
  57. ~StagingGpuMemoryToken() = default;
  58. Bool operator==(const StagingGpuMemoryToken& b) const
  59. {
  60. return m_buffer == b.m_buffer && m_offset == b.m_offset && m_range == b.m_range && m_type == b.m_type;
  61. }
  62. void markUnused()
  63. {
  64. m_offset = m_range = MAX_U32;
  65. }
  66. Bool isUnused() const
  67. {
  68. return m_offset == MAX_U32 && m_range == MAX_U32;
  69. }
  70. };
  71. /// Manages staging GPU memory.
  72. class StagingGpuMemoryPool
  73. {
  74. public:
  75. StagingGpuMemoryPool() = default;
  76. StagingGpuMemoryPool(const StagingGpuMemoryPool&) = delete; // Non-copyable
  77. ~StagingGpuMemoryPool();
  78. StagingGpuMemoryPool& operator=(const StagingGpuMemoryPool&) = delete; // Non-copyable
  79. Error init(GrManager* gr, const ConfigSet& cfg);
  80. void endFrame();
  81. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  82. /// N-(MAX_FRAMES_IN_FLIGHT-1) frame.
  83. void* allocateFrame(PtrSize size, StagingGpuMemoryType usage, StagingGpuMemoryToken& token);
  84. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  85. /// N-(MAX_FRAMES_IN_FLIGHT-1) frame.
  86. void* tryAllocateFrame(PtrSize size, StagingGpuMemoryType usage, StagingGpuMemoryToken& token);
  87. private:
  88. class PerFrameBuffer
  89. {
  90. public:
  91. PtrSize m_size = 0;
  92. BufferPtr m_buff;
  93. U8* m_mappedMem = nullptr; ///< Cache it
  94. FrameGpuAllocator m_alloc;
  95. };
  96. GrManager* m_gr = nullptr;
  97. Array<PerFrameBuffer, U(StagingGpuMemoryType::COUNT)> m_perFrameBuffers;
  98. void initBuffer(StagingGpuMemoryType type, U32 alignment, PtrSize maxAllocSize, BufferUsageBit usage,
  99. GrManager& gr);
  100. };
  101. /// @}
  102. } // end namespace anki