From bd7c4315328c7eb54e7b52e294aba9543c5ad1c4 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 24 Mar 2016 23:30:01 -0700 Subject: [PATCH] Vulkan: Increase new size for new slabs. --- Common/Vulkan/VulkanMemory.cpp | 20 +++++++++++++++----- Common/Vulkan/VulkanMemory.h | 5 +++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Common/Vulkan/VulkanMemory.cpp b/Common/Vulkan/VulkanMemory.cpp index 9eea7cd13f..cf7bbb2868 100644 --- a/Common/Vulkan/VulkanMemory.cpp +++ b/Common/Vulkan/VulkanMemory.cpp @@ -101,8 +101,8 @@ void VulkanPushBuffer::Defragment(VulkanContext *vulkan) { assert(res); } -VulkanDeviceAllocator::VulkanDeviceAllocator(VulkanContext *vulkan, size_t minSlabSize) - : vulkan_(vulkan), minSlabSize_(minSlabSize), memoryTypeIndex_(UNDEFINED_MEMORY_TYPE) { +VulkanDeviceAllocator::VulkanDeviceAllocator(VulkanContext *vulkan, size_t minSlabSize, size_t maxSlabSize) + : vulkan_(vulkan), minSlabSize_(minSlabSize), maxSlabSize_(maxSlabSize), memoryTypeIndex_(UNDEFINED_MEMORY_TYPE) { assert((minSlabSize_ & (SLAB_GRAIN_SIZE - 1)) == 0); } @@ -268,6 +268,11 @@ void VulkanDeviceAllocator::ExecuteFree(FreeInfo *userdata) { } bool VulkanDeviceAllocator::AllocateSlab(size_t minBytes) { + if (!slabs_.empty() && minSlabSize_ < maxSlabSize_) { + // We're allocating an additional slab, so rachet up its size. + minSlabSize_ <<= 1; + } + VkMemoryAllocateInfo alloc = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO }; alloc.allocationSize = minSlabSize_; alloc.memoryTypeIndex = memoryTypeIndex_; @@ -297,7 +302,11 @@ void VulkanDeviceAllocator::Decimate() { bool foundFree = false; for (size_t i = 0; i < slabs_.size(); ++i) { - if (!slabs_[i].allocSizes.empty()) { + // Go backwards. This way, we keep the largest free slab. + // We do this here (instead of the for) since size_t is unsigned. + size_t index = slabs_.size() - i - 1; + + if (!slabs_[index].allocSizes.empty()) { continue; } @@ -308,8 +317,9 @@ void VulkanDeviceAllocator::Decimate() { } // Okay, let's free this one up. - vulkan_->Delete().QueueDeleteDeviceMemory(slabs_[i].deviceMemory); - slabs_.erase(slabs_.begin() + i); + vulkan_->Delete().QueueDeleteDeviceMemory(slabs_[index].deviceMemory); + slabs_.erase(slabs_.begin() + index); + // Let's check the next one, which is now in this same slot. --i; } diff --git a/Common/Vulkan/VulkanMemory.h b/Common/Vulkan/VulkanMemory.h index 9956414fa9..ea0d504c61 100644 --- a/Common/Vulkan/VulkanMemory.h +++ b/Common/Vulkan/VulkanMemory.h @@ -130,7 +130,7 @@ private: class VulkanDeviceAllocator { public: - VulkanDeviceAllocator(VulkanContext *vulkan, size_t minSlabSize); + VulkanDeviceAllocator(VulkanContext *vulkan, size_t minSlabSize, size_t maxSlabSize); ~VulkanDeviceAllocator(); void Destroy(); @@ -183,8 +183,9 @@ private: void Decimate(); void ExecuteFree(FreeInfo *userdata); - VulkanContext *vulkan_; + VulkanContext *const vulkan_; std::vector slabs_; size_t minSlabSize_; + const size_t maxSlabSize_; uint32_t memoryTypeIndex_; };