GpuMemoryPools.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /// @addtogroup core
  13. /// @{
  14. /// @memberof UnifiedGeometryBuffer
  15. class UnifiedGeometryBufferAllocation
  16. {
  17. friend class UnifiedGeometryBuffer;
  18. public:
  19. UnifiedGeometryBufferAllocation() = default;
  20. UnifiedGeometryBufferAllocation(const UnifiedGeometryBufferAllocation&) = delete;
  21. UnifiedGeometryBufferAllocation(UnifiedGeometryBufferAllocation&& b)
  22. {
  23. *this = std::move(b);
  24. }
  25. ~UnifiedGeometryBufferAllocation();
  26. UnifiedGeometryBufferAllocation& operator=(const UnifiedGeometryBufferAllocation&) = delete;
  27. UnifiedGeometryBufferAllocation& operator=(UnifiedGeometryBufferAllocation&& b)
  28. {
  29. ANKI_ASSERT(!isValid() && "Forgot to delete");
  30. m_token = b.m_token;
  31. b.m_token = {};
  32. return *this;
  33. }
  34. Bool isValid() const
  35. {
  36. return m_token.m_offset != kMaxPtrSize;
  37. }
  38. /// Get offset in the Unified Geometry Buffer buffer.
  39. U32 getOffset() const
  40. {
  41. ANKI_ASSERT(isValid());
  42. return U32(m_token.m_offset);
  43. }
  44. U32 getAllocatedSize() const
  45. {
  46. ANKI_ASSERT(isValid());
  47. return U32(m_token.m_size);
  48. }
  49. private:
  50. SegregatedListsGpuMemoryPoolToken m_token;
  51. };
  52. /// Manages vertex and index memory for the whole application.
  53. class UnifiedGeometryBuffer : public MakeSingleton<UnifiedGeometryBuffer>
  54. {
  55. template<typename>
  56. friend class MakeSingleton;
  57. public:
  58. UnifiedGeometryBuffer(const UnifiedGeometryBuffer&) = delete; // Non-copyable
  59. UnifiedGeometryBuffer& operator=(const UnifiedGeometryBuffer&) = delete; // Non-copyable
  60. void init();
  61. void allocate(PtrSize size, U32 alignment, UnifiedGeometryBufferAllocation& alloc)
  62. {
  63. m_pool.allocate(size, alignment, alloc.m_token);
  64. }
  65. void deferredFree(UnifiedGeometryBufferAllocation& alloc)
  66. {
  67. m_pool.deferredFree(alloc.m_token);
  68. }
  69. void endFrame()
  70. {
  71. m_pool.endFrame();
  72. }
  73. const BufferPtr& getBuffer() const
  74. {
  75. return m_pool.getGpuBuffer();
  76. }
  77. void getStats(F32& externalFragmentation, PtrSize& userAllocatedSize, PtrSize& totalSize) const
  78. {
  79. m_pool.getStats(externalFragmentation, userAllocatedSize, totalSize);
  80. }
  81. private:
  82. SegregatedListsGpuMemoryPool m_pool;
  83. UnifiedGeometryBuffer() = default;
  84. ~UnifiedGeometryBuffer() = default;
  85. };
  86. inline UnifiedGeometryBufferAllocation::~UnifiedGeometryBufferAllocation()
  87. {
  88. UnifiedGeometryBuffer::getSingleton().deferredFree(*this);
  89. }
  90. /// @memberof GpuSceneBuffer
  91. class GpuSceneBufferAllocation
  92. {
  93. friend class GpuSceneBuffer;
  94. public:
  95. GpuSceneBufferAllocation() = default;
  96. GpuSceneBufferAllocation(const GpuSceneBufferAllocation&) = delete;
  97. GpuSceneBufferAllocation(GpuSceneBufferAllocation&& b)
  98. {
  99. *this = std::move(b);
  100. }
  101. ~GpuSceneBufferAllocation();
  102. GpuSceneBufferAllocation& operator=(const GpuSceneBufferAllocation&) = delete;
  103. GpuSceneBufferAllocation& operator=(GpuSceneBufferAllocation&& b)
  104. {
  105. ANKI_ASSERT(!isValid() && "Forgot to delete");
  106. m_token = b.m_token;
  107. b.m_token = {};
  108. return *this;
  109. }
  110. Bool isValid() const
  111. {
  112. return m_token.m_offset != kMaxPtrSize;
  113. }
  114. /// Get offset in the Unified Geometry Buffer buffer.
  115. U32 getOffset() const
  116. {
  117. ANKI_ASSERT(isValid());
  118. return U32(m_token.m_offset);
  119. }
  120. U32 getAllocatedSize() const
  121. {
  122. ANKI_ASSERT(isValid());
  123. return U32(m_token.m_size);
  124. }
  125. private:
  126. SegregatedListsGpuMemoryPoolToken m_token;
  127. };
  128. /// Memory pool for the GPU scene.
  129. class GpuSceneBuffer : public MakeSingleton<GpuSceneBuffer>
  130. {
  131. template<typename>
  132. friend class MakeSingleton;
  133. public:
  134. GpuSceneBuffer(const GpuSceneBuffer&) = delete; // Non-copyable
  135. GpuSceneBuffer& operator=(const GpuSceneBuffer&) = delete; // Non-copyable
  136. void init();
  137. void allocate(PtrSize size, U32 alignment, GpuSceneBufferAllocation& alloc)
  138. {
  139. m_pool.allocate(size, alignment, alloc.m_token);
  140. }
  141. void deferredFree(GpuSceneBufferAllocation& alloc)
  142. {
  143. m_pool.deferredFree(alloc.m_token);
  144. }
  145. void endFrame()
  146. {
  147. m_pool.endFrame();
  148. }
  149. const BufferPtr& getBuffer() const
  150. {
  151. return m_pool.getGpuBuffer();
  152. }
  153. void getStats(F32& externalFragmentation, PtrSize& userAllocatedSize, PtrSize& totalSize) const
  154. {
  155. m_pool.getStats(externalFragmentation, userAllocatedSize, totalSize);
  156. }
  157. private:
  158. SegregatedListsGpuMemoryPool m_pool;
  159. GpuSceneBuffer() = default;
  160. ~GpuSceneBuffer() = default;
  161. };
  162. inline GpuSceneBufferAllocation::~GpuSceneBufferAllocation()
  163. {
  164. GpuSceneBuffer::getSingleton().deferredFree(*this);
  165. }
  166. /// Token that gets returned when requesting for memory to write to a resource.
  167. class RebarGpuMemoryToken
  168. {
  169. public:
  170. PtrSize m_offset = 0;
  171. PtrSize m_range = 0;
  172. RebarGpuMemoryToken() = default;
  173. ~RebarGpuMemoryToken() = default;
  174. Bool operator==(const RebarGpuMemoryToken& b) const
  175. {
  176. return m_offset == b.m_offset && m_range == b.m_range;
  177. }
  178. void markUnused()
  179. {
  180. m_offset = m_range = kMaxU32;
  181. }
  182. Bool isUnused() const
  183. {
  184. return m_offset == kMaxU32 && m_range == kMaxU32;
  185. }
  186. };
  187. /// Manages staging GPU memory.
  188. class RebarStagingGpuMemoryPool : public MakeSingleton<RebarStagingGpuMemoryPool>
  189. {
  190. template<typename>
  191. friend class MakeSingleton;
  192. public:
  193. RebarStagingGpuMemoryPool(const RebarStagingGpuMemoryPool&) = delete; // Non-copyable
  194. ~RebarStagingGpuMemoryPool();
  195. RebarStagingGpuMemoryPool& operator=(const RebarStagingGpuMemoryPool&) = delete; // Non-copyable
  196. void init();
  197. PtrSize endFrame();
  198. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  199. /// N-(kMaxFramesInFlight-1) frame.
  200. void* allocateFrame(PtrSize size, RebarGpuMemoryToken& token);
  201. /// Allocate staging memory for various operations. The memory will be reclaimed at the begining of the
  202. /// N-(kMaxFramesInFlight-1) frame.
  203. void* tryAllocateFrame(PtrSize size, RebarGpuMemoryToken& token);
  204. ANKI_PURE const BufferPtr& getBuffer() const
  205. {
  206. return m_buffer;
  207. }
  208. U8* getBufferMappedAddress()
  209. {
  210. return m_mappedMem;
  211. }
  212. private:
  213. BufferPtr m_buffer;
  214. U8* m_mappedMem = nullptr; ///< Cache it.
  215. PtrSize m_bufferSize = 0; ///< Cache it.
  216. Atomic<PtrSize> m_offset = {0};
  217. PtrSize m_previousFrameEndOffset = 0;
  218. U32 m_alignment = 0;
  219. RebarStagingGpuMemoryPool() = default;
  220. };
  221. /// Creates the copy jobs that will patch the GPU Scene.
  222. class GpuSceneMicroPatcher : public MakeSingleton<GpuSceneMicroPatcher>
  223. {
  224. template<typename>
  225. friend class MakeSingleton;
  226. public:
  227. GpuSceneMicroPatcher(const GpuSceneMicroPatcher&) = delete;
  228. GpuSceneMicroPatcher& operator=(const GpuSceneMicroPatcher&) = delete;
  229. Error init();
  230. /// Copy data for the GPU scene to a staging buffer.
  231. /// @note It's thread-safe.
  232. void newCopy(StackMemoryPool& frameCpuPool, PtrSize gpuSceneDestOffset, PtrSize dataSize, const void* data);
  233. template<typename T>
  234. void newCopy(StackMemoryPool& frameCpuPool, PtrSize gpuSceneDestOffset, const T& value)
  235. {
  236. newCopy(frameCpuPool, gpuSceneDestOffset, sizeof(value), &value);
  237. }
  238. /// @see newCopy
  239. void newCopy(StackMemoryPool& frameCpuPool, const GpuSceneBufferAllocation& dest, PtrSize dataSize,
  240. const void* data)
  241. {
  242. ANKI_ASSERT(dataSize <= dest.getAllocatedSize());
  243. newCopy(frameCpuPool, dest.getOffset(), dataSize, data);
  244. }
  245. /// @see newCopy
  246. template<typename T>
  247. void newCopy(StackMemoryPool& frameCpuPool, const GpuSceneBufferAllocation& dest, const T& value)
  248. {
  249. ANKI_ASSERT(sizeof(value) <= dest.getAllocatedSize());
  250. newCopy(frameCpuPool, dest.getOffset(), sizeof(value), &value);
  251. }
  252. /// Check if there is a need to call patchGpuScene or if no copies are needed.
  253. /// @note Not thread-safe. Nothing else should be happening before calling it.
  254. Bool patchingIsNeeded() const
  255. {
  256. return m_crntFramePatchHeaders.getSize() > 0;
  257. }
  258. /// Copy the data to the GPU scene buffer.
  259. /// @note Not thread-safe. Nothing else should be happening before calling it.
  260. void patchGpuScene(CommandBuffer& cmdb);
  261. private:
  262. static constexpr U32 kDwordsPerPatch = 64;
  263. class PatchHeader;
  264. DynamicArray<PatchHeader, MemoryPoolPtrWrapper<StackMemoryPool>> m_crntFramePatchHeaders;
  265. DynamicArray<U32, MemoryPoolPtrWrapper<StackMemoryPool>> m_crntFramePatchData;
  266. Mutex m_mtx;
  267. ShaderProgramResourcePtr m_copyProgram;
  268. ShaderProgramPtr m_grProgram;
  269. GpuSceneMicroPatcher();
  270. ~GpuSceneMicroPatcher();
  271. };
  272. /// @}
  273. } // end namespace anki