From f2b9be9bd3837f4bc55cc92d82b8c567dfd98c93 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Wed, 14 Dec 2016 23:44:19 -0800
Subject: [PATCH] Memory: Always flush whole pages from surface cache

This prevents individual writes touching a cached page, but which don't
overlap the surface, from constantly hitting the surface cache lookup.
---
 src/core/memory.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 65e4bba85..d058dc844 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -357,14 +357,24 @@ void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) {
     }
 }
 
+static void RoundToPages(PAddr& start, u32& size) {
+    PAddr start_rounded_down = start & ~PAGE_MASK;
+    PAddr end_rounded_up = ((start + size) + PAGE_MASK) & ~PAGE_MASK;
+
+    start = start_rounded_down;
+    size = end_rounded_up - start_rounded_down;
+}
+
 void RasterizerFlushRegion(PAddr start, u32 size) {
     if (VideoCore::g_renderer != nullptr) {
+        RoundToPages(start, size);
         VideoCore::g_renderer->Rasterizer()->FlushRegion(start, size);
     }
 }
 
 void RasterizerFlushAndInvalidateRegion(PAddr start, u32 size) {
     if (VideoCore::g_renderer != nullptr) {
+        RoundToPages(start, size);
         VideoCore::g_renderer->Rasterizer()->FlushAndInvalidateRegion(start, size);
     }
 }