From abacc1222e225b548ce39b0aba32bfe43c0c8d72 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 21:36:20 -0800 Subject: [PATCH] The encoder trail names what a host step drew: ranges read, draws, the largest, kept chunks A stalled host-stepped render encoder's trail line named its states but not what its step recorded from the records and range buffers the GPU wrote, which is where a wrong count would come from. With the trail on, a host step now notes each range it read from a buffer (location and length as the bytes held them, and the commands it ran), its draws, their vertices or indices times instances summed, the draw of the most of those (primitive type, count, instances), and the kept chunks it ran again, whose draws it did not read. Default off: nothing is read or formatted with the trail off. Verified: the trail unit test (encoder_trail::tests) covers the line's new part. --- .../metal2vulkan-runtime/src/encoder_trail.rs | 159 +++++++++++++++++- .../src/indirect_execute.rs | 15 ++ .../src/render/executions.rs | 41 +++++ .../src/render/executions/kept.rs | 8 + docs/API.md | 6 +- 5 files changed, 224 insertions(+), 5 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/encoder_trail.rs b/crates/metal2vulkan-runtime/src/encoder_trail.rs index 93bac8af3..ed1e3dbe4 100644 --- a/crates/metal2vulkan-runtime/src/encoder_trail.rs +++ b/crates/metal2vulkan-runtime/src/encoder_trail.rs @@ -17,7 +17,10 @@ //! state's label and its functions' names ([`label_hash`]). No name and no shader text. //! - **Host steps** ([`Step`]). A host step that records an ended encoder's segment //! (`CommandBuffer::record_host_step`) notes the states its indirect commands name, as the GPU -//! and the CPU left the records, and its batch names them after the encoder's own. A submission +//! and the CPU left the records, and what it drew of them ([`Drawn`]): each range it read from a +//! buffer, the draws it recorded, their vertices or indices times their instances, the draw of +//! the most of those, and the kept chunks it ran again (`render/executions/kept.rs`), whose +//! draws it did not read. Its batch names them after the encoder's own. A submission //! the step makes and waits for itself (a rasterization-rate map's fan-out probe, //! `render/fan_out.rs`, or a predicated loop's check run again, `command_buffer/rerun.rs`) //! signals no timeline: once it ran for [`AFTER`], one line names it ([`waited_line`]). @@ -62,6 +65,94 @@ pub(crate) struct Notes { indirect: u64, /// A render pass's shape, noted at its end ([`pass_named`]). pass: Option, + /// What a host step drew of its commands. + drawn: Drawn, +} + +/// What a host step drew of an execution's commands (the module docs, **Host steps**). +#[derive(Default)] +pub(crate) struct Drawn { + /// Each range it read from a buffer, at most [`KEPT`]: its location and length as the bytes + /// held them, and the commands it ran of them. + ranges: Vec<([u32; 2], std::ops::Range)>, + /// The ranges read past [`KEPT`]. + more_ranges: usize, + /// The draws it recorded. + draws: u64, + /// Their vertices or indices times their instances, summed. + work: u64, + /// The draw of the most of those: whether indexed, its primitive type's raw value, its + /// vertices or indices, and its instances. + largest: Option<(bool, u32, u32, u32)>, + /// The kept chunks it ran again, and their commands. + kept: u64, + kept_commands: u64, +} + +impl Drawn { + fn join(&mut self, other: Drawn) { + for range in other.ranges { + match self.ranges.len() < KEPT { + true => self.ranges.push(range), + false => self.more_ranges += 1, + } + } + self.more_ranges += other.more_ranges; + self.draws += other.draws; + self.work = self.work.saturating_add(other.work); + if let Some(largest) = other.largest { + self.largest(largest); + } + self.kept += other.kept; + self.kept_commands += other.kept_commands; + } + + fn largest(&mut self, draw: (bool, u32, u32, u32)) { + let work = |(_, _, count, instances): (bool, u32, u32, u32)| { + u64::from(count) * u64::from(instances) + }; + if self + .largest + .is_none_or(|largest| work(largest) < work(draw)) + { + self.largest = Some(draw); + } + } + + fn any(&self) -> bool { + !self.ranges.is_empty() || self.draws > 0 || self.kept > 0 + } + + /// As a line names it. + fn described(&self) -> String { + let ranges: Vec = (self.ranges.iter()) + .map(|([location, length], ran)| { + format!("{location}+{length} ran {}..{}", ran.start, ran.end) + }) + .collect(); + let more = match self.more_ranges { + 0 => String::new(), + more => format!(" and {more} more"), + }; + let largest = match self.largest { + Some((indexed, primitive, count, instances)) => format!( + ", the largest {} of primitive type {primitive}: {count} {} times {instances} \ + instance(s)", + if indexed { "an indexed draw" } else { "a draw" }, + if indexed { "indices" } else { "vertices" }, + ), + None => String::new(), + }; + format!( + "its host step read ranges [{}]{more}, drew {} draw(s) of {} vertices or indices \ + times instances{largest}, and ran {} kept chunk(s) of {} command(s) again", + ranges.join(", "), + self.draws, + self.work, + self.kept, + self.kept_commands + ) + } } impl Notes { @@ -102,6 +193,30 @@ impl Notes { } } + /// A host step read the range `raw` (location, then length) from a buffer, and ran the + /// commands `ran` of it. + pub(crate) fn range_read(&mut self, raw: [u32; 2], ran: std::ops::Range) { + match self.drawn.ranges.len() < KEPT { + true => self.drawn.ranges.push((raw, ran)), + false => self.drawn.more_ranges += 1, + } + } + + /// A host step recorded a draw, `indexed` or not, of primitive type `primitive` (its raw + /// value), of `count` vertices or indices, times `instances`. + pub(crate) fn drew(&mut self, indexed: bool, primitive: u32, count: u32, instances: u32) { + let drawn = &mut self.drawn; + drawn.draws += 1; + drawn.work = (drawn.work).saturating_add(u64::from(count) * u64::from(instances)); + drawn.largest((indexed, primitive, count, instances)); + } + + /// A host step ran a kept chunk of `commands` again. + pub(crate) fn kept_again(&mut self, commands: u64) { + self.drawn.kept += 1; + self.drawn.kept_commands += commands; + } + /// Its render pass, as [`pass_named`] names it. pub(crate) fn pass(&mut self, pass: String) { self.pass = Some(pass); @@ -158,11 +273,12 @@ impl Step { self.states.hashed(hash); } self.states.more += notes.more; + self.states.drawn.join(notes.drawn); } - /// Whether its commands named any state. + /// Whether its commands named any state, or it drew any. fn named(&self) -> bool { - !self.states.states.is_empty() || self.states.more > 0 + !self.states.states.is_empty() || self.states.more > 0 || self.states.drawn.any() } /// The encoder, and the states its commands named where they named any, as a line names it. @@ -179,7 +295,11 @@ impl Step { more => format!(" and {more} more"), }; let states = self.states.states.join(" "); - format!("{encoder}, its host step's commands' states [{states}]{more}") + let drawn = match self.states.drawn.any() { + true => format!("; {}", self.states.drawn.described()), + false => String::new(), + }; + format!("{encoder}, its host step's commands' states [{states}]{more}{drawn}") } /// The encoder's name after the step: [`described`](Self::described) where its commands @@ -338,5 +458,36 @@ mod tests { &*stepped, format!("Render encoder 3 of command buffer 7, its host step's commands' states [{b}]") ); + // What the step drew follows: the ranges it read, its draws and their work, the draw of + // the most, and the kept chunks it ran again. + let mut drawn = Notes::default(); + drawn.range_read([4, 9], 4..10); + drawn.drew(true, 3, 36, 2); + drawn.drew(false, 3, 6, u32::MAX); + drawn.drew(true, 3, 300, 1); + drawn.kept_again(64); + step.noted(drawn); + let stepped = step.stepped().expect("a state named"); + let work = 72 + 6 * u64::from(u32::MAX) + 300; + assert_eq!( + &*stepped, + format!( + "Render encoder 3 of command buffer 7, its host step's commands' states [{b}]; \ + its host step read ranges [4+9 ran 4..10], drew 3 draw(s) of {work} vertices \ + or indices times instances, the largest a draw of primitive type 3: 6 vertices \ + times {} instance(s), and ran 1 kept chunk(s) of 64 command(s) again", + u32::MAX + ) + ); + // A step that drew and named no state is named too. + let mut unnamed = Step::new(None); + let mut drawn = Notes::default(); + drawn.drew(true, 3, 3, 1); + unnamed.noted(drawn); + let stepped = unnamed.stepped().expect("a draw noted"); + assert!( + stepped.contains("drew 1 draw(s) of 3 vertices"), + "{stepped}" + ); } } diff --git a/crates/metal2vulkan-runtime/src/indirect_execute.rs b/crates/metal2vulkan-runtime/src/indirect_execute.rs index 503756d7b..ed81af246 100644 --- a/crates/metal2vulkan-runtime/src/indirect_execute.rs +++ b/crates/metal2vulkan-runtime/src/indirect_execute.rs @@ -266,6 +266,21 @@ impl ExecutionRange { matches!(self, ExecutionRange::Indirect { .. }) } + /// A range read from a buffer: its location and length as its bytes hold them now, for the + /// encoder trail (`encoder_trail.rs`, **Host steps**); `None` for one given, or bytes no + /// mapping holds. + pub(super) fn raw(&self) -> Option<[u32; 2]> { + let ExecutionRange::Indirect { buffer, offset } = self else { + return None; + }; + let [l0, l1, l2, l3, n0, n1, n2, n3] = + buffer.read_mapped::<{ RANGE_BYTES as usize }>(*offset)?; + Some([ + u32::from_le_bytes([l0, l1, l2, l3]), + u32::from_le_bytes([n0, n1, n2, n3]), + ]) + } + /// The commands it runs of a buffer of `size` commands: the range given, or the range its /// bytes hold now as Metal's execution takes it (an M3 Max and an M4, for a compute encoder; /// a probe, the same chips, for a render encoder): from `location` to `location + length`, diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs index cc508bb82..1e50931c1 100644 --- a/crates/metal2vulkan-runtime/src/render/executions.rs +++ b/crates/metal2vulkan-runtime/src/render/executions.rs @@ -1090,6 +1090,27 @@ impl Found { } } +/// A draw `action` asks for, as the encoder trail notes it (`encoder_trail.rs`, **Host steps**): +/// whether indexed, its primitive type's raw value, its vertices or indices, and its instances; +/// `None` for any other command. +fn trail_draw(action: &Action) -> Option<(bool, u32, u32, u32)> { + match *action { + Action::DrawPrimitives { + primitive_type, + vertex_count, + instance_count, + .. + } => Some((false, primitive_type.into(), vertex_count, instance_count)), + Action::DrawIndexedPrimitives { + primitive_type, + index_count, + instance_count, + .. + } => Some((true, primitive_type.into(), index_count, instance_count)), + _ => None, + } +} + /// The vertices of each primitive of `draw` of `pipeline`, where its indices may be drawn as /// consecutive ranges, each a draw of its own, as Metal draws the whole (`rate_mapped_pass.rs`, /// **Pieces**): a list of points, lines or triangles, which Metal never restarts, of one @@ -2185,6 +2206,7 @@ impl RenderExecution { "a range read from a buffer recorded before its host step", )); } + self.trail_range(command_buffer, &range); found.faults = ReplayFaults(found.faults.0 | faults.0); // The caller's check sees each command the execution will record, before it records any, // where it may see something; so do the command buffer's executed-commands handlers, @@ -2254,6 +2276,20 @@ impl RenderExecution { self.finish(encoder, found, rendered, &device) } + /// Note, for the encoder trail, the range a host step read from a buffer and the commands + /// `ran` of it (`encoder_trail.rs`, **Host steps**): nothing where the trail is off, the + /// range was given, or no step is under way. + pub(super) fn trail_range(&self, command_buffer: &CommandBuffer, ran: &Range) { + if !command_buffer.device().shared().encoder_trail { + return; + } + if let Some(raw) = self.range.raw() { + let mut notes = crate::encoder_trail::Notes::default(); + notes.range_read(raw, ran.clone()); + command_buffer.trail_step(notes); + } + } + /// An encoder of `core` that replays the commands into a secondary of `command_buffer` begun /// against the execution's instance, its draws counting in the query `lent` lends, over the /// pass's `storage`, in `layout`, with `residency`, the command buffer's, its draws' variants @@ -2493,6 +2529,11 @@ impl RenderExecution { continue; } }; + if let (Some((notes, _)), false) = (&mut trail, probing) { + if let Some((indexed, primitive, count, instances)) = trail_draw(&record.action) { + notes.drew(indexed, primitive, count, instances); + } + } // The probe probes no mesh or patch draw, nor a command after one: its functions run in // dispatches before the pass, which may write what a later vertex function reads. if probing && matches!(draw, Draw::Mesh(..) | Draw::Patches { .. }) { diff --git a/crates/metal2vulkan-runtime/src/render/executions/kept.rs b/crates/metal2vulkan-runtime/src/render/executions/kept.rs index 7d9f91f89..cc97c2373 100644 --- a/crates/metal2vulkan-runtime/src/render/executions/kept.rs +++ b/crates/metal2vulkan-runtime/src/render/executions/kept.rs @@ -614,6 +614,7 @@ impl RenderExecution { let device = command_buffer.device().clone(); let (range, faults) = self.range.commands(self.icb.size()); found.faults = ReplayFaults(found.faults.0 | faults.0); + self.trail_range(command_buffer, &range); let Keeping { mut key, kept, @@ -655,6 +656,13 @@ impl RenderExecution { Some((kept, held)) }); if let Some((kept, held)) = again { + // The encoder trail notes the chunk run again, whose draws were not read + // (`encoder_trail.rs`, **Host steps**). + if step && shared.encoder_trail { + let mut notes = crate::encoder_trail::Notes::default(); + notes.kept_again(u64::from(covered.end - covered.start)); + command_buffer.trail_step(notes); + } core.run_kept(&kept.effects, held)?; found.misses = found.misses.wrapping_add(kept.misses); found.faults = ReplayFaults(found.faults.0 | kept.faults.0); diff --git a/docs/API.md b/docs/API.md index 89a8d8df8..9ff2d6bb0 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1901,7 +1901,11 @@ buffer's object ID, its place among the command buffer's encoders, a render pass count, attachments' formats and sizes, Vulkan render pass instances and whether it is over a rasterization-rate map, its executions of indirect commands, and the label hash of each pipeline state it bound (the first 8 bytes of the SHA-256 of the state's label and its functions' names; no -name and no shader text), and of each state its indirect commands named at a host step. Once no +name and no shader text), and of each state its indirect commands named at a host step, with what +that step drew: each range it read from a buffer (its location and length as the bytes held them, +and the commands it ran), its draws, their vertices or indices times their instances summed, the +draw of the most of those (its primitive type, count and instances), and the kept chunks it ran +again, whose draws it did not read. Once no work of the device advanced for 2 s while some was pending, it logs one line for each `VkQueue` whose oldest batch is pending (id name `metal2vulkan-encoder-trail`, `Info` severity): that batch's encoders, and the last batch the queue completed, with how long before; once a stall. A