From ba28bdafd3b74676bc8bbc53952658e6bcfbadb4 Mon Sep 17 00:00:00 2001 From: Squareys Date: Fri, 22 Jul 2016 13:55:33 +0200 Subject: [PATCH] Vk: Improve PhysicalDevice::getMemoryType(...) and doc++ Signed-off-by: Squareys --- src/Magnum/Vk/PhysicalDevice.cpp | 17 ++++++++++------- src/Magnum/Vk/PhysicalDevice.h | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Magnum/Vk/PhysicalDevice.cpp b/src/Magnum/Vk/PhysicalDevice.cpp index 8fce6acfd..ee139373c 100644 --- a/src/Magnum/Vk/PhysicalDevice.cpp +++ b/src/Magnum/Vk/PhysicalDevice.cpp @@ -82,18 +82,21 @@ UnsignedInt PhysicalDevice::getQueueFamilyIndex(QueueFamily family) { CORRADE_ASSERT(false, "The device does not support the given queue family.", -1); // TODO } -UnsignedInt PhysicalDevice::getMemoryType(UnsignedInt typeBits, VkFlags properties) { +UnsignedInt PhysicalDevice::getMemoryType(UnsignedInt supportedTypeBits, MemoryProperties properties) { if(_deviceMemoryProperties.memoryHeapCount == 0 && _deviceMemoryProperties.memoryTypeCount == 0) { + /* first query for physical device memory properties */ vkGetPhysicalDeviceMemoryProperties(_physicalDevice, &_deviceMemoryProperties); } - for (uint32_t i = 0; i < 32; i++) { - if((typeBits & 1) != 0) { - if((_deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties) { - return i; - } + const UnsignedInt p = UnsignedInt(properties); + for (UnsignedInt i = 0; i < 32; ++i) { + /* check whether the memory type at index i is supported */ + const UnsignedInt memoryTypeSupported = (supportedTypeBits >> i) & 1; + if(memoryTypeSupported != 0 && + (_deviceMemoryProperties.memoryTypes[i].propertyFlags & p) == p) { + /* memory type and properties match */ + return i; } - typeBits >>= 1; } CORRADE_ASSERT(false, "Physical devices does not support memory with given properties.", -1); // TODO diff --git a/src/Magnum/Vk/PhysicalDevice.h b/src/Magnum/Vk/PhysicalDevice.h index a9cb8911b..a1907a8eb 100644 --- a/src/Magnum/Vk/PhysicalDevice.h +++ b/src/Magnum/Vk/PhysicalDevice.h @@ -33,11 +33,24 @@ #include "Magnum/Magnum.h" #include "Magnum/Vk/visibility.h" +#include + #include "vulkan.h" namespace Magnum { namespace Vk { +enum class MemoryProperty: UnsignedInt { + DeviceLocal = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + HostVisible = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + Coherent = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + HostCached = VK_MEMORY_PROPERTY_HOST_CACHED_BIT, + LazilyAllocated = VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, +}; + +typedef Corrade::Containers::EnumSet MemoryProperties; +CORRADE_ENUMSET_OPERATORS(MemoryProperties) + enum class QueueFamily: UnsignedInt { Graphics = VK_QUEUE_GRAPHICS_BIT, Compute = VK_QUEUE_COMPUTE_BIT, @@ -106,7 +119,15 @@ class MAGNUM_VK_EXPORT PhysicalDevice { return deviceProperties; } - UnsignedInt getMemoryType(UnsignedInt typeBits, VkFlags properties); + /** + * @brief Get the index of the memory type with properties + * @param supportedTypeBits A bit set with a bit for every memory type index. A + * bit at index i should be set if the memory type at index i + * is supported by the resource for which to get the memory type. + * @param properties Properties required by the memory type + * @return Index of the memory type for this device + */ + UnsignedInt getMemoryType(UnsignedInt supportedTypeBits, MemoryProperties properties); private: VkPhysicalDevice _physicalDevice;