From 035c98d14f4af62ce50bd21eb6fa8cc283ea7b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 29 Sep 2026 13:16:59 -0600 Subject: [PATCH] D3D11: Re-read the device and context on DeviceRestore The restored draw context can be a new device, so the cached pointers could go stale. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/D3D11/DrawEngineD3D11.cpp | 9 +++++++++ GPU/D3D11/GPU_D3D11.cpp | 3 +++ GPU/D3D11/ShaderManagerD3D11.cpp | 6 ++++++ GPU/D3D11/TextureCacheD3D11.cpp | 7 ++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/GPU/D3D11/DrawEngineD3D11.cpp b/GPU/D3D11/DrawEngineD3D11.cpp index a75fd7ab9e..bcfc96e546 100644 --- a/GPU/D3D11/DrawEngineD3D11.cpp +++ b/GPU/D3D11/DrawEngineD3D11.cpp @@ -128,10 +128,19 @@ void DrawEngineD3D11::DestroyDeviceObjects() { void DrawEngineD3D11::DeviceLost() { DestroyDeviceObjects(); draw_ = nullptr; + device_ = nullptr; + context_ = nullptr; + device1_ = nullptr; + context1_ = nullptr; } void DrawEngineD3D11::DeviceRestore(Draw::DrawContext *draw) { + // The restored context can be a new device, so don't keep the old pointers. draw_ = draw; + device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); + context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT); + device1_ = (ID3D11Device1 *)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX); + context1_ = (ID3D11DeviceContext1 *)draw->GetNativeObject(Draw::NativeObject::CONTEXT_EX); InitDeviceObjects(); } diff --git a/GPU/D3D11/GPU_D3D11.cpp b/GPU/D3D11/GPU_D3D11.cpp index 441da4de04..589e515d28 100644 --- a/GPU/D3D11/GPU_D3D11.cpp +++ b/GPU/D3D11/GPU_D3D11.cpp @@ -89,6 +89,9 @@ void GPU_D3D11::DeviceLost() { } void GPU_D3D11::DeviceRestore(Draw::DrawContext *draw) { + // The restored context can be a new device, so don't keep the old pointers. + device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); + context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT); GPUCommonHW::DeviceRestore(draw); } diff --git a/GPU/D3D11/ShaderManagerD3D11.cpp b/GPU/D3D11/ShaderManagerD3D11.cpp index d7e3b94300..03d29ed4b8 100644 --- a/GPU/D3D11/ShaderManagerD3D11.cpp +++ b/GPU/D3D11/ShaderManagerD3D11.cpp @@ -111,10 +111,16 @@ void ShaderManagerD3D11::DestroyDeviceObjects() { void ShaderManagerD3D11::DeviceLost() { DestroyDeviceObjects(); draw_ = nullptr; + device_ = nullptr; + context_ = nullptr; } void ShaderManagerD3D11::DeviceRestore(Draw::DrawContext *draw) { + // The restored context can be a new device, so don't keep the old pointers. draw_ = draw; + device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); + context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT); + featureLevel_ = (D3D_FEATURE_LEVEL)draw->GetNativeObject(Draw::NativeObject::FEATURE_LEVEL); InitDeviceObjects(); } diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp index 9b65c403e3..ad87ac020f 100644 --- a/GPU/D3D11/TextureCacheD3D11.cpp +++ b/GPU/D3D11/TextureCacheD3D11.cpp @@ -178,9 +178,14 @@ void TextureCacheD3D11::DeviceLost() { TextureCacheCommon::DeviceLost(); DestroyDeviceObjects(); draw_ = nullptr; + device_ = nullptr; + context_ = nullptr; } -void TextureCacheD3D11::DeviceRestore(Draw::DrawContext *draw) { +void TextureCacheD3D11::DeviceRestore(Draw::DrawContext *draw) { + // The restored context can be a new device, so don't keep the old pointers. + device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); + context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT); TextureCacheCommon::DeviceRestore(draw); InitDeviceObjects(); }