From 9b6bacff5e868b07f4f9edfc27766b19f7be560e Mon Sep 17 00:00:00 2001 From: Lars Hoerttrich <larshoerttrich@uni-koblenz.de> Date: Sat, 29 May 2021 13:23:14 +0200 Subject: [PATCH] [#31] Changed searchMemoryType()-functions to avoid multiple declarations in ImageManager and BufferManager. Now using the exact function given in the vulkan specs to search for memory types to avoid licensing issues --- src/vkcv/BufferManager.cpp | 37 +++++++++++++++++++------------------ src/vkcv/ImageManager.cpp | 11 +++++++++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/vkcv/BufferManager.cpp b/src/vkcv/BufferManager.cpp index cb2425a5..db314e21 100644 --- a/src/vkcv/BufferManager.cpp +++ b/src/vkcv/BufferManager.cpp @@ -28,28 +28,29 @@ namespace vkcv { } /** - * @brief searches memory type index for buffer allocation, inspired by vulkan tutorial and "https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp" + * @brief searches memory type index for buffer allocation, combines requirements of buffer and application * @param physicalMemoryProperties Memory Properties of physical device - * @param typeBits + * @param typeBits Bit field for suitable memory types * @param requirements Property flags that are required * @return memory type index for Buffer */ - uint32_t searchMemoryType(const vk::PhysicalDeviceMemoryProperties& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) { - uint32_t memoryTypeIndex = 0; - - for (uint32_t i = 0; i < physicalMemoryProperties.memoryTypeCount; i++) - { - if ((typeBits & 1) && - ((physicalMemoryProperties.memoryTypes[i].propertyFlags & requirements) == requirements)) - { - memoryTypeIndex = i; - break; - } - - typeBits >>= 1; + uint32_t searchBufferMemoryType(const vk::PhysicalDeviceMemoryProperties& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) { + const uint32_t memoryCount = physicalMemoryProperties.memoryTypeCount; + for (uint32_t memoryIndex = 0; memoryIndex < memoryCount; ++memoryIndex) { + const uint32_t memoryTypeBits = (1 << memoryIndex); + const bool isRequiredMemoryType = typeBits & memoryTypeBits; + + const vk::MemoryPropertyFlags properties = + physicalMemoryProperties.memoryTypes[memoryIndex].propertyFlags; + const bool hasRequiredProperties = + (properties & requirements) == requirements; + + if (isRequiredMemoryType && hasRequiredProperties) + return static_cast<int32_t>(memoryIndex); } - - return memoryTypeIndex; + + // failed to find memory type + return -1; } uint64_t BufferManager::createBuffer(BufferType type, size_t size, BufferMemoryType memoryType) { @@ -103,7 +104,7 @@ namespace vkcv { break; } - const uint32_t memoryTypeIndex = searchMemoryType( + const uint32_t memoryTypeIndex = searchBufferMemoryType( physicalDevice.getMemoryProperties(), requirements.memoryTypeBits, memoryTypeFlags diff --git a/src/vkcv/ImageManager.cpp b/src/vkcv/ImageManager.cpp index c99f1b8a..a77976f5 100644 --- a/src/vkcv/ImageManager.cpp +++ b/src/vkcv/ImageManager.cpp @@ -8,7 +8,14 @@ namespace vkcv { - uint32_t searchMemoryType(const vk::PhysicalDeviceMemoryProperties& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) { + /** + * @brief searches memory type index for image allocation, combines requirements of image and application + * @param physicalMemoryProperties Memory Properties of physical device + * @param typeBits Bit field for suitable memory types + * @param requirements Property flags that are required + * @return memory type index for image + */ + uint32_t searchImageMemoryType(const vk::PhysicalDeviceMemoryProperties& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) { const uint32_t memoryCount = physicalMemoryProperties.memoryTypeCount; for (uint32_t memoryIndex = 0; memoryIndex < memoryCount; ++memoryIndex) { const uint32_t memoryTypeBits = (1 << memoryIndex); @@ -75,7 +82,7 @@ namespace vkcv { vk::MemoryPropertyFlags memoryTypeFlags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; bool mappable = false; - const uint32_t memoryTypeIndex = searchMemoryType( + const uint32_t memoryTypeIndex = searchImageMemoryType( physicalDevice.getMemoryProperties(), requirements.memoryTypeBits, memoryTypeFlags -- GitLab