From dd986f0aae41f069dfd24bd217bb2a3acfd100c4 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 3 Nov 2026 13:36:38 -0800 Subject: [PATCH] Drop a carried shadow's image when another shadow over its bytes writes them back What: ShadowSync::copy_back, which records a copy back or a merge of one shadowed texture's writes into its truth, now also drops from the encoder's carry (ShadowCarry) every carried image of another texture that shares that truth. The dropped texture copies in again at its next use. The doc of copy_back and ShadowCarry's list of what ends a carry say so. Why: copy_back already left every other texture over the bytes that the encoder had met no longer current (or stale inside the written rectangle). An image the command buffer carried from an earlier encoder, for a texture this encoder never met, stayed carried: the shadow copies are recorded through EncoderCore::shadow_command, which never reaches ShadowSync::wrote. So the next pass over that texture rendered over texels the bytes no longer held, and wrote the result back. This breaks the module's copy-in rule, under which a texture copies in again after another shadowed texture over its bytes wrote them. The fix is structural: whatever writes a truth through one shadow ends the carry of every other shadow over that truth, whether merged back or copied back. Verified on the M3 Max (MoltenVK 1.4.2), --cfg m2v_gpu, under maclock and memcap: - tests::shadow_twins: both cases pass. They failed on the commit before, which is the negative control (3,927 and 5,318 bytes differed). With the fix, every pass after a pass over the other texture copies in again: 76,800 texels copied in, none taken from the carry. - The unit case in a_carried_surface_is_dropped_by_anything_that_lets_another_writer_in passes; before the fix it fails. - The runtime's shadow, merge_scratch, linear_runs, dirty_pages, blit_cover, cpu_copy_layouts, fresh_pages, copied_log, in_place and linear_plan tests: 123 of 123 passed. - Validation runtime_lane shadow, padded and linear: 91 of 91 passed. clippy --all-targets --all-features -D warnings passed plain and with --cfg m2v_gpu, and cargo fmt was run. RUNTIME_VERSION and the runtime-sources pin are unchanged. (cherry picked from commit 73b62e05fd00b75b5ef1ff74952307fc68020197) (cherry picked from commit 5d3ee8792e7a7f65985cf806de1e829e11b6e8c5) --- crates/metal2vulkan-runtime/src/shadow.rs | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index 5a35627d3..26875cabb 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -2654,7 +2654,8 @@ impl ShadowSync { /// and **Tracking**). Another queue's work comes between only where the command buffer's batches /// split. So the carry is dropped wherever that no longer follows: the encoder that published it /// did not end holding the truth, a command of the command buffer wrote the truth another way -/// ([`ShadowSync::wrote`]), work the encoder records apart stands between +/// ([`ShadowSync::wrote`]), a copy back or merge of another shadowed texture over the same bytes +/// wrote them (`ShadowSync::copy_back`), work the encoder records apart stands between /// ([`ShadowSync::leave`]), an encoder waits for a fence (`EncoderCore::ordered_other_work`), or /// the next encoder starts another batch: after an event or semaphore wait, after a signal, or /// past a host step (`State::shadows_to_carry`), where another queue's batch may run first, as @@ -2801,18 +2802,19 @@ impl ShadowSync { } } - /// Copy back entry `i` (dirty): its image's bytes replace the buffer's, so every other - /// texture over them is no longer current, or, where the copy back covers a rectangle of - /// texels laid out as its own, stale there alone (the module's rules). One kept is merged - /// instead: one kept before a command that wrote its bytes two ways is then no longer current - /// either, as the buffer holds the other way's writes too, and leaves every other texture - /// over the bytes no longer current; one kept as it merges back still holds what the - /// encoder's commands left in them, and its merge changes only words they changed through - /// it, inside the area they wrote in where they bound it, so it leaves the others stale there - /// alone, as a copy back of that area does. A copy back covers only the - /// area the commands wrote in, where they bound it: the image's other texels are what the last - /// copy in left, which the buffer still holds (a write of its bytes another way leaves the - /// image stale, and copies in again before the next use). + /// Copy back entry `i` (dirty): its image's bytes replace the buffer's, so every other texture + /// over them is no longer current, or, where the copy back covers a rectangle of texels laid + /// out as its own, stale there alone (the module's rules); one the encoder has not met that its + /// command buffer carries ([`ShadowCarry`]) is carried no further, and copies in at its next + /// use. One kept is merged instead: one kept before a command that wrote its bytes two ways is + /// then no longer current either, as the buffer holds the other way's writes too, and leaves + /// every other texture over the bytes no longer current; one kept as it merges back still holds + /// what the encoder's commands left in them, and its merge changes only words they changed + /// through it, inside the area they wrote in where they bound it, so it leaves the others stale + /// there alone, as a copy back of that area does. A copy back covers only the area the commands + /// wrote in, where they bound it: the image's other texels are what the last copy in left, + /// which the buffer still holds (a write of its bytes another way leaves the image stale, and + /// copies in again before the next use). fn copy_back(&mut self, i: usize, copies: &mut Vec>) { let entry = &mut self.entries[i]; let two_ways = entry.two_ways; @@ -2853,6 +2855,8 @@ impl ShadowSync { other.changed(&written, changed, Lost::OtherShadow); } } + // A carried image holds the bytes as they were before this write. + (self.carry.entries).retain(|carried| !carried.copies.shares_truth(&written)); } /// Go on with a run of passes over the texture of `copies` (`pass_runs.rs`): its image, which