From eb7b6d217d2fad01ea89e6d09c361291fd86481e Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Tue, 27 Feb 2024 20:44:15 +1000
Subject: [PATCH] System: Ensure GPU is flushed before invalidating on pause

---
 src/core/system.cpp | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/core/system.cpp b/src/core/system.cpp
index cf883508e..bed6219e5 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -1110,6 +1110,9 @@ void System::PauseSystem(bool paused)
 
   if (paused)
   {
+    // Make sure the GPU is flushed, otherwise the VB might still be mapped.
+    g_gpu->FlushRender();
+
     FullscreenUI::OnSystemPaused();
 
     InputManager::PauseVibration();
@@ -1187,7 +1190,10 @@ bool System::LoadState(const char* filename)
 
   ResetPerformanceCounters();
   ResetThrottler();
-  InvalidateDisplay();
+
+  if (IsPaused())
+    InvalidateDisplay();
+
   Log_VerbosePrintf("Loading state took %.2f msec", load_timer.GetTimeMilliseconds());
   return true;
 }
@@ -1974,7 +1980,6 @@ void System::RecreateSystem()
 
   ResetPerformanceCounters();
   ResetThrottler();
-  InvalidateDisplay();
 
   if (was_paused)
     PauseSystem(true);
@@ -3662,7 +3667,8 @@ void System::CheckForSettingsChanges(const Settings& old_settings)
         g_settings.runahead_frames != old_settings.runahead_frames)
     {
       g_gpu->UpdateSettings(old_settings);
-      InvalidateDisplay();
+      if (!IsPaused())
+        InvalidateDisplay();
     }
 
     if (g_settings.gpu_widescreen_hack != old_settings.gpu_widescreen_hack ||