diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index 5accbe62e..e0a0098bd 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -71,6 +71,7 @@ use super::address::{AddressPlacement, AddressSpace, Place, RecordsId}; use super::buffer::{Buffer, Modes}; use super::degradation::{Degradation, NoticesFor}; use super::depth_stencil::DepthStencilState; +use super::device::carry::CallHold; use super::device::lock; use super::error::{internal, invalid, Error, IndirectRule, MemoryKind, Problem, RuntimeGap}; use super::families::GpuFamily; @@ -78,8 +79,8 @@ use super::indirect_execute::Unforeseen; use super::kept_commands::{Kept, KeptChunk, KeptChunks, StepChunk, StepCopy, UnlockedWrites}; use super::memory::MemoryClass; use super::pipeline::{ - ComputePipelineState, ComputeProgram, RenderPipelineState, RenderProgram, RenderStateKind, - WeakComputePipelineState, WeakRenderPipelineState, + ComputePipelineState, ComputeProgram, GraphicsState, RenderPipelineState, RenderProgram, + RenderStateKind, WeakComputePipelineState, WeakRenderPipelineState, }; use super::released_programs::{KeptProgram, KeptPrograms}; use super::render::{index_size, DrawProblem}; @@ -115,9 +116,11 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, OnceLock, Weak}; pub(crate) mod carry; +mod set_each; mod set_states; -use set_states::{Keyed, SetStates, StateKey}; +pub use set_each::{IndirectComputeSet, IndirectRenderSet}; +use set_states::{Keyed, SetKeys, SetStates, StateKey}; /// The threadgroup lengths a fresh descriptor binds, as Metal's answers. const DEFAULT_THREADGROUP_LENGTHS: u32 = 31; @@ -651,6 +654,8 @@ impl Device { } let options = ResourceOptions::new(modes.storage, modes.cache, storage.hazard_tracking_mode()); + let states = CommandStates::new(size); + let set_keys = states.states.keys(); let icb = IndirectCommandBuffer(Arc::new(IcbShared { storage, id, @@ -660,13 +665,15 @@ impl Device { inheritance, size, options, - pipelines: Mutex::new(CommandStates::new(size)), + pipelines: Mutex::new(states), + set_keys, unlocked: UnlockedWrites::new(size), gpu_written: AtomicBool::new(false), gpu_writer: OnceLock::new(), unforeseen: Mutex::new(Vec::new()), cpu_writes: AtomicU64::new(0), unlocked_writes: AtomicU64::new(0), + added_writes: AtomicU64::new(0), barriers: AtomicBool::new(false), nines: AtomicBool::new(false), overflow, @@ -743,12 +750,40 @@ impl CommandStates { } /// A pipeline state a setter sets on a command ([`IndirectCommandBuffer::edit`]): its key, what -/// it may do to indirect command buffers, and the command's state made of it, which only a set -/// where no command names the state yet makes ([`SetStates::set`]). +/// it may do to indirect command buffers, its stamp, and the state under the setter's hold, of +/// which only a set where no command names the state yet makes the command's state +/// ([`make`](Self::make), [`SetStates::set`]). struct StateSet<'s> { key: StateKey, named: Named, - make: &'s dyn Fn() -> CommandState, + stamp: IdStamp, + of: StateOf<'s>, +} + +/// The state a [`StateSet`] sets, and the setter's hold of it, under which its program is read. +enum StateOf<'s> { + Compute(&'s ComputePipelineState, &'s CallHold<'s>), + Render(&'s GraphicsState, &'s CallHold<'s>), +} + +impl StateSet<'_> { + /// The command's state: the state, named without holding it, and its program. + fn make(&self) -> CommandState { + match self.of { + StateOf::Compute(state, held) => CommandState::Compute( + state.downgrade(), + state.program_held(held), + self.stamp, + self.named, + ), + StateOf::Render(graphics, held) => CommandState::Render( + graphics.downgrade(), + graphics.program_held(held), + self.stamp, + self.named, + ), + } + } } /// The ID a state had when the CPU set it on a command, and the serial of its hold of the ID, @@ -1103,11 +1138,14 @@ struct IcbShared { /// The options asked for, the hazard tracking mode resolved. options: ResourceOptions, /// The pipeline state the CPU set on each command, and the kept recordings of its chunks - /// ([`CommandStates`]). Held while the CPU writes a record, but for a buffer slot's setter's - /// write, which takes no lock ([`edit_unlocked`](IndirectCommandBuffer::edit_unlocked)). + /// ([`CommandStates`]). Held while the CPU writes a record, but for a setter's write that sets + /// no state and adds nothing to what the records may hold, which takes no lock ([`Batch`]). pipelines: Mutex, - /// The chunks a buffer slot's setter wrote without `pipelines`, for the kept recordings' - /// next look to forget ([`UnlockedWrites`]). + /// The key of the state each command names, as `pipelines`' table holds it, for a pipeline + /// setter's look without the lock ([`SetKeys`]). + set_keys: Arc, + /// The chunks a setter wrote without `pipelines` ([`Batch`]), for the kept recordings' next + /// look to forget ([`UnlockedWrites`]). unlocked: UnlockedWrites, /// A route that may write the buffer's records met it (`write_routes.rs`): the GPU may write /// them from then on (`written_by_gpu`). Set, and read where it decides, under @@ -1122,12 +1160,16 @@ struct IcbShared { /// ([`cpu_writes`](IndirectCommandBuffer::cpu_writes)). Each adds one after it wrote, under /// the lock, by a load and a store (`Release`), and its readers load it without the lock. cpu_writes: AtomicU64, - /// The buffer slots' setters' writes so far without `pipelines` - /// ([`edit_unlocked`](IndirectCommandBuffer::edit_unlocked)), counted apart: each adds one - /// after it wrote, by an atomic add (`Release`). Counted in one with the writes under the - /// lock, each of those took an atomic add too, and a draw took about 4 ns more - /// (`tests::icb_encode_bench`). + /// The setters' writes so far without `pipelines` ([`Batch`]), counted apart: a batch adds + /// its writes after it made them, by an atomic add (`Release`). Counted in one with the + /// writes under the lock, each of those took an atomic add too, and a draw took about 4 ns + /// more (`tests::icb_encode_bench`). unlocked_writes: AtomicU64, + /// What the CPU's edits added to what the records may hold (`CommandStates::added`'s + /// writes, [`packed`]), for an edit's look without `pipelines` + /// ([`adds_nothing`](IndirectCommandBuffer::adds_nothing)): set under the lock with the + /// summary (`Release`), which only grows, so a write it holds stays held. + added_writes: AtomicU64, /// The CPU set a render command's barrier: an execution's render pass instance may split at /// it (`render/executions.rs`), for good. barriers: AtomicBool, @@ -1182,12 +1224,14 @@ const _: fn(&IcbShared) = |icb| { size, options, pipelines, + set_keys, unlocked, gpu_written, gpu_writer, unforeseen, cpu_writes, unlocked_writes, + added_writes, barriers, nines, overflow, @@ -1206,12 +1250,14 @@ const _: fn(&IcbShared) = |icb| { shared(size); shared(options); shared(pipelines); + shared(set_keys); shared(unlocked); shared(gpu_written); shared(gpu_writer); shared(unforeseen); shared(cpu_writes); shared(unlocked_writes); + shared(added_writes); shared(barriers); shared(nines); shared(overflow); @@ -1625,16 +1671,16 @@ impl IndirectCommandBuffer { .map_err(|_| internal("an indirect command buffer's header was refused")) } - /// Set command `index`'s written flag (below the size) where `written`, else clear it: the - /// CPU's record writes keep it, as a kernel's do (`written_flag_offset`), so a blit's reset - /// writes only the records a flag marks (`blit_indirect.rs`). A clear flag is a record that - /// holds the reset record. - fn mark(&self, index: u32, written: bool) { + /// Set command `index`'s written flag (below the size) where `written`, else clear it, in + /// `storage`, the storage's bytes ([`bytes`](Self::bytes)): the CPU's record writes keep it, + /// as a kernel's do (`written_flag_offset`), so a blit's reset writes only the records a flag + /// marks (`blit_indirect.rs`). A clear flag is a record that holds the reset record. + fn mark_at(&self, storage: *mut u8, index: u32, written: bool) { let at = written_flag_offset(self.0.size, index) as usize; // SAFETY: the flag of a command below the size lies inside the mapped storage. A GPU // write to it meanwhile is the program's race, as one to the record is. unsafe { - self.bytes() + storage .add(at) .cast::() .write_unaligned(u32::from(written)); @@ -1693,18 +1739,19 @@ impl IndirectCommandBuffer { record .encode_into(&mut bytes) .map_err(|_| internal("an indirect command's record does not encode"))?; - // SAFETY: as `record`'s; the CPU's writes are one at a time (`pipelines`), but a buffer - // slot's setter's, without it (`edit_unlocked`): one of the same command meanwhile is the - // program's race, as two threads encoding one command are on Metal. + let storage = self.bytes(); + // SAFETY: as `record`'s; the CPU's writes are one at a time (`pipelines`), but a setter's + // without it (`Batch`): one of the same command meanwhile is the program's race, as two + // threads encoding one command are on Metal. unsafe { std::ptr::copy_nonoverlapping( bytes.as_ptr(), - self.bytes().add(record_offset(index)), + storage.add(record_offset(index)), COMMAND_BYTES, ); } // A reset record, all zero, needs no reset. - self.mark(index, bytes.iter().any(|&byte| byte != 0)); + self.mark_at(storage, index, bytes.iter().any(|&byte| byte != 0)); self.wrote(commands, index..index + 1); Ok(()) } @@ -1730,6 +1777,14 @@ impl IndirectCommandBuffer { locked.wrapping_add(self.0.unlocked_writes.load(Ordering::Acquire)) } + /// [`cpu_writes`](Self::cpu_writes) apart: those under `pipelines`, and the setters' without + /// it ([`Batch`]), for a device test of which way a write went. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(super) fn writes_apart(&self) -> (u64, u64) { + let locked = self.0.cpu_writes.load(Ordering::Acquire); + (locked, self.0.unlocked_writes.load(Ordering::Acquire)) + } + /// Reset commands `range` (inside the size). fn reset_commands(&self, range: Range) -> Result<(), Error> { let mut commands = lock(&self.0.pipelines); @@ -1929,36 +1984,62 @@ impl IndirectCommandBuffer { && types.intersects(COMPUTE_TYPES)) } - /// Write `field` into command `index`'s record (below the size), and where the field names a - /// pipeline state, `set`'s as the state the CPU set on it: the field's words alone, in place, as - /// Metal's setter writes its field, the rest of the record as the GPU and the CPU left it - /// (`CommandRecord::write_field`); then what the record holds joins what its records may - /// (`grown_writes`, `add_written`). All of it under one hold of `pipelines`; a buffer slot's - /// takes [`edit_slot`](Self::edit_slot). - fn edit(&self, index: u32, field: RecordField, set: Option>) -> Result<(), Error> { - let mut commands = lock(&self.0.pipelines); - // SAFETY: the record of a command below the size lies inside the mapped storage, and the - // CPU's writes of the records are one at a time (`pipelines`, held while the slice lives), - // but a buffer slot's setter's (`edit_unlocked`): one of the same command meanwhile, as a - // GPU write to it, is the program's race, as on Metal. - let bytes = unsafe { - std::slice::from_raw_parts_mut(self.bytes().add(record_offset(index)), COMMAND_BYTES) + /// Write `field`, which names no pipeline state, into command `index`'s record (below the + /// size): a [`Batch`] of it alone. + fn edit(&self, index: u32, field: RecordField) -> Result<(), Error> { + self.batch(index).field(field, Then::Nothing) + } + + /// Write `field` into command `index`'s record (below the size) in `storage`, the storage's + /// bytes ([`bytes`](Self::bytes)): the field's words alone, in place, as Metal's setter writes + /// its field, the rest of the record as the GPU and the CPU left it + /// (`CommandRecord::write_field`). Every setter's write of a field, under `pipelines` or not, + /// is this one. A record that does not decode is a GPU write's, the program's race. + #[inline] + fn write_field(&self, storage: *mut u8, index: u32, field: RecordField) -> Result<(), Error> { + // SAFETY: the record of a command below the size lies inside the mapped storage. Another + // write of the same command meanwhile, under `pipelines` or not, is the program's race, as + // two threads encoding one command, or a GPU write to it, are on Metal. + let record = unsafe { + std::slice::from_raw_parts_mut(storage.add(record_offset(index)), COMMAND_BYTES) }; - CommandRecord::write_field(bytes, field).map_err(|error| match error { + CommandRecord::write_field(record, field).map_err(|error| match error { RecordError::Length { .. } | RecordError::Action { .. } | RecordError::Barrier { .. } | RecordError::PipelineState => undecodable(), _ => internal("an indirect command's record does not take a setter's field"), })?; - self.mark(index, true); + // Its written flag set after the field, as every write of a record but the reset + // record's sets it. + self.mark_at(storage, index, true); + Ok(()) + } + + /// [`write_field`](Self::write_field) of `field` with `set` as the state the CPU set on the + /// command where the field names one, under one hold of `pipelines`; then what the record + /// holds joins what its records may (`grown_writes`, `add_written`). A [`Batch`] takes it + /// for a field it cannot write without the lock. + fn edit_locked( + &self, + storage: *mut u8, + index: u32, + field: RecordField, + set: Option>, + ) -> Result<(), Error> { + let mut commands = lock(&self.0.pipelines); + self.write_field(storage, index, field)?; self.wrote(&mut commands, index..index + 1); + // SAFETY: as `write_field`'s; the CPU's writes of the records under `pipelines`, held + // while the slice lives, are one at a time. + let record = + unsafe { std::slice::from_raw_parts(storage.add(record_offset(index)), COMMAND_BYTES) }; let named = set.as_ref().map(|set| set.named); // Read before the state is set, so a state set is always added: only a GPU write since // the field's, the program's race, leaves a record that does not decode here. - let grown = self.grown_writes(&commands, bytes, field, named)?; + let grown = self.grown_writes(&commands, record, field, named)?; if let Some(set) = set { - commands.states.set(index, set.key, set.make); + commands.states.set(index, set.key, || set.make()); } let followed = match grown { Some(writes) => self.add_written(&mut commands, index, writes, named), @@ -1971,40 +2052,25 @@ impl IndirectCommandBuffer { Ok(()) } - /// [`edit`](Self::edit) of `field`, a buffer slot: without `pipelines` - /// ([`edit_unlocked`](Self::edit_unlocked)) on a buffer the GPU may not have written, else - /// under it. Apart from `edit`, whose other writes then compile as they did before the - /// unlocked write: beside it in `edit`, a draw took about 5 ns more - /// (`tests::icb_encode_bench`). - fn edit_slot(&self, index: u32, field: RecordField) -> Result<(), Error> { - match self.gpu_written() { - false => self.edit_unlocked(index, field), - true => self.edit(index, field, None), + /// Whether an edit of `field` that sets no state adds nothing to what the records may hold, + /// as [`grown_writes`](Self::grown_writes) would find it, without `pipelines`: on a buffer the + /// GPU may not have written, whose actions and barriers the CPU's edits all added, so that a + /// record's other fields add nothing, a field of another kind adds nothing, and an action, or + /// a barrier set, adds what it holds where the edits did not add it already (`added_writes`). + fn adds_nothing(&self, field: RecordField) -> bool { + if self.gpu_written() { + return false; } - } - - /// [`edit`](Self::edit) of `field`, a buffer slot, into command `index`'s record (below the - /// size) without `pipelines`, on a buffer the GPU may not have written: there such an edit - /// names no state and adds nothing to what the records may hold (`grown_writes` reads no record - /// for it), so the count of the CPU's writes and the kept recordings of the command's chunk - /// alone follow it, as they follow a write under the lock ([`wrote`](Self::wrote)): the chunk - /// noted for the kept recordings' next look to forget ([`UnlockedWrites`], - /// `KeptChunks::settle`), then the write counted, by an atomic add. A buffer the GPU starts - /// writing meanwhile is the race a write under the lock has with it too (`gpu_written` is set - /// under `unforeseen`'s lock). The lock's two locked instructions were most of a buffer - /// setter's cost (`tests::icb_encode_bench`). - fn edit_unlocked(&self, index: u32, field: RecordField) -> Result<(), Error> { - // SAFETY: the record of a command below the size lies inside the mapped storage. Another - // write of the same command meanwhile, under `pipelines` or not, is the program's race, as - // two threads encoding one command, or a GPU write to it, are on Metal. - let bytes = unsafe { - std::slice::from_raw_parts_mut(self.bytes().add(record_offset(index)), COMMAND_BYTES) + let writes = match field { + RecordField::Action(action) => action.writes(), + RecordField::Barrier(true) => CommandWrites { + operations: CommandWrites::BARRIER, + primitive_types: 0, + }, + _ => return true, }; - CommandRecord::write_field(bytes, field).map_err(field_error)?; - self.mark(index, true); - self.0.unlocked.wrote(index); - self.0.unlocked_writes.fetch_add(1, Ordering::Release); - Ok(()) + let added = self.0.added_writes.load(Ordering::Acquire); + added | packed(writes) == added } /// What `record`, command `index`'s after an edit of `field`, holds (`RecordView::writes`: its @@ -2067,10 +2133,9 @@ impl IndirectCommandBuffer { primitive_types: 0, }; let (added, added_named) = commands.added; - commands.added = ( - added.union(writes).union(pipeline), - added_named.union(named.unwrap_or_default()), - ); + let added = added.union(writes).union(pipeline); + commands.added = (added, added_named.union(named.unwrap_or_default())); + self.0.added_writes.store(packed(added), Ordering::Release); followed } @@ -2091,68 +2156,30 @@ impl IndirectCommandBuffer { self.clear_overflow(&mut commands, index..index + 1) } - /// Set `set`'s buffer on command `index`, for `call`, at the buffer's address plus the offset's - /// low 32 bits, as Metal keeps it: on an M4 under macOS 27.0.1, a kernel or vertex buffer set - /// at 2^40 + 16 or 2^32 + 32, plainly or with an attribute stride, read and answered 16 or 32 - /// past the buffer's address, and so did a fragment buffer's getter; one set at 2^32 + 16 in a - /// buffer past 4 GiB read at 16, where a compute encoder's own buffer reads at the whole offset - /// (with and without API validation; the consumer measured the getters so on an M3 Max), and so - /// did object and mesh buffers' getters and reads. Refused (`Invalid`): a buffer of another - /// device. On a buffer that inherits its buffers the record keeps it and reads it back, and an - /// execution binds the encoder's buffers alone, none where the encoder's slot is unbound, as - /// plain Metal's, where its validation stops the call. - /// - /// A slot at or past the set's limit (the descriptor's count of the stage) is **taken** and - /// kept nowhere. Plain Metal takes it: on an M3 Max under macOS 27.0.1 a buffer of - /// `maxKernelBufferBindCount` 2 took `setKernelBuffer:offset:atIndex:` at index 2 and at - /// index 4 with no error and its `size` still read 3 commands, where Metal's own API - /// validation stops the call ("index(2) must be < 2."). Nothing of either side binds such a - /// slot to a function: an execution binds the slots below the count alone (`bindings` in - /// `indirect_execute.rs`, `buffers` in `render/executions.rs`), as Metal's count-sized - /// argument table does, and [`read_buffer`](Self::read_buffer) answers none at or past the - /// count. So the binding is dropped rather than written: the record's own slots are held to - /// the descriptor's counts by the engine (`indirect_commands::descriptor`'s `validate_record`, - /// which an execution's records and the records the GPU writes both go through), and what - /// Metal keeps such a slot in for its private getters is unmeasured. - fn set_buffer(&self, index: u32, call: &'static str, set: BufferSet<'_>) -> Result<(), Error> { - let BufferSet { - stage, - limit, - slot, - buffer, - offset, - attribute_stride, - } = set; - if let Some(buffer) = buffer { - if !buffer.generation().same(self.generation()) { - return Err(buffer.generation().foreign(call, self.generation())); + /// A [`Batch`] of a setter's writes, or of the setters `set_each` names, on command `index` + /// (below the size). + fn batch(&self, index: u32) -> Batch<'_> { + Batch { + icb: self, + index, + storage: self.bytes(), + unlocked: 0, + } + } + + /// Note `then`, after a field's write: a draw of primitive type 9 marks the buffer for the + /// executions that look for one ([`nine_among`](Self::nine_among)); a render command's + /// barrier, for the executions whose render pass instances may split at it. + fn then(&self, then: Then) { + match then { + Then::Nothing => {} + Then::Drew(primitive_type) => { + if primitive_type == DrawPrimitiveType::Nine { + self.0.nines.store(true, Ordering::Release); + } } + Then::Barrier => self.0.barriers.store(true, Ordering::Release), } - // At or past the descriptor's count nothing binds the slot and no getter reads it, so - // the binding is kept nowhere; the call is still taken, as plain Metal takes it. - if slot >= limit { - return Ok(()); - } - // Metal keeps the offset's low 32 bits: its getters answer them and the GPU reads - // there, inside a buffer past 4 GiB too. A nil buffer keeps them too, as its address: - // Metal's getters answer them and its function sees them as the buffer's address. - let base = buffer.map_or(0, |buffer| buffer.gpu_address().0); - let binding = BufferBinding { - address: base + u64::from(offset as u32), - attribute_stride, - }; - // Past the slots the record holds no function binds a buffer (Metal's argument tables - // hold 31): plain Metal keeps it for its private getters, and so does the overflow. - if slot >= RECORD_BUFFERS { - let mut commands = lock(&self.0.pipelines); - return self.write_overflow(&mut commands, index, stage, slot, Some(binding)); - } - let field = RecordField::Buffer { - stage, - slot: slot as usize, - binding: Some(binding), - }; - self.edit_slot(index, field) } /// Where Metal keeps threadgroup length `slot` of command `index` of a compute buffer: at @@ -2196,7 +2223,7 @@ impl IndirectCommandBuffer { index: slot as usize, length, }; - self.edit(command, field, None) + self.edit(command, field) } LengthPlace::Overflow(at) => { let _pipelines = lock(&self.0.pipelines); @@ -3882,6 +3909,137 @@ pub(super) struct WrittenRecords { pub(super) records: Vec)>>, } +/// What the buffer notes after a field's write ([`IndirectCommandBuffer::then`]). +#[derive(Clone, Copy)] +enum Then { + Nothing, + /// A draw of the primitive type. + Drew(DrawPrimitiveType), + /// A render command's barrier. + Barrier, +} + +/// One command's setters' writes, made in turn as each setter's checks pass: a setter's alone, or +/// the setters a call of `set_each` names (`set_each.rs`), in the storage's bytes as the batch +/// found them, each setter writing into the batch rather than handing its write back (a plan +/// value handed back cost a buffer setter about 9 ns, `tests::icb_encode_bench`). A field that sets no state and adds nothing to +/// what the records may hold ([`adds_nothing`](IndirectCommandBuffer::adds_nothing)) is written +/// without `pipelines`, the count of the CPU's writes and the kept recordings of the command's +/// chunk alone following it, as they follow a write under the lock +/// ([`wrote`](IndirectCommandBuffer::wrote)): once the batch ends, the chunk noted for the kept +/// recordings' next look to forget ([`UnlockedWrites`], `KeptChunks::settle`), then the writes +/// counted, by one atomic add. A buffer the GPU starts writing meanwhile is the race a write under +/// the lock has with it too (`gpu_written` is set under `unforeseen`'s lock). The lock's two locked +/// instructions were most of a buffer setter's cost (`tests::icb_encode_bench`). Another write is +/// made under `pipelines` ([`edit_locked`](IndirectCommandBuffer::edit_locked)). +struct Batch<'i> { + icb: &'i IndirectCommandBuffer, + index: u32, + /// The storage's bytes ([`bytes`](IndirectCommandBuffer::bytes)). + storage: *mut u8, + /// The writes made without `pipelines`, not yet counted. + unlocked: u64, +} + +impl Batch<'_> { + /// Write `field`, which names no pipeline state, then note `then`: without `pipelines` where + /// it adds nothing to what the records may hold, else under it. + fn field(&mut self, field: RecordField, then: Then) -> Result<(), Error> { + let icb = self.icb; + match icb.adds_nothing(field) { + true => { + icb.write_field(self.storage, self.index, field)?; + self.unlocked += 1; + } + false => icb.edit_locked(self.storage, self.index, field, None)?, + } + icb.then(then); + Ok(()) + } + + /// Set `set` on the command, as its pipeline of ID `id`, under `pipelines`. + fn state(&mut self, id: NonZeroU64, set: StateSet<'_>) -> Result<(), Error> { + let field = RecordField::Pipeline(id); + self.icb + .edit_locked(self.storage, self.index, field, Some(set)) + } + + /// Set `set`'s buffer on the command, for `call`, at the buffer's address plus the offset's + /// low 32 bits, as Metal keeps it: on an M4 under macOS 27.0.1, a kernel or vertex buffer set + /// at 2^40 + 16 or 2^32 + 32, plainly or with an attribute stride, read and answered 16 or 32 + /// past the buffer's address, and so did a fragment buffer's getter; one set at 2^32 + 16 in a + /// buffer past 4 GiB read at 16, where a compute encoder's own buffer reads at the whole offset + /// (with and without API validation; the consumer measured the getters so on an M3 Max), and so + /// did object and mesh buffers' getters and reads. Refused (`Invalid`): a buffer of another + /// device. On a buffer that inherits its buffers the record keeps it and reads it back, and an + /// execution binds the encoder's buffers alone, none where the encoder's slot is unbound, as + /// plain Metal's, where its validation stops the call. + /// + /// A slot at or past the set's limit (the descriptor's count of the stage) is **taken** and + /// kept nowhere. Plain Metal takes it: on an M3 Max under macOS 27.0.1 a buffer of + /// `maxKernelBufferBindCount` 2 took `setKernelBuffer:offset:atIndex:` at index 2 and at + /// index 4 with no error and its `size` still read 3 commands, where Metal's own API + /// validation stops the call ("index(2) must be < 2."). Nothing of either side binds such a + /// slot to a function: an execution binds the slots below the count alone (`bindings` in + /// `indirect_execute.rs`, `buffers` in `render/executions.rs`), as Metal's count-sized + /// argument table does, and [`read_buffer`](IndirectCommandBuffer::read_buffer) answers none + /// at or past the + /// count. So the binding is dropped rather than written: the record's own slots are held to + /// the descriptor's counts by the engine (`indirect_commands::descriptor`'s `validate_record`, + /// which an execution's records and the records the GPU writes both go through), and what + /// Metal keeps such a slot in for its private getters is unmeasured. + fn buffer(&mut self, call: &'static str, set: BufferSet<'_>) -> Result<(), Error> { + let icb = self.icb; + let BufferSet { + stage, + limit, + slot, + buffer, + offset, + attribute_stride, + } = set; + if let Some(buffer) = buffer { + if !buffer.generation().same(icb.generation()) { + return Err(buffer.generation().foreign(call, icb.generation())); + } + } + // At or past the descriptor's count nothing binds the slot and no getter reads it, so + // the binding is kept nowhere; the call is still taken, as plain Metal takes it. + if slot >= limit { + return Ok(()); + } + // Metal keeps the offset's low 32 bits: its getters answer them and the GPU reads + // there, inside a buffer past 4 GiB too. A nil buffer keeps them too, as its address: + // Metal's getters answer them and its function sees them as the buffer's address. + let base = buffer.map_or(0, |buffer| buffer.gpu_address().0); + let binding = BufferBinding { + address: base + u64::from(offset as u32), + attribute_stride, + }; + // Past the slots the record holds no function binds a buffer (Metal's argument tables + // hold 31): plain Metal keeps it for its private getters, and so does the overflow. + if slot >= RECORD_BUFFERS { + let mut commands = lock(&icb.0.pipelines); + return icb.write_overflow(&mut commands, self.index, stage, slot, Some(binding)); + } + let field = RecordField::Buffer { + stage, + slot: slot as usize, + binding: Some(binding), + }; + self.field(field, Then::Nothing) + } +} + +impl Drop for Batch<'_> { + fn drop(&mut self) { + if self.unlocked != 0 { + self.icb.0.unlocked.wrote(self.index); + (self.icb.0.unlocked_writes).fetch_add(self.unlocked, Ordering::Release); + } + } +} + /// A buffer a command's setter sets: at `slot` of `stage`, below the descriptor's `limit`, at /// `offset` and `attribute_stride` (none: the pipeline's own); `None` unsets the slot. struct BufferSet<'a> { @@ -4099,18 +4257,10 @@ fn record_offset(index: u32) -> usize { COMMAND_BUFFER_HEADER_BYTES + index as usize * COMMAND_BYTES } -/// The error of a buffer slot's setter whose field the record refuses -/// (`CommandRecord::write_field`), written without the lock -/// ([`edit_unlocked`](IndirectCommandBuffer::edit_unlocked)). `edit` maps its own in place: a -/// call of this one from it made a draw about 1 ns slower (`tests::icb_encode_bench`). -fn field_error(error: RecordError) -> Error { - match error { - RecordError::Length { .. } - | RecordError::Action { .. } - | RecordError::Barrier { .. } - | RecordError::PipelineState => undecodable(), - _ => internal("an indirect command's record does not take a setter's field"), - } +/// `writes` in one word, as `IcbShared::added_writes` keeps them: the operations above the +/// primitive types. +fn packed(writes: CommandWrites) -> u64 { + u64::from(writes.operations) << 8 | u64::from(writes.primitive_types) } /// The runtime's error for a record that does not decode (a GPU write, the program's race). @@ -4131,26 +4281,272 @@ pub struct IndirectComputeCommand { index: u32, } -impl IndirectComputeCommand { - /// Write `field` into the record, with `set` as the state the CPU set where it names one - /// (`IndirectCommandBuffer::edit`). - fn edit(&self, field: RecordField, set: Option>) -> Result<(), Error> { - self.icb.edit(self.index, field, set) - } +/// Compute command `index` of `icb`, borrowed: what an [`IndirectComputeCommand`]'s calls and +/// [`IndirectCommandBuffer::set_compute_command`] reach, without a handle of the buffer of its +/// own. +struct ComputeAt<'i> { + icb: &'i IndirectCommandBuffer, + index: u32, +} - fn descriptor(&self) -> &IndirectCommandBufferDescriptor { - &self.icb.0.descriptor +impl IndirectComputeCommand { + fn at(&self) -> ComputeAt<'_> { + ComputeAt { + icb: &self.icb, + index: self.index, + } } /// `setComputePipelineState:`. Refused (`Invalid`): a buffer that inherits its pipeline /// state, a state made without `supportIndirectCommandBuffers` (both end Metal's process), a /// state of another device. pub fn set_compute_pipeline_state(&self, state: &ComputePipelineState) -> Result<(), Error> { + self.at().set_compute_pipeline_state(state) + } + + /// `setKernelBuffer:offset:atIndex:`: buffer `index` at `buffer`'s address plus `offset`'s + /// low 32 bits, as Metal keeps it, at the pipeline's own attribute stride; `None` binds no + /// buffer and keeps the offset's low 32 bits as the slot's address, as Metal's nil does: its + /// getter answers them, and a function the command runs sees them as the buffer's address (an + /// execution on the host binds no buffer there, its function seeing none, announced as + /// `indirect-nil-buffer-offset`). Taken at an index at or past the descriptor's + /// `max_kernel_buffer_bind_count` and kept nowhere, as plain Metal takes it (where its own + /// validation stops the call): no execution binds such a slot and no getter answers it. + /// Measured on an M3 Max under macOS 27.0.1: a buffer of `maxKernelBufferBindCount` 2 took + /// the call at index 2 and at index 4 with no error, and its `size` still read 3 commands. + /// Refused (`Invalid`): a buffer of another device. On a buffer that inherits its buffers it + /// is kept and read back, and executions bind the encoder's. + pub fn set_kernel_buffer( + &self, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.at().set_kernel_buffer(buffer, offset, index) + } + + /// `setKernelBuffer:offset:attributeStride:atIndex:`: as + /// [`set_kernel_buffer`](Self::set_kernel_buffer), at attribute stride `stride` (all ones is + /// the pipeline's own, as Metal reads it back). Where the descriptor does not support dynamic + /// attribute strides the stride is dropped and the buffer set as `set_kernel_buffer` sets it, + /// as plain Metal keeps it (its stride reads back all ones, the pipeline's own; an M3 Max and + /// an M4; Metal's API validation stops the call). `None` sets a nil buffer at the stride, which + /// the stride getter answers. + pub fn set_kernel_buffer_with_stride( + &self, + buffer: Option<&Buffer>, + offset: u64, + stride: u64, + index: u32, + ) -> Result<(), Error> { + self.at() + .set_kernel_buffer_with_stride(buffer, offset, stride, index) + } + + /// `concurrentDispatchThreadgroups:threadsPerThreadgroup:`: the command dispatches + /// `threadgroups` of `threads_per_threadgroup`, each axis's low 32 bits, as Metal keeps them. + /// Every compute buffer's records hold it: on a buffer of dispatches of threads it runs as + /// on its own, as plain Metal runs it, where Metal's validation stops the call + /// ("MTLIndirectCommandTypeConcurrentDispatch is not enabled"). + pub fn concurrent_dispatch_threadgroups( + &self, + threadgroups: Size, + threads_per_threadgroup: Size, + ) -> Result<(), Error> { + self.at() + .concurrent_dispatch_threadgroups(threadgroups, threads_per_threadgroup) + } + + /// `concurrentDispatchThreads:threadsPerThreadgroup:`: the command dispatches `threads` in + /// groups of `threads_per_threadgroup`, each axis's low 32 bits, as Metal keeps them. Every + /// compute buffer's records hold it: on a buffer of dispatches of threadgroups it runs as on + /// its own, as plain Metal runs it, where Metal's validation stops the call. + pub fn concurrent_dispatch_threads( + &self, + threads: Size, + threads_per_threadgroup: Size, + ) -> Result<(), Error> { + self.at() + .concurrent_dispatch_threads(threads, threads_per_threadgroup) + } + + /// `setBarrier`: the command waits for the commands before it. + pub fn set_barrier(&self) -> Result<(), Error> { + self.at().set_barrier() + } + + /// `clearBarrier`. + pub fn clear_barrier(&self) -> Result<(), Error> { + self.at().clear_barrier() + } + + /// `setThreadgroupMemoryLength:atIndex:`: as set, a multiple of 16 or not and past the + /// device's maximum alike, where Metal keeps it, at any index, the descriptor's + /// `max_kernel_threadgroup_memory_bind_count` or not: 38 lengths a command, of one array, so + /// an index of 38 or more sets a later command's length (`index - 38` of the next command's, + /// and so on), and one past the last command's is kept beside the buffer for the getter. No + /// function binds one past index 30: Metal keeps lengths 31 to 37 for its private getter + /// alone, as the runtime does. Past the last command's lengths Metal keeps each command's + /// words: 2 a command where the buffer does not inherit its pipeline state, then 8 a command; + /// a length set there is reset and copied with that command, as on Metal: a CPU reset clears + /// the 2, a GPU reset both, and a GPU copy carries both (and clears lengths 36 and 37 where + /// the commands hold the 2). Differs: Metal's pipeline setter writes the 2 words and clears + /// lengths 36 and 37, and its dispatch and imageblock setters write the 8 (the threadgroup + /// counts and size, the imageblock's width and height), so its getter answers those there; + /// the runtime keeps the lengths set there alone, and its pipeline setter leaves lengths 36 + /// and 37. A GPU copy writes a value of its own two places past the commands' words, which + /// the runtime does not. Far past the last command's lengths (about 4000 places on an M3 + /// Max), Metal's setter ends the process; the runtime keeps the length. + pub fn set_threadgroup_memory_length(&self, length: u32, index: u32) -> Result<(), Error> { + self.at().set_threadgroup_memory_length(length, index) + } + + /// `setStageInRegion:`: each origin and size value's low 32 bits, as Metal keeps them. + pub fn set_stage_in_region(&self, region: Region) -> Result<(), Error> { + self.at().set_stage_in_region(region) + } + + /// `setImageblockWidth:height:`: as set. A side past 65535, which Metal keeps, is refused + /// (`NotImplemented(IndirectImageblockSize)`): the record holds 16 bits a side, as a + /// kernel's `set_imageblock_size` writes it. + pub fn set_imageblock_size(&self, width: u64, height: u64) -> Result<(), Error> { + self.at().set_imageblock_size(width, height) + } + + /// `reset`: the command as a fresh buffer's, but for the pipeline state it executes with, + /// which Metal's reset keeps while its getter reports none (measured on the M3 Max and the M4): + /// a dispatch encoded after the reset runs it. [`IndirectCommandBuffer:: commands`] reports + /// none, as Metal's getter does; a pipeline setter replaces it. The imageblock size is kept + /// too, as plain Metal's reads back after `reset` (the size set before it, measured by the + /// integration consumer on an M3 without API validation). + pub fn reset(&self) -> Result<(), Error> { + self.at().reset() + } + /// `getCommandType`, Apple's private getter: the command's + /// [`command_type`](EncodedComputeCommand::command_type) as + /// [`IndirectCommandBuffer::commands`] reads it now, its record's action alone read. Each of + /// the command's getters reads its field of the record as it is at the call (a runtime + /// extension standing for Metal's private getters, as `commands` is), so a field read after + /// the GPU may have written the records may not be what an execution committed earlier + /// runs ([`IndirectCommandBuffer::gpu_written`]). + pub fn command_type(&self) -> Result { + self.at().command_type() + } + + /// `hasBarrier`, Apple's private getter: the command's + /// [`barrier`](EncodedComputeCommand::barrier), read alone. + pub fn has_barrier(&self) -> Result { + self.at().has_barrier() + } + + /// `dispatchThreadgroupsArguments` and `dispatchThreadsArguments`, Apple's private getters: + /// the command's [`dispatch`](EncodedComputeCommand::dispatch), read alone. + pub fn dispatch(&self) -> Result, Error> { + self.at().dispatch() + } + + /// The kernel buffer set at `index`: the command's + /// [`kernel_buffers`](EncodedComputeCommand::kernel_buffers)`[index]`, read alone, none at or + /// past the descriptor's `max_kernel_buffer_bind_count`. Metal's private getters of the slot + /// (`getKernelBufferAddressAtIndex:`, `getKernelAttributeStrideAtIndex:`) answer it as + /// [`EncodedComputeCommand::kernel_buffer_address`] and + /// [`EncodedComputeCommand::kernel_attribute_stride`] do. + pub fn kernel_buffer(&self, index: u64) -> Result, Error> { + self.at().kernel_buffer(index) + } + + /// `getThreadgroupMemoryLengthAtIndex:`, Apple's private getter: the length at `index`'s low + /// 32 bits, as Metal's takes them, where Metal keeps it, as + /// [`set_threadgroup_memory_length`](Self::set_threadgroup_memory_length) sets it (below 31, + /// the command's + /// [`threadgroup_memory_lengths`](EncodedComputeCommand::threadgroup_memory_lengths)`[index]`; + /// at 38 or more, a later command's; past the last command's, the commands' words there), + /// read alone: 0 where none was set, at any index, the descriptor's + /// `max_kernel_threadgroup_memory_bind_count` or not; always some. Differs: past the last + /// command's lengths Metal's getter answers what its pipeline, dispatch and imageblock + /// setters wrote there, the runtime's only lengths set there (the setter's docs); far past + /// them (an M3 Max: 100000 places past command 0's of 3), Metal's getter ends the process, + /// and the runtime's answers. + pub fn threadgroup_memory_length(&self, index: u64) -> Result, Error> { + self.at().threadgroup_memory_length(index) + } + + /// `getStageInRegion`, Apple's private getter: the command's + /// [`stage_in_region`](EncodedComputeCommand::stage_in_region), read alone. + pub fn stage_in_region(&self) -> Result { + self.at().stage_in_region() + } + + /// The imageblock size Apple's private getter answers: the command's + /// [`imageblock_width`](EncodedComputeCommand::imageblock_width) and + /// [`imageblock_height`](EncodedComputeCommand::imageblock_height), read alone. + pub fn imageblock_size(&self) -> Result<(u64, u64), Error> { + self.at().imageblock_size() + } + + /// The command's [`pipeline`](EncodedComputeCommand::pipeline), read alone: the state set on + /// it, none when unset, reset, or when no state of the ID it names lives. + pub fn pipeline_state(&self) -> Result, Error> { + self.at().pipeline_state() + } + + /// The command's [`pipeline_id`](EncodedComputeCommand::pipeline_id), read alone: the ID of + /// the state its record names, none where it names none. + pub fn pipeline_id(&self) -> Result, Error> { + self.at().pipeline_id() + } + + /// `getPipelineStateUniqueIdentifier`, Apple's private getter: the command's + /// [`pipeline_unique_identifier`](EncodedComputeCommand::pipeline_unique_identifier), read + /// alone, whose kind the device announces as `commands` announces it. + pub fn pipeline_unique_identifier(&self) -> Result, Error> { + self.at().pipeline_unique_identifier() + } +} + +impl ComputeAt<'_> { + /// Write `field`, which names no pipeline state, into the record + /// (`IndirectCommandBuffer::edit`). + fn edit(&self, field: RecordField) -> Result<(), Error> { + self.icb.edit(self.index, field) + } + + fn descriptor(&self) -> &IndirectCommandBufferDescriptor { + &self.icb.0.descriptor + } + + fn set_compute_pipeline_state(&self, state: &ComputePipelineState) -> Result<(), Error> { + self.pipeline_in(&mut self.icb.batch(self.index), state) + } + + /// [`set_compute_pipeline_state`](Self::set_compute_pipeline_state) of `state` in `batch`. + /// The state the command names already, as the CPU set it since the buffer's last carry + /// (`SetKeys`), passed every check then and is the command's state still: its ID is written + /// again without the lock or a hold of the state, as any field that adds nothing to what the + /// records may hold is (`Batch::field`). + fn pipeline_in( + &self, + batch: &mut Batch<'_>, + state: &ComputePipelineState, + ) -> Result<(), Error> { const CALL: &str = "set_compute_pipeline_state"; - self.icb.check_command_types(CALL)?; + if let (Some(id), Some(serial)) = ( + NonZeroU64::new(state.gpu_resource_id().0), + state.id_serial(), + ) { + let key = StateKey { + compute: true, + id: id.get(), + serial, + }; + if self.icb.0.set_keys.names(self.index, key) { + return batch.field(RecordField::Pipeline(id), Then::Nothing); + } + } // The state's program, held while the command takes it (`pipeline/carry.rs`): one hold - // for every read of the state below. + // for every read of the state. let held = state.hold(); + self.icb.check_command_types(CALL)?; let device = state.bound_device()?; if !device.same(self.icb.generation()) { return Err(device.foreign(CALL, self.icb.generation())); @@ -4188,43 +4584,47 @@ impl IndirectComputeCommand { serial, }, named, - make: &|| { - CommandState::Compute(state.downgrade(), state.program_held(&held), stamp, named) - }, + stamp, + of: StateOf::Compute(state, &held), }; - self.edit(RecordField::Pipeline(id), Some(set)) + batch.state(id, set) } - /// `setKernelBuffer:offset:atIndex:`: buffer `index` at `buffer`'s address plus `offset`'s - /// low 32 bits, as Metal keeps it, at the pipeline's own attribute stride; `None` binds no - /// buffer and keeps the offset's low 32 bits as the slot's address, as Metal's nil does: its - /// getter answers them, and a function the command runs sees them as the buffer's address (an - /// execution on the host binds no buffer there, its function seeing none, announced as - /// `indirect-nil-buffer-offset`). Taken at an index at or past the descriptor's - /// `max_kernel_buffer_bind_count` and kept nowhere, as plain Metal takes it (where its own - /// validation stops the call): no execution binds such a slot and no getter answers it. - /// Measured on an M3 Max under macOS 27.0.1: a buffer of `maxKernelBufferBindCount` 2 took - /// the call at index 2 and at index 4 with no error, and its `size` still read 3 commands. - /// Refused (`Invalid`): a buffer of another device. On a buffer that inherits its buffers it - /// is kept and read back, and executions bind the encoder's. - pub fn set_kernel_buffer( + fn set_kernel_buffer( &self, buffer: Option<&Buffer>, offset: u64, index: u32, ) -> Result<(), Error> { - self.set_buffer("set_kernel_buffer", buffer, offset, None, index) + self.kernel_buffer_in(&mut self.icb.batch(self.index), buffer, offset, index) } - /// `setKernelBuffer:offset:attributeStride:atIndex:`: as - /// [`set_kernel_buffer`](Self::set_kernel_buffer), at attribute stride `stride` (all ones is - /// the pipeline's own, as Metal reads it back). Where the descriptor does not support dynamic - /// attribute strides the stride is dropped and the buffer set as `set_kernel_buffer` sets it, - /// as plain Metal keeps it (its stride reads back all ones, the pipeline's own; an M3 Max and - /// an M4; Metal's API validation stops the call). `None` sets a nil buffer at the stride, which - /// the stride getter answers. - pub fn set_kernel_buffer_with_stride( + /// [`set_kernel_buffer`](Self::set_kernel_buffer) in `batch`. + fn kernel_buffer_in( &self, + batch: &mut Batch<'_>, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.buffer_in(batch, "set_kernel_buffer", buffer, offset, None, index) + } + + fn set_kernel_buffer_with_stride( + &self, + buffer: Option<&Buffer>, + offset: u64, + stride: u64, + index: u32, + ) -> Result<(), Error> { + let batch = &mut self.icb.batch(self.index); + self.kernel_buffer_with_stride_in(batch, buffer, offset, stride, index) + } + + /// [`set_kernel_buffer_with_stride`](Self::set_kernel_buffer_with_stride) in `batch`. + fn kernel_buffer_with_stride_in( + &self, + batch: &mut Batch<'_>, buffer: Option<&Buffer>, offset: u64, stride: u64, @@ -4233,11 +4633,14 @@ impl IndirectComputeCommand { const CALL: &str = "set_kernel_buffer_with_stride"; let dynamic = self.descriptor().support_dynamic_attribute_stride; let stride = (dynamic && stride != u64::MAX).then_some(stride); - self.set_buffer(CALL, buffer, offset, stride, index) + self.buffer_in(batch, CALL, buffer, offset, stride, index) } - fn set_buffer( + /// A kernel buffer setter for `call` in `batch` ([`Batch::buffer`], against the + /// descriptor's count). + fn buffer_in( &self, + batch: &mut Batch<'_>, call: &'static str, buffer: Option<&Buffer>, offset: u64, @@ -4252,20 +4655,27 @@ impl IndirectComputeCommand { offset, attribute_stride, }; - self.icb.set_buffer(self.index, call, set) + batch.buffer(call, set) } - /// `concurrentDispatchThreadgroups:threadsPerThreadgroup:`: the command dispatches - /// `threadgroups` of `threads_per_threadgroup`, each axis's low 32 bits, as Metal keeps them. - /// Every compute buffer's records hold it: on a buffer of dispatches of threads it runs as - /// on its own, as plain Metal runs it, where Metal's validation stops the call - /// ("MTLIndirectCommandTypeConcurrentDispatch is not enabled"). - pub fn concurrent_dispatch_threadgroups( + fn concurrent_dispatch_threadgroups( &self, threadgroups: Size, threads_per_threadgroup: Size, ) -> Result<(), Error> { - self.set_dispatch( + let batch = &mut self.icb.batch(self.index); + self.dispatch_threadgroups_in(batch, threadgroups, threads_per_threadgroup) + } + + /// [`concurrent_dispatch_threadgroups`](Self::concurrent_dispatch_threadgroups) in `batch`. + fn dispatch_threadgroups_in( + &self, + batch: &mut Batch<'_>, + threadgroups: Size, + threads_per_threadgroup: Size, + ) -> Result<(), Error> { + self.dispatch_in( + batch, "concurrent_dispatch_threadgroups", Action::DispatchThreadgroups { grid: low_bits(threadgroups), @@ -4274,16 +4684,24 @@ impl IndirectComputeCommand { ) } - /// `concurrentDispatchThreads:threadsPerThreadgroup:`: the command dispatches `threads` in - /// groups of `threads_per_threadgroup`, each axis's low 32 bits, as Metal keeps them. Every - /// compute buffer's records hold it: on a buffer of dispatches of threadgroups it runs as on - /// its own, as plain Metal runs it, where Metal's validation stops the call. - pub fn concurrent_dispatch_threads( + fn concurrent_dispatch_threads( &self, threads: Size, threads_per_threadgroup: Size, ) -> Result<(), Error> { - self.set_dispatch( + let batch = &mut self.icb.batch(self.index); + self.dispatch_threads_in(batch, threads, threads_per_threadgroup) + } + + /// [`concurrent_dispatch_threads`](Self::concurrent_dispatch_threads) in `batch`. + fn dispatch_threads_in( + &self, + batch: &mut Batch<'_>, + threads: Size, + threads_per_threadgroup: Size, + ) -> Result<(), Error> { + self.dispatch_in( + batch, "concurrent_dispatch_threads", Action::DispatchThreads { grid: low_bits(threads), @@ -4293,46 +4711,37 @@ impl IndirectComputeCommand { } /// Set the record's dispatch `action`, which every compute buffer's records hold - /// (`CommandDescriptor::holds`). Refused for `call` on a buffer of no command types - /// (`NoCommandTypes`), where plain Metal's process ends. - fn set_dispatch(&self, call: &'static str, action: Action) -> Result<(), Error> { + /// (`CommandDescriptor::holds`), in `batch`. Refused for `call` on a buffer of no command + /// types (`NoCommandTypes`), where plain Metal's process ends. + fn dispatch_in( + &self, + batch: &mut Batch<'_>, + call: &'static str, + action: Action, + ) -> Result<(), Error> { self.icb.check_command_types(call)?; - self.edit(RecordField::Action(action), None) + batch.field(RecordField::Action(action), Then::Nothing) } - /// `setBarrier`: the command waits for the commands before it. - pub fn set_barrier(&self) -> Result<(), Error> { - self.edit(RecordField::Barrier(true), None) + fn set_barrier(&self) -> Result<(), Error> { + self.barrier_in(&mut self.icb.batch(self.index), true) } - /// `clearBarrier`. - pub fn clear_barrier(&self) -> Result<(), Error> { - self.edit(RecordField::Barrier(false), None) + fn clear_barrier(&self) -> Result<(), Error> { + self.barrier_in(&mut self.icb.batch(self.index), false) } - /// `setThreadgroupMemoryLength:atIndex:`: as set, a multiple of 16 or not and past the - /// device's maximum alike, where Metal keeps it, at any index, the descriptor's - /// `max_kernel_threadgroup_memory_bind_count` or not: 38 lengths a command, of one array, so - /// an index of 38 or more sets a later command's length (`index - 38` of the next command's, - /// and so on), and one past the last command's is kept beside the buffer for the getter. No - /// function binds one past index 30: Metal keeps lengths 31 to 37 for its private getter - /// alone, as the runtime does. Past the last command's lengths Metal keeps each command's - /// words: 2 a command where the buffer does not inherit its pipeline state, then 8 a command; - /// a length set there is reset and copied with that command, as on Metal: a CPU reset clears - /// the 2, a GPU reset both, and a GPU copy carries both (and clears lengths 36 and 37 where - /// the commands hold the 2). Differs: Metal's pipeline setter writes the 2 words and clears - /// lengths 36 and 37, and its dispatch and imageblock setters write the 8 (the threadgroup - /// counts and size, the imageblock's width and height), so its getter answers those there; - /// the runtime keeps the lengths set there alone, and its pipeline setter leaves lengths 36 - /// and 37. A GPU copy writes a value of its own two places past the commands' words, which - /// the runtime does not. Far past the last command's lengths (about 4000 places on an M3 - /// Max), Metal's setter ends the process; the runtime keeps the length. - pub fn set_threadgroup_memory_length(&self, length: u32, index: u32) -> Result<(), Error> { + /// [`set_barrier`](Self::set_barrier) (`set`) or [`clear_barrier`](Self::clear_barrier) in + /// `batch`. + fn barrier_in(&self, batch: &mut Batch<'_>, set: bool) -> Result<(), Error> { + batch.field(RecordField::Barrier(set), Then::Nothing) + } + + fn set_threadgroup_memory_length(&self, length: u32, index: u32) -> Result<(), Error> { self.icb.set_length(self.index, index, length) } - /// `setStageInRegion:`: each origin and size value's low 32 bits, as Metal keeps them. - pub fn set_stage_in_region(&self, region: Region) -> Result<(), Error> { + fn set_stage_in_region(&self, region: Region) -> Result<(), Error> { let Region { origin, size } = region; let values = [ origin.x, @@ -4343,29 +4752,20 @@ impl IndirectComputeCommand { size.depth, ]; let region = values.map(|value| value as u32); - self.edit(RecordField::StageInRegion(region), None) + self.edit(RecordField::StageInRegion(region)) } - /// `setImageblockWidth:height:`: as set. A side past 65535, which Metal keeps, is refused - /// (`NotImplemented(IndirectImageblockSize)`): the record holds 16 bits a side, as a - /// kernel's `set_imageblock_size` writes it. - pub fn set_imageblock_size(&self, width: u64, height: u64) -> Result<(), Error> { + fn set_imageblock_size(&self, width: u64, height: u64) -> Result<(), Error> { let (Ok(w), Ok(h)) = (u16::try_from(width), u16::try_from(height)) else { return Err(Error::NotImplemented(RuntimeGap::IndirectImageblockSize { width, height, })); }; - self.edit(RecordField::ImageblockSize([w, h]), None) + self.edit(RecordField::ImageblockSize([w, h])) } - /// `reset`: the command as a fresh buffer's, but for the pipeline state it executes with, - /// which Metal's reset keeps while its getter reports none (measured on the M3 Max and the M4): - /// a dispatch encoded after the reset runs it. [`IndirectCommandBuffer:: commands`] reports - /// none, as Metal's getter does; a pipeline setter replaces it. The imageblock size is kept - /// too, as plain Metal's reads back after `reset` (the size set before it, measured by the - /// integration consumer on an M3 without API validation). - pub fn reset(&self) -> Result<(), Error> { + fn reset(&self) -> Result<(), Error> { self.icb.check_command_types("reset")?; self.icb.reset_command(self.index, |record| { let retained = record.bindings.pipeline.or(record.retained_pipeline); @@ -4377,14 +4777,7 @@ impl IndirectComputeCommand { true }) } - /// `getCommandType`, Apple's private getter: the command's - /// [`command_type`](EncodedComputeCommand::command_type) as - /// [`IndirectCommandBuffer::commands`] reads it now, its record's action alone read. Each of - /// the command's getters reads its field of the record as it is at the call (a runtime - /// extension standing for Metal's private getters, as `commands` is), so a field read after - /// the GPU may have written the records may not be what an execution committed earlier - /// runs ([`IndirectCommandBuffer::gpu_written`]). - pub fn command_type(&self) -> Result { + fn command_type(&self) -> Result { self.check_getter("command_type")?; self.icb .read_record(self.index, |view| command_type(&view.action())) @@ -4397,54 +4790,30 @@ impl IndirectComputeCommand { self.icb.check_command_types(call) } - /// `hasBarrier`, Apple's private getter: the command's - /// [`barrier`](EncodedComputeCommand::barrier), read alone. - pub fn has_barrier(&self) -> Result { + fn has_barrier(&self) -> Result { self.check_getter("has_barrier")?; self.icb.read_record(self.index, |view| view.barrier()) } - /// `dispatchThreadgroupsArguments` and `dispatchThreadsArguments`, Apple's private getters: - /// the command's [`dispatch`](EncodedComputeCommand::dispatch), read alone. - pub fn dispatch(&self) -> Result, Error> { + fn dispatch(&self) -> Result, Error> { self.check_getter("dispatch")?; self.icb .read_record(self.index, |view| dispatch_of(&view.action())) } - /// The kernel buffer set at `index`: the command's - /// [`kernel_buffers`](EncodedComputeCommand::kernel_buffers)`[index]`, read alone, none at or - /// past the descriptor's `max_kernel_buffer_bind_count`. Metal's private getters of the slot - /// (`getKernelBufferAddressAtIndex:`, `getKernelAttributeStrideAtIndex:`) answer it as - /// [`EncodedComputeCommand::kernel_buffer_address`] and - /// [`EncodedComputeCommand::kernel_attribute_stride`] do. - pub fn kernel_buffer(&self, index: u64) -> Result, Error> { + fn kernel_buffer(&self, index: u64) -> Result, Error> { self.check_getter("kernel_buffer")?; let count = self.descriptor().max_kernel_buffer_bind_count; self.icb .read_buffer(self.index, BufferStage::Kernel, count, index) } - /// `getThreadgroupMemoryLengthAtIndex:`, Apple's private getter: the length at `index`'s low - /// 32 bits, as Metal's takes them, where Metal keeps it, as - /// [`set_threadgroup_memory_length`](Self::set_threadgroup_memory_length) sets it (below 31, - /// the command's - /// [`threadgroup_memory_lengths`](EncodedComputeCommand::threadgroup_memory_lengths)`[index]`; - /// at 38 or more, a later command's; past the last command's, the commands' words there), - /// read alone: 0 where none was set, at any index, the descriptor's - /// `max_kernel_threadgroup_memory_bind_count` or not; always some. Differs: past the last - /// command's lengths Metal's getter answers what its pipeline, dispatch and imageblock - /// setters wrote there, the runtime's only lengths set there (the setter's docs); far past - /// them (an M3 Max: 100000 places past command 0's of 3), Metal's getter ends the process, - /// and the runtime's answers. - pub fn threadgroup_memory_length(&self, index: u64) -> Result, Error> { + fn threadgroup_memory_length(&self, index: u64) -> Result, Error> { self.check_getter("threadgroup_memory_length")?; self.icb.length(self.index, index as u32).map(Some) } - /// `getStageInRegion`, Apple's private getter: the command's - /// [`stage_in_region`](EncodedComputeCommand::stage_in_region), read alone. - pub fn stage_in_region(&self) -> Result { + fn stage_in_region(&self) -> Result { self.check_getter("stage_in_region")?; let [x, y, z, width, height, depth] = self .icb @@ -4456,10 +4825,7 @@ impl IndirectComputeCommand { }) } - /// The imageblock size Apple's private getter answers: the command's - /// [`imageblock_width`](EncodedComputeCommand::imageblock_width) and - /// [`imageblock_height`](EncodedComputeCommand::imageblock_height), read alone. - pub fn imageblock_size(&self) -> Result<(u64, u64), Error> { + fn imageblock_size(&self) -> Result<(u64, u64), Error> { self.check_getter("imageblock_size")?; let [width, height] = self .icb @@ -4467,26 +4833,19 @@ impl IndirectComputeCommand { Ok((width.into(), height.into())) } - /// The command's [`pipeline`](EncodedComputeCommand::pipeline), read alone: the state set on - /// it, none when unset, reset, or when no state of the ID it names lives. - pub fn pipeline_state(&self) -> Result, Error> { + fn pipeline_state(&self) -> Result, Error> { self.check_getter("pipeline_state")?; Ok(self.icb.compute_pipeline_now(self.index)?.0) } - /// The command's [`pipeline_id`](EncodedComputeCommand::pipeline_id), read alone: the ID of - /// the state its record names, none where it names none. - pub fn pipeline_id(&self) -> Result, Error> { + fn pipeline_id(&self) -> Result, Error> { self.check_getter("pipeline_id")?; self.icb.read_record(self.index, |view| { view.pipeline().map(|id| ResourceId(id.get())) }) } - /// `getPipelineStateUniqueIdentifier`, Apple's private getter: the command's - /// [`pipeline_unique_identifier`](EncodedComputeCommand::pipeline_unique_identifier), read - /// alone, whose kind the device announces as `commands` announces it. - pub fn pipeline_unique_identifier(&self) -> Result, Error> { + fn pipeline_unique_identifier(&self) -> Result, Error> { self.check_getter("pipeline_unique_identifier")?; let (_, identifier) = self.icb.compute_pipeline_now(self.index)?; self.icb.announce_identifier(identifier); @@ -4743,7 +5102,494 @@ pub struct IndirectRenderCommand { index: u32, } +/// Render command `index` of `icb`, borrowed: what an [`IndirectRenderCommand`]'s calls and +/// [`IndirectCommandBuffer::set_render_command`] reach, without a handle of the buffer of its own. +struct RenderAt<'i> { + icb: &'i IndirectCommandBuffer, + index: u32, +} + impl IndirectRenderCommand { + fn at(&self) -> RenderAt<'_> { + RenderAt { + icb: &self.icb, + index: self.index, + } + } + + /// `setRenderPipelineState:`. Refused (`Invalid`): a buffer that inherits its pipeline + /// state, a state made without `supportIndirectCommandBuffers` (a tile pipeline's among + /// them), a state of another device, and a state whose kind of draw the buffer's command + /// types lack (`PipelineKind`: a vertex function's on a buffer of mesh draws alone, a mesh + /// one's on a buffer of no mesh draw), where Metal's process ends. + pub fn set_render_pipeline_state(&self, state: &RenderPipelineState) -> Result<(), Error> { + self.at().set_render_pipeline_state(state) + } + + /// `setVertexBuffer:offset:atIndex:`: vertex buffer `index` at `buffer`'s address plus + /// `offset`'s low 32 bits, as Metal keeps it, at the pipeline's own attribute stride; `None` + /// sets a nil buffer at the offset's low 32 bits, as + /// [`IndirectComputeCommand::set_kernel_buffer`] does, an index at or past the descriptor's + /// `max_vertex_buffer_bind_count` taken and kept nowhere as that call's is. Refused + /// (`Invalid`): a buffer of another device. On a buffer that inherits its buffers it is kept + /// and read back, and executions bind the encoder's. + pub fn set_vertex_buffer( + &self, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.at().set_vertex_buffer(buffer, offset, index) + } + + /// `setVertexBuffer:offset:attributeStride:atIndex:`: as + /// [`set_vertex_buffer`](Self::set_vertex_buffer), at attribute stride `stride` (all ones is + /// the pipeline's own). On a buffer that does not support dynamic attribute strides the + /// stride is dropped and the buffer set, as plain Metal sets it where its validation stops + /// the call: the stride reads back all ones and the draw reads the buffer at its offset, as a + /// kernel buffer's is. `None` sets a nil buffer at the stride, as + /// [`IndirectComputeCommand::set_kernel_buffer_with_stride`] does. + pub fn set_vertex_buffer_with_stride( + &self, + buffer: Option<&Buffer>, + offset: u64, + stride: u64, + index: u32, + ) -> Result<(), Error> { + self.at() + .set_vertex_buffer_with_stride(buffer, offset, stride, index) + } + + /// `setFragmentBuffer:offset:atIndex:`: as [`set_vertex_buffer`](Self::set_vertex_buffer), + /// against the descriptor's `max_fragment_buffer_bind_count`. + pub fn set_fragment_buffer( + &self, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.at().set_fragment_buffer(buffer, offset, index) + } + + /// `drawPrimitives:vertexStart:vertexCount:instanceCount:baseInstance:`: the command draws + /// `vertex_count` vertices of `primitive_type` from `vertex_start`, `instance_count` times + /// from `base_instance`, each value's low 32 bits, which the record holds (Metal takes larger + /// values; what it keeps of them is not measured: its getter answers an object). A type is a + /// public one or one of Metal's private ones ([`DrawPrimitiveType`]), which an execution draws + /// as an encoder's draw of it draws, type 9 failing the command buffer so where an execution + /// of a range given at its call finds it there (the M3 Max, macOS 27.0.1: types 5 to 9 drew + /// and failed as the encoder's draws of them, direct and indexed). A draw of no vertices or + /// no instances runs nothing, as plain Metal runs it where its validation stops the call + /// ("instanceCount(0) must be non-zero"). The records of every render buffer but one + /// of mesh draws alone hold it: on a buffer whose command types lack `DRAW` it runs as on its + /// own, as plain Metal runs it, where Metal's validation stops the call ("drawPrimitives is not + /// enabled on this indirect command buffer"). Refused (`Invalid`): a buffer of mesh draws alone + /// (`CommandType`), where Metal's process ends, with and without its validation (an M4: buffers + /// of either mesh kind and of both, the encoder's pipeline inherited). + pub fn draw_primitives( + &self, + primitive_type: impl Into, + vertex_start: u64, + vertex_count: u64, + instance_count: u64, + base_instance: u64, + ) -> Result<(), Error> { + self.at().draw_primitives( + primitive_type, + vertex_start, + vertex_count, + instance_count, + base_instance, + ) + } + + /// `drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount: + /// baseVertex:baseInstance:`: the command draws `index_count` indices of `index_type` from + /// `index_buffer_offset` of `index_buffer`, `instance_count` times from `base_instance`, each + /// index plus `base_vertex`, of a public primitive type or a private one, as + /// [`draw_primitives`](Self::draw_primitives) says (type 9 faults the command buffer, of any + /// index or instance count, as the encoder's draw does). The record holds the buffer as one + /// address (its `gpu_address` plus the offset's low 32 bits, as Metal keeps it; its validation + /// checks the whole offset, as the refusal below does), which an execution resolves against + /// the encoder's resident resources (`render/executions.rs`), and each value's low 32 bits, + /// `base_vertex` as a signed one: Metal keeps a base vertex's so (a count past 32 bits always + /// runs past the buffer, which is refused). It runs as on its own on a buffer of patch draws, + /// whose records hold it, as plain Metal runs it, where Metal's validation stops a buffer + /// whose command types lack `DRAW_INDEXED`. A draw of no indices or no instances runs nothing + /// (but type 9's), and the indices are read from the offset rounded down to a multiple of the + /// index size, as plain Metal runs them where its validation stops the call + /// (`idx_count_zero`, `idx_instances_zero`; 32-bit indices at offset 2 and 16-bit ones at 1 + /// drew those at 0). + /// + /// On a buffer of draws of primitives alone, whose records cannot hold it, the draw is + /// **taken** and its record kept, and an execution of the command **runs nothing**: it draws + /// no vertex, reads no index, and reports no fault, as plain Metal's execution of it does + /// (`IndirectCommandBuffer::takes_as_nothing`, which carries the reading). The read-back is + /// the indexed draw as set, the record's own, as every read-back is (what Metal's private + /// getters answer for such a record is unmeasured). + /// + /// Refused (`Invalid`): a buffer of mesh draws alone, whose records cannot hold it + /// (`CommandType`; Metal's process ends), and, as Metal's validation stops it, indices past + /// the buffer's length from that offset (`IndexBufferRange`, of the whole count; plain Metal + /// draws from whatever memory follows), and an index buffer of another device. + pub fn draw_indexed_primitives( + &self, + primitive_type: impl Into, + index_count: u64, + index_type: IndexType, + index_buffer: &Buffer, + index_buffer_offset: u64, + instance_count: u64, + base_vertex: i64, + base_instance: u64, + ) -> Result<(), Error> { + self.at().draw_indexed_primitives( + primitive_type, + index_count, + index_type, + index_buffer, + index_buffer_offset, + instance_count, + base_vertex, + base_instance, + ) + } + + /// `drawPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset:instanceCount: + /// baseInstance:tessellationFactorBuffer:tessellationFactorBufferOffset: + /// tessellationFactorBufferInstanceStride:`: the command draws `patch_count` patches from + /// `patch_start` (each patch's index from `patch_index_buffer` where it has one), + /// `instance_count` times from `base_instance`, their factors read from + /// `tessellation_factor_buffer` at the offset and the instance stride. The record holds each + /// buffer as one address, at its offset's low 32 bits as Metal keeps it (the patch index and + /// factor buffers), which an execution resolves against the encoder's resident resources, and + /// each value's low 32 bits (Metal keeps a patch count's so). The control points are kept, + /// though Metal's execution draws with its pipeline's (measured: 3 and 0 drew as a quad + /// pipeline's 4), as the execution does, more than 32 included (a Metal probe `icb_cp33` drew + /// as 3 do, where Metal's validation stops the call); a draw of no patches or no instances runs + /// nothing, and the patch indices are read from the offset rounded down to a multiple of 4, as + /// plain Metal runs them where its validation stops the call (`patch_instances_zero`; indices 9 + /// and 3 at offset 2 drew patches 9 and 3). A factor offset and instance stride that are not + /// multiples of 4, which Metal's validation stops for the encoder's, are read as plain Metal + /// reads them, as the encoder's are: the factors from the offset rounded down to a multiple of + /// 2, each instance's at the stride rounded down to a multiple of 4 (Metal probes + /// `icb_factor_offset2`, 4 and 5: strides 2 and 14 drew what 0 and 12 draw, 6 and 10 what 4 and + /// 8 draw). It runs as on its own on a buffer of indexed patch draws, whose records hold it, as + /// plain Metal runs it, where Metal's validation stops a buffer whose command types lack + /// `DRAW_PATCHES`. Refused (`Invalid`): a buffer of neither patch kind, whose records cannot + /// hold it (`CommandType`; plain Metal ends its process), and, as Metal's validation stops + /// each, buffers of another device. Refused as the encoder's draw refuses them, which Metal's + /// validation of a command was not measured to: patch indices past the patch index buffer + /// from its offset's low 32 bits, which the command keeps (`PatchIndexBufferRange`; plain + /// Metal read 0 past it, the buffer's page padding, which stays undefined). An odd factor + /// offset is read from the byte below, and an odd instance stride + /// rounded down to a multiple of 4, as plain Metal reads a command's and the encoder's alike, + /// with and without its validation. + pub fn draw_patches( + &self, + number_of_patch_control_points: u64, + patch_start: u64, + patch_count: u64, + patch_index_buffer: Option<&Buffer>, + patch_index_buffer_offset: u64, + instance_count: u64, + base_instance: u64, + tessellation_factor_buffer: &Buffer, + tessellation_factor_buffer_offset: u64, + tessellation_factor_buffer_instance_stride: u64, + ) -> Result<(), Error> { + self.at().draw_patches( + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + ) + } + + /// `drawIndexedPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset: + /// controlPointIndexBuffer:controlPointIndexBufferOffset:instanceCount:baseInstance: + /// tessellationFactorBuffer:tessellationFactorBufferOffset: + /// tessellationFactorBufferInstanceStride:`: the command draws as + /// [`draw_patches`](Self::draw_patches) does, but control point `k` of the patch whose index is + /// `p` is the index (of its pipeline's `tessellationControlPointIndexType`) at position + /// `p · N + k` of `control_point_index_buffer` from its offset, `N` its pipeline's control + /// points, whatever the command names, as the encoder's `draw_indexed_patches` reads them + /// (a probe, an M4: the command's 0, 1 and 4 drew as the pipeline's 3). The record holds each + /// buffer as one address, at its offset's low 32 bits (the control-point index buffer's too), + /// and each value's low 32 bits (probe 9b: Metal keeps a patch start's so; its getters answer + /// each count's). `None` for the control-point index buffer, which Metal takes, makes the + /// command a plain patch draw of the same values, as Metal records it: its `getCommandType` + /// answers `DrawPatches` on an M4 (probe 9b, a buffer of indexed patch draws alone) and on the + /// consumer's M3 Max, and it draws as a plain patch draw on that pipeline draws, every + /// control-point index 0 on a pipeline with an index type (probe 9b: the same marks as its + /// plain patch draw on the same pipeline). It runs as on its own on a buffer of patch draws, + /// whose records hold it, as plain Metal runs it, where Metal's validation stops a buffer whose + /// command types lack `DRAW_INDEXED_PATCHES` ("drawIndexedPatches is not enabled on this + /// indirect command buffer"). Refused (`Invalid`) as `draw_patches` is, for a buffer of neither + /// patch kind (`CommandType`), and for a control-point index buffer of another device. Metal's + /// validation does not stop a factor offset or instance stride that is not a multiple of 4 here + /// (probe 9), which the runtime reads as `draw_patches` does; nor does it check the + /// control-point index buffer, whose index size the pipeline set at execution states (an + /// execution runs what Metal's does, `render/executions.rs`). + pub fn draw_indexed_patches( + &self, + number_of_patch_control_points: u64, + patch_start: u64, + patch_count: u64, + patch_index_buffer: Option<&Buffer>, + patch_index_buffer_offset: u64, + control_point_index_buffer: Option<&Buffer>, + control_point_index_buffer_offset: u64, + instance_count: u64, + base_instance: u64, + tessellation_factor_buffer: &Buffer, + tessellation_factor_buffer_offset: u64, + tessellation_factor_buffer_instance_stride: u64, + ) -> Result<(), Error> { + self.at().draw_indexed_patches( + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + control_point_index_buffer, + control_point_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + ) + } + + /// `setObjectBuffer:offset:atIndex:`: object buffer `index` at `buffer`'s address plus + /// `offset`'s low 32 bits, as Metal keeps it; `None` sets a nil buffer at the + /// offset's low 32 bits, as [`IndirectComputeCommand::set_kernel_buffer`] does, an index at + /// or past the descriptor's `max_object_buffer_bind_count` taken and kept nowhere as that + /// call's is. Refused (`Invalid`): a buffer of another device, a command of a buffer of + /// dispatches (`CommandKind`). + pub fn set_object_buffer( + &self, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.at().set_object_buffer(buffer, offset, index) + } + + /// `setMeshBuffer:offset:atIndex:`: as [`set_object_buffer`](Self::set_object_buffer), + /// against the descriptor's `max_mesh_buffer_bind_count`. + pub fn set_mesh_buffer( + &self, + buffer: Option<&Buffer>, + offset: u64, + index: u32, + ) -> Result<(), Error> { + self.at().set_mesh_buffer(buffer, offset, index) + } + + /// `drawMeshThreadgroups:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`: the + /// command draws `threadgroups_per_grid` threadgroups (of the object function where its + /// pipeline has one, else of the mesh function) of the threads given, each axis's low 32 + /// bits, as the encoder's `draw_mesh_threadgroups` draws them (`render/mesh.rs`). Refused + /// (`Invalid`) for a buffer whose records do not hold it (`CommandType`) and a command of a + /// buffer of dispatches (`CommandKind`). + pub fn draw_mesh_threadgroups( + &self, + threadgroups_per_grid: Size, + threads_per_object_threadgroup: Size, + threads_per_mesh_threadgroup: Size, + ) -> Result<(), Error> { + self.at().draw_mesh_threadgroups( + threadgroups_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + ) + } + + /// `drawMeshThreads:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`: the command + /// draws `threads_per_grid` threads, as the encoder's `draw_mesh_threads` draws them, each + /// axis's low 32 bits. Refused as + /// [`draw_mesh_threadgroups`](Self::draw_mesh_threadgroups) is. + pub fn draw_mesh_threads( + &self, + threads_per_grid: Size, + threads_per_object_threadgroup: Size, + threads_per_mesh_threadgroup: Size, + ) -> Result<(), Error> { + self.at().draw_mesh_threads( + threads_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + ) + } + + /// `setBarrier`: the command's draw runs after the earlier draws of its execution and of the + /// encoder, their writes visible to it, as Metal orders it on a buffer of mesh draws (a Metal + /// probe, the M3 Max and an M4: a reader's object function reads a writer's mesh function's + /// words) and plain Metal on a buffer without mesh draws, where Metal's validation stops the + /// call ("setBarrier is not enabled on this indirect command buffer (requires + /// DrawMeshThreadgroups or DrawMeshThreads)"; an M4). An execution draws such a command in a + /// render pass instance of its own (`render/executions.rs`), a memoryless attachment kept + /// across it, whether the barrier was set before or after the encoder that executes the + /// command ended. A command of a buffer of dispatches does nothing, as plain Metal's. + pub fn set_barrier(&self) -> Result<(), Error> { + self.at().set_barrier() + } + + /// `clearBarrier`: the command keeps no barrier, as plain Metal's reads back where its + /// validation stops the call on a buffer without mesh draws. A command of a buffer of + /// dispatches does nothing, as plain Metal's. + pub fn clear_barrier(&self) -> Result<(), Error> { + self.at().clear_barrier() + } + + /// `reset`: the command as a fresh buffer's. Refused (`CommandKind`) for a command of a + /// buffer of dispatches, where Metal's process ends (SIGSEGV), but taken on a buffer of no + /// command types, where plain Metal's returns (an M3 Max, macOS 27.0.1, three processes, + /// beside a compute command's `reset` on the same buffer, which ends the process). + pub fn reset(&self) -> Result<(), Error> { + self.at().reset() + } + + /// `setDepthStencilState:`: refused on a device that does not claim Apple10 (`Invalid`, + /// `RenderState`), where Metal's process ends, and `NotImplemented(IndirectRenderCommands)` on + /// one that does, whose commands would draw with it (the module docs). Refused (`Invalid`) + /// first: a state of another device. + pub fn set_depth_stencil_state(&self, state: Option<&DepthStencilState>) -> Result<(), Error> { + self.at().set_depth_stencil_state(state) + } + + /// `setDepthBias:slopeScale:clamp:`: refused, as `set_depth_stencil_state` is. + pub fn set_depth_bias( + &self, + _depth_bias: f32, + _slope_scale: f32, + _clamp: f32, + ) -> Result<(), Error> { + self.at().set_depth_bias(_depth_bias, _slope_scale, _clamp) + } + + /// `setDepthClipMode:`: refused, as `set_depth_stencil_state` is. + pub fn set_depth_clip_mode(&self, _mode: DepthClipMode) -> Result<(), Error> { + self.at().set_depth_clip_mode(_mode) + } + + /// `setCullMode:`: refused, as `set_depth_stencil_state` is. + pub fn set_cull_mode(&self, _mode: CullMode) -> Result<(), Error> { + self.at().set_cull_mode(_mode) + } + + /// `setFrontFacingWinding:`: refused, as `set_depth_stencil_state` is. + pub fn set_front_facing_winding(&self, _winding: Winding) -> Result<(), Error> { + self.at().set_front_facing_winding(_winding) + } + + /// `setTriangleFillMode:`: refused, as `set_depth_stencil_state` is. + pub fn set_triangle_fill_mode(&self, _mode: TriangleFillMode) -> Result<(), Error> { + self.at().set_triangle_fill_mode(_mode) + } + + /// `setObjectThreadgroupMemoryLength:atIndex:`: the command's mesh draw runs its object + /// function with the lengths the command set, 0 where it set none, never the encoder's (the + /// M3 Max: an argument past a length of 16 reached the next argument, whatever the encoder + /// set). Taken at any index, as plain Metal takes one at or past the descriptor's + /// `max_object_threadgroup_memory_bind_count`, where its validation stops it (an M4); the + /// record holds indices below 31, which is every one a function binds, and one past them + /// changes nothing. Differs: the record's first two lengths are an indexed patch draw's + /// control-point index buffer, so such a draw on the command clears them, and a length set + /// there while the command holds one is dropped. Refused (`Invalid`): a command of a buffer + /// whose command types hold no mesh draw (`ObjectThreadgroupMemory`), where Metal's process + /// ends, and of a buffer of dispatches (`CommandKind`). + pub fn set_object_threadgroup_memory_length( + &self, + length: u32, + index: u32, + ) -> Result<(), Error> { + self.at() + .set_object_threadgroup_memory_length(length, index) + } + + /// `getCommandType`, Apple's private getter: the command's + /// [`command_type`](EncodedRenderCommand::command_type) as + /// [`IndirectCommandBuffer::commands`] reads it now, its record's action alone read, as a + /// compute command's getters read theirs ([`IndirectComputeCommand::command_type`]). Each + /// getter is refused (`Invalid`, `CommandKind`) for a command of a buffer of dispatches, + /// where Metal's answer was not measured. + pub fn command_type(&self) -> Result { + self.at().command_type() + } + + /// `hasBarrier`, Apple's private getter: the command's + /// [`barrier`](EncodedRenderCommand::barrier), read alone. + pub fn has_barrier(&self) -> Result { + self.at().has_barrier() + } + + /// `drawArguments`, `drawIndexedArguments`, `drawPatchesArguments`, + /// `drawIndexedPatchesArguments` and `getTessellationFactorArguments`, Apple's private + /// getters: the command's [`draw`](EncodedRenderCommand::draw), read alone, as Metal answers + /// it: a draw of primitive type 7, 8 or 9 reads as one of points, Metal's 0 (the M3 Max, + /// macOS 27.0.1), though it draws as set. + pub fn draw(&self) -> Result, Error> { + self.at().draw() + } + + /// The vertex buffer set at `index`: the command's + /// [`vertex_buffers`](EncodedRenderCommand::vertex_buffers)`[index]`, read alone, none at or + /// past the descriptor's `max_vertex_buffer_bind_count`, which Metal's private getters of the + /// slot answer as [`EncodedRenderCommand::vertex_buffer_address`] and + /// [`EncodedRenderCommand::vertex_attribute_stride`] do. + pub fn vertex_buffer(&self, index: u64) -> Result, Error> { + self.at().vertex_buffer(index) + } + + /// The fragment buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a + /// vertex buffer, below the descriptor's `max_fragment_buffer_bind_count` + /// ([`EncodedRenderCommand::fragment_buffers`]). + pub fn fragment_buffer(&self, index: u64) -> Result, Error> { + self.at().fragment_buffer(index) + } + + /// The object buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a + /// vertex buffer, below the descriptor's `max_object_buffer_bind_count` + /// ([`EncodedRenderCommand::object_buffers`]). + pub fn object_buffer(&self, index: u64) -> Result, Error> { + self.at().object_buffer(index) + } + + /// The mesh buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a vertex + /// buffer, below the descriptor's `max_mesh_buffer_bind_count` + /// ([`EncodedRenderCommand::mesh_buffers`]). + pub fn mesh_buffer(&self, index: u64) -> Result, Error> { + self.at().mesh_buffer(index) + } + + /// The command's [`pipeline`](EncodedRenderCommand::pipeline), read alone. + pub fn pipeline_state(&self) -> Result, Error> { + self.at().pipeline_state() + } + + /// The command's [`pipeline_id`](EncodedRenderCommand::pipeline_id), read alone. + pub fn pipeline_id(&self) -> Result, Error> { + self.at().pipeline_id() + } + + /// `getPipelineStateUniqueIdentifier`, Apple's private getter: the command's + /// [`pipeline_unique_identifier`](EncodedRenderCommand::pipeline_unique_identifier), read + /// alone, whose kind the device announces as `commands` announces it. + pub fn pipeline_unique_identifier(&self) -> Result, Error> { + self.at().pipeline_unique_identifier() + } +} + +impl RenderAt<'_> { fn descriptor(&self) -> &IndirectCommandBufferDescriptor { &self.icb.0.descriptor } @@ -4761,32 +5607,53 @@ impl IndirectRenderCommand { !self.icb.is_render() } - /// Write `field` into the record, with `set` as the state the CPU set where it names one + /// Write `field`, which names no pipeline state, into the record /// (`IndirectCommandBuffer::edit`), for `call`: refused (`CommandKind`) for a command of a /// buffer of dispatches (`of_compute`). - fn edit( + fn edit(&self, call: &'static str, field: RecordField) -> Result<(), Error> { + self.edit_in(&mut self.icb.batch(self.index), call, field, Then::Nothing) + } + + /// [`edit`](Self::edit) of `field` in `batch`, then noting `then`. + fn edit_in( &self, + batch: &mut Batch<'_>, call: &'static str, field: RecordField, - set: Option>, + then: Then, ) -> Result<(), Error> { if self.of_compute() { return Err(problem(call, IndirectCommandBufferProblem::CommandKind)); } - self.icb.edit(self.index, field, set) + batch.field(field, then) } - /// `setRenderPipelineState:`. Refused (`Invalid`): a buffer that inherits its pipeline - /// state, a state made without `supportIndirectCommandBuffers` (a tile pipeline's among - /// them), a state of another device, and a state whose kind of draw the buffer's command - /// types lack (`PipelineKind`: a vertex function's on a buffer of mesh draws alone, a mesh - /// one's on a buffer of no mesh draw), where Metal's process ends. - pub fn set_render_pipeline_state(&self, state: &RenderPipelineState) -> Result<(), Error> { + fn set_render_pipeline_state(&self, state: &RenderPipelineState) -> Result<(), Error> { + self.pipeline_in(&mut self.icb.batch(self.index), state) + } + + /// [`set_render_pipeline_state`](Self::set_render_pipeline_state) of `state` in `batch`: + /// the state the command names already is written again without the lock or a hold, as + /// `ComputeAt::pipeline_in` writes a compute state. + fn pipeline_in(&self, batch: &mut Batch<'_>, state: &RenderPipelineState) -> Result<(), Error> { const CALL: &str = "set_render_pipeline_state"; - self.icb.check_command_types(CALL)?; + if let (Some(id), Some(serial)) = ( + NonZeroU64::new(state.gpu_resource_id().0), + state.id_serial(), + ) { + let key = StateKey { + compute: false, + id: id.get(), + serial, + }; + if self.icb.0.set_keys.names(self.index, key) { + return batch.field(RecordField::Pipeline(id), Then::Nothing); + } + } // The state's program, held while the command takes it (`pipeline/carry.rs`): one hold - // for every read of the state below. + // for every read of the state. let held = state.hold(); + self.icb.check_command_types(CALL)?; let device = state.bound_device()?; if !device.same(self.icb.generation()) { return Err(device.foreign(CALL, self.icb.generation())); @@ -4841,36 +5708,38 @@ impl IndirectRenderCommand { serial, }, named, - make: &|| { - CommandState::Render( - graphics.downgrade(), - graphics.program_held(&held), - stamp, - named, - ) - }, + stamp, + of: StateOf::Render(graphics, &held), }; - self.edit(CALL, RecordField::Pipeline(id), Some(set)) + if self.of_compute() { + return Err(problem(CALL, IndirectCommandBufferProblem::CommandKind)); + } + batch.state(id, set) } - /// `setVertexBuffer:offset:atIndex:`: vertex buffer `index` at `buffer`'s address plus - /// `offset`'s low 32 bits, as Metal keeps it, at the pipeline's own attribute stride; `None` - /// sets a nil buffer at the offset's low 32 bits, as - /// [`IndirectComputeCommand::set_kernel_buffer`] does, an index at or past the descriptor's - /// `max_vertex_buffer_bind_count` taken and kept nowhere as that call's is. Refused - /// (`Invalid`): a buffer of another device. On a buffer that inherits its buffers it is kept - /// and read back, and executions bind the encoder's. - pub fn set_vertex_buffer( + fn set_vertex_buffer( &self, buffer: Option<&Buffer>, offset: u64, index: u32, + ) -> Result<(), Error> { + self.vertex_buffer_in(&mut self.icb.batch(self.index), buffer, offset, index) + } + + /// [`set_vertex_buffer`](Self::set_vertex_buffer) in `batch`. + fn vertex_buffer_in( + &self, + batch: &mut Batch<'_>, + buffer: Option<&Buffer>, + offset: u64, + index: u32, ) -> Result<(), Error> { self.icb.check_command_types("set_vertex_buffer")?; if self.of_compute() { return Ok(()); } - self.set_buffer( + self.buffer_in( + batch, "set_vertex_buffer", BufferStage::Vertex, buffer, @@ -4880,39 +5749,63 @@ impl IndirectRenderCommand { ) } - /// `setVertexBuffer:offset:attributeStride:atIndex:`: as - /// [`set_vertex_buffer`](Self::set_vertex_buffer), at attribute stride `stride` (all ones is - /// the pipeline's own). On a buffer that does not support dynamic attribute strides the - /// stride is dropped and the buffer set, as plain Metal sets it where its validation stops - /// the call: the stride reads back all ones and the draw reads the buffer at its offset, as a - /// kernel buffer's is. `None` sets a nil buffer at the stride, as - /// [`IndirectComputeCommand::set_kernel_buffer_with_stride`] does. - pub fn set_vertex_buffer_with_stride( + fn set_vertex_buffer_with_stride( &self, buffer: Option<&Buffer>, offset: u64, stride: u64, index: u32, + ) -> Result<(), Error> { + let batch = &mut self.icb.batch(self.index); + self.vertex_buffer_with_stride_in(batch, buffer, offset, stride, index) + } + + /// [`set_vertex_buffer_with_stride`](Self::set_vertex_buffer_with_stride) in `batch`. + fn vertex_buffer_with_stride_in( + &self, + batch: &mut Batch<'_>, + buffer: Option<&Buffer>, + offset: u64, + stride: u64, + index: u32, ) -> Result<(), Error> { const CALL: &str = "set_vertex_buffer_with_stride"; self.icb.check_command_types(CALL)?; let dynamic = self.descriptor().support_dynamic_attribute_stride; let stride = (dynamic && stride != u64::MAX).then_some(stride); - self.set_buffer(CALL, BufferStage::Vertex, buffer, offset, stride, index) + self.buffer_in( + batch, + CALL, + BufferStage::Vertex, + buffer, + offset, + stride, + index, + ) } - /// `setFragmentBuffer:offset:atIndex:`: as [`set_vertex_buffer`](Self::set_vertex_buffer), - /// against the descriptor's `max_fragment_buffer_bind_count`. - pub fn set_fragment_buffer( + fn set_fragment_buffer( &self, buffer: Option<&Buffer>, offset: u64, index: u32, + ) -> Result<(), Error> { + self.fragment_buffer_in(&mut self.icb.batch(self.index), buffer, offset, index) + } + + /// [`set_fragment_buffer`](Self::set_fragment_buffer) in `batch`. + fn fragment_buffer_in( + &self, + batch: &mut Batch<'_>, + buffer: Option<&Buffer>, + offset: u64, + index: u32, ) -> Result<(), Error> { if self.of_compute() { return Ok(()); } - self.set_buffer( + self.buffer_in( + batch, "set_fragment_buffer", BufferStage::Fragment, buffer, @@ -4930,6 +5823,22 @@ impl IndirectRenderCommand { offset: u64, attribute_stride: Option, index: u32, + ) -> Result<(), Error> { + let batch = &mut self.icb.batch(self.index); + self.buffer_in(batch, call, stage, buffer, offset, attribute_stride, index) + } + + /// A buffer setter of `stage` for `call` in `batch`: refused (`CommandKind`) for a command of + /// a buffer of dispatches, then as [`Batch::buffer`] sets it against the stage's count. + fn buffer_in( + &self, + batch: &mut Batch<'_>, + call: &'static str, + stage: BufferStage, + buffer: Option<&Buffer>, + offset: u64, + attribute_stride: Option, + index: u32, ) -> Result<(), Error> { if self.of_compute() { return Err(problem(call, IndirectCommandBufferProblem::CommandKind)); @@ -4949,7 +5858,7 @@ impl IndirectRenderCommand { offset, attribute_stride, }; - self.icb.set_buffer(self.index, call, set) + batch.buffer(call, set) } /// Refuse a draw `action` of `kind` for `call` where the buffer's records cannot hold it @@ -4979,35 +5888,39 @@ impl IndirectRenderCommand { Ok(()) } - /// `drawPrimitives:vertexStart:vertexCount:instanceCount:baseInstance:`: the command draws - /// `vertex_count` vertices of `primitive_type` from `vertex_start`, `instance_count` times - /// from `base_instance`, each value's low 32 bits, which the record holds (Metal takes larger - /// values; what it keeps of them is not measured: its getter answers an object). A type is a - /// public one or one of Metal's private ones ([`DrawPrimitiveType`]), which an execution draws - /// as an encoder's draw of it draws, type 9 failing the command buffer so where an execution - /// of a range given at its call finds it there (the M3 Max, macOS 27.0.1: types 5 to 9 drew - /// and failed as the encoder's draws of them, direct and indexed). A draw of no vertices or - /// no instances runs nothing, as plain Metal runs it where its validation stops the call - /// ("instanceCount(0) must be non-zero"). The records of every render buffer but one - /// of mesh draws alone hold it: on a buffer whose command types lack `DRAW` it runs as on its - /// own, as plain Metal runs it, where Metal's validation stops the call ("drawPrimitives is not - /// enabled on this indirect command buffer"). Refused (`Invalid`): a buffer of mesh draws alone - /// (`CommandType`), where Metal's process ends, with and without its validation (an M4: buffers - /// of either mesh kind and of both, the encoder's pipeline inherited). - pub fn draw_primitives( + fn draw_primitives( &self, primitive_type: impl Into, vertex_start: u64, vertex_count: u64, instance_count: u64, base_instance: u64, + ) -> Result<(), Error> { + self.draw_primitives_in( + &mut self.icb.batch(self.index), + primitive_type.into(), + vertex_start, + vertex_count, + instance_count, + base_instance, + ) + } + + /// [`draw_primitives`](Self::draw_primitives) in `batch`. + fn draw_primitives_in( + &self, + batch: &mut Batch<'_>, + primitive_type: DrawPrimitiveType, + vertex_start: u64, + vertex_count: u64, + instance_count: u64, + base_instance: u64, ) -> Result<(), Error> { const CALL: &str = "draw_primitives"; self.icb.check_command_types(CALL)?; if self.of_compute() { return Ok(()); } - let primitive_type = primitive_type.into(); let action = Action::DrawPrimitives { primitive_type: primitive_type.raw() as u32, vertex_start: vertex_start as u32, @@ -5016,41 +5929,11 @@ impl IndirectRenderCommand { base_instance: base_instance as u32, }; self.check_draw(CALL, IndirectCommandType::DRAW, &action)?; - self.edit(CALL, RecordField::Action(action), None)?; - self.drew(primitive_type); - Ok(()) + let then = Then::Drew(primitive_type); + self.edit_in(batch, CALL, RecordField::Action(action), then) } - /// `drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount: - /// baseVertex:baseInstance:`: the command draws `index_count` indices of `index_type` from - /// `index_buffer_offset` of `index_buffer`, `instance_count` times from `base_instance`, each - /// index plus `base_vertex`, of a public primitive type or a private one, as - /// [`draw_primitives`](Self::draw_primitives) says (type 9 faults the command buffer, of any - /// index or instance count, as the encoder's draw does). The record holds the buffer as one - /// address (its `gpu_address` plus the offset's low 32 bits, as Metal keeps it; its validation - /// checks the whole offset, as the refusal below does), which an execution resolves against - /// the encoder's resident resources (`render/executions.rs`), and each value's low 32 bits, - /// `base_vertex` as a signed one: Metal keeps a base vertex's so (a count past 32 bits always - /// runs past the buffer, which is refused). It runs as on its own on a buffer of patch draws, - /// whose records hold it, as plain Metal runs it, where Metal's validation stops a buffer - /// whose command types lack `DRAW_INDEXED`. A draw of no indices or no instances runs nothing - /// (but type 9's), and the indices are read from the offset rounded down to a multiple of the - /// index size, as plain Metal runs them where its validation stops the call - /// (`idx_count_zero`, `idx_instances_zero`; 32-bit indices at offset 2 and 16-bit ones at 1 - /// drew those at 0). - /// - /// On a buffer of draws of primitives alone, whose records cannot hold it, the draw is - /// **taken** and its record kept, and an execution of the command **runs nothing**: it draws - /// no vertex, reads no index, and reports no fault, as plain Metal's execution of it does - /// (`IndirectCommandBuffer::takes_as_nothing`, which carries the reading). The read-back is - /// the indexed draw as set, the record's own, as every read-back is (what Metal's private - /// getters answer for such a record is unmeasured). - /// - /// Refused (`Invalid`): a buffer of mesh draws alone, whose records cannot hold it - /// (`CommandType`; Metal's process ends), and, as Metal's validation stops it, indices past - /// the buffer's length from that offset (`IndexBufferRange`, of the whole count; plain Metal - /// draws from whatever memory follows), and an index buffer of another device. - pub fn draw_indexed_primitives( + fn draw_indexed_primitives( &self, primitive_type: impl Into, index_count: u64, @@ -5060,9 +5943,34 @@ impl IndirectRenderCommand { instance_count: u64, base_vertex: i64, base_instance: u64, + ) -> Result<(), Error> { + self.draw_indexed_primitives_in( + &mut self.icb.batch(self.index), + primitive_type.into(), + index_count, + index_type, + index_buffer, + index_buffer_offset, + instance_count, + base_vertex, + base_instance, + ) + } + + /// [`draw_indexed_primitives`](Self::draw_indexed_primitives) in `batch`. + fn draw_indexed_primitives_in( + &self, + batch: &mut Batch<'_>, + primitive_type: DrawPrimitiveType, + index_count: u64, + index_type: IndexType, + index_buffer: &Buffer, + index_buffer_offset: u64, + instance_count: u64, + base_vertex: i64, + base_instance: u64, ) -> Result<(), Error> { const CALL: &str = "draw_indexed_primitives"; - let primitive_type = primitive_type.into(); let action = Action::DrawIndexedPrimitives { primitive_type: primitive_type.raw() as u32, index_count: index_count as u32, @@ -5094,50 +6002,11 @@ impl IndirectRenderCommand { }), )); } - self.edit(CALL, RecordField::Action(action), None)?; - self.drew(primitive_type); - Ok(()) + let then = Then::Drew(primitive_type); + self.edit_in(batch, CALL, RecordField::Action(action), then) } - /// The command now draws `primitive_type`: a draw of type 9 marks its buffer for the - /// executions that look for one ([`IndirectCommandBuffer::nine_among`]). - fn drew(&self, primitive_type: DrawPrimitiveType) { - if primitive_type == DrawPrimitiveType::Nine { - self.icb.0.nines.store(true, Ordering::Release); - } - } - - /// `drawPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset:instanceCount: - /// baseInstance:tessellationFactorBuffer:tessellationFactorBufferOffset: - /// tessellationFactorBufferInstanceStride:`: the command draws `patch_count` patches from - /// `patch_start` (each patch's index from `patch_index_buffer` where it has one), - /// `instance_count` times from `base_instance`, their factors read from - /// `tessellation_factor_buffer` at the offset and the instance stride. The record holds each - /// buffer as one address, at its offset's low 32 bits as Metal keeps it (the patch index and - /// factor buffers), which an execution resolves against the encoder's resident resources, and - /// each value's low 32 bits (Metal keeps a patch count's so). The control points are kept, - /// though Metal's execution draws with its pipeline's (measured: 3 and 0 drew as a quad - /// pipeline's 4), as the execution does, more than 32 included (a Metal probe `icb_cp33` drew - /// as 3 do, where Metal's validation stops the call); a draw of no patches or no instances runs - /// nothing, and the patch indices are read from the offset rounded down to a multiple of 4, as - /// plain Metal runs them where its validation stops the call (`patch_instances_zero`; indices 9 - /// and 3 at offset 2 drew patches 9 and 3). A factor offset and instance stride that are not - /// multiples of 4, which Metal's validation stops for the encoder's, are read as plain Metal - /// reads them, as the encoder's are: the factors from the offset rounded down to a multiple of - /// 2, each instance's at the stride rounded down to a multiple of 4 (Metal probes - /// `icb_factor_offset2`, 4 and 5: strides 2 and 14 drew what 0 and 12 draw, 6 and 10 what 4 and - /// 8 draw). It runs as on its own on a buffer of indexed patch draws, whose records hold it, as - /// plain Metal runs it, where Metal's validation stops a buffer whose command types lack - /// `DRAW_PATCHES`. Refused (`Invalid`): a buffer of neither patch kind, whose records cannot - /// hold it (`CommandType`; plain Metal ends its process), and, as Metal's validation stops - /// each, buffers of another device. Refused as the encoder's draw refuses them, which Metal's - /// validation of a command was not measured to: patch indices past the patch index buffer - /// from its offset's low 32 bits, which the command keeps (`PatchIndexBufferRange`; plain - /// Metal read 0 past it, the buffer's page padding, which stays undefined). An odd factor - /// offset is read from the byte below, and an odd instance stride - /// rounded down to a multiple of 4, as plain Metal reads a command's and the encoder's alike, - /// with and without its validation. - pub fn draw_patches( + fn draw_patches( &self, number_of_patch_control_points: u64, patch_start: u64, @@ -5174,35 +6043,10 @@ impl IndirectRenderCommand { None, tessellation_factor_buffer, )?; - self.edit(CALL, RecordField::Action(action), None) + self.edit(CALL, RecordField::Action(action)) } - /// `drawIndexedPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset: - /// controlPointIndexBuffer:controlPointIndexBufferOffset:instanceCount:baseInstance: - /// tessellationFactorBuffer:tessellationFactorBufferOffset: - /// tessellationFactorBufferInstanceStride:`: the command draws as - /// [`draw_patches`](Self::draw_patches) does, but control point `k` of the patch whose index is - /// `p` is the index (of its pipeline's `tessellationControlPointIndexType`) at position - /// `p · N + k` of `control_point_index_buffer` from its offset, `N` its pipeline's control - /// points, whatever the command names, as the encoder's `draw_indexed_patches` reads them - /// (a probe, an M4: the command's 0, 1 and 4 drew as the pipeline's 3). The record holds each - /// buffer as one address, at its offset's low 32 bits (the control-point index buffer's too), - /// and each value's low 32 bits (probe 9b: Metal keeps a patch start's so; its getters answer - /// each count's). `None` for the control-point index buffer, which Metal takes, makes the - /// command a plain patch draw of the same values, as Metal records it: its `getCommandType` - /// answers `DrawPatches` on an M4 (probe 9b, a buffer of indexed patch draws alone) and on the - /// consumer's M3 Max, and it draws as a plain patch draw on that pipeline draws, every - /// control-point index 0 on a pipeline with an index type (probe 9b: the same marks as its - /// plain patch draw on the same pipeline). It runs as on its own on a buffer of patch draws, - /// whose records hold it, as plain Metal runs it, where Metal's validation stops a buffer whose - /// command types lack `DRAW_INDEXED_PATCHES` ("drawIndexedPatches is not enabled on this - /// indirect command buffer"). Refused (`Invalid`) as `draw_patches` is, for a buffer of neither - /// patch kind (`CommandType`), and for a control-point index buffer of another device. Metal's - /// validation does not stop a factor offset or instance stride that is not a multiple of 4 here - /// (probe 9), which the runtime reads as `draw_patches` does; nor does it check the - /// control-point index buffer, whose index size the pipeline set at execution states (an - /// execution runs what Metal's does, `render/executions.rs`). - pub fn draw_indexed_patches( + fn draw_indexed_patches( &self, number_of_patch_control_points: u64, patch_start: u64, @@ -5267,10 +6111,10 @@ impl IndirectRenderCommand { { for index in 0..2 { let field = RecordField::ThreadgroupMemoryLength { index, length: 0 }; - self.edit(CALL, field, None)?; + self.edit(CALL, field)?; } } - self.edit(CALL, RecordField::Action(action), None) + self.edit(CALL, RecordField::Action(action)) } /// Refuse a patch draw's values for `call` as [`draw_patches`](Self::draw_patches) states: @@ -5313,13 +6157,7 @@ impl IndirectRenderCommand { Ok(()) } - /// `setObjectBuffer:offset:atIndex:`: object buffer `index` at `buffer`'s address plus - /// `offset`'s low 32 bits, as Metal keeps it; `None` sets a nil buffer at the - /// offset's low 32 bits, as [`IndirectComputeCommand::set_kernel_buffer`] does, an index at - /// or past the descriptor's `max_object_buffer_bind_count` taken and kept nowhere as that - /// call's is. Refused (`Invalid`): a buffer of another device, a command of a buffer of - /// dispatches (`CommandKind`). - pub fn set_object_buffer( + fn set_object_buffer( &self, buffer: Option<&Buffer>, offset: u64, @@ -5335,9 +6173,7 @@ impl IndirectRenderCommand { ) } - /// `setMeshBuffer:offset:atIndex:`: as [`set_object_buffer`](Self::set_object_buffer), - /// against the descriptor's `max_mesh_buffer_bind_count`. - pub fn set_mesh_buffer( + fn set_mesh_buffer( &self, buffer: Option<&Buffer>, offset: u64, @@ -5353,13 +6189,7 @@ impl IndirectRenderCommand { ) } - /// `drawMeshThreadgroups:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`: the - /// command draws `threadgroups_per_grid` threadgroups (of the object function where its - /// pipeline has one, else of the mesh function) of the threads given, each axis's low 32 - /// bits, as the encoder's `draw_mesh_threadgroups` draws them (`render/mesh.rs`). Refused - /// (`Invalid`) for a buffer whose records do not hold it (`CommandType`) and a command of a - /// buffer of dispatches (`CommandKind`). - pub fn draw_mesh_threadgroups( + fn draw_mesh_threadgroups( &self, threadgroups_per_grid: Size, threads_per_object_threadgroup: Size, @@ -5372,14 +6202,10 @@ impl IndirectRenderCommand { mesh_threads: low_bits(threads_per_mesh_threadgroup), }; self.check_draw(CALL, IndirectCommandType::DRAW_MESH_THREADGROUPS, &action)?; - self.edit(CALL, RecordField::Action(action), None) + self.edit(CALL, RecordField::Action(action)) } - /// `drawMeshThreads:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`: the command - /// draws `threads_per_grid` threads, as the encoder's `draw_mesh_threads` draws them, each - /// axis's low 32 bits. Refused as - /// [`draw_mesh_threadgroups`](Self::draw_mesh_threadgroups) is. - pub fn draw_mesh_threads( + fn draw_mesh_threads( &self, threads_per_grid: Size, threads_per_object_threadgroup: Size, @@ -5392,42 +6218,31 @@ impl IndirectRenderCommand { mesh_threads: low_bits(threads_per_mesh_threadgroup), }; self.check_draw(CALL, IndirectCommandType::DRAW_MESH_THREADS, &action)?; - self.edit(CALL, RecordField::Action(action), None) + self.edit(CALL, RecordField::Action(action)) } - /// `setBarrier`: the command's draw runs after the earlier draws of its execution and of the - /// encoder, their writes visible to it, as Metal orders it on a buffer of mesh draws (a Metal - /// probe, the M3 Max and an M4: a reader's object function reads a writer's mesh function's - /// words) and plain Metal on a buffer without mesh draws, where Metal's validation stops the - /// call ("setBarrier is not enabled on this indirect command buffer (requires - /// DrawMeshThreadgroups or DrawMeshThreads)"; an M4). An execution draws such a command in a - /// render pass instance of its own (`render/executions.rs`), a memoryless attachment kept - /// across it, whether the barrier was set before or after the encoder that executes the - /// command ended. A command of a buffer of dispatches does nothing, as plain Metal's. - pub fn set_barrier(&self) -> Result<(), Error> { + fn set_barrier(&self) -> Result<(), Error> { + self.barrier_in(&mut self.icb.batch(self.index), true) + } + + /// [`set_barrier`](Self::set_barrier) (`set`) or [`clear_barrier`](Self::clear_barrier) in + /// `batch`: nothing on a command of a buffer of dispatches. + fn barrier_in(&self, batch: &mut Batch<'_>, set: bool) -> Result<(), Error> { if self.of_compute() { return Ok(()); } - self.edit("set_barrier", RecordField::Barrier(true), None)?; - self.icb.0.barriers.store(true, Ordering::Release); - Ok(()) + let (call, then) = match set { + true => ("set_barrier", Then::Barrier), + false => ("clear_barrier", Then::Nothing), + }; + self.edit_in(batch, call, RecordField::Barrier(set), then) } - /// `clearBarrier`: the command keeps no barrier, as plain Metal's reads back where its - /// validation stops the call on a buffer without mesh draws. A command of a buffer of - /// dispatches does nothing, as plain Metal's. - pub fn clear_barrier(&self) -> Result<(), Error> { - if self.of_compute() { - return Ok(()); - } - self.edit("clear_barrier", RecordField::Barrier(false), None) + fn clear_barrier(&self) -> Result<(), Error> { + self.barrier_in(&mut self.icb.batch(self.index), false) } - /// `reset`: the command as a fresh buffer's. Refused (`CommandKind`) for a command of a - /// buffer of dispatches, where Metal's process ends (SIGSEGV), but taken on a buffer of no - /// command types, where plain Metal's returns (an M3 Max, macOS 27.0.1, three processes, - /// beside a compute command's `reset` on the same buffer, which ends the process). - pub fn reset(&self) -> Result<(), Error> { + fn reset(&self) -> Result<(), Error> { if self.of_compute() && !self.icb.has_no_command_types() { return Err(problem("reset", IndirectCommandBufferProblem::CommandKind)); } @@ -5454,11 +6269,7 @@ impl IndirectRenderCommand { problem(call, IndirectCommandBufferProblem::RenderState) } - /// `setDepthStencilState:`: refused on a device that does not claim Apple10 (`Invalid`, - /// `RenderState`), where Metal's process ends, and `NotImplemented(IndirectRenderCommands)` on - /// one that does, whose commands would draw with it (the module docs). Refused (`Invalid`) - /// first: a state of another device. - pub fn set_depth_stencil_state(&self, state: Option<&DepthStencilState>) -> Result<(), Error> { + fn set_depth_stencil_state(&self, state: Option<&DepthStencilState>) -> Result<(), Error> { const CALL: &str = "set_depth_stencil_state"; if let Some(state) = state.filter(|state| !state.root_device().same(self.icb.generation())) { @@ -5467,8 +6278,7 @@ impl IndirectRenderCommand { Err(self.render_state(CALL)) } - /// `setDepthBias:slopeScale:clamp:`: refused, as `set_depth_stencil_state` is. - pub fn set_depth_bias( + fn set_depth_bias( &self, _depth_bias: f32, _slope_scale: f32, @@ -5477,42 +6287,23 @@ impl IndirectRenderCommand { Err(self.render_state("set_depth_bias")) } - /// `setDepthClipMode:`: refused, as `set_depth_stencil_state` is. - pub fn set_depth_clip_mode(&self, _mode: DepthClipMode) -> Result<(), Error> { + fn set_depth_clip_mode(&self, _mode: DepthClipMode) -> Result<(), Error> { Err(self.render_state("set_depth_clip_mode")) } - /// `setCullMode:`: refused, as `set_depth_stencil_state` is. - pub fn set_cull_mode(&self, _mode: CullMode) -> Result<(), Error> { + fn set_cull_mode(&self, _mode: CullMode) -> Result<(), Error> { Err(self.render_state("set_cull_mode")) } - /// `setFrontFacingWinding:`: refused, as `set_depth_stencil_state` is. - pub fn set_front_facing_winding(&self, _winding: Winding) -> Result<(), Error> { + fn set_front_facing_winding(&self, _winding: Winding) -> Result<(), Error> { Err(self.render_state("set_front_facing_winding")) } - /// `setTriangleFillMode:`: refused, as `set_depth_stencil_state` is. - pub fn set_triangle_fill_mode(&self, _mode: TriangleFillMode) -> Result<(), Error> { + fn set_triangle_fill_mode(&self, _mode: TriangleFillMode) -> Result<(), Error> { Err(self.render_state("set_triangle_fill_mode")) } - /// `setObjectThreadgroupMemoryLength:atIndex:`: the command's mesh draw runs its object - /// function with the lengths the command set, 0 where it set none, never the encoder's (the - /// M3 Max: an argument past a length of 16 reached the next argument, whatever the encoder - /// set). Taken at any index, as plain Metal takes one at or past the descriptor's - /// `max_object_threadgroup_memory_bind_count`, where its validation stops it (an M4); the - /// record holds indices below 31, which is every one a function binds, and one past them - /// changes nothing. Differs: the record's first two lengths are an indexed patch draw's - /// control-point index buffer, so such a draw on the command clears them, and a length set - /// there while the command holds one is dropped. Refused (`Invalid`): a command of a buffer - /// whose command types hold no mesh draw (`ObjectThreadgroupMemory`), where Metal's process - /// ends, and of a buffer of dispatches (`CommandKind`). - pub fn set_object_threadgroup_memory_length( - &self, - length: u32, - index: u32, - ) -> Result<(), Error> { + fn set_object_threadgroup_memory_length(&self, length: u32, index: u32) -> Result<(), Error> { const CALL: &str = "set_object_threadgroup_memory_length"; if self.of_compute() { return Err(problem(CALL, IndirectCommandBufferProblem::CommandKind)); @@ -5535,7 +6326,7 @@ impl IndirectRenderCommand { index: index as usize, length, }; - self.edit(CALL, field, None) + self.edit(CALL, field) } /// Refuse, for `call`, a getter of a command of a buffer of dispatches (`CommandKind`, @@ -5548,86 +6339,57 @@ impl IndirectRenderCommand { } } - /// `getCommandType`, Apple's private getter: the command's - /// [`command_type`](EncodedRenderCommand::command_type) as - /// [`IndirectCommandBuffer::commands`] reads it now, its record's action alone read, as a - /// compute command's getters read theirs ([`IndirectComputeCommand::command_type`]). Each - /// getter is refused (`Invalid`, `CommandKind`) for a command of a buffer of dispatches, - /// where Metal's answer was not measured. - pub fn command_type(&self) -> Result { + fn command_type(&self) -> Result { self.check_getter("command_type")?; self.icb .read_record(self.index, |view| command_type(&view.action())) } - /// `hasBarrier`, Apple's private getter: the command's - /// [`barrier`](EncodedRenderCommand::barrier), read alone. - pub fn has_barrier(&self) -> Result { + fn has_barrier(&self) -> Result { self.check_getter("has_barrier")?; self.icb.read_record(self.index, |view| view.barrier()) } - /// `drawArguments`, `drawIndexedArguments`, `drawPatchesArguments`, - /// `drawIndexedPatchesArguments` and `getTessellationFactorArguments`, Apple's private - /// getters: the command's [`draw`](EncodedRenderCommand::draw), read alone, as Metal answers - /// it: a draw of primitive type 7, 8 or 9 reads as one of points, Metal's 0 (the M3 Max, - /// macOS 27.0.1), though it draws as set. - pub fn draw(&self) -> Result, Error> { + fn draw(&self) -> Result, Error> { self.check_getter("draw")?; self.icb .read_record(self.index, |view| answered_draw(&view.action())) } - /// The vertex buffer set at `index`: the command's - /// [`vertex_buffers`](EncodedRenderCommand::vertex_buffers)`[index]`, read alone, none at or - /// past the descriptor's `max_vertex_buffer_bind_count`, which Metal's private getters of the - /// slot answer as [`EncodedRenderCommand::vertex_buffer_address`] and - /// [`EncodedRenderCommand::vertex_attribute_stride`] do. - pub fn vertex_buffer(&self, index: u64) -> Result, Error> { + fn vertex_buffer(&self, index: u64) -> Result, Error> { self.check_getter("vertex_buffer")?; let count = self.descriptor().max_vertex_buffer_bind_count; self.icb .read_buffer(self.index, BufferStage::Vertex, count, index) } - /// The fragment buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a - /// vertex buffer, below the descriptor's `max_fragment_buffer_bind_count` - /// ([`EncodedRenderCommand::fragment_buffers`]). - pub fn fragment_buffer(&self, index: u64) -> Result, Error> { + fn fragment_buffer(&self, index: u64) -> Result, Error> { self.check_getter("fragment_buffer")?; let count = self.descriptor().max_fragment_buffer_bind_count; self.icb .read_buffer(self.index, BufferStage::Fragment, count, index) } - /// The object buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a - /// vertex buffer, below the descriptor's `max_object_buffer_bind_count` - /// ([`EncodedRenderCommand::object_buffers`]). - pub fn object_buffer(&self, index: u64) -> Result, Error> { + fn object_buffer(&self, index: u64) -> Result, Error> { self.check_getter("object_buffer")?; let count = self.descriptor().max_object_buffer_bind_count; self.icb .read_buffer(self.index, BufferStage::Object, count, index) } - /// The mesh buffer set at `index`, as [`vertex_buffer`](Self::vertex_buffer) reads a vertex - /// buffer, below the descriptor's `max_mesh_buffer_bind_count` - /// ([`EncodedRenderCommand::mesh_buffers`]). - pub fn mesh_buffer(&self, index: u64) -> Result, Error> { + fn mesh_buffer(&self, index: u64) -> Result, Error> { self.check_getter("mesh_buffer")?; let count = self.descriptor().max_mesh_buffer_bind_count; self.icb .read_buffer(self.index, BufferStage::Mesh, count, index) } - /// The command's [`pipeline`](EncodedRenderCommand::pipeline), read alone. - pub fn pipeline_state(&self) -> Result, Error> { + fn pipeline_state(&self) -> Result, Error> { self.check_getter("pipeline_state")?; Ok(self.icb.render_pipeline_now(self.index)?.0) } - /// The command's [`pipeline_id`](EncodedRenderCommand::pipeline_id), read alone. - pub fn pipeline_id(&self) -> Result, Error> { + fn pipeline_id(&self) -> Result, Error> { self.check_getter("pipeline_id")?; self.icb.read_record(self.index, |view| { let runs = view.pipeline().or(view.retained_pipeline()); @@ -5635,10 +6397,7 @@ impl IndirectRenderCommand { }) } - /// `getPipelineStateUniqueIdentifier`, Apple's private getter: the command's - /// [`pipeline_unique_identifier`](EncodedRenderCommand::pipeline_unique_identifier), read - /// alone, whose kind the device announces as `commands` announces it. - pub fn pipeline_unique_identifier(&self) -> Result, Error> { + fn pipeline_unique_identifier(&self) -> Result, Error> { self.check_getter("pipeline_unique_identifier")?; let (_, identifier) = self.icb.render_pipeline_now(self.index)?; self.icb.announce_identifier(identifier); diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer/carry.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer/carry.rs index 2fa06b382..fa1ddd6c9 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer/carry.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer/carry.rs @@ -8,8 +8,10 @@ //! they are; in pooled host-visible memory, new memory there, the records lost with the //! `VkDevice`, whose memory Vulkan makes undefined (vkspec 5.2.3, "Lost Device"), written again //! as a new buffer's: its header, every command reset, its slots past them zero. The CPU's -//! writes of the records wait for the carry (`pipelines`), and a host read outside a command -//! buffer's work holds the storage's parts it reads. +//! writes of the records under `pipelines` wait for the carry; a setter's write without it +//! (`Batch`: one that adds nothing to what the records may hold) does not, and one meanwhile +//! races the carry. A host read outside a command buffer's work holds the storage's parts it +//! reads. //! - **What stays.** The object, its records ID, address, `gpuResourceID`, `uniqueIdentifier` and //! label, which are the device's line's rather than a generation's (`device/line.rs`'s //! `Carried`); the threadgroup lengths past the last command's, which the host keeps; what its @@ -64,6 +66,9 @@ impl IndirectCommandBuffer { return Ok(Contents::Kept); } let mut commands = lock(&shared.pipelines); + // Each command's next set of its state checked again against the generation it meets, + // under the lock, whether the carry succeeds or not. + commands.states.forget_keys(); // The slots past the records first: a buffer whose storage stays the lost generation's // stays whole there, its device that of its storage (`generation`). if let Some(overflow) = &shared.overflow { diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_each.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_each.rs new file mode 100644 index 000000000..cfb2921a9 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_each.rs @@ -0,0 +1,576 @@ +//! A command's setters called in turn in one call ([`IndirectRenderCommand::set_each`], +//! [`IndirectComputeCommand::set_each`], and without a command's handle +//! [`IndirectCommandBuffer::set_render_command`] and +//! [`IndirectCommandBuffer::set_compute_command`]): a runtime extension with no Metal +//! counterpart, whose `MTLIndirectRenderCommand` and `MTLIndirectComputeCommand` have the setters +//! alone. Each call does what its setter does, refusals included; the first refused stops the +//! rest, its place answered with its error, and the calls before it stay made, as they would have +//! one setter call at a time. Their writes are one [`Batch`]: the storage's bytes found once, and +//! the writes made without the buffer's lock counted once. + +use super::{ + Batch, ComputeAt, IndirectCommandBuffer, IndirectComputeCommand, IndirectRenderCommand, + RenderAt, +}; +use crate::depth_stencil::DepthStencilState; +use crate::render_values::DrawPrimitiveType; +use crate::values::{Region, Size}; +use crate::{Buffer, ComputePipelineState, Error, RenderPipelineState}; +use metal2vulkan::metal::{CullMode, DepthClipMode, IndexType, TriangleFillMode, Winding}; + +/// One call of an [`IndirectRenderCommand`]'s setters, for +/// [`set_each`](IndirectRenderCommand::set_each): a variant a setter, named after it, its fields +/// the setter's arguments, so each does what its setter's Metal counterpart does there (its +/// docs). A runtime extension: Metal has no such value. +#[derive(Clone, Copy, Debug)] +pub enum IndirectRenderSet<'a> { + /// [`set_render_pipeline_state`](IndirectRenderCommand::set_render_pipeline_state) + /// (`setRenderPipelineState:`). + SetRenderPipelineState(&'a RenderPipelineState), + /// [`set_vertex_buffer`](IndirectRenderCommand::set_vertex_buffer) + /// (`setVertexBuffer:offset:atIndex:`). + SetVertexBuffer { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_vertex_buffer_with_stride`](IndirectRenderCommand::set_vertex_buffer_with_stride) + /// (`setVertexBuffer:offset:attributeStride:atIndex:`). + SetVertexBufferWithStride { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `attributeStride`. + stride: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_fragment_buffer`](IndirectRenderCommand::set_fragment_buffer) + /// (`setFragmentBuffer:offset:atIndex:`). + SetFragmentBuffer { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_object_buffer`](IndirectRenderCommand::set_object_buffer) + /// (`setObjectBuffer:offset:atIndex:`). + SetObjectBuffer { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_mesh_buffer`](IndirectRenderCommand::set_mesh_buffer) + /// (`setMeshBuffer:offset:atIndex:`). + SetMeshBuffer { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_object_threadgroup_memory_length`](IndirectRenderCommand::set_object_threadgroup_memory_length) + /// (`setObjectThreadgroupMemoryLength:atIndex:`). + SetObjectThreadgroupMemoryLength { + /// `length`. + length: u32, + /// `atIndex`. + index: u32, + }, + /// [`draw_primitives`](IndirectRenderCommand::draw_primitives) + /// (`drawPrimitives:vertexStart:vertexCount:instanceCount:baseInstance:`). + DrawPrimitives { + /// `primitiveType`. + primitive_type: DrawPrimitiveType, + /// `vertexStart`. + vertex_start: u64, + /// `vertexCount`. + vertex_count: u64, + /// `instanceCount`. + instance_count: u64, + /// `baseInstance`. + base_instance: u64, + }, + /// [`draw_indexed_primitives`](IndirectRenderCommand::draw_indexed_primitives) + /// (`drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:baseVertex:baseInstance:`). + DrawIndexedPrimitives { + /// `primitiveType`. + primitive_type: DrawPrimitiveType, + /// `indexCount`. + index_count: u64, + /// `indexType`. + index_type: IndexType, + /// `indexBuffer`. + index_buffer: &'a Buffer, + /// `indexBufferOffset`. + index_buffer_offset: u64, + /// `instanceCount`. + instance_count: u64, + /// `baseVertex`. + base_vertex: i64, + /// `baseInstance`. + base_instance: u64, + }, + /// [`draw_patches`](IndirectRenderCommand::draw_patches) + /// (`drawPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset:instanceCount:baseInstance:tessellationFactorBuffer:tessellationFactorBufferOffset:tessellationFactorBufferInstanceStride:`). + DrawPatches { + /// `numberOfPatchControlPoints`. + number_of_patch_control_points: u64, + /// `patchStart`. + patch_start: u64, + /// `patchCount`. + patch_count: u64, + /// `patchIndexBuffer`. + patch_index_buffer: Option<&'a Buffer>, + /// `patchIndexBufferOffset`. + patch_index_buffer_offset: u64, + /// `instanceCount`. + instance_count: u64, + /// `baseInstance`. + base_instance: u64, + /// `tessellationFactorBuffer`. + tessellation_factor_buffer: &'a Buffer, + /// `tessellationFactorBufferOffset`. + tessellation_factor_buffer_offset: u64, + /// `tessellationFactorBufferInstanceStride`. + tessellation_factor_buffer_instance_stride: u64, + }, + /// [`draw_indexed_patches`](IndirectRenderCommand::draw_indexed_patches) + /// (`drawIndexedPatches:patchStart:patchCount:patchIndexBuffer:patchIndexBufferOffset:controlPointIndexBuffer:controlPointIndexBufferOffset:instanceCount:baseInstance:tessellationFactorBuffer:tessellationFactorBufferOffset:tessellationFactorBufferInstanceStride:`). + DrawIndexedPatches { + /// `numberOfPatchControlPoints`. + number_of_patch_control_points: u64, + /// `patchStart`. + patch_start: u64, + /// `patchCount`. + patch_count: u64, + /// `patchIndexBuffer`. + patch_index_buffer: Option<&'a Buffer>, + /// `patchIndexBufferOffset`. + patch_index_buffer_offset: u64, + /// `controlPointIndexBuffer`. + control_point_index_buffer: Option<&'a Buffer>, + /// `controlPointIndexBufferOffset`. + control_point_index_buffer_offset: u64, + /// `instanceCount`. + instance_count: u64, + /// `baseInstance`. + base_instance: u64, + /// `tessellationFactorBuffer`. + tessellation_factor_buffer: &'a Buffer, + /// `tessellationFactorBufferOffset`. + tessellation_factor_buffer_offset: u64, + /// `tessellationFactorBufferInstanceStride`. + tessellation_factor_buffer_instance_stride: u64, + }, + /// [`draw_mesh_threadgroups`](IndirectRenderCommand::draw_mesh_threadgroups) + /// (`drawMeshThreadgroups:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`). + DrawMeshThreadgroups { + /// `threadgroupsPerGrid`. + threadgroups_per_grid: Size, + /// `threadsPerObjectThreadgroup`. + threads_per_object_threadgroup: Size, + /// `threadsPerMeshThreadgroup`. + threads_per_mesh_threadgroup: Size, + }, + /// [`draw_mesh_threads`](IndirectRenderCommand::draw_mesh_threads) + /// (`drawMeshThreads:threadsPerObjectThreadgroup:threadsPerMeshThreadgroup:`). + DrawMeshThreads { + /// `threadsPerGrid`. + threads_per_grid: Size, + /// `threadsPerObjectThreadgroup`. + threads_per_object_threadgroup: Size, + /// `threadsPerMeshThreadgroup`. + threads_per_mesh_threadgroup: Size, + }, + /// [`set_barrier`](IndirectRenderCommand::set_barrier) (`setBarrier`). + SetBarrier, + /// [`clear_barrier`](IndirectRenderCommand::clear_barrier) (`clearBarrier`). + ClearBarrier, + /// [`reset`](IndirectRenderCommand::reset) (`reset`). + Reset, + /// [`set_depth_stencil_state`](IndirectRenderCommand::set_depth_stencil_state) + /// (`setDepthStencilState:`). + SetDepthStencilState(Option<&'a DepthStencilState>), + /// [`set_depth_bias`](IndirectRenderCommand::set_depth_bias) + /// (`setDepthBias:slopeScale:clamp:`). + SetDepthBias { + /// `depthBias`. + depth_bias: f32, + /// `slopeScale`. + slope_scale: f32, + /// `clamp`. + clamp: f32, + }, + /// [`set_depth_clip_mode`](IndirectRenderCommand::set_depth_clip_mode) + /// (`setDepthClipMode:`). + SetDepthClipMode(DepthClipMode), + /// [`set_cull_mode`](IndirectRenderCommand::set_cull_mode) (`setCullMode:`). + SetCullMode(CullMode), + /// [`set_front_facing_winding`](IndirectRenderCommand::set_front_facing_winding) + /// (`setFrontFacingWinding:`). + SetFrontFacingWinding(Winding), + /// [`set_triangle_fill_mode`](IndirectRenderCommand::set_triangle_fill_mode) + /// (`setTriangleFillMode:`). + SetTriangleFillMode(TriangleFillMode), +} + +/// One call of an [`IndirectComputeCommand`]'s setters, for +/// [`set_each`](IndirectComputeCommand::set_each), as [`IndirectRenderSet`] is a render +/// command's. A runtime extension: Metal has no such value. +#[derive(Clone, Copy, Debug)] +pub enum IndirectComputeSet<'a> { + /// [`set_compute_pipeline_state`](IndirectComputeCommand::set_compute_pipeline_state) + /// (`setComputePipelineState:`). + SetComputePipelineState(&'a ComputePipelineState), + /// [`set_kernel_buffer`](IndirectComputeCommand::set_kernel_buffer) + /// (`setKernelBuffer:offset:atIndex:`). + SetKernelBuffer { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `atIndex`. + index: u32, + }, + /// [`set_kernel_buffer_with_stride`](IndirectComputeCommand::set_kernel_buffer_with_stride) + /// (`setKernelBuffer:offset:attributeStride:atIndex:`). + SetKernelBufferWithStride { + /// `buffer`. + buffer: Option<&'a Buffer>, + /// `offset`. + offset: u64, + /// `attributeStride`. + stride: u64, + /// `atIndex`. + index: u32, + }, + /// [`concurrent_dispatch_threadgroups`](IndirectComputeCommand::concurrent_dispatch_threadgroups) + /// (`concurrentDispatchThreadgroups:threadsPerThreadgroup:`). + ConcurrentDispatchThreadgroups { + /// `threadgroupsPerGrid`. + threadgroups: Size, + /// `threadsPerThreadgroup`. + threads_per_threadgroup: Size, + }, + /// [`concurrent_dispatch_threads`](IndirectComputeCommand::concurrent_dispatch_threads) + /// (`concurrentDispatchThreads:threadsPerThreadgroup:`). + ConcurrentDispatchThreads { + /// `threadsPerGrid`. + threads: Size, + /// `threadsPerThreadgroup`. + threads_per_threadgroup: Size, + }, + /// [`set_barrier`](IndirectComputeCommand::set_barrier) (`setBarrier`). + SetBarrier, + /// [`clear_barrier`](IndirectComputeCommand::clear_barrier) (`clearBarrier`). + ClearBarrier, + /// [`set_threadgroup_memory_length`](IndirectComputeCommand::set_threadgroup_memory_length) + /// (`setThreadgroupMemoryLength:atIndex:`). + SetThreadgroupMemoryLength { + /// `length`. + length: u32, + /// `atIndex`. + index: u32, + }, + /// [`set_stage_in_region`](IndirectComputeCommand::set_stage_in_region) + /// (`setStageInRegion:`). + SetStageInRegion(Region), + /// [`set_imageblock_size`](IndirectComputeCommand::set_imageblock_size) + /// (`setImageblockWidth:height:`). + SetImageblockSize { + /// `width`. + width: u64, + /// `height`. + height: u64, + }, + /// [`reset`](IndirectComputeCommand::reset) (`reset`). + Reset, +} + +impl IndirectCommandBuffer { + /// `indirectRenderCommandAtIndex:` and the setters `sets` name on its command, called in turn + /// ([`IndirectRenderSet`]), as [`IndirectRenderCommand::set_each`] calls them: a runtime + /// extension, the calls Metal makes one at a time made in one, with no command's handle (the + /// buffer's is not cloned). Command `index`'s refusals, as + /// [`indirect_render_command_at`](Self::indirect_render_command_at)'s, are answered at place + /// 0, before any setter is called, `sets` empty or not; a setter's refusal at its place, those + /// before it made and those after it not taken. + pub fn set_render_command<'a>( + &self, + index: u64, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + const CALL: &str = "set_render_command"; + let index = (self.command_at(CALL, index, true)).map_err(|error| (0, error))?; + RenderAt { icb: self, index }.set_each(sets) + } + + /// `indirectComputeCommandAtIndex:` and the setters `sets` name on its command, called in turn + /// ([`IndirectComputeSet`]), as [`IndirectComputeCommand::set_each`] calls them: a runtime + /// extension with no command's handle, as [`set_render_command`](Self::set_render_command) + /// is. Command `index`'s refusals, as + /// [`indirect_compute_command_at`](Self::indirect_compute_command_at)'s (a buffer of render + /// command types among them), are answered at place 0, before any setter is called. + pub fn set_compute_command<'a>( + &self, + index: u64, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + const CALL: &str = "set_compute_command"; + let index = (self.command_at(CALL, index, false)).map_err(|error| (0, error))?; + ComputeAt { icb: self, index }.set_each(sets) + } +} + +impl IndirectRenderCommand { + /// The setters `sets` name, called in turn ([`IndirectRenderSet`]): a runtime extension with + /// no Metal counterpart, each call doing what its setter does. The first a setter refuses + /// stops them, answered with its place among them and the setter's error; those before it + /// stay made, and those after it are not taken, as one setter call at a time would leave the + /// command. + pub fn set_each<'a>( + &self, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + self.at().set_each(sets) + } +} + +impl IndirectComputeCommand { + /// The setters `sets` name, called in turn ([`IndirectComputeSet`]), as + /// [`IndirectRenderCommand::set_each`] calls a render command's: a runtime extension with no + /// Metal counterpart; the first refused stops them, answered with its place and error, those + /// before it made. + pub fn set_each<'a>( + &self, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + self.at().set_each(sets) + } +} + +impl RenderAt<'_> { + /// [`IndirectRenderCommand::set_each`] of `sets`, their writes one [`Batch`]. + fn set_each<'a>( + &self, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + let mut batch = self.icb.batch(self.index); + for (at, set) in sets.into_iter().enumerate() { + self.set_one(&mut batch, set).map_err(|error| (at, error))?; + } + Ok(()) + } + + /// The setter `set` names: in `batch` where the setter writes a field, else the setter. + fn set_one(&self, batch: &mut Batch<'_>, set: IndirectRenderSet<'_>) -> Result<(), Error> { + use IndirectRenderSet as S; + match set { + S::SetRenderPipelineState(state) => self.pipeline_in(batch, state), + S::SetVertexBuffer { + buffer, + offset, + index, + } => self.vertex_buffer_in(batch, buffer, offset, index), + S::SetVertexBufferWithStride { + buffer, + offset, + stride, + index, + } => self.vertex_buffer_with_stride_in(batch, buffer, offset, stride, index), + S::SetFragmentBuffer { + buffer, + offset, + index, + } => self.fragment_buffer_in(batch, buffer, offset, index), + S::SetObjectBuffer { + buffer, + offset, + index, + } => self.set_object_buffer(buffer, offset, index), + S::SetMeshBuffer { + buffer, + offset, + index, + } => self.set_mesh_buffer(buffer, offset, index), + S::SetObjectThreadgroupMemoryLength { length, index } => { + self.set_object_threadgroup_memory_length(length, index) + } + S::DrawPrimitives { + primitive_type, + vertex_start, + vertex_count, + instance_count, + base_instance, + } => self.draw_primitives_in( + batch, + primitive_type, + vertex_start, + vertex_count, + instance_count, + base_instance, + ), + S::DrawIndexedPrimitives { + primitive_type, + index_count, + index_type, + index_buffer, + index_buffer_offset, + instance_count, + base_vertex, + base_instance, + } => self.draw_indexed_primitives_in( + batch, + primitive_type, + index_count, + index_type, + index_buffer, + index_buffer_offset, + instance_count, + base_vertex, + base_instance, + ), + S::DrawPatches { + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + } => self.draw_patches( + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + ), + S::DrawIndexedPatches { + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + control_point_index_buffer, + control_point_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + } => self.draw_indexed_patches( + number_of_patch_control_points, + patch_start, + patch_count, + patch_index_buffer, + patch_index_buffer_offset, + control_point_index_buffer, + control_point_index_buffer_offset, + instance_count, + base_instance, + tessellation_factor_buffer, + tessellation_factor_buffer_offset, + tessellation_factor_buffer_instance_stride, + ), + S::DrawMeshThreadgroups { + threadgroups_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + } => self.draw_mesh_threadgroups( + threadgroups_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + ), + S::DrawMeshThreads { + threads_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + } => self.draw_mesh_threads( + threads_per_grid, + threads_per_object_threadgroup, + threads_per_mesh_threadgroup, + ), + S::SetBarrier => self.barrier_in(batch, true), + S::ClearBarrier => self.barrier_in(batch, false), + S::Reset => self.reset(), + S::SetDepthStencilState(state) => self.set_depth_stencil_state(state), + S::SetDepthBias { + depth_bias, + slope_scale, + clamp, + } => self.set_depth_bias(depth_bias, slope_scale, clamp), + S::SetDepthClipMode(mode) => self.set_depth_clip_mode(mode), + S::SetCullMode(mode) => self.set_cull_mode(mode), + S::SetFrontFacingWinding(winding) => self.set_front_facing_winding(winding), + S::SetTriangleFillMode(mode) => self.set_triangle_fill_mode(mode), + } + } +} + +impl ComputeAt<'_> { + /// [`IndirectComputeCommand::set_each`] of `sets`, their writes one [`Batch`]. + fn set_each<'a>( + &self, + sets: impl IntoIterator>, + ) -> Result<(), (usize, Error)> { + let mut batch = self.icb.batch(self.index); + for (at, set) in sets.into_iter().enumerate() { + self.set_one(&mut batch, set).map_err(|error| (at, error))?; + } + Ok(()) + } + + /// The setter `set` names: in `batch` where the setter writes a field, else the setter. + fn set_one(&self, batch: &mut Batch<'_>, set: IndirectComputeSet<'_>) -> Result<(), Error> { + use IndirectComputeSet as S; + match set { + S::SetComputePipelineState(state) => self.pipeline_in(batch, state), + S::SetKernelBuffer { + buffer, + offset, + index, + } => self.kernel_buffer_in(batch, buffer, offset, index), + S::SetKernelBufferWithStride { + buffer, + offset, + stride, + index, + } => self.kernel_buffer_with_stride_in(batch, buffer, offset, stride, index), + S::ConcurrentDispatchThreadgroups { + threadgroups, + threads_per_threadgroup, + } => self.dispatch_threadgroups_in(batch, threadgroups, threads_per_threadgroup), + S::ConcurrentDispatchThreads { + threads, + threads_per_threadgroup, + } => self.dispatch_threads_in(batch, threads, threads_per_threadgroup), + S::SetBarrier => self.barrier_in(batch, true), + S::ClearBarrier => self.barrier_in(batch, false), + S::SetThreadgroupMemoryLength { length, index } => { + self.set_threadgroup_memory_length(length, index) + } + S::SetStageInRegion(region) => self.set_stage_in_region(region), + S::SetImageblockSize { width, height } => self.set_imageblock_size(width, height), + S::Reset => self.reset(), + } + } +} diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_states.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_states.rs index 8f71911e6..8a2f2a710 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_states.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer/set_states.rs @@ -6,10 +6,13 @@ //! set costs a store whatever the buffer's size, and the table keeps one entry a state, however //! many commands name it. An entry goes with the last command that names it, so the table holds //! no state none of the commands names. A recovery's carry carries each entry once -//! ([`entries_mut`](SetStates::entries_mut)). +//! ([`entries_mut`](SetStates::entries_mut)). Each command's key is also kept where a setter +//! reads it without the lock ([`SetKeys`]). use std::collections::HashMap; use std::ops::Range; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, OnceLock}; /// What tells two states the CPU set apart: the kind, the ID and the serial of the state's hold /// of it (`IdStamp`), which no other state's hold of any ID shares. @@ -25,6 +28,62 @@ pub(super) trait Keyed { fn key(&self) -> StateKey; } +/// The key of the state each command names, as [`SetStates`] holds it, readable without the +/// buffer's lock: two words a command, the state's ID (0 for none) and its serial above its kind, +/// stored under the lock as a set or a clear changes the command's state, none until a state is +/// set. A setter of the state a command names already finds it here and writes its ID again +/// without the lock, the table unchanged. A command's two words are stored one after the other: +/// a read of them while another thread sets the same command is the program's race, as two +/// threads encoding one command are on Metal. +pub(super) struct SetKeys { + size: u32, + words: OnceLock>, +} + +impl SetKeys { + fn new(size: u32) -> SetKeys { + SetKeys { + size, + words: OnceLock::new(), + } + } + + /// Whether command `index` (below the size) names the state of key `key`. + pub(super) fn names(&self, index: u32, key: StateKey) -> bool { + let Some(words) = self.words.get() else { + return false; + }; + let at = 2 * index as usize; + words[at].load(Ordering::Acquire) == key.id + && words[at + 1].load(Ordering::Acquire) == serial_and_kind(key) + } + + /// Command `index` (below the size) names the state of key `key`, or none. + fn store(&self, index: u32, key: Option) { + let words = self.words.get_or_init(|| { + (0..2 * self.size as usize) + .map(|_| AtomicU64::new(0)) + .collect() + }); + let at = 2 * index as usize; + let (id, serial) = key.map_or((0, 0), |key| (key.id, serial_and_kind(key))); + words[at].store(id, Ordering::Release); + words[at + 1].store(serial, Ordering::Release); + } + + /// No command names a state. + fn clear_all(&self) { + for word in self.words.get().into_iter().flatten() { + word.store(0, Ordering::Release); + } + } +} + +/// `key`'s serial above its kind, in one word. +fn serial_and_kind(key: StateKey) -> u64 { + key.serial << 1 | u64::from(key.compute) +} + /// A distinct state, and how many commands name it. struct Entry { state: S, @@ -47,11 +106,18 @@ pub(super) struct SetStates { /// The place of the entry set last, looked at first: a buffer's commands mostly name one /// state in turn. last: Option<(StateKey, u32)>, + /// Each command's key, for the setters' look without the lock. + keys: Arc, } impl SetStates { /// The table of a buffer of `size` commands: none set. pub(super) fn new(size: u32) -> SetStates { + SetStates::empty(size, Arc::new(SetKeys::new(size))) + } + + /// The table of a buffer of `size` commands, none set, its keys in `keys`. + fn empty(size: u32, keys: Arc) -> SetStates { SetStates { size, slots: Vec::new(), @@ -59,9 +125,15 @@ impl SetStates { free: Vec::new(), places: HashMap::new(), last: None, + keys, } } + /// Each command's key, readable without the lock ([`SetKeys`]). + pub(super) fn keys(&self) -> Arc { + Arc::clone(&self.keys) + } + /// The state the CPU set on command `index`. pub(super) fn get(&self, index: u32) -> Option<&S> { let slot = *self.slots.get(index as usize)?; @@ -95,6 +167,9 @@ impl SetStates { } let slot = place + 1; let before = std::mem::replace(&mut self.slots[index as usize], slot); + // Stored again where the command named the state already: the keys may have been + // forgotten since (`forget_keys`). + self.keys.store(index, Some(key)); if before == slot { return; } @@ -108,13 +183,24 @@ impl SetStates { pub(super) fn clear(&mut self, index: u32) { if let Some(slot) = self.slots.get_mut(index as usize) { let before = std::mem::replace(slot, 0); + if before != 0 { + self.keys.store(index, None); + } self.release(before); } } + /// The keys read without the lock name no state until a set stores each command's again, + /// the table unchanged: a recovery's carry forgets them, so that each command's next set of + /// its state is checked again, under the lock, against the generation it was carried to. + pub(super) fn forget_keys(&self) { + self.keys.clear_all(); + } + /// No command names a state from now on. pub(super) fn clear_all(&mut self) { - *self = SetStates::new(self.size); + self.keys.clear_all(); + *self = SetStates::empty(self.size, Arc::clone(&self.keys)); } /// Each distinct state a command names, once. @@ -195,11 +281,14 @@ mod tests { /// One entry a distinct state, made once while a command names it, shared by the commands /// that name it, gone with the last of them and made again after; a reset command names - /// none; the range skips commands that name none. + /// none; the range skips commands that name none. The keys read without the lock follow + /// every set and clear, kind included, and come back with a set after they were forgotten. #[test] fn a_state_is_kept_once_while_a_command_names_it() { let mut table = SetStates::new(6); + let keys = table.keys(); assert!(table.get(0).is_none()); + assert!(!keys.names(0, key(7)), "none before a set"); assert_eq!(table.in_range(0..6).count(), 0); let mut made = 0; let mut set = |table: &mut SetStates, index, id| { @@ -216,6 +305,13 @@ mod tests { assert_eq!(table.get(0), Some(&Made(key(7), 1))); assert_eq!(table.get(1), Some(&Made(key(9), 2))); assert_eq!(table.get(5), None); + assert!(keys.names(0, key(7)) && keys.names(1, key(9))); + assert!(!keys.names(1, key(7)) && !keys.names(5, key(7))); + let compute = StateKey { + compute: true, + ..key(7) + }; + assert!(!keys.names(0, compute), "the kind tells keys apart"); let ids: Vec = table.in_range(0..6).map(|state| state.0.id).collect(); assert_eq!(ids, [7, 9, 7, 7, 9]); assert_eq!(table.entries_mut().count(), 2); @@ -223,6 +319,7 @@ mod tests { for index in [0, 2] { table.clear(index); } + assert!(!keys.names(0, key(7)) && keys.names(3, key(7))); assert_eq!(table.entries_mut().count(), 2); set(&mut table, 3, 9); assert_eq!(table.entries_mut().count(), 1); @@ -237,5 +334,14 @@ mod tests { table.clear_all(); assert_eq!(table.in_range(0..6).count(), 0); assert_eq!(table.entries_mut().count(), 0); + assert!(!keys.names(0, key(7)), "the same keys, cleared"); + set(&mut table, 2, 9); + assert!(keys.names(2, key(9))); + // Forgotten, then stored again by a set of the state the command names already. + table.forget_keys(); + assert!(!keys.names(2, key(9))); + set(&mut table, 2, 9); + assert!(keys.names(2, key(9))); + assert_eq!(table.entries_mut().count(), 1); } } diff --git a/crates/metal2vulkan-runtime/src/kept_commands.rs b/crates/metal2vulkan-runtime/src/kept_commands.rs index 706fa3329..8e1ba8a47 100644 --- a/crates/metal2vulkan-runtime/src/kept_commands.rs +++ b/crates/metal2vulkan-runtime/src/kept_commands.rs @@ -532,8 +532,8 @@ pub(super) struct StepChunk { pub(super) copy: StepCopy, } -/// The chunks the CPU wrote a record of without the buffer's lock (a buffer slot's setter, -/// `IndirectCommandBuffer::edit_unlocked`), noted for the next look at the kept recordings to +/// The chunks the CPU wrote a record of without the buffer's lock (a setter's write, in +/// `indirect_command_buffer`'s `Batch`), noted for the next look at the kept recordings to /// forget ([`KeptChunks::settle`]): a flag a chunk, and one that some flag is set. A write notes /// its chunk after it wrote, and the note is published (`Release`) before the write is counted /// (`IndirectCommandBuffer::cpu_writes`), so a settle after a read of the count that counts the diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs index 4ca375345..7dc0b2ffd 100644 --- a/crates/metal2vulkan-runtime/src/lib.rs +++ b/crates/metal2vulkan-runtime/src/lib.rs @@ -343,8 +343,9 @@ pub use host_ram::{HostRam, HostRamUse}; pub use indirect_command_buffer::{ EncodedComputeCommand, EncodedRenderCommand, IndirectBufferBinding, IndirectCommand, IndirectCommandBuffer, IndirectCommandBufferDescriptor, IndirectCommandBufferProblem, - IndirectCommandCheck, IndirectCommandCheckNeeds, IndirectComputeCommand, IndirectDispatch, - IndirectDraw, IndirectRenderCommand, NIL_PATCH_BUFFER_ADDRESS, + IndirectCommandCheck, IndirectCommandCheckNeeds, IndirectComputeCommand, IndirectComputeSet, + IndirectDispatch, IndirectDraw, IndirectRenderCommand, IndirectRenderSet, + NIL_PATCH_BUFFER_ADDRESS, }; pub use interop::{SemaphoreProblem, VkEventSignal, VkEventWait, VkHostMemory}; pub use library::{Function, FunctionDescriptor, Library}; diff --git a/crates/metal2vulkan-runtime/src/tests/icb_encode_bench.rs b/crates/metal2vulkan-runtime/src/tests/icb_encode_bench.rs index f71cefa01..9c5945371 100644 --- a/crates/metal2vulkan-runtime/src/tests/icb_encode_bench.rs +++ b/crates/metal2vulkan-runtime/src/tests/icb_encode_bench.rs @@ -9,6 +9,9 @@ //! `icb_slots_cost` loop through the runtime alone; //! - **compute slot**: `indirect_compute_command_at(i)` of a buffer of dispatches, a compute //! pipeline state, two kernel buffers (one at `(i % 16) * 16`) and a dispatch of threadgroups; +//! - **set_each**: each slot's setters in one `set_each` call; +//! - **set_render_command**, **set_compute_command**: each slot's setters in one call of the +//! buffer's, with no command's handle; //! - **three fields**: `indirect_render_command_at(i)`, a vertex buffer at `(i % 8) * 256`, a //! fragment buffer at `(i % 4) * 64` and a draw of primitives, a consumer's loop; //! - **draw only**: the command and its draw; @@ -24,7 +27,7 @@ use super::blit_holds::{device, Scratch}; use super::indirect_command_buffer_device::{compute_state, render_state, EXAMPLES}; use crate::{ DrawPrimitiveType, Error, IndirectCommandBuffer, IndirectCommandBufferDescriptor, - ResourceOptions, Size, + IndirectComputeSet, IndirectRenderSet, ResourceOptions, Size, }; use metal2vulkan::bind::indirect_commands::COMMAND_BYTES; use metal2vulkan::metal::IndirectCommandType; @@ -111,6 +114,46 @@ fn icb_encode_costs() { command.draw_primitives(triangle, 0, 3, 1, 0).unwrap(); }), ); + let render_sets = |i: u64| { + [ + IndirectRenderSet::SetRenderPipelineState(&render), + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&vertex), + offset: (i % 16) * 16, + index: 0, + }, + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&fragment), + offset: 0, + index: 1, + }, + IndirectRenderSet::SetFragmentBuffer { + buffer: Some(&vertex), + offset: 256, + index: 0, + }, + IndirectRenderSet::DrawPrimitives { + primitive_type: triangle, + vertex_start: 0, + vertex_count: 3, + instance_count: 1, + base_instance: 0, + }, + ] + }; + print( + &format!("{size} commands, full slot, set_each"), + per_command(size, |i| { + let command = icb.indirect_render_command_at(i).unwrap(); + command.set_each(render_sets(i)).unwrap(); + }), + ); + print( + &format!("{size} commands, full slot, set_render_command"), + per_command(size, |i| { + icb.set_render_command(i, render_sets(i)).unwrap(); + }), + ); let dispatches: IndirectCommandBuffer = device .new_indirect_command_buffer(&compute_descriptor, size, shared) .unwrap(); @@ -128,6 +171,38 @@ fn icb_encode_costs() { .unwrap(); }), ); + let compute_sets = |i: u64| { + [ + IndirectComputeSet::SetComputePipelineState(&compute), + IndirectComputeSet::SetKernelBuffer { + buffer: Some(&vertex), + offset: (i % 16) * 16, + index: 0, + }, + IndirectComputeSet::SetKernelBuffer { + buffer: Some(&fragment), + offset: 0, + index: 1, + }, + IndirectComputeSet::ConcurrentDispatchThreadgroups { + threadgroups: group, + threads_per_threadgroup: group, + }, + ] + }; + print( + &format!("{size} commands, compute slot, set_each"), + per_command(size, |i| { + let command = dispatches.indirect_compute_command_at(i).unwrap(); + command.set_each(compute_sets(i)).unwrap(); + }), + ); + print( + &format!("{size} commands, compute slot, set_compute_command"), + per_command(size, |i| { + dispatches.set_compute_command(i, compute_sets(i)).unwrap(); + }), + ); print( &format!("{size} commands, three fields"), per_command(size, |i| { diff --git a/crates/metal2vulkan-runtime/src/tests/indirect_command_buffer_device.rs b/crates/metal2vulkan-runtime/src/tests/indirect_command_buffer_device.rs index ea7fe4ce3..5dc241146 100644 --- a/crates/metal2vulkan-runtime/src/tests/indirect_command_buffer_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/indirect_command_buffer_device.rs @@ -18,9 +18,10 @@ use crate::error::{internal, invalid}; use crate::write_routes::GpuWriter; use crate::written_commands::Named; use crate::{ - Buffer, ComputePipelineState, Device, DrawProblem, Error, IndirectCommandBuffer, - IndirectCommandBufferDescriptor, IndirectCommandBufferProblem, PipelineOptions, Problem, - Region, RenderPipelineState, ResourceOptions, RuntimeGap, Size, + Buffer, ComputePipelineState, Device, DrawPrimitiveType, DrawProblem, Error, GpuAddress, + IndirectBufferBinding, IndirectCommandBuffer, IndirectCommandBufferDescriptor, + IndirectCommandBufferProblem, IndirectComputeSet, IndirectDraw, IndirectRenderSet, + PipelineOptions, Problem, Region, RenderPipelineState, ResourceOptions, RuntimeGap, Size, }; use metal2vulkan::bind::indirect_commands::{ Action, BufferBinding, BufferStage, CommandRecord, RecordField, RecordView, @@ -741,3 +742,474 @@ fn device_of_another(scratch: &Scratch) -> Device { std::fs::create_dir_all(&cache).unwrap(); device(&cache) } + +/// `set_each`, and `set_render_command` and `set_compute_command` with no command's handle, set a +/// command as its setters one call at a time do: the same records and the same CPU writes +/// counted, a render and a compute buffer alike; the first refused stops the rest, answered with +/// its place and the setter's error, those before it made and those after it not taken. The +/// buffer's calls answer the command's own refusals (an index past the size, a compute command of +/// a render buffer) at place 0. +#[test] +fn set_each_sets_as_the_setters_one_at_a_time() { + let scratch = Scratch::new("icb-set-each"); + let device = device(&scratch.0); + let state = render_state(&device, true); + let unsupported = render_state(&device, false); + let shared = ResourceOptions::shared(); + let (a, b) = ( + device.new_buffer(4096, shared).unwrap(), + device.new_buffer(4096, shared).unwrap(), + ); + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::DRAW; + descriptor.max_vertex_buffer_bind_count = 2; + descriptor.max_fragment_buffer_bind_count = 1; + let made = || { + device + .new_indirect_command_buffer(&descriptor, u64::from(COMMANDS), shared) + .unwrap() + }; + let (one_by_one, each, whole) = (made(), made(), made()); + let triangle = DrawPrimitiveType::from_raw(3).unwrap(); + for index in 0..u64::from(COMMANDS) { + let c = one_by_one.indirect_render_command_at(index).unwrap(); + c.set_render_pipeline_state(&state).unwrap(); + c.set_vertex_buffer(Some(&a), index * 16, 0).unwrap(); + c.set_vertex_buffer(Some(&b), 0, 1).unwrap(); + c.set_fragment_buffer(Some(&a), 256, 0).unwrap(); + c.draw_primitives(triangle, 0, 3, 1, index).unwrap(); + let sets = [ + IndirectRenderSet::SetRenderPipelineState(&state), + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&a), + offset: index * 16, + index: 0, + }, + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&b), + offset: 0, + index: 1, + }, + IndirectRenderSet::SetFragmentBuffer { + buffer: Some(&a), + offset: 256, + index: 0, + }, + IndirectRenderSet::DrawPrimitives { + primitive_type: triangle, + vertex_start: 0, + vertex_count: 3, + instance_count: 1, + base_instance: index, + }, + ]; + let c = each.indirect_render_command_at(index).unwrap(); + assert_eq!(c.set_each(sets), Ok(()), "command {index}"); + assert_eq!(whole.set_render_command(index, sets), Ok(())); + } + assert!(storage(&one_by_one) == storage(&each), "the records"); + assert!( + storage(&one_by_one) == storage(&whole), + "the buffer's records" + ); + assert_eq!( + one_by_one.cpu_writes(), + each.cpu_writes(), + "the writes counted" + ); + assert_eq!(one_by_one.cpu_writes(), whole.cpu_writes()); + let named = |icb: &IndirectCommandBuffer| { + (0..u64::from(COMMANDS)) + .map(|index| { + let c = icb.indirect_render_command_at(index).unwrap(); + c.pipeline_state().unwrap().map(|s| s.gpu_resource_id()) + }) + .collect::>() + }; + assert_eq!(named(&one_by_one), named(&each), "the states set"); + assert_eq!(named(&one_by_one), named(&whole)); + + // The command's own refusals, at place 0 before any setter, none set. + let before = (storage(&whole), whole.cpu_writes()); + let past = u64::from(COMMANDS); + let refused = whole.indirect_render_command_at(past).err().unwrap(); + let draw = [IndirectRenderSet::SetBarrier]; + assert_eq!(whole.set_render_command(past, draw), Err((0, refused))); + let refused = problem( + "set_compute_command", + IndirectCommandBufferProblem::CommandKind, + ); + assert_eq!(whole.set_compute_command(0, []), Err((0, refused))); + assert!( + (storage(&whole), whole.cpu_writes()) == before, + "nothing set" + ); + + // A refusal mid-way: the buffer before it set, the draw after it not taken. + let fresh = made(); + let c = fresh.indirect_render_command_at(0).unwrap(); + let before = fresh.cpu_writes(); + let sets = [ + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&a), + offset: 32, + index: 0, + }, + IndirectRenderSet::SetRenderPipelineState(&unsupported), + IndirectRenderSet::DrawPrimitives { + primitive_type: triangle, + vertex_start: 0, + vertex_count: 3, + instance_count: 1, + base_instance: 0, + }, + ]; + let refused = problem( + "set_render_pipeline_state", + IndirectCommandBufferProblem::PipelineWithoutSupport, + ); + assert_eq!(c.set_each(sets), Err((1, refused))); + let set = c.vertex_buffer(0).unwrap().map(|binding| binding.address); + assert_eq!( + set, + Some(GpuAddress(a.gpu_address().0 + 32)), + "the buffer before" + ); + assert_eq!(c.draw().unwrap(), None, "the draw after"); + assert_eq!( + c.pipeline_state().unwrap().map(|s| s.gpu_resource_id()), + None + ); + assert_eq!(fresh.cpu_writes(), before + 1, "the one write made"); + + // A compute command, and a refusal first: nothing made. + let kernel = compute_state(&device, EXAMPLES, "scale_and_offset", true); + let plain = compute_state(&device, EXAMPLES, "scale_and_offset", false); + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::CONCURRENT_DISPATCH; + descriptor.max_kernel_buffer_bind_count = 2; + let made = || { + device + .new_indirect_command_buffer(&descriptor, 2, shared) + .unwrap() + }; + let (one_by_one, each, whole) = (made(), made(), made()); + let (groups, threads) = (Size::new(4, 1, 1), Size::new(64, 1, 1)); + let c = one_by_one.indirect_compute_command_at(1).unwrap(); + c.set_compute_pipeline_state(&kernel).unwrap(); + c.set_kernel_buffer(Some(&a), 48, 0).unwrap(); + c.set_kernel_buffer(Some(&b), 0, 1).unwrap(); + c.concurrent_dispatch_threadgroups(groups, threads).unwrap(); + let c = each.indirect_compute_command_at(1).unwrap(); + let sets = [ + IndirectComputeSet::SetComputePipelineState(&kernel), + IndirectComputeSet::SetKernelBuffer { + buffer: Some(&a), + offset: 48, + index: 0, + }, + IndirectComputeSet::SetKernelBuffer { + buffer: Some(&b), + offset: 0, + index: 1, + }, + IndirectComputeSet::ConcurrentDispatchThreadgroups { + threadgroups: groups, + threads_per_threadgroup: threads, + }, + ]; + assert_eq!(c.set_each(sets), Ok(())); + assert_eq!(whole.set_compute_command(1, sets), Ok(())); + assert!( + storage(&one_by_one) == storage(&each), + "the compute records" + ); + assert!(storage(&one_by_one) == storage(&whole)); + assert_eq!(one_by_one.cpu_writes(), each.cpu_writes()); + assert_eq!(one_by_one.cpu_writes(), whole.cpu_writes()); + let refused = whole.indirect_compute_command_at(2).err().unwrap(); + assert_eq!(whole.set_compute_command(2, sets), Err((0, refused))); + let c = each.indirect_compute_command_at(0).unwrap(); + let before = (storage(&each), each.cpu_writes()); + let sets = [ + IndirectComputeSet::SetComputePipelineState(&plain), + IndirectComputeSet::SetKernelBuffer { + buffer: Some(&a), + offset: 0, + index: 0, + }, + ]; + let refused = problem( + "set_compute_pipeline_state", + IndirectCommandBufferProblem::PipelineWithoutSupport, + ); + assert_eq!(c.set_each(sets), Err((0, refused))); + assert!( + (storage(&each), each.cpu_writes()) == before, + "nothing made" + ); +} + +/// Setters on two threads at once: each thread's commands read back as it set them, the writes +/// all counted, where the threads set different commands of one buffer (one by its commands' +/// setters, the other by `set_render_command`); and where one thread sets +/// a command's pipeline state over and over while the other sets its buffers, each reads back its +/// own field as it set it after every set, and the command reads back the last state set, its +/// record naming that state's ID and identifier, and the last buffers set: no field one thread +/// wrote is lost to the other's write. +#[test] +fn setters_on_two_threads_keep_each_other_s_fields() { + const SETS: u64 = 2000; + let scratch = Scratch::new("icb-two-threads"); + let device = device(&scratch.0); + let states = [render_state(&device, true), render_state(&device, true)]; + assert_ne!(states[0].gpu_resource_id(), states[1].gpu_resource_id()); + let shared = ResourceOptions::shared(); + let (a, b) = ( + device.new_buffer(1 << 20, shared).unwrap(), + device.new_buffer(1 << 20, shared).unwrap(), + ); + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::DRAW; + descriptor.max_vertex_buffer_bind_count = 2; + descriptor.max_fragment_buffer_bind_count = 1; + let size = 64; + let icb = device + .new_indirect_command_buffer(&descriptor, size, shared) + .unwrap(); + let triangle = DrawPrimitiveType::from_raw(3).unwrap(); + let before = icb.cpu_writes(); + // Different commands: thread `t` sets the commands `index % 2 == t`, each over and over, + // the last round's values its own. + let slot = |t: usize, index: u64, round: u64| { + if t == 1 { + let sets = [ + IndirectRenderSet::SetRenderPipelineState(&states[t]), + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&a), + offset: (index + round) * 16, + index: 0, + }, + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&b), + offset: index * 32, + index: 1, + }, + IndirectRenderSet::SetFragmentBuffer { + buffer: Some(&b), + offset: round * 16, + index: 0, + }, + IndirectRenderSet::DrawPrimitives { + primitive_type: triangle, + vertex_start: index, + vertex_count: 3, + instance_count: 1, + base_instance: round, + }, + ]; + return icb.set_render_command(index, sets).unwrap(); + } + let c = icb.indirect_render_command_at(index).unwrap(); + c.set_render_pipeline_state(&states[t]).unwrap(); + c.set_vertex_buffer(Some(&a), (index + round) * 16, 0) + .unwrap(); + c.set_vertex_buffer(Some(&b), index * 32, 1).unwrap(); + c.set_fragment_buffer(Some(&b), round * 16, 0).unwrap(); + c.draw_primitives(triangle, index, 3, 1, round).unwrap(); + }; + let rounds = SETS / size; + std::thread::scope(|scope| { + for t in 0..2 { + let slot = &slot; + scope.spawn(move || { + for round in 0..rounds { + for index in (t as u64..size).step_by(2) { + slot(t, index, round); + } + } + }); + } + }); + let last = rounds - 1; + for index in 0..size { + let c = icb.indirect_render_command_at(index).unwrap(); + let t = (index % 2) as usize; + let named = c.pipeline_state().unwrap().map(|s| s.gpu_resource_id()); + assert_eq!(named, Some(states[t].gpu_resource_id()), "command {index}"); + let at = |buffer: &Buffer, offset: u64| Some(GpuAddress(buffer.gpu_address().0 + offset)); + let address = |binding: Option| binding.map(|b| b.address); + assert_eq!( + address(c.vertex_buffer(0).unwrap()), + at(&a, (index + last) * 16) + ); + assert_eq!(address(c.vertex_buffer(1).unwrap()), at(&b, index * 32)); + assert_eq!(address(c.fragment_buffer(0).unwrap()), at(&b, last * 16)); + let drawn = c.draw().unwrap(); + let expected = IndirectDraw::Primitives { + primitive_type: triangle, + vertex_start: index, + vertex_count: 3, + instance_count: 1, + base_instance: last, + }; + assert_eq!(drawn, Some(expected), "command {index}"); + } + assert_eq!( + icb.cpu_writes() - before, + 5 * rounds * size, + "every write counted" + ); + + // One command: its pipeline state on one thread, its buffers on the other, each thread + // reading back its own field after every set, which the other's writes must leave as set. + let c = icb.indirect_render_command_at(7).unwrap(); + std::thread::scope(|scope| { + scope.spawn(|| { + for set in 0..SETS { + let state = &states[(set % 2) as usize]; + c.set_render_pipeline_state(state).unwrap(); + let id = c.pipeline_id().unwrap(); + assert_eq!(id, Some(state.gpu_resource_id()), "set {set}: the state"); + } + }); + scope.spawn(|| { + for set in 0..SETS { + c.set_vertex_buffer(Some(&b), set * 16, 0).unwrap(); + c.set_fragment_buffer(Some(&a), set * 32, 0).unwrap(); + let address = c.vertex_buffer(0).unwrap().map(|b| b.address); + let expected = GpuAddress(b.gpu_address().0 + set * 16); + assert_eq!(address, Some(expected), "set {set}: the vertex buffer"); + } + }); + }); + let state = &states[((SETS - 1) % 2) as usize]; + let named = c.pipeline_state().unwrap().map(|s| s.gpu_resource_id()); + assert_eq!(named, Some(state.gpu_resource_id()), "the last state set"); + assert_eq!(c.pipeline_id().unwrap(), Some(state.gpu_resource_id())); + assert_eq!( + c.pipeline_unique_identifier().unwrap(), + Some(state.unique_identifier()), + "the record names the state set" + ); + let address = c.vertex_buffer(0).unwrap().map(|b| b.address); + assert_eq!( + address, + Some(GpuAddress(b.gpu_address().0 + (SETS - 1) * 16)) + ); + let address = c.fragment_buffer(0).unwrap().map(|b| b.address); + assert_eq!( + address, + Some(GpuAddress(a.gpu_address().0 + (SETS - 1) * 32)) + ); +} + +/// A setter's write takes the buffer's lock only where it sets a pipeline state the command does +/// not name yet or adds to what the records may hold, on a buffer the GPU may not have written: a +/// buffer slot, a draw or a barrier the CPU's edits added already, a barrier cleared, and the +/// state the command names already go without it, counted apart (`writes_apart`), a call of +/// `set_render_command` counting its writes so once; on a buffer the GPU may write, every write +/// takes it. +#[test] +fn setters_take_the_lock_only_to_set_a_state_or_add_to_the_records() { + let scratch = Scratch::new("icb-unlocked-writes"); + let device = device(&scratch.0); + let state = render_state(&device, true); + let shared = ResourceOptions::shared(); + let buffer = device.new_buffer(4096, shared).unwrap(); + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::DRAW; + descriptor.max_vertex_buffer_bind_count = 1; + let icb = device + .new_indirect_command_buffer(&descriptor, u64::from(COMMANDS), shared) + .unwrap(); + let c: Vec<_> = (0..u64::from(COMMANDS)) + .map(|index| icb.indirect_render_command_at(index).unwrap()) + .collect(); + let (triangle, point) = (PrimitiveType::Triangle, PrimitiveType::Point); + let mut at = icb.writes_apart(); + // A call's writes, and how many went under the lock and how many without it. + let mut went = |what: &str, call: Result<(), Error>, locked: u64, unlocked: u64| { + call.unwrap(); + let now = icb.writes_apart(); + assert_eq!(now, (at.0 + locked, at.1 + unlocked), "{what}"); + at = now; + }; + went("a state", c[0].set_render_pipeline_state(&state), 1, 0); + went( + "a buffer", + c[0].set_vertex_buffer(Some(&buffer), 0, 0), + 0, + 1, + ); + went( + "a first draw", + c[0].draw_primitives(triangle, 0, 3, 1, 0), + 1, + 0, + ); + went( + "the draw again", + c[1].draw_primitives(triangle, 0, 3, 1, 0), + 0, + 1, + ); + went( + "another type", + c[1].draw_primitives(point, 0, 3, 1, 0), + 1, + 0, + ); + went("a first barrier", c[1].set_barrier(), 1, 0); + went("a barrier again", c[2].set_barrier(), 0, 1); + went("a barrier cleared", c[2].clear_barrier(), 0, 1); + let other = render_state(&device, true); + let (one, other) = (&state, &other); + let states = [ + ("the state, another command", one, 1, 0), + ("a state named already", one, 0, 1), + ("another state", other, 1, 0), + ("that state again", other, 0, 1), + ]; + for (what, state, locked, unlocked) in states { + went( + what, + c[1].set_render_pipeline_state(state), + locked, + unlocked, + ); + } + let sets = [ + IndirectRenderSet::SetVertexBuffer { + buffer: Some(&buffer), + offset: 64, + index: 0, + }, + IndirectRenderSet::DrawPrimitives { + primitive_type: DrawPrimitiveType::from_raw(3).unwrap(), + vertex_start: 0, + vertex_count: 3, + instance_count: 1, + base_instance: 0, + }, + ]; + let batch = icb.set_render_command(3, sets).map_err(|(_, error)| error); + went("a batch", batch, 0, 2); + icb.written_by_gpu(GpuWriter::Reset); + went( + "GPU-written, a draw", + c[3].draw_primitives(triangle, 0, 3, 1, 0), + 1, + 0, + ); + went( + "GPU-written, a buffer", + c[3].set_vertex_buffer(Some(&buffer), 0, 0), + 1, + 0, + ); + let named = c[1].set_render_pipeline_state(other); + went("GPU-written, a state named", named, 1, 0); + let named = c[1].pipeline_state().unwrap().map(|s| s.gpu_resource_id()); + assert_eq!(named, Some(other.gpu_resource_id()), "the state set last"); +} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 814c4f44a..d50029bb7 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 518; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2241; +pub const RUNTIME_VERSION: u32 = 2242; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/crates/metal2vulkan/src/indirect_commands.rs b/crates/metal2vulkan/src/indirect_commands.rs index 5c2fbf768..4b5688196 100644 --- a/crates/metal2vulkan/src/indirect_commands.rs +++ b/crates/metal2vulkan/src/indirect_commands.rs @@ -580,6 +580,13 @@ pub enum Action { } impl Action { + /// What a record holding the action, and no barrier, holds of what a command may write + /// ([`CommandRecord::writes`] of such a record): its operation, and a draw's primitive type + /// (every type for a private one, past Metal's five). + pub fn writes(&self) -> crate::pipeline::CommandWrites { + writes_of(self, false) + } + /// Whether the action's arguments take the stage-in region's words /// (`layout::DRAW_ARGUMENT_WORDS`): an indexed draw's, a patch draw's, an indexed patch /// draw's or a mesh draw's. A render command sets no stage-in region. diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 3fac8da82..10e2ff987 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1330,8 +1330,10 @@ enum metal2vulkan-runtime::InPlaceRefusal #[derive(Clone, Copy, Debug, PartialEq enum metal2vulkan-runtime::InPlaceRowEnds #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum InPlaceRowEnds enum metal2vulkan-runtime::IndirectCommand #[derive(Clone, Debug)] #[non_exhaustive] pub enum IndirectCommand enum metal2vulkan-runtime::IndirectCommandBufferProblem #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum IndirectCommandBufferProblem +enum metal2vulkan-runtime::IndirectComputeSet #[derive(Clone, Copy, Debug)] pub enum IndirectComputeSet<'a> enum metal2vulkan-runtime::IndirectDispatch #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum IndirectDispatch enum metal2vulkan-runtime::IndirectDraw #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum IndirectDraw +enum metal2vulkan-runtime::IndirectRenderSet #[derive(Clone, Copy, Debug)] pub enum IndirectRenderSet<'a> enum metal2vulkan-runtime::IndirectRule #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum IndirectRule enum metal2vulkan-runtime::InstanceApiVersion #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum InstanceApiVersion enum metal2vulkan-runtime::LayerSettingValue #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum LayerSettingValue @@ -8748,8 +8750,10 @@ method metal2vulkan-runtime::IndirectCommandBuffer::may_name_encoding_states pub method metal2vulkan-runtime::IndirectCommandBuffer::reset_with_range pub fn reset_with_range(&self, range: Range) -> Result<(), Error> method metal2vulkan-runtime::IndirectCommandBuffer::resource_index pub fn resource_index(&self) -> u64 method metal2vulkan-runtime::IndirectCommandBuffer::resource_options pub fn resource_options(&self) -> ResourceOptions +method metal2vulkan-runtime::IndirectCommandBuffer::set_compute_command pub fn set_compute_command<'a>(&self, index: u64, sets: impl IntoIterator>) -> Result<(), (usize, Error)> method metal2vulkan-runtime::IndirectCommandBuffer::set_label pub fn set_label(&self, label: &str) method metal2vulkan-runtime::IndirectCommandBuffer::set_purgeable_state pub fn set_purgeable_state(&self, state: PurgeableState) -> Result +method metal2vulkan-runtime::IndirectCommandBuffer::set_render_command pub fn set_render_command<'a>(&self, index: u64, sets: impl IntoIterator>) -> Result<(), (usize, Error)> method metal2vulkan-runtime::IndirectCommandBuffer::size pub fn size(&self) -> u64 method metal2vulkan-runtime::IndirectCommandBuffer::storage_mode pub fn storage_mode(&self) -> StorageMode method metal2vulkan-runtime::IndirectCommandBuffer::unique_identifier pub fn unique_identifier(&self) -> u64 @@ -8769,6 +8773,7 @@ method metal2vulkan-runtime::IndirectComputeCommand::pipeline_unique_identifier method metal2vulkan-runtime::IndirectComputeCommand::reset pub fn reset(&self) -> Result<(), Error> method metal2vulkan-runtime::IndirectComputeCommand::set_barrier pub fn set_barrier(&self) -> Result<(), Error> method metal2vulkan-runtime::IndirectComputeCommand::set_compute_pipeline_state pub fn set_compute_pipeline_state(&self, state: &ComputePipelineState) -> Result<(), Error> +method metal2vulkan-runtime::IndirectComputeCommand::set_each pub fn set_each<'a>(&self, sets: impl IntoIterator>) -> Result<(), (usize, Error)> method metal2vulkan-runtime::IndirectComputeCommand::set_imageblock_size pub fn set_imageblock_size(&self, width: u64, height: u64) -> Result<(), Error> method metal2vulkan-runtime::IndirectComputeCommand::set_kernel_buffer pub fn set_kernel_buffer(&self, buffer: Option<&Buffer>, offset: u64, index: u32) -> Result<(), Error> method metal2vulkan-runtime::IndirectComputeCommand::set_kernel_buffer_with_stride pub fn set_kernel_buffer_with_stride(&self, buffer: Option<&Buffer>, offset: u64, stride: u64, index: u32) -> Result<(), Error> @@ -8800,6 +8805,7 @@ method metal2vulkan-runtime::IndirectRenderCommand::set_cull_mode pub fn set_cul method metal2vulkan-runtime::IndirectRenderCommand::set_depth_bias pub fn set_depth_bias(&self, _depth_bias: f32, _slope_scale: f32, _clamp: f32) -> Result<(), Error> method metal2vulkan-runtime::IndirectRenderCommand::set_depth_clip_mode pub fn set_depth_clip_mode(&self, _mode: DepthClipMode) -> Result<(), Error> method metal2vulkan-runtime::IndirectRenderCommand::set_depth_stencil_state pub fn set_depth_stencil_state(&self, state: Option<&DepthStencilState>) -> Result<(), Error> +method metal2vulkan-runtime::IndirectRenderCommand::set_each pub fn set_each<'a>(&self, sets: impl IntoIterator>) -> Result<(), (usize, Error)> method metal2vulkan-runtime::IndirectRenderCommand::set_fragment_buffer pub fn set_fragment_buffer(&self, buffer: Option<&Buffer>, offset: u64, index: u32) -> Result<(), Error> method metal2vulkan-runtime::IndirectRenderCommand::set_front_facing_winding pub fn set_front_facing_winding(&self, _winding: Winding) -> Result<(), Error> method metal2vulkan-runtime::IndirectRenderCommand::set_mesh_buffer pub fn set_mesh_buffer(&self, buffer: Option<&Buffer>, offset: u64, index: u32) -> Result<(), Error> @@ -9575,6 +9581,7 @@ method metal2vulkan::bind::YCbCrRefreshModule::shape pub fn shape(firsts: [u32; method metal2vulkan::bind::YCbCrSampleModule::push pub fn push(size: [u32; 2], extent: [u32; 2]) -> [u8; Self::PUSH_BYTES as usize] method metal2vulkan::bind::YCbCrWriteModule::groups pub fn groups(planes: &YCbCrPlanes) -> [u32; 2] method metal2vulkan::bind::YCbCrWriteModule::shape pub fn shape(planes: &YCbCrPlanes, all: bool) -> [u8; Self::SHAPE_WORDS as usize * 4] +method metal2vulkan::bind::indirect_commands::Action::writes pub fn writes(&self) -> crate::pipeline::CommandWrites method metal2vulkan::bind::indirect_commands::Bindings::buffers pub fn buffers(&self, stage: BufferStage) -> &[Option; BINDING_COUNT] method metal2vulkan::bind::indirect_commands::Bindings::buffers_mut pub fn buffers_mut(&mut self, stage: BufferStage) -> &mut [Option; BINDING_COUNT] method metal2vulkan::bind::indirect_commands::BufferBinding::buffer_alignment pub fn buffer_alignment(&self) -> u64 @@ -12574,6 +12581,17 @@ variant metal2vulkan-runtime::IndirectCommandBufferProblem::PipelineKind Pipelin variant metal2vulkan-runtime::IndirectCommandBufferProblem::PipelineWithoutSupport PipelineWithoutSupport variant metal2vulkan-runtime::IndirectCommandBufferProblem::Range Range { location: u64, length: u64, size: u64 } variant metal2vulkan-runtime::IndirectCommandBufferProblem::RenderState RenderState +variant metal2vulkan-runtime::IndirectComputeSet::ClearBarrier ClearBarrier +variant metal2vulkan-runtime::IndirectComputeSet::ConcurrentDispatchThreadgroups ConcurrentDispatchThreadgroups { threadgroups: Size, threads_per_threadgroup: Size } +variant metal2vulkan-runtime::IndirectComputeSet::ConcurrentDispatchThreads ConcurrentDispatchThreads { threads: Size, threads_per_threadgroup: Size } +variant metal2vulkan-runtime::IndirectComputeSet::Reset Reset +variant metal2vulkan-runtime::IndirectComputeSet::SetBarrier SetBarrier +variant metal2vulkan-runtime::IndirectComputeSet::SetComputePipelineState SetComputePipelineState(&'a ComputePipelineState) +variant metal2vulkan-runtime::IndirectComputeSet::SetImageblockSize SetImageblockSize { width: u64, height: u64 } +variant metal2vulkan-runtime::IndirectComputeSet::SetKernelBuffer SetKernelBuffer { buffer: Option<&'a Buffer>, offset: u64, index: u32 } +variant metal2vulkan-runtime::IndirectComputeSet::SetKernelBufferWithStride SetKernelBufferWithStride { buffer: Option<&'a Buffer>, offset: u64, stride: u64, index: u32 } +variant metal2vulkan-runtime::IndirectComputeSet::SetStageInRegion SetStageInRegion(Region) +variant metal2vulkan-runtime::IndirectComputeSet::SetThreadgroupMemoryLength SetThreadgroupMemoryLength { length: u32, index: u32 } variant metal2vulkan-runtime::IndirectDispatch::Threadgroups Threadgroups { threadgroups_per_grid: Size, threads_per_threadgroup: Size } variant metal2vulkan-runtime::IndirectDispatch::Threads Threads { threads_per_grid: Size, threads_per_threadgroup: Size } variant metal2vulkan-runtime::IndirectDraw::IndexedPatches IndexedPatches { control_points: u64, patch_start: u64, patch_count: u64, patch_index_buffer: Option, control_point_index_buffer: Option, instance_count: u64, base_instance: u64, factor_buffer: GpuAddress, factor_instance_stride: u64 } @@ -12582,6 +12600,28 @@ variant metal2vulkan-runtime::IndirectDraw::MeshThreadgroups MeshThreadgroups { variant metal2vulkan-runtime::IndirectDraw::MeshThreads MeshThreads { threads_per_grid: Size, threads_per_object_threadgroup: Size, threads_per_mesh_threadgroup: Size } variant metal2vulkan-runtime::IndirectDraw::Patches Patches { control_points: u64, patch_start: u64, patch_count: u64, patch_index_buffer: Option, instance_count: u64, base_instance: u64, factor_buffer: GpuAddress, factor_instance_stride: u64 } variant metal2vulkan-runtime::IndirectDraw::Primitives Primitives { primitive_type: DrawPrimitiveType, vertex_start: u64, vertex_count: u64, instance_count: u64, base_instance: u64 } +variant metal2vulkan-runtime::IndirectRenderSet::ClearBarrier ClearBarrier +variant metal2vulkan-runtime::IndirectRenderSet::DrawIndexedPatches DrawIndexedPatches { number_of_patch_control_points: u64, patch_start: u64, patch_count: u64, patch_index_buffer: Option<&'a Buffer>, patch_index_buffer_offset: u64, control_point_index_buffer: Option<&'a Buffer>, control_point_index_buffer_offset: u64, instance_count: u64, base_instance: u64, tessellation_factor_buffer: &'a Buffer, tessellation_factor_buffer_offset: u64, tessellation_factor_buffer_instance_stride: u64 } +variant metal2vulkan-runtime::IndirectRenderSet::DrawIndexedPrimitives DrawIndexedPrimitives { primitive_type: DrawPrimitiveType, index_count: u64, index_type: IndexType, index_buffer: &'a Buffer, index_buffer_offset: u64, instance_count: u64, base_vertex: i64, base_instance: u64 } +variant metal2vulkan-runtime::IndirectRenderSet::DrawMeshThreadgroups DrawMeshThreadgroups { threadgroups_per_grid: Size, threads_per_object_threadgroup: Size, threads_per_mesh_threadgroup: Size } +variant metal2vulkan-runtime::IndirectRenderSet::DrawMeshThreads DrawMeshThreads { threads_per_grid: Size, threads_per_object_threadgroup: Size, threads_per_mesh_threadgroup: Size } +variant metal2vulkan-runtime::IndirectRenderSet::DrawPatches DrawPatches { number_of_patch_control_points: u64, patch_start: u64, patch_count: u64, patch_index_buffer: Option<&'a Buffer>, patch_index_buffer_offset: u64, instance_count: u64, base_instance: u64, tessellation_factor_buffer: &'a Buffer, tessellation_factor_buffer_offset: u64, tessellation_factor_buffer_instance_stride: u64 } +variant metal2vulkan-runtime::IndirectRenderSet::DrawPrimitives DrawPrimitives { primitive_type: DrawPrimitiveType, vertex_start: u64, vertex_count: u64, instance_count: u64, base_instance: u64 } +variant metal2vulkan-runtime::IndirectRenderSet::Reset Reset +variant metal2vulkan-runtime::IndirectRenderSet::SetBarrier SetBarrier +variant metal2vulkan-runtime::IndirectRenderSet::SetCullMode SetCullMode(CullMode) +variant metal2vulkan-runtime::IndirectRenderSet::SetDepthBias SetDepthBias { depth_bias: f32, slope_scale: f32, clamp: f32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetDepthClipMode SetDepthClipMode(DepthClipMode) +variant metal2vulkan-runtime::IndirectRenderSet::SetDepthStencilState SetDepthStencilState(Option<&'a DepthStencilState>) +variant metal2vulkan-runtime::IndirectRenderSet::SetFragmentBuffer SetFragmentBuffer { buffer: Option<&'a Buffer>, offset: u64, index: u32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetFrontFacingWinding SetFrontFacingWinding(Winding) +variant metal2vulkan-runtime::IndirectRenderSet::SetMeshBuffer SetMeshBuffer { buffer: Option<&'a Buffer>, offset: u64, index: u32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetObjectBuffer SetObjectBuffer { buffer: Option<&'a Buffer>, offset: u64, index: u32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetObjectThreadgroupMemoryLength SetObjectThreadgroupMemoryLength { length: u32, index: u32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetRenderPipelineState SetRenderPipelineState(&'a RenderPipelineState) +variant metal2vulkan-runtime::IndirectRenderSet::SetTriangleFillMode SetTriangleFillMode(TriangleFillMode) +variant metal2vulkan-runtime::IndirectRenderSet::SetVertexBuffer SetVertexBuffer { buffer: Option<&'a Buffer>, offset: u64, index: u32 } +variant metal2vulkan-runtime::IndirectRenderSet::SetVertexBufferWithStride SetVertexBufferWithStride { buffer: Option<&'a Buffer>, offset: u64, stride: u64, index: u32 } variant metal2vulkan-runtime::IndirectRule::CommandRenderStates CommandRenderStates variant metal2vulkan-runtime::IndirectRule::FamilyRow FamilyRow variant metal2vulkan-runtime::IndirectRule::MemorylessReadPastPass MemorylessReadPastPass diff --git a/docs/API.md b/docs/API.md index aec30d99f..f9ae17f9a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -5054,6 +5054,21 @@ each item named states the rule in full. **Indirect command buffers** +- **A command's setters in one call** (`IndirectRenderCommand::set_each`, + `IndirectComputeCommand::set_each`, no Metal counterpart): an `IndirectRenderSet` or + `IndirectComputeSet` names one setter call, its fields the setter's arguments, and each does + what that setter does, refusals included. The first refused stops the rest and answers its place + among them with the setter's error; the calls before it stay made and those after it are not + taken, as one setter call at a time would leave the command. + `IndirectCommandBuffer::set_render_command` and `set_compute_command` (no Metal counterpart: + `indirectRenderCommandAtIndex:` or `indirectComputeCommandAtIndex:`, then the setters) take the + command's index with the sets, with no command handle; the refusals of the index (past the + size) and of the kind (a compute command of a render buffer) answer at place 0, before any + setter. A setter's write that adds nothing to what the records may hold, on a buffer the GPU + may not have written, takes no lock: a buffer slot, a draw, dispatch or barrier the CPU's + writes added already, and a pipeline state the command names already, which is not held + either. Each command's first set of a state, and every set after a device recovery carried + the buffer, takes the lock and checks the state. - **A command's read-back answers Metal's private getters**: `command_type`, the buffer addresses and attribute strides, as Metal's `get*` getters answer them. `IndirectComputeCommand` and `IndirectRenderCommand` read one field of their record alone (`command_type`, `has_barrier`, diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 9fa5e6221..0cc69ef9c 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -370,7 +370,7 @@ Columns: | `Mipmap` | A `generate_mipmaps` Metal stops | every device (a public call's own check of its arguments) | `delegated`: MipmapProblem, section 9 | 1: `crates/metal2vulkan-runtime/src/blit_texture.rs (mipmaps)` | `-` | | `BufferTexture` | A texture over a buffer plain Metal itself refuses | every device (a public call's own check of its arguments) | `delegated`: BufferTextureProblem, section 9 | 3: `crates/metal2vulkan-runtime/src/buffer_texture.rs (refusal_error, 2 sites)`, `crates/metal2vulkan-runtime/src/capabilities.rs (linear_alignment_rule)` | `-` | | `RenderPass` | A render pass descriptor, or a render encoder's late store action, Metal stops | every device (a public call's own check of its arguments) | `delegated`: RenderPassProblem, section 9 | 15: `crates/metal2vulkan-runtime/src/rate_mapped_pass.rs (check_descriptor)`, `crates/metal2vulkan-runtime/src/rate_mapped_pass.rs (of)` (+8) | `-` | -| `Draw` | A draw, or render encoder state, Metal stops | every device (a public call's own check of its arguments) | `delegated`: DrawProblem, section 9 | 3: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives)` (+1) | `-` | +| `Draw` | A draw, or render encoder state, Metal stops | every device (a public call's own check of its arguments) | `delegated`: DrawProblem, section 9 | 3: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives_in)` (+1) | `-` | | `PointSizeTopology` | `new_render_pipeline_state` | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max and an M4: Metal's pipeline creation answers an error, with and without API validation | 1: `crates/metal2vulkan-runtime/src/pipeline/render.rs (create_checked)` | `-` | | `IndirectScaledTessellation` | `new_render_pipeline_state` | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: plain Metal ends its process, the API validation layer off | 1: `crates/metal2vulkan-runtime/src/pipeline/render.rs (create_reflecting)` | `-` | | `CompiledKernel` | `new_compute_pipeline_state` | every device (a public call's own check of its arguments) | `ours`: this crate's already-compiled stage: Metal's descriptor takes a function | 1: `crates/metal2vulkan-runtime/src/pipeline/mod.rs (of_kernel)` | `-` | @@ -515,7 +515,7 @@ Columns: | `DrawProblem::ViewMappings` | `set_vertex_amplification_count` | every device (a public call's own check of its arguments) | `ours`: this crate's slice of view mappings: Metal's call takes a pointer, and reading the count would pass the memory the caller handed over | 1: `crates/metal2vulkan-runtime/src/amplification.rs (new)` | `-` | | `DrawProblem::PatchIndexBufferRange` | A direct patch draw's patch indices run past its patch index buffer | every device (a public call's own check of its arguments) | `metal-takes`: an M4: plain Metal reads past the patch index buffer rather than refusing, probe case `pib_short`: undefined | 2: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`, `crates/metal2vulkan-runtime/src/render.rs (check_patch_indices)` | `-` | | `DrawProblem::ControlPointIndexBufferRange` | A direct indexed patch draw's control-point indices run past its control-point index buffer | every device (a public call's own check of its arguments) | `metal-takes`: an M4: plain Metal reads past the control-point index buffer rather than refusing, probe cases `cpi_short` and `cpi_start1_count2`: undefined | 1: `crates/metal2vulkan-runtime/src/render.rs (check_control_point_indices)` | `-` | -| `DrawProblem::IndexBufferRange` | An indirect render command's indexed draw whose `count` indices from `offset` (rounded down to a multiple of the index size, as Metal reads it)... | every device (a public call's own check of its arguments) | `metal-takes`: an Apple M3 Max, macOS 27.0.1, probe `refusals_without_a_named_device` shape `idxc-past`, four fresh processes with one reading: plain Metal completes an indirect indexed draw of 12 indices from byte 480 of a 512-byte index buffer, with no error, and its vertex function is given the eight indices inside the buffer and then the four words that follow it in the heap, each of which holds its own word number -- so the fetch read the following memory by name, where the same draw made directly on the encoder was given index 0 at those four positions, `idxv-past` | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives)` | `-` | +| `DrawProblem::IndexBufferRange` | An indirect render command's indexed draw whose `count` indices from `offset` (rounded down to a multiple of the index size, as Metal reads it)... | every device (a public call's own check of its arguments) | `metal-takes`: an Apple M3 Max, macOS 27.0.1, probe `refusals_without_a_named_device` shape `idxc-past`, four fresh processes with one reading: plain Metal completes an indirect indexed draw of 12 indices from byte 480 of a 512-byte index buffer, with no error, and its vertex function is given the eight indices inside the buffer and then the four words that follow it in the heap, each of which holds its own word number -- so the fetch read the following memory by name, where the same draw made directly on the encoder was given index 0 at those four positions, `idxv-past` | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives_in)` | `-` | | `RenderStateProblem::Count` | A count of viewports outside 1 to 16, or of no scissor rectangle (more than 16 scissor rectangles are taken, as plain Metal takes them) | every device (a public call's own check of its arguments) | `unknown`: plain Metal's answer for a viewport count outside 1 to 16, and for no scissor rectangle, is unmeasured; the more than 16 scissor rectangles it takes are not refused | 2: `crates/metal2vulkan-runtime/src/render_state.rs (check_count)`, `crates/metal2vulkan-runtime/src/render_state.rs (scissor_rects)` | `-` | | `RenderStateProblem::ViewportNotFinite` | A viewport field that is NaN or infinite, but for a NaN origin x, which draws nothing, as plain Metal's | every device (a public call's own check of its arguments) | `unknown`: plain Metal's answer for a viewport field that is NaN or infinite is measured only for a NaN origin x, which draws nothing and is not refused | 1: `crates/metal2vulkan-runtime/src/render_state.rs (set_viewports)` | `-` | | `VisibilityProblem::NoBuffer` | The pass has no visibility result buffer | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal's process ends, SIGSEGV, at `setVisibilityResultMode:offset:` in a pass with no visibility result buffer | 1: `crates/metal2vulkan-runtime/src/visibility.rs (check_mode)` | `-` | @@ -534,13 +534,13 @@ Columns: | `FunctionTableProblem::StageWithoutFunction` | A render pipeline state's `function_handle` or `new_visible_function_table` for `stage`, which is not one stage | every device (a public call's own check of its arguments) | `metal-measured`: an M4, macOS 27.0.1: plain Metal answers the handle nil and the table of more than one stage nil, and faults on a table of no stage | 1: `crates/metal2vulkan-runtime/src/function_table.rs (not_one_stage)` | `-` | | `IndirectCommandBufferProblem::NoCommandTypes` | A call on a buffer of no command types that plain Metal's process does not survive | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1, build 26A434, the validation layer off, one shape per process, three processes a shape, each beside the same call on a buffer of the matching command type, `probes/icb_refusals_that_metal_takes.m`. Plain Metal ends the process — SIGBUS, SIGSEGV in some processes of the same shape — at `setRenderPipelineState:`, `setVertexBuffer:offset:atIndex:`, `drawPrimitives:`, `drawIndexedPrimitives:`, `setComputePipelineState:`, `concurrentDispatchThreadgroups:`, `concurrentDispatchThreads:`, a compute command's `reset`, and `resetWithRange:` of a non-empty range, where the buffer of the matching type takes each. It **takes**, and the runtime takes with it, `indirectRenderCommandAtIndex:` and `indirectComputeCommandAtIndex:` — an object, not nil — a render command's `reset`, `setFragmentBuffer:`, `setBarrier` and `clearBarrier`, `setKernelBuffer:` plain and with an attribute stride, `setThreadgroupMemoryLength:atIndex:`, `setStageInRegion:`, `setImageblockWidth:height:`, `resetWithRange:` of an empty range, the execution in a pass which paints nothing, `optimizeIndirectCommandBuffer:`, the blit copy in either direction, and the argument-buffer encoding, whose word is the buffer's own place among the device's indirect command buffers, assigned in creation order and decided by no command type: two buffers encoded side by side take two different IDs whichever types they hold, and the earlier reading that a buffer of no types encodes a `Draw` buffer's bytes held only because each shape made one buffer in its own process. The runtime takes both of those calls: the encoding writes that ID, and the copy beside a buffer of a disagreeing type completes and moves nothing, which is what Metal moves in the one direction a public call can read. The read-back is a runtime extension and has no Metal call to measure | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_command_types)` | `-` | | `IndirectCommandBufferProblem::Range` | A range of commands not inside the buffer | every device (a public call's own check of its arguments) | `unknown`: plain Metal's answer for a reset or read-back range that is not inside the buffer is unmeasured | 2: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (command_range)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (inside)` | `-` | -| `IndirectCommandBufferProblem::InheritedPipelineState` | A pipeline state set on a command of a buffer that inherits it (this ends Metal's process) | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal ends the process, SIGBUS, at `setRenderPipelineState:` on a command of a buffer whose `inheritPipelineState` is set, where the same setter on a buffer that does not inherit it runs the draw | 2: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_compute_pipeline_state)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state)` | `-` | -| `IndirectCommandBufferProblem::PipelineWithoutSupport` | A pipeline state made without `supportIndirectCommandBuffers`, set on a command | every device (a public call's own check of its arguments) | `metal-takes`: an Apple M3 Max, macOS 27.0.1, build 26A434, the validation layer off, one shape per process, three processes a shape, `probes/icb_refusals_that_metal_takes.m`. The two kinds do not answer alike. `setComputePipelineState:` with a state made without the flag ends the process, SIGSEGV, where the same call with the flag set dispatches and the kernel's marker words read back: that arm is a refusal of Metal's own. `setRenderPipelineState:` and `drawPrimitives:` both return, and the command buffer then fails at execution with status 5 and `Caused GPU Address Fault Error 0000000b:kIOGPUCommandBufferCallbackErrorPageFault`, leaving the target at its clear value, where the same pipeline with the flag set paints; a plainly valid command buffer submitted after the fault completes and paints, so the fault does not poison the queue. That arm stands on undefined behaviour — a GPU address fault is not an answer — and so reaches past Metal, and the refusal is kept there deliberately: a typed `Invalid` at the setter is the useful answer where Metal's own is a page fault at execution | 3: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_compute_pipeline_state)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state, 2 sites)` | `-` | +| `IndirectCommandBufferProblem::InheritedPipelineState` | A pipeline state set on a command of a buffer that inherits it (this ends Metal's process) | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal ends the process, SIGBUS, at `setRenderPipelineState:` on a command of a buffer whose `inheritPipelineState` is set, where the same setter on a buffer that does not inherit it runs the draw | 2: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in, 2 sites)` | `-` | +| `IndirectCommandBufferProblem::PipelineWithoutSupport` | A pipeline state made without `supportIndirectCommandBuffers`, set on a command | every device (a public call's own check of its arguments) | `metal-takes`: an Apple M3 Max, macOS 27.0.1, build 26A434, the validation layer off, one shape per process, three processes a shape, `probes/icb_refusals_that_metal_takes.m`. The two kinds do not answer alike. `setComputePipelineState:` with a state made without the flag ends the process, SIGSEGV, where the same call with the flag set dispatches and the kernel's marker words read back: that arm is a refusal of Metal's own. `setRenderPipelineState:` and `drawPrimitives:` both return, and the command buffer then fails at execution with status 5 and `Caused GPU Address Fault Error 0000000b:kIOGPUCommandBufferCallbackErrorPageFault`, leaving the target at its clear value, where the same pipeline with the flag set paints; a plainly valid command buffer submitted after the fault completes and paints, so the fault does not poison the queue. That arm stands on undefined behaviour — a GPU address fault is not an answer — and so reaches past Metal, and the refusal is kept there deliberately: a typed `Invalid` at the setter is the useful answer where Metal's own is a page fault at execution | 3: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in, 3 sites)` | `-` | | `IndirectCommandBufferProblem::CommandType` | A draw of a kind (`DRAW_PATCHES`, `DRAW_INDEXED_PATCHES`, a mesh kind, or `DRAW` and `DRAW_INDEXED` on a buffer of mesh draws, a probe) whose... | every device (a public call's own check of its arguments) | `metal-takes`: an Apple M3 Max, macOS 27.0.1, build 26A434, the validation layer off, one shape per process, `probes/icb_refusals_that_metal_takes.m`. Plain Metal ends the process, SIGBUS, at a patch draw on a buffer of `DRAW` alone, at a mesh pipeline's `setRenderPipelineState:` on such a buffer, and at a draw of primitives on a buffer of `DRAW_MESH_THREADGROUPS` alone, three processes each | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)` | `-` | -| `IndirectCommandBufferProblem::CommandKind` | A compute command of a buffer whose commands are render ones, or a setter plain Metal does not ignore on a render command of a buffer whose... | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: plain Metal's process ends — SIGBUS, SIGBUS and SIGSEGV — at `indirectComputeCommandAtIndex:` on a buffer of draws and at a render command's setters and reset on one of dispatches, the API validation layer off | 7: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_getter)` (+5) | `-` | +| `IndirectCommandBufferProblem::CommandKind` | A compute command of a buffer whose commands are render ones, or a setter plain Metal does not ignore on a render command of a buffer whose... | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: plain Metal's process ends — SIGBUS, SIGBUS and SIGSEGV — at `indirectComputeCommandAtIndex:` on a buffer of draws and at a render command's setters and reset on one of dispatches, the API validation layer off | 8: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (buffer_in)`, `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)` (+6) | `-` | | `IndirectCommandBufferProblem::RenderState` | A render state set on a render command (`set_depth_stencil_state`, `set_depth_bias`, `set_depth_clip_mode`, `set_cull_mode`,... | every device (a public call's own check of its arguments) | `metal-measured`: an M4, macOS 27.0.1: plain Metal raises `APINotSupported` at each setter and its process ends, whether the buffer inherits the state or not | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (render_state)` | `-` | | `IndirectCommandBufferProblem::ObjectThreadgroupMemory` | An object threadgroup length set on a render command of a buffer whose command types hold no mesh draw (`set_object_threadgroup_memory_length`) | every device (a public call's own check of its arguments) | `metal-measured`: an M4: Metal's process ends at `set_object_threadgroup_memory_length` on a buffer of no mesh draw, with and without its validation | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_object_threadgroup_memory_length)` | `-` | -| `IndirectCommandBufferProblem::PipelineKind` | A render pipeline state set on a command of a buffer whose command types hold no draw of its kind (`set_render_pipeline_state`) | every device (a public call's own check of its arguments) | `metal-measured`: an M4: Metal's process ends in `set_render_pipeline_state` on a buffer of no draw of the state's kind, with and without its validation | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state)` | `-` | +| `IndirectCommandBufferProblem::PipelineKind` | A render pipeline state set on a command of a buffer whose command types hold no draw of its kind (`set_render_pipeline_state`) | every device (a public call's own check of its arguments) | `metal-measured`: an M4: Metal's process ends in `set_render_pipeline_state` on a buffer of no draw of the state's kind, with and without its validation | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in)` | `-` | | `RateMapProblem::ScreenSize` | A screen width or height past 32768 | every device (a public call's own check of its arguments) | `unknown`: plain Metal's answer for a screen side past 32768 is unmeasured; the side of 0 its validation stops is compiled by plain Metal and is not refused | 1: `crates/metal2vulkan-runtime/src/rate_map.rs (validate)` | `-` | | `RateMapProblem::LayerCount` | A layer count past 2 ("layerCount (3) must be >0 and <=2") | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal's process ends, SIGBUS, at `newRasterizationRateMapWithDescriptor:` of 3 and of 4 layers, where 1 and 2 make the map | 2: `crates/metal2vulkan-runtime/src/rate_map.rs (reset)`, `crates/metal2vulkan-runtime/src/rate_map.rs (validate)` | `-` | | `RateMapProblem::NoLayers` | A coordinate map of a map of no layers, which ends Metal's process | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal's process ends, SIGSEGV, at a coordinate map of a map of no layers, which it makes | 2: `crates/metal2vulkan-runtime/src/rate_map.rs (map_physical_to_screen_coordinates)`, `crates/metal2vulkan-runtime/src/rate_map.rs (map_screen_to_physical_coordinates)` | `-` | @@ -861,7 +861,7 @@ Each refusal's sites, with the function that builds it. - `invalid::Mipmap`: `crates/metal2vulkan-runtime/src/blit_texture.rs (mipmaps)` - `invalid::BufferTexture`: `crates/metal2vulkan-runtime/src/buffer_texture.rs (refusal_error, 2 sites)`; `crates/metal2vulkan-runtime/src/capabilities.rs (linear_alignment_rule)` - `invalid::RenderPass`: `crates/metal2vulkan-runtime/src/rate_mapped_pass.rs (check_descriptor)`; `crates/metal2vulkan-runtime/src/rate_mapped_pass.rs (of)`; `crates/metal2vulkan-runtime/src/render.rs (store_problem)`; `crates/metal2vulkan-runtime/src/render/late_resolve.rs (problem)`; `crates/metal2vulkan-runtime/src/render_pass.rs (2 sites)`; `crates/metal2vulkan-runtime/src/render_pass.rs (check_target)`; `crates/metal2vulkan-runtime/src/render_pass.rs (depth_stencil, 2 sites)`; `crates/metal2vulkan-runtime/src/render_pass.rs (layout, 4 sites)`; `crates/metal2vulkan-runtime/src/resolve.rs (problem)`; `crates/metal2vulkan-runtime/src/sample_positions.rs (problem)` -- `invalid::Draw`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives)`; `crates/metal2vulkan-runtime/src/render.rs (draw_problem)` +- `invalid::Draw`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives_in)`; `crates/metal2vulkan-runtime/src/render.rs (draw_problem)` - `invalid::PointSizeTopology`: `crates/metal2vulkan-runtime/src/pipeline/render.rs (create_checked)` - `invalid::IndirectScaledTessellation`: `crates/metal2vulkan-runtime/src/pipeline/render.rs (create_reflecting)` - `invalid::CompiledKernel`: `crates/metal2vulkan-runtime/src/pipeline/mod.rs (of_kernel)` @@ -984,7 +984,7 @@ Each refusal's sites, with the function that builds it. - `subproblem::DrawProblem::ViewMappings`: `crates/metal2vulkan-runtime/src/amplification.rs (new)` - `subproblem::DrawProblem::PatchIndexBufferRange`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_patches)`; `crates/metal2vulkan-runtime/src/render.rs (check_patch_indices)` - `subproblem::DrawProblem::ControlPointIndexBufferRange`: `crates/metal2vulkan-runtime/src/render.rs (check_control_point_indices)` -- `subproblem::DrawProblem::IndexBufferRange`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives)` +- `subproblem::DrawProblem::IndexBufferRange`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (draw_indexed_primitives_in)` - `subproblem::RenderStateProblem::Count`: `crates/metal2vulkan-runtime/src/render_state.rs (check_count)`; `crates/metal2vulkan-runtime/src/render_state.rs (scissor_rects)` - `subproblem::RenderStateProblem::ViewportNotFinite`: `crates/metal2vulkan-runtime/src/render_state.rs (set_viewports)` - `subproblem::VisibilityProblem::NoBuffer`: `crates/metal2vulkan-runtime/src/visibility.rs (check_mode)` @@ -1003,13 +1003,13 @@ Each refusal's sites, with the function that builds it. - `subproblem::FunctionTableProblem::StageWithoutFunction`: `crates/metal2vulkan-runtime/src/function_table.rs (not_one_stage)` - `subproblem::IndirectCommandBufferProblem::NoCommandTypes`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_command_types)` - `subproblem::IndirectCommandBufferProblem::Range`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (command_range)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (inside)` -- `subproblem::IndirectCommandBufferProblem::InheritedPipelineState`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_compute_pipeline_state)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state)` -- `subproblem::IndirectCommandBufferProblem::PipelineWithoutSupport`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_compute_pipeline_state)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state, 2 sites)` +- `subproblem::IndirectCommandBufferProblem::InheritedPipelineState`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in, 2 sites)` +- `subproblem::IndirectCommandBufferProblem::PipelineWithoutSupport`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in, 3 sites)` - `subproblem::IndirectCommandBufferProblem::CommandType`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)` -- `subproblem::IndirectCommandBufferProblem::CommandKind`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_getter)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_kind)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (edit)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (reset)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_buffer)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_object_threadgroup_memory_length)` +- `subproblem::IndirectCommandBufferProblem::CommandKind`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (buffer_in)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_draw)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_getter)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (check_kind)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (edit_in)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (reset)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_object_threadgroup_memory_length)` - `subproblem::IndirectCommandBufferProblem::RenderState`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (render_state)` - `subproblem::IndirectCommandBufferProblem::ObjectThreadgroupMemory`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_object_threadgroup_memory_length)` -- `subproblem::IndirectCommandBufferProblem::PipelineKind`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (set_render_pipeline_state)` +- `subproblem::IndirectCommandBufferProblem::PipelineKind`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs (pipeline_in)` - `subproblem::RateMapProblem::ScreenSize`: `crates/metal2vulkan-runtime/src/rate_map.rs (validate)` - `subproblem::RateMapProblem::LayerCount`: `crates/metal2vulkan-runtime/src/rate_map.rs (reset)`; `crates/metal2vulkan-runtime/src/rate_map.rs (validate)` - `subproblem::RateMapProblem::NoLayers`: `crates/metal2vulkan-runtime/src/rate_map.rs (map_physical_to_screen_coordinates)`; `crates/metal2vulkan-runtime/src/rate_map.rs (map_screen_to_physical_coordinates)`