From e9882a10aa0751726cdc65e00e7eb25dd57fc6a4 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin <stenzek@gmail.com> Date: Sat, 1 Aug 2020 16:45:06 +1000 Subject: [PATCH] System: Move restore/reset graphics API state to System Fixes frame stepping. --- .../app/src/cpp/android_host_interface.cpp | 12 +++---- src/core/system.cpp | 35 +++++++++++++++---- .../libretro_host_interface.cpp | 4 --- src/duckstation-qt/qthostinterface.cpp | 4 --- src/duckstation-sdl/sdl_host_interface.cpp | 4 --- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index 7a54fe092..e54721e53 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -249,16 +249,16 @@ void AndroidHostInterface::EmulationThreadEntryPoint(ANativeWindow* initial_surf { DrawImGuiWindows(); - g_gpu->ResetGraphicsAPIState(); - m_display->Render(); ImGui::NewFrame(); - g_gpu->RestoreGraphicsAPIState(); - System::UpdatePerformanceCounters(); + if (System::IsRunning()) + { + System::UpdatePerformanceCounters(); - if (m_speed_limiter_enabled) - System::Throttle(); + if (m_speed_limiter_enabled) + System::Throttle(); + } } } diff --git a/src/core/system.cpp b/src/core/system.cpp index c2b3253c2..661a26fa1 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -586,6 +586,11 @@ bool DoState(StateWrapper& sw) void Reset() { + if (IsShutdown()) + return; + + g_gpu->RestoreGraphicsAPIState(); + CPU::Reset(); CPU::CodeCache::Flush(); Bus::Reset(); @@ -602,6 +607,8 @@ void Reset() s_internal_frame_number = 0; TimingEvents::Reset(); ResetPerformanceCounters(); + + g_gpu->ResetGraphicsAPIState(); } bool LoadState(ByteStream* state) @@ -609,7 +616,13 @@ bool LoadState(ByteStream* state) if (IsShutdown()) return false; - return DoLoadState(state, false); + g_gpu->RestoreGraphicsAPIState(); + + const bool result = DoLoadState(state, false); + + g_gpu->ResetGraphicsAPIState(); + + return result; } bool DoLoadState(ByteStream* state, bool force_software_renderer) @@ -732,11 +745,9 @@ bool SaveState(ByteStream* state, u32 screenshot_size /* = 128 */) if (screenshot_size > 0) { std::vector<u32> screenshot_buffer; - g_gpu->ResetGraphicsAPIState(); - const bool screenshot_saved = - g_host_interface->GetDisplay()->WriteDisplayTextureToBuffer(&screenshot_buffer, screenshot_size, screenshot_size); - g_gpu->RestoreGraphicsAPIState(); - if (screenshot_saved && !screenshot_buffer.empty()) + if (g_host_interface->GetDisplay()->WriteDisplayTextureToBuffer(&screenshot_buffer, screenshot_size, + screenshot_size) && + !screenshot_buffer.empty()) { header.offset_to_screenshot = static_cast<u32>(state->GetPosition()); header.screenshot_width = screenshot_size; @@ -751,8 +762,14 @@ bool SaveState(ByteStream* state, u32 screenshot_size /* = 128 */) { header.offset_to_data = static_cast<u32>(state->GetPosition()); + g_gpu->RestoreGraphicsAPIState(); + StateWrapper sw(state, StateWrapper::Mode::Write); - if (!DoState(sw)) + const bool result = DoState(sw); + + g_gpu->ResetGraphicsAPIState(); + + if (!result) return false; header.data_compression_type = 0; @@ -774,6 +791,8 @@ void RunFrame() { s_frame_timer.Reset(); + g_gpu->RestoreGraphicsAPIState(); + if (g_settings.cpu_execution_mode == CPUExecutionMode::Interpreter) CPU::Execute(); else @@ -781,6 +800,8 @@ void RunFrame() // Generate any pending samples from the SPU before sleeping, this way we reduce the chances of underruns. g_spu.GeneratePendingSamples(); + + g_gpu->ResetGraphicsAPIState(); } void SetThrottleFrequency(float frequency) diff --git a/src/duckstation-libretro/libretro_host_interface.cpp b/src/duckstation-libretro/libretro_host_interface.cpp index 4f7f1c6a9..41481984a 100644 --- a/src/duckstation-libretro/libretro_host_interface.cpp +++ b/src/duckstation-libretro/libretro_host_interface.cpp @@ -259,12 +259,8 @@ void LibretroHostInterface::retro_run_frame() UpdateControllers(); - g_gpu->RestoreGraphicsAPIState(); - System::RunFrame(); - g_gpu->ResetGraphicsAPIState(); - m_display->Render(); } diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index c03be4c66..053a600a9 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -1094,14 +1094,10 @@ void QtHostInterface::threadEntryPoint() void QtHostInterface::renderDisplay() { - g_gpu->ResetGraphicsAPIState(); - DrawImGuiWindows(); m_display->Render(); ImGui::NewFrame(); - - g_gpu->RestoreGraphicsAPIState(); } void QtHostInterface::wakeThread() diff --git a/src/duckstation-sdl/sdl_host_interface.cpp b/src/duckstation-sdl/sdl_host_interface.cpp index cfb7d9a3d..3b2a89dd4 100644 --- a/src/duckstation-sdl/sdl_host_interface.cpp +++ b/src/duckstation-sdl/sdl_host_interface.cpp @@ -1505,16 +1505,12 @@ void SDLHostInterface::Run() { DrawImGuiWindows(); - if (System::IsRunning()) - g_gpu->ResetGraphicsAPIState(); - m_display->Render(); ImGui_ImplSDL2_NewFrame(m_window); ImGui::NewFrame(); if (System::IsRunning()) { - g_gpu->RestoreGraphicsAPIState(); System::UpdatePerformanceCounters(); if (m_speed_limiter_enabled)