]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor Vulkan memory mapping functions
authorMikko Rasa <tdb@tdb.fi>
Tue, 25 Jan 2022 17:16:57 +0000 (19:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 25 Jan 2022 17:36:31 +0000 (19:36 +0200)
An allocation is now always mapped in its entirety.  Unmapping is done
by ID instead of mapped address.

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

index 750c926bf2e90c0ff5feacd7a299febaa3a453ba..c3dcb8e12c5ccbb1fb21d9abc0b76862a65cad16 100644 (file)
@@ -70,14 +70,13 @@ bool VulkanBuffer::can_map() const
 
 void *VulkanBuffer::map()
 {
-       size_t size = static_cast<const Buffer *>(this)->size;
-       mapped_address = device.get_allocator().map(memory_id, 0, size);
+       mapped_address = device.get_allocator().map(memory_id);
        return mapped_address;
 }
 
 bool VulkanBuffer::unmap()
 {
-       device.get_allocator().unmap(mapped_address);
+       device.get_allocator().unmap(memory_id);
        mapped_address = 0;
        return true;
 }
index fad6787307fa955fb15d5a9ac5d6bae2974f44a0..b15344c7aeb39280a2f9c5d2e26ef765314687e0 100644 (file)
@@ -106,7 +106,7 @@ void MemoryAllocator::release(unsigned id)
        vk.FreeMemory(allocations[id-1].memory);
 }
 
-void *MemoryAllocator::map(unsigned id, size_t offset, size_t size)
+void *MemoryAllocator::map(unsigned id)
 {
        if(!id || id>allocations.size() || !allocations[id-1].memory)
                throw key_error(id);
@@ -115,21 +115,24 @@ void *MemoryAllocator::map(unsigned id, size_t offset, size_t size)
 
        const VulkanFunctions &vk = device.get_functions();
 
-       vk.MapMemory(alloc.memory, offset, size, 0, &alloc.mapped_address);
+       vk.MapMemory(alloc.memory, 0, alloc.size, 0, &alloc.mapped_address);
 
        return alloc.mapped_address;
 }
 
-void MemoryAllocator::unmap(void *ptr)
+void MemoryAllocator::unmap(unsigned id)
 {
-       auto i = find_member(allocations, ptr, &Allocation::mapped_address);
-       if(i==allocations.end())
+       if(!id || id>allocations.size() || !allocations[id-1].memory)
+               throw key_error(id);
+
+       Allocation &alloc = allocations[id-1];
+       if(!alloc.mapped_address)
                throw invalid_operation("MemoryAllocator::unmap");
 
        const VulkanFunctions &vk = device.get_functions();
 
-       vk.UnmapMemory(i->memory);
-       i->mapped_address = 0;
+       vk.UnmapMemory(alloc.memory);
+       alloc.mapped_address = 0;
 }
 
 } // namespace GL
index fee4726be95f9fde3b50471aad720511642695f8..230f21a202126d176034f576981426eddab9f281 100644 (file)
@@ -46,8 +46,8 @@ public:
        unsigned allocate(VkImage, MemoryType);
        void release(unsigned);
 
-       void *map(unsigned, std::size_t, std::size_t);
-       void unmap(void *);
+       void *map(unsigned);
+       void unmap(unsigned);
 };
 
 } // namespace GL
index 9fc941401ed90ef954b4a156ae7b1b801baf149f..4ad50d860f126952ad4024c6d9208cd05149f055 100644 (file)
@@ -126,7 +126,7 @@ TransferQueue::StagingBuffer::StagingBuffer(Device &d, size_t s):
 
        MemoryAllocator &allocator = device.get_allocator();
        memory_id = allocator.allocate(buffer, STAGING_MEMORY);
-       mapped_address = allocator.map(memory_id, 0, size);
+       mapped_address = allocator.map(memory_id);
 }
 
 TransferQueue::StagingBuffer::StagingBuffer(StagingBuffer &&other):
@@ -149,7 +149,7 @@ TransferQueue::StagingBuffer::~StagingBuffer()
 
        if(mapped_address)
        {
-               allocator.unmap(mapped_address);
+               allocator.unmap(memory_id);
                allocator.release(memory_id);
        }
        if(buffer)