diff --git a/crates/metal2vulkan-runtime/src/blit_indirect.rs b/crates/metal2vulkan-runtime/src/blit_indirect.rs index 724e58b95..791d832ea 100644 --- a/crates/metal2vulkan-runtime/src/blit_indirect.rs +++ b/crates/metal2vulkan-runtime/src/blit_indirect.rs @@ -186,7 +186,10 @@ impl BlitCommandEncoder { /// Carry the slots past the records' of `source`'s commands `from` into `destination`'s from /// command `to`, as the copy carries the records: every slot of a stage both hold, and the - /// destination's slots past the source's cleared, which the source holds unset. + /// destination's slots past the source's cleared, which the source holds unset; and the + /// threadgroup lengths and words past the records', lengths 36 and 37 cleared where the + /// destination's commands hold pipeline words, as Metal's copy clears them + /// (`Overflow::copy_clears`). fn copy_overflow( &mut self, source: &IndirectCommandBuffer, @@ -214,7 +217,7 @@ impl BlitCommandEncoder { })?; self.core().retain(held.storage().clone()); self.core().retain(into.storage().clone()); - return Ok(()); + return self.clear_runs(into, to..to + count); } Some(held) => held, }; @@ -260,6 +263,45 @@ impl BlitCommandEncoder { Ok(()) } + /// Clear the runs of commands `range` of `overflow` a copy into them clears after it carried + /// the source's blocks (`Overflow::copy_clears`), by one copy from zeroed bytes of the upload + /// arena: a fill a run costs the device a command each. + fn clear_runs(&mut self, overflow: &Overflow, range: Range) -> Result<(), Error> { + let runs = overflow.copy_clears(range.clone()); + let Some(&(_, size)) = runs.first() else { + return Ok(()); + }; + let device = self.core().device().clone(); + let zeros = vec![0u8; size as usize]; + let upload = self + .core() + .command_buffer() + .state() + .uploads + .upload(&device, &zeros, size)?; + let storage = overflow.storage(); + let regions: Vec = runs + .iter() + .map(|&(at, size)| vk::BufferCopy { + src_offset: upload.offset(), + dst_offset: storage.at(at).1, + size, + }) + .collect(); + let (from, (raw, _)) = (upload.raw(), storage.at(0)); + self.command( + &[writes(storage, overflow.bytes(range))], + |device, command_buffer| { + // SAFETY: the encoder's command buffer is recording; the overflow is live and + // retained until it completes, and the arena's region until the command buffer + // completes; each region lies inside both, and none overlaps another. + unsafe { device.cmd_copy_buffer(command_buffer, from, raw, ®ions) }; + }, + )?; + self.core().retain(storage.clone()); + Ok(()) + } + /// Clear `bytes` of `overflow`, the slots of commands a reset or a copy clears. fn clear_overflow(&mut self, overflow: &Overflow, bytes: Range) -> Result<(), Error> { if bytes.is_empty() { @@ -270,7 +312,8 @@ impl BlitCommandEncoder { let size = bytes.end - bytes.start; self.command(&[writes(storage, bytes)], |device, command_buffer| { // SAFETY: the encoder's command buffer is recording; the overflow is live and - // retained until it completes; the range lies inside it, a multiple of 16 bytes. + // retained until it completes; the range lies inside it, its offset and size multiples + // of 4 bytes. unsafe { device.cmd_fill_buffer(command_buffer, raw, at, size, 0) }; })?; self.core().retain(storage.clone()); diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index 5ec5bb754..0d659a56f 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -576,6 +576,7 @@ impl Device { storage, past: layout.past, lengths: layout.lengths, + pipeline_words: layout.pipeline_words, commands: size, }) } @@ -968,11 +969,14 @@ struct IcbShared { /// The buffer slots past the record's, where the descriptor counts more, and a compute /// buffer's threadgroup lengths 31 to 37 ([`Overflow`]). overflow: Option, - /// The threadgroup lengths set past the last command's 38 ([`LENGTH_STRIDE`]), by their - /// place past them, none 0. Metal keeps them in its storage's slack for its getter alone (an - /// M3 Max, 3 commands: the last command's lengths at 100, 300 and 1000 read back as set, - /// changing no other command's state); no command's copy or reset reaches them. Past about - /// 4000 places its setter ends the process (`SIGSEGV`, `SIGBUS`); the runtime keeps them all. + /// The threadgroup lengths set past the last command's 38 ([`LENGTH_STRIDE`]) and past the + /// commands' words there ([`Overflow::tail_at`]), by their place past the last command's 38, + /// none 0. Metal keeps them in its storage's slack for its getter alone (an M3 Max, 3 + /// commands: the last command's lengths at 100, 300 and 1000 read back as set, changing no + /// other command's state); no command's reset reaches them, and a GPU copy reaches the + /// first two alone, writing there a value of its own, which the runtime does not (it keeps + /// them). Past about 4000 places Metal's setter ends the process (`SIGSEGV`, `SIGBUS`); the + /// runtime keeps them all. tail: Mutex>, /// The command buffers that may write its records on the GPU ([`GpuWrite`]): how many have /// not yet completed, and the stamp of the last one's completion. @@ -988,32 +992,40 @@ struct IcbShared { /// The buffer slots of each command past the 32 of a stage its record holds, below the /// descriptor's count of the stage, and a compute command's threadgroup lengths 31 to 37, past -/// the 31 its record holds, in a host-visible buffer of their own outside the Metal address -/// space. A command's block holds its buffer slots, 16 bytes a slot, the address word and the -/// stride word as a record's slot holds them (both 0 where none is set), its stages in -/// [`OVERFLOW_STAGES`]' order, then, in a buffer of compute commands, its lengths 31 to 37 -/// ([`LENGTHS_BYTES`]); each command's block follows the one before. Metal keeps such a slot or +/// the 31 its record holds, and its words past the last command's lengths, in a host-visible +/// buffer of their own outside the Metal address space. A command's block holds its buffer +/// slots, 16 bytes a slot, the address word and the stride word as a record's slot holds them +/// (both 0 where none is set), its stages in [`OVERFLOW_STAGES`]' order, then, in a buffer of +/// compute commands, its lengths 31 to 37 ([`LENGTHS_BYTES`]), its pipeline words where the +/// buffer does not inherit its pipeline state ([`PIPELINE_WORDS`]) and its dispatch words +/// ([`DISPATCH_WORDS`]); each command's block follows the one before. Metal keeps such a slot or /// length for its getters alone (no function binds past index 30), its storage growing with the /// buffer count; its getters answer every slot below the count and every length, its GPU copy -/// carries them, and its resets clear them. +/// carries them (but lengths 36 and 37 where the commands hold pipeline words, which it clears), +/// its GPU reset clears them, and its CPU resets clear them but the dispatch words. pub(super) struct Overflow { /// Made zeroed. storage: Buffer, /// The slots past the record's of each of [`OVERFLOW_STAGES`]. past: [u32; 5], - /// Whether each command's block holds its threadgroup lengths 31 to 37: the buffer holds - /// compute commands. + /// Whether each command's block holds its threadgroup lengths 31 to 37 and its dispatch + /// words: the buffer holds compute commands. lengths: bool, + /// Whether each command's block holds its pipeline words: the buffer holds compute commands + /// and does not inherit its pipeline state. + pipeline_words: bool, /// The commands whose blocks it holds: the buffer's size. commands: u32, } /// What [`Overflow`] holds of each command, of a descriptor: the slots past the record's of -/// each stage, and whether it holds the threadgroup lengths 31 to 37. +/// each stage, whether it holds the threadgroup lengths 31 to 37 and the dispatch words, and +/// whether it holds the pipeline words. #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct OverflowLayout { past: [u32; 5], lengths: bool, + pipeline_words: bool, } /// How many threadgroup lengths Metal keeps a compute command, at `command * 38 + index` of one @@ -1027,6 +1039,24 @@ const LENGTH_STRIDE: u32 = 38; /// padded to 8 bytes. const LENGTHS_BYTES: u64 = 32; +/// The bytes of a command's threadgroup lengths 31 to 35 in its [`Overflow`] block, which +/// Metal's GPU copy carries whatever the descriptor; lengths 36 and 37 follow. +const LENGTHS_BEFORE_36: u64 = 20; + +/// The words Metal keeps a compute command past the last command's threadgroup lengths, at +/// `2 * command` of them, where the buffer does not inherit its pipeline state (an M3 Max, macOS +/// 27.0.1, buffers of 1 to 8 commands): its pipeline setter writes them, and so do its getter and +/// setter of a length there. Metal's CPU and GPU resets of the command clear them and its GPU copy +/// carries them. +const PIPELINE_WORDS: u64 = 2; + +/// The words Metal keeps a compute command past the last command's threadgroup lengths and the +/// [`PIPELINE_WORDS`], at `8 * command` of them (the probes of [`PIPELINE_WORDS`]'s): its dispatch +/// writes the threadgroup counts and size in the first six and its imageblock setter the width +/// and height in the last two, and so do its getter and setter of a length there. Metal's GPU +/// reset of the command clears them and its GPU copy carries them; its CPU resets leave them. +const DISPATCH_WORDS: u64 = 8; + /// The stages whose slots [`Overflow`] holds, in its order. const OVERFLOW_STAGES: [BufferStage; 5] = [ BufferStage::Kernel, @@ -1054,7 +1084,12 @@ impl OverflowLayout { // Any compute command may hold a length past its record: Metal's setter keeps one at // any index, whatever the count. let lengths = descriptor.command_types.intersects(COMPUTE_TYPES); - (lengths || past.iter().any(|&slots| slots > 0)).then_some(OverflowLayout { past, lengths }) + let pipeline_words = lengths && !descriptor.inherit_pipeline_state; + (lengths || past.iter().any(|&slots| slots > 0)).then_some(OverflowLayout { + past, + lengths, + pipeline_words, + }) } /// The bytes of one command's buffer slots. @@ -1062,9 +1097,30 @@ impl OverflowLayout { self.past.iter().map(|&slots| u64::from(slots)).sum::() * SLOT_BYTES } - /// The bytes of one command's block: its buffer slots, then its lengths 31 to 37. + /// The bytes of one command's pipeline words: none where the buffer holds no compute + /// commands or inherits its pipeline state. + fn pipeline_bytes(&self) -> u64 { + if self.pipeline_words { + PIPELINE_WORDS * 4 + } else { + 0 + } + } + + /// The bytes of one command's lengths 31 to 37, pipeline words and dispatch words: none where + /// the buffer holds no compute commands. + fn lengths_bytes(&self) -> u64 { + if self.lengths { + LENGTHS_BYTES + self.pipeline_bytes() + DISPATCH_WORDS * 4 + } else { + 0 + } + } + + /// The bytes of one command's block: its buffer slots, then its lengths 31 to 37, pipeline + /// words and dispatch words. fn command_bytes(&self) -> u64 { - self.slot_bytes() + if self.lengths { LENGTHS_BYTES } else { 0 } + self.slot_bytes() + self.lengths_bytes() } /// The bytes an overflow of `commands` commands holds. @@ -1079,6 +1135,7 @@ impl Overflow { OverflowLayout { past: self.past, lengths: self.lengths, + pipeline_words: self.pipeline_words, } } @@ -1097,6 +1154,56 @@ impl Overflow { }) } + /// The offset of the word `past` places past the last command's threadgroup lengths, where + /// Metal keeps a command's words there: each command's [`PIPELINE_WORDS`] first, where the + /// commands hold them, then each command's [`DISPATCH_WORDS`]. `None` past them, or where + /// the blocks hold no lengths. + fn tail_at(&self, past: u64) -> Option { + if !self.lengths { + return None; + } + let commands = u64::from(self.commands); + let pipeline = if self.pipeline_words { + PIPELINE_WORDS + } else { + 0 + }; + let (command, at) = match past.checked_sub(pipeline * commands) { + None => (past / pipeline, LENGTHS_BYTES + past % pipeline * 4), + Some(past) if past < DISPATCH_WORDS * commands => ( + past / DISPATCH_WORDS, + LENGTHS_BYTES + pipeline * 4 + past % DISPATCH_WORDS * 4, + ), + Some(_) => return None, + }; + let command = command as u32; + Some(self.bytes(command..command).start + self.layout().slot_bytes() + at) + } + + /// The bytes of command `index`'s block a CPU reset clears, as Metal's: all but its dispatch + /// words. + fn cpu_reset_bytes(&self, index: u32) -> Range { + let block = self.bytes(index..index + 1); + let kept = if self.lengths { DISPATCH_WORDS * 4 } else { 0 }; + block.start..block.end - kept + } + + /// The runs of commands `range`'s blocks a GPU copy into them clears after it carried the + /// source's blocks, as `(offset, bytes)`: lengths 36 and 37 where the commands hold + /// pipeline words, as Metal's copy clears them; none otherwise. + pub(super) fn copy_clears(&self, range: Range) -> Vec<(u64, u64)> { + if !self.pipeline_words { + return Vec::new(); + } + let lengths = self.layout().slot_bytes() + LENGTHS_BEFORE_36; + range + .map(|index| { + let at = self.bytes(index..index).start + lengths; + (at, LENGTHS_BYTES - LENGTHS_BEFORE_36) + }) + .collect() + } + /// The offset of slot `slot` (at or past the record's) of `stage` of command `index`, `None` /// past the descriptor's count. fn at(&self, index: u32, stage: BufferStage, slot: u32) -> Option { @@ -1127,12 +1234,37 @@ impl Overflow { source += u64::from(held) * SLOT_BYTES; destination += u64::from(taken) * SLOT_BYTES; } - // The threadgroup lengths 31 to 37, as Metal's copy carries a command's 38. - match (self.lengths, into.lengths) { - (true, true) => runs.copies.push((source, destination, LENGTHS_BYTES)), - (false, true) => runs.clears.push((destination, LENGTHS_BYTES)), + // The threadgroup lengths 31 to 37, pipeline words and dispatch words, as Metal's copy + // carries a command's: lengths 36 and 37 cleared where the destination's commands hold + // pipeline words. + if !into.lengths { + return runs; + } + if !self.lengths { + runs.clears + .push((destination, into.layout().lengths_bytes())); + return runs; + } + runs.copies.push((source, destination, LENGTHS_BEFORE_36)); + let (after, rest) = (LENGTHS_BEFORE_36, LENGTHS_BYTES - LENGTHS_BEFORE_36); + match into.pipeline_words { + true => runs.clears.push((destination + after, rest)), + false => runs + .copies + .push((source + after, destination + after, rest)), + } + let (source, destination) = (source + LENGTHS_BYTES, destination + LENGTHS_BYTES); + let pipeline = PIPELINE_WORDS * 4; + match (self.pipeline_words, into.pipeline_words) { + (true, true) => runs.copies.push((source, destination, pipeline)), + (false, true) => runs.clears.push((destination, pipeline)), _ => {} } + let (source, destination) = ( + source + self.layout().pipeline_bytes(), + destination + into.layout().pipeline_bytes(), + ); + runs.copies.push((source, destination, DISPATCH_WORDS * 4)); runs } @@ -1143,7 +1275,7 @@ impl Overflow { /// Whether `other` lays its commands' blocks out as this one does. pub(super) fn same_layout(&self, other: &Overflow) -> bool { - self.past == other.past && self.lengths == other.lengths + self.layout() == other.layout() } } @@ -1176,8 +1308,9 @@ impl Drop for GpuWrite { } /// Where a threadgroup length is kept (`IndirectCommandBuffer::length_place`): command and -/// index of a record, an offset of the [`Overflow`], or a place past the last command's lengths -/// (`IcbShared::tail`). +/// index of a record, an offset of the [`Overflow`] (a length 31 to 37 or a command's word past +/// the last command's lengths), or a place past the last command's lengths and the commands' +/// words there (`IcbShared::tail`). enum LengthPlace { Record(u32, u32), Overflow(u64), @@ -1606,14 +1739,21 @@ impl IndirectCommandBuffer { /// Where Metal keeps threadgroup length `slot` of command `index` of a compute buffer: at /// `index * 38 + slot` of one array of the buffer's ([`LENGTH_STRIDE`]), the length of /// command `(index * 38 + slot) / 38` at the remainder: in its record below 31, in its - /// [`Overflow`] block at 31 to 37, and past the last command's in [`IcbShared::tail`]. + /// [`Overflow`] block at 31 to 37; past the last command's, the commands' words there + /// ([`Overflow::tail_at`]), and past those in [`IcbShared::tail`]. fn length_place(&self, index: u32, slot: u32) -> Result { let stride = u64::from(LENGTH_STRIDE); let flat = u64::from(index) * stride + u64::from(slot); let (command, slot) = (flat / stride, (flat % stride) as u32); let size = u64::from(self.0.size); if command >= size { - return Ok(LengthPlace::Tail(flat - size * stride)); + let past = flat - size * stride; + let word = self + .0 + .overflow + .as_ref() + .and_then(|overflow| overflow.tail_at(past)); + return Ok(word.map_or(LengthPlace::Tail(past), LengthPlace::Overflow)); } let command = command as u32; if slot < THREADGROUP_BUFFER_ARGUMENT_COUNT { @@ -1783,24 +1923,26 @@ impl IndirectCommandBuffer { }) } - /// Clear the slots past the record's of commands `range`, as a reset does. The caller holds - /// `pipelines`. + /// Clear the slots past the record's of commands `range`, as a CPU reset does: all but + /// their dispatch words ([`Overflow::cpu_reset_bytes`]). The caller holds `pipelines`. fn clear_overflow(&self, range: Range) -> Result<(), Error> { let Some(overflow) = self.0.overflow.as_ref() else { return Ok(()); }; - let bytes = overflow.bytes(range); let contents = overflow .storage .contents() .ok_or_else(|| internal("an indirect command buffer's overflow is not mapped"))?; - // SAFETY: as `write_overflow`'s; `bytes` lies inside the overflow. - unsafe { - std::ptr::write_bytes( - contents.as_ptr().add(bytes.start as usize), - 0, - (bytes.end - bytes.start) as usize, - ); + for index in range { + let bytes = overflow.cpu_reset_bytes(index); + // SAFETY: as `write_overflow`'s; `bytes` lies inside the overflow. + unsafe { + std::ptr::write_bytes( + contents.as_ptr().add(bytes.start as usize), + 0, + (bytes.end - bytes.start) as usize, + ); + } } self.wrote(); Ok(()) @@ -3405,8 +3547,17 @@ impl IndirectComputeCommand { /// 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. Differs: 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. + /// 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.icb.set_length(self.index, index, length) } @@ -3497,10 +3648,13 @@ impl IndirectComputeCommand { /// [`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), read alone: 0 where none was set, at any index, the - /// descriptor's `max_kernel_threadgroup_memory_bind_count` or not; always some. Differs: far - /// past the last command's lengths (an M3 Max: 100000 places past command 0's of 3), Metal's - /// getter ends the process; the runtime's answers. + /// 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.icb.length(self.index, index as u32).map(Some) } diff --git a/crates/metal2vulkan-validation/runtime-sources.sha256 b/crates/metal2vulkan-validation/runtime-sources.sha256 index 9890095a2..cc44388ef 100644 --- a/crates/metal2vulkan-validation/runtime-sources.sha256 +++ b/crates/metal2vulkan-validation/runtime-sources.sha256 @@ -1 +1 @@ -2a12330e60b124ccbb27529490a7271b113a3724f35240dbb4bb429499e4e4dd +a3b8c15082102613a2b3ee8f34a2d3bfc31e56373dfc6344c9df59292d834c84 diff --git a/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs b/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs index ddf41f9e4..1850c7f80 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs @@ -32,6 +32,7 @@ use metal2vulkan_runtime::{ PurgeableState, Region, RenderPassDescriptor, RenderPipelineState, ResourceId, ResourceOptions, RuntimeGap, Size, StorageMode, StoreAction, TextureDescriptor, }; +use std::ops::Range; fn invalid(call: &'static str, problem: Problem) -> Error { Error::Invalid(Invalid::new(call, problem)) @@ -1285,7 +1286,9 @@ fn kept_lengths(icb: &IndirectCommandBuffer, range: u64) -> [Vec<(u64, u32)>; 3] /// past the count is kept as well, and one past the last command's 38 reads back as set (at /// 1000 of the last command), changing no command's record; the getter takes the index's low 32 /// bits. A command's reset, on the CPU or by a blit, clears its 38 and leaves the next command's; -/// a blit's copy of a command carries its 38. None announces anything. +/// a blit's copy of a command carries its lengths 0 to 35 (lengths 36 and 37 and the words past +/// the last command's: [`words_past_lengths_reset_and_copy_as_metal_s`]). None announces +/// anything. #[test] fn threadgroup_lengths_are_kept_where_metal_keeps_them() { notices::on_device(|device, messages| { @@ -1426,6 +1429,220 @@ fn threadgroup_lengths_are_kept_where_metal_keeps_them() { }); } +/// The places past the last command's threadgroup lengths [`words_past_lengths_reset_and_copy_as_metal_s`] +/// sets and reads. +const PAST: u64 = 60; + +/// Every threadgroup length command 0 of `icb` reads, from 0 to its commands' 38 and [`PAST`] +/// places past them. +fn flat_lengths(icb: &IndirectCommandBuffer) -> Vec { + let first = icb.indirect_compute_command_at(0).unwrap(); + (0..icb.size() * 38 + PAST) + .map(|at| first.threadgroup_memory_length(at).unwrap().unwrap()) + .collect() +} + +/// Set, as the probes set them, `base + 100 k + s` at each `s` of 0 to 37 of every command `k` +/// of `icb`, and through its last command `base + 100000 + p` at each place `p` of 0 to +/// [`PAST`] past the last command's lengths; the lengths as read back, as set. +fn fill_lengths(icb: &IndirectCommandBuffer, base: u32) -> Vec { + let size = icb.size(); + for index in 0..size { + let command = icb.indirect_compute_command_at(index).unwrap(); + for at in 0..38 { + let length = base + 100 * index as u32 + at; + command.set_threadgroup_memory_length(length, at).unwrap(); + } + } + let last = icb.indirect_compute_command_at(size - 1).unwrap(); + for past in 0..PAST as u32 { + last.set_threadgroup_memory_length(base + 100000 + past, 38 + past) + .unwrap(); + } + let lengths = flat_lengths(icb); + let commands = (size * 38) as usize; + let set = |at: usize| match at.checked_sub(commands) { + Some(past) => base + 100000 + past as u32, + None => base + 100 * (at / 38) as u32 + (at % 38) as u32, + }; + assert!( + (0..lengths.len()).all(|at| lengths[at] == set(at)), + "read back as set" + ); + lengths +} + +/// `lengths` but at the places `unknown`, which Metal's GPU copy writes with a value of its own. +fn known(mut lengths: Vec, unknown: Range) -> Vec { + lengths[unknown].fill(0); + lengths +} + +/// A length set past the last command's threadgroup lengths is reset and copied as Metal resets +/// and copies the command whose words lie there (an M3 Max, macOS 27.0.1, buffers of 1 to 8 +/// commands, each case in its own process): where the buffer does not inherit its pipeline state, +/// 2 words a command from the last command's lengths, then 8 a command; where it does, the 8 alone. +/// A command's CPU reset clears its 38 and its 2 words, leaving its 8; a blit's reset clears +/// all; a blit's copy carries its lengths 0 to 35, its 2 words and its 8, and clears its lengths +/// 36 and 37 where the buffer does not inherit its pipeline state (carrying them where it does). +/// Two places past the commands' words, which Metal's copy writes with a value of its own, are +/// not compared. None announces anything. +#[test] +fn words_past_lengths_reset_and_copy_as_metal_s() { + notices::on_device(|device, messages| { + messages.take_notices(); + let queue = device.new_command_queue().unwrap(); + let blit = |work: &dyn Fn(&mut BlitCommandEncoder)| { + let command_buffer = queue.command_buffer().unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + work(&mut blit); + blit.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.error(), None); + }; + let mut descriptor = compute_descriptor(); + descriptor.max_kernel_threadgroup_memory_bind_count = 8; + let mut inheriting = descriptor; + inheriting.inherit_pipeline_state = true; + let mut wider = descriptor; + wider.max_kernel_buffer_bind_count = 40; + // Three commands, their lengths to 114, 2 words each from 114, 8 each from 120. + let made = || icb(device, &descriptor, 3); + + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + buffer + .indirect_compute_command_at(1) + .unwrap() + .reset() + .unwrap(); + expected[38..76].fill(0); + expected[116..118].fill(0); + assert_eq!(flat_lengths(&buffer), expected, "a command's reset"); + + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + buffer.reset_with_range(0..2).unwrap(); + expected[0..76].fill(0); + expected[114..118].fill(0); + assert_eq!(flat_lengths(&buffer), expected, "a CPU reset of a range"); + + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + blit(&|blit| blit.reset_commands_in_buffer(&buffer, 1..2).unwrap()); + expected[38..76].fill(0); + expected[116..118].fill(0); + expected[128..136].fill(0); + assert_eq!( + flat_lengths(&buffer), + expected, + "a blit's reset of command 1" + ); + + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + blit(&|blit| blit.reset_commands_in_buffer(&buffer, 0..3).unwrap()); + expected[0..144].fill(0); + assert_eq!(flat_lengths(&buffer), expected, "a blit's reset of all"); + + // A copy of command 2 into command 0 of another buffer, of one descriptor and of a + // descriptor of more buffers (the runtime's answer: Metal garbles a copy between + // descriptors). + for (source_descriptor, name) in [(descriptor, "one descriptor"), (wider, "two")] { + let source = icb(device, &source_descriptor, 3); + let from = fill_lengths(&source, 100000); + let into = made(); + let mut expected = fill_lengths(&into, 300000); + blit(&|blit| { + blit.copy_indirect_command_buffer(&source, 2..3, &into, 0) + .unwrap() + }); + expected[0..36].copy_from_slice(&from[76..112]); + expected[36..38].fill(0); + expected[114..116].copy_from_slice(&from[118..120]); + expected[120..128].copy_from_slice(&from[136..144]); + assert_eq!( + known(flat_lengths(&into), 144..146), + known(expected, 144..146), + "a blit's copy of command 2 into command 0, {name}" + ); + } + + // A copy of all three into command 2 of five: lengths to 190, 2 words each from 190, 8 + // each from 200. + let source = made(); + let from = fill_lengths(&source, 100000); + let into = icb(device, &descriptor, 5); + let mut expected = fill_lengths(&into, 300000); + blit(&|blit| { + blit.copy_indirect_command_buffer(&source, 0..3, &into, 2) + .unwrap() + }); + for command in 0..3 { + let (at, to) = (command * 38, (2 + command) * 38); + expected[to..to + 36].copy_from_slice(&from[at..at + 36]); + expected[to + 36..to + 38].fill(0); + } + expected[194..200].copy_from_slice(&from[114..120]); + expected[216..240].copy_from_slice(&from[120..144]); + assert_eq!( + known(flat_lengths(&into), 240..242), + known(expected, 240..242), + "a blit's copy of all into command 2 of 5" + ); + + // Inheriting the pipeline state, four commands: their lengths to 152, 8 words each from + // 152. + let made = || icb(device, &inheriting, 4); + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + buffer + .indirect_compute_command_at(1) + .unwrap() + .reset() + .unwrap(); + expected[38..76].fill(0); + assert_eq!( + flat_lengths(&buffer), + expected, + "inheriting: a command's reset" + ); + + let buffer = made(); + let mut expected = fill_lengths(&buffer, 100000); + blit(&|blit| blit.reset_commands_in_buffer(&buffer, 1..2).unwrap()); + expected[38..76].fill(0); + expected[160..168].fill(0); + assert_eq!( + flat_lengths(&buffer), + expected, + "inheriting: a blit's reset" + ); + + let source = made(); + let from = fill_lengths(&source, 100000); + let into = made(); + let mut expected = fill_lengths(&into, 300000); + blit(&|blit| { + blit.copy_indirect_command_buffer(&source, 1..2, &into, 2) + .unwrap() + }); + expected[76..114].copy_from_slice(&from[38..76]); + expected[168..176].copy_from_slice(&from[160..168]); + assert_eq!( + known(flat_lengths(&into), 184..186), + known(expected, 184..186), + "inheriting: a blit's copy of command 1 into command 2" + ); + assert_eq!( + counts(&messages.take_notices()), + Default::default(), + "nothing announced" + ); + }); +} + /// Metal's buffer getters read a command's slots as its setters left them, and a GPU copy /// carries them and a GPU reset clears them as Metal's do (on an M4): an address getter answers the /// buffer's address plus the offset's low 32 bits, the offset's low 32 bits alone for a nil buffer, diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 49ff1167a..73370870d 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -14,7 +14,7 @@ Columns: - `none`: no public API path: malformed or non-Metal input, Apple-private calls, or a cap no valid program of ordinary size meets; - `?`: not assessed yet. -Generated at `21c95649f`. +Generated at `c2b39a526`. ## Totals @@ -172,7 +172,7 @@ Generated at `21c95649f`. | `FunctionTablePastMaxBufferLength` | A visible-function table of more entries than one buffer of the device's `max_buffer_length` holds words for, or than its size word holds | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/function_table.rs:622` | `none`: a function table past one buffer | | `IndirectCommandBufferPastMaxBufferLength` | An indirect command buffer of more commands than one buffer of the device's `max_buffer_length` holds the records of | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:523` | `none`: an ICB past one buffer | | `IndirectRenderCommands` | What indirect command buffers lack of their render commands | every device (runtime piece not built) | 15: `crates/metal2vulkan-runtime/src/compute.rs:1317`, `crates/metal2vulkan-runtime/src/compute.rs:1402` (+13) | `rare`: indirect render commands' remaining pieces | -| `IndirectImageblockSize` | An indirect compute command's imageblock side past 65535, which Metal keeps | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:3434` | `none`: an imageblock side past 65535 | +| `IndirectImageblockSize` | An indirect compute command's imageblock side past 65535, which Metal keeps | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:3585` | `none`: an imageblock side past 65535 | | `CommandReplay` | The device replay of indirect command buffers | every device (runtime piece not built) | 2: `crates/metal2vulkan-runtime/src/binding.rs:723`, `crates/metal2vulkan-runtime/src/binding.rs:726` | `rare`: the device replay of indirect command buffers | | `RenderAttachments` | Render attachments and their binding forms | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/binding.rs:1794` | `rare`: render attachment forms not built | | `AccelerationStructures` | Acceleration structures | every device (runtime piece not built) | 1: `crates/metal2vulkan-runtime/src/binding.rs:1797` | `rare`: ray tracing (out of scope by ruling) | @@ -425,8 +425,8 @@ Each refusal's sites, with the function that builds it. - `gap::HeapTextureMemory`: `crates/metal2vulkan-runtime/src/texture/heap.rs:1512 (heap_plan)`; `crates/metal2vulkan-runtime/src/texture/heap.rs:1519 (heap_plan)`; `crates/metal2vulkan-runtime/src/texture/heap.rs:2061 (host_heap_plan)`; `crates/metal2vulkan-runtime/src/texture/heap.rs:2293 (memory_shadow)`; `crates/metal2vulkan-runtime/src/texture/heap.rs:556 (memory_refusal)` - `gap::FunctionTablePastMaxBufferLength`: `crates/metal2vulkan-runtime/src/function_table.rs:622 (past_limit)` - `gap::IndirectCommandBufferPastMaxBufferLength`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:523 (new_indirect_command_buffer)` -- `gap::IndirectRenderCommands`: `crates/metal2vulkan-runtime/src/compute.rs:1317 (execute_range)`; `crates/metal2vulkan-runtime/src/compute.rs:1402 (execute_indirect_range)`; `crates/metal2vulkan-runtime/src/depth_stencil.rs:85 (gpu_resource_id)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:4459 (render_state)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:518 (new_indirect_command_buffer)`; `crates/metal2vulkan-runtime/src/pipeline/mod.rs:1632 (compile_error)`; `crates/metal2vulkan-runtime/src/render.rs:7292 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:7876 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:8334 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:8888 (draw_resident)`; `crates/metal2vulkan-runtime/src/render/executions.rs:1127 (back_memoryless)`; `crates/metal2vulkan-runtime/src/render/executions.rs:2942 (record_forms)`; `crates/metal2vulkan-runtime/src/render/executions.rs:393 (execute_range)`; `crates/metal2vulkan-runtime/src/render/executions.rs:461 (execute_indirect_range)`; `crates/metal2vulkan-runtime/src/render/executions.rs:476 (execute_indirect_range)` -- `gap::IndirectImageblockSize`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:3434 (set_imageblock_size)` +- `gap::IndirectRenderCommands`: `crates/metal2vulkan-runtime/src/compute.rs:1317 (execute_range)`; `crates/metal2vulkan-runtime/src/compute.rs:1402 (execute_indirect_range)`; `crates/metal2vulkan-runtime/src/depth_stencil.rs:85 (gpu_resource_id)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:4613 (render_state)`; `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:518 (new_indirect_command_buffer)`; `crates/metal2vulkan-runtime/src/pipeline/mod.rs:1632 (compile_error)`; `crates/metal2vulkan-runtime/src/render.rs:7292 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:7876 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:8334 (draw_resident)`; `crates/metal2vulkan-runtime/src/render.rs:8888 (draw_resident)`; `crates/metal2vulkan-runtime/src/render/executions.rs:1127 (back_memoryless)`; `crates/metal2vulkan-runtime/src/render/executions.rs:2942 (record_forms)`; `crates/metal2vulkan-runtime/src/render/executions.rs:393 (execute_range)`; `crates/metal2vulkan-runtime/src/render/executions.rs:461 (execute_indirect_range)`; `crates/metal2vulkan-runtime/src/render/executions.rs:476 (execute_indirect_range)` +- `gap::IndirectImageblockSize`: `crates/metal2vulkan-runtime/src/indirect_command_buffer.rs:3585 (set_imageblock_size)` - `gap::CommandReplay`: `crates/metal2vulkan-runtime/src/binding.rs:723 (bind_into)`; `crates/metal2vulkan-runtime/src/binding.rs:726 (bind_into)` - `gap::RenderAttachments`: `crates/metal2vulkan-runtime/src/binding.rs:1794 (unexpected)` - `gap::AccelerationStructures`: `crates/metal2vulkan-runtime/src/binding.rs:1797 (unexpected)`