From 5ee15471a3b695ab22fc47fb864ed9083f6674ce Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 23:32:31 -0800 Subject: [PATCH] D3D12.X: retain submitted texture staging after wait failure Texture submission can succeed even when the retirement-fence creation or Signal used to track its lifetime fails. If the fallback queue wait also fails, releasing the command objects and staging resource can free memory that submitted GPU work still references. Drop those objects only after a successful fallback wait; otherwise retain them deliberately. Apply the same rule to synchronous uploads whose native tracking Signal fails. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/gpu/texture.rs | 31 ++++++++++++++++++++- crates/xcl/src/hle/d3d12x/title_textures.rs | 6 ++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/gpu/texture.rs b/crates/xcl/src/gpu/texture.rs index b8e3dbf5b6..62f7e11bca 100644 --- a/crates/xcl/src/gpu/texture.rs +++ b/crates/xcl/src/gpu/texture.rs @@ -310,7 +310,11 @@ pub(crate) unsafe fn upload_batch_with_zero_mips( crate::hle::d3d12x::real::native_queue_execute(queue, &list); up.value += 1; let copy_error = |e| format!("texture copy: {e}"); - crate::hle::d3d12x::real::native_queue_signal(queue, &up.fence, up.value).map_err(|e| copy_error(fence::api_error(dev, "Signal", e)))?; + if let Err(e) = crate::hle::d3d12x::real::native_queue_signal(queue, &up.fence, up.value) { + let signal = copy_error(fence::api_error(dev, "Signal", e)); + drop_after_wait(up, wait(dev, queue))?; + return Err(signal); + } if let Err(e) = fence::wait(&up.fence, up.value, dev, None, 10_000) { // the GPU may still read the upload buffer and run the list: they stay alive (a fresh // uploader next time) – freeing them under the GPU removed the device @@ -599,6 +603,15 @@ pub unsafe fn wait(dev: &ID3D12Device, queue: &ID3D12CommandQueue) -> Result<(), fence::wait(&fence, 1, dev, None, 10_000).map_err(copy_error) } +/// Release GPU submission objects only once a fallback queue wait made that safe. If the wait +/// itself fails, deliberately retain them: submitted work may still reference their resources. +pub(crate) fn drop_after_wait(objects: T, waited: Result<(), String>) -> Result<(), String> { + if waited.is_err() { + std::mem::forget(objects); + } + waited +} + unsafe fn buffer(dev: &ID3D12Device, size: u64) -> Result { let rd = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, @@ -640,6 +653,22 @@ mod tests { use super::*; use windows::Win32::Graphics::Dxgi::Common::*; + #[test] + fn submitted_objects_are_retained_when_the_fallback_wait_fails() { + struct CountDrop<'a>(&'a std::sync::atomic::AtomicUsize); + impl Drop for CountDrop<'_> { + fn drop(&mut self) { + self.0.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + } + + let drops = std::sync::atomic::AtomicUsize::new(0); + assert!(drop_after_wait(CountDrop(&drops), Ok(())).is_ok()); + assert_eq!(drops.load(std::sync::atomic::Ordering::Relaxed), 1); + assert_eq!(drop_after_wait(CountDrop(&drops), Err("wait failed".into())).unwrap_err(), "wait failed"); + assert_eq!(drops.load(std::sync::atomic::Ordering::Relaxed), 1); + } + #[test] fn shared_zero_mip_staging_compacts_and_clears_the_full_span() { // A 1024² R8 texture with five mips, including the driver's row and placement alignment. diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 4536b9f335..a06e1146bf 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -1333,8 +1333,7 @@ unsafe fn submit_on_title_queue( g.insert(key, QueueUploads { fence, value: 0, in_flight: std::collections::VecDeque::new(), bytes: 0 }); } Err(e) => { - texture::wait(dev, queue)?; - drop(f); + texture::drop_after_wait(f, texture::wait(dev, queue))?; return Err(e.to_string()); } } @@ -1343,8 +1342,7 @@ unsafe fn submit_on_title_queue( q.value += 1; if let Err(e) = super::real::native_queue_signal(queue, &q.fence, q.value) { q.value -= 1; - texture::wait(dev, queue)?; - drop(f); + texture::drop_after_wait(f, texture::wait(dev, queue))?; return Err(e.to_string()); } q.bytes += f.bytes;