]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor allocation index and ID handling in the Vulkan backend
authorMikko Rasa <tdb@tdb.fi>
Tue, 25 Jan 2022 16:56:16 +0000 (18:56 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 25 Jan 2022 17:33:13 +0000 (19:33 +0200)
MemoryAllocator now always uses indices internally.  Incoming IDs are
checked for validity.

source/backends/vulkan/memoryallocator.cpp
source/backends/vulkan/memoryallocator.h

index 4289e28d6a64a20b39d6dc87ba161ad6749ba4bc..fad6787307fa955fb15d5a9ac5d6bae2974f44a0 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/algorithm.h>
+#include <msp/core/maputils.h>
 #include <msp/graphics/vulkancontext_platform.h>
 #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();
 
index 4715bea8311a2f88db74919f24af71005b72a18c..fee4726be95f9fde3b50471aad720511642695f8 100644 (file)
@@ -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);