From cc6b083fc241017cbb8b2a6bb0a4afe052890846 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 3 Nov 2026 17:44:05 -0800 Subject: [PATCH] Let what every write route of a command buffer may write reach every buffer it made resident for writing What: what an indirect command buffer's records may hold (WrittenCommands, command_writes) took a writer's writes only from the encoder that made the buffer resident for writing (Writers: the state set then, each set after, and that encoder's executions). The routes that mark the buffer (write_routes.rs) were already the command buffer's. Routes now keeps what all of its routes may write (states set in any encoder, before the residency or after it, and what executed buffers' records may name) and adds it to every buffer made resident for writing, at the call that brings either; an encoder opens with what the ended ones met. For two encoders open together (a parallel render encoder's) the commit adds what each did not see of the other, and the followers that sets off are marked outside the command buffer's lock, as the commit's marks are. Writers is gone: Routes holds a superset of what it held. Why: Metal runs what a kernel wrote with the buffer resident for writing in any encoder of the command buffer, before the kernel's or after it (the probe icb_routed_writes.m, M3 Max, macOS 27.0.1). The marks matched that; the summary did not, and an execution of a buffer that inherits the encoder's state decides on the summary whether the GPU alone runs it, as draws of the one primitive type it allows. With the residency in another encoder than the kernel's, the GPU alone ran the kernel's point as a draw of the CPU's triangles, which drew nothing (the previous commit's lane). The bug is older than the write routes: the summary was per encoder before them too. What it moves: an execution of a buffer whose command buffer sets, in another encoder than the one that made the buffer resident, a state that may write what the GPU-alone forms rule out: a render execution takes the host step where that state may write more than draws of one primitive type, buffers, resets and cleared barriers; a compute execution in a concurrent encoder orders its commands where it may set a barrier, and a checked one calls its check where the records may name a state that may encode. The lanes that made a buffer writable with a state set in an encoder of its own (indirect_host_steps::encoding_state_set: icb_replay_encode's, which may set pipelines, dispatch, set threadgroup lengths and barriers) showed it: with it, 6 of the 480 tests of the 40 modules changed (render_merged_executions' GPU forms into a shadowed target took the host step, 6 executions; five dispatch_executions tests saw barriers, check calls and a host step), all others as before, resident_draws among them (its writer and residency share an encoder: no host step on its write path, its chunks kept). Those lanes are about the GPU-written path, not about what the state writes, so the lever now sets icb_reset_encode's state (resets alone, which no GPU-alone form rules out). Docs: written_commands.rs and write_routes.rs module docs, IndirectCommandBuffer::command_writes, render/executions/gpu.rs, the runtime README and docs/API.md section 10. How verified (debug, --cfg m2v_gpu, M3 Max under MoltenVK): runtime_lane::write_routes passes (it failed before this commit: After and Before drew no point); the 39 ICB lane modules and write_routes, 480 tests, pass with the reset lever; the runtime's tests::write_routes_device::a_route_reaches_every_buffer_its_command_buffer_made_writable (new: a later encoder's state, one set before the residency, and a parallel encoder's at the commit) and tests::written_commands pass; clippy -D warnings with --cfg m2v_gpu is clean. (cherry picked from commit 7d13a4dee21162a972b40fd9868e3e2f9112a677) (cherry picked from commit 13b9646e48b767defdd189bf156a2a9130b3b857) --- crates/metal2vulkan-runtime/README.md | 4 +- .../src/command_buffer/mod.rs | 3 +- crates/metal2vulkan-runtime/src/compute.rs | 8 -- .../src/indirect_command_buffer.rs | 10 +-- crates/metal2vulkan-runtime/src/render.rs | 5 -- .../src/render/arguments.rs | 1 - .../src/render/executions.rs | 4 +- .../src/render/executions/gpu.rs | 6 +- crates/metal2vulkan-runtime/src/tests/mod.rs | 2 + .../src/tests/write_routes_device.rs | 69 +++++++++++++++++ .../src/tests/written_commands.rs | 25 +------ .../metal2vulkan-runtime/src/write_routes.rs | 75 ++++++++++++++----- .../src/written_commands.rs | 57 +++----------- .../src/runtime_lane/indirect_host_steps.rs | 8 +- docs/API.md | 13 +++- 15 files changed, 166 insertions(+), 124 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/write_routes_device.rs diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index c246bba8d..6d4ab2ed2 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -413,7 +413,9 @@ Calls Metal does not have, for a consumer that emulates a Metal device: one; or a blit copied or reset commands into it. Only then does an execution of it wait for the work before it and read its records on the host. A buffer made resident for writing beside functions that call no such operation runs its executions from the CPU's records, as one made - resident for reading does (`WorkCounts::indirect_write_residencies_unmarked`). + resident for reading does (`WorkCounts::indirect_write_residencies_unmarked`). What its records + may hold (`command_writes`), which decides whether an execution runs on the GPU alone, takes what + every such state of the command buffer may write, whichever encoder set it. - `HostMemory::with_dma_buf` and its `DmaBufSource`: dma-bufs of a no-copy object's pages, for a device that refuses their pointer. - `HostMemory::with_dirty_pages` and its `DirtyPages` (with `DirtyStamp`): the consumer's log of diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index bc94a72b7..5651b925c 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -2882,7 +2882,8 @@ impl CommandBuffer { /// before (`IndirectCommandBuffer::written_by_gpu`). Those it leaves unmarked are counted, and /// held, where it executed indirect commands, until it completes. fn mark_gpu_written(&self) { - let decided = self.state().routes.at_commit(); + let (decided, followed) = self.state().routes.at_commit(); + followed.mark(); match decided { AtCommit::Mark(buffers, writer) => { for icb in buffers { diff --git a/crates/metal2vulkan-runtime/src/compute.rs b/crates/metal2vulkan-runtime/src/compute.rs index a08ed7f1f..3324abd3d 100644 --- a/crates/metal2vulkan-runtime/src/compute.rs +++ b/crates/metal2vulkan-runtime/src/compute.rs @@ -84,7 +84,6 @@ use super::texture::Texture; use super::tracker::Barrier; use super::used_resources::EncoderKind; use super::values::{Region, Size}; -use super::written_commands::Writers; use ash::vk; use metal2vulkan::bind::{ BindShape, BindState, BindingPlan, Dispatch, DispatchCommands, FlatIndirect, HeapPlacement, @@ -173,8 +172,6 @@ pub struct ComputeCommandEncoder { /// of `stage_in_region` until the next `setStageInRegion:`. indirect_stage_in: Option<(Buffer, u64)>, residency: Residency, - /// The indirect command buffers it made resident for writing (`written_commands.rs`). - writers: Writers, /// The bodies of the predicated loops it opened and has not closed (`predication.rs`). captures: Captures, /// Its calls so far, where the device had a buffer over host pages as it opened @@ -283,7 +280,6 @@ impl CommandBuffer { stage_in_region: None, indirect_stage_in: None, residency: self.inherited_residency(false), - writers: Writers::default(), captures: Captures::carried(carried), log: (self.device().shared().rebinds.any()) .then(|| self.device().shared().scheduler.spare_logs().compute()), @@ -314,7 +310,6 @@ impl ComputeCommandEncoder { stage_in_region: None, indirect_stage_in: None, residency, - writers: Writers::default(), captures: Captures::default(), log: None, dispatches: 0, @@ -447,7 +442,6 @@ impl ComputeCommandEncoder { this.core .check_device("set_compute_pipeline_state", state.device())?; this.pipeline = Some(state.clone()); - this.writers.state_set(state.indirect_command_writes()); (this.core.routes).state_set(state.indirect_command_writes(), || { vec![state.function_name().to_owned()] }); @@ -1231,7 +1225,6 @@ impl ComputeCommandEncoder { } if let ResourceRef::IndirectCommandBuffer(icb) = resource { if usage.contains(ResourceUsage::WRITE) { - self.writers.made_writable(icb.written()); self.core.routes.writable(icb); self.core.retain(icb.gpu_write()); } @@ -1480,7 +1473,6 @@ impl ComputeCommandEncoder { self.sync_heaps(); // Its commands run the states their records name, where it inherits none. if !icb.descriptor().inherit_pipeline_state { - self.writers.runs(icb.written().named().writes); self.core.routes.executed(icb); } let table = if icb.descriptor().inherit_buffers { diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index 21c6225a3..f0f5fdf82 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -2800,11 +2800,11 @@ impl IndirectCommandBuffer { /// What the buffer's records may hold, from every route that writes them encoded so far: a /// runtime extension, with no Metal counterpart. The CPU's setters add what each command /// holds after the call (its draw or dispatch, a draw's primitive type among it, its barrier, - /// and [`CommandWrites::PIPELINE`] where it names a state); an encoder that made the buffer - /// resident for writing adds what its states may write - /// ([`ComputePipelineState::indirect_command_writes`], - /// [`RenderPipelineState::indirect_command_writes`]): the state set when it made the buffer - /// resident, each set after, and the states its executions' commands name; a blit encoder's + /// and [`CommandWrites::PIPELINE`] where it names a state); a command buffer that made the + /// buffer resident for writing, in any of its encoders, adds what the states set in any of + /// them may write ([`ComputePipelineState::indirect_command_writes`], + /// [`RenderPipelineState::indirect_command_writes`]), before the residency or after it, and + /// what the states its executions' commands name may write; a blit encoder's /// `copy_indirect_command_buffer` into it adds the source buffer's, now and as the source /// gains more. Resets add nothing, and nothing is taken away: a record written again keeps /// the earlier writes here, as a GPU write may stay. A clear bit is proven of every write diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 1be94527a..c2520640d 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -344,7 +344,6 @@ use super::tracker::{self, Access, Barrier, GeometryWrites, IndirectReads, Unord use super::used_resources::EncoderKind; use super::versioned::Versioned; use super::visibility::{self, Continuation, Visibility, VisibilityResultMode}; -use super::written_commands::Writers; use crate::device::functions::Vk; use crate::work_counts::Begun; use crate::work_counts::NotInline; @@ -1487,8 +1486,6 @@ pub(crate) struct RenderLowering { /// What `use_resource(s)` and `use_heap(s)` made resident, each in its render stages /// (`render/arguments.rs`). residency: Residency, - /// The indirect command buffers it made resident for writing (`written_commands.rs`). - writers: Writers, /// The executions of render commands, in encoding order, left to record with the pass /// (`render/executions.rs`). executions: Vec, @@ -2085,7 +2082,6 @@ impl RenderLowering { tile_memory_splits: 0, tile_memory: tile_memory::TileMemory::default(), residency, - writers: Writers::default(), executions: Vec::new(), pending: Vec::new(), draw_calls: 0, @@ -2657,7 +2653,6 @@ impl RenderLowering { } self.shape_inputs() .check_pipeline(CALL, &state, &self.layout)?; - self.writers.state_set(state.indirect_command_writes()); (self.core.routes).state_set(state.indirect_command_writes(), || state.function_names()); self.pipeline = Some(state); self.grouped_set(&drawn); diff --git a/crates/metal2vulkan-runtime/src/render/arguments.rs b/crates/metal2vulkan-runtime/src/render/arguments.rs index dcf5d9942..8381c1ae9 100644 --- a/crates/metal2vulkan-runtime/src/render/arguments.rs +++ b/crates/metal2vulkan-runtime/src/render/arguments.rs @@ -80,7 +80,6 @@ impl RenderLowering { } if let ResourceRef::IndirectCommandBuffer(icb) = resource { if usage.contains(ResourceUsage::WRITE) { - self.writers.made_writable(icb.written()); self.core.routes.writable(icb); self.core.retain(icb.gpu_write()); } diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs index 9e4bffbab..855dd4f12 100644 --- a/crates/metal2vulkan-runtime/src/render/executions.rs +++ b/crates/metal2vulkan-runtime/src/render/executions.rs @@ -329,7 +329,7 @@ use crate::tracker::{ use crate::versioned::Versioned; use crate::visibility::{Lent, Visibility}; use crate::write_routes::replay_why; -use crate::written_commands::{Writers, WrittenCommands}; +use crate::written_commands::WrittenCommands; use crate::Device; use ahead::Ahead; use ash::vk; @@ -551,7 +551,6 @@ impl RenderLowering { self.sync_heaps(); // Its commands run the states their records name, where it inherits none. if !icb.descriptor().inherit_pipeline_state { - self.writers.runs(icb.written().named().writes); self.core.routes.executed(icb); } // An execution keeps an extended-range, padded or bfloat attachment's texels as a draw @@ -2486,7 +2485,6 @@ impl RenderExecution { .arguments() .with_buffer(storage.tile_memory), residency: residency.clone(), - writers: Writers::default(), executions: Vec::new(), pending: Vec::new(), draw_calls: 0, diff --git a/crates/metal2vulkan-runtime/src/render/executions/gpu.rs b/crates/metal2vulkan-runtime/src/render/executions/gpu.rs index d5f23bfec..2fdc9b811 100644 --- a/crates/metal2vulkan-runtime/src/render/executions/gpu.rs +++ b/crates/metal2vulkan-runtime/src/render/executions/gpu.rs @@ -13,9 +13,9 @@ //! state counting as one, where it looks for them; the encoder's state encodes no indirect //! commands, where it looks for such states), so it is never called; of a buffer of draws of //! primitives alone (`IndirectCommandType::DRAW`) that inherits the encoder's pipeline state -//! and not its buffers, and whose records the CPU and every encoder that made it resident for -//! writing may fill with draws of one primitive type, buffers, resets and cleared barriers -//! alone; in a pass whose fragment stages wrote no bytes before the call (no copy of them is +//! and not its buffers, and whose records the CPU and every route of a command buffer that made +//! it resident for writing (`write_routes.rs`) may fill with draws of one primitive type, +//! buffers, resets and cleared barriers alone; in a pass whose fragment stages wrote no bytes before the call (no copy of them is //! read, `render/pass_start_reads.rs`), with no tile threadgroup memory argument set, and where //! the pass has a shadowed texture, whose command buffer made resident past the call nothing //! that meets a shadowed texture's truth (`shadows::meets_truths`), as the host step's rule. diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index a11f36b17..a0b812379 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -378,6 +378,8 @@ mod warm_record; mod word_fill; mod word_samples; mod wrapped; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod write_routes_device; mod written_commands; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod ycbcr_device; diff --git a/crates/metal2vulkan-runtime/src/tests/write_routes_device.rs b/crates/metal2vulkan-runtime/src/tests/write_routes_device.rs new file mode 100644 index 000000000..ec94f9618 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/write_routes_device.rs @@ -0,0 +1,69 @@ +//! A command buffer's write routes (`write_routes.rs`): what each route may write joins what the +//! records may hold (`written_commands.rs`) of every buffer the command buffer made resident for +//! writing, whichever of its encoders met which, and marks the buffer. +//! +//! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, +//! `m2v_cpu_icd`), on the kind of device the cfg selects, which the buffers need; nothing is +//! encoded or submitted. + +use super::blit_holds::{device, Scratch}; +use crate::write_routes::{AtCommit, Routes}; +use crate::{IndirectCommandBufferDescriptor, ResourceOptions}; +use metal2vulkan::metal::IndirectCommandType; +use metal2vulkan::pipeline::CommandWrites as W; + +fn draws(primitive_types: u8) -> W { + W { + operations: W::DRAW, + primitive_types, + } +} + +/// A buffer made resident for writing takes what every state set in its command buffer may +/// write: one set in a later encoder, one set before it was made resident, as each is set, and +/// one set in an encoder open beside its own (a parallel render encoder's) at the commit; a buffer +/// the command buffer did not make resident takes nothing, and the commit lets the routes go. +#[test] +fn a_route_reaches_every_buffer_its_command_buffer_made_writable() { + let scratch = Scratch::new("write-routes"); + let device = device(&scratch.0); + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::DRAW; + descriptor.inherit_pipeline_state = true; + let shared = ResourceOptions::shared(); + let icb = || (device.new_indirect_command_buffer(&descriptor, 1, shared)).unwrap(); + let (before, after, beside, next) = (icb(), icb(), icb(), icb()); + let functions = || vec!["writer".to_owned()]; + let mut command_buffer = Routes::default(); + let mut first = command_buffer.carried(); + first.writable(&before); + command_buffer.join(first); + assert!(before.command_writes().is_empty()); + assert!(!before.gpu_written()); + let mut second = command_buffer.carried(); + second.state_set(draws(0b1), functions); + assert_eq!(before.command_writes(), draws(0b1)); + assert!(before.gpu_written()); + second.writable(&after); + assert_eq!(after.command_writes(), draws(0b1)); + command_buffer.join(second); + let (mut one, mut other) = (command_buffer.carried(), command_buffer.carried()); + one.writable(&beside); + other.state_set(draws(0b10), functions); + assert_eq!(beside.command_writes(), draws(0b1)); + assert_eq!(before.command_writes(), draws(0b11)); + command_buffer.join(one); + command_buffer.join(other); + assert_eq!(beside.command_writes(), draws(0b1)); + let (decided, followed) = command_buffer.at_commit(); + assert!(matches!(decided, AtCommit::Unmarked(0, None))); + followed.mark(); + for icb in [&before, &after, &beside] { + assert_eq!(icb.command_writes(), draws(0b11)); + assert!(icb.gpu_written()); + } + assert!(next.command_writes().is_empty()); + command_buffer.writable(&next); + assert!(next.command_writes().is_empty()); + assert!(!next.gpu_written()); +} diff --git a/crates/metal2vulkan-runtime/src/tests/written_commands.rs b/crates/metal2vulkan-runtime/src/tests/written_commands.rs index a888dca14..f5db4e471 100644 --- a/crates/metal2vulkan-runtime/src/tests/written_commands.rs +++ b/crates/metal2vulkan-runtime/src/tests/written_commands.rs @@ -7,7 +7,7 @@ use metal2vulkan::bind::indirect_commands::{Action, CommandRecord}; use metal2vulkan::pipeline::CommandWrites as W; use crate::write_routes::Unmarked; -use crate::written_commands::{Named, Writers, WrittenCommands}; +use crate::written_commands::{Named, WrittenCommands}; fn draws(primitive_types: u8) -> W { W { @@ -112,29 +112,6 @@ fn a_copy_gains_what_its_source_gains() { } } -/// An encoder's writers: a buffer made resident takes the state already set and each set after, -/// and an execution's named states, however many times it is made resident; a buffer it did not -/// make resident takes none. -#[test] -fn an_encoder_adds_its_states() { - let buffer = Arc::new(WrittenCommands::default()); - let other = Arc::new(WrittenCommands::default()); - let mut writers = Writers::default(); - writers.state_set(draws(0b1)); - assert!(buffer.writes().is_empty()); - writers.made_writable(&buffer); - writers.made_writable(&buffer); - assert_eq!(buffer.writes(), draws(0b1)); - writers.state_set(draws(0b100)); - writers.runs(W { - operations: W::DISPATCH, - primitive_types: 0, - }); - assert_eq!(buffer.writes().operations, W::DRAW | W::DISPATCH); - assert_eq!(buffer.writes().primitive_types, 0b101); - assert!(other.writes().is_empty()); -} - /// A commit's unmarked buffers (`write_routes.rs`) are held by an executed buffer's summary until /// it first names a state whose functions may write records, which sets them off once, through a /// blit copy's destination too; a summary that names one already holds none. diff --git a/crates/metal2vulkan-runtime/src/write_routes.rs b/crates/metal2vulkan-runtime/src/write_routes.rs index 04001a8c0..572aeef6a 100644 --- a/crates/metal2vulkan-runtime/src/write_routes.rs +++ b/crates/metal2vulkan-runtime/src/write_routes.rs @@ -11,7 +11,16 @@ //! buffer is written by the GPU where its command buffer also runs a function that may call one: //! a state set in any of its encoders, before the residency or after it, whose functions' //! `indirect_command_writes` is not empty; or an execution in any of them of indirect commands -//! whose records may name such a state (`WrittenCommands::named`). +//! whose records may name such a state (`WrittenCommands::named`). Metal runs the commands such +//! a function writes with the buffer resident for writing in any encoder of the command buffer, +//! one before the function's or one after it (the probe +//! `crates/metal2vulkan-conformance/probes/icb_routed_writes.m`). +//! - **What they write.** What every route of the command buffer may write joins what the records +//! of every buffer it made resident for writing may hold (`written_commands.rs`), at the call +//! that brings either (for two encoders open together, a parallel render encoder's, at the +//! commit): an execution decides on that summary (`render/executions/gpu.rs` runs draws of the +//! one primitive type it allows on the GPU alone), so it holds what a writer in another encoder +//! may write as it holds a writer's in the encoder that made the buffer resident. //! - **When.** As soon as both meet, at the call that completes the pair: an encoder opens with //! what its command buffer's ended encoders met ([`Routes::carried`]), so a later encoder's //! execution of the buffer is encoded as one of records the GPU may write, a part of its own @@ -120,12 +129,15 @@ fn operation_names(writes: CommandWrites) -> Vec<&'static str> { } /// The routes an encoder, and then its command buffer, met (the module docs): what it made -/// resident for writing, the first state set that may write records, and what it executed under -/// the states its records name, each buffer once. +/// resident for writing, the first state set that may write records, what they all may write, +/// and what it executed under the states its records name, each buffer once. #[derive(Debug, Default)] pub(crate) struct Routes { writable: Vec, writer: Option, + /// What the states set may write, and the states the executed buffers' records may name, as + /// each was met: what a buffer made resident for writing gains (`WrittenCommands::add_writer`). + writes: CommandWrites, executed: Vec, } @@ -136,19 +148,21 @@ impl Routes { Routes { writable: self.writable.clone(), writer: self.writer.clone(), + writes: self.writes, executed: self.executed.clone(), } } - /// The encoder made `icb` resident for writing: marked at once where a route that may write - /// its records met the command buffer. + /// The encoder made `icb` resident for writing: its records may hold what the routes the + /// command buffer met may write, and it is marked at once where one met it. pub(crate) fn writable(&mut self, icb: &IndirectCommandBuffer) { - if icb.gpu_written() { - return; - } if !self.writable.contains(icb) { self.writable.push(icb.clone()); } + icb.written().add_writer(self.writes).mark(); + if icb.gpu_written() { + return; + } if let Some(writer) = self.writer() { icb.written_by_gpu(writer); } @@ -156,7 +170,8 @@ impl Routes { /// The encoder set a state whose functions may call `writes`' operations, of the functions /// `functions` names, asked for the first such state alone: where they may call one, the - /// buffers made resident for writing are marked at once. + /// records of the buffers made resident for writing may hold what they write, and the buffers + /// are marked at once. pub(crate) fn state_set( &mut self, writes: CommandWrites, @@ -165,6 +180,7 @@ impl Routes { if writes.is_empty() { return; } + self.wrote(writes); let writer = self.writer.get_or_insert_with(|| GpuWriter::State { functions: functions(), writes, @@ -173,17 +189,28 @@ impl Routes { } /// The encoder executed commands of `icb` under the states their records name: where they may - /// name one whose functions may write records, the buffers made resident for writing are - /// marked at once. + /// name one whose functions may write records, the records of the buffers made resident for + /// writing may hold what those write, and the buffers are marked at once. pub(crate) fn executed(&mut self, icb: &IndirectCommandBuffer) { if !self.executed.contains(icb) { self.executed.push(icb.clone()); } - if !icb.written().named().writes.is_empty() { + let named = icb.written().named().writes; + if !named.is_empty() { + self.wrote(named); mark(&self.writable, &GpuWriter::Execution); } } + /// A route that may write `writes`: the records of every buffer made resident for writing may + /// hold them, and of every one made resident later. + fn wrote(&mut self, writes: CommandWrites) { + self.writes = self.writes.union(writes); + for icb in &self.writable { + icb.written().add_writer(writes).mark(); + } + } + /// The route that may write records met so far: the first state set, else an execution whose /// records may name one. fn writer(&self) -> Option { @@ -198,11 +225,14 @@ impl Routes { } } - /// An ended encoder's routes join its command buffer's. + /// An ended encoder's routes join its command buffer's. An encoder opened with what those + /// ended before it met, so its buffers took their writes as it ran; what two encoders open + /// together (a parallel render encoder's) did not see of each other's, the commit adds. pub(crate) fn join(&mut self, ended: Routes) { let Routes { writable, writer, + writes, executed, } = ended; for icb in writable { @@ -218,19 +248,28 @@ impl Routes { self.executed.push(icb); } } + self.writes = self.writes.union(writes); } /// What the commit decides (the module docs): the buffers to mark, each with what met it, or /// those it leaves unmarked, which an executed buffer's records may yet mark ([`Unmarked`], - /// held by the command buffer until it completes; `None` where it executed none). The routes - /// hold no buffer after it: a buffer the command buffer made resident or executed lives no - /// longer for them than for its work, which releases it as it completes. - pub(crate) fn at_commit(&mut self) -> AtCommit { + /// held by the command buffer until it completes; `None` where it executed none); and the + /// buffers the summaries' gains set off ([`Followed`]), which the caller marks without its + /// lock: each buffer made resident for writing takes what every route of the command buffer + /// may write, first. The routes hold no buffer after it: a buffer the command buffer made + /// resident or executed lives no longer for them than for its work, which releases it as it + /// completes. + pub(crate) fn at_commit(&mut self) -> (AtCommit, Followed) { + let mut followed = Followed::default(); + for icb in &self.writable { + followed.0.extend(icb.written().add_writer(self.writes).0); + } let decided = self.decide(); self.writable.clear(); self.writer = None; + self.writes = CommandWrites::NONE; self.executed.clear(); - decided + (decided, followed) } /// [`at_commit`](Self::at_commit)'s decision. diff --git a/crates/metal2vulkan-runtime/src/written_commands.rs b/crates/metal2vulkan-runtime/src/written_commands.rs index 0ee927814..fdec19ab6 100644 --- a/crates/metal2vulkan-runtime/src/written_commands.rs +++ b/crates/metal2vulkan-runtime/src/written_commands.rs @@ -4,16 +4,17 @@ //! //! - **The routes.** The CPU's setters add what the record holds after each edit: its draw or //! dispatch (a draw's primitive type among them), its barrier, and the state it names, with -//! what that state's functions may write and whether they may encode. An encoder that made the -//! buffer resident for writing adds what its states may write (`indirect_command_writes`, read -//! from their functions' AIR): the state set when the buffer was made resident, each set after, -//! and the states an execution in the encoder may run (the executed buffer's named states; -//! inherited states are the encoder's own). A blit's copy adds the source's, and for good: the -//! source's later gains reach the copy too, since what it copies is the source's records when -//! the GPU runs it. A reset, the CPU's or a blit's, adds nothing. Nothing is taken away: a -//! record written again keeps the earlier writes in the summary, as a GPU write may stay. +//! what that state's functions may write and whether they may encode. A command buffer that made +//! the buffer resident for writing, in any of its encoders, adds what every route it met may +//! write (`write_routes.rs`): each state set in any of its encoders, before the residency or +//! after it (`indirect_command_writes`, read from their functions' AIR), and the states an +//! execution in any of them may run (the executed buffer's named states; inherited states are +//! the encoder's own). A blit's copy adds the source's, and for good: the source's later gains +//! reach the copy too, since what it copies is the source's records when the GPU runs it. A +//! reset, the CPU's or a blit's, adds nothing. Nothing is taken away: a record written again +//! keeps the earlier writes in the summary, as a GPU write may stay. //! - **What it is for.** A clear bit is proven of every write encoded so far; a route encoded -//! later (an encoder that makes the buffer resident for writing after a commit read the +//! later (a command buffer that makes the buffer resident for writing after a commit read the //! summary) adds its own then. A commit that left a buffer unmarked though its command buffer //! made it resident for writing (`write_routes.rs`) is followed by the buffers it executed: //! where one's summary first names a state whose functions may write records, it marks them. @@ -191,41 +192,3 @@ impl WrittenCommands { self.add(writes, named) } } - -/// The indirect command buffers an encoder made resident for writing, and what its state set last -/// may write into them: Metal's `useResource:usage:` reaches the encoder's later commands, so a -/// buffer made resident takes the state already set and every state set after it. -#[derive(Debug, Default)] -pub(crate) struct Writers { - open: Vec>, - /// What the state set last may write. - state: CommandWrites, -} - -impl Writers { - /// The encoder made the buffer of `written` resident for writing. - pub(crate) fn made_writable(&mut self, written: &Arc) { - if self.open.iter().any(|open| Arc::ptr_eq(open, written)) { - return; - } - written.add_writer(self.state).mark(); - self.open.push(Arc::clone(written)); - } - - /// The encoder set a state whose functions may write `writes`. - pub(crate) fn state_set(&mut self, writes: CommandWrites) { - self.state = writes; - self.runs(writes); - } - - /// The encoder runs functions that may write `writes` (an execution's commands under states - /// their records name). - pub(crate) fn runs(&self, writes: CommandWrites) { - if writes.is_empty() { - return; - } - for open in &self.open { - open.add_writer(writes).mark(); - } - } -} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs b/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs index ad6795c08..c19597c56 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs @@ -118,14 +118,14 @@ pub(super) fn state(device: &Device, stem: &str, indirect: bool) -> ComputePipel .unwrap() } -/// Set a state that may encode indirect commands (`icb_replay_encode`'s, made without +/// Set a state that may write indirect commands (`icb_reset_encode`'s, made without /// `supportIndirectCommandBuffers`, so no record names it) in an encoder of `command_buffer` of its /// own that runs nothing: a buffer the command buffer makes resident for writing, before or after, /// may be written by the GPU (`IndirectCommandBuffer::gpu_written`), and what its records may hold -/// gains nothing of the state, which the encoder that made it resident did not set -/// (`IndirectCommandBuffer::command_writes`). +/// (`IndirectCommandBuffer::command_writes`) gains the state's resets alone, which neither form of +/// an execution the GPU alone runs rules out, and no state it may name. pub(super) fn encoding_state_set(command_buffer: &CommandBuffer) { - let state = state(command_buffer.device(), "icb_replay_encode", false); + let state = state(command_buffer.device(), "icb_reset_encode", false); let mut encoder = command_buffer.compute_command_encoder().unwrap(); encoder.set_compute_pipeline_state(&state).unwrap(); encoder.end_encoding().unwrap(); diff --git a/docs/API.md b/docs/API.md index 0225a3422..74837e0b7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3803,10 +3803,15 @@ each item named states the rule in full. it. From then on an execution runs the records as they stand once the work before it completed. A buffer made resident for writing alongside functions that call no such operation stays unmarked, and its executions run the CPU's records as a buffer made resident for reading - does (`WorkCounts::indirect_write_residencies_unmarked` counts each such commit's buffers). A - buffer no encoder of the command buffer made resident for writing is outside Metal's contract for - a function's write: its executions run the CPU's records, where Metal on an M3 Max ran a compute - execution's commands as the kernel wrote them (and a render execution's as the CPU did). + does (`WorkCounts::indirect_write_residencies_unmarked` counts each such commit's buffers). What + the records may hold (`IndirectCommandBuffer::command_writes`) takes what each such state, and + each state such an execution's records may name, may write, in whichever encoder of the command + buffer it was set: Metal on an M3 Max runs what a kernel wrote with the buffer resident for + writing only in an encoder before the kernel's or after it, as it does with the buffer resident + in the kernel's own. A buffer no encoder of the command buffer made resident for writing is + outside Metal's contract for a function's write: its executions run the CPU's records, where + Metal on an M3 Max ran a compute execution's commands as the kernel wrote them (and a render + execution's as the CPU did). - **What an execution runs, where its records are read** (`CommandBuffer::add_executed_commands_handler`, no Metal counterpart): a handler is called each time the runtime reads an execution's records, at `commit`, in a host step, or not at all for an