ContiguousArrayAllocator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. void GpuSceneContiguousArrays::ContiguousArrayAllocator::destroy()
  10. {
  11. for(U32 i = 0; i < kMaxFramesInFlight; ++i)
  12. {
  13. collectGarbage(i);
  14. }
  15. }
  16. U32 GpuSceneContiguousArrays::ContiguousArrayAllocator::allocateObject()
  17. {
  18. LockGuard lock(m_mtx);
  19. if(!m_allocation.isValid())
  20. {
  21. // Initialize
  22. const U32 alignment = GrManager::getSingleton().getDeviceCapabilities().m_storageBufferBindOffsetAlignment;
  23. GpuSceneBuffer::getSingleton().allocate(m_objectSize * m_initialArraySize, alignment, m_allocation);
  24. m_nextSlotIndex = 0;
  25. m_freeSlotStack.resize(m_initialArraySize);
  26. for(U32 i = 0; i < m_initialArraySize; ++i)
  27. {
  28. m_freeSlotStack[i] = i;
  29. }
  30. }
  31. else if(m_nextSlotIndex == m_freeSlotStack.getSize())
  32. {
  33. // Grow
  34. ANKI_ASSERT(!"TODO");
  35. }
  36. const U32 idx = m_freeSlotStack[m_nextSlotIndex];
  37. ++m_nextSlotIndex;
  38. ANKI_ASSERT(idx < m_freeSlotStack.getSize());
  39. return idx;
  40. }
  41. void GpuSceneContiguousArrays::ContiguousArrayAllocator::deferredFree(U32 crntFrameIdx, U32 index)
  42. {
  43. LockGuard lock(m_mtx);
  44. ANKI_ASSERT(index < m_freeSlotStack.getSize());
  45. m_garbage[crntFrameIdx].emplaceBack(index);
  46. }
  47. void GpuSceneContiguousArrays::ContiguousArrayAllocator::collectGarbage(U32 newFrameIdx)
  48. {
  49. LockGuard lock(m_mtx);
  50. if(m_garbage[newFrameIdx].getSize() == 0) [[likely]]
  51. {
  52. return;
  53. }
  54. // Release deferred frees
  55. for(U32 idx : m_garbage[newFrameIdx])
  56. {
  57. ANKI_ASSERT(m_nextSlotIndex > 0);
  58. --m_nextSlotIndex;
  59. m_freeSlotStack[m_nextSlotIndex] = idx;
  60. }
  61. m_garbage[newFrameIdx].destroy();
  62. // Sort so we can keep memory close to the beginning of the array for better cache behaviour
  63. std::sort(m_freeSlotStack.getBegin() + m_nextSlotIndex, m_freeSlotStack.getEnd());
  64. // Adjust the stack size
  65. const U32 allocatedSlots = m_nextSlotIndex;
  66. if(U32(F32(allocatedSlots) * m_growRate) < m_freeSlotStack.getSize() && m_freeSlotStack.getSize() > m_initialArraySize)
  67. {
  68. // Shrink
  69. ANKI_ASSERT(!"TODO");
  70. }
  71. else if(allocatedSlots == 0)
  72. {
  73. ANKI_ASSERT(m_nextSlotIndex == 0);
  74. GpuSceneBuffer::getSingleton().deferredFree(m_allocation);
  75. m_freeSlotStack.destroy();
  76. }
  77. }
  78. GpuSceneContiguousArrays::GpuSceneContiguousArrays()
  79. {
  80. const ConfigSet& cfg = ConfigSet::getSingleton();
  81. constexpr F32 kGrowRate = 2.0;
  82. const Array<U32, U32(GpuSceneContiguousArrayType::kCount)> minElementCount = {
  83. cfg.getSceneMinGpuSceneTransforms(), cfg.getSceneMinGpuSceneMeshes(),
  84. cfg.getSceneMinGpuSceneParticleEmitters(), cfg.getSceneMinGpuSceneLights(),
  85. cfg.getSceneMinGpuSceneReflectionProbes(), cfg.getSceneMinGpuSceneGlobalIlluminationProbes(),
  86. cfg.getSceneMinGpuSceneDecals(), cfg.getSceneMinGpuSceneFogDensityVolumes(),
  87. cfg.getSceneMinGpuSceneRenderables(), cfg.getSceneMinGpuSceneRenderables(),
  88. cfg.getSceneMinGpuSceneRenderables(), cfg.getSceneMinGpuSceneRenderables()};
  89. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  90. {
  91. const U32 initialArraySize = minElementCount[type] / m_componentCount[type];
  92. const U16 elementSize = m_componentSize[type] * m_componentCount[type];
  93. m_allocs[type].init(initialArraySize, elementSize, kGrowRate);
  94. }
  95. }
  96. GpuSceneContiguousArrays::~GpuSceneContiguousArrays()
  97. {
  98. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  99. {
  100. m_allocs[type].destroy();
  101. }
  102. }
  103. GpuSceneContiguousArrayIndex GpuSceneContiguousArrays::allocate(GpuSceneContiguousArrayType type)
  104. {
  105. GpuSceneContiguousArrayIndex out;
  106. out.m_index = m_allocs[type].allocateObject();
  107. out.m_type = type;
  108. return out;
  109. }
  110. void GpuSceneContiguousArrays::deferredFree(GpuSceneContiguousArrayIndex& idx)
  111. {
  112. if(idx.isValid())
  113. {
  114. m_allocs[idx.m_type].deferredFree(m_frame, idx.m_index);
  115. idx.invalidate();
  116. }
  117. }
  118. void GpuSceneContiguousArrays::endFrame()
  119. {
  120. m_frame = (m_frame + 1) % kMaxFramesInFlight;
  121. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  122. {
  123. m_allocs[type].collectGarbage(m_frame);
  124. }
  125. }
  126. } // end namespace anki