From b43e119ea11f171a5140b024c7941196069e4a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 26 Oct 2026 15:16:33 +0100 Subject: [PATCH] GPU: Don't keep state from before a device loss - Metal: Load the shader cache again in DeviceRestore, like Vulkan. DeviceLost saves it and throws everything away, so the next save (the next app switch, or quitting) overwrote it with only what had been drawn since. - Software renderer: Bring the post-shader back after a device loss. DestroyDeviceObjects asks for it with restorePostShader_, but CreateDeviceObjects cleared the flag again before BeginFrame could act on it. The hardware backends recover through renderResized_. - Forget pointers into the old device's objects in DeviceLost, rather than relying on BeginFrame to come before the next flush: Vulkan's last pipeline and bound views, Metal's UBO allocations, the GLES last linked shader and bound texture. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Common/PresentationCommon.cpp | 3 +-- GPU/GLES/ShaderManagerGLES.cpp | 1 + GPU/GLES/TextureCacheGLES.cpp | 1 + GPU/Metal/DrawEngineMetal.mm | 4 ++++ GPU/Metal/GPU_Metal.mm | 6 ++++++ GPU/Vulkan/DrawEngineVulkan.cpp | 4 ++++ 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/GPU/Common/PresentationCommon.cpp b/GPU/Common/PresentationCommon.cpp index 6bc3179b85..392ea6d045 100644 --- a/GPU/Common/PresentationCommon.cpp +++ b/GPU/Common/PresentationCommon.cpp @@ -599,8 +599,7 @@ void PresentationCommon::CreateDeviceObjects() { texColorRBSwizzle_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), draw_->GetFshaderPreset(FS_TEXTURE_COLOR_2D_RB_SWIZZLE) }, false, &vsTexColBufDesc); texColorBlend_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), draw_->GetFshaderPreset(FS_TEXTURE_COLOR_2D) }, false, &vsTexColBufDesc, true); overlayVdata_ = draw_->CreateBuffer(sizeof(Vertex) * 4, BufferUsageFlag::DYNAMIC | BufferUsageFlag::VERTEXDATA); - - restorePostShader_ = false; + // Not resetting restorePostShader_: after a device loss, BeginFrame uses it to bring the post-shader back. } template diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index ea3439b176..17cd36cb05 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -612,6 +612,7 @@ void ShaderManagerGLES::Clear() { linkedShaderCache_.clear(); fsCache_.Clear(); vsCache_.Clear(); + lastShader_ = nullptr; lastFSID_.set_invalid(); lastVSID_.set_invalid(); diff --git a/GPU/GLES/TextureCacheGLES.cpp b/GPU/GLES/TextureCacheGLES.cpp index 3050cf55dd..5f5e81883e 100644 --- a/GPU/GLES/TextureCacheGLES.cpp +++ b/GPU/GLES/TextureCacheGLES.cpp @@ -407,6 +407,7 @@ bool TextureCacheGLES::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level, void TextureCacheGLES::DeviceLost() { TextureCacheCommon::DeviceLost(); + lastBoundTexture_ = nullptr; draw_ = nullptr; render_ = nullptr; } diff --git a/GPU/Metal/DrawEngineMetal.mm b/GPU/Metal/DrawEngineMetal.mm index 905a1b8e4d..1dbd5276d7 100644 --- a/GPU/Metal/DrawEngineMetal.mm +++ b/GPU/Metal/DrawEngineMetal.mm @@ -178,6 +178,10 @@ void DrawEngineMetal::DeviceLost() { } boundSecondary_ = nil; boundDepal_ = nil; + // Allocated from the old render manager's push buffer. + baseUBO_ = MetalPushAlloc{}; + lightUBO_ = MetalPushAlloc{}; + uboFrame_ = (uint64_t)-1; draw_ = nullptr; rm_ = nullptr; } diff --git a/GPU/Metal/GPU_Metal.mm b/GPU/Metal/GPU_Metal.mm index 43e07d0971..0deea8a6eb 100644 --- a/GPU/Metal/GPU_Metal.mm +++ b/GPU/Metal/GPU_Metal.mm @@ -214,6 +214,12 @@ void GPU_Metal::DeviceLost() { void GPU_Metal::DeviceRestore(Draw::DrawContext *draw) { GPUCommonHW::DeviceRestore(draw); + + // DeviceLost saved the cache and then threw everything away. Load it again, or the next save would + // overwrite the file with only what gets drawn from now on. + if (shaderCachePath_.Valid()) { + LoadCache(shaderCachePath_); + } } void GPU_Metal::GetStats(StringWriter &w) { diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index aadc9edee9..163420f617 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -133,6 +133,10 @@ void DrawEngineVulkan::DestroyDeviceObjects() { void DrawEngineVulkan::DeviceLost() { DestroyDeviceObjects(); DirtyAllUBOs(); + // The pipelines and views these point to are gone (BeginFrame resets them too, but a flush could come first). + lastPipeline_ = nullptr; + boundDepal_ = VK_NULL_HANDLE; + boundSecondary_ = VK_NULL_HANDLE; draw_ = nullptr; }