From fb9329032a276dd1160f9bb12e1cc37837104371 Mon Sep 17 00:00:00 2001
From: Sam Cooper <rukadare@gmail.com>
Date: Wed, 22 Dec 2021 00:42:16 +0000
Subject: [PATCH 1/4] Add deviceUUID filter with DXVK_FILTER_DEVICE_UUID

---
 src/dxvk/dxvk_device_filter.cpp | 21 +++++++++++++++++++++
 src/dxvk/dxvk_device_filter.h   | 15 +++++++++++++--
 src/dxvk/dxvk_instance.cpp      |  2 ++
 3 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/src/dxvk/dxvk_device_filter.cpp b/src/dxvk/dxvk_device_filter.cpp
index 02d8329ee..7fff83d22 100644
--- a/src/dxvk/dxvk_device_filter.cpp
+++ b/src/dxvk/dxvk_device_filter.cpp
@@ -1,13 +1,25 @@
 #include "dxvk_device_filter.h"
 
+std::string convertUUID(const uint8_t* uuid) {
+    std::ostringstream convert;
+    for (int a = 0; a < 16; a++) {
+        convert << static_cast<int>(uuid[a]);
+    }
+    std::string key_string = convert.str();
+    return key_string;
+}
+
 namespace dxvk {
   
   DxvkDeviceFilter::DxvkDeviceFilter(DxvkDeviceFilterFlags flags)
   : m_flags(flags) {
     m_matchDeviceName = env::getEnvVar("DXVK_FILTER_DEVICE_NAME");
+    m_matchDeviceUUID = env::getEnvVar("DXVK_FILTER_DEVICE_UUID");
     
     if (m_matchDeviceName.size() != 0)
       m_flags.set(DxvkDeviceFilterFlag::MatchDeviceName);
+    if (m_matchDeviceUUID.size() != 0)
+      m_flags.set(DxvkDeviceFilterFlag::MatchDeviceUUID);
   }
   
   
@@ -36,5 +48,14 @@ namespace dxvk {
 
     return true;
   }
+
+  bool DxvkDeviceFilter::testCreatedAdapter(const DxvkDeviceInfo& deviceInfo) const {
+    if (m_flags.test(DxvkDeviceFilterFlag::MatchDeviceUUID)) {
+      if (convertUUID(deviceInfo.coreDeviceId.deviceUUID).find(m_matchDeviceUUID) == std::string::npos)
+        return false;
+    }
+
+    return true;
+  }
   
 }
diff --git a/src/dxvk/dxvk_device_filter.h b/src/dxvk/dxvk_device_filter.h
index 7b411e6ad..ffc19ab85 100644
--- a/src/dxvk/dxvk_device_filter.h
+++ b/src/dxvk/dxvk_device_filter.h
@@ -13,7 +13,8 @@ namespace dxvk {
    */
   enum class DxvkDeviceFilterFlag {
     MatchDeviceName   = 0,
-    SkipCpuDevices    = 1,
+    MatchDeviceUUID   = 1,
+    SkipCpuDevices    = 2,
   };
   
   using DxvkDeviceFilterFlags = Flags<DxvkDeviceFilterFlag>;
@@ -42,13 +43,23 @@ namespace dxvk {
      */
     bool testAdapter(
       const VkPhysicalDeviceProperties& properties) const;
+
+    /**
+     * \brief Tests a created adapter
+     *
+     * \param [in] properties Adapter properties
+     * \returns \c true if the test passes
+     */
+    bool testCreatedAdapter(
+      const DxvkDeviceInfo& deviceInfo) const;
     
   private:
     
     DxvkDeviceFilterFlags m_flags;
     
     std::string m_matchDeviceName;
+    std::string m_matchDeviceUUID;
     
   };
   
-}
\ No newline at end of file
+}
diff --git a/src/dxvk/dxvk_instance.cpp b/src/dxvk/dxvk_instance.cpp
index 3723338fe..b46935ad6 100644
--- a/src/dxvk/dxvk_instance.cpp
+++ b/src/dxvk/dxvk_instance.cpp
@@ -178,6 +178,8 @@ namespace dxvk {
     for (uint32_t i = 0; i < numAdapters; i++) {
       if (filter.testAdapter(deviceProperties[i]))
         result.push_back(new DxvkAdapter(m_vki, adapters[i]));
+      if(!filter.testCreatedAdapter(result.back()->devicePropertiesExt()))
+        result.pop_back();
     }
     
     std::stable_sort(result.begin(), result.end(),

From 04fc623b6742f5ba5a6a30998d94264901f061c1 Mon Sep 17 00:00:00 2001
From: Sam Cooper <rukadare@gmail.com>
Date: Sun, 20 Mar 2022 20:07:48 +0000
Subject: [PATCH 2/4] Switch convertUUID to char

---
 src/dxvk/dxvk_device_filter.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/dxvk/dxvk_device_filter.cpp b/src/dxvk/dxvk_device_filter.cpp
index 7fff83d22..bf6779117 100644
--- a/src/dxvk/dxvk_device_filter.cpp
+++ b/src/dxvk/dxvk_device_filter.cpp
@@ -1,11 +1,11 @@
 #include "dxvk_device_filter.h"
 
 std::string convertUUID(const uint8_t* uuid) {
-    std::ostringstream convert;
-    for (int a = 0; a < 16; a++) {
-        convert << static_cast<int>(uuid[a]);
+    std::ostringstream stream;
+    for (unsigned int i = 0; i < VK_UUID_SIZE; i++) {
+        stream << static_cast<char>(uuid[i]);
     }
-    std::string key_string = convert.str();
+    std::string key_string = stream.str();
     return key_string;
 }
 

From 51aad4e2b59e366c9a36ac9c7e63d9cfc37564c3 Mon Sep 17 00:00:00 2001
From: Sam Cooper <rukadare@gmail.com>
Date: Sun, 20 Mar 2022 20:46:23 +0000
Subject: [PATCH 3/4] Simpler convert

---
 src/dxvk/dxvk_device_filter.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/dxvk/dxvk_device_filter.cpp b/src/dxvk/dxvk_device_filter.cpp
index bf6779117..eacd4a9df 100644
--- a/src/dxvk/dxvk_device_filter.cpp
+++ b/src/dxvk/dxvk_device_filter.cpp
@@ -1,12 +1,14 @@
 #include "dxvk_device_filter.h"
 
 std::string convertUUID(const uint8_t* uuid) {
-    std::ostringstream stream;
-    for (unsigned int i = 0; i < VK_UUID_SIZE; i++) {
-        stream << static_cast<char>(uuid[i]);
-    }
-    std::string key_string = stream.str();
-    return key_string;
+  std::string uuidStr{VK_UUID_SIZE};
+
+  for(unsigned int i = 0; i < VK_UUID_SIZE; i++)
+  {
+    uuidStr[i] = uuid[i];
+  }
+
+  return uuidStr;
 }
 
 namespace dxvk {

From 2a6bf03359c5255718630323e0a26b508a199d1c Mon Sep 17 00:00:00 2001
From: Sam Cooper <rukadare@gmail.com>
Date: Sun, 20 Mar 2022 22:13:36 +0000
Subject: [PATCH 4/4] Correct conversion to uint32_t

---
 src/dxvk/dxvk_device_filter.cpp | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/dxvk/dxvk_device_filter.cpp b/src/dxvk/dxvk_device_filter.cpp
index eacd4a9df..cc92b3ea1 100644
--- a/src/dxvk/dxvk_device_filter.cpp
+++ b/src/dxvk/dxvk_device_filter.cpp
@@ -1,14 +1,11 @@
 #include "dxvk_device_filter.h"
 
-std::string convertUUID(const uint8_t* uuid) {
-  std::string uuidStr{VK_UUID_SIZE};
-
-  for(unsigned int i = 0; i < VK_UUID_SIZE; i++)
-  {
-    uuidStr[i] = uuid[i];
+std::string convertUUID(const uint8_t uuid[VK_UUID_SIZE]) {
+  std::ostringstream stream;
+  for (unsigned int i = 0; i < VK_UUID_SIZE; i++) {
+    stream << static_cast<uint32_t>(uuid[i]);
   }
-
-  return uuidStr;
+  return stream.str();
 }
 
 namespace dxvk {