From b8468fda43ddd8044d5838c0c8e4379371b94897 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Wed, 6 Jun 2018 23:10:38 +0200
Subject: [PATCH] [dxvk] Improve swap chain format fallback logic

When requesting an unsupported non-SRGB format, we shouldn't
fall back to an SRGB format since that will cause Gamma issues.
---
 src/dxvk/dxvk_surface.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/dxvk/dxvk_surface.cpp b/src/dxvk/dxvk_surface.cpp
index 630ee6fa..20dc456b 100644
--- a/src/dxvk/dxvk_surface.cpp
+++ b/src/dxvk/dxvk_surface.cpp
@@ -1,4 +1,5 @@
 #include "dxvk_surface.h"
+#include "dxvk_format.h"
 
 #include "../util/util_math.h"
 
@@ -50,6 +51,18 @@ namespace dxvk {
             return fmt;
         }
       }
+
+      // If that didn't work, we'll fall back to a format
+      // which has similar properties to the preferred one
+      DxvkFormatFlags prefFlags = imageFormatInfo(preferred[0].format)->flags;
+
+      for (auto fmt : m_surfaceFormats) {
+        auto currFlags = imageFormatInfo(fmt.format)->flags;
+
+        if ((currFlags & DxvkFormatFlag::ColorSpaceSrgb)
+         == (prefFlags & DxvkFormatFlag::ColorSpaceSrgb))
+          return fmt;
+      }
     }
     
     // Otherwise, fall back to the first format