ContiguousArrayAllocator.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <AnKi/Scene/ContiguousArrayAllocator.h>
  6. #include <AnKi/Core/ConfigSet.h>
  7. #include <AnKi/Gr/GrManager.h>
  8. namespace anki {
  9. static NumericCVar<U32> g_minGpuSceneTransformsCVar(CVarSubsystem::kScene, "MinGpuSceneTransforms", 2 * 10 * 1024, 8, 100 * 1024,
  10. "The min number of transforms stored in the GPU scene");
  11. static NumericCVar<U32> g_minGpuSceneMeshesCVar(CVarSubsystem::kScene, "MinGpuSceneMeshes", 8 * 1024, 8, 100 * 1024,
  12. "The min number of meshes stored in the GPU scene");
  13. static NumericCVar<U32> g_minGpuSceneParticleEmittersCVar(CVarSubsystem::kScene, "MinGpuSceneParticleEmitters", 1 * 1024, 8, 100 * 1024,
  14. "The min number of particle emitters stored in the GPU scene");
  15. static NumericCVar<U32> g_minGpuSceneLightsCVar(CVarSubsystem::kScene, "MinGpuSceneLights", 2 * 1024, 8, 100 * 1024,
  16. "The min number of lights stored in the GPU scene");
  17. static NumericCVar<U32> g_minGpuSceneReflectionProbesCVar(CVarSubsystem::kScene, "MinGpuSceneReflectionProbes", 128, 8, 100 * 1024,
  18. "The min number of reflection probes stored in the GPU scene");
  19. static NumericCVar<U32> g_minGpuSceneGlobalIlluminationProbesCVar(CVarSubsystem::kScene, "MinGpuSceneGlobalIlluminationProbes", 128, 8, 100 * 1024,
  20. "The min number of GI probes stored in the GPU scene");
  21. static NumericCVar<U32> g_minGpuSceneDecalsCVar(CVarSubsystem::kScene, "MinGpuSceneDecals", 2 * 1024, 8, 100 * 1024,
  22. "The min number of decals stored in the GPU scene");
  23. static NumericCVar<U32> g_minGpuSceneFogDensityVolumesCVar(CVarSubsystem::kScene, "MinGpuSceneFogDensityVolumes", 512, 8, 100 * 1024,
  24. "The min number fog density volumes stored in the GPU scene");
  25. static NumericCVar<U32> g_minGpuSceneRenderablesCVar(CVarSubsystem::kScene, "MinGpuSceneRenderables", 10 * 1024, 8, 100 * 1024,
  26. "The min number of renderables stored in the GPU scene");
  27. void GpuSceneContiguousArrays::ContiguousArrayAllocator::destroy()
  28. {
  29. for(U32 i = 0; i < kMaxFramesInFlight; ++i)
  30. {
  31. collectGarbage(i);
  32. }
  33. }
  34. U32 GpuSceneContiguousArrays::ContiguousArrayAllocator::allocateObject()
  35. {
  36. LockGuard lock(m_mtx);
  37. if(!m_allocation.isValid())
  38. {
  39. // Initialize
  40. const U32 alignment = GrManager::getSingleton().getDeviceCapabilities().m_storageBufferBindOffsetAlignment;
  41. GpuSceneBuffer::getSingleton().allocate(m_objectSize * m_initialArraySize, alignment, m_allocation);
  42. m_nextSlotIndex = 0;
  43. m_freeSlotStack.resize(m_initialArraySize);
  44. for(U32 i = 0; i < m_initialArraySize; ++i)
  45. {
  46. m_freeSlotStack[i] = i;
  47. }
  48. }
  49. else if(m_nextSlotIndex == m_freeSlotStack.getSize())
  50. {
  51. // Grow
  52. ANKI_ASSERT(!"TODO");
  53. }
  54. const U32 idx = m_freeSlotStack[m_nextSlotIndex];
  55. ++m_nextSlotIndex;
  56. ANKI_ASSERT(idx < m_freeSlotStack.getSize());
  57. return idx;
  58. }
  59. void GpuSceneContiguousArrays::ContiguousArrayAllocator::deferredFree(U32 crntFrameIdx, U32 index)
  60. {
  61. LockGuard lock(m_mtx);
  62. ANKI_ASSERT(index < m_freeSlotStack.getSize());
  63. m_garbage[crntFrameIdx].emplaceBack(index);
  64. }
  65. void GpuSceneContiguousArrays::ContiguousArrayAllocator::collectGarbage(U32 newFrameIdx)
  66. {
  67. LockGuard lock(m_mtx);
  68. if(m_garbage[newFrameIdx].getSize() == 0) [[likely]]
  69. {
  70. return;
  71. }
  72. // Release deferred frees
  73. for(U32 idx : m_garbage[newFrameIdx])
  74. {
  75. ANKI_ASSERT(m_nextSlotIndex > 0);
  76. --m_nextSlotIndex;
  77. m_freeSlotStack[m_nextSlotIndex] = idx;
  78. }
  79. m_garbage[newFrameIdx].destroy();
  80. // Sort so we can keep memory close to the beginning of the array for better cache behaviour
  81. std::sort(m_freeSlotStack.getBegin() + m_nextSlotIndex, m_freeSlotStack.getEnd());
  82. // Adjust the stack size
  83. const U32 allocatedSlots = m_nextSlotIndex;
  84. if(U32(F32(allocatedSlots) * m_growRate) < m_freeSlotStack.getSize() && m_freeSlotStack.getSize() > m_initialArraySize)
  85. {
  86. // Shrink
  87. ANKI_ASSERT(!"TODO");
  88. }
  89. else if(allocatedSlots == 0)
  90. {
  91. ANKI_ASSERT(m_nextSlotIndex == 0);
  92. GpuSceneBuffer::getSingleton().deferredFree(m_allocation);
  93. m_freeSlotStack.destroy();
  94. }
  95. }
  96. GpuSceneContiguousArrays::GpuSceneContiguousArrays()
  97. {
  98. constexpr F32 kGrowRate = 2.0;
  99. const Array<U32, U32(GpuSceneContiguousArrayType::kCount)> minElementCount = {
  100. g_minGpuSceneTransformsCVar.get(), g_minGpuSceneMeshesCVar.get(),
  101. g_minGpuSceneParticleEmittersCVar.get(), g_minGpuSceneLightsCVar.get(),
  102. g_minGpuSceneReflectionProbesCVar.get(), g_minGpuSceneGlobalIlluminationProbesCVar.get(),
  103. g_minGpuSceneDecalsCVar.get(), g_minGpuSceneFogDensityVolumesCVar.get(),
  104. g_minGpuSceneRenderablesCVar.get(), g_minGpuSceneRenderablesCVar.get(),
  105. g_minGpuSceneRenderablesCVar.get(), g_minGpuSceneRenderablesCVar.get()};
  106. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  107. {
  108. const U32 initialArraySize = minElementCount[type] / m_componentCount[type];
  109. const U16 elementSize = m_componentSize[type] * m_componentCount[type];
  110. m_allocs[type].init(initialArraySize, elementSize, kGrowRate);
  111. }
  112. }
  113. GpuSceneContiguousArrays::~GpuSceneContiguousArrays()
  114. {
  115. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  116. {
  117. m_allocs[type].destroy();
  118. }
  119. }
  120. GpuSceneContiguousArrayIndex GpuSceneContiguousArrays::allocate(GpuSceneContiguousArrayType type)
  121. {
  122. GpuSceneContiguousArrayIndex out;
  123. out.m_index = m_allocs[type].allocateObject();
  124. out.m_type = type;
  125. return out;
  126. }
  127. void GpuSceneContiguousArrays::deferredFree(GpuSceneContiguousArrayIndex& idx)
  128. {
  129. if(idx.isValid())
  130. {
  131. m_allocs[idx.m_type].deferredFree(m_frame, idx.m_index);
  132. idx.invalidate();
  133. }
  134. }
  135. void GpuSceneContiguousArrays::endFrame()
  136. {
  137. m_frame = (m_frame + 1) % kMaxFramesInFlight;
  138. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  139. {
  140. m_allocs[type].collectGarbage(m_frame);
  141. }
  142. }
  143. } // end namespace anki