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