From ca1e01ad05bdf25117dea6a93ea43ef0991b0cd3 Mon Sep 17 00:00:00 2001 From: Jugurta <74503778+i0x404@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:24:49 +0100 Subject: [PATCH 1/4] Refactor Vulkan stream buffer memory type selection This is a fix for GPUs with Vulkan V1.1 and V1.2 --- .../renderer_vulkan/vk_stream_buffer.cpp | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp index 2a5bf7b0a2..468caf9340 100644 --- a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp +++ b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp @@ -33,11 +33,9 @@ vk::MemoryPropertyFlags MakePropertyFlags(BufferType type) { case BufferType::Upload: return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; case BufferType::Download: - return vk::MemoryPropertyFlagBits::eHostVisible | - vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached; + return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached; case BufferType::Stream: - return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | - vk::MemoryPropertyFlagBits::eHostCoherent; + return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; default: UNREACHABLE_MSG("Unknown buffer type {}", type); return vk::MemoryPropertyFlagBits::eHostVisible; @@ -47,19 +45,42 @@ 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 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]; + for (auto remove_flag : remove_flags) { + if ((flags & remove_flag) == vk::MemoryPropertyFlags{}) { + continue; + } + flags &= ~remove_flag; preferred_type = FindMemoryType(properties, flags); + if (preferred_type) { + return *preferred_type; + } } - ASSERT_MSG(preferred_type, "No suitable memory type found"); - return preferred_type.value(); + + // 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 + LOG_CRITICAL(Render_Vulkan, "Failed to find a suitable memory type for buffer type {}", + BufferTypeName(type)); + return 0; // Return 0 as a fallback, though this will likely cause issues } constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000; From 8f25ac55fbea483ce40383e9fa561e2839fdc54e Mon Sep 17 00:00:00 2001 From: Jugurta <74503778+i0x404@users.noreply.github.com> Date: Thu, 22 Aug 2024 12:57:17 +0000 Subject: [PATCH 2/4] add requested refactoring --- src/video_core/renderer_vulkan/vk_stream_buffer.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp index 468caf9340..735fc34b97 100644 --- a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp +++ b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp @@ -33,9 +33,11 @@ vk::MemoryPropertyFlags MakePropertyFlags(BufferType type) { case BufferType::Upload: return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; case BufferType::Download: - return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached; + return vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached; case BufferType::Stream: - return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; + return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent; default: UNREACHABLE_MSG("Unknown buffer type {}", type); return vk::MemoryPropertyFlagBits::eHostVisible; @@ -78,9 +80,7 @@ u32 GetMemoryType(const vk::PhysicalDeviceMemoryProperties& properties, BufferTy } // If we reach here, we couldn't find any suitable memory type - LOG_CRITICAL(Render_Vulkan, "Failed to find a suitable memory type for buffer type {}", - BufferTypeName(type)); - return 0; // Return 0 as a fallback, though this will likely cause issues + UNREACHABLE_MSG("Failed to find a suitable memory type for buffer type {}", BufferTypeName(type)); } constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000; From e029a820f81111d08313aa9886324cf716db58d3 Mon Sep 17 00:00:00 2001 From: Jugurta <74503778+i0x404@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:57:38 +0000 Subject: [PATCH 3/4] clang format --- src/video_core/renderer_vulkan/vk_stream_buffer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp index 735fc34b97..c28be1ff24 100644 --- a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp +++ b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp @@ -80,7 +80,8 @@ u32 GetMemoryType(const vk::PhysicalDeviceMemoryProperties& properties, BufferTy } // 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)); + UNREACHABLE_MSG("Failed to find a suitable memory type for buffer type {}", ++ BufferTypeName(type)); } constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000; From 05e657414a1752dff788dd195625ad76c6256fcc Mon Sep 17 00:00:00 2001 From: Jugurta <74503778+i0x404@users.noreply.github.com> Date: Fri, 23 Aug 2024 19:12:55 +0100 Subject: [PATCH 4/4] fix typo --- src/video_core/renderer_vulkan/vk_stream_buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp index c28be1ff24..a12ab53e18 100644 --- a/src/video_core/renderer_vulkan/vk_stream_buffer.cpp +++ b/src/video_core/renderer_vulkan/vk_stream_buffer.cpp @@ -81,7 +81,7 @@ u32 GetMemoryType(const vk::PhysicalDeviceMemoryProperties& properties, BufferTy // 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)); + BufferTypeName(type)); } constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000;