Refactor Vulkan stream buffer memory type selection (#238)

* Refactor Vulkan stream buffer memory type selection

This is a fix for GPUs with Vulkan V1.1 and V1.2

* add requested refactoring

* clang format

* fix typo
This commit is contained in:
Jugurta 2024-08-25 09:27:04 +01:00 committed by GitHub
parent 9c1f778e79
commit 2f0b2f9839
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,19 +47,41 @@ vk::MemoryPropertyFlags MakePropertyFlags(BufferType type) {
/// Get the preferred host visible memory type.
u32 GetMemoryType(const vk::PhysicalDeviceMemoryProperties& properties, BufferType type) {
vk::MemoryPropertyFlags flags = MakePropertyFlags(type);
std::optional preferred_type = FindMemoryType(properties, flags);
std::optional<u32> preferred_type;
// Try to find a memory type with all the requested flags
preferred_type = FindMemoryType(properties, flags);
if (preferred_type) {
return *preferred_type;
}
// If not found, try removing flags one by one
constexpr std::array remove_flags = {
vk::MemoryPropertyFlagBits::eDeviceLocal,
vk::MemoryPropertyFlagBits::eHostCached,
vk::MemoryPropertyFlagBits::eHostCoherent,
};
for (u32 i = 0; i < remove_flags.size() && !preferred_type; i++) {
flags &= ~remove_flags[i];
preferred_type = FindMemoryType(properties, flags);
for (auto remove_flag : remove_flags) {
if ((flags & remove_flag) == vk::MemoryPropertyFlags{}) {
continue;
}
ASSERT_MSG(preferred_type, "No suitable memory type found");
return preferred_type.value();
flags &= ~remove_flag;
preferred_type = FindMemoryType(properties, flags);
if (preferred_type) {
return *preferred_type;
}
}
// If still not found, try with only eHostVisible flag
preferred_type = FindMemoryType(properties, vk::MemoryPropertyFlagBits::eHostVisible);
if (preferred_type) {
return *preferred_type;
}
// If we reach here, we couldn't find any suitable memory type
UNREACHABLE_MSG("Failed to find a suitable memory type for buffer type {}",
BufferTypeName(type));
}
constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000;