From 40a1558ff6e1dc189be057627d704b3e4e2bb114 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 5 Nov 2026 05:53:09 -0800 Subject: [PATCH] Decode a host step's records in place, through RecordView::record_into A host step decoded each record (CommandRecord::decode: a record made, then moved out of its Result, its Option and the filter, and into the pair the list holds) and pushed it: about 3 KB moved several times a record, 5001 records a frame on the replayed textured page, where the decodes' and pushes' moves were 37 of the steps thread's _platform_memmove samples in a 6 s window on the M3 at 43114cb72b. RecordView::record_into (new, public) writes a record's every field over a record the caller holds, as `*record = view.record()` leaves it; record() is it over a default record. The runtime's reads (written_records_of, decode_written) push each record's slot first and decode into it (push_written), dropping it to None where it does not decode or the descriptor does not allow it, as before. API table: one line added, RecordView::record_into; nothing removed. Verified (M3, potluck): indirect_commands::tests::records:: a_field_written_alone_is_the_record_encoded_again also writes each of its 20 000 records over the last round's with record_into and finds it equal to decode's; the translator's indirect_commands tests 41/41. Negative control: record_into leaving the imageblock size as the record held it fails it (and 4 other tests that decode through record()). Timing, the replayed textured page, against 43114cb72b, interleaved, two rounds of 3 frames: warm host_step_ms 27.8-29.9 -> 26.0-27.4, encoding to completion 58.4-61.5 -> 56.7-59.3 ms, pixels e3306dc52a1c9e86 at every frame of both. Sampled: the reads' moves 37 -> 10 samples (2069 and 2070). The runtime's suite 1656/1656 and the residency and draw lanes 248/248 with --cfg m2v_gpu; the frame-cost lane passes (19.39 allocations a draw, bound 20; release 7.55, bound 9). clippy -D warnings with --cfg m2v_gpu over the translator, the runtime and validation; api_signatures blessed. (cherry picked from commit b960f51b05a1dac33816b96fd88d04a8565ae02c) --- .../src/indirect_command_buffer.rs | 51 ++++++++++++------- .../src/indirect_commands/host.rs | 41 +++++++++------ .../src/indirect_commands/tests/records.rs | 8 ++- crates/metal2vulkan/tests/api_signatures.tsv | 1 + 4 files changed, 67 insertions(+), 34 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index 21b128ca4..16b7e1f07 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -2978,12 +2978,9 @@ impl IndirectCommandBuffer { let mut faults = ReplayFaults::default(); let mut decoded = Vec::with_capacity(count); for bytes in records { - let bytes: &[u8; COMMAND_BYTES] = bytes.borrow(); - let record = self.written_record(bytes); - if record.is_none() { + if !self.push_written(bytes.borrow(), &mut decoded) { faults = ReplayFaults(faults.0 | ReplayFaults::RECORD.0); } - decoded.push(record.map(|record| (record, None))); } WrittenRecords { faults, @@ -3017,12 +3014,30 @@ impl IndirectCommandBuffer { } } - /// One record's `bytes` as [`written_records`](Self::written_records) decodes them: `None` - /// where they do not decode, or set what the descriptor does not allow. - fn written_record(&self, bytes: &[u8]) -> Option { - CommandRecord::decode(bytes) - .ok() - .filter(|record| self.allows(record)) + /// Push one record's `bytes` onto `decoded` as [`written_records`](Self::written_records) + /// decodes them, decoded in place (`RecordView::record_into`), with no state named: `None`, + /// and false, where they do not decode, or set what the descriptor does not allow. + fn push_written( + &self, + bytes: &[u8], + decoded: &mut Vec)>>, + ) -> bool { + let Ok(view) = RecordView::new(bytes) else { + decoded.push(None); + return false; + }; + decoded.push(Some((CommandRecord::default(), None))); + let Some(slot) = decoded.last_mut() else { + return false; + }; + let allowed = slot.as_mut().is_some_and(|(record, _)| { + view.record_into(record); + self.allows(record) + }); + if !allowed { + *slot = None; + } + allowed } /// The bytes of the records of commands `range` (inside the size) as the GPU and the CPU left @@ -3074,15 +3089,15 @@ impl IndirectCommandBuffer { /// `start`, decoded and checked as [`written_records`](Self::written_records) does, once its /// header holds no fault ([`written_header_fault`](Self::written_header_fault)). pub(super) fn decode_written(&self, bytes: &[u8]) -> WrittenRecords { - let (records, _) = bytes.as_chunks::(); - let records: Vec)>> = (records.iter()) - .map(|record| Some((self.written_record(record)?, None))) - .collect(); + let (read, _) = bytes.as_chunks::(); + let mut records = Vec::with_capacity(read.len()); + let mut faults = ReplayFaults::default(); + for record in read { + if !self.push_written(record, &mut records) { + faults = ReplayFaults::RECORD; + } + } (self.device().shared().counts).indirect_records_read(records.len() as u64); - let faults = match records.iter().any(Option::is_none) { - true => ReplayFaults::RECORD, - false => ReplayFaults::default(), - }; WrittenRecords { faults, records } } diff --git a/crates/metal2vulkan/src/indirect_commands/host.rs b/crates/metal2vulkan/src/indirect_commands/host.rs index 16db70129..94fd41300 100644 --- a/crates/metal2vulkan/src/indirect_commands/host.rs +++ b/crates/metal2vulkan/src/indirect_commands/host.rs @@ -426,25 +426,36 @@ impl<'a> RecordView<'a> { /// The whole record ([`CommandRecord::decode`]). pub fn record(&self) -> CommandRecord { - let mut record = CommandRecord { - action: self.action, - barrier: self.barrier, - bindings: Bindings { - pipeline: self.pipeline(), - ..Default::default() - }, - retained_pipeline: self.retained_pipeline(), - stage_in_region: self.stage_in_region(), - imageblock_size: self.imageblock_size(), - ..Default::default() - }; - for (index, binding) in record.bindings.buffers.iter_mut().flatten().enumerate() { + let mut record = CommandRecord::default(); + self.record_into(&mut record); + record + } + + /// The whole record ([`record`](Self::record)) written over `record`, each of its fields + /// this one's, as `*record = self.record()` leaves it: a consumer that reads many records + /// into storage it holds decodes each in place, with no record made and moved there. + pub fn record_into(&self, record: &mut CommandRecord) { + let CommandRecord { + action, + barrier, + bindings: Bindings { pipeline, buffers }, + threadgroup_memory_lengths, + stage_in_region, + imageblock_size, + retained_pipeline, + } = record; + *action = self.action; + *barrier = self.barrier; + *pipeline = self.pipeline(); + *retained_pipeline = self.retained_pipeline(); + *stage_in_region = self.stage_in_region(); + *imageblock_size = self.imageblock_size(); + for (index, binding) in buffers.iter_mut().flatten().enumerate() { *binding = binding_at(self.bytes, index); } - for (index, length) in record.threadgroup_memory_lengths.iter_mut().enumerate() { + for (index, length) in threadgroup_memory_lengths.iter_mut().enumerate() { *length = self.threadgroup_memory_length(index).unwrap_or(0); } - record } } diff --git a/crates/metal2vulkan/src/indirect_commands/tests/records.rs b/crates/metal2vulkan/src/indirect_commands/tests/records.rs index 667aa0059..58f18dad3 100644 --- a/crates/metal2vulkan/src/indirect_commands/tests/records.rs +++ b/crates/metal2vulkan/src/indirect_commands/tests/records.rs @@ -824,11 +824,12 @@ fn with_field(mut record: CommandRecord, field: RecordField) -> Option Result, RecordError> method metal2vulkan::bind::indirect_commands::RecordView::pipeline pub fn pipeline(&self) -> Option method metal2vulkan::bind::indirect_commands::RecordView::record pub fn record(&self) -> CommandRecord +method metal2vulkan::bind::indirect_commands::RecordView::record_into pub fn record_into(&self, record: &mut CommandRecord) method metal2vulkan::bind::indirect_commands::RecordView::retained_pipeline pub fn retained_pipeline(&self) -> Option method metal2vulkan::bind::indirect_commands::RecordView::stage_in_region pub fn stage_in_region(&self) -> [u32; 6] method metal2vulkan::bind::indirect_commands::RecordView::threadgroup_memory_length pub fn threadgroup_memory_length(&self, index: usize) -> Option