From 1f81d53c306ade6de3eb4de5cc8482ff06a1dde8 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 11:03:24 -0800 Subject: [PATCH] Player: a compute pipeline record's stage input plays as the kernel's The player refused every compute pipeline record whose stageInput was not nil ("a stage input"), so a recording that makes a kernel with a [[stage_in]] parameter stopped at that state's frame. The runtime takes the stage input (ComputePipelineDescriptor::stage_input); only the player lacked it. translator::stage_input reads the record's stageInput, {layouts, attributes, indexType, indexBufferIndex}, into the translator's StageInputDescriptor the way vertex_descriptor reads a vertex descriptor: layouts and attributes at their indices, a stride of 2^64 - 1 as MTLBufferLayoutStrideDynamic, Metal's raw step functions, attribute formats and index type through their enums, and Metal's unset layout for a buffer an attribute reads that no layout names. Nil stays no stage input. A raw value the enums lack, or a key the form does not hold, is the player's refusal. A descriptor the runtime refuses is the runtime's typed error: a gap or a translation refusal counts as a skipped compute state in the play's report, any other error ends the play with it. docs/REPLAY-STREAM.md states the form. Test: a_compute_pipeline_record_s_stage_input_is_read (the record form, a dynamic stride, a buffer no layout names, nil, and an unknown key). Verified on msi: clippy -D warnings over the workspace with all features, and the validation suite, the new test among them. --- .../src/replay/new_runtime/mod.rs | 4 +- .../src/replay/tests.rs | 87 +++++++++++++++++++ .../src/replay/translator.rs | 57 +++++++++++- docs/REPLAY-STREAM.md | 6 +- 4 files changed, 147 insertions(+), 7 deletions(-) diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index a2b018f3f..37414ce71 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -1929,9 +1929,6 @@ impl NewRuntime { )?; let function = self.function_of(call, id)?; let kernel = function.descriptor.with_linked(linked); - if f.get("stageInput").is_some() { - return Err(refuse(call, "a stage input")); - } if let Some(libraries) = f.fields("libraries", &["preloaded", "loaded"])? { for key in ["preloaded", "loaded"] { if !libraries.list(key)?.unwrap_or_default().is_empty() { @@ -1941,6 +1938,7 @@ impl NewRuntime { } f.only("openClMode", Fields::bool, false)?; let mut d = rt::ComputePipelineDescriptor::new(rt::StageSource::Function(kernel)); + d.stage_input = translator::stage_input(call, &f)?; if let Some(multiple) = f.bool("threadgroupSizeIsMultipleOfExecutionWidth")? { d.threadgroup_size_is_multiple_of_execution_width = multiple; } diff --git a/crates/metal2vulkan-validation/src/replay/tests.rs b/crates/metal2vulkan-validation/src/replay/tests.rs index 397e7749b..0299c7fab 100644 --- a/crates/metal2vulkan-validation/src/replay/tests.rs +++ b/crates/metal2vulkan-validation/src/replay/tests.rs @@ -319,6 +319,93 @@ fn a_render_pipeline_record_s_tessellation_state_and_depths_are_read() { assert!(matches!(read(&outer), Err(PlayError::Refused { .. }))); } +/// A compute pipeline record's stage input is the kernel's: its layouts and attributes at their +/// indices, a dynamic stride as none, Metal's unset layout for a buffer an attribute reads that +/// no layout names, and the index type and buffer; nil is no stage input, and a key the form +/// does not hold is refused. +#[test] +fn a_compute_pipeline_record_s_stage_input_is_read() { + use super::args::Fields; + use super::translator::stage_input; + use metal2vulkan::metal::{AttributeFormat, IndexType, StepFunction}; + use metal2vulkan::pipeline::{ + StageInputAttributeDescriptor, StageInputDescriptor, StageInputLayoutDescriptor, + }; + let call = |stage_input: Value| Call { + selector: "newComputePipelineStateWithDescriptor:options:reflection:error:".into(), + receiver: 1, + result: 2, + args: vec![map(&[("label", Value::Nil), ("stageInput", stage_input)])], + answers: Vec::new(), + }; + let read = |call: &Call| -> Result, PlayError> { + stage_input(call, &Fields::arg(call, 0, &["label", "stageInput"])?) + }; + let layout = |buffer: u64, stride: u64, step: u64| { + map(&[ + ("bufferIndex", Value::Uint(buffer)), + ("stride", Value::Uint(stride)), + ("stepFunction", Value::Uint(step)), + ("stepRate", Value::Uint(1)), + ]) + }; + let attribute = |index: u64, format: u64, offset: u64, buffer: u64| { + map(&[ + ("attributeIndex", Value::Uint(index)), + ("format", Value::Uint(format)), + ("offset", Value::Uint(offset)), + ("bufferIndex", Value::Uint(buffer)), + ]) + }; + let form = |layouts: Vec, attributes: Vec, extra: Option<(&str, Value)>| { + let mut keys = vec![ + ("layouts", Value::List(layouts)), + ("attributes", Value::List(attributes)), + ("indexType", Value::Uint(1)), + ("indexBufferIndex", Value::Uint(3)), + ]; + keys.extend(extra); + map(&keys) + }; + // A half3 at buffer 1 (stride 8) and a float3 at buffer 10 (dynamic stride), each a thread + // position in the grid's x; attribute 2 reads buffer 4, which no layout names. + let recorded = call(form( + vec![layout(1, 8, 5), layout(10, u64::MAX, 5)], + vec![ + attribute(0, 26, 0, 1), + attribute(1, 30, 4, 10), + attribute(2, 30, 0, 4), + ], + None, + )); + let mut expected = StageInputDescriptor::new(); + let mut eight = StageInputLayoutDescriptor::new(Some(8)); + eight.step_function = StepFunction::ThreadPositionInGridX; + let mut dynamic = StageInputLayoutDescriptor::new(None); + dynamic.step_function = StepFunction::ThreadPositionInGridX; + expected.layouts.insert(1, eight); + expected.layouts.insert(10, dynamic); + expected + .layouts + .insert(4, StageInputLayoutDescriptor::new(Some(0))); + let half3 = StageInputAttributeDescriptor::new(AttributeFormat::Half3, 0, 1); + expected.attributes.insert(0, half3); + let float3 = StageInputAttributeDescriptor::new(AttributeFormat::Float3, 4, 10); + expected.attributes.insert(1, float3); + let unlaid = StageInputAttributeDescriptor::new(AttributeFormat::Float3, 0, 4); + expected.attributes.insert(2, unlaid); + expected.index_type = IndexType::UInt32; + expected.index_buffer_index = 3; + assert_eq!(read(&recorded).unwrap(), Some(expected)); + assert_eq!(read(&call(Value::Nil)).unwrap(), None); + let extra = call(form( + Vec::new(), + Vec::new(), + Some(("stepRate", Value::Uint(1))), + )); + assert!(matches!(read(&extra), Err(PlayError::Refused { .. }))); +} + /// The header's flags, the same in every segment, are the stream's; a segment with another bit, /// or with flags unlike the first segment's, is refused. #[test] diff --git a/crates/metal2vulkan-validation/src/replay/translator.rs b/crates/metal2vulkan-validation/src/replay/translator.rs index 60cff4c42..11a982c0b 100644 --- a/crates/metal2vulkan-validation/src/replay/translator.rs +++ b/crates/metal2vulkan-validation/src/replay/translator.rs @@ -1,6 +1,7 @@ //! What a call describes in the translator's types, which every backend takes: a sampler -//! descriptor, function constant values, a depth-stencil descriptor and a render pipeline -//! descriptor; and the keys of a render pass descriptor's setter records. +//! descriptor, function constant values, a depth-stencil descriptor, a render pipeline +//! descriptor and a compute pipeline's stage input; and the keys of a render pass descriptor's +//! setter records. use super::args::{metal, narrow, refuse, Fields}; use super::player::PlayError; @@ -11,7 +12,8 @@ use metal2vulkan::library::{FunctionConstantValue, FunctionConstantValues}; use metal2vulkan::metal::state::{DepthStencilDescriptor, StencilDescriptor}; use metal2vulkan::metal::PixelFormat; use metal2vulkan::pipeline::{ - ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource, TessellationDescriptor, + ColorAttachmentDescriptor, RenderPipelineDescriptor, StageInputAttributeDescriptor, + StageInputDescriptor, StageInputLayoutDescriptor, StageSource, TessellationDescriptor, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, VertexDescriptor, }; @@ -245,6 +247,55 @@ fn vertex_descriptor(call: &Call, f: &Fields) -> Result Result, PlayError> { + let known = ["layouts", "attributes", "indexType", "indexBufferIndex"]; + let Some(s) = f.fields("stageInput", &known)? else { + return Ok(None); + }; + let mut out = StageInputDescriptor::new(); + for layout in s.list("layouts")?.unwrap_or_default() { + let known = ["bufferIndex", "stride", "stepFunction", "stepRate"]; + let l = Fields::of(call, layout, &known)?; + let stride = match l.required("stride", Fields::uint)? { + DYNAMIC_STRIDE => None, + stride => Some(narrow(call, stride, "stride")?), + }; + let mut made = StageInputLayoutDescriptor::new(stride); + made.step_function = l.required("stepFunction", Fields::metal)?; + made.step_rate = l.required("stepRate", Fields::count)?; + out.layouts + .insert(l.required("bufferIndex", Fields::count)?, made); + } + for attribute in s.list("attributes")?.unwrap_or_default() { + let known = ["attributeIndex", "format", "offset", "bufferIndex"]; + let a = Fields::of(call, attribute, &known)?; + let made = StageInputAttributeDescriptor::new( + a.required("format", Fields::metal)?, + a.required("offset", Fields::count)?, + a.required("bufferIndex", Fields::count)?, + ); + out.attributes + .insert(a.required("attributeIndex", Fields::count)?, made); + } + let read: Vec = out.attributes.values().map(|a| a.buffer_index).collect(); + for buffer in read { + out.layouts + .entry(buffer) + .or_insert_with(|| StageInputLayoutDescriptor::new(Some(0))); + } + out.index_type = s.required("indexType", Fields::metal)?; + out.index_buffer_index = s.required("indexBufferIndex", Fields::count)?; + Ok(Some(out)) +} + const PRIVATE_KEYS: [&str; 7] = [ "openGlModeEnabled", "sampleMask", diff --git a/docs/REPLAY-STREAM.md b/docs/REPLAY-STREAM.md index 49a6f23ef..015413bdf 100644 --- a/docs/REPLAY-STREAM.md +++ b/docs/REPLAY-STREAM.md @@ -457,7 +457,11 @@ and the (name id, value) answer pairs. - `newComputePipelineStateWithDescriptor:options:reflection:error:`: `{label, function, threadgroupSizeIsMultipleOfExecutionWidth, maxTotalThreadsPerThreadgroup, maxCallStackDepth, supportIndirectCommandBuffers, stageInput, linked, libraries: {preloaded, loaded}, - openClMode}`. + openClMode}`. `stageInput` is nil where the descriptor has none, or `{layouts, attributes, + indexType, indexBufferIndex}`: the layouts and attributes in the vertex descriptor's form, + Metal's raw step functions and attribute formats, and the index type and index buffer as + Metal's `MTLStageInputOutputDescriptor` holds them. A player gives it to the pipeline; an + attribute's buffer that no layout names takes Metal's unset layout. - **Keys that are not Metal's property names:** | key | Metal's |