2
0

ContiguousArrayAllocator.cpp 4.4 KB

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