From 44a5aea4ef4225a07fb6392889d9982e8249af8f Mon Sep 17 00:00:00 2001 From: Sidney Just Date: Thu, 3 Mar 2022 17:04:20 -0800 Subject: [PATCH 1/2] Fixed loop termination when ending defragment passes --- include/vk_mem_alloc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index 35e0ae6..009d15f 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -13276,7 +13276,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo for (const FragmentedBlock& block : immovableBlocks) { VmaBlockVector* vector = m_pBlockVectors[block.data]; - for (size_t i = m_ImmovableBlockCount; vector->GetBlockCount(); ++i) + for (size_t i = m_ImmovableBlockCount; i < vector->GetBlockCount(); ++i) { if (vector->GetBlock(i) == block.block) { From 7b9c21f1fe5b3f3394629d1a418d9e4751d468b9 Mon Sep 17 00:00:00 2001 From: Sidney Just Date: Thu, 3 Mar 2022 17:05:05 -0800 Subject: [PATCH 2/2] Fixed defragmenter not acquiring a lock before touching blocks --- include/vk_mem_alloc.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index 009d15f..415bcc5 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -10909,6 +10909,7 @@ public: size_t GetBlockCount() const { return m_Blocks.size(); } // To be used only while the m_Mutex is locked. Used during defragmentation. VmaDeviceMemoryBlock* GetBlock(size_t index) const { return m_Blocks[index]; } + VMA_RW_MUTEX &GetMutex() { return m_Mutex; } VkResult CreateMinBlocks(); void AddStatistics(VmaStatistics& inoutStats); @@ -13064,6 +13065,8 @@ VkResult VmaDefragmentationContext_T::DefragmentPassBegin(VmaDefragmentationPass { if (m_PoolBlockVector != VMA_NULL) { + VmaMutexLockWrite lock(m_PoolBlockVector->GetMutex(), m_PoolBlockVector->GetAllocator()->m_UseMutex); + if (m_PoolBlockVector->GetBlockCount() > 1) ComputeDefragmentation(*m_PoolBlockVector, 0); else if (m_PoolBlockVector->GetBlockCount() == 1) @@ -13075,6 +13078,8 @@ VkResult VmaDefragmentationContext_T::DefragmentPassBegin(VmaDefragmentationPass { if (m_pBlockVectors[i] != VMA_NULL) { + VmaMutexLockWrite lock(m_pBlockVectors[i]->GetMutex(), m_pBlockVectors[i]->GetAllocator()->m_UseMutex); + if (m_pBlockVectors[i]->GetBlockCount() > 1) { if (ComputeDefragmentation(*m_pBlockVectors[i], i))