diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index bce3a094b..0402465c1 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -2073,7 +2073,33 @@ while the driver's exit handler waited on a lock, with every runtime thread alre longest in `first_use_wait_max_nanos`, and again by where it waited, each with its count, time and longest: `first_use_variant_waits` (a kernel's pipeline for a new workgroup size, threadgroup memory lengths or tiles), `first_use_lowering_waits` (a later lowering the reaper - made) and `first_use_line_waits` (a render content's line-class pipeline). + or a `prepare_dispatch` call made) and `first_use_line_waits` (a render content's line-class + pipeline). +- `ComputePipelineState::prepare_dispatch(Dispatch, threads_per_threadgroup, + threadgroup_memory_lengths)` has no Metal counterpart: it is a hook for a consumer's pre-pass. + Metal compiles a compute state whole when the state is made; here a kernel's `VkPipeline` waits + for the dispatch's threadgroup size and threadgroup memory lengths, and an observing kernel's + ragged grid, or a threadgroup that is not whole subgroups, for its later lowering. A consumer + that reads a command buffer's dispatches before it encodes them may pass each one's own + arguments: its grid as its call took it (`Dispatch::Threads` for `dispatchThreads:`, + `Dispatch::Threadgroups` for `dispatchThreadgroups:`, `Dispatch::Indirect` for the indirect + form), its threadgroup size, and the encoder's `setThreadgroupMemoryLength:atIndex:` settings, + in stream order, on threads of its own; rt starts none. The call decides what the dispatch + runs as the dispatch decides it (one function serves both), lowers a variant the dispatch would + wait for on the calling thread, then makes the variant's `VkPipeline`. It returns at once where + that is made, or another thread is lowering or making it. A make runs outside the kernel's + lock, its variant marked pending, so makes of different variants run at once; only a dispatch + waits, and only for its own variant's make, a first-use wait. The call makes nothing where the + dispatch would refuse its threadgroup size or threadgroup memory, or would record nothing (a + grid with no threads, a kernel that writes no memory), and reports no error: the dispatch + reports its own. What the dispatch checks of the encoder's bindings is not checked. A make + that failed is made again by the next call or dispatch that needs it. What a dispatch records + is the same whether or not its pipeline was made ahead. `Metrics::made_ahead_variants` and + `made_ahead_lowerings` count what the calls made, with their time in `made_ahead_variant_nanos` + and `made_ahead_lowering_nanos`. +- Each lowering on a calling thread is one translation within its own 500 MiB ceiling, so the + caller's width multiplies the bound: calls on 4 threads at once hold at most 2 GiB of + translation. - `ComputePipelineState::precision_notes` carries the translator's notes of where the kernel's answers may differ from Metal's on this device, fixed per operation: those of the modules made with the state, and the emulated-SIMD-group module's once it is lowered. A kernel compiled diff --git a/crates/metal2vulkan-rt/src/tests/choice.rs b/crates/metal2vulkan-rt/src/tests/choice.rs index 3d282df33..3a0470c21 100644 --- a/crates/metal2vulkan-rt/src/tests/choice.rs +++ b/crates/metal2vulkan-rt/src/tests/choice.rs @@ -9,7 +9,7 @@ //! its threads, so every workgroup is within the floor's `maxComputeWorkGroupSize`, whatever the //! threadgroup size. -use crate::encoder::{choose, image_tile, Choice, Direct, Module}; +use crate::encoder::{choose, image_tile, Choice, Direct, Dispatch, Module}; use crate::floor::{WORKGROUP_INVOCATIONS, WORKGROUP_SIZE}; use crate::pipeline::{Form, SQUARE}; use metal2vulkan::pipeline::{grid_tile, DEEP_THREADGROUP, GRID_TILE_THREADS}; @@ -230,3 +230,36 @@ fn a_threadgroup_deeper_than_the_floor_runs_as_one_row() { ); assert_eq!(workgroup(choice.local), [256, 1, 1]); } + +/// The grid `ComputePipelineState::prepare_dispatch` decides from (`Dispatch::direct`) is the +/// one each dispatch call forms, so the pre-pass and the dispatch choose alike: a +/// `dispatchThreadgroups` grid is its counts with their threads, a product that passes `u32::MAX` +/// saturating, and never ragged, so an observing kernel runs its whole module there; a +/// `dispatchThreads` grid of exactly `u32::MAX` threads on a size that does not divide it is +/// ragged, its partial-threadgroups variant; an indirect dispatch has no grid, its whole module +/// at its own size. +#[test] +fn a_prepared_dispatch_takes_the_grid_its_call_forms() { + let sized = Form::Sized { partial: None }; + let size = [2, 1, 1]; + let saturated = Dispatch::Threadgroups([u32::MAX, 3, 1]).direct(size); + let want = Direct { + groups: [u32::MAX, 3, 1], + threads: [u32::MAX, 3, 1], + ragged: false, + }; + assert_eq!(saturated, Some(want)); + assert_eq!(choose(&sized, saturated, size).module, Module::Whole); + let max = Dispatch::Threads([u32::MAX, 1, 1]).direct(size); + assert_eq!(max, Some(threads_of([u32::MAX, 1, 1], size))); + assert_eq!( + max.map(|max| (max.groups, max.ragged)), + Some(([1 << 31, 1, 1], true)) + ); + assert_eq!(choose(&sized, max, size).module, Module::Partial); + let whole = Dispatch::Threads([8, 4, 1]).direct([4, 2, 1]); + assert_eq!(whole, Some(threads_of([8, 4, 1], [4, 2, 1]))); + assert_eq!(choose(&sized, whole, [4, 2, 1]).module, Module::Whole); + assert_eq!(Dispatch::Indirect.direct(size), None); + assert_eq!(choose(&sized, None, size).module, Module::Whole); +} diff --git a/crates/metal2vulkan-rt/src/tests/sampling.rs b/crates/metal2vulkan-rt/src/tests/sampling.rs index 28117a835..f5a1b5eda 100644 --- a/crates/metal2vulkan-rt/src/tests/sampling.rs +++ b/crates/metal2vulkan-rt/src/tests/sampling.rs @@ -955,7 +955,9 @@ mod on_device { /// grid's texels and nothing past them, with no first-use wait. A grid two deep runs in /// workgroups of its tile on a `VkPipeline` made at that dispatch: one wait, the same /// texels. A column 256 deep, whose tile is 1 by 1 by 256, deeper than 64, runs its tile as - /// one row of 256 threads on every device: one wait, its one texel. + /// one row of 256 threads on every device: one wait, its one texel. A grid eight deep whose + /// dispatch `prepare_dispatch` prepared first finds its tile's `VkPipeline` made, the one the + /// call made: no wait, the same texels. #[test] fn every_device_runs_an_image_only_kernel_in_square_workgroups() { let Some(library) = library() else { @@ -1022,8 +1024,24 @@ mod on_device { "{name}: the square workgroups' pipeline, made with the state" ); let queue = device.new_command_queue(4).expect("a queue"); - for (grid, depth, waits) in [([20, 12], 1, 0), ([20, 12], 2, 1), ([1, 1], 256, 1)] { + let runs = [ + ([20, 12], 1, 0, false), + ([20, 12], 2, 1, false), + ([1, 1], 256, 1, false), + ([20, 12], 8, 0, true), + ]; + for (grid, depth, waits, prepared) in runs { fill(&output, OUT, [0.0; 4]); + if prepared { + let ahead = counters.metrics(); + let threads = crate::Dispatch::Threads([grid[0], grid[1], depth]); + down.prepare_dispatch(threads, [4, 4, 1], &[]); + let made = counters.metrics().made_ahead_variants - ahead.made_ahead_variants; + assert_eq!( + made, 1, + "{name}: {depth} deep: the call made its tile's pipeline" + ); + } let before = counters.metrics(); let command_buffer = queue.command_buffer().expect("a command buffer"); let mut encoder = command_buffer diff --git a/crates/metal2vulkan-rt/src/tests/translate.rs b/crates/metal2vulkan-rt/src/tests/translate.rs index 751a4f72a..f70a30dda 100644 --- a/crates/metal2vulkan-rt/src/tests/translate.rs +++ b/crates/metal2vulkan-rt/src/tests/translate.rs @@ -17,7 +17,10 @@ //! and a read through a buffer argument bound to nothing reads the nil buffer's zeros. //! Threadgroup memory, a declared array's and a threadgroup argument's, reads zero until the //! program writes it, in a dispatch's later threadgroups, in a second dispatch and on the -//! emulated-SIMD-group module alike. +//! emulated-SIMD-group module alike. A dispatch that `prepare_dispatch` prepared with its own +//! arguments finds its pipeline, and the lowering it needs (the partial-threadgroups or the +//! emulated-SIMD-group variant), made by the call, and what the runtime records and submits is +//! the same with the calls and without. //! //! `llvm-as` assembles the kernels; without it a test skips visibly. @@ -555,6 +558,373 @@ mod on_device { }); } + /// How a prepared case dispatches. + #[derive(Clone, Copy, Debug)] + enum How { + Threads, + Groups, + Indirect, + } + + /// Records `how` over `grid` (threads, or threadgroup counts) at `size` of + /// `icb_replay_grid_template` on `device`, its threadgroup memory `length` at index 0, waits + /// for it, and checks its answer against Metal's. The dispatch's own answer. + fn grid_template_run( + name: &str, + device: &Device, + state: &ComputePipelineState, + (grid, size, how): ([u32; 3], [u32; 3], How), + length: u32, + ) -> Result<(), Error> { + let threads = match how { + How::Threads => grid, + How::Groups | How::Indirect => std::array::from_fn(|axis| grid[axis] * size[axis]), + }; + let slots = threads.iter().product::().max(1); + let want = grid_template(threads, size, [1000, slots]); + let mut buffers = Buffers::new(device, 1 << 16); + let out = buffers.buffer(4 * u64::from(slots)); + let arguments = buffers.buffer(16); + write(&arguments, 0, &le_bytes(&grid)); + let queue = device.new_command_queue(4).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = command_buffer + .compute_command_encoder(DispatchType::Serial) + .expect("an encoder"); + encoder.set_compute_pipeline_state(state).expect("a state"); + encoder + .set_threadgroup_memory_length(length, 0) + .expect("a length"); + encoder + .set_bytes(&le_bytes(&[1000, slots]), 0) + .expect("the parameters"); + encoder.set_buffer(Some(&out), 0, 1).expect("a buffer"); + let dispatched = match how { + How::Threads => encoder.dispatch_threads(grid, size), + How::Groups => encoder.dispatch_threadgroups(grid, size), + How::Indirect => { + encoder.dispatch_threadgroups_with_indirect_buffer(&arguments, 0, size) + } + }; + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(name, &command_buffer); + assert_eq!(command_buffer.error(), None, "{name}"); + if dispatched.is_ok() && slots > 1 { + assert_eq!( + words(&out, 0, want.len()), + want, + "{name}: {how:?} {grid:?} by {size:?}" + ); + } + dispatched + } + + /// `ComputePipelineState::prepare_dispatch` makes what a dispatch of `icb_replay_grid_template`, + /// an observing kernel, then finds made: for a whole `dispatchThreads` grid, a ragged one (its + /// partial-threadgroups variant, lowered on the calling thread), a `dispatchThreadgroups` grid + /// and an indirect dispatch, each prepared with the dispatch's own arguments, the call makes + /// exactly one pipeline (and the ragged grid one lowering), and the dispatch adds no first-use + /// wait and makes nothing. A `dispatchThreadgroups` grid whose threads saturate on an axis, + /// and a `dispatchThreads` grid of exactly `u32::MAX` threads on a size that does not divide + /// it, prepared alone, make what a small grid of the same call and size then runs with no + /// wait: the whole module and the partial-threadgroups variant. A second call with the same + /// arguments makes nothing. A call for what the dispatch records nothing for (a grid with no + /// threads) or refuses (a threadgroup past Metal's, threadgroup memory past the device's) + /// makes nothing, and the dispatch answers as it does unprepared. Each answer is Metal's. + #[test] + fn every_device_finds_made_what_prepare_dispatch_made() { + use crate::Dispatch; + + let Some(descriptor) = descriptor("icb_replay_grid_template") else { + return; + }; + each_device("compute_prepare", |name, _, device| { + let raw = device.raw(); + // SAFETY: the physical device is the instance's. + let shared = unsafe { + raw.instance + .get_physical_device_properties(raw.physical_device) + .limits + .max_compute_shared_memory_size + }; + let state = state(name, &device, &descriptor); + let counters = device.counters(); + let length = |size: [u32; 3]| (4 * size.iter().product::()).next_multiple_of(16); + // What is prepared, at what size; the dispatch that then runs; the pipelines and + // lowerings the call makes. + let cases = [ + ( + Dispatch::Threads([8, 4, 1]), + [4, 2, 1], + ([8, 4, 1], How::Threads), + 1, + 0, + ), + ( + Dispatch::Threads([10, 3, 1]), + [4, 2, 1], + ([10, 3, 1], How::Threads), + 1, + 1, + ), + ( + Dispatch::Threadgroups([3, 2, 1]), + [2, 2, 1], + ([3, 2, 1], How::Groups), + 1, + 0, + ), + ( + Dispatch::Indirect, + [1, 1, 4], + ([1, 1, 2], How::Indirect), + 1, + 0, + ), + ( + Dispatch::Threadgroups([u32::MAX, 1, 1]), + [2, 1, 1], + ([3, 1, 1], How::Groups), + 1, + 0, + ), + ( + Dispatch::Threads([u32::MAX, 1, 1]), + [2, 1, 1], + ([3, 1, 1], How::Threads), + 1, + 0, + ), + ]; + for (prepared, size, (grid, how), made, lowered) in cases { + let lengths = [(0, length(size))]; + let before = counters.metrics(); + state.prepare_dispatch(prepared, size, &lengths); + let ahead = counters.metrics(); + assert_eq!( + ( + ahead.made_ahead_variants - before.made_ahead_variants, + ahead.made_ahead_lowerings - before.made_ahead_lowerings, + ahead.pipelines_created - before.pipelines_created, + ), + (made, lowered, made), + "{name}: {prepared:?} by {size:?}: what the call made" + ); + state.prepare_dispatch(prepared, size, &lengths); + let again = counters.metrics(); + assert_eq!( + (again.made_ahead_variants, again.pipelines_created), + (ahead.made_ahead_variants, ahead.pipelines_created), + "{name}: {prepared:?} by {size:?}: a second call makes nothing" + ); + grid_template_run(name, &device, &state, (grid, size, how), length(size)) + .unwrap_or_else(|error| panic!("{name}: {how:?} {grid:?}: {error}")); + let after = counters.metrics(); + assert_eq!( + ( + after.first_use_waits - again.first_use_waits, + after.pipelines_created - again.pipelines_created, + after.translations - again.translations, + ), + (0, 0, 0), + "{name}: {how:?} {grid:?} by {size:?}: the dispatch found it made" + ); + } + // What the dispatch records nothing for, or refuses: the call makes nothing. + let refusals = [ + ( + ([0, 4, 1], [4, 4, 1], How::Threads), + length([4, 4, 1]), + true, + ), + (([8, 1, 1], [2048, 1, 1], How::Threads), 16, false), + (([8, 1, 1], [8, 1, 1], How::Threads), shared + 16, false), + ]; + for ((grid, size, how), length, runs) in refusals { + let before = counters.metrics(); + state.prepare_dispatch(Dispatch::Threads(grid), size, &[(0, length)]); + let ahead = counters.metrics(); + assert_eq!( + ( + ahead.made_ahead_variants - before.made_ahead_variants, + ahead.made_ahead_lowerings - before.made_ahead_lowerings, + ahead.pipelines_created - before.pipelines_created, + ), + (0, 0, 0), + "{name}: {grid:?} by {size:?} at {length}: the call made nothing" + ); + let answer = grid_template_run(name, &device, &state, (grid, size, how), length); + match (runs, answer) { + (true, Ok(())) => {} + (false, Err(Error::Refused(Refusal::ThreadgroupSize { .. }))) => {} + (false, Err(Error::Limit(Limit::ThreadgroupMemory { .. }))) => {} + (_, answer) => panic!("{name}: {grid:?} by {size:?} at {length}: {answer:?}"), + } + let after = counters.metrics(); + assert_eq!( + after.pipelines_created - ahead.pipelines_created, + 0, + "{name}: {grid:?} by {size:?} at {length}: the dispatch made nothing" + ); + } + println!( + "{name}: prepared dispatches found made: {} kinds, 3 that make nothing", + cases.len() + ); + }); + } + + /// `prepare_dispatch` of `simd_float_products` in threadgroups of 80, which are not whole + /// SIMD groups of a kernel that runs only whole ones, lowers its emulated-SIMD-group variant + /// on the calling thread and makes that variant's pipeline: the dispatch then adds no + /// first-use wait and makes nothing. On a device whose module runs any threadgroup, the call + /// makes the size's pipeline alone. A device that cannot pin SIMD groups at 32 has no state + /// of the kernel, and the test says so. + #[test] + fn every_device_prepares_the_emulated_simdgroup_variant() { + use crate::Dispatch; + + let Some(descriptor) = descriptor_of("gsm2_simd_products", "simd_float_products") else { + return; + }; + each_device("compute_prepare_simd", |name, _, device| { + let state = match device + .new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers) + { + Ok(state) => state, + Err(Error::Unsupported(Gap::KernelInput)) => { + println!( + "{name}: SIMD groups not pinnable at 32; the tier's kernel is refused" + ); + return; + } + Err(error) => panic!("{name}: {error}"), + }; + let emulates = !state.inner.compiled.whole.interface.multiples.is_empty(); + let counters = device.counters(); + let (groups, size) = ([3, 1, 1], [80, 1, 1]); + let before = counters.metrics(); + state.prepare_dispatch(Dispatch::Threadgroups(groups), size, &[]); + let ahead = counters.metrics(); + assert_eq!( + ( + ahead.made_ahead_variants - before.made_ahead_variants, + ahead.made_ahead_lowerings - before.made_ahead_lowerings, + ), + (1, u64::from(emulates)), + "{name}: what the call made" + ); + assert_eq!( + emulated_made(&state), + emulates, + "{name}: the emulated module" + ); + let threads = groups[0] * size[0]; + let mut buffers = Buffers::new(&device, 1 << 16); + let a = buffers.buffer(u64::from(threads) * 4); + let out = buffers.buffer(u64::from(threads) * 16); + let queue = device.new_command_queue(4).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = command_buffer + .compute_command_encoder(DispatchType::Serial) + .expect("an encoder"); + encoder.set_compute_pipeline_state(&state).expect("a state"); + encoder.set_buffer(Some(&a), 0, 0).expect("a buffer"); + encoder.set_buffer(Some(&out), 0, 1).expect("a buffer"); + encoder + .dispatch_threadgroups(groups, size) + .unwrap_or_else(|error| panic!("{name}: {error}")); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(name, &command_buffer); + assert_eq!(command_buffer.error(), None, "{name}"); + let after = counters.metrics(); + assert_eq!( + ( + after.first_use_waits - ahead.first_use_waits, + after.pipelines_created - ahead.pipelines_created, + ), + (0, 0), + "{name}: the dispatch found it made" + ); + println!("{name}: threadgroups of 80 prepared, emulated {emulates}"); + }); + } + + /// One run of `icb_replay_grid_template`'s dispatches on `device`, each prepared first where + /// `prepare`: the counts of what the runtime recorded and submitted. + fn recorded_counts( + name: &str, + device: &Device, + descriptor: &ComputePipelineDescriptor, + prepare: bool, + ) -> Vec<(&'static str, u64)> { + let state = state(name, device, descriptor); + let counters = device.counters(); + let before = counters.metrics(); + let runs = [ + ([10, 3, 1], [4, 2, 1], How::Threads), + ([8, 4, 1], [4, 2, 1], How::Threads), + ([3, 2, 1], [4, 2, 1], How::Groups), + ([1, 1, 2], [1, 1, 320], How::Groups), + ([1, 1, 2], [1, 1, 8], How::Indirect), + ]; + for (grid, size, how) in runs { + let length = (4 * size.iter().product::()).next_multiple_of(16); + if prepare { + let dispatch = match how { + How::Threads => crate::Dispatch::Threads(grid), + How::Groups => crate::Dispatch::Threadgroups(grid), + How::Indirect => crate::Dispatch::Indirect, + }; + state.prepare_dispatch(dispatch, size, &[(0, length)]); + } + grid_template_run(name, device, &state, (grid, size, how), length) + .unwrap_or_else(|error| panic!("{name}: {how:?} {grid:?}: {error}")); + } + let after = counters.metrics(); + vec![ + ("submissions", after.submissions - before.submissions), + ("submit_infos", after.submit_infos - before.submit_infos), + ("passes", after.passes - before.passes), + ("barriers", after.barriers - before.barriers), + ("bytes_copied", after.bytes_copied - before.bytes_copied), + ( + "pipelines_created", + after.pipelines_created - before.pipelines_created, + ), + ("translations", after.translations - before.translations), + ("ring_writes", after.ring_writes - before.ring_writes), + ("pushes", after.pushes - before.pushes), + ] + } + + /// What the runtime records of a run of dispatches is the same with each prepared first and + /// without: on two devices of one physical device, the same dispatches of + /// `icb_replay_grid_template` (whole, ragged, threadgroup counts, deep and indirect) submit, + /// pass, barrier, copy, push and write their ring alike, make the same pipelines and + /// translations, and answer Metal's words. + #[test] + fn every_device_records_the_same_with_and_without_prepare_dispatch() { + let Some(descriptor) = descriptor("icb_replay_grid_template") else { + return; + }; + each_device("compute_prepare_recorded", |name, instance, device| { + let devices = devices_of_the_cfgs_kind(instance); + let (_, physical) = (devices.iter()) + .find(|(other, _)| other == name) + .expect("the device's own physical device"); + let plain = recorded_counts(name, &device, &descriptor, false); + let second = Device::new(physical, DeviceOptions::default()) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let prepared = recorded_counts(name, &second, &descriptor, true); + drop(second); + assert_eq!(plain, prepared, "{name}: what was recorded and submitted"); + println!("{name}: recorded alike with and without prepare_dispatch: {plain:?}"); + }); + } + /// Float patterns from a fixed generator: every sign, exponent and mantissa. fn patterns(count: usize, seed: u32) -> Vec { let mut state = seed; diff --git a/crates/metal2vulkan-validation/src/replay/prepass.rs b/crates/metal2vulkan-validation/src/replay/prepass.rs index fcd13a626..97467ba51 100644 --- a/crates/metal2vulkan-validation/src/replay/prepass.rs +++ b/crates/metal2vulkan-validation/src/replay/prepass.rs @@ -38,15 +38,24 @@ pub(super) struct Prepass { buffers: HashMap, /// Each live compute pipeline state's id to the position of the call that made it. states: HashMap, - /// Each live compute encoder's command buffer (its making's position), state and threadgroup - /// memory lengths. - encoders: HashMap)>, + /// Each live compute encoder's. + encoders: HashMap, /// The dispatches due so far, with their state's making's position. seen: HashSet<(u64, Ahead)>, /// The dispatches due at each position, in stream order. due: HashMap>, } +/// What a compute encoder set so far. +struct Encoder { + /// Its command buffer's making's position. + buffer: u64, + /// The state it set, 0 for none. + state: u64, + /// The threadgroup memory lengths it set, as (index, length) pairs in index order. + lengths: Vec<(u32, u32)>, +} + /// A size argument's three extents. fn size(value: Option<&Value>) -> Option<[u32; 3]> { let fields = value?.as_map()?; @@ -79,7 +88,13 @@ impl Prepass { self.states.insert(call.result, position); } else if s.starts_with("computeCommandEncoder") { if let Some(&buffer) = self.buffers.get(&call.receiver) { - self.encoders.insert(call.result, (buffer, 0, Vec::new())); + let lengths = Vec::new(); + let encoder = Encoder { + buffer, + state: 0, + lengths, + }; + self.encoders.insert(call.result, encoder); } } return; @@ -89,7 +104,7 @@ impl Prepass { }; match s { "setComputePipelineState:" => { - encoder.1 = call.args.first().and_then(Value::as_object).unwrap_or(0); + encoder.state = call.args.first().and_then(Value::as_object).unwrap_or(0); } "setThreadgroupMemoryLength:atIndex:" => { let get = |at: usize| { @@ -98,7 +113,7 @@ impl Prepass { .and_then(|v| u32::try_from(v).ok()) }; if let (Some(length), Some(index)) = (get(0), get(1)) { - let lengths = &mut encoder.2; + let lengths = &mut encoder.lengths; match lengths.binary_search_by_key(&index, |&(at, _)| at) { Ok(at) => lengths[at].1 = length, Err(at) => lengths.insert(at, (index, length)), @@ -111,7 +126,8 @@ impl Prepass { else { return; }; - let (buffer, state, lengths) = (encoder.0, encoder.1, encoder.2.clone()); + let (buffer, state) = (encoder.buffer, encoder.state); + let lengths = encoder.lengths.clone(); let Some(&made) = self.states.get(&state) else { return; }; diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 3a2235631..d2046c4e9 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1426,6 +1426,7 @@ enum metal2vulkan-rt::CommandBufferError #[derive(Clone, Copy, Debug, PartialEq, enum metal2vulkan-rt::CommandBufferStatus #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum CommandBufferStatus enum metal2vulkan-rt::CompareFunction #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum CompareFunction enum metal2vulkan-rt::ConsumerFeature #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum ConsumerFeature +enum metal2vulkan-rt::Dispatch #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Dispatch enum metal2vulkan-rt::DispatchType #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum DispatchType enum metal2vulkan-rt::DrawKind #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum DrawKind enum metal2vulkan-rt::Error #[derive(Debug)] #[non_exhaustive] pub enum Error @@ -2422,6 +2423,10 @@ field metal2vulkan-rt::Metrics.first_use_waits pub first_use_waits: u64 field metal2vulkan-rt::Metrics.idle_frees_by_age pub idle_frees_by_age: [u64; 6] field metal2vulkan-rt::Metrics.idle_reuses_by_age pub idle_reuses_by_age: [u64; 7] field metal2vulkan-rt::Metrics.long_take_zero_writes pub long_take_zero_writes: Timed +field metal2vulkan-rt::Metrics.made_ahead_lowering_nanos pub made_ahead_lowering_nanos: u64 +field metal2vulkan-rt::Metrics.made_ahead_lowerings pub made_ahead_lowerings: u64 +field metal2vulkan-rt::Metrics.made_ahead_variant_nanos pub made_ahead_variant_nanos: u64 +field metal2vulkan-rt::Metrics.made_ahead_variants pub made_ahead_variants: u64 field metal2vulkan-rt::Metrics.maker_allocation_nanos pub maker_allocation_nanos: u64 field metal2vulkan-rt::Metrics.maker_makes pub maker_makes: Timed field metal2vulkan-rt::Metrics.maker_wait_max_nanos pub maker_wait_max_nanos: u64 @@ -9716,6 +9721,7 @@ method metal2vulkan-rt::ComputePipelineDescriptor::uses_direct_arguments pub fn method metal2vulkan-rt::ComputePipelineState::imageblock_memory_length_for_dimensions pub fn imageblock_memory_length_for_dimensions(&self, _dimensions: [u32; 3]) -> Result method metal2vulkan-rt::ComputePipelineState::max_total_threads_per_threadgroup pub fn max_total_threads_per_threadgroup(&self) -> u32 method metal2vulkan-rt::ComputePipelineState::precision_notes pub fn precision_notes(&self) -> Vec +method metal2vulkan-rt::ComputePipelineState::prepare_dispatch pub fn prepare_dispatch(&self, dispatch: Dispatch, threads_per_threadgroup: [u32; 3], threadgroup_memory_lengths: &[(u32, u32)]) method metal2vulkan-rt::ComputePipelineState::required_threads_per_threadgroup pub fn required_threads_per_threadgroup(&self) -> [u32; 3] method metal2vulkan-rt::ComputePipelineState::static_threadgroup_memory_length pub fn static_threadgroup_memory_length(&self) -> u32 method metal2vulkan-rt::ComputePipelineState::support_indirect_command_buffers pub fn support_indirect_command_buffers(&self) -> bool @@ -13882,6 +13888,9 @@ variant metal2vulkan-rt::CompareFunction::Never Never variant metal2vulkan-rt::CompareFunction::None None variant metal2vulkan-rt::CompareFunction::NotEqual NotEqual variant metal2vulkan-rt::ConsumerFeature::SamplerYcbcrConversion SamplerYcbcrConversion +variant metal2vulkan-rt::Dispatch::Indirect Indirect +variant metal2vulkan-rt::Dispatch::Threadgroups Threadgroups([u32; 3]) +variant metal2vulkan-rt::Dispatch::Threads Threads([u32; 3]) variant metal2vulkan-rt::DispatchType::Concurrent Concurrent variant metal2vulkan-rt::DispatchType::Serial Serial variant metal2vulkan-rt::DrawKind::AttachmentFormats AttachmentFormats diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 7032e3489..f0ded9749 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -644,7 +644,7 @@ Columns: | `SamplerArgumentIndex` | A sampler argument index past Metal's 16 entries (`setSamplerState`) | every device (a public call's own check of its arguments) | `metal-documented`: the sampler argument table has 16 entries per stage | 2: `crates/metal2vulkan-rt/src/encoder.rs (set_sampler_state)`, `crates/metal2vulkan-rt/src/render_encoder.rs (set_sampler_state)` | `-` | | `SamplerIds` | A consumer's answer to `Device::new_compute_pipeline_state`'s numbered samplers with another count of indices | every device (a public call's own check of its arguments) | `ours`: each numbered sampler takes one index | 1: `crates/metal2vulkan-rt/src/pipeline.rs (number_samplers)` | `-` | | `NoBufferAtIndex` | `setBufferOffset` at an index where no buffer is bound, or a draw whose vertex descriptor reads a vertex buffer index where none is bound | every device (a public call's own check of its arguments) | `ours`: there is no buffer whose offset to change, and none whose vertices the draw reads; Metal's validation stops such a draw | 3: `crates/metal2vulkan-rt/src/encoder.rs (set_buffer_offset)`, `crates/metal2vulkan-rt/src/render_encoder.rs (find)` (+1) | `-` | -| `ThreadgroupSize` | A threadgroup of no threads, or of more than Metal's `maxTotalThreadsPerThreadgroup` for the kernel (a required size's threads, where the... | every device (a public call's own check of its arguments) | `metal-documented`: a threadgroup's threads total at most the state's `maxTotalThreadsPerThreadgroup` | 2: `crates/metal2vulkan-rt/src/encoder.rs (dispatch, 2 sites)` | `-` | +| `ThreadgroupSize` | A threadgroup of no threads, or of more than Metal's `maxTotalThreadsPerThreadgroup` for the kernel (a required size's threads, where the... | every device (a public call's own check of its arguments) | `metal-documented`: a threadgroup's threads total at most the state's `maxTotalThreadsPerThreadgroup` | 2: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)`, `crates/metal2vulkan-rt/src/encoder.rs (size_checked)` | `-` | | `IndirectOffset` | An indirect dispatch's argument offset that is not a multiple of 4, or that leaves fewer than 12 bytes (three 32-bit counts) in the buffer | every device (a public call's own check of its arguments) | `ours`: `vkCmdDispatchIndirect` reads three 32-bit counts at a 4-byte aligned offset inside the buffer | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch_threadgroups_with_indirect_buffer)` | `-` | | `BufferOffsetAlignment` | A dispatch whose kernel, or a draw whose vertex or fragment function, reaches a buffer bound where its address, the buffer's `gpuAddress` plus the... | every device (a public call's own check of its arguments) | `metal-documented`: Metal's API validation refuses the bind offset: "must be a multiple of 4 but was set to 2" for a `uint *` argument bound at offset 2, an Apple M3 Max, macOS 27.0.1, `probes/vector_access_placement.m` in the conformance crate | 1: `crates/metal2vulkan-rt/src/encoder.rs (check_alignment)` | `-` | | `IndicesGranted` | A grant of an index already granted (`Device::grant_indices`) | every device (a public call's own check of its arguments) | `ours`: each index has one grant, so each resource ID names one object | 1: `crates/metal2vulkan-rt/src/arrays.rs (grant)` | `-` | @@ -699,8 +699,8 @@ Columns: |---|---|---|---|---|---| | `BlockLen` | A block longer than one allocation and one buffer hold on the device (`Device::take_block` past `maxMemoryAllocationSize` or `maxBufferSize`) | at the device's limit | `device`: the device's `maxMemoryAllocationSize` and `maxBufferSize` | 4: `crates/metal2vulkan-rt/src/import.rs (check_len)`, `crates/metal2vulkan-rt/src/memory.rs (round)` (+2) | `-` | | `BlockIds` | A block after every block number has been used (`Device::take_block`) | every device (a public call's own check of its arguments) | `ours`: block numbers are 32 bits | 2: `crates/metal2vulkan-rt/src/memory.rs (allocation_number)`, `crates/metal2vulkan-rt/src/memory.rs (number)` | `-` | -| `ThreadgroupMemory` | A dispatch whose threadgroup memory, the kernel's own and the lengths set (each rounded up to 16 bytes), is past the device's... | at the device's limit | `device`: the device's `maxComputeSharedMemorySize` | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | -| `ThreadgroupThreads` | A dispatch whose threadgroup is within Metal's `maxTotalThreadsPerThreadgroup` for the kernel and past the state's on this device | at the device's limit | `device`: the device's compute work-group and shared-memory limits | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | +| `ThreadgroupMemory` | A dispatch whose threadgroup memory, the kernel's own and the lengths set (each rounded up to 16 bytes), is past the device's... | at the device's limit | `device`: the device's `maxComputeSharedMemorySize` | 1: `crates/metal2vulkan-rt/src/encoder.rs (threadgroup_memory)` | `-` | +| `ThreadgroupThreads` | A dispatch whose threadgroup is within Metal's `maxTotalThreadsPerThreadgroup` for the kernel and past the state's on this device | at the device's limit | `device`: the device's compute work-group and shared-memory limits | 1: `crates/metal2vulkan-rt/src/encoder.rs (size_checked)` | `-` | | `BytesLength` | `setBytes` data longer than one parameter ring chunk | every device (a public call's own check of its arguments) | `ours`: a ring chunk is 64 KiB | 2: `crates/metal2vulkan-rt/src/encoder.rs (set_bytes)`, `crates/metal2vulkan-rt/src/render_encoder.rs (set_bytes)` | `-` | | `IndexRange` | A grant of no indices, or one past its class's range (`Device::grant_indices`, [`IndexClass::range`]) | every device (a public call's own check of its arguments) | `ours`: the descriptor arrays are 65,536 sampled and 65,536 storage textures, one index of both a texture's, and 4,000 samplers of each use, and index 0 is nil | 1: `crates/metal2vulkan-rt/src/arrays.rs (grant)` | `-` | | `TextureSize` | A texture whose extent or array layers are past what the device makes of its format, type, usage and sample count | at the device's limit | `device`: the image format query's `maxExtent` and `maxArrayLayers` | 1: `crates/metal2vulkan-rt/src/texture.rs (plan)` | `-` | @@ -722,8 +722,8 @@ Columns: | `IndirectCommandBuffers` | A compute pipeline state for indirect command buffers (`supportIndirectCommandBuffers`) | every device (a public call's own check of its arguments) | `ours`: indirect command buffers are not built | 2: `crates/metal2vulkan-rt/src/pipeline.rs (new_compute_pipeline_state)`, `crates/metal2vulkan-rt/src/render.rs (refuse)` | `-` | | `KernelInput` | A kernel that reads something the compute encoder does not supply yet | every device (a public call's own check of its arguments) | `ours`: the encoder supplies only those | 1: `crates/metal2vulkan-rt/src/pipeline.rs (gap)` | `-` | | `ImageblockLength` | An imageblock length Metal answers that no case measures | every device (a public call's own check of its arguments) | `ours`: the length is not measured against Metal's | 2: `crates/metal2vulkan-rt/src/pipeline.rs (imageblock_memory_length_for_dimensions)`, `crates/metal2vulkan-rt/src/render.rs (imageblock_sample_length)` | `-` | -| `PartialSimdgroups` | A dispatch whose threadgroup is not whole SIMD groups of a kernel whose tier runs each SIMD group as one subgroup of `width` threads (a pinned... | every device (a public call's own check of its arguments) | `ours`: no emulated-SIMD-group module | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | -| `PartialThreadgroups` | `dispatchThreads` with a grid that is not a whole number of threadgroups on some axis, of a kernel that observes its threadgroup partition and... | every device (a public call's own check of its arguments) | `ours`: the translator's partial-threadgroups variant does not take the emulated SIMD-group tier, nor a vote word past the workgroup memory | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | +| `PartialSimdgroups` | A dispatch whose threadgroup is not whole SIMD groups of a kernel whose tier runs each SIMD group as one subgroup of `width` threads (a pinned... | every device (a public call's own check of its arguments) | `ours`: no emulated-SIMD-group module | 1: `crates/metal2vulkan-rt/src/encoder.rs (decide)` | `-` | +| `PartialThreadgroups` | `dispatchThreads` with a grid that is not a whole number of threadgroups on some axis, of a kernel that observes its threadgroup partition and... | every device (a public call's own check of its arguments) | `ours`: the translator's partial-threadgroups variant does not take the emulated SIMD-group tier, nor a vote word past the workgroup memory | 1: `crates/metal2vulkan-rt/src/encoder.rs (decide)` | `-` | | `NoStageInRegion` | A dispatch of a kernel that reads the stage-in region (`[[grid_origin]]`, `[[grid_size]]`) on an encoder that never set one... | every device (a public call's own check of its arguments) | `ours`: no replay stream dispatches such a kernel without setting a region | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | | `MemorylessTexture` | A texture of `Memoryless` storage, which only render passes use | every device (a public call's own check of its arguments) | `ours`: render passes are not built | 1: `crates/metal2vulkan-rt/src/texture.rs (checked)` | `-` | | `TextureBuffer` | A texture buffer (`MTLTextureTypeTextureBuffer`) | every device (a public call's own check of its arguments) | `ours`: no texel-buffer array; no replay stream uses one | 2: `crates/metal2vulkan-rt/src/texture.rs (shape, 2 sites)` | `-` | @@ -1267,7 +1267,7 @@ Each refusal's sites, with the function that builds it. - `rt:Refusal::SamplerArgumentIndex`: `crates/metal2vulkan-rt/src/encoder.rs (set_sampler_state)`; `crates/metal2vulkan-rt/src/render_encoder.rs (set_sampler_state)` - `rt:Refusal::SamplerIds`: `crates/metal2vulkan-rt/src/pipeline.rs (number_samplers)` - `rt:Refusal::NoBufferAtIndex`: `crates/metal2vulkan-rt/src/encoder.rs (set_buffer_offset)`; `crates/metal2vulkan-rt/src/render_encoder.rs (find)`; `crates/metal2vulkan-rt/src/render_encoder.rs (set_buffer_offset)` -- `rt:Refusal::ThreadgroupSize`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch, 2 sites)` +- `rt:Refusal::ThreadgroupSize`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)`; `crates/metal2vulkan-rt/src/encoder.rs (size_checked)` - `rt:Refusal::IndirectOffset`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch_threadgroups_with_indirect_buffer)` - `rt:Refusal::BufferOffsetAlignment`: `crates/metal2vulkan-rt/src/encoder.rs (check_alignment)` - `rt:Refusal::IndicesGranted`: `crates/metal2vulkan-rt/src/arrays.rs (grant)` @@ -1317,8 +1317,8 @@ Each refusal's sites, with the function that builds it. - `rt:Refusal::StencilAttachment`: `crates/metal2vulkan-rt/src/render_encoder.rs (draw)` - `rt:Limit::BlockLen`: `crates/metal2vulkan-rt/src/import.rs (check_len)`; `crates/metal2vulkan-rt/src/memory.rs (round)`; `crates/metal2vulkan-rt/src/surface.rs (copy_form)`; `crates/metal2vulkan-rt/src/surface.rs (dma_buf_in_place)` - `rt:Limit::BlockIds`: `crates/metal2vulkan-rt/src/memory.rs (allocation_number)`; `crates/metal2vulkan-rt/src/memory.rs (number)` -- `rt:Limit::ThreadgroupMemory`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` -- `rt:Limit::ThreadgroupThreads`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` +- `rt:Limit::ThreadgroupMemory`: `crates/metal2vulkan-rt/src/encoder.rs (threadgroup_memory)` +- `rt:Limit::ThreadgroupThreads`: `crates/metal2vulkan-rt/src/encoder.rs (size_checked)` - `rt:Limit::BytesLength`: `crates/metal2vulkan-rt/src/encoder.rs (set_bytes)`; `crates/metal2vulkan-rt/src/render_encoder.rs (set_bytes)` - `rt:Limit::IndexRange`: `crates/metal2vulkan-rt/src/arrays.rs (grant)` - `rt:Limit::TextureSize`: `crates/metal2vulkan-rt/src/texture.rs (plan)` @@ -1335,8 +1335,8 @@ Each refusal's sites, with the function that builds it. - `rt:Gap::IndirectCommandBuffers`: `crates/metal2vulkan-rt/src/pipeline.rs (new_compute_pipeline_state)`; `crates/metal2vulkan-rt/src/render.rs (refuse)` - `rt:Gap::KernelInput`: `crates/metal2vulkan-rt/src/pipeline.rs (gap)` - `rt:Gap::ImageblockLength`: `crates/metal2vulkan-rt/src/pipeline.rs (imageblock_memory_length_for_dimensions)`; `crates/metal2vulkan-rt/src/render.rs (imageblock_sample_length)` -- `rt:Gap::PartialSimdgroups`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` -- `rt:Gap::PartialThreadgroups`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` +- `rt:Gap::PartialSimdgroups`: `crates/metal2vulkan-rt/src/encoder.rs (decide)` +- `rt:Gap::PartialThreadgroups`: `crates/metal2vulkan-rt/src/encoder.rs (decide)` - `rt:Gap::NoStageInRegion`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` - `rt:Gap::MemorylessTexture`: `crates/metal2vulkan-rt/src/texture.rs (checked)` - `rt:Gap::TextureBuffer`: `crates/metal2vulkan-rt/src/texture.rs (shape, 2 sites)`