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)
This commit is contained in:
Anees Iqbal committed 2026-11-05 06:06:01 -08:00
1 parent 7089c6ddb8
commit 40a1558ff6
4 files changed
+67 -34

No files matched your search

@@ -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> {
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<S>(
&self,
bytes: &[u8],
decoded: &mut Vec<Option<(CommandRecord, Option<S>)>>,
) -> 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<S>(&self, bytes: &[u8]) -> WrittenRecords<S> {
let (records, _) = bytes.as_chunks::<COMMAND_BYTES>();
let records: Vec<Option<(CommandRecord, Option<S>)>> = (records.iter())
.map(|record| Some((self.written_record(record)?, None)))
.collect();
let (read, _) = bytes.as_chunks::<COMMAND_BYTES>();
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 }
}
@@ -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
}
}
@@ -824,11 +824,12 @@ fn with_field(mut record: CommandRecord, field: RecordField) -> Option<CommandRe
/// that decode, as what they decode to; where `encode_into` refuses the record with the field
/// set, or the field's index is past the record's, it refuses too and leaves the bytes as they
/// were. It writes nothing outside the bytes it answers. `RecordView` reads each field as
/// `decode` does.
/// `decode` does, and its `record_into` writes the record over the last round's, every field.
#[test]
fn a_field_written_alone_is_the_record_encoded_again() {
let mut draws = Draws(0x9e37_79b9_7f4a_7c15);
let (mut written, mut refused) = (0, 0);
let mut into = CommandRecord::default();
for round in 0..20_000 {
let mut bytes = vec![0u8; COMMAND_BYTES];
if round % 2 == 0 {
@@ -846,6 +847,11 @@ fn a_field_written_alone_is_the_record_encoded_again() {
let record = CommandRecord::decode(&bytes).unwrap();
let view = RecordView::new(&bytes).unwrap();
assert_eq!(view.record(), record, "round {round}: the view's record");
view.record_into(&mut into);
assert_eq!(
into, record,
"round {round}: the view's record over the last"
);
assert_eq!(view.writes(), record.writes(), "round {round}");
for stage in BufferStage::of(false)
.into_iter()
@@ -8889,6 +8889,7 @@ method metal2vulkan::bind::indirect_commands::RecordView::imageblock_size pub fn
method metal2vulkan::bind::indirect_commands::RecordView::new pub fn new(bytes: &'a [u8]) -> Result<RecordView<'a>, RecordError>
method metal2vulkan::bind::indirect_commands::RecordView::pipeline pub fn pipeline(&self) -> Option<NonZeroU64>
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<NonZeroU64>
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<u32>
Can't render this file because it is too large.