Skip to content

Commit fb00cc9

Browse files
Removed VMA_DEFRAGMENTATION_* flags. VmaDefragmentationInfo2::flags is now reserved for future use and should be 0. Research various options and chosen some parameters as default.
1 parent 434ac86 commit fb00cc9

2 files changed

Lines changed: 34 additions & 60 deletions

File tree

src/VmaReplay/VmaReplay.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ void Player::Defragment()
17851785
const duration defragDurationEnd = timeAfterDefragEnd - timeAfterGpu;
17861786

17871787
// If anything changed.
1788-
if(defragStats.allocationsLost > 0 || defragStats.allocationsMoved > 0)
1788+
if(defragStats.allocationsMoved > 0)
17891789
{
17901790
// Go over allocation that changed and destroy their buffers and images.
17911791
size_t i = 0;
@@ -1825,7 +1825,6 @@ void Player::Defragment()
18251825
printf(" bytesFreed: %llu\n", defragStats.bytesFreed);
18261826
printf(" allocationsMoved: %u\n", defragStats.allocationsMoved);
18271827
printf(" deviceMemoryBlocksFreed: %u\n", defragStats.deviceMemoryBlocksFreed);
1828-
printf(" allocationsLost: %u\n", defragStats.allocationsLost);
18291828

18301829
vmaCalculateStats(m_Allocator, &stats);
18311830
PrintStats(stats, "after defragmentation");

src/vk_mem_alloc.h

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,25 +2578,8 @@ Call function vmaDefragmentationEnd() to destroy it.
25782578
*/
25792579
VK_DEFINE_HANDLE(VmaDefragmentationContext)
25802580

2581-
/// Flags to be used in vmaDefragmentationBegin().
2581+
/// Flags to be used in vmaDefragmentationBegin(). None at the moment. Reserved for future use.
25822582
typedef enum VmaDefragmentationFlagBits {
2583-
/** Add this flag to change defragmentation algorithm to fast rather than default (balanced).
2584-
This algorithm will favor speed over quality of defragmentation.
2585-
Defragmentation will be done as fast and move as little allocations and bytes as possible while
2586-
still providing some benefits.
2587-
*/
2588-
VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT = 0x00000001,
2589-
/** Add this flag to change defragmentation algorithm to optimal rather than default (balanced).
2590-
This algorithm will favor quality of defragmentation over speed.
2591-
Allocations will be as perfectly compacted as possible.
2592-
*/
2593-
VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT = 0x00000002,
2594-
/** \brief A bit mask to extract only `ALGORITHM` bits from entire set of flags.
2595-
*/
2596-
VMA_DEFRAGMENTATION_ALGORITHM_MASK =
2597-
VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT |
2598-
VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT,
2599-
26002583
VMA_DEFRAGMENTATION_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
26012584
} VmaDefragmentationFlagBits;
26022585
typedef VkFlags VmaDefragmentationFlags;
@@ -2606,7 +2589,7 @@ typedef VkFlags VmaDefragmentationFlags;
26062589
To be used with function vmaDefragmentationBegin().
26072590
*/
26082591
typedef struct VmaDefragmentationInfo2 {
2609-
/** \brief Flags for defragmentation. Use #VmaDefragmentationFlagBits enum.
2592+
/** \brief Reserved for future use. Should be 0.
26102593
*/
26112594
VmaDefragmentationFlags flags;
26122595
/** \brief Number of allocations in `pAllocations` array.
@@ -5737,8 +5720,7 @@ class VmaDefragmentationAlgorithm
57375720
VmaDefragmentationAlgorithm(
57385721
VmaAllocator hAllocator,
57395722
VmaBlockVector* pBlockVector,
5740-
uint32_t currentFrameIndex,
5741-
uint32_t algorithmFlags); // Zero or one of VMA_DEFRAGMENTATION_*_ALGORITHM_BIT.
5723+
uint32_t currentFrameIndex);
57425724
virtual ~VmaDefragmentationAlgorithm();
57435725

57445726
void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged);
@@ -5756,7 +5738,6 @@ class VmaDefragmentationAlgorithm
57565738
VmaAllocator const m_hAllocator;
57575739
VmaBlockVector* const m_pBlockVector;
57585740
const uint32_t m_CurrentFrameIndex;
5759-
const uint32_t m_AlgorithmFlags;
57605741

57615742
uint32_t m_AllocationCount;
57625743
bool m_AllAllocations;
@@ -5819,7 +5800,7 @@ class VmaDefragmentationAlgorithm
58195800
m_HasNonMovableAllocations = blockAllocCount != defragmentAllocCount;
58205801
}
58215802

5822-
void SortAllocationsBySizeDescecnding()
5803+
void SortAllocationsBySizeDescending()
58235804
{
58245805
VMA_SORT(m_Allocations.begin(), m_Allocations.end(), AllocationInfoSizeGreater());
58255806
}
@@ -5911,7 +5892,7 @@ class VmaBlockVectorDefragmentationContext
59115892
VmaPool hCustomPool, // Optional.
59125893
VmaBlockVector* pBlockVector,
59135894
uint32_t currFrameIndex,
5914-
uint32_t algorithmFlags); // Zero or one of VMA_DEFRAGMENTATION_*_ALGORITHM_BIT.
5895+
uint32_t flags);
59155896
~VmaBlockVectorDefragmentationContext();
59165897

59175898
VmaPool GetCustomPool() const { return m_hCustomPool; }
@@ -5952,7 +5933,7 @@ struct VmaDefragmentationContext_T
59525933
VmaDefragmentationContext_T(
59535934
VmaAllocator hAllocator,
59545935
uint32_t currFrameIndex,
5955-
uint32_t algorithmFlags,
5936+
uint32_t flags,
59565937
VmaDefragmentationStats* pStats);
59575938
~VmaDefragmentationContext_T();
59585939

@@ -5976,7 +5957,7 @@ struct VmaDefragmentationContext_T
59765957
private:
59775958
const VmaAllocator m_hAllocator;
59785959
const uint32_t m_CurrFrameIndex;
5979-
const uint32_t m_AlgorithmFlags;
5960+
const uint32_t m_Flags;
59805961
VmaDefragmentationStats* const m_pStats;
59815962
// Owner of these objects.
59825963
VmaBlockVectorDefragmentationContext* m_DefaultPoolContexts[VK_MAX_MEMORY_TYPES];
@@ -11896,12 +11877,10 @@ void VmaBlockVector::AddStats(VmaStats* pStats)
1189611877
VmaDefragmentationAlgorithm::VmaDefragmentationAlgorithm(
1189711878
VmaAllocator hAllocator,
1189811879
VmaBlockVector* pBlockVector,
11899-
uint32_t currentFrameIndex,
11900-
uint32_t algorithmFlags) :
11880+
uint32_t currentFrameIndex) :
1190111881
m_hAllocator(hAllocator),
1190211882
m_pBlockVector(pBlockVector),
1190311883
m_CurrentFrameIndex(currentFrameIndex),
11904-
m_AlgorithmFlags(algorithmFlags),
1190511884
m_AllAllocations(false),
1190611885
m_AllocationCount(0),
1190711886
m_BytesMoved(0),
@@ -11961,21 +11940,17 @@ VkResult VmaDefragmentationAlgorithm::DefragmentRound(
1196111940
return VK_SUCCESS;
1196211941
}
1196311942

11964-
uint32_t strategy = UINT32_MAX;
11965-
switch(m_AlgorithmFlags)
11966-
{
11967-
case VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT:
11968-
strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_TIME_BIT;
11969-
break;
11970-
case VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT:
11971-
strategy = VMA_ALLOCATION_INTERNAL_STRATEGY_MIN_OFFSET;
11972-
break;
11973-
default:
11974-
strategy = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT;
11975-
}
11943+
// This is a choice based on research.
11944+
// Option 1:
11945+
uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_TIME_BIT;
11946+
// Option 2:
11947+
//uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT;
11948+
// Option 3:
11949+
//uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_FRAGMENTATION_BIT;
1197611950

1197711951
size_t srcBlockMinIndex = 0;
1197811952
// When FAST_ALGORITHM, move allocations from only last out of blocks that contain non-movable allocations.
11953+
/*
1197911954
if(m_AlgorithmFlags & VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT)
1198011955
{
1198111956
const size_t blocksWithNonMovableCount = CalcBlocksWithNonMovableCount();
@@ -11984,6 +11959,7 @@ VkResult VmaDefragmentationAlgorithm::DefragmentRound(
1198411959
srcBlockMinIndex = blocksWithNonMovableCount - 1;
1198511960
}
1198611961
}
11962+
*/
1198711963

1198811964
size_t srcBlockIndex = m_Blocks.size() - 1;
1198911965
size_t srcAllocIndex = SIZE_MAX;
@@ -12146,21 +12122,21 @@ VkResult VmaDefragmentationAlgorithm::Defragment(
1214612122
}
1214712123

1214812124
pBlockInfo->CalcHasNonMovableAllocations();
12149-
if((m_AlgorithmFlags & VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT) != 0)
12150-
{
12151-
pBlockInfo->SortAllocationsByOffsetDescending();
12152-
}
12153-
else
12154-
{
12155-
pBlockInfo->SortAllocationsBySizeDescecnding();
12156-
}
12125+
12126+
// This is a choice based on research.
12127+
// Option 1:
12128+
pBlockInfo->SortAllocationsByOffsetDescending();
12129+
// Option 2:
12130+
//pBlockInfo->SortAllocationsBySizeDescending();
1215712131
}
1215812132

1215912133
// Sort m_Blocks this time by the main criterium, from most "destination" to most "source" blocks.
1216012134
VMA_SORT(m_Blocks.begin(), m_Blocks.end(), BlockInfoCompareMoveDestination());
1216112135

12136+
// This is a choice based on research.
12137+
const uint32_t roundCount = 2;
12138+
1216212139
// Execute defragmentation rounds (the main part).
12163-
const uint32_t roundCount = (m_AlgorithmFlags & VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT) ? 1 : 2;
1216412140
VkResult result = VK_SUCCESS;
1216512141
for(uint32_t round = 0; (round < roundCount) && (result == VK_SUCCESS); ++round)
1216612142
{
@@ -12229,7 +12205,7 @@ void VmaBlockVectorDefragmentationContext::Begin()
1222912205
m_Allocations.size() == m_pBlockVector->CalcAllocationCount();
1223012206

1223112207
m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm)(
12232-
m_hAllocator, m_pBlockVector, m_CurrFrameIndex, m_AlgorithmFlags);
12208+
m_hAllocator, m_pBlockVector, m_CurrFrameIndex);
1223312209

1223412210
if(allAllocations)
1223512211
{
@@ -12250,11 +12226,11 @@ void VmaBlockVectorDefragmentationContext::Begin()
1225012226
VmaDefragmentationContext_T::VmaDefragmentationContext_T(
1225112227
VmaAllocator hAllocator,
1225212228
uint32_t currFrameIndex,
12253-
uint32_t algorithmFlags,
12229+
uint32_t flags,
1225412230
VmaDefragmentationStats* pStats) :
1225512231
m_hAllocator(hAllocator),
1225612232
m_CurrFrameIndex(currFrameIndex),
12257-
m_AlgorithmFlags(algorithmFlags),
12233+
m_Flags(flags),
1225812234
m_pStats(pStats),
1225912235
m_CustomPoolContexts(VmaStlAllocator<VmaBlockVectorDefragmentationContext*>(hAllocator->GetAllocationCallbacks()))
1226012236
{
@@ -12307,7 +12283,7 @@ void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, VmaPool* pPools)
1230712283
pool,
1230812284
&pool->m_BlockVector,
1230912285
m_CurrFrameIndex,
12310-
m_AlgorithmFlags);
12286+
m_Flags);
1231112287
m_CustomPoolContexts.push_back(pBlockVectorDefragCtx);
1231212288
}
1231312289

@@ -12355,7 +12331,7 @@ void VmaDefragmentationContext_T::AddAllocations(
1235512331
hAllocPool,
1235612332
&hAllocPool->m_BlockVector,
1235712333
m_CurrFrameIndex,
12358-
m_AlgorithmFlags);
12334+
m_Flags);
1235912335
m_CustomPoolContexts.push_back(pBlockVectorDefragCtx);
1236012336
}
1236112337
}
@@ -12372,7 +12348,7 @@ void VmaDefragmentationContext_T::AddAllocations(
1237212348
VMA_NULL, // hCustomPool
1237312349
m_hAllocator->m_pBlockVectors[memTypeIndex],
1237412350
m_CurrFrameIndex,
12375-
m_AlgorithmFlags);
12351+
m_Flags);
1237612352
m_DefaultPoolContexts[memTypeIndex] = pBlockVectorDefragCtx;
1237712353
}
1237812354
}
@@ -13713,9 +13689,8 @@ VkResult VmaAllocator_T::DefragmentationBegin(
1371313689
memset(info.pAllocationsChanged, 0, info.allocationCount * sizeof(VkBool32));
1371413690
}
1371513691

13716-
const uint32_t algorithmFlags = info.flags & VMA_DEFRAGMENTATION_ALGORITHM_MASK;
1371713692
*pContext = vma_new(this, VmaDefragmentationContext_T)(
13718-
this, m_CurrentFrameIndex.load(), algorithmFlags, pStats);
13693+
this, m_CurrentFrameIndex.load(), info.flags, pStats);
1371913694

1372013695
(*pContext)->AddPools(info.poolCount, info.pPools);
1372113696
(*pContext)->AddAllocations(

0 commit comments

Comments
 (0)