GpuMemoryPools.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2009-2023, 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/StackGpuMemoryPool.h>
  9. #include <AnKi/Gr/Utils/SegregatedListsGpuMemoryPool.h>
  10. #include <AnKi/Resource/ShaderProgramResource.h>
  11. namespace anki {
  12. // Forward
  13. class ConfigSet;
  14. /// @addtogroup core
  15. /// @{
  16. /// Manages vertex and index memory for the whole application.
  17. class UnifiedGeometryMemoryPool
  18. {
  19. public:
  20. UnifiedGeometryMemoryPool() = default;
  21. UnifiedGeometryMemoryPool(const UnifiedGeometryMemoryPool&) = delete; // Non-copyable
  22. UnifiedGeometryMemoryPool& operator=(const UnifiedGeometryMemoryPool&) = delete; // Non-copyable
  23. void init(HeapMemoryPool* pool, GrManager* gr, const ConfigSet& cfg);
  24. void allocate(PtrSize size, U32 alignment, SegregatedListsGpuMemoryPoolToken& token)
  25. {
  26. m_pool.allocate(size, alignment, token);
  27. }
  28. void deferredFree(SegregatedListsGpuMemoryPoolToken& token)
  29. {
  30. m_pool.deferredFree(token);
  31. }
  32. void endFrame()
  33. {
  34. m_pool.endFrame();
  35. }
  36. const BufferPtr& getBuffer() const
  37. {
  38. return m_pool.getGpuBuffer();
  39. }
  40. void getStats(F32& externalFragmentation, PtrSize& userAllocatedSize, PtrSize& totalSize) const
  41. {
  42. m_pool.getStats(externalFragmentation, userAllocatedSize, totalSize);
  43. }
  44. private:
  45. SegregatedListsGpuMemoryPool m_pool;
  46. };
  47. /// Memory pool for the GPU scene.
  48. class GpuSceneMemoryPool
  49. {
  50. public:
  51. GpuSceneMemoryPool() = default;
  52. GpuSceneMemoryPool(const GpuSceneMemoryPool&) = delete; // Non-copyable
  53. GpuSceneMemoryPool& operator=(const GpuSceneMemoryPool&) = delete; // Non-copyable
  54. void init(HeapMemoryPool* pool, GrManager* gr, const ConfigSet& cfg);
  55. void allocate(PtrSize size, U32 alignment, SegregatedListsGpuMemoryPoolToken& token)
  56. {
  57. m_pool.allocate(size, alignment, token);
  58. }
  59. void deferredFree(SegregatedListsGpuMemoryPoolToken& token)
  60. {
  61. m_pool.deferredFree(token);
  62. }
  63. void endFrame()
  64. {
  65. m_pool.endFrame();
  66. }
  67. const BufferPtr& getBuffer() const
  68. {
  69. return m_pool.getGpuBuffer();
  70. }
  71. void getStats(F32& externalFragmentation, PtrSize& userAllocatedSize, PtrSize& totalSize) const
  72. {
  73. m_pool.getStats(externalFragmentation, userAllocatedSize, totalSize);
  74. }
  75. GrManager& getGrManager()
  76. {
  77. return m_pool.getGrManager();
  78. }
  79. private:
  80. SegregatedListsGpuMemoryPool m_pool;
  81. };
  82. /// Token that gets returned when requesting for memory to write to a resource.
  83. class RebarGpuMemoryToken
  84. {
  85. public:
  86. PtrSize m_offset = 0;
  87. PtrSize m_range = 0;
  88. RebarGpuMemoryToken() = default;
  89. ~RebarGpuMemoryToken() = default;
  90. Bool operator==(const RebarGpuMemoryToken& b) const
  91. {
  92. return m_offset == b.m_offset && m_range == b.m_range;
  93. }
  94. void markUnused()
  95. {
  96. m_offset = m_range = kMaxU32;
  97. }
  98. Bool isUnused() const
  99. {
  100. return m_offset == kMaxU32 && m_range == kMaxU32;
  101. }
  102. };
  103. /// Manages staging GPU memory.
  104. class RebarStagingGpuMemoryPool
  105. {
  106. public:
  107. RebarStagingGpuMemoryPool() = default;
  108. RebarStagingGpuMemoryPool(const RebarStagingGpuMemoryPool&) = delete; // Non-copyable
  109. ~RebarStagingGpuMemoryPool();
  110. RebarStagingGpuMemoryPool& operator=(const RebarStagingGpuMemoryPool&) = delete; // Non-copyable
  111. Error init(GrManager* gr, const ConfigSet& cfg);
  112. PtrSize endFrame();
  113. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  114. /// N-(kMaxFramesInFlight-1) frame.
  115. void* allocateFrame(PtrSize size, RebarGpuMemoryToken& token);
  116. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  117. /// N-(kMaxFramesInFlight-1) frame.
  118. void* tryAllocateFrame(PtrSize size, RebarGpuMemoryToken& token);
  119. ANKI_PURE const BufferPtr& getBuffer() const
  120. {
  121. return m_buffer;
  122. }
  123. private:
  124. BufferPtr m_buffer;
  125. U8* m_mappedMem = nullptr; ///< Cache it.
  126. PtrSize m_bufferSize = 0; ///< Cache it.
  127. Atomic<PtrSize> m_offset = {0};
  128. PtrSize m_previousFrameEndOffset = 0;
  129. U32 m_alignment = 0;
  130. };
  131. /// Creates the copy jobs that will patch the GPU Scene.
  132. class GpuSceneMicroPatcher
  133. {
  134. public:
  135. GpuSceneMicroPatcher() = default;
  136. GpuSceneMicroPatcher(const GpuSceneMicroPatcher&) = delete;
  137. ~GpuSceneMicroPatcher();
  138. GpuSceneMicroPatcher& operator=(const GpuSceneMicroPatcher&) = delete;
  139. Error init(ResourceManager* rsrc);
  140. /// Copy data for the GPU scene to a staging buffer.
  141. /// @note It's thread-safe.
  142. void newCopy(StackMemoryPool& frameCpuPool, PtrSize gpuSceneDestOffset, PtrSize dataSize, const void* data);
  143. /// Check if there is a need to call patchGpuScene or if no copies are needed.
  144. /// @note Not thread-safe. Nothing else should be happening before calling it.
  145. Bool patchingIsNeeded() const
  146. {
  147. return m_crntFramePatchHeaders.getSize() > 0;
  148. }
  149. /// Copy the data to the GPU scene buffer.
  150. /// @note Not thread-safe. Nothing else should be happening before calling it.
  151. void patchGpuScene(RebarStagingGpuMemoryPool& rebarPool, CommandBuffer& cmdb, const BufferPtr& gpuSceneBuffer);
  152. private:
  153. static constexpr U32 kDwordsPerPatch = 64;
  154. class PatchHeader;
  155. DynamicArray<PatchHeader> m_crntFramePatchHeaders;
  156. DynamicArray<U32> m_crntFramePatchData;
  157. Mutex m_mtx;
  158. ShaderProgramResourcePtr m_copyProgram;
  159. ShaderProgramPtr m_grProgram;
  160. };
  161. /// @}
  162. } // end namespace anki