//! Executing an indirect command buffer's compute commands on the GPU alone, where the GPU may //! write their records (`executeCommandsInBuffer:withRange:`, the orchestrator's rulings of //! 2026-10-06 and 2026-10-21). //! //! - **Totality, decided at commit.** An execution a host step would record (`indirect_execute.rs`: //! a route that may write its buffer's records met it, or it lies inside a block of command //! predication) runs here where every record it may hold is covered: its range is given as //! such, not read from a buffer; a caller's check (`execute_commands_in_buffer_checked`) has //! nothing to see (`Execute::check_sees`), else the host step runs it before the commands reach //! the device; where the buffer inherits the encoder's buffers, the encoder binds no //! visible-function table (**Inherited buffers**, below); every pipeline state its records may name (the encoder's where //! the buffer inherits it, else every state made with `supportIndirectCommandBuffers` alive at //! the commit) compiles as a flat device-replay variant (`ComputePipelineState::replay_variant`: //! its kernel does not observe its threadgroup partition) whose plan binds no descriptor, and //! fetches no `[[stage_in]]` attribute, whose reach past 4 GiB the host step checks //! (`fetch_reach.rs`); no command of the range has a state the CPU set that the program released //! since, whose program the command runs while its record names the ID //! (`IndirectCommandBuffer::name_state`), and the variants of the states alive run the ID's //! holder instead; where the ID's holder as the GPU last could write the records is a state the //! program released since, whose program the buffer keeps (`released_programs.rs`), the ID names //! that program's variant, as Metal's record the GPU wrote runs it, unless the CPU set the state //! that took the ID since on a command of the range (`IndirectCommandBuffer::gpu_states`); and //! one of the two recordings below takes the variants. Each flat variant runs //! every grid and threadgroup size of its state, so the records' contents cannot leave a command //! uncovered. Otherwise the execution takes the host step (`indirect_execute.rs`, part 3a), which //! the device announces once (`Degradation::HostReplayedIndirectCommands`). Nothing is decided at //! run time, but for a checked execution whose check a route encoded since the commit gives //! something to see before its batch is submitted: its host step records it then //! (`Execute::outgrown`), from the states `commit` settled for it. //! - **What runs.** One segment: the replay's preprocess (`bind::replay_preprocess_module`, an //! owned kernel) over the range, reading the records where the GPU and the CPU left them and //! writing each command's slot table, region record, and what the recording reads; a barrier; //! then the recording: //! - **generated commands** (`generated_execute.rs`), where the device enabled //! `VK_EXT_device_generated_commands` (`DeviceOptions::withhold_generated_commands` keeps it //! off) and the product sets the path up for the variants //! (`bind::replay_preprocess_generated`): per command one `vkCmdExecuteGeneratedCommandsEXT` //! of the sequences the preprocess wrote; //! - **indirect dispatches** (`dispatch_execute.rs`) on every other device, Vulkan 1.2's core //! (`bind::replay_preprocess`): per command of the range, in order, per variant its pipeline, //! its push bytes naming the command's slot table and region record, and one //! `vkCmdDispatchIndirect` of the group count the preprocess wrote for that command and //! variant: the command's own for the state its record names, zero for every other variant, //! and for a command that runs nothing. At most [`MAX_DISPATCHES`] (commands times variants) //! per execution, and at most [`DISPATCHES_PAST_UNORDERED`] commands times the variants past //! [`FREE_STATES_UNORDERED`] where no barrier goes between the commands //! ([`DISPATCHES_PAST_ORDERED`] past [`FREE_STATES_ORDERED`] where one does): past those the //! host step runs the execution, as it does it faster ([`dispatches_run`]). //! - **Order.** Metal orders a command after the earlier ones of the range only where its record //! sets a barrier, in either kind of encoder (`indirect_execute.rs`, **Order**; a serial //! encoder adds a barrier before and after the range alone, which the segment's own are): where //! no route that wrote the buffer's records may set one (`IndirectCommandBuffer::command_writes`, //! no `CommandWrites::BARRIER`), the indirect dispatches go with no barrier between them, and //! else with one between every two (`Barrier::between_dispatches`). A route encoded after the //! commit can still run before the execution only where the execution's batch waits for an //! event its command buffer signals, so its batch is held until then ([`Rerecord`]): where the //! buffer's records may set a barrier by the time the batch is submitted, the commands are //! recorded again then, with the barriers, from what the commit found (no work on the GPU is //! waited for). The generated-commands path runs the commands of a segment its records leave //! without a barrier together, as Metal's commands may run. //! - **Command predication.** An execution inside a block runs its preprocess by //! `vkCmdDispatchIndirect` from a gate the step before it writes (`predication.rs`, //! `Execute::preprocess_gate`): one invocation per command where the counter is 0, else none. //! Where the block skips, the preprocess writes nothing: every command's group counts (and //! sequence counts) stay the zeros the scratch was uploaded with, so no command runs, and nothing //! is reported, as Metal's skipped execution reports nothing. //! - **Faults.** A record the preprocess cannot run (a header with the producers' fault, a record //! that does not decode, sets what its descriptor does not allow, or names no variant's state) //! runs nothing, and the command buffer's execution report states why, from the status words the //! preprocess wrote (`bind::ReplayFaults`), as the host step's report does. //! - **Inherited buffers.** Where the buffer inherits the encoder's buffers, every command binds //! the encoder's kernel buffers as they stood at the call, each at its offset then (a //! `setBufferOffset:` before the call included), as the host step binds them: the replay takes //! the encoder's table (`bind::ReplayExecution::parent_buffers`), whose root entries the host //! writes once into the scratch and the preprocess copies into each command's slot table, //! resolving nothing, so a buffer the encoder alone names is read. Inline bytes the encoder //! bound are read where it copied them at their call, in the command buffer's upload arena, //! whose device address the scratch takes (`inherited_bytes`). The commands' uses of the //! encoder's bindings order them as a dispatch's would (`variant_uses`). A command that sets a //! buffer of its own runs nothing and reports it (`bind::ReplayFaults::BUFFERS`), as the host's //! decode refuses it. //! - **What the commands reach.** The replay's resident set, which the preprocess resolves each //! record's buffer addresses in and the commands' kernels search for the pointers they load, //! holds the resources the command buffer's encoders made resident and the device's live //! buffers Metal reaches unnamed (`live_buffers.rs`, `BindState::resident_ranges`), as a directly //! encoded command's does: every buffer of its own, and a no-copy buffer or a heap's buffer once //! a command buffer's commands used it. A record's address in none of them (a buffer the //! program released before the execution, among others) misses as the host step's does: its //! slot is unbound, so it reads zero and drops writes, the command runs, and the command //! buffer's execution report counts the miss (`bind::replay`). Where the live buffers are past //! the search's capacity, the execution takes the host step. //! - **Objects.** Per execution: the preprocess's scratch (a region of the command buffer's upload //! arena, `upload.rs`) and what the recording makes (`generated_execute.rs`, //! `dispatch_execute.rs`); the command buffer holds them until it completes. The execution record //! lists each variant once. use super::argument_table::ArgumentTable; use super::binding; use super::blit::reads; use super::command_buffer::{CommandBuffer, ReplayTable}; use super::degradation::CompiledPipeline; use super::device::lock; use super::dispatch_execute::{self, Template}; use super::encoder::{EncoderCore, RecordedSegment}; use super::error::{internal, Error}; use super::executed_commands::RecordsRead; use super::execution::{ExecutedPipeline, Execution}; use super::fetch_reach; use super::generated_execute; use super::indirect_command_buffer::IndirectCommandBuffer; use super::indirect_execute::{Execute, ExecutionRange}; use super::owned_kernel::OwnedModule; use super::pipeline::{ComputePipelineState, Variant, VariantObjects}; use super::residency::Residency; use super::resource::ResourceKey; use super::tracker::{Barrier, Use}; use super::upload::Upload; use super::Device; use ash::vk; use metal2vulkan::bind::{ replay_preprocess, replay_preprocess_generated, BindState, BindingPlan, DispatchCommands, GeneratedVariant, NeutralResource, PredicationStep, PushHole, ReplayExecution, ReplayPreprocess, ReplayRefusal, ReplayVariant, ResidentRange, RootTable, StageTable, }; use metal2vulkan::error::BindError; use metal2vulkan::pipeline::{CommandWrites, Stage}; use std::num::NonZeroU64; use std::sync::{Arc, Mutex}; /// The threads of a flat variant's threadgroup: a wave on most devices, and within every /// state's maximum but the smallest, which takes its maximum. const FLAT_THREADS: u32 = 64; /// The most indirect dispatches one execution records on the dispatches' path (commands times /// variants): Metal's largest buffer's commands (16,384) of up to two states, or fewer commands of /// more. Each costs the CPU one recorded dispatch and, on MoltenVK, a Metal dispatch the GPU runs /// even at a zero group count (`dispatch_execute.rs`), so a range past it keeps the host step. pub(super) const MAX_DISPATCHES: u64 = 1 << 15; /// The variants (pipeline states the records may name) an execution the indirect dispatches run /// may have, whatever its commands, where no barrier goes between them (an execution whose /// records no route may set a barrier in, the module's order): with them every count of /// commands ran faster on the GPU alone than in the host step. Each variant costs every command /// one more indirect dispatch, which MoltenVK encodes as a Metal dispatch the GPU runs even at a /// zero group count; past these, [`DISPATCHES_PAST_UNORDERED`] bounds those dispatches. Measured /// with host_ab's icb workload on an M3 Max (macOS 27, MoltenVK 1.4.1), frame time on the GPU /// alone over a host-step build's, interleaved runs: at 4,096 commands 0.53x with one variant, /// 0.89x with four, 1.03x with five and 1.32x with eight; at 1,024 commands 0.88x with four, /// 1.00x with five and 1.30x with eight; at 256 commands 0.90x and 0.96x with four, 1.06x and /// 1.00x with five, 1.26x with eight. pub(super) const FREE_STATES_UNORDERED: usize = 4; /// The indirect dispatches past [`FREE_STATES_UNORDERED`] (the commands times the variants past /// it) an execution whose commands go with no barrier between them may record on the GPU alone: /// what the host step's round trip costs beside them, about 0.8 us of the GPU's time each against /// 0.2 to 0.5 ms a frame. Measured as [`FREE_STATES_UNORDERED`] was: 256 commands of five variants /// ran as fast on both paths, 4,096 of five and 1,024 of five 0.95x and 1.00x in the host step, /// 256 of twelve 0.63x; 16 of twelve 1.28x and 16 and 64 of five 1.5x to 1.8x (slower) in the /// host step. pub(super) const DISPATCHES_PAST_UNORDERED: u64 = 256; /// [`FREE_STATES_UNORDERED`] where a barrier goes between every two commands (where the buffer's /// records may set one): MoltenVK ends its Metal compute encoder at /// each barrier, which the host step's commands pay too, so a variant's dispatches weigh less /// against it. Measured as [`FREE_STATES_UNORDERED`] was: at 256 commands 0.79x with one variant, /// 0.90x with eight, 0.94x with twelve, 0.96x with fourteen and 1.06x with sixteen; at 64 /// commands 0.72x, 0.87x, 0.91x, 0.93x and 0.93x, and 1.02x with twenty-four; at 16 commands /// 0.63x to 0.68x at every count up to sixteen, 0.85x with twenty-four. At 1,024 and 4,096 /// commands, where a Metal encoder a command bounds both paths, runs vary from 0.88x to 1.30x with /// no trend in the count. pub(super) const FREE_STATES_ORDERED: usize = 12; /// [`DISPATCHES_PAST_UNORDERED`] where a barrier goes between every two commands, past /// [`FREE_STATES_ORDERED`]: about 0.6 us of the GPU's time each against the host step's 0.3 ms a /// frame. Measured as [`FREE_STATES_UNORDERED`] was: 256 commands of fourteen variants (512 past) /// ran 0.96x and of sixteen 1.06x on the GPU alone, 64 of twenty-four (768 past) 1.02x, 16 of /// twenty-four (192 past) 0.85x; 16, 64 and 256 commands of thirteen 1.51x, 1.15x and 1.04x /// (slower) in the host step. pub(super) const DISPATCHES_PAST_ORDERED: u64 = 512; /// Whether the indirect dispatches run an execution of `commands` commands over `variants` /// variants, with a barrier between every two commands where `ordered`: within /// [`MAX_DISPATCHES`], and within its order's dispatches past its order's free variants. Both /// paths run the commands exactly; the bounds choose the faster. fn dispatches_run(commands: u32, variants: usize, ordered: bool) -> bool { let (free, past) = match ordered { true => (FREE_STATES_ORDERED, DISPATCHES_PAST_ORDERED), false => (FREE_STATES_UNORDERED, DISPATCHES_PAST_UNORDERED), }; let commands = u64::from(commands); commands * variants.saturating_sub(free) as u64 <= past && commands * variants as u64 <= MAX_DISPATCHES } /// One variant of the execution: its compiled variant and plan, its replay commands, and its /// registration. pub(super) struct Prepared { pub(super) variant: Arc, pub(super) plan: Arc, pub(super) commands: DispatchCommands, registered: ReplayVariant, } /// How the commands run once the preprocess wrote what they read. It keeps no copy of the /// device's functions: the generated commands' are the device's, fetched as the commands are /// recorded. pub(super) enum Form { /// Through the device's generated-commands extension (`generated_execute.rs`). Generated, /// By indirect dispatches (`dispatch_execute.rs`), each variant's push template in the /// variants' order. Dispatches(Vec