From 57eb936200415ea3178445f7252bb0b0584cd557 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sun, 5 Aug 2018 23:30:18 -0400
Subject: [PATCH 1/2] gl_rasterizer_cache: Avoid superfluous surface copies.

---
 .../renderer_opengl/gl_rasterizer_cache.cpp        | 14 ++++++++++----
 .../renderer_opengl/gl_rasterizer_cache.h          | 11 +++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index c8f0c4e28..257aa9571 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -46,6 +46,8 @@ struct FormatTuple {
     params.height = Common::AlignUp(config.tic.Height(), GetCompressionFactor(params.pixel_format));
     params.unaligned_height = config.tic.Height();
     params.size_in_bytes = params.SizeInBytes();
+    params.cache_width = Common::AlignUp(params.width, 16);
+    params.cache_height = Common::AlignUp(params.height, 16);
     return params;
 }
 
@@ -63,6 +65,8 @@ struct FormatTuple {
     params.height = config.height;
     params.unaligned_height = config.height;
     params.size_in_bytes = params.SizeInBytes();
+    params.cache_width = Common::AlignUp(params.width, 16);
+    params.cache_height = Common::AlignUp(params.height, 16);
     return params;
 }
 
@@ -82,6 +86,8 @@ struct FormatTuple {
     params.height = zeta_height;
     params.unaligned_height = zeta_height;
     params.size_in_bytes = params.SizeInBytes();
+    params.cache_width = Common::AlignUp(params.width, 16);
+    params.cache_height = Common::AlignUp(params.height, 16);
     return params;
 }
 
@@ -680,12 +686,12 @@ Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params) {
             // If use_accurate_framebuffers is enabled, always load from memory
             FlushSurface(surface);
             UnregisterSurface(surface);
-        } else if (surface->GetSurfaceParams() != params) {
-            // If surface parameters changed, recreate the surface from the old one
-            return RecreateSurface(surface, params);
-        } else {
+        } else if (surface->GetSurfaceParams().IsCompatibleSurface(params)) {
             // Use the cached surface as-is
             return surface;
+        } else {
+            // If surface parameters changed, recreate the surface from the old one
+            return RecreateSurface(surface, params);
         }
     }
 
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index 4e1e18d9c..39fcf22b4 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -9,6 +9,7 @@
 #include <memory>
 #include <vector>
 #include <boost/icl/interval_map.hpp>
+
 #include "common/common_types.h"
 #include "common/math_util.h"
 #include "video_core/engines/maxwell_3d.h"
@@ -546,6 +547,12 @@ struct SurfaceParams {
         return !operator==(other);
     }
 
+    /// Checks if surfaces are compatible for caching
+    bool IsCompatibleSurface(const SurfaceParams& other) const {
+        return std::tie(pixel_format, type, cache_width, cache_height) ==
+               std::tie(other.pixel_format, other.type, other.cache_width, other.cache_height);
+    }
+
     Tegra::GPUVAddr addr;
     bool is_tiled;
     u32 block_height;
@@ -556,6 +563,10 @@ struct SurfaceParams {
     u32 height;
     u32 unaligned_height;
     size_t size_in_bytes;
+
+    // Parameters used for caching only
+    u32 cache_width;
+    u32 cache_height;
 };
 
 class CachedSurface final {

From 904d7eaa94a4383e636efbf357807a26ad490a56 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sun, 5 Aug 2018 23:57:19 -0400
Subject: [PATCH 2/2] maxwell_3d: Remove outdated assert.

---
 src/video_core/engines/maxwell_3d.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index a235b543e..5c0ae8009 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -285,8 +285,6 @@ Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
 
     // TODO(Subv): Different data types for separate components are not supported
     ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
-    // TODO(Subv): Only UNORM formats are supported for now.
-    ASSERT(r_type == Texture::ComponentType::UNORM);
 
     return tic_entry;
 }