diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 40b834b60..0a56194ee 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -98,7 +98,7 @@ use super::replay::Replay; use super::residency::Residency; use super::resource::{ObjectId, ResourceKey}; use super::scheduler::{BatchSync, Hand, StepWaits}; -use super::shadow::ShadowCarry; +use super::shadow::{ShadowCarry, Takes}; use super::spares::CommandPool; use super::sparse::encoder::SparseUpdate; use super::sparse::indirect; @@ -851,6 +851,10 @@ pub(super) struct State { /// The keeps the encoder being encoded again records after itself, where it leaves a run of /// passes another pass went on in (`pass_runs.rs`, `command_buffer/reencode.rs`). pub(super) replaying_keeps: Vec, + /// What the first encoding of the encoder being encoded again decided of its copies taken at + /// use, which its encoding again takes as they did (`shadow::Takes`, + /// `command_buffer/reencode.rs`). + pub(super) replaying_takes: Takes, /// The pools of its commands' set 0 (`descriptor_arena.rs`), given back or released with /// the uploads. pub(super) descriptors: DescriptorArena, @@ -1068,6 +1072,7 @@ impl State { residency, taken, replay: None, + takes: Takes::default(), executions: 0..0, held: Vec::new(), unlogged: None, @@ -1105,12 +1110,15 @@ impl State { } /// Keep `replay`, the log of ended encoder `encoder`, whose executions take `executions` - /// 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). + /// 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). pub(super) fn keep_replay( &mut self, encoder: usize, replay: Option, + takes: Takes, executions: Range, ) -> Option { let replay = replay?; @@ -1118,6 +1126,7 @@ impl State { return Some(replay); }; record.replay = Some(Arc::new(replay)); + record.takes = takes; record.executions = executions; None } @@ -2190,6 +2199,7 @@ impl CommandBuffer { carry_batches: 1, pass_runs: PassRuns::default(), replaying_keeps: Vec::new(), + replaying_takes: Takes::default(), descriptors: DescriptorArena::default(), status_words: None, mesh_grid_past: false, diff --git a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs index a396c7d98..5f307ca61 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs @@ -43,6 +43,11 @@ //! its host step needs. Work a host step recorded again since (with the barriers between its //! commands, `gpu_execute::Rerecord`, or as a route outgrew it, `DeferredPass::outgrown`) //! keeps that step's recording. +//! - **Copies taken at use.** A tiled texture's copy of its bytes taken at use (`shadow.rs`) is +//! taken by the first use encoded after its mark was set, which clears it. An encoder encoded +//! again takes it where the use in its place in its first encoding took it, whose recording is +//! never submitted, and leaves the mark as it finds it: set by a `did_modify_data` since, it is +//! the next use's to take (`shadow::Takes`). //! - **Runs of passes.** An encoder encoded again that was a pass of a run of passes merged once //! (a shadowed texture's or a linear attachment's, `pass_runs.rs`) leaves the run once it is //! put in place: the merge of the run's pass before it, which it took over, records after all, @@ -161,10 +166,17 @@ impl CommandBufferShared { }) .collect(); state.replaying_keeps = runs.iter().filter_map(|run| run.keep_after()).collect(); + // Its first encoding's takes of copies taken at use, taken again where they were: + // that encoding's recording is never submitted (`shadow::Takes`). + state.replaying_takes = state.ended[at].takes.clone(); (state.residency_before(at, replay.kind()), runs) }; let run = replay.run(&handle, residency); - lock(&self.state).replaying_keeps.clear(); + { + let mut state = lock(&self.state); + state.replaying_keeps.clear(); + state.replaying_takes.clear(); + } // Its steps' gates listed as the commit lists the first encoding's. let run = { let mut state = lock(&self.state); diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index a1f16bdf9..7bbe067f7 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -726,6 +726,10 @@ impl EncoderCore { ) -> Result { let mut core = EncoderCore::segment_raw(command_buffer, ordered)?; core.replaying = true; + // Its first uses of copies taken at use take them where its first encoding's did, whose + // recording is never submitted (`shadow::Takes`, `command_buffer/reencode.rs`). + let first = command_buffer.state().replaying_takes.clone(); + core.shadows = ShadowSync::again(first); Ok(core) } @@ -2789,6 +2793,9 @@ 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`). + let takes = shadows.into_takes(); if !first_copies.is_empty() { let again = state.shadows_copied_in(&first_copies); if again > 0 { @@ -2820,7 +2827,8 @@ impl EncoderCore { let base = state.executions.len(); // 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 unkept = state.keep_replay(encoder, replay, base..base + executions.len()); + let executed = base..base + executions.len(); + let unkept = state.keep_replay(encoder, replay, takes, executed); if let (Some(bytes), None) = (logged, &unkept) { open.command_buffer .device() diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index f7b61bceb..2289c36db 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -243,7 +243,10 @@ //! they are when the command buffer runs, after its commit. Every other use copies nothing in, //! whatever wrote the bytes, and nothing written through the image is copied back or merged: it //! stays in the image, as a Mac's texture's own memory keeps it. A use whose encoding is left -//! out of its command buffer's work ([`ShadowSync::forget`]) leaves the copy to the next use. +//! 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`]). //! 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). @@ -3110,15 +3113,58 @@ pub(super) struct ShadowSync> { /// How the encoder's first copies in so far stood against their images' last standing, which /// [`tally`](Self::tally) hands to the counters. tally: CarryTally, + /// How its first uses of a copy taken at use decide whether they take it ([`TakeBy`]). + takes: TakeBy, } impl Default for ShadowSync { /// No texture used yet, and nothing carried: every texture copies in at its first use. fn default() -> Self { - ShadowSync { - entries: Vec::new(), - carry: ShadowCarry::default(), - tally: CarryTally::default(), + ShadowSync::carrying(ShadowCarry::default()) + } +} + +/// 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)>); + +impl Takes { + /// None decided: an encoder that met no copy taken at use. + pub(super) fn clear(&mut self) { + self.0.clear(); + } +} + +/// How an encoder's first uses of a copy taken at use decide whether they take it. +enum TakeBy { + /// As the program encodes it: where the texture's mark asks ([`LevelsCopy::take`], which + /// clears it), each decision noted. + Mark(Takes), + /// Encoded again (`command_buffer/reencode.rs`): as the use in its place in the first + /// encoding decided, the first encoding's decisions not met yet held here. The first + /// recording is never submitted, so its take is this one's; the mark is left as it is found, + /// for the next use encoded after a later `Texture::did_modify_data`. + Again(Takes), +} + +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 { + match self { + TakeBy::Mark(noted) => { + let took = levels.take(); + noted.0.push((key, took)); + took + } + // 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, + }, } } } @@ -3132,6 +3178,27 @@ impl ShadowSync { entries: Vec::new(), carry, tally: CarryTally::default(), + takes: TakeBy::Mark(Takes::default()), + } + } + + /// An encoder encoded again (`command_buffer/reencode.rs`), whose first encoding decided + /// `first` of its copies taken at use ([`Takes`]): each of its first uses of such a texture + /// takes the copy where the use in its place there took it, the texture's mark untouched. + /// Nothing is carried: every other texture copies in at its first use. + pub(super) fn again(first: Takes) -> ShadowSync { + ShadowSync { + takes: TakeBy::Again(first), + ..ShadowSync::default() + } + } + + /// What its first uses of copies taken at use decided, as the program encoded them, for an + /// encoding of the encoder again ([`again`](Self::again)); none for an encoding again. + pub(super) fn into_takes(self) -> Takes { + match self.takes { + TakeBy::Mark(noted) => noted, + TakeBy::Again(_) => Takes::default(), } } @@ -3392,8 +3459,10 @@ impl ShadowSync { None => (Standing::Unread, None, None), }; // 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. - let took = copies.levels.as_ref().is_some_and(LevelsCopy::take); + // `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 (standing, held, stale) = match &copies.levels { Some(_) if took => (Standing::Unread, None, None), Some(_) => (Standing::Current, None, None), @@ -3923,8 +3992,10 @@ impl ShadowSync { /// copy in there moved, and nothing written through one there ran. A later command copies in /// again. pub(super) fn forget(&mut self) { - // A copy taken at use whose copy in is left out is taken at the next use instead. - for entry in self.entries.iter().filter(|entry| entry.took) { + // 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(); } diff --git a/crates/metal2vulkan-runtime/src/tests/shadow.rs b/crates/metal2vulkan-runtime/src/tests/shadow.rs index 558ed8679..0e4faf6b4 100644 --- a/crates/metal2vulkan-runtime/src/tests/shadow.rs +++ b/crates/metal2vulkan-runtime/src/tests/shadow.rs @@ -5,8 +5,9 @@ use super::super::host_aliases::{self, Alias}; use super::super::key_hash::KeyMap; use super::super::resource::ResourceKey; use super::super::shadow::{ - self, Backing, CopyCause, ImageTruth, KeptArea, Lost, ShadowBuffer, ShadowCarry, ShadowCopies, - ShadowCopy, ShadowSource, ShadowSync, ShadowTarget, ShadowUse, Step, Within, + self, Backing, CopyCause, ImageTruth, KeptArea, LaterLevels, LevelsCopy, Lost, ShadowBuffer, + ShadowCarry, ShadowCopies, ShadowCopy, ShadowSource, ShadowSync, ShadowTarget, ShadowUse, Step, + Within, }; use super::super::shadow_merge::ScratchSpares; use super::super::tracker::{Access, Use, UseRange}; @@ -2404,3 +2405,46 @@ fn a_pass_storing_its_loaded_area_merges_in_whole_blocks() { assert!(joined[0].tile_stores); assert_eq!(made_for(&joined, &used), [(true, Some(area))]); } + +/// A shadowed texture over `bytes` of the buffer `buffer` whose shadow is a copy taken at use (a +/// tiled texture's of more than one level), its later levels kept. +fn taken_at_use(buffer: ResourceKey, bytes: Range) -> Arc { + let mut copies = Arc::into_inner(shadow(buffer, bytes)).expect("one holder"); + let aspects = vk::ImageAspectFlags::COLOR; + copies.levels = Some(LevelsCopy::new(LaterLevels::Kept, aspects)); + Arc::new(copies) +} + +/// A copy taken at use is taken by the first use encoded after its mark, which clears it. An +/// encoding of an encoder again (`command_buffer/reencode.rs`) takes it where its first encoding +/// took it and not where it did not, whatever the mark, which it leaves as it finds it: a +/// `did_modify_data` after the first encoding is the next use's to take. +#[test] +fn an_encoding_again_takes_a_copy_taken_at_use_where_its_first_encoding_took_it() { + 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)], &[])); + // The first encoding takes it at its first use alone. + let mut first = ShadowSync::default(); + assert_eq!(first_use(&mut first), [Moved::Into(k)]); + assert_eq!(first_use(&mut first), []); + let took = first.into_takes(); + // A later encoder takes nothing. + let mut later = ShadowSync::default(); + assert_eq!(first_use(&mut later), []); + let took_none = later.into_takes(); + // Encoded again, the first takes it again, and the mark stays clear. + 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()); + 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. + levels.mark_stale(); + assert_eq!(first_use(&mut ShadowSync::again(took_none)), []); + assert_eq!(first_use(&mut ShadowSync::again(took)), [Moved::Into(k)]); + assert_eq!(first_use(&mut ShadowSync::default()), [Moved::Into(k)]); + assert_eq!(first_use(&mut ShadowSync::default()), []); +} diff --git a/crates/metal2vulkan-runtime/src/used_resources.rs b/crates/metal2vulkan-runtime/src/used_resources.rs index d4244e1da..610f3c2f1 100644 --- a/crates/metal2vulkan-runtime/src/used_resources.rs +++ b/crates/metal2vulkan-runtime/src/used_resources.rs @@ -115,6 +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`). + pub(super) takes: crate::shadow::Takes, /// Its executions' place among the command buffer's (`State::executions`). pub(super) executions: std::ops::Range, /// The images it holds in their attachment layout (`texture/held_depth.rs`): the scheduler diff --git a/docs/API.md b/docs/API.md index 26b179553..9af2413e7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -453,7 +453,10 @@ after its making, or after `Texture::did_modify_data` (Metal's private `didModif level, then every later level made from the first as `generate_mipmaps` makes them for the format: the device's blit chain or the compute filter (`device-filtered-mipmaps`), or an integer format's texel picks. The copy is taken as that command is encoded and runs in its command buffer's order on -the GPU, so it reads the bytes as they are when the command buffer runs. Between takes no write of +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 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