Skip to content

Commit da85ec3

Browse files
Made VmaDefragmentationAlgorithm an abstract class and specific implementation as new class VmaDefragmentationAlgorithm_Generic, so user can easily plug his own defragmentation algorithms.
1 parent fb00cc9 commit da85ec3

1 file changed

Lines changed: 57 additions & 29 deletions

File tree

src/vk_mem_alloc.h

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,7 +5089,7 @@ class VmaBlockMetadata_Generic : public VmaBlockMetadata
50895089
virtual bool ResizeAllocation(const VmaAllocation alloc, VkDeviceSize newSize);
50905090

50915091
private:
5092-
friend class VmaDefragmentationAlgorithm;
5092+
friend class VmaDefragmentationAlgorithm_Generic;
50935093

50945094
uint32_t m_FreeCount;
50955095
VkDeviceSize m_SumFreeSize;
@@ -5621,7 +5621,7 @@ struct VmaBlockVector
56215621
size_t CalcAllocationCount();
56225622

56235623
private:
5624-
friend class VmaDefragmentationAlgorithm;
5624+
friend class VmaDefragmentationAlgorithm_Generic;
56255625

56265626
const VmaAllocator m_hAllocator;
56275627
const uint32_t m_MemoryTypeIndex;
@@ -5720,31 +5720,32 @@ class VmaDefragmentationAlgorithm
57205720
VmaDefragmentationAlgorithm(
57215721
VmaAllocator hAllocator,
57225722
VmaBlockVector* pBlockVector,
5723-
uint32_t currentFrameIndex);
5724-
virtual ~VmaDefragmentationAlgorithm();
5723+
uint32_t currentFrameIndex) :
5724+
m_hAllocator(hAllocator),
5725+
m_pBlockVector(pBlockVector),
5726+
m_CurrentFrameIndex(currentFrameIndex)
5727+
{
5728+
}
5729+
virtual ~VmaDefragmentationAlgorithm()
5730+
{
5731+
}
57255732

5726-
void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged);
5727-
void AddAll() { m_AllAllocations = true; }
5733+
virtual void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged) = 0;
5734+
virtual void AddAll() = 0;
57285735

5729-
VkResult Defragment(
5736+
virtual VkResult Defragment(
57305737
VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,
57315738
VkDeviceSize maxBytesToMove,
5732-
uint32_t maxAllocationsToMove);
5739+
uint32_t maxAllocationsToMove) = 0;
57335740

5734-
VkDeviceSize GetBytesMoved() const { return m_BytesMoved; }
5735-
uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; }
5741+
virtual VkDeviceSize GetBytesMoved() const = 0;
5742+
virtual uint32_t GetAllocationsMoved() const = 0;
57365743

5737-
private:
5744+
protected:
57385745
VmaAllocator const m_hAllocator;
57395746
VmaBlockVector* const m_pBlockVector;
57405747
const uint32_t m_CurrentFrameIndex;
57415748

5742-
uint32_t m_AllocationCount;
5743-
bool m_AllAllocations;
5744-
5745-
VkDeviceSize m_BytesMoved;
5746-
uint32_t m_AllocationsMoved;
5747-
57485749
struct AllocationInfo
57495750
{
57505751
VmaAllocation m_hAllocation;
@@ -5761,6 +5762,35 @@ class VmaDefragmentationAlgorithm
57615762
{
57625763
}
57635764
};
5765+
};
5766+
5767+
class VmaDefragmentationAlgorithm_Generic : public VmaDefragmentationAlgorithm
5768+
{
5769+
VMA_CLASS_NO_COPY(VmaDefragmentationAlgorithm_Generic)
5770+
public:
5771+
VmaDefragmentationAlgorithm_Generic(
5772+
VmaAllocator hAllocator,
5773+
VmaBlockVector* pBlockVector,
5774+
uint32_t currentFrameIndex);
5775+
virtual ~VmaDefragmentationAlgorithm_Generic();
5776+
5777+
virtual void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged);
5778+
virtual void AddAll() { m_AllAllocations = true; }
5779+
5780+
virtual VkResult Defragment(
5781+
VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,
5782+
VkDeviceSize maxBytesToMove,
5783+
uint32_t maxAllocationsToMove);
5784+
5785+
virtual VkDeviceSize GetBytesMoved() const { return m_BytesMoved; }
5786+
virtual uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; }
5787+
5788+
private:
5789+
uint32_t m_AllocationCount;
5790+
bool m_AllAllocations;
5791+
5792+
VkDeviceSize m_BytesMoved;
5793+
uint32_t m_AllocationsMoved;
57645794

57655795
struct AllocationInfoSizeGreater
57665796
{
@@ -11872,15 +11902,13 @@ void VmaBlockVector::AddStats(VmaStats* pStats)
1187211902
}
1187311903

1187411904
////////////////////////////////////////////////////////////////////////////////
11875-
// VmaDefragmentationAlgorithm members definition
11905+
// VmaDefragmentationAlgorithm_Generic members definition
1187611906

11877-
VmaDefragmentationAlgorithm::VmaDefragmentationAlgorithm(
11907+
VmaDefragmentationAlgorithm_Generic::VmaDefragmentationAlgorithm_Generic(
1187811908
VmaAllocator hAllocator,
1187911909
VmaBlockVector* pBlockVector,
1188011910
uint32_t currentFrameIndex) :
11881-
m_hAllocator(hAllocator),
11882-
m_pBlockVector(pBlockVector),
11883-
m_CurrentFrameIndex(currentFrameIndex),
11911+
VmaDefragmentationAlgorithm(hAllocator, pBlockVector, currentFrameIndex),
1188411912
m_AllAllocations(false),
1188511913
m_AllocationCount(0),
1188611914
m_BytesMoved(0),
@@ -11901,15 +11929,15 @@ VmaDefragmentationAlgorithm::VmaDefragmentationAlgorithm(
1190111929
VMA_SORT(m_Blocks.begin(), m_Blocks.end(), BlockPointerLess());
1190211930
}
1190311931

11904-
VmaDefragmentationAlgorithm::~VmaDefragmentationAlgorithm()
11932+
VmaDefragmentationAlgorithm_Generic::~VmaDefragmentationAlgorithm_Generic()
1190511933
{
1190611934
for(size_t i = m_Blocks.size(); i--; )
1190711935
{
1190811936
vma_delete(m_hAllocator, m_Blocks[i]);
1190911937
}
1191011938
}
1191111939

11912-
void VmaDefragmentationAlgorithm::AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged)
11940+
void VmaDefragmentationAlgorithm_Generic::AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged)
1191311941
{
1191411942
// Now as we are inside VmaBlockVector::m_Mutex, we can make final check if this allocation was not lost.
1191511943
if(hAlloc->GetLastUseFrameIndex() != VMA_FRAME_INDEX_LOST)
@@ -11930,7 +11958,7 @@ void VmaDefragmentationAlgorithm::AddAllocation(VmaAllocation hAlloc, VkBool32*
1193011958
}
1193111959
}
1193211960

11933-
VkResult VmaDefragmentationAlgorithm::DefragmentRound(
11961+
VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound(
1193411962
VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,
1193511963
VkDeviceSize maxBytesToMove,
1193611964
uint32_t maxAllocationsToMove)
@@ -12078,7 +12106,7 @@ VkResult VmaDefragmentationAlgorithm::DefragmentRound(
1207812106
}
1207912107
}
1208012108

12081-
size_t VmaDefragmentationAlgorithm::CalcBlocksWithNonMovableCount() const
12109+
size_t VmaDefragmentationAlgorithm_Generic::CalcBlocksWithNonMovableCount() const
1208212110
{
1208312111
size_t result = 0;
1208412112
for(size_t i = 0; i < m_Blocks.size(); ++i)
@@ -12091,7 +12119,7 @@ size_t VmaDefragmentationAlgorithm::CalcBlocksWithNonMovableCount() const
1209112119
return result;
1209212120
}
1209312121

12094-
VkResult VmaDefragmentationAlgorithm::Defragment(
12122+
VkResult VmaDefragmentationAlgorithm_Generic::Defragment(
1209512123
VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,
1209612124
VkDeviceSize maxBytesToMove,
1209712125
uint32_t maxAllocationsToMove)
@@ -12146,7 +12174,7 @@ VkResult VmaDefragmentationAlgorithm::Defragment(
1214612174
return result;
1214712175
}
1214812176

12149-
bool VmaDefragmentationAlgorithm::MoveMakesSense(
12177+
bool VmaDefragmentationAlgorithm_Generic::MoveMakesSense(
1215012178
size_t dstBlockIndex, VkDeviceSize dstOffset,
1215112179
size_t srcBlockIndex, VkDeviceSize srcOffset)
1215212180
{
@@ -12204,7 +12232,7 @@ void VmaBlockVectorDefragmentationContext::Begin()
1220412232
const bool allAllocations = m_AllAllocations ||
1220512233
m_Allocations.size() == m_pBlockVector->CalcAllocationCount();
1220612234

12207-
m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm)(
12235+
m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm_Generic)(
1220812236
m_hAllocator, m_pBlockVector, m_CurrFrameIndex);
1220912237

1221012238
if(allAllocations)

0 commit comments

Comments
 (0)