From 3ea121cad377d59125125cb539e8a5b7259dc88e Mon Sep 17 00:00:00 2001 From: TUNA_BIN <65464657+TUNA-NOPE@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:41:09 +0000 Subject: [PATCH] metal2vulkan 0093: a command buffer dropped uncommitted takes its standings with it With surface_lazy_open_ids (on with Coherent), a command buffer dropped without a commit left the open set, so the standing its encoders left read as committed, and a later encoder could skip a copy in it needed and draw stale pixels. Found by the Codex review of #7. --- src/patches/README.md | 4 +- ...mmand-buffer-dropped-uncommitted-tak.patch | 171 ++++++++++++++++++ 2 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 src/patches/metal2vulkan/0093-shadow_lazy-a-command-buffer-dropped-uncommitted-tak.patch diff --git a/src/patches/README.md b/src/patches/README.md index e364891..c2c89cc 100644 --- a/src/patches/README.md +++ b/src/patches/README.md @@ -13,7 +13,7 @@ earlier "cohwl" build (2026-11-05). None of it is upstream yet. Most of the new code sits behind a switch and does nothing until it is set. What macbox turns on is in [docs/performance.md](../../docs/performance.md#gpu-tuning-switches). -## metal2vulkan (on `development` at 0db5825a), 92 patches +## metal2vulkan (on `development` at 0db5825a), 93 patches | Patches | What they do | |---|---| @@ -31,7 +31,7 @@ is in [docs/performance.md](../../docs/performance.md#gpu-tuning-switches). | 0040 flat variant | the flat dispatch stays SIMD16 and counts a 1-D workgroup from its id. Always on | | 0042–0044 render lowering lanes | lowering render calls on a worker thread and on lanes per queue; off unless Reims sets `REIMS_VGPU_RENDER_LOWERING=worker`, which it refuses while the census runs | | 0045, 0046 recycled images | host-copied textures' images that the host writes too are recycled | -| 0050, 0051, 0058–0061, 0069, 0070 surface fixes | `surface_lazy_max_texels`, `surface_twin_reader_first`, `surface_usage_shadow_image`, `surface_coherent_own_back`, `surface_lazy_open_ids`, `surface_twin_make_once` (a race between two textures over one surface: 91 → 100 fps over 12 boots). All but `surface_usage_shadow_image` on with Coherent | +| 0050, 0051, 0058–0061, 0069, 0070, 0093 surface fixes | `surface_lazy_max_texels`, `surface_twin_reader_first`, `surface_usage_shadow_image`, `surface_coherent_own_back`, `surface_lazy_open_ids`, `surface_twin_make_once` (a race between two textures over one surface: 91 → 100 fps over 12 boots). 0093: a command buffer dropped without a commit takes the standings its encoders left with it, so `surface_lazy_open_ids` never reads them as committed. All but `surface_usage_shadow_image` on with Coherent | | 0085 `inline_lazy`, 0092 `inline_checks` | a lazy surface's first pass instance recorded inline; a batch's submission checks run in the scheduler's pass. On with Coherent | | 0062, 0068, 0076, 0077, 0080, 0081, 0083, 0084, 0086, 0090 experiments | `xr_unused_bits_off`, `surface_coherent_writer_wins`, `M2V_FLAT_TILE`, `shared_ccs`, `host_mutable`, `batch_end_light`, `view_render_direct`, `precise_barriers`, `checks_first`: live switches, all off. Most are in [docs/performance.md](../../docs/performance.md#tried-no-gain) | | 0047, 0048, 0052–0057, 0063–0067, 0071–0075, 0078, 0079, 0082, 0086–0089, 0091 diagnostics | the pass timer's units and censuses, logs of views, twins, barriers and the scheduler, counters of why shadows copy back, and `M2V_PENDING_AUDIT` (command buffers begun while their submission is pending). Off unless their variable is set | diff --git a/src/patches/metal2vulkan/0093-shadow_lazy-a-command-buffer-dropped-uncommitted-tak.patch b/src/patches/metal2vulkan/0093-shadow_lazy-a-command-buffer-dropped-uncommitted-tak.patch new file mode 100644 index 0000000..3348a1e --- /dev/null +++ b/src/patches/metal2vulkan/0093-shadow_lazy-a-command-buffer-dropped-uncommitted-tak.patch @@ -0,0 +1,171 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: macbox +Date: Tue, 10 Nov 2026 08:27:58 +0000 +Subject: [PATCH] shadow_lazy: a command buffer dropped uncommitted takes its + standings with it + +With surface_lazy_open_ids, dropped() took the id out of the open set, so a +standing its encoders left read as committed and a later encoder could skip +the copy in it needed. Clear those standings, count them (dropped-standings +in the report), and test it without a GPU. +--- + .../metal2vulkan-runtime/src/shadow_lazy.rs | 113 +++++++++++++----- + 1 file changed, 84 insertions(+), 29 deletions(-) + +diff --git a/crates/metal2vulkan-runtime/src/shadow_lazy.rs b/crates/metal2vulkan-runtime/src/shadow_lazy.rs +index 9cf44341..31d7366d 100644 +--- a/crates/metal2vulkan-runtime/src/shadow_lazy.rs ++++ b/crates/metal2vulkan-runtime/src/shadow_lazy.rs +@@ -150,6 +150,8 @@ static TWINS: AtomicU64 = AtomicU64::new(0); + static READERS_FIRST: AtomicU64 = AtomicU64::new(0); + static PRESENTS: AtomicU64 = AtomicU64::new(0); + static PRESENTS_UNMATCHED: AtomicU64 = AtomicU64::new(0); ++/// LOCAL (Atlas): standings cleared because their command buffer was dropped uncommitted ([`dropped`]). ++static DROPPED_STANDINGS: AtomicU64 = AtomicU64::new(0); + /// Milliseconds since the first report's chance at the last report. + static REPORTED: AtomicU64 = AtomicU64::new(0); + +@@ -236,9 +238,22 @@ fn note_open(id: u64) { + open.insert(id); + } + +-/// LOCAL (Atlas): the command buffer `id` went without a commit: it holds no standing open. ++/// LOCAL (Atlas): the command buffer `id` went without a commit. A standing its encoders left ++/// stands for writes that never ran, so it goes: with `surface_lazy_open_ids` the open set is what ++/// says a standing's command buffer has not committed, and taking `id` out of it alone would read as ++/// committed, and a later encoder would skip the copy in it needs. A committed command buffer left ++/// the set at its commit, so only one dropped uncommitted looks through the surfaces. + pub(crate) fn dropped(id: u64) { +- open_ids().lock().unwrap().remove(&id); ++ if !open_ids().lock().unwrap().remove(&id) { ++ return; ++ } ++ for lazy in surfaces() { ++ let mut stood = lazy.stood.lock().unwrap(); ++ if stood.is_some_and(|stood| stood.id == id) { ++ *stood = None; ++ DROPPED_STANDINGS.fetch_add(1, Ordering::Relaxed); ++ } ++ } + } + + /// LOCAL (Atlas): the command buffers committed whose last batch the scheduler has not planned +@@ -989,7 +1004,8 @@ fn report() { + "surface_lazy: taken {taken} refused {refused} resyncs {resyncs} | refused: touched {} \ + written {} epoch {} own {} uncommitted {} no-standing {} | in: first {} beyond-held {} \ + changed {} bounded {} | ahead: deferred {} shared {} flushes {} whole {} texels {} \ +- discarded {} presents {} unmatched {} twins {} readers-first {} own-backs-skipped {}", ++ discarded {} presents {} unmatched {} twins {} readers-first {} own-backs-skipped {} \ ++ dropped-standings {}", + by[Refused::Touched as usize], + by[Refused::Written as usize], + by[Refused::Epoch as usize], +@@ -1011,6 +1027,7 @@ fn report() { + read(&TWINS), + read(&READERS_FIRST), + read(&OWN_BACKS_SKIPPED), ++ read(&DROPPED_STANDINGS), + ); + eprintln!("coherent_writer_wins: yielded {}", super::shadow_coherent::YIELDED.load(Ordering::Relaxed)); + // LOCAL (Atlas, lab diagnostics): why the large shadows' copies back were recorded. +@@ -1263,32 +1280,36 @@ impl Lazy { + size: [u32; 2], + texels: u64, + ) -> Option> { +- (surface && takes_surface(size[0], texels)).then(|| { +- Arc::new(Lazy { +- defer_no: Default::default(), +- yielded: Default::default(), +- truth: OnceLock::new(), +- written: AtomicU64::new(0), +- plain: AtomicU64::new(0), +- beside: Mutex::default(), +- stood: Mutex::new(None), +- resynced: Mutex::new(Instant::now()), +- device: Arc::new(device.clone()), +- pages: pages(), +- size, +- displayed: AtomicBool::new(false), +- keeps: OnceLock::new(), +- pending: Mutex::new(None), +- ahead: AtomicBool::new(false), +- flush_after: AtomicU64::new(0), +- queue: OnceLock::new(), +- taken: AtomicU64::new(0), +- refused: AtomicU64::new(0), +- copied_in: AtomicU64::new(0), +- presents: AtomicU64::new(0), +- coherent: OnceLock::new(), +- owners: Mutex::new(Vec::new()), +- }) ++ (surface && takes_surface(size[0], texels)) ++ .then(|| Lazy::new(Arc::new(device.clone()), pages(), size)) ++ } ++ ++ /// A surface of `size` over the host addresses `pages`, its queue from `device`. ++ fn new(device: Hold, pages: Option>, size: [u32; 2]) -> Arc { ++ Arc::new(Lazy { ++ defer_no: Default::default(), ++ yielded: Default::default(), ++ truth: OnceLock::new(), ++ written: AtomicU64::new(0), ++ plain: AtomicU64::new(0), ++ beside: Mutex::default(), ++ stood: Mutex::new(None), ++ resynced: Mutex::new(Instant::now()), ++ device, ++ pages, ++ size, ++ displayed: AtomicBool::new(false), ++ keeps: OnceLock::new(), ++ pending: Mutex::new(None), ++ ahead: AtomicBool::new(false), ++ flush_after: AtomicU64::new(0), ++ queue: OnceLock::new(), ++ taken: AtomicU64::new(0), ++ refused: AtomicU64::new(0), ++ copied_in: AtomicU64::new(0), ++ presents: AtomicU64::new(0), ++ coherent: OnceLock::new(), ++ owners: Mutex::new(Vec::new()), + }) + } + +@@ -1908,3 +1929,37 @@ impl Lazy { + Some((stood.held, stood.stale, stood.written)) + } + } ++ ++#[cfg(test)] ++mod tests { ++ use super::*; ++ ++ /// A surface the command buffer `id`'s encoder left standing, as [`Lazy::stand`] leaves it. ++ fn left_standing_by(id: u64) -> Arc { ++ let lazy = Lazy::new(Arc::new(()), None, [64, 64]); ++ registry().write().unwrap().push(Arc::downgrade(&lazy)); ++ note_open(id); ++ *lazy.stood.lock().unwrap() = ++ Some(Stood { id, touched: 0, held: None, stale: None, written: 0, plain: 0, epoch: 0 }); ++ lazy ++ } ++ ++ /// With `surface_lazy_open_ids`, a command buffer dropped without a commit takes its standings ++ /// with it: they stand for writes that never ran, and must not read as committed. ++ #[test] ++ fn a_command_buffer_dropped_uncommitted_takes_its_standing_with_it() { ++ crate::live::set(OPEN_IDS, Some("1")); ++ // Identities no command buffer of the test process takes (`ShadowCarry::id` counts up). ++ let (kept, gone) = (u64::MAX - 21, u64::MAX - 22); ++ let committed_one = left_standing_by(kept); ++ let dropped_one = left_standing_by(gone); ++ assert!(!committed_one.standing_committed()); ++ assert!(!dropped_one.standing_committed()); ++ committed(kept); ++ dropped(kept); ++ dropped(gone); ++ assert!(committed_one.standing_committed()); ++ assert!(dropped_one.stood.lock().unwrap().is_none()); ++ assert!(!dropped_one.standing_committed()); ++ } ++}