Skip to content

Commit 2e4d3ef

Browse files
Added BasicTestAllocatePages() - test for vmaAllocateMemoryPages, vmaFreeMemoryPages.
1 parent d062b78 commit 2e4d3ef

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

src/Tests.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,6 +4213,103 @@ static void BasicTestBuddyAllocator()
42134213
vmaDestroyPool(g_hAllocator, pool);
42144214
}
42154215

4216+
static void BasicTestAllocatePages()
4217+
{
4218+
wprintf(L"Basic test allocate pages\n");
4219+
4220+
RandomNumberGenerator rand{765461};
4221+
4222+
VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
4223+
sampleBufCreateInfo.size = 1024; // Whatever.
4224+
sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
4225+
4226+
VmaAllocationCreateInfo sampleAllocCreateInfo = {};
4227+
sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
4228+
4229+
VmaPoolCreateInfo poolCreateInfo = {};
4230+
VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
4231+
assert(res == VK_SUCCESS);
4232+
4233+
// 1 block of 1 MB.
4234+
poolCreateInfo.blockSize = 1024 * 1024;
4235+
poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
4236+
4237+
// Create pool.
4238+
VmaPool pool = nullptr;
4239+
res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
4240+
assert(res == VK_SUCCESS);
4241+
4242+
// Make 100 allocations of 4 KB - they should fit into the pool.
4243+
VkMemoryRequirements memReq;
4244+
memReq.memoryTypeBits = UINT32_MAX;
4245+
memReq.alignment = 4 * 1024;
4246+
memReq.size = 4 * 1024;
4247+
4248+
VmaAllocationCreateInfo allocCreateInfo = {};
4249+
allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
4250+
allocCreateInfo.pool = pool;
4251+
4252+
constexpr uint32_t allocCount = 100;
4253+
4254+
std::vector<VmaAllocation> alloc{allocCount};
4255+
std::vector<VmaAllocationInfo> allocInfo{allocCount};
4256+
res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), allocInfo.data());
4257+
assert(res == VK_SUCCESS);
4258+
for(uint32_t i = 0; i < allocCount; ++i)
4259+
{
4260+
assert(alloc[i] != VK_NULL_HANDLE &&
4261+
allocInfo[i].pMappedData != nullptr &&
4262+
allocInfo[i].deviceMemory == allocInfo[0].deviceMemory &&
4263+
allocInfo[i].memoryType == allocInfo[0].memoryType);
4264+
}
4265+
4266+
// Free the allocations.
4267+
vmaFreeMemoryPages(g_hAllocator, allocCount, alloc.data());
4268+
std::fill(alloc.begin(), alloc.end(), nullptr);
4269+
std::fill(allocInfo.begin(), allocInfo.end(), VmaAllocationInfo{});
4270+
4271+
// Try to make 100 allocations of 100 KB. This call should fail due to not enough memory.
4272+
// Also test optional allocationInfo = null.
4273+
memReq.size = 100 * 1024;
4274+
res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), nullptr);
4275+
assert(res != VK_SUCCESS);
4276+
assert(std::find_if(alloc.begin(), alloc.end(), [](VmaAllocation alloc){ return alloc != VK_NULL_HANDLE; }) == alloc.end());
4277+
4278+
// Make 100 allocations of 4 KB, but with required alignment of 128 KB. This should also fail.
4279+
memReq.size = 4 * 1024;
4280+
memReq.alignment = 128 * 1024;
4281+
res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), allocInfo.data());
4282+
assert(res != VK_SUCCESS);
4283+
4284+
// Make 100 dedicated allocations of 4 KB.
4285+
memReq.alignment = 4 * 1024;
4286+
memReq.size = 4 * 1024;
4287+
4288+
VmaAllocationCreateInfo dedicatedAllocCreateInfo = {};
4289+
dedicatedAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
4290+
dedicatedAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
4291+
res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &dedicatedAllocCreateInfo, allocCount, alloc.data(), allocInfo.data());
4292+
assert(res == VK_SUCCESS);
4293+
for(uint32_t i = 0; i < allocCount; ++i)
4294+
{
4295+
assert(alloc[i] != VK_NULL_HANDLE &&
4296+
allocInfo[i].pMappedData != nullptr &&
4297+
allocInfo[i].memoryType == allocInfo[0].memoryType &&
4298+
allocInfo[i].offset == 0);
4299+
if(i > 0)
4300+
{
4301+
assert(allocInfo[i].deviceMemory != allocInfo[0].deviceMemory);
4302+
}
4303+
}
4304+
4305+
// Free the allocations.
4306+
vmaFreeMemoryPages(g_hAllocator, allocCount, alloc.data());
4307+
std::fill(alloc.begin(), alloc.end(), nullptr);
4308+
std::fill(allocInfo.begin(), allocInfo.end(), VmaAllocationInfo{});
4309+
4310+
vmaDestroyPool(g_hAllocator, pool);
4311+
}
4312+
42164313
void Test()
42174314
{
42184315
wprintf(L"TESTING:\n");
@@ -4246,6 +4343,7 @@ void Test()
42464343
TestLinearAllocatorMultiBlock();
42474344

42484345
BasicTestBuddyAllocator();
4346+
BasicTestAllocatePages();
42494347

42504348
{
42514349
FILE* file;

0 commit comments

Comments
 (0)