From abd07e41582b6d8f7efdedb936cdd7a7fddf9912 Mon Sep 17 00:00:00 2001
From: ameerj <52414509+ameerj@users.noreply.github.com>
Date: Tue, 12 Oct 2021 00:35:01 -0400
Subject: [PATCH] video_core: Refactor resolution scale function

---
 src/common/settings.h                         | 14 +++++++
 .../renderer_opengl/gl_texture_cache.cpp      |  8 +---
 .../renderer_vulkan/vk_texture_cache.cpp      | 19 +++------
 src/video_core/texture_cache/texture_cache.h  | 39 ++++++-------------
 4 files changed, 34 insertions(+), 46 deletions(-)

diff --git a/src/common/settings.h b/src/common/settings.h
index f629c7c561..09f7cdd842 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -72,6 +72,20 @@ struct ResolutionScalingInfo {
     f32 up_factor{1.0f};
     f32 down_factor{1.0f};
     bool active{};
+
+    s32 ScaleUp(s32 value) const {
+        if (value == 0) {
+            return 0;
+        }
+        return std::max((value * static_cast<s32>(up_scale)) >> static_cast<s32>(down_shift), 1);
+    }
+
+    u32 ScaleUp(u32 value) const {
+        if (value == 0U) {
+            return 0U;
+        }
+        return std::max((value * up_scale) >> down_shift, 1U);
+    }
 };
 
 /** The BasicSetting class is a simple resource manager. It defines a label and default value
diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp
index 3dfd13d6a9..ec1afd31a4 100644
--- a/src/video_core/renderer_opengl/gl_texture_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp
@@ -924,12 +924,8 @@ bool Image::Scale() {
     const GLenum filter = linear_color_format ? GL_LINEAR : GL_NEAREST;
 
     const auto& resolution = runtime->resolution;
-    const u32 up = resolution.up_scale;
-    const u32 down = resolution.down_shift;
-    const auto scale = [&](u32 value) { return std::max<u32>((value * up) >> down, 1U); };
-
-    const u32 scaled_width = scale(info.size.width);
-    const u32 scaled_height = is_2d ? scale(info.size.height) : info.size.height;
+    const u32 scaled_width = resolution.ScaleUp(info.size.width);
+    const u32 scaled_height = is_2d ? resolution.ScaleUp(info.size.height) : info.size.height;
     const u32 original_width = info.size.width;
     const u32 original_height = info.size.height;
 
diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp
index 65506f75ea..caefce5fc8 100644
--- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp
@@ -607,16 +607,13 @@ void BlitScale(VKScheduler& scheduler, VkImage src_image, VkImage dst_image, con
     scheduler.RequestOutsideRenderPassOperationContext();
     scheduler.Record([dst_image, src_image, extent, resources, aspect_mask, resolution, type,
                       scaling, vk_filter](vk::CommandBuffer cmdbuf) {
-        const auto scale_up = [&](u32 value) {
-            return std::max<u32>((value * resolution.up_scale) >> resolution.down_shift, 1U);
-        };
         const VkOffset2D src_size{
-            .x = static_cast<s32>(scaling ? extent.width : scale_up(extent.width)),
-            .y = static_cast<s32>(scaling ? extent.height : scale_up(extent.height)),
+            .x = static_cast<s32>(scaling ? extent.width : resolution.ScaleUp(extent.width)),
+            .y = static_cast<s32>(scaling ? extent.height : resolution.ScaleUp(extent.height)),
         };
         const VkOffset2D dst_size{
-            .x = static_cast<s32>(scaling ? scale_up(extent.width) : extent.width),
-            .y = static_cast<s32>(scaling ? scale_up(extent.height) : extent.height),
+            .x = static_cast<s32>(scaling ? resolution.ScaleUp(extent.width) : extent.width),
+            .y = static_cast<s32>(scaling ? resolution.ScaleUp(extent.height) : extent.height),
         };
         boost::container::small_vector<VkImageBlit, 4> regions;
         regions.reserve(resources.levels);
@@ -1144,13 +1141,9 @@ bool Image::ScaleUp() {
         return false;
     }
     if (!scaled_image) {
-        const u32 up = resolution.up_scale;
-        const u32 down = resolution.down_shift;
-        const auto scale = [&](u32 value) { return std::max<u32>((value * up) >> down, 1U); };
-
         const bool is_2d = info.type == ImageType::e2D;
-        const u32 scaled_width = scale(info.size.width);
-        const u32 scaled_height = is_2d ? scale(info.size.height) : info.size.height;
+        const u32 scaled_width = resolution.ScaleUp(info.size.width);
+        const u32 scaled_height = is_2d ? resolution.ScaleUp(info.size.height) : info.size.height;
         auto scaled_info = info;
         scaled_info.size.width = scaled_width;
         scaled_info.size.height = scaled_height;
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index 38895c2e93..c77332b46e 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -504,17 +504,11 @@ void TextureCache<P>::BlitImage(const Tegra::Engines::Fermi2D::Surface& dst,
         is_dst_rescaled = True(dst_image.flags & ImageFlagBits::Rescaled);
     }
     const auto& resolution = Settings::values.resolution_info;
-    const auto scale_up = [&](u32 value) -> u32 {
-        if (value == 0) {
-            return 0U;
-        }
-        return std::max<u32>((value * resolution.up_scale) >> resolution.down_shift, 1U);
-    };
     const auto scale_region = [&](Region2D& region) {
-        region.start.x = scale_up(region.start.x);
-        region.start.y = scale_up(region.start.y);
-        region.end.x = scale_up(region.end.x);
-        region.end.y = scale_up(region.end.y);
+        region.start.x = resolution.ScaleUp(region.start.x);
+        region.start.y = resolution.ScaleUp(region.start.y);
+        region.end.x = resolution.ScaleUp(region.end.x);
+        region.end.y = resolution.ScaleUp(region.end.y);
     };
 
     // TODO: Deduplicate
@@ -1721,20 +1715,14 @@ void TextureCache<P>::CopyImage(ImageId dst_id, ImageId src_id, std::vector<Imag
         ASSERT(True(dst.flags & ImageFlagBits::Rescaled));
         const bool both_2d{src.info.type == ImageType::e2D && dst.info.type == ImageType::e2D};
         const auto& resolution = Settings::values.resolution_info;
-        const auto scale_up = [&](u32 value) -> u32 {
-            if (value == 0) {
-                return 0U;
-            }
-            return std::max<u32>((value * resolution.up_scale) >> resolution.down_shift, 1U);
-        };
         for (auto& copy : copies) {
-            copy.src_offset.x = scale_up(copy.src_offset.x);
-            copy.dst_offset.x = scale_up(copy.dst_offset.x);
-            copy.extent.width = scale_up(copy.extent.width);
+            copy.src_offset.x = resolution.ScaleUp(copy.src_offset.x);
+            copy.dst_offset.x = resolution.ScaleUp(copy.dst_offset.x);
+            copy.extent.width = resolution.ScaleUp(copy.extent.width);
             if (both_2d) {
-                copy.src_offset.y = scale_up(copy.src_offset.y);
-                copy.dst_offset.y = scale_up(copy.dst_offset.y);
-                copy.extent.height = scale_up(copy.extent.height);
+                copy.src_offset.y = resolution.ScaleUp(copy.src_offset.y);
+                copy.dst_offset.y = resolution.ScaleUp(copy.dst_offset.y);
+                copy.extent.height = resolution.ScaleUp(copy.extent.height);
             }
         }
     }
@@ -1812,12 +1800,9 @@ std::pair<FramebufferId, ImageViewId> TextureCache<P>::RenderTargetFromImage(
     Extent3D extent = MipSize(image.info.size, view_info.range.base.level);
     if (is_rescaled) {
         const auto& resolution = Settings::values.resolution_info;
-        const auto scale_up = [&](u32 value) {
-            return std::max<u32>((value * resolution.up_scale) >> resolution.down_shift, 1U);
-        };
-        extent.width = scale_up(extent.width);
+        extent.width = resolution.ScaleUp(extent.width);
         if (image.info.type == ImageType::e2D) {
-            extent.height = scale_up(extent.height);
+            extent.height = resolution.ScaleUp(extent.height);
         }
     }
     const u32 num_samples = image.info.num_samples;