#include <msp/core/algorithm.h>
+#include <msp/core/maputils.h>
#include <msp/graphics/vulkancontext_platform.h>
#include "device.h"
#include "error.h"
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)
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)
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();