ContiguousArrayAllocator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/Scene/Common.h>
  7. #include <AnKi/Core/GpuMemoryPools.h>
  8. namespace anki {
  9. /// @addtogroup scene
  10. /// @{
  11. /// Some of the GPU scene structures are stored in structured buffers
  12. enum class GpuSceneContiguousArrayType : U8
  13. {
  14. kTransformPairs,
  15. kMeshLods,
  16. kParticleEmitters,
  17. kPointLights,
  18. kSpotLights,
  19. kReflectionProbes,
  20. kGlobalIlluminationProbes,
  21. kDecals,
  22. kFogDensityVolumes,
  23. kRenderables,
  24. kRenderableBoundingVolumesGBuffer,
  25. kRenderableBoundingVolumesForward,
  26. kRenderableBoundingVolumesDepth,
  27. kCount,
  28. kFirst = 0
  29. };
  30. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(GpuSceneContiguousArrayType)
  31. class GpuSceneContiguousArrayIndex
  32. {
  33. friend class AllGpuSceneContiguousArrays;
  34. public:
  35. GpuSceneContiguousArrayIndex() = default;
  36. GpuSceneContiguousArrayIndex(const GpuSceneContiguousArrayIndex& b) = delete;
  37. GpuSceneContiguousArrayIndex(GpuSceneContiguousArrayIndex&& b)
  38. {
  39. *this = std::move(b);
  40. }
  41. ~GpuSceneContiguousArrayIndex();
  42. GpuSceneContiguousArrayIndex& operator=(const GpuSceneContiguousArrayIndex&) = delete;
  43. GpuSceneContiguousArrayIndex& operator=(GpuSceneContiguousArrayIndex&& b)
  44. {
  45. ANKI_ASSERT(!isValid());
  46. m_index = b.m_index;
  47. m_type = b.m_type;
  48. b.invalidate();
  49. return *this;
  50. }
  51. U32 get() const
  52. {
  53. ANKI_ASSERT(m_index != kMaxU32);
  54. return m_index;
  55. }
  56. Bool isValid() const
  57. {
  58. return m_index != kMaxU32;
  59. }
  60. U32 getOffsetInGpuScene() const;
  61. private:
  62. U32 m_index = kMaxU32;
  63. GpuSceneContiguousArrayType m_type = GpuSceneContiguousArrayType::kCount;
  64. void invalidate()
  65. {
  66. m_index = kMaxU32;
  67. m_type = GpuSceneContiguousArrayType::kCount;
  68. }
  69. };
  70. /// Contains a number of contiguous array allocators for various GPU scene contiguous objects.
  71. class AllGpuSceneContiguousArrays : public MakeSingleton<AllGpuSceneContiguousArrays>
  72. {
  73. template<typename>
  74. friend class MakeSingleton;
  75. public:
  76. /// @note Thread-safe against allocate(), deferredFree() and endFrame()
  77. GpuSceneContiguousArrayIndex allocate(GpuSceneContiguousArrayType type);
  78. /// @note It's not thread-safe
  79. PtrSize getArrayBase(GpuSceneContiguousArrayType type) const
  80. {
  81. return m_allocs[type].getArrayBase();
  82. }
  83. /// @note It's not thread-safe
  84. U32 getElementCount(GpuSceneContiguousArrayType type) const
  85. {
  86. return m_allocs[type].getElementCount();
  87. }
  88. /// @note It's not thread-safe
  89. U32 getElementOffsetInGpuScene(const GpuSceneContiguousArrayIndex& idx) const
  90. {
  91. ANKI_ASSERT(idx.isValid());
  92. return U32(getArrayBase(idx.m_type) + m_componentCount[idx.m_type] * m_componentSize[idx.m_type] * idx.m_index);
  93. }
  94. constexpr static U32 getElementSize(GpuSceneContiguousArrayType type)
  95. {
  96. return m_componentSize[type] * m_componentCount[type];
  97. }
  98. /// @note Thread-safe against allocate(), deferredFree() and endFrame()
  99. void deferredFree(GpuSceneContiguousArrayIndex& idx);
  100. /// @note Thread-safe against allocate(), deferredFree() and endFrame()
  101. void endFrame();
  102. private:
  103. /// GPU scene allocator that emulates a contiguous array of elements. It's an array of objects of the same size.
  104. /// This array is a contiguous piece of memory. This helps same, in function, objects be close together.
  105. class ContiguousArrayAllocator
  106. {
  107. friend class AllGpuSceneContiguousArrays;
  108. public:
  109. ~ContiguousArrayAllocator()
  110. {
  111. ANKI_ASSERT(m_nextSlotIndex == 0 && "Forgot to deallocate");
  112. for([[maybe_unused]] const SceneDynamicArray<U32>& arr : m_garbage)
  113. {
  114. ANKI_ASSERT(arr.getSize() == 0);
  115. }
  116. }
  117. void init(U32 initialArraySize, U16 objectSize, F32 arrayGrowRate)
  118. {
  119. ANKI_ASSERT(initialArraySize > 0);
  120. ANKI_ASSERT(objectSize > 0 && objectSize <= 256); // 256 is arbitary
  121. ANKI_ASSERT(arrayGrowRate > 1.0);
  122. m_objectSize = objectSize;
  123. m_growRate = arrayGrowRate;
  124. m_initialArraySize = initialArraySize;
  125. }
  126. void destroy();
  127. /// Allocate a new object and return its index in the array.
  128. /// @note It's thread-safe against itself, deferredFree and endFrame.
  129. U32 allocateObject();
  130. /// Safely free an index allocated by allocateObject.
  131. /// @note It's thread-safe against itself, allocateObject and endFrame.
  132. void deferredFree(U32 crntFrameIdx, U32 index);
  133. /// Call this every frame.
  134. /// @note It's thread-safe against itself, deferredFree and allocateObject.
  135. void collectGarbage(U32 newFrameIdx);
  136. PtrSize getArrayBase() const
  137. {
  138. return m_allocation.getOffset();
  139. }
  140. U32 getElementCount() const
  141. {
  142. return m_nextSlotIndex;
  143. }
  144. private:
  145. GpuSceneBufferAllocation m_allocation;
  146. SceneDynamicArray<U32> m_freeSlotStack;
  147. Array<SceneDynamicArray<U32>, kMaxFramesInFlight> m_garbage;
  148. mutable SpinLock m_mtx;
  149. F32 m_growRate = 2.0;
  150. U32 m_initialArraySize = 0;
  151. U16 m_objectSize = 0;
  152. U32 m_nextSlotIndex = 0;
  153. };
  154. Array<ContiguousArrayAllocator, U32(GpuSceneContiguousArrayType::kCount)> m_allocs;
  155. U8 m_frame = 0;
  156. static constexpr Array<U8, U32(GpuSceneContiguousArrayType::kCount)> m_componentCount = {
  157. 2, kMaxLodCount, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  158. static constexpr Array<U8, U32(GpuSceneContiguousArrayType::kCount)> m_componentSize = {
  159. sizeof(Mat3x4),
  160. sizeof(GpuSceneMeshLod),
  161. sizeof(GpuSceneParticleEmitter),
  162. sizeof(GpuScenePointLight),
  163. sizeof(GpuSceneSpotLight),
  164. sizeof(GpuSceneReflectionProbe),
  165. sizeof(GpuSceneGlobalIlluminationProbe),
  166. sizeof(GpuSceneDecal),
  167. sizeof(GpuSceneFogDensityVolume),
  168. sizeof(GpuSceneRenderable),
  169. sizeof(GpuSceneRenderable),
  170. sizeof(GpuSceneRenderable),
  171. sizeof(GpuSceneRenderableAabb)};
  172. AllGpuSceneContiguousArrays();
  173. ~AllGpuSceneContiguousArrays();
  174. };
  175. inline GpuSceneContiguousArrayIndex::~GpuSceneContiguousArrayIndex()
  176. {
  177. AllGpuSceneContiguousArrays::getSingleton().deferredFree(*this);
  178. }
  179. inline U32 GpuSceneContiguousArrayIndex::getOffsetInGpuScene() const
  180. {
  181. return AllGpuSceneContiguousArrays::getSingleton().getElementOffsetInGpuScene(*this);
  182. }
  183. /// @}
  184. } // end namespace anki