Browse Source

Vk: remember allocated memory size.

Will be used for mapping and in the future possibly for memory slices /
views.
pull/234/head
Vladimír Vondruš 6 years ago
parent
commit
70805d0acc
  1. 10
      src/Magnum/Vk/Memory.cpp
  2. 7
      src/Magnum/Vk/Memory.h
  3. 7
      src/Magnum/Vk/Test/MemoryVkTest.cpp

10
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;
}

7
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;
};
}}

7
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<Memory>::value);
CORRADE_VERIFY(std::is_nothrow_move_assignable<Memory>::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);
}

Loading…
Cancel
Save