From 58b0e6859ae28f5e8deee5e27daac18d0bfa3971 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 27 Jun 2020 02:22:52 +1000 Subject: [PATCH] Frontend: Reset/restore GPU state before saving screenshot Fixes driver crashes in Vulkan. --- src/core/system.cpp | 8 +++++--- src/frontend-common/common_host_interface.cpp | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index 9641238ec..040b31d1b 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -499,9 +499,11 @@ bool System::SaveState(ByteStream* state) const u32 screenshot_height = 128; std::vector screenshot_buffer; - if (m_host_interface->GetDisplay()->WriteDisplayTextureToBuffer(&screenshot_buffer, screenshot_width, - screenshot_height) && - !screenshot_buffer.empty()) + m_gpu->ResetGraphicsAPIState(); + const bool screenshot_saved = + m_host_interface->GetDisplay()->WriteDisplayTextureToBuffer(&screenshot_buffer, screenshot_width, screenshot_height); + m_gpu->RestoreGraphicsAPIState(); + if (screenshot_saved && !screenshot_buffer.empty()) { header.offset_to_screenshot = static_cast(state->GetPosition()); header.screenshot_width = screenshot_width; diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index fce0a44ef..8c5dcb305 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -1947,7 +1947,11 @@ bool CommonHostInterface::SaveScreenshot(const char* filename /* = nullptr */, b filename = auto_filename.c_str(); } - if (!m_display->WriteDisplayTextureToFile(filename, full_resolution, apply_aspect_ratio)) + m_system->GetGPU()->ResetGraphicsAPIState(); + const bool screenshot_saved = + m_display->WriteDisplayTextureToFile(filename, full_resolution, apply_aspect_ratio); + m_system->GetGPU()->RestoreGraphicsAPIState(); + if (!screenshot_saved) { AddFormattedOSDMessage(10.0f, "Failed to save screenshot to '%s'", filename); return false;