From a420c3d752c4e2e40d4ed2c8ed3139c45c001acf Mon Sep 17 00:00:00 2001 From: Adam Sawicki Date: Tue, 23 Feb 2021 18:21:42 +0100 Subject: [PATCH] Fixed budget management in VmaBlockVector::Allocate for cases when some of multi-page allocations fail --- src/SparseBindingTest.cpp | 8 ++++---- src/vk_mem_alloc.h | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/SparseBindingTest.cpp b/src/SparseBindingTest.cpp index 4299db1..ce4d2db 100644 --- a/src/SparseBindingTest.cpp +++ b/src/SparseBindingTest.cpp @@ -547,7 +547,7 @@ void TestSparseBinding() RandomNumberGenerator rand(4652467); - for(uint32_t i = 0; i < frameCount; ++i) + for(uint32_t frameIndex = 0; frameIndex < frameCount; ++frameIndex) { // Bump frame index. ++g_FrameIndex; @@ -562,11 +562,11 @@ void TestSparseBinding() images.push_back(std::move(imageInfo)); // Delete all images that expired. - for(size_t i = images.size(); i--; ) + for(size_t imageIndex = images.size(); imageIndex--; ) { - if(g_FrameIndex >= images[i].endFrame) + if(g_FrameIndex >= images[imageIndex].endFrame) { - images.erase(images.begin() + i); + images.erase(images.begin() + imageIndex); } } } diff --git a/src/vk_mem_alloc.h b/src/vk_mem_alloc.h index 3a7ae8d..39d7034 100644 --- a/src/vk_mem_alloc.h +++ b/src/vk_mem_alloc.h @@ -12773,9 +12773,13 @@ VkResult VmaBlockVector::Allocate( if(res != VK_SUCCESS) { // Free all already created allocations. + const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex); while(allocIndex--) { - Free(pAllocations[allocIndex]); + VmaAllocation_T* const alloc = pAllocations[allocIndex]; + const VkDeviceSize allocSize = alloc->GetSize(); + Free(alloc); + m_hAllocator->m_Budget.RemoveAllocation(heapIndex, allocSize); } memset(pAllocations, 0, sizeof(VmaAllocation) * allocationCount); }