diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index ee1a62c80..c2374cc96 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, Takes}; +use super::shadow::{Replaced, ShadowCarry, Takes}; use super::spares::CommandPool; use super::sparse::encoder::SparseUpdate; use super::sparse::indirect; @@ -855,16 +855,15 @@ 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, - /// What the recording a host step records a segment again in place of decided of its copies - /// taken at use, while the step records it: each core the step records into keeps those - /// takes where it meets them (`shadow::ShadowSync::over`, `EncoderCore::segment`). None - /// otherwise. The step holds the turn (`reencoding`), so no other recording into the command - /// buffer's pool meets it. - pub(super) step_takes: Takes, + /// What the recordings never submitted that a host step or a rebind records work again in + /// place of decided of their copies taken at use, while it records, that no core recorded in + /// their place met yet (`shadow::Replaced`): a host step's segment's, an encoder's first + /// encoding's, its segments' among it, which a rebind encodes again, and a segment's that a + /// rebind records again on the GPU alone. Each core recorded meanwhile takes from them where + /// it meets them (`shadow::ShadowSync::over`, `EncoderCore::segment`). None otherwise. The + /// step and the rebind hold the turn (`reencoding`), so no other recording into the command + /// buffer's pool meets them. + pub(super) replaced_takes: Option, /// The pools of its commands' set 0 (`descriptor_arena.rs`), given back or released with /// the uploads. pub(super) descriptors: DescriptorArena, @@ -1896,6 +1895,26 @@ impl State { } } + /// Batch `index` and every later one never run, the command buffer failed at that batch's + /// submission (`CommandBufferShared::never_runs_from`): the copies taken at use the encoders + /// of their parts and those parts' segments' recordings took are given back, so the next use + /// takes each (`shadow::Takes::give_back`), every one such an encoder took, whichever of its + /// parts recorded it, as [`fail_part`](Self::fail_part) gives them back. + pub(super) fn give_back_from(&self, index: usize) { + if index >= self.batches.all().len() { + return; + } + let parts = &self.recorded[self.batch_range(index).start..]; + let encoders: BTreeSet = parts.iter().map(|part| part.encoder).collect(); + for record in encoders.iter().filter_map(|&at| self.ended.get(at)) { + record.takes.give_back(); + } + let segments = parts.iter().flat_map(|part| &part.segments); + for done in segments.filter_map(|segment| segment.recorded.as_ref()) { + done.takes.give_back(); + } + } + /// 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, @@ -2030,9 +2049,12 @@ impl State { *placed += done.executions.len(); executions.splice(at..at, done.executions); retained.extend(done.retained.into_entry()); - // The segment's keys are its encoder's. + // The segment's keys are its encoder's, and so are its takes of copies taken at + // use, given back with the encoder's where its batch never runs + // ([`give_back_from`](Self::give_back_from)). if let Some(record) = ended.get_mut(encoder.encoder) { record.keys.extend(done.keys); + record.takes.join(done.takes); } *resident_misses = resident_misses.wrapping_add(done.resident_misses); *faults = ReplayFaults(faults.0 | done.faults.0); @@ -2246,8 +2268,7 @@ impl CommandBuffer { carry_batches: 1, pass_runs: PassRuns::default(), replaying_keeps: Vec::new(), - replaying_takes: Takes::default(), - step_takes: Takes::default(), + replaced_takes: None, descriptors: DescriptorArena::default(), status_words: None, mesh_grid_past: false, @@ -4606,7 +4627,7 @@ impl CommandBufferShared { let mut state = lock(&self.state); let segment = &state.recorded[encoder].segments[at]; let replaced = segment.recorded.as_ref().map(|done| done.takes.clone()); - state.step_takes = replaced.unwrap_or_default(); + state.replaced_takes = replaced.map(Replaced::new); } let recorded = work.step(&buffer, &inputs); // A device test's failure of the recording (`CommandBuffer::fail_host_step`), the @@ -4620,7 +4641,7 @@ impl CommandBufferShared { } }; let mut state = lock(&self.state); - state.step_takes.clear(); + state.replaced_takes = None; match recorded { Ok(done) => { let segment = &mut state.recorded[encoder].segments[at]; @@ -5183,6 +5204,13 @@ impl CommandBufferShared { self.wake_parked(parked); } + /// Its batch `index` failed at its submission (a host step's recording or the `vkQueueSubmit` + /// among it, [`failed`](Self::failed)), so it and every later batch never run: the copies + /// taken at use their encoders took are given back (`State::give_back_from`). + pub(crate) fn never_runs_from(&self, index: usize) { + lock(&self.state).give_back_from(index); + } + /// A batch could not be submitted (the scheduler dropped the rest): the command buffer /// finishes with `error` (one committed on a revoked queue with its `AccessRevoked`) once /// what it submitted before completed, at its `VkQueue`'s last diff --git a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs index 5f307ca61..635e9af4f 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs @@ -45,9 +45,14 @@ //! 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 +//! again, its segments recorded again among it, takes it where the use in its place in its +//! first encoding or that encoding's segments took it, keeping that take, 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`). +//! the next use's to take (`State::replaced_takes`, `shadow::ShadowSync::over`). So does work +//! recorded again on the GPU alone, where its first recording took it. A take of the first +//! recordings the encoding again does not meet is given back as it takes their place, and one +//! of its own the first recordings did not hold is given back where it is refused +//! (`shadow::Takes::give_back_unheld`). //! - **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, @@ -72,6 +77,7 @@ use crate::pipeline::budget::Urgency; use crate::replay::Replay; use crate::residency::Residency; use crate::resource::ResourceKey; +use crate::shadow::{Replaced, Takes}; use crate::used_resources::EncoderKind; use std::collections::BTreeSet; use std::sync::atomic::Ordering; @@ -151,7 +157,7 @@ impl CommandBufferShared { // (`Predication::replaying`). // The merges its first encoding left to runs of passes (`pass_runs.rs`): encoded again, // it leaves them, and keeps after itself what a later pass of a run went on over. - let (residency, runs) = { + let (residency, runs, first) = { let mut state = lock(&self.state); state.replaying_predication = match state.predicated { true => state.predication.replaying(at), @@ -166,16 +172,18 @@ 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) + // Its first encoding's takes of copies taken at use, its segments' among them, + // kept where the encoding again meets them: that encoding's recordings are never + // submitted (`shadow::ShadowSync::over`). + let first = state.takes_of(at); + state.replaced_takes = Some(Replaced::new(first.clone())); + (state.residency_before(at, replay.kind()), runs, first) }; let run = replay.run(&handle, residency); { let mut state = lock(&self.state); state.replaying_keeps.clear(); - state.replaying_takes.clear(); + state.replaced_takes = None; } // Its steps' gates listed as the commit lists the first encoding's. let run = { @@ -228,6 +236,8 @@ impl CommandBufferShared { } Err(unspliced) => { counts.reencode_refused(); + // Its first recordings stay: a take of its own they did not hold goes back. + unspliced.0.all_takes().give_back_unheld(&first); let refusal = "its place holds other parts or work than its encoding again's"; let reset = { let mut state = lock(&self.state); @@ -269,7 +279,7 @@ impl CommandBufferShared { // hold through this turn: only the first submission places segments, and // it waits for the turn; a host step, which records one, takes the turn too. What it // reaches through its resident set its first submission points it at as it lies then. - let alone: Vec<((usize, usize), Deferred)> = { + let alone: Vec<((usize, usize), Deferred, Takes)> = { let state = lock(&self.state); match state.submitted { true => Vec::new(), @@ -284,18 +294,26 @@ impl CommandBufferShared { .filter(|(_, recorded)| !spliced.contains(&recorded.encoder)) .flat_map(|(part, recorded)| { let segments = recorded.segments.iter().enumerate(); - segments - .filter(|(_, segment)| moved(segment)) - .map(move |(at, segment)| ((part, at), segment.work.clone())) + segments.filter(|(_, segment)| moved(segment)).map( + move |(at, segment)| { + let takes = (segment.recorded.as_ref()) + .map(|done| done.takes.clone()) + .unwrap_or_default(); + ((part, at), segment.work.clone(), takes) + }, + ) }) .collect() } } }; - for (place, work) in alone { + for (place, work, first) in alone { // Its notices were announced as it committed (`replay::is_replaying`), and its - // commands take what they use without holding it. + // commands take what they use without holding it. It keeps its first recording's + // takes of copies taken at use where it meets them (`shadow::ShadowSync::over`). + lock(&self.state).replaced_takes = Some(Replaced::new(first.clone())); let again = crate::replay::replaying(|| work.record_on_gpu(&handle)); + lock(&self.state).replaced_takes = None; let counts = &device.shared().counts; let Ok(Some((recorded, inputs))) = again else { counts.reencode_refused(); @@ -306,6 +324,8 @@ impl CommandBufferShared { Ok(()) => counts.segment_recorded_again(), Err(unspliced) => { counts.reencode_refused(); + // The first recording stays: a take of its own it did not hold goes back. + unspliced.0.takes.give_back_unheld(&first); drop(unspliced); } } @@ -322,6 +342,19 @@ impl CommandBufferShared { } impl State { + /// What ended encoder `at`'s recordings decided of their copies taken at use + /// (`shadow::Takes`): its own, then its parts' segments' recordings' in order. + pub(super) fn takes_of(&self, at: usize) -> Takes { + let mut takes = self.ended[at].takes.clone(); + let segments = (self.recorded.iter()) + .filter(|part| part.encoder == at) + .flat_map(|part| &part.segments); + for done in segments.filter_map(|segment| segment.recorded.as_ref()) { + takes.join(done.takes.clone()); + } + takes + } + /// The residency an ended encoder `at` of `kind` started with, as the command buffer's /// encoders ended before it leave it (`CommandBuffer::inherited_residency`): none for a blit /// encoder, which makes nothing resident. @@ -343,10 +376,11 @@ impl State { /// Put `replayed`, ended encoder `at` encoded again, and what it made resident in its place, /// before the submission of its event group: each of its parts' recordings, uses and segments /// (recorded on the GPU alone), its executions (the later encoders' and segments' places - /// among them moved by the difference), its keys, and what its commands and its segments' - /// took (held weakly until that submission). What its first recording retained stays retained, - /// and so does what its first segments recorded and what their host steps needed; its - /// `VkCommandBuffer`s go with the pool unsubmitted. Refused, changing nothing and giving + /// among them moved by the difference), its keys, what its commands and its segments' took + /// (held weakly until that submission), and its takes of copies taken at use, a take of the + /// first recordings it did not meet given back. What its first recording retained stays + /// retained, and so does what its first segments recorded and what their host steps needed; + /// its `VkCommandBuffer`s go with the pool unsubmitted. Refused, changing nothing and giving /// `replayed` back, where its place holds other parts than the encoding again's, or a segment /// but one whose pipelines are being made (which goes, the encoding again having left one of /// its own in its place), work its commit recorded on the GPU alone, a run's merge (which @@ -387,6 +421,9 @@ impl State { if !fits { return Err(Box::new((replayed, residency))); } + // A take of copies taken at use of its first recordings the encoding again did not meet + // goes back: they are never submitted (`shadow::ShadowSync::over`). + self.takes_of(at).give_back_unheld(&replayed.all_takes()); let Replayed { parts: mut replayed_parts, held, @@ -395,6 +432,7 @@ impl State { keys, mut taken, heaps, + takes, } = replayed; // Held weakly until its group's submission, as the commit held what the first recording // and its segments took. @@ -456,6 +494,7 @@ impl State { record.keys = keys; record.held = held; record.taken = taken; + record.takes = takes; if let Some(residency) = residency { if let Some(first) = record.residency.replace(residency) { self.retained.push(Box::new(first)); @@ -470,8 +509,9 @@ impl State { /// before the first submission, with `inputs`, what its host step needs now: what its /// commands took held weakly until the first submission, as the commit holds them. What the first /// recording retained, its executions and its host step's inputs stay retained; its - /// `VkCommandBuffer`s go with the pool unsubmitted, and what its commands took, held weakly, - /// goes. Refused, changing nothing and giving `recorded` and `inputs` back, where the place + /// `VkCommandBuffer`s go with the pool unsubmitted, what its commands took, held weakly, + /// goes, and so does a take of copies taken at use of its that `recorded` did not meet, given + /// back. Refused, changing nothing and giving `recorded` and `inputs` back, where the place /// holds no such segment, or the command buffer was submitted. pub(super) fn splice_segment( &mut self, @@ -491,6 +531,11 @@ impl State { let first = segment.recorded.replace(recorded); let before = segment.inputs.replace(inputs); if let Some(first) = first { + // A take of copies taken at use of the first recording this one did not meet goes + // back (`shadow::ShadowSync::over`). + if let Some(now) = &segment.recorded { + first.takes.give_back_unheld(&now.takes); + } let RecordedSegment { retained, executions, diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 0117ce470..12f8cfe65 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -510,6 +510,22 @@ pub(super) struct Replayed { pub(super) keys: BTreeSet, pub(super) taken: Taken, pub(super) heaps: Vec, + /// What its first uses of copies taken at use decided (`shadow::Takes`), its segments' + /// apart: the first encoding's takes kept where it met them. + pub(super) takes: Takes, +} + +impl Replayed { + /// What it decided of copies taken at use, its segments' recordings' after its own + /// (`shadow::Takes`). + pub(super) fn all_takes(&self) -> Takes { + let mut takes = self.takes.clone(); + let segments = self.parts.iter().flat_map(|part| &part.segments); + for done in segments.filter_map(|segment| segment.recorded.as_ref()) { + takes.join(done.takes.clone()); + } + takes + } } /// One part of an encoder encoded again ([`Replayed::parts`]): its `VkCommandBuffer`s, what they @@ -731,10 +747,6 @@ 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) } @@ -744,7 +756,7 @@ impl EncoderCore { let (raw, keeping, replaced) = { let mut state = command_buffer.state(); let raw = command_buffer.allocate_raw(&mut state)?; - (raw, state.keeping(), state.step_takes.clone()) + (raw, state.keeping(), state.replaced_takes.clone()) }; let device = command_buffer.device().shared().vk(); // A chunk kept for later command buffers (`kept_commands.rs`) may run in several at once. @@ -762,9 +774,12 @@ impl EncoderCore { .counts .encoder_command_buffer_begun(Begun::Segment); let mut core = EncoderCore::over(command_buffer, raw, ordered); - // A host step's recording keeps the takes of the recording it replaces - // (`State::step_takes`); none elsewhere. - core.shadows = ShadowSync::over(replaced); + // Work recorded again keeps the takes of copies taken at use of the recordings it + // replaces, which are never submitted (`State::replaced_takes`): a host step's, a + // rebind's encoding again (`command_buffer/reencode.rs`). + if let Some(replaced) = replaced { + core.shadows = ShadowSync::over(replaced); + } Ok(core) } @@ -2959,6 +2974,7 @@ impl EncoderCore { taken, fails_command_buffer, faults, + shadows, .. } = self; if fails_command_buffer.is_some() || faults.is_some() || taken.dead() { @@ -3017,6 +3033,7 @@ impl EncoderCore { keys, taken, heaps, + takes: shadows.into_takes(), }) } } diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 911fc7a1a..10ddd6732 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -1707,6 +1707,7 @@ fn submit_ready( }; let Held { buffer, + index, last, waits: events, binary_needs, @@ -1742,6 +1743,7 @@ fn submit_ready( state.binary.dropped(&held.binary_needs); } spent.extend(rest.into_iter().map(|held| held.buffer)); + buffer.never_runs_from(index); buffer.failed(error, &state.values); if buffer.settle_storage_forms() { settled.push(buffer.hold()); @@ -1960,6 +1962,7 @@ fn submit_plain_ready( drop(busy); let Held { buffer, + index, binary_needs, waits: events, .. @@ -1973,6 +1976,7 @@ fn submit_plain_ready( state.binary.dropped(&held.binary_needs); } spent.extend(rest.into_iter().map(|held| held.buffer)); + buffer.never_runs_from(index); buffer.failed(error, &state.values); spent.push(buffer); return submitted; @@ -2195,6 +2199,7 @@ fn settle_group( for (queue, held) in grouped.drain(..) { let Held { buffer, + index, waits: events, binary_needs, .. @@ -2202,6 +2207,7 @@ fn settle_group( drop(events); binary.dropped(&binary_needs); buffer.left_group(); + buffer.never_runs_from(index); if !failed.iter().any(|done| done.same(&buffer)) { // The rest of the command buffer never runs (dropped here, before it can // finish). diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index a36a03d15..6824e03c1 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -243,26 +243,29 @@ //! 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. 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`]). 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 +//! out of its command buffer's work ([`ShadowSync::forget`]) leaves the copy to the next use. +//! Work recorded again in place of a recording never submitted takes the copy where that +//! recording took it, keeping its take, and leaves the mark as it finds it there: a host step's +//! recording of work its commit recorded (`CommandBufferShared::host_step`), and a rebind's +//! encoding again of an encoder, its segments among it, or its recording again of work its +//! commit recorded on the GPU alone (`command_buffer/reencode.rs`). A use that recording did not +//! meet decides by the mark, and a take of that recording no use meets is given back +//! ([`ShadowSync::over`], [`Takes::give_back_unheld`]). A take whose encoder runs nothing, its +//! command buffer failed before it ran, is given back: as its part is failed (committed as +//! failed, an encoder a failure names, a submission that runs nothing, `State::fail_part`), or +//! as its batch's submission fails, a host step's recording or the `vkQueueSubmit` among it +//! (`State::give_back_from`). The next use takes the copy, where that take is still the +//! texture's latest and nothing set the mark since ([`Takes::give_back`]). 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). A command buffer dropped without a commit gives back every -//! take of its ended encoders (`CommandBufferShared`'s drop). Work its commit recorded and its -//! host step records again (`CommandBufferShared::host_step`), the commit's recording never -//! submitted, takes the copy where the commit's recording took it, keeping that take, and -//! decides a use that recording did not meet by the mark; a take of that recording the step's -//! does not meet is given back ([`ShadowSync::over`], [`Takes::give_back_unheld`]). +//! take of its ended encoders (`CommandBufferShared`'s drop). //! 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). use super::blit_texture::announce_mipmaps; use super::buffer::{Buffer, CallerMemory, WeakBuffer}; +use super::device::lock; use super::dirty_pages::{CopyInGate, ShadowLog}; use super::error::{Error, RuntimeGap}; use super::image_plan::{ImageFacts, ImagePlan}; @@ -292,7 +295,7 @@ use metal2vulkan::error::{Feature, Unsupported}; use metal2vulkan::metal::PixelFormat; use std::ops::Range; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Weak}; +use std::sync::{Arc, Mutex, Weak}; /// What a shadowed texture's copies need: its truth, its shadow, and the copies between them. pub(super) struct ShadowCopies { @@ -2959,9 +2962,9 @@ struct Entry { /// changed. stale: Option<[u32; 4]>, /// 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. + /// ([`ShadowCopies::levels`]) the encoder's use took, by the mark ([`LevelsCopy::take`]) or + /// kept from a recording it replaces ([`ShadowSync::over`]): given back where what it + /// recorded is left out ([`ShadowSync::forget`]). `None` where it took none. took: Option, } @@ -3182,11 +3185,6 @@ pub(super) struct Took { } 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); @@ -3227,45 +3225,60 @@ impl Took { } } -/// 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. Recorded in place of a recording that decided `replaced` - /// ([`ShadowSync::over`]), a use that one met decides as it did, keeping its take, and - /// noted so; only the others ask the mark. - Mark { replaced: Takes, noted: 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), +/// The decisions of copies taken at use ([`Takes`]) of recordings never submitted that work is +/// recorded again in place of, those no use recorded in their place met yet, in their order: +/// shared by every core recorded in their place ([`ShadowSync::over`]), each first use of a +/// texture among them meeting the first of its texture's left (`State::replaced_takes`). +#[derive(Clone, Debug)] +pub(super) struct Replaced(Arc>); + +impl Replaced { + /// `takes`, none of them met yet. + pub(super) fn new(takes: Takes) -> Replaced { + Replaced(Arc::new(Mutex::new(takes))) + } + + /// Meet the first decision left of the texture of `key`, its take where it took the copy; + /// `None` where none is left. + fn meet(&self, key: ResourceKey) -> Option> { + let mut takes = lock(&self.0); + let at = takes.0.iter().position(|(met, _)| *met == key)?; + Some(takes.0.remove(at).1) + } +} + +/// How an encoder's first uses of a copy taken at use decide whether they take it: where the +/// texture's mark asks ([`LevelsCopy::take`], which clears it), each decision noted. Recorded in +/// place of recordings never submitted ([`ShadowSync::over`]), a use that meets a decision of +/// theirs decides as it did, keeping its take, and is noted so; only the others ask the mark. +#[derive(Default)] +struct TakeBy { + /// The decisions of the recordings this one is recorded in place of, where it is. + replaced: Option, + /// What its first uses decided, in order. + noted: Takes, } impl TakeBy { /// 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 or keeps the take of the recording it - /// replaces (none for an encoding again's). + /// and the take's number where it takes it by the mark or keeps the take of a recording it + /// replaces. fn take(&mut self, copies: &Arc, levels: &LevelsCopy) -> (bool, Option) { let key = copies.shadow_key; - match self { - TakeBy::Mark { replaced, noted } => { - let took = match replaced.0.iter().position(|(met, _)| *met == key) { - Some(at) => replaced.0.remove(at).1, - None => levels.take().map(|number| Took { - copies: Arc::downgrade(copies), - number, - }), - }; - let number = took.as_ref().map(|took| took.number); - noted.0.push((key, 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.is_some(), None), - None => (false, None), - }, - } + let kept = self + .replaced + .as_ref() + .and_then(|replaced| replaced.meet(key)); + let took = match kept { + Some(took) => took, + None => levels.take().map(|number| Took { + copies: Arc::downgrade(copies), + number, + }), + }; + let number = took.as_ref().map(|took| took.number); + self.noted.0.push((key, took)); + (number.is_some(), number) } } @@ -3278,49 +3291,34 @@ impl ShadowSync { entries: Vec::new(), carry, tally: CarryTally::default(), - takes: TakeBy::Mark { - replaced: Takes::default(), - noted: Takes::default(), - }, + takes: TakeBy::default(), } } - /// A core that records work again in place of a recording that decided `replaced` of its - /// copies taken at use ([`Takes`]), which never runs: a host step's recording of work its - /// commit recorded (`CommandBufferShared::host_step`). Each of its first uses that recording - /// met takes the copy where it took it, keeping its take; any other where the texture's mark - /// asks. A take of `replaced` it never meets is given back as its recording replaces that - /// one ([`Takes::give_back_unheld`]). Nothing is carried: every other texture copies in at - /// its first use. - pub(super) fn over(replaced: Takes) -> ShadowSync { + /// A core that records work again in place of recordings never submitted, whose decisions of + /// their copies taken at use no core in their place met yet are `replaced`: a host step's + /// recording of work its commit recorded (`CommandBufferShared::host_step`), a rebind's + /// encoding again of an encoder and its segments, and its recording again of work its commit + /// recorded on the GPU alone (`command_buffer/reencode.rs`). Each of its first uses that meets + /// a decision of theirs takes the copy where that one took it, keeping its take; any other + /// where the texture's mark asks. A take of theirs no use meets is given back as their + /// recordings are replaced ([`Takes::give_back_unheld`]). Nothing is carried: every other + /// texture copies in at its first use. + pub(super) fn over(replaced: Replaced) -> ShadowSync { ShadowSync { - takes: TakeBy::Mark { - replaced, + takes: TakeBy { + replaced: Some(replaced), noted: Takes::default(), }, ..ShadowSync::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 or as a - /// recording in place of another kept them ([`over`](Self::over)), for an encoding of the - /// encoder again ([`again`](Self::again)) and for a give-back; none for an encoding again. + /// recording in place of others kept them ([`over`](Self::over)), for a recording of the + /// work again and for a give-back. pub(super) fn into_takes(self) -> Takes { - match self.takes { - TakeBy::Mark { noted, .. } => noted, - TakeBy::Again(_) => Takes::default(), - } + self.takes.noted } /// How the encoder's first copies in since this was last called stood against their images' @@ -3580,8 +3578,8 @@ 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. Encoded - // again, where its first encoding took it ([`TakeBy`]). + // `did_modify_data`, wherever it is; else its image holds what it took. Recorded + // again, where the recording it replaces took it ([`TakeBy`]). let (took, number) = match &copies.levels { Some(levels) => self.takes.take(copies, levels), None => (false, None), @@ -4115,8 +4113,7 @@ 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. An - // encoding again took none by the mark, and gives none back to it. + // A copy taken at use whose copy in is left out is taken at the next use instead. for entry in &self.entries { if let (Some(number), Some(levels)) = (entry.took, &entry.copies.levels) { levels.give_back(number); diff --git a/crates/metal2vulkan-runtime/src/tests/shadow.rs b/crates/metal2vulkan-runtime/src/tests/shadow.rs index e0a5fa6b9..4677e5efa 100644 --- a/crates/metal2vulkan-runtime/src/tests/shadow.rs +++ b/crates/metal2vulkan-runtime/src/tests/shadow.rs @@ -5,9 +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, LaterLevels, LevelsCopy, Lost, ShadowBuffer, - ShadowCarry, ShadowCopies, ShadowCopy, ShadowSource, ShadowSync, ShadowTarget, ShadowUse, Step, - Within, + self, Backing, CopyCause, ImageTruth, KeptArea, LaterLevels, LevelsCopy, Lost, Replaced, + ShadowBuffer, ShadowCarry, ShadowCopies, ShadowCopy, ShadowSource, ShadowSync, ShadowTarget, + ShadowUse, Step, Within, }; use super::super::shadow_merge::ScratchSpares; use super::super::tracker::{Access, Use, UseRange}; @@ -2416,15 +2416,18 @@ fn taken_at_use(buffer: ResourceKey, bytes: Range) -> Arc { } /// 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. +/// encoding of an encoder again (`command_buffer/reencode.rs`, `ShadowSync::over`) takes it where +/// its first encoding took it, keeping that take, 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. The cores recorded in place of the first share its decisions (`Replaced`): the first +/// use of the texture among them meets its take, and another decides by the mark. #[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)], &[])); + let again = |takes| ShadowSync::over(Replaced::new(takes)); // The first encoding takes it at its first use alone. let mut first = ShadowSync::default(); assert_eq!(first_use(&mut first), [Moved::Into(k)]); @@ -2435,17 +2438,24 @@ fn an_encoding_again_takes_a_copy_taken_at_use_where_its_first_encoding_took_it( 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), []); - // An encoding again notes no take of its own. - assert_eq!(first_use(&mut ShadowSync::again(again.into_takes())), []); + let mut once = again(took.clone()); + assert_eq!(first_use(&mut once), [Moved::Into(k)]); + assert_eq!(first_use(&mut once), []); + // It keeps the take: encoded again in its turn, it takes the copy there too. + assert_eq!(first_use(&mut again(once.into_takes())), [Moved::Into(k)]); assert_eq!(first_use(&mut ShadowSync::default()), []); + // Two cores in place of the first meet its take once. + let shared = Replaced::new(took.clone()); + assert_eq!( + first_use(&mut ShadowSync::over(shared.clone())), + [Moved::Into(k)] + ); + assert_eq!(first_use(&mut ShadowSync::over(shared)), []); // 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 again(took_none)), []); + assert_eq!(first_use(&mut again(took)), [Moved::Into(k)]); assert_eq!(first_use(&mut ShadowSync::default()), [Moved::Into(k)]); assert_eq!(first_use(&mut ShadowSync::default()), []); } @@ -2517,7 +2527,7 @@ fn a_recording_in_place_of_another_keeps_its_takes() { assert_eq!(first_use(&mut commit, &t), [Moved::Into(kt)]); let took = commit.into_takes(); // The step's takes t's where the commit's took it, the mark clear, and u's by its mark. - let mut step = ShadowSync::over(took.clone()); + let mut step = ShadowSync::over(Replaced::new(took.clone())); assert_eq!(first_use(&mut step, &t), [Moved::Into(kt)]); assert_eq!(first_use(&mut step, &u), [Moved::Into(ku)]); let kept = step.into_takes(); @@ -2534,7 +2544,7 @@ fn a_recording_in_place_of_another_keeps_its_takes() { let mut commit = ShadowSync::default(); assert_eq!(first_use(&mut commit, &t), [Moved::Into(kt)]); let took = commit.into_takes(); - let step = ShadowSync::::over(took.clone()); + let step = ShadowSync::::over(Replaced::new(took.clone())); took.give_back_unheld(&step.into_takes()); assert_eq!(first_use(&mut ShadowSync::default(), &t), [Moved::Into(kt)]); // A use the commit's met and took nothing at takes nothing after a `did_modify_data`, which @@ -2543,6 +2553,9 @@ fn a_recording_in_place_of_another_keeps_its_takes() { assert_eq!(first_use(&mut commit, &t), []); let took_none = commit.into_takes(); levels.mark_stale(); - assert_eq!(first_use(&mut ShadowSync::over(took_none), &t), []); + assert_eq!( + first_use(&mut ShadowSync::over(Replaced::new(took_none)), &t), + [] + ); assert_eq!(first_use(&mut ShadowSync::default(), &t), [Moved::Into(kt)]); } diff --git a/crates/metal2vulkan-runtime/src/used_resources.rs b/crates/metal2vulkan-runtime/src/used_resources.rs index edf90ee74..394ec4b2e 100644 --- a/crates/metal2vulkan-runtime/src/used_resources.rs +++ b/crates/metal2vulkan-runtime/src/used_resources.rs @@ -115,9 +115,10 @@ 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: 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`). + /// What its first uses of copies taken at use decided, as the program encoded it or as its + /// encoding again kept them, and its placed segments' recordings' after them: 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`, `State::give_back_from`). 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 a5b11c105..4f49cea98 100644 --- a/docs/API.md +++ b/docs/API.md @@ -467,12 +467,13 @@ the device's blit chain or the compute filter (`device-filtered-mipmaps`), or an 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. 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. A take whose encoder runs nothing, its command buffer having failed -before it ran (committed as failed, or a submission that fails) or dropped without a commit, is -given back: the next use takes the copy. Work a host step records again after the commit (an -execution of indirect commands the CPU wrote since) takes the copy where the commit's recording -took it. Between takes no write of +the copy in the encoding again where the first encoding took it, its work recorded on the GPU alone +(a render pass that executes indirect commands the GPU writes) among it, and a `did_modify_data` +made since is left to the next use encoded. A take whose encoder runs nothing, its command buffer +having failed before it ran (committed as failed, a submission that fails, a host step that fails +or a `vkQueueSubmit` the driver refuses) or dropped without a commit, is given back: the next use +takes the copy. Work a host step records again after the commit (an execution of indirect commands +the CPU wrote since) takes the copy where the commit's recording took it. 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 54aa2c988..3e498ceeb 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -1609,3 +1609,15 @@ compares with Metal's. 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. + +- **A tiled texture's take in the work apart of a predicated loop's run again** + (`Buffer::new_tiled_texture`, more than one level, `shadow.rs`'s **Copies taken at use**, + `command_buffer/rerun.rs`): each run again records the laying's work apart again (an indirect + dispatch, or an execution of indirect commands) as a new recording, which decides its first use + of the texture by the texture's mark, as a use encoded then would. Where `Texture::did_modify_data` + or a `replace_region` of the first level is called while the loop still runs again, the next run + again takes the copy: it copies the bytes into the levels mid-loop, over what the earlier runs + wrote into them, and clears the mark, so the first use encoded after the call takes no copy and + reads the levels as the loop left them, where Metal's takes the copy of the bytes as they are + then. A recording of a run again carries no take, so a failure of the loop's command buffer gives + none back.