ContiguousArrayAllocator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 AllGpuSceneContiguousArrays::ContiguousArrayAllocator::destroy()
  10. {
  11. for(U32 i = 0; i < kMaxFramesInFlight; ++i)
  12. {
  13. collectGarbage(i);
  14. }
  15. }
  16. U32 AllGpuSceneContiguousArrays::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 AllGpuSceneContiguousArrays::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 AllGpuSceneContiguousArrays::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()
  67. && m_freeSlotStack.getSize() > m_initialArraySize)
  68. {
  69. // Shrink
  70. ANKI_ASSERT(!"TODO");
  71. }
  72. else if(allocatedSlots == 0)
  73. {
  74. ANKI_ASSERT(m_nextSlotIndex == 0);
  75. GpuSceneBuffer::getSingleton().deferredFree(m_allocation);
  76. m_freeSlotStack.destroy();
  77. }
  78. }
  79. AllGpuSceneContiguousArrays::AllGpuSceneContiguousArrays()
  80. {
  81. const ConfigSet& cfg = ConfigSet::getSingleton();
  82. constexpr F32 kGrowRate = 2.0;
  83. const Array<U32, U32(GpuSceneContiguousArrayType::kCount)> minElementCount = {
  84. cfg.getSceneMinGpuSceneTransforms(),
  85. cfg.getSceneMinGpuSceneMeshes(),
  86. cfg.getSceneMinGpuSceneParticleEmitters(),
  87. cfg.getSceneMinGpuSceneLights(),
  88. cfg.getSceneMinGpuSceneLights(),
  89. cfg.getSceneMinGpuSceneReflectionProbes(),
  90. cfg.getSceneMinGpuSceneGlobalIlluminationProbes(),
  91. cfg.getSceneMinGpuSceneDecals(),
  92. cfg.getSceneMinGpuSceneFogDensityVolumes(),
  93. cfg.getSceneMinGpuSceneRenderables(),
  94. cfg.getSceneMinGpuSceneRenderables(),
  95. cfg.getSceneMinGpuSceneRenderables(),
  96. cfg.getSceneMinGpuSceneRenderables()};
  97. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  98. {
  99. const U32 initialArraySize = minElementCount[type] / m_componentCount[type];
  100. const U16 elementSize = m_componentSize[type] * m_componentCount[type];
  101. m_allocs[type].init(initialArraySize, elementSize, kGrowRate);
  102. }
  103. }
  104. AllGpuSceneContiguousArrays::~AllGpuSceneContiguousArrays()
  105. {
  106. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  107. {
  108. m_allocs[type].destroy();
  109. }
  110. }
  111. GpuSceneContiguousArrayIndex AllGpuSceneContiguousArrays::allocate(GpuSceneContiguousArrayType type)
  112. {
  113. GpuSceneContiguousArrayIndex out;
  114. out.m_index = m_allocs[type].allocateObject();
  115. out.m_type = type;
  116. return out;
  117. }
  118. void AllGpuSceneContiguousArrays::deferredFree(GpuSceneContiguousArrayIndex& idx)
  119. {
  120. if(idx.isValid())
  121. {
  122. m_allocs[idx.m_type].deferredFree(m_frame, idx.m_index);
  123. idx.invalidate();
  124. }
  125. }
  126. void AllGpuSceneContiguousArrays::endFrame()
  127. {
  128. m_frame = (m_frame + 1) % kMaxFramesInFlight;
  129. for(GpuSceneContiguousArrayType type : EnumIterable<GpuSceneContiguousArrayType>())
  130. {
  131. m_allocs[type].collectGarbage(m_frame);
  132. }
  133. }
  134. } // end namespace anki