Skip to content

Commit 52076eb

Browse files
Added VmaDefragmentationInfo2::poolCount, pPools. Added test for it - TestDefragmentationWholePool. Removed VmaDefragmentationStats::allocationsLost. Optimized defragmentation algorithm.
1 parent da5d248 commit 52076eb

2 files changed

Lines changed: 282 additions & 56 deletions

File tree

src/Tests.cpp

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,93 @@ void TestDefragmentationSimple()
13611361
vmaDestroyPool(g_hAllocator, pool);
13621362
}
13631363

1364+
void TestDefragmentationWholePool()
1365+
{
1366+
wprintf(L"Test defragmentation whole pool\n");
1367+
1368+
RandomNumberGenerator rand(668);
1369+
1370+
const VkDeviceSize BUF_SIZE = 0x10000;
1371+
const VkDeviceSize BLOCK_SIZE = BUF_SIZE * 8;
1372+
1373+
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
1374+
bufCreateInfo.size = BUF_SIZE;
1375+
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1376+
1377+
VmaAllocationCreateInfo exampleAllocCreateInfo = {};
1378+
exampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
1379+
1380+
uint32_t memTypeIndex = UINT32_MAX;
1381+
vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &bufCreateInfo, &exampleAllocCreateInfo, &memTypeIndex);
1382+
1383+
VmaPoolCreateInfo poolCreateInfo = {};
1384+
poolCreateInfo.blockSize = BLOCK_SIZE;
1385+
poolCreateInfo.memoryTypeIndex = memTypeIndex;
1386+
1387+
VmaDefragmentationStats defragStats[2];
1388+
for(size_t caseIndex = 0; caseIndex < 2; ++caseIndex)
1389+
{
1390+
VmaPool pool;
1391+
ERR_GUARD_VULKAN( vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool) );
1392+
1393+
std::vector<AllocInfo> allocations;
1394+
1395+
// Buffers of fixed size.
1396+
// Fill 2 blocks. Remove odd buffers. Defragment all of them.
1397+
for(size_t i = 0; i < BLOCK_SIZE / BUF_SIZE * 2; ++i)
1398+
{
1399+
AllocInfo allocInfo;
1400+
CreateBuffer(pool, bufCreateInfo, false, allocInfo);
1401+
allocations.push_back(allocInfo);
1402+
}
1403+
1404+
for(size_t i = 1; i < allocations.size(); ++i)
1405+
{
1406+
DestroyAllocation(allocations[i]);
1407+
allocations.erase(allocations.begin() + i);
1408+
}
1409+
1410+
VmaDefragmentationInfo2 defragInfo = {};
1411+
defragInfo.maxCpuAllocationsToMove = UINT32_MAX;
1412+
defragInfo.maxCpuBytesToMove = VK_WHOLE_SIZE;
1413+
std::vector<VmaAllocation> allocationsToDefrag;
1414+
if(caseIndex == 0)
1415+
{
1416+
defragInfo.poolCount = 1;
1417+
defragInfo.pPools = &pool;
1418+
}
1419+
else
1420+
{
1421+
const size_t allocCount = allocations.size();
1422+
allocationsToDefrag.resize(allocCount);
1423+
std::transform(
1424+
allocations.begin(), allocations.end(),
1425+
allocationsToDefrag.begin(),
1426+
[](const AllocInfo& allocInfo) { return allocInfo.m_Allocation; });
1427+
defragInfo.allocationCount = (uint32_t)allocCount;
1428+
defragInfo.pAllocations = allocationsToDefrag.data();
1429+
}
1430+
1431+
VmaDefragmentationContext defragCtx = VK_NULL_HANDLE;
1432+
VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &defragStats[caseIndex], &defragCtx);
1433+
TEST(res >= VK_SUCCESS);
1434+
vmaDefragmentationEnd(g_hAllocator, defragCtx);
1435+
1436+
TEST(defragStats[caseIndex].allocationsMoved > 0 && defragStats[caseIndex].bytesMoved > 0);
1437+
1438+
ValidateAllocationsData(allocations.data(), allocations.size());
1439+
1440+
DestroyAllAllocations(allocations);
1441+
1442+
vmaDestroyPool(g_hAllocator, pool);
1443+
}
1444+
1445+
TEST(defragStats[0].bytesMoved == defragStats[1].bytesMoved);
1446+
TEST(defragStats[0].allocationsMoved == defragStats[1].allocationsMoved);
1447+
TEST(defragStats[0].bytesFreed == defragStats[1].bytesFreed);
1448+
TEST(defragStats[0].deviceMemoryBlocksFreed == defragStats[1].deviceMemoryBlocksFreed);
1449+
}
1450+
13641451
void TestDefragmentationFull()
13651452
{
13661453
std::vector<AllocInfo> allocations;
@@ -1577,7 +1664,6 @@ static void TestDefragmentationGpu(uint32_t flags)
15771664

15781665
TEST(stats.allocationsMoved > 0 && stats.bytesMoved > 0);
15791666
TEST(stats.deviceMemoryBlocksFreed > 0 && stats.bytesFreed > 0);
1580-
TEST(stats.allocationsLost == 0);
15811667
}
15821668

15831669
ValidateGpuData(allocations.data(), allocations.size());
@@ -4933,17 +5019,18 @@ void Test()
49335019
{
49345020
wprintf(L"TESTING:\n");
49355021

4936-
if(true)
5022+
if(false)
49375023
{
49385024
// # Temporarily insert custom tests here
49395025
// ########################################
49405026
// ########################################
49415027

4942-
TestDefragmentationGpu(0);
4943-
TestDefragmentationGpu(VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT);
4944-
TestDefragmentationGpu(VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT);
4945-
TestDefragmentationSimple();
4946-
TestDefragmentationFull();
5028+
TestDefragmentationWholePool();
5029+
//TestDefragmentationSimple();
5030+
//TestDefragmentationFull();
5031+
//TestDefragmentationGpu(0);
5032+
//TestDefragmentationGpu(VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT);
5033+
//TestDefragmentationGpu(VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT);
49475034
return;
49485035
}
49495036

@@ -4979,6 +5066,7 @@ void Test()
49795066

49805067
TestDefragmentationSimple();
49815068
TestDefragmentationFull();
5069+
TestDefragmentationWholePool();
49825070
TestDefragmentationGpu(0);
49835071
TestDefragmentationGpu(VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT);
49845072
TestDefragmentationGpu(VMA_DEFRAGMENTATION_OPTIMAL_ALGORITHM_BIT);

0 commit comments

Comments
 (0)