From 70805d0acce71c6388f585c47cede54a0db140f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 16 Nov 2020 23:29:34 +0100 Subject: [PATCH] Vk: remember allocated memory size. Will be used for mapping and in the future possibly for memory slices / views. --- src/Magnum/Vk/Memory.cpp | 10 +++++++--- src/Magnum/Vk/Memory.h | 7 ++++++- src/Magnum/Vk/Test/MemoryVkTest.cpp | 7 ++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Magnum/Vk/Memory.cpp b/src/Magnum/Vk/Memory.cpp index 314ce807e..bc4dcae74 100644 --- a/src/Magnum/Vk/Memory.cpp +++ b/src/Magnum/Vk/Memory.cpp @@ -58,22 +58,24 @@ MemoryAllocateInfo::MemoryAllocateInfo(const VkMemoryAllocateInfo& info): member instead of doing a copy */ _info(info) {} -Memory Memory::wrap(Device& device, const VkDeviceMemory handle, const HandleFlags flags) { +Memory Memory::wrap(Device& device, const VkDeviceMemory handle, const UnsignedLong size, const HandleFlags flags) { Memory out{NoCreate}; out._device = &device; out._handle = handle; out._flags = flags; + out._size = size; return out; } -Memory::Memory(Device& device, const MemoryAllocateInfo& info): _device{&device}, _flags{HandleFlag::DestroyOnDestruction} { +Memory::Memory(Device& device, const MemoryAllocateInfo& info): _device{&device}, _flags{HandleFlag::DestroyOnDestruction}, _size{info->allocationSize} { MAGNUM_VK_INTERNAL_ASSERT_SUCCESS(device->AllocateMemory(device, info, nullptr, &_handle)); } Memory::Memory(NoCreateT): _device{}, _handle{} {} -Memory::Memory(Memory&& other) noexcept: _device{other._device}, _handle{other._handle}, _flags{other._flags} { +Memory::Memory(Memory&& other) noexcept: _device{other._device}, _handle{other._handle}, _flags{other._flags}, _size{other._size} { other._handle = {}; + other._size = {}; } Memory::~Memory() { @@ -86,12 +88,14 @@ Memory& Memory::operator=(Memory&& other) noexcept { swap(other._device, _device); swap(other._handle, _handle); swap(other._flags, _flags); + swap(other._size, _size); return *this; } VkDeviceMemory Memory::release() { const VkDeviceMemory handle = _handle; _handle = {}; + _size = {}; return handle; } diff --git a/src/Magnum/Vk/Memory.h b/src/Magnum/Vk/Memory.h index 6092be9e4..292754d50 100644 --- a/src/Magnum/Vk/Memory.h +++ b/src/Magnum/Vk/Memory.h @@ -245,6 +245,7 @@ class MAGNUM_VK_EXPORT Memory { * @brief Wrap existing Vulkan handle * @param device Vulkan device the memory is allocated on * @param handle The @type_vk{DeviceMemory} handle + * @param size Memory size * @param flags Handle flags * * The @p handle is expected to be originating from @p device. Unlike @@ -253,7 +254,7 @@ class MAGNUM_VK_EXPORT Memory { * behavior. * @see @ref release() */ - static Memory wrap(Device& device, VkDeviceMemory handle, HandleFlags flags = {}); + static Memory wrap(Device& device, VkDeviceMemory handle, UnsignedLong size, HandleFlags flags = {}); /** * @brief Constructor @@ -303,6 +304,9 @@ class MAGNUM_VK_EXPORT Memory { /** @brief Handle flags */ HandleFlags handleFlags() const { return _flags; } + /** @brief Memory allocation size */ + UnsignedLong size() const { return _size; } + /** * @brief Release the underlying Vulkan memory * @@ -319,6 +323,7 @@ class MAGNUM_VK_EXPORT Memory { VkDeviceMemory _handle; HandleFlags _flags; + UnsignedLong _size; }; }} diff --git a/src/Magnum/Vk/Test/MemoryVkTest.cpp b/src/Magnum/Vk/Test/MemoryVkTest.cpp index 12e554873..2ed835235 100644 --- a/src/Magnum/Vk/Test/MemoryVkTest.cpp +++ b/src/Magnum/Vk/Test/MemoryVkTest.cpp @@ -51,6 +51,7 @@ void MemoryVkTest::construct() { Memory memory{device(), MemoryAllocateInfo{1024*1024, device().properties().pickMemory(MemoryFlag::DeviceLocal)}}; CORRADE_VERIFY(memory.handle()); CORRADE_COMPARE(memory.handleFlags(), HandleFlag::DestroyOnDestruction); + CORRADE_COMPARE(memory.size(), 1024*1024); } void MemoryVkTest::constructMove() { @@ -61,6 +62,7 @@ void MemoryVkTest::constructMove() { CORRADE_VERIFY(!a.handle()); CORRADE_COMPARE(b.handle(), handle); CORRADE_COMPARE(b.handleFlags(), HandleFlag::DestroyOnDestruction); + CORRADE_COMPARE(b.size(), 1024*1024); Memory c{NoCreate}; c = std::move(b); @@ -68,6 +70,7 @@ void MemoryVkTest::constructMove() { CORRADE_COMPARE(b.handleFlags(), HandleFlags{}); CORRADE_COMPARE(c.handle(), handle); CORRADE_COMPARE(c.handleFlags(), HandleFlag::DestroyOnDestruction); + CORRADE_COMPARE(c.size(), 1024*1024); CORRADE_VERIFY(std::is_nothrow_move_constructible::value); CORRADE_VERIFY(std::is_nothrow_move_assignable::value); @@ -80,12 +83,14 @@ void MemoryVkTest::wrap() { nullptr, &memory)), Result::Success); CORRADE_VERIFY(memory); - auto wrapped = Memory::wrap(device(), memory, HandleFlag::DestroyOnDestruction); + auto wrapped = Memory::wrap(device(), memory, 1024*1024, HandleFlag::DestroyOnDestruction); CORRADE_COMPARE(wrapped.handle(), memory); + CORRADE_COMPARE(wrapped.size(), 1024*1024); /* Release the handle again, destroy by hand */ CORRADE_COMPARE(wrapped.release(), memory); CORRADE_VERIFY(!wrapped.handle()); + CORRADE_COMPARE(wrapped.size(), 0); device()->FreeMemory(device(), memory, nullptr); }