ContiguousArrayAllocator.cpp 4.0 KB

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