From 189f9db18c0304714ca17ce600770aacc2bf3287 Mon Sep 17 00:00:00 2001 From: dervoxx Date: Tue, 24 Nov 2026 02:24:25 +0100 Subject: [PATCH] D3D12.X: preserve unwritten gaps when tracking same-list waits --- crates/xcl/src/hle/d3d12x/list_x.rs | 33 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index cdf0c9e7c..fd9a1bab5 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -61,8 +61,6 @@ const WAIT_TIMEOUT: Duration = Duration::from_secs(2); /// hang, while giving heavy initial 3D frames (>1 s of dispatches) plenty of room to finish /// cleanly without prematurely breaking synchronization. const GPU_WAIT_WATCHDOG_TIMEOUT: Duration = Duration::from_millis(5000); -/// Cap on tracked per-list written UMA ranges (for same-list write-then-wait detection). -const MAX_WRITTEN_RANGES: usize = 256; fn wait_timeout() -> Duration { static T: OnceLock = OnceLock::new(); @@ -565,7 +563,7 @@ struct ListState { /// Last `OMSetDepthBounds` values, restored after `KickoffX` depth_bounds: Option<(f32, f32)>, occlusion: Occlusion, - /// UMA address ranges written by this list since last Reset/KickoffX + /// Sorted, disjoint UMA ranges written by this list since last Reset/KickoffX written_ranges: Vec<(u64, u64)>, /// Sparse-window `(va, bytes)` ranges written by memory ops (`Write32/64`, `Fill*`, /// `CopyMemoryToMemoryX`, timestamps) to copy back to the CPU view after execution. @@ -594,11 +592,14 @@ impl ListState { return; } let end = va.saturating_add(size); - if self.written_ranges.len() < MAX_WRITTEN_RANGES { - self.written_ranges.push((va, end)); - } else if let Some(last) = self.written_ranges.last_mut() { - last.0 = last.0.min(va); - last.1 = last.1.max(end); + let ranges = &mut self.written_ranges; + let first = ranges.partition_point(|&(_, stop)| stop < va); + let count = ranges[first..].partition_point(|&(start, _)| start <= end); + if count == 0 { + ranges.insert(first, (va, end)); + } else { + ranges[first] = (va.min(ranges[first].0), end.max(ranges[first + count - 1].1)); + ranges.drain(first + 1..first + count); } if crate::gpu::uma::get().is_some_and(|u| u.is_sparse(va as usize)) { if self.sparse_writes.is_empty() { @@ -633,7 +634,8 @@ impl ListState { fn has_prior_write(&self, va: u64, size: u64) -> bool { let end = va.saturating_add(size); - self.written_ranges.iter().any(|&(s, e)| va < e && s < end) + let first = self.written_ranges.partition_point(|&(_, stop)| stop <= va); + self.written_ranges.get(first).is_some_and(|&(start, _)| start < end) } fn clear_waits(&mut self) { @@ -4314,6 +4316,19 @@ mod tests { assert!(!s.has_prior_write(0x0FF8, 8)); } + #[test] + fn scattered_writes_preserve_unwritten_gaps() { + let mut s = ListState::default(); + for i in 0..300 { + s.note_write(0x1000 + i * 16, 8); + } + let a = 0x1000 + 260 * 16; + assert!(!s.has_prior_write(a + 8, 8)); + s.note_write(a + 8, 8); + assert!(s.has_prior_write(a + 8, 8)); + assert!(!s.has_prior_write(a - 8, 8)); + } + #[test] fn execute_with_waits_splits_batches_before_unsatisfied_list() { // Use synthetic non-zero list pointers distinct from real COM objects.