From b130f648d7c629411c487722f864c6bafcd2562c Mon Sep 17 00:00:00 2001
From: Fernando Sahmkow <fsahmkow27@gmail.com>
Date: Fri, 19 Nov 2021 03:17:54 +0100
Subject: [PATCH] TextureCache: Fix regression caused by ART and improve blit
 detection algorithm to be smarter.

---
 src/video_core/texture_cache/texture_cache.h |  9 +++----
 src/video_core/texture_cache/util.cpp        | 28 +++++++++++++++++---
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index 241f71a91f..5ade3ce55d 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -475,6 +475,7 @@ void TextureCache<P>::BlitImage(const Tegra::Engines::Fermi2D::Surface& dst,
     const BlitImages images = GetBlitImages(dst, src);
     const ImageId dst_id = images.dst_id;
     const ImageId src_id = images.src_id;
+
     PrepareImage(src_id, false, false);
     PrepareImage(dst_id, true, false);
 
@@ -1094,12 +1095,8 @@ typename TextureCache<P>::BlitImages TextureCache<P>::GetBlitImages(
         if (GetFormatType(dst_info.format) != GetFormatType(src_info.format)) {
             continue;
         }
-        if (!dst_id) {
-            dst_id = InsertImage(dst_info, dst_addr, RelaxedOptions{});
-        }
-        if (!src_id) {
-            src_id = InsertImage(src_info, src_addr, RelaxedOptions{});
-        }
+        src_id = FindOrInsertImage(src_info, src_addr);
+        dst_id = FindOrInsertImage(dst_info, dst_addr);
     } while (has_deleted_images);
     return BlitImages{
         .dst_id = dst_id,
diff --git a/src/video_core/texture_cache/util.cpp b/src/video_core/texture_cache/util.cpp
index ddc9fb13a7..8f9eb387ce 100644
--- a/src/video_core/texture_cache/util.cpp
+++ b/src/video_core/texture_cache/util.cpp
@@ -1151,17 +1151,37 @@ bool IsSubresource(const ImageInfo& candidate, const ImageBase& image, GPUVAddr
 
 void DeduceBlitImages(ImageInfo& dst_info, ImageInfo& src_info, const ImageBase* dst,
                       const ImageBase* src) {
-    if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
+    if (src) {
         src_info.format = src->info.format;
+        src_info.num_samples = src->info.num_samples;
+        src_info.size = src->info.size;
     }
-    if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
+    if (dst) {
         dst_info.format = dst->info.format;
+        dst_info.num_samples = dst->info.num_samples;
+        dst_info.size = dst->info.size;
     }
     if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
-        dst_info.format = src->info.format;
+        if (dst) {
+            src_info.format = dst_info.format;
+        } else {
+            dst_info.format = src->info.format;
+        }
     }
     if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
-        src_info.format = dst->info.format;
+        if (src) {
+            if (GetFormatType(src->info.format) == SurfaceType::ColorTexture) {
+                dst_info.format = src->info.format;
+            }
+        } else {
+            src_info.format = dst->info.format;
+        }
+    }
+    if (src_info.num_samples > 1) {
+        dst_info.format = src_info.format;
+    }
+    if (dst_info.num_samples > 1) {
+        src_info.format = dst_info.format;
     }
 }
 
-- 
GitLab