From 9d024acefeea2ff548781dad225caa494e748d7d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 09:26:11 -0800 Subject: [PATCH] Runtime: a tiled texture's take whose encoder runs nothing is given back A tiled texture of more than one level over a buffer takes its copy of the bytes at the first use encoded after its making or a did_modify_data, which clears its mark. Where that encoder runs nothing because its command buffer failed before it ran (committed as failed, an encoder a failure names, a protection fault, a submission that fails EncodedUseRebound or DetachedBacking, a hang's discard or recovery, a predication record that drops it), the copy was never taken but the mark stayed cleared, so every later use read zeros or an older copy until the next did_modify_data. The take is now given back where the runtime knows its recording will not run: State::fail_part, the one point every such failure releases a part through. The mark is an AtomicU64 whose low bit is the mark and whose upper bits count the takes; LevelsCopy::take returns the take's number, and give_back(number) sets the mark again only where that take is still the latest and nothing set the mark since (a later take holds its own copy; a did_modify_data since already asks for one), so giving back twice, or after a later take, changes nothing. shadow::Takes now holds each take's number and a weak hold of the texture's copies; every ended encoder's record keeps its takes (not only one whose log is kept), and a recorded segment carries its own core's takes, both given back by fail_part. ShadowSync::forget gives its takes back by number too. An encoder split into parts at a host step gives back every take of its own where any of its parts runs nothing, so a take that ran in an earlier part is taken again at the next use when only a later batch fails; that gap is in docs/FAST-PATHS.md's known gaps. docs/API.md and shadow.rs state the rule. Tests: tests::shadow gains a unit test of the give-back's rules (latest take, a later take, a mark set since, twice, forget). The device tests of the previous commit fail without this change; the runs of both commits are in the report. --- .../src/command_buffer/checks.rs | 14 +- .../src/command_buffer/first_submission.rs | 7 +- .../src/command_buffer/mod.rs | 47 +++--- .../src/command_buffer/recovery.rs | 7 +- .../src/command_buffer/rerun.rs | 1 + crates/metal2vulkan-runtime/src/encoder.rs | 20 ++- crates/metal2vulkan-runtime/src/shadow.rs | 136 +++++++++++++----- .../src/sparse/encoder.rs | 2 +- .../metal2vulkan-runtime/src/tests/shadow.rs | 51 ++++++- .../src/used_resources.rs | 5 +- docs/API.md | 4 +- docs/FAST-PATHS.md | 9 ++ 12 files changed, 235 insertions(+), 68 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command_buffer/checks.rs b/crates/metal2vulkan-runtime/src/command_buffer/checks.rs index a699a7c30..1066b7b6b 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/checks.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/checks.rs @@ -271,13 +271,16 @@ impl State { } let from = self.batch_range(index).start; let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = self; for part in recorded[from..] .iter_mut() .filter(|part| encoders.contains(&part.encoder)) { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } if self.failure.is_none() { self.failure = Some(CommandBufferError::Injected(code)); @@ -297,10 +300,13 @@ impl State { } let from = self.batch_range(index).start; let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = self; for part in &mut recorded[from..] { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/first_submission.rs b/crates/metal2vulkan-runtime/src/command_buffer/first_submission.rs index 0cfda9504..0320b2200 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/first_submission.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/first_submission.rs @@ -579,14 +579,17 @@ impl State { } due.sort_by_key(|(encoders, _)| encoders.start); let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = self; let failing = |encoder: &usize| due.iter().any(|(encoders, _)| encoders.contains(encoder)); for part in recorded[range.start..] .iter_mut() .filter(|part| failing(&part.encoder)) { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } self.unrunnable .extend(due.iter().flat_map(|(encoders, _)| encoders.clone())); diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 0a56194ee..e7ee67ff4 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -1049,14 +1049,16 @@ impl State { /// with the event waits and signals encoded before it, and what it made resident joins the /// command buffer's residency (`Residency::join_encoder`): its heaps now, its resources where /// the command buffer's work first reads that residency - /// ([`joined_residency`](Self::joined_residency)). Its place among the ended encoders, which - /// each of its recordings' parts carries. + /// ([`joined_residency`](Self::joined_residency)). `takes` is what its first uses of copies + /// taken at use decided (`shadow::Takes`): an encoding of it again takes each as they did, + /// and a part of it that runs nothing gives its takes back ([`fail_part`](Self::fail_part)). + /// Its place among the ended encoders, which each of its recordings' parts carries. pub(super) fn encoder_ended( &mut self, kind: EncoderKind, keys: BTreeSet, residency: Option, - taken: Taken, + (taken, takes): (Taken, Takes), ) -> usize { if let Some(ended) = &residency { self.residency @@ -1072,7 +1074,7 @@ impl State { residency, taken, replay: None, - takes: Takes::default(), + takes, executions: 0..0, held: Vec::new(), unlogged: None, @@ -1110,15 +1112,12 @@ impl State { } /// Keep `replay`, the log of ended encoder `encoder`, whose executions take `executions` - /// among the command buffer's, with `takes`, what its first uses of copies taken at use - /// decided, which an encoding of it again takes as they did. One of no such encoder comes - /// back, for the caller to let go of without this lock (what it holds may be the last hold of - /// a no-copy buffer). + /// among the command buffer's. One of no such encoder comes back, for the caller to let go + /// of without this lock (what it holds may be the last hold of a no-copy buffer). pub(super) fn keep_replay( &mut self, encoder: usize, replay: Option, - takes: Takes, executions: Range, ) -> Option { let replay = replay?; @@ -1126,7 +1125,6 @@ impl State { return Some(replay); }; record.replay = Some(Arc::new(replay)); - record.takes = takes; record.executions = executions; None } @@ -1831,13 +1829,16 @@ impl State { /// (a code-0 `commit_failed` has none). fn fail(&mut self, failure: Failure) { let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = self; for part in recorded .iter_mut() .filter(|part| failure.encoders.fails(part.encoder)) { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } match failure.signals { FailedSignals::Every => {} @@ -1863,21 +1864,35 @@ impl State { /// failed encoder's ([`fail_part`](Self::fail_part)). pub(super) fn leave_out_ended(&mut self, first: usize) { let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = self; for part in recorded.iter_mut().filter(|part| part.encoder >= first) { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } } /// A failed encoder's recorded `part` runs nothing: its segments (with their recordings), /// sparse updates and writes into ASTC carriers go to `retained`, released with what the /// command buffer retained without this lock, its `VkCommandBuffer`s are left unsubmitted, - /// and it keeps its place among the batches with its fences' waits and updates alone. - pub(super) fn fail_part(part: &mut Recorded, retained: &mut Vec>) { + /// and it keeps its place among the batches with its fences' waits and updates alone. The + /// copies taken at use its encoder (among the `ended`) and its segments' recordings took are + /// given back, so the next use takes each (`shadow::Takes::give_back`): every one its encoder + /// took, whichever of its parts recorded it. + pub(super) fn fail_part( + part: &mut Recorded, + ended: &[EncoderRecord], + retained: &mut Vec>, + ) { + if let Some(record) = ended.get(part.encoder) { + record.takes.give_back(); + } for segment in part.segments.drain(..) { retained.push(Box::new((segment.work, segment.inputs))); if let Some(mut done) = segment.recorded { + done.takes.give_back(); retained.extend(done.retained.into_entry()); done.taken.retain_into(retained); } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs b/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs index e9ba323b7..b813dfdc1 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/recovery.rs @@ -109,7 +109,10 @@ pub(super) fn recovered(buffer: &CommandBufferShared) -> Option { pub(super) fn cut(buffer: &CommandBufferShared, check: &LoopCheck) { let mut state = lock(&buffer.state); let State { - recorded, retained, .. + recorded, + retained, + ended, + .. } = &mut *state; let holds_check = |part: &Recorded| { part.segments.iter().any(|segment| match &segment.work { @@ -121,6 +124,6 @@ pub(super) fn cut(buffer: &CommandBufferShared, check: &LoopCheck) { return; }; for part in &mut recorded[from..] { - State::fail_part(part, retained); + State::fail_part(part, ended, retained); } } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/rerun.rs b/crates/metal2vulkan-runtime/src/command_buffer/rerun.rs index 9424c2169..d34e75e57 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/rerun.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/rerun.rs @@ -470,6 +470,7 @@ fn nothing() -> RecordedSegment { taken: Default::default(), resident_misses: 0, faults: ReplayFaults::default(), + takes: Default::default(), } } diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 7bbe067f7..5b892082c 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -69,7 +69,7 @@ use super::retained::Retained; use super::row_copies::RowCopies; use super::shadow::{ CopyCause, CopyGate, GatedCopyIn, LaterRun, LevelsCopy, ShadowCopies, ShadowCopy, ShadowSync, - ShadowUse, Step, Within, + ShadowUse, Step, Takes, Within, }; use super::shadow_copy_in::CopyIn; use super::shadow_merge::{MergeLease, ShadowMerge}; @@ -319,6 +319,9 @@ pub(super) struct RecordedSegment { /// Why some of its commands ran nothing: records the host step could not run /// (`indirect_execute.rs`), which the execution report states. pub(super) faults: ReplayFaults, + /// The copies taken at use its recording took (`shadow::Takes`), given back where its part + /// runs nothing (`State::fail_part`). + pub(super) takes: Takes, } impl RecordedSegment { @@ -334,6 +337,7 @@ impl RecordedSegment { taken: Default::default(), resident_misses: 0, faults: ReplayFaults::default(), + takes: Takes::default(), } } @@ -347,6 +351,7 @@ impl RecordedSegment { self.taken.join(other.taken); self.resident_misses = self.resident_misses.wrapping_add(other.resident_misses); self.faults = ReplayFaults(self.faults.0 | other.faults.0); + self.takes.join(other.takes); } } @@ -2793,8 +2798,8 @@ impl EncoderCore { } // What the encoder leaves the command buffer's later encoders (`shadow::ShadowCarry`). state.shadows_carried(shadows.ended()); - // What its first uses of copies taken at use decided, for an encoding of it again - // (`shadow::Takes`). + // What its first uses of copies taken at use decided, for an encoding of it again and + // for a part of it that runs nothing to give back (`shadow::Takes`). let takes = shadows.into_takes(); if !first_copies.is_empty() { let again = state.shadows_copied_in(&first_copies); @@ -2805,7 +2810,7 @@ impl EncoderCore { } state.use_heaps(heaps); state.routes.join(routes); - let encoder = state.encoder_ended(kind, keys, residency, taken); + let encoder = state.encoder_ended(kind, keys, residency, (taken, takes)); if let Some(why) = unlogged { state.encoder_unlogged(encoder, why); } @@ -2828,7 +2833,7 @@ impl EncoderCore { // One part, its executions these: a log not kept comes back, to go without the lock. let logged = replay.as_ref().map(Replay::bytes); let executed = base..base + executions.len(); - let unkept = state.keep_replay(encoder, replay, takes, executed); + let unkept = state.keep_replay(encoder, replay, executed); if let (Some(bytes), None) = (logged, &unkept) { open.command_buffer .device() @@ -3031,6 +3036,7 @@ impl EncoderCore { taken, resident_misses: 0, faults: ReplayFaults::default(), + takes: Takes::default(), } } @@ -3067,6 +3073,7 @@ impl EncoderCore { taken, resident_misses: 0, faults: ReplayFaults::default(), + takes: Takes::default(), }) } @@ -3092,6 +3099,7 @@ impl EncoderCore { executions, keys, taken, + shadows, .. } = self; ended.push(raw); @@ -3104,6 +3112,7 @@ impl EncoderCore { taken, resident_misses: 0, faults: ReplayFaults::default(), + takes: shadows.into_takes(), }) } @@ -3345,6 +3354,7 @@ impl Captured { taken: self.taken, resident_misses: 0, faults: ReplayFaults::default(), + takes: Takes::default(), }) } } diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index 2289c36db..8b74bd576 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -246,7 +246,13 @@ //! out of its command buffer's work ([`ShadowSync::forget`]) leaves the copy to the next use. An //! encoder a rebind encodes again (`command_buffer/reencode.rs`), whose first recording is never //! submitted, takes the copy where its first encoding took it and leaves the mark as it finds it -//! ([`Takes`]). +//! ([`Takes`]). A take whose encoder runs nothing, its command buffer failed before it ran +//! (committed as failed, an encoder a failure names, a submission that fails), is given back as +//! its part is failed (`State::fail_part`, [`Takes::give_back`]): the next use takes the copy, +//! where that take is still the texture's latest and nothing set the mark since. An encoder of +//! several parts (split at a host step) gives back every take of its own where any of its parts +//! runs nothing, so one that ran in an earlier part is taken again at the next use +//! (docs/FAST-PATHS.md, known gaps). //! For a format `generate_mipmaps` leaves as it is or refuses, the copy in moves the first level //! alone and the later levels keep what they hold (docs/FAST-PATHS.md, known gaps). @@ -280,8 +286,8 @@ use ash::vk; use metal2vulkan::error::{Feature, Unsupported}; use metal2vulkan::metal::PixelFormat; use std::ops::Range; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; -use std::sync::Arc; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Weak}; /// What a shadowed texture's copies need: its truth, its shadow, and the copies between them. pub(super) struct ShadowCopies { @@ -324,16 +330,21 @@ pub(super) struct ShadowCopies { /// makes them ([`LaterLevels`]), before that command; no other command copies anything in, and /// nothing it writes is copied back. pub(super) struct LevelsCopy { - /// Whether the next use takes the copy: set as the texture is made, by - /// `Texture::did_modify_data` and by a `replace_region` of the first level, taken by the - /// encoder whose command uses the texture first after. - stale: AtomicBool, + /// Its mark and its takes: the lowest bit set where the next use takes the copy (as the + /// texture is made, by `Texture::did_modify_data` and by a `replace_region` of the first + /// level), cleared by the encoder whose command uses the texture first after, which takes it; + /// the bits above count the takes, so a take given back is known for the latest + /// ([`give_back`](Self::give_back)). + mark: AtomicU64, /// How each take makes the later levels from the first after the copy. later: LaterLevels, /// The image's aspects. aspects: vk::ImageAspectFlags, } +/// The bit of [`LevelsCopy::mark`] set where the next use takes the copy. +const STALE: u64 = 1; + /// How a copy taken at use makes its later levels from its first, right after its copy into the /// first: as `generate_mipmaps` makes them for the texture's format (`mipmap_plan.rs`). pub(super) enum LaterLevels { @@ -414,7 +425,7 @@ impl LevelsCopy { /// `aspects`. pub(super) fn new(later: LaterLevels, aspects: vk::ImageAspectFlags) -> LevelsCopy { LevelsCopy { - stale: AtomicBool::new(true), + mark: AtomicU64::new(STALE), later, aspects, } @@ -423,13 +434,38 @@ impl LevelsCopy { /// Take the copy at the next use again (`Texture::did_modify_data`, and a `replace_region` of /// the first level, which writes the bytes). pub(super) fn mark_stale(&self) { - self.stale.store(true, Ordering::Release); + self.mark.fetch_or(STALE, Ordering::AcqRel); } /// Whether this use takes the copy: once after each [`mark_stale`](Self::mark_stale) (and - /// the making). - fn take(&self) -> bool { - self.stale.swap(false, Ordering::AcqRel) + /// the making), which it clears. The take's number where it does, counted from 1. + fn take(&self) -> Option { + let mut mark = self.mark.load(Ordering::Acquire); + loop { + if mark & STALE == 0 { + return None; + } + let number = (mark >> 1) + 1; + match (self.mark).compare_exchange_weak( + mark, + number << 1, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => return Some(number), + Err(now) => mark = now, + } + } + } + + /// Give take `number` back, its recording never run (`State::fail_part`, or left out, + /// [`ShadowSync::forget`]): the mark is set again, so the next use takes the copy, where that + /// take is still the latest and nothing set the mark since. A later take holds the copy its + /// own use takes; a mark set since asks the next use for it already. + pub(super) fn give_back(&self, number: u64) { + let taken = number << 1; + let _ = + (self.mark).compare_exchange(taken, taken | STALE, Ordering::AcqRel, Ordering::Acquire); } /// Whether a take records a run made for it ([`run`](Self::run)): the compute filter's or @@ -2917,10 +2953,11 @@ struct Entry { /// reads only inside a rectangle and writes none, the rest left here. `None` where none /// changed. stale: Option<[u32; 4]>, - /// The encoder took the copy at use of a texture whose shadow is one - /// ([`ShadowCopies::levels`]): given back where what it recorded is left out - /// ([`ShadowSync::forget`]). - took: bool, + /// The number of the take of the copy at use of a texture whose shadow is one + /// ([`ShadowCopies::levels`]) the encoder's use took by the mark ([`LevelsCopy::take`]): + /// given back where what it recorded is left out ([`ShadowSync::forget`]). `None` where it + /// took none so, an encoding again's take among them. + took: Option, } impl Entry { @@ -3126,16 +3163,41 @@ impl Default for ShadowSync { /// What an encoder's first uses of textures whose shadows are copies taken at use /// ([`ShadowCopies::levels`]) decided, in the order it met them: each texture's shadow key, and -/// whether that use took the copy. A texture's first use in an encoder decides, and so does its -/// first use after work the encoder records apart ([`ShadowSync::leave`]). -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub(super) struct Takes(Vec<(ResourceKey, bool)>); +/// the take where that use took the copy. A texture's first use in an encoder decides, and so +/// does its first use after work the encoder records apart ([`ShadowSync::leave`]). +#[derive(Clone, Debug, Default)] +pub(super) struct Takes(Vec<(ResourceKey, Option)>); + +/// A take of a copy taken at use: the texture's copies, and the take's number +/// ([`LevelsCopy::take`]). +#[derive(Clone, Debug)] +pub(super) struct Took { + copies: Weak, + number: u64, +} impl Takes { /// None decided: an encoder that met no copy taken at use. pub(super) fn clear(&mut self) { self.0.clear(); } + + /// `other`'s, decided after these, joined after them. + pub(super) fn join(&mut self, other: Takes) { + self.0.extend(other.0); + } + + /// Give back every take: the recording that carries them never runs (`State::fail_part`). + /// Each texture's next use takes the copy again, where its take here is still its latest + /// ([`LevelsCopy::give_back`]). + pub(super) fn give_back(&self) { + for took in self.0.iter().filter_map(|(_, took)| took.as_ref()) { + let copies = took.copies.upgrade(); + if let Some(levels) = copies.as_ref().and_then(|copies| copies.levels.as_ref()) { + levels.give_back(took.number); + } + } + } } /// How an encoder's first uses of a copy taken at use decide whether they take it. @@ -3151,19 +3213,24 @@ enum TakeBy { } impl TakeBy { - /// Whether this first use in the encoder of the texture whose shadow key is `key` and whose - /// copy is `levels` takes the copy. - fn take(&mut self, key: ResourceKey, levels: &LevelsCopy) -> bool { + /// Whether this first use in the encoder of `copies`, whose copy is `levels`, takes the copy, + /// and the take's number where it takes it by the mark (none for an encoding again's). + fn take(&mut self, copies: &Arc, levels: &LevelsCopy) -> (bool, Option) { + let key = copies.shadow_key; match self { TakeBy::Mark(noted) => { - let took = levels.take(); + let number = levels.take(); + let took = number.map(|number| Took { + copies: Arc::downgrade(copies), + number, + }); noted.0.push((key, took)); - took + (number.is_some(), number) } // A use the first encoding did not meet takes nothing, as it did not. TakeBy::Again(first) => match first.0.iter().position(|(met, _)| *met == key) { - Some(at) => first.0.remove(at).1, - None => false, + Some(at) => (first.0.remove(at).1.is_some(), None), + None => (false, None), }, } } @@ -3461,8 +3528,10 @@ impl ShadowSync { // A copy taken at use is copied in at the first use after its making or its // `did_modify_data`, wherever it is; else its image holds what it took. Encoded // again, where its first encoding took it ([`TakeBy`]). - let took = (copies.levels.as_ref()) - .is_some_and(|levels| self.takes.take(copies.shadow_key, levels)); + let (took, number) = match &copies.levels { + Some(levels) => self.takes.take(copies, levels), + None => (false, None), + }; let (standing, held, stale) = match &copies.levels { Some(_) if took => (Standing::Unread, None, None), Some(_) => (Standing::Current, None, None), @@ -3480,7 +3549,7 @@ impl ShadowSync { stale, carried: carried.is_some(), run: false, - took, + took: number, }); self.entries.len() - 1 } @@ -3994,10 +4063,9 @@ impl ShadowSync { pub(super) fn forget(&mut self) { // A copy taken at use whose copy in is left out is taken at the next use instead. An // encoding again took none by the mark, and gives none back to it. - let marked = matches!(self.takes, TakeBy::Mark(_)); - for entry in (self.entries.iter()).filter(|entry| marked && entry.took) { - if let Some(levels) = &entry.copies.levels { - levels.mark_stale(); + for entry in &self.entries { + if let (Some(number), Some(levels)) = (entry.took, &entry.copies.levels) { + levels.give_back(number); } } self.drop_entries(); diff --git a/crates/metal2vulkan-runtime/src/sparse/encoder.rs b/crates/metal2vulkan-runtime/src/sparse/encoder.rs index 0629f18e0..592d88a54 100644 --- a/crates/metal2vulkan-runtime/src/sparse/encoder.rs +++ b/crates/metal2vulkan-runtime/src/sparse/encoder.rs @@ -440,7 +440,7 @@ impl ResourceStateCommandEncoder { EncoderKind::ResourceState, keys, None, - crate::rebind::Taken::default(), + (crate::rebind::Taken::default(), Default::default()), ); state.end_sparse_encoder( &open.command_buffer, diff --git a/crates/metal2vulkan-runtime/src/tests/shadow.rs b/crates/metal2vulkan-runtime/src/tests/shadow.rs index 0e4faf6b4..7feabef51 100644 --- a/crates/metal2vulkan-runtime/src/tests/shadow.rs +++ b/crates/metal2vulkan-runtime/src/tests/shadow.rs @@ -2438,7 +2438,8 @@ fn an_encoding_again_takes_a_copy_taken_at_use_where_its_first_encoding_took_it( let mut again = ShadowSync::again(took.clone()); assert_eq!(first_use(&mut again), [Moved::Into(k)]); assert_eq!(first_use(&mut again), []); - assert_eq!(again.into_takes(), Default::default()); + // An encoding again notes no take of its own. + assert_eq!(first_use(&mut ShadowSync::again(again.into_takes())), []); assert_eq!(first_use(&mut ShadowSync::default()), []); // After a `did_modify_data`, the later one encoded again takes nothing and the first takes it, // as each did; the mark stays for the next use encoded. @@ -2448,3 +2449,51 @@ fn an_encoding_again_takes_a_copy_taken_at_use_where_its_first_encoding_took_it( assert_eq!(first_use(&mut ShadowSync::default()), [Moved::Into(k)]); assert_eq!(first_use(&mut ShadowSync::default()), []); } + +/// A take whose recording never runs is given back (`Takes::give_back`, as `State::fail_part` +/// gives back a failed encoder's): the next use takes the copy again, where that take is still the +/// latest and the mark was not set since. A take a later one followed, or one of an encoder that +/// took none, gives nothing back; nor does giving back twice. What an encoder leaves out +/// (`ShadowSync::forget`) is given back so too. +#[test] +fn a_take_given_back_is_taken_again_at_the_next_use() { + let t = taken_at_use(ResourceKey::new(), 0..4096); + let k = t.shadow_key; + let levels = t.levels.as_ref().expect("a copy taken at use"); + let first_use = |sync: &mut ShadowSync| moved(&planned(sync, &mut 0, &[(&t, false)], &[])); + let encode = || { + let mut sync = ShadowSync::default(); + let moved = first_use(&mut sync); + (moved, sync.into_takes()) + }; + // Given back: the next use takes it, once. + let (moved, failed) = encode(); + assert_eq!(moved, [Moved::Into(k)]); + failed.give_back(); + let (moved, _) = encode(); + assert_eq!(moved, [Moved::Into(k)]); + assert_eq!(encode().0, []); + // A later take holds the copy: giving the first back again changes nothing. + failed.give_back(); + assert_eq!(encode().0, []); + // An encoder that took none gives nothing back. + let (moved, none) = encode(); + assert_eq!(moved, []); + none.give_back(); + assert_eq!(encode().0, []); + // A mark set since the take asks the next use for it once, the give-back adding nothing. + levels.mark_stale(); + let (moved, taken) = encode(); + assert_eq!(moved, [Moved::Into(k)]); + levels.mark_stale(); + taken.give_back(); + assert_eq!(encode().0, [Moved::Into(k)]); + assert_eq!(encode().0, []); + // Left out by the encoder that took it: the next use takes it. + levels.mark_stale(); + let mut sync = ShadowSync::default(); + assert_eq!(first_use(&mut sync), [Moved::Into(k)]); + sync.forget(); + assert_eq!(encode().0, [Moved::Into(k)]); + assert_eq!(encode().0, []); +} diff --git a/crates/metal2vulkan-runtime/src/used_resources.rs b/crates/metal2vulkan-runtime/src/used_resources.rs index 610f3c2f1..edf90ee74 100644 --- a/crates/metal2vulkan-runtime/src/used_resources.rs +++ b/crates/metal2vulkan-runtime/src/used_resources.rs @@ -115,8 +115,9 @@ pub(super) struct EncoderRecord { /// encodes it again from them. Kept, with what they hold, until the command buffer's release /// (`CommandBufferShared::complete`); `None` where it cannot be encoded again, or need not. pub(super) replay: Option>, - /// What its first uses of copies taken at use decided as the program encoded it, where its - /// log is kept: an encoding of it again takes each as they did (`shadow::Takes`). + /// What its first uses of copies taken at use decided as the program encoded it: an + /// encoding of it again takes each as they did, and a part of it that runs nothing gives its + /// takes back (`shadow::Takes`, `State::fail_part`). pub(super) takes: crate::shadow::Takes, /// Its executions' place among the command buffer's (`State::executions`). pub(super) executions: std::ops::Range, diff --git a/docs/API.md b/docs/API.md index 4788aa116..c1278137d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -456,7 +456,9 @@ texel picks. The copy is taken as that command is encoded and runs in its comman the GPU, so it reads the bytes as they are when the command buffer runs. A rebind that encodes the encoder again before its command buffer's first submission (`Buffer::rebind_bytes_no_copy`) takes the copy in the encoding again where the first encoding took it, and a `did_modify_data` made since -is left to the next use encoded. Between takes no write of +is left to the next use encoded. A take whose encoder runs nothing, its command buffer having failed +before it ran (committed as failed, or a submission that fails), is given back: the next use takes +the copy. Between takes no write of the bytes reaches a level, and nothing the GPU writes into a level reaches the bytes or the other levels. `get_bytes` and `replace_region` of the first level read and write the buffer's bytes, as the Macs' do, and of a later level the image (an accepted difference, section 10). While the diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index c780ca45e..de459a307 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -1591,3 +1591,12 @@ compares with Metal's. compute filter or picks need a scratch past the device's limits. Every other format's later levels are made as `generate_mipmaps` makes them: the blit chain, the compute filter or the integer picks. + +- **A tiled texture's take in an encoder whose later part alone runs nothing** + (`Buffer::new_tiled_texture`, more than one level, `shadow.rs`'s **Copies taken at use**, + `State::fail_part`): a take whose encoder runs nothing is given back, so the next use takes the + copy. An encoder split into parts at a host step (an execution of indirect commands the GPU may + write) gives back every take of its own where any of its parts runs nothing, so a take that ran + in an earlier part, where a failure of a later batch fails only a later one, is taken again at + the next use: that use reads the bytes as they are then, where Metal's levels keep the copy that + ran.