From 7e48ef177b668f2ed4c0346fa268e3dbfbb63203 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 25 Jan 2022 18:56:16 +0200 Subject: [PATCH] Refactor allocation index and ID handling in the Vulkan backend MemoryAllocator now always uses indices internally. Incoming IDs are checked for validity. --- source/backends/vulkan/memoryallocator.cpp | 39 +++++++++------------- source/backends/vulkan/memoryallocator.h | 2 -- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/source/backends/vulkan/memoryallocator.cpp b/source/backends/vulkan/memoryallocator.cpp index 4289e28d..fad67873 100644 --- a/source/backends/vulkan/memoryallocator.cpp +++ b/source/backends/vulkan/memoryallocator.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "device.h" #include "error.h" @@ -64,17 +65,7 @@ unsigned MemoryAllocator::allocate(size_t size, unsigned type_bits, MemoryType t alloc.size = size; allocations.push_back(alloc); - return allocations.size(); -} - -MemoryAllocator::Allocation &MemoryAllocator::get_allocation(unsigned id) -{ - return allocations[id-1]; -} - -const MemoryAllocator::Allocation &MemoryAllocator::get_allocation(unsigned id) const -{ - return allocations[id-1]; + return allocations.size()-1; } unsigned MemoryAllocator::allocate(VkBuffer buffer, MemoryType type) @@ -84,11 +75,11 @@ unsigned MemoryAllocator::allocate(VkBuffer buffer, MemoryType type) VkMemoryRequirements requirements; vk.GetBufferMemoryRequirements(buffer, requirements); - unsigned id = allocate(requirements.size, requirements.memoryTypeBits, type); + unsigned index = allocate(requirements.size, requirements.memoryTypeBits, type); - vk.BindBufferMemory(buffer, get_allocation(id).memory, 0); + vk.BindBufferMemory(buffer, allocations[index].memory, 0); - return id; + return index+1; } unsigned MemoryAllocator::allocate(VkImage image, MemoryType type) @@ -98,29 +89,29 @@ unsigned MemoryAllocator::allocate(VkImage image, MemoryType type) VkMemoryRequirements requirements; vk.GetImageMemoryRequirements(image, requirements); - unsigned id = allocate(requirements.size, requirements.memoryTypeBits, type); + unsigned index = allocate(requirements.size, requirements.memoryTypeBits, type); - vk.BindImageMemory(image, get_allocation(id).memory, 0); + vk.BindImageMemory(image, allocations[index].memory, 0); - return id; + return index+1; } void MemoryAllocator::release(unsigned id) { - Allocation &alloc = get_allocation(id); - if(!alloc.memory) - throw invalid_operation("MemoryAllocator::release"); + if(!id || id>allocations.size() || !allocations[id-1].memory) + throw key_error(id); const VulkanFunctions &vk = device.get_functions(); - vk.FreeMemory(alloc.memory); + vk.FreeMemory(allocations[id-1].memory); } void *MemoryAllocator::map(unsigned id, size_t offset, size_t size) { - Allocation &alloc = get_allocation(id); - if(alloc.mapped_address) - throw invalid_operation("MemoryAllocator::map"); + if(!id || id>allocations.size() || !allocations[id-1].memory) + throw key_error(id); + + Allocation &alloc = allocations[id-1]; const VulkanFunctions &vk = device.get_functions(); diff --git a/source/backends/vulkan/memoryallocator.h b/source/backends/vulkan/memoryallocator.h index 4715bea8..fee4726b 100644 --- a/source/backends/vulkan/memoryallocator.h +++ b/source/backends/vulkan/memoryallocator.h @@ -40,8 +40,6 @@ public: private: unsigned find_memory_type_index(unsigned, MemoryType); unsigned allocate(std::size_t, unsigned, MemoryType); - Allocation &get_allocation(unsigned); - const Allocation &get_allocation(unsigned) const; public: unsigned allocate(VkBuffer, MemoryType); -- 2.43.0