From 866228a37dd644ae89a66db369f80f10d9462ae3 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Mon, 24 Feb 2025 17:44:08 +0100
Subject: [PATCH] [dxvk] Fix memory type selection for non-device local
 property flags

Fixes potential memory allocation issues on unified memory setups
that have unique memory types for different resources.
---
 src/dxvk/dxvk_memory.cpp | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/dxvk/dxvk_memory.cpp b/src/dxvk/dxvk_memory.cpp
index 345b0b4e7..ea2d35594 100644
--- a/src/dxvk/dxvk_memory.cpp
+++ b/src/dxvk/dxvk_memory.cpp
@@ -1911,7 +1911,9 @@ namespace dxvk {
     // flags. This lets us avoid iterating over unsupported memory types
     for (uint32_t i = 0; i < m_memTypesByPropertyFlags.size(); i++) {
       VkMemoryPropertyFlags flags = VkMemoryPropertyFlags(i);
-      uint32_t mask = 0u;
+
+      uint32_t vidmemMask = 0u;
+      uint32_t sysmemMask = 0u;
 
       for (uint32_t j = 0; j < m_memTypeCount; j++) {
         VkMemoryPropertyFlags typeFlags = m_memTypes[j].properties.propertyFlags;
@@ -1919,16 +1921,16 @@ namespace dxvk {
         if ((typeFlags & flags) != flags)
           continue;
 
-        // Do not include device-local memory types if a non-device
-        // local one exists with the same required propery flags.
-        if (mask && !(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
-         && (typeFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
-          continue;
-
-        mask |= 1u << j;
+        if (typeFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
+          vidmemMask |= 1u << j;
+        else
+          sysmemMask |= 1u << j;
       }
 
-      m_memTypesByPropertyFlags[i] = mask;
+      // If a system memory type exists with the given properties, do not
+      // include any device-local memory types. This way we won't ever pick
+      // host-visible vram when explicitly trying to allocate system memory.
+      m_memTypesByPropertyFlags[i] = sysmemMask ? sysmemMask : vidmemMask;
     }
 
     // If there is no cached coherent memory type, reuse the uncached