From 2768c2c91de37bda83940a170cf9bed0f4a4ab09 Mon Sep 17 00:00:00 2001
From: Connor McLaughlin <stenzek@gmail.com>
Date: Sat, 22 Aug 2020 18:16:35 +1000
Subject: [PATCH] System: Fix VRAM sometimes getting lost when loading state

---
 src/core/system.cpp | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/core/system.cpp b/src/core/system.cpp
index 67a316f64..50ab3d24d 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -571,7 +571,10 @@ bool DoState(StateWrapper& sw)
   if (!sw.DoMarker("InterruptController") || !g_interrupt_controller.DoState(sw))
     return false;
 
-  if (!sw.DoMarker("GPU") || !g_gpu->DoState(sw))
+  g_gpu->RestoreGraphicsAPIState();
+  const bool gpu_result = sw.DoMarker("GPU") && g_gpu->DoState(sw);
+  g_gpu->ResetGraphicsAPIState();
+  if (!gpu_result)
     return false;
 
   if (!sw.DoMarker("CDROM") || !g_cdrom.DoState(sw))
@@ -630,13 +633,7 @@ bool LoadState(ByteStream* state)
   if (IsShutdown())
     return false;
 
-  g_gpu->RestoreGraphicsAPIState();
-
-  const bool result = DoLoadState(state, false);
-
-  g_gpu->ResetGraphicsAPIState();
-
-  return result;
+  return DoLoadState(state, false);
 }
 
 bool DoLoadState(ByteStream* state, bool force_software_renderer)