From 4250334773d9061155523b97398926935e8728b4 Mon Sep 17 00:00:00 2001 From: Prime1997 <215761102+Prime1997@users.noreply.github.com> Date: Sun, 29 Nov 2026 06:40:48 +0000 Subject: [PATCH] D3D12.X: the fence and CPU-write threads run at high priority xcl-fences writes the completed values title threads spin on, and xcl-cpu-writes writes the values a GPU command list leaves for the CPU. At normal priority both can queue behind the very title threads that spin waiting for them. Both sleep nearly all the time, so THREAD_PRIORITY_HIGHEST costs the title little; not time-critical, as a fences wake can settle readbacks for up to 20 ms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DaZV7vMq866ni7pGnJC2Ew --- crates/xcl/src/hle/d3d12x/cpu_writes.rs | 2 ++ crates/xcl/src/hle/d3d12x/fence_x.rs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/cpu_writes.rs b/crates/xcl/src/hle/d3d12x/cpu_writes.rs index f672e42f0a..f399082b96 100644 --- a/crates/xcl/src/hle/d3d12x/cpu_writes.rs +++ b/crates/xcl/src/hle/d3d12x/cpu_writes.rs @@ -69,6 +69,8 @@ fn worker() -> &'static Mutex> { W.get_or_init(|| { let (tx, rx) = mpsc::channel::(); let spawned = std::thread::Builder::new().name("xcl-cpu-writes".into()).spawn(move || unsafe { + // Title threads spin on the values written here (`xcl-fences` has why it is high). + th::SetThreadPriority(th::GetCurrentThread(), th::THREAD_PRIORITY_HIGHEST); let event = th::CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()); for job in rx { if super::real::fence_completed(&job.fence) < job.value && job.fence.SetEventOnCompletion(job.value, HANDLE(event as *mut c_void)).is_ok() { diff --git a/crates/xcl/src/hle/d3d12x/fence_x.rs b/crates/xcl/src/hle/d3d12x/fence_x.rs index 348b70fbc0..0f6cfd70bb 100644 --- a/crates/xcl/src/hle/d3d12x/fence_x.rs +++ b/crates/xcl/src/hle/d3d12x/fence_x.rs @@ -158,10 +158,16 @@ fn event() -> usize { static E: OnceLock = OnceLock::new(); *E.get_or_init(|| unsafe { let e = th::CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()) as usize; - let spawned = std::thread::Builder::new().name("xcl-fences".into()).spawn(move || loop { - th::WaitForSingleObject(e as _, th::INFINITE); - super::title_textures::poll_fence_downloads(); - table().refresh(); + let spawned = std::thread::Builder::new().name("xcl-fences".into()).spawn(move || { + // Title threads spin on the cells this thread writes: at normal priority it can queue + // behind those very threads. It sleeps nearly all the time, so a high priority costs + // them little (not time-critical: a wake can settle readbacks for up to 20 ms). + th::SetThreadPriority(th::GetCurrentThread(), th::THREAD_PRIORITY_HIGHEST); + loop { + th::WaitForSingleObject(e as _, th::INFINITE); + super::title_textures::poll_fence_downloads(); + table().refresh(); + } }); if let Err(err) = spawned { crate::xlog_error!("D3D12.X: no thread for the fences' completed values: {err}");