From 3fbaca0b9b44c1e4db9f595ef2e0dfe4e926e3bd Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 14:23:20 -0800 Subject: [PATCH] Translator and rt tests: a control point's attribute on a Constant layout reads its one element (red) A post-tessellation vertex function may read a control-point attribute whose vertex buffer layout has the Constant step function (step rate 0). Metal makes such a pipeline, and on an Apple M3 Max every control point of every patch reads the layout's element 0 at the attribute's offset (an Objective-C probe, Mac job m3-max-80062; the other elements held other values and none was read). That matches Apple's documentation of MTLVertexStepFunctionConstant ("fetches attribute data once and uses that data for every vertex"). The translator refuses the pair, NotImplemented(Tessellation), and a walk met that refusal behind the array-field one. - a_control_point_attribute_on_a_constant_layout_reads_its_one_element (engine_tessellation): tess_triangle_points with its control points' half2 from buffer 2 at offset 4, on a Constant layout, compiles. It pushes no stride of buffer 2 and passes L3. - every_device_reads_a_constant_layout_s_one_element_at_each_control_point (rt, patch): the same draw as every_device_draws_patches_from_their_factors, with that half2 shifting the third control point's x. Element 0's half2 at offset 4 is 0; element e's is e, with 5 at offset 0. So a control point that reads another element, or the element's first bytes, moves the patch and changes the texels. Red: lavapipe (llvmpipe), job m3-max-80108: both refused, "Tessellation; needs NotImplemented at function tess_triangle_points"; the control draw passes. --- .../metal2vulkan-runtime/src/tests/patch.rs | 56 +++++++++++++++++++ .../src/tests/engine_tessellation.rs | 45 +++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/crates/metal2vulkan-runtime/src/tests/patch.rs b/crates/metal2vulkan-runtime/src/tests/patch.rs index 5b36e8d6f..c27b1c4ff 100644 --- a/crates/metal2vulkan-runtime/src/tests/patch.rs +++ b/crates/metal2vulkan-runtime/src/tests/patch.rs @@ -461,6 +461,62 @@ BUFFER_META"#; }); } + /// A control point's attribute on a layout of the `Constant` step function (rate 0) reads the + /// layout's one element at the attribute's offset for every control point of every patch, as + /// Metal reads it (an Apple M3 Max reads element 0 for each control point): [`patches`] with + /// the `half2` that shifts the third control point's x from buffer 2 at offset 4, 8-byte + /// elements. Element 0's `half2` there is 0, and element `e`'s is `e` (`e` up to 5, every + /// control point of both patches), with 5 at offset 0, so a control point that reads another + /// element, or the element's first bytes, moves the first patch off the top half of clip + /// space. The patches draw as [`every_device_draws_patches_from_their_factors`]'s. + #[test] + fn every_device_reads_a_constant_layout_s_one_element_at_each_control_point() { + let Some(libraries) = libraries() else { + return; + }; + let mut d = patches(&libraries); + let v = d.vertex_descriptor.as_mut().expect("a vertex descriptor"); + v.attributes + .insert(1, VertexAttributeDescriptor::new(VertexFormat::Half2, 4, 2)); + let mut constant = VertexBufferLayoutDescriptor::new(Some(8)); + constant.step_function = VertexStepFunction::Constant; + constant.step_rate = 0; + v.layouts.insert(2, constant); + // half 0, 1, 2, 3, 4 and 5. + let halves: [u16; 6] = [0, 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500]; + let shifts: Vec = halves + .iter() + .flat_map(|h| [FIVE.to_le_bytes(), [0; 2], h.to_le_bytes(), [0; 2]].concat()) + .collect(); + each_device("draw_patches_constant_points", |name, _, device| { + let state = state(name, &device, &d); + let mut buffers = Buffers::new(&device); + let (points, tints, factors) = patch_buffers(&device, &mut buffers); + let shifts = buffers.of(&device, &shifts); + let color = target( + &device, + PixelFormat::RGBA8Unorm, + TextureUsage::RENDER_TARGET, + ); + let queue = device.new_command_queue(4).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = (command_buffer.render_command_encoder(&pass(&color, None))) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let draw = |result: Result<(), Error>| result.unwrap_or_else(|e| panic!("{name}: {e}")); + draw(encoder.set_render_pipeline_state(&state)); + draw(encoder.set_vertex_buffer(Some(&points), 0, 0)); + draw(encoder.set_vertex_buffer(Some(&tints), 0, 1)); + draw(encoder.set_vertex_buffer(Some(&shifts), 0, 2)); + draw(encoder.set_tessellation_factor_buffer(Some(&factors), 8, 0)); + draw(encoder.draw_patches(3, 0, 2, None, 1, 0)); + encoder.end_encoding(); + run(name, &command_buffer); + let want = texels(|_, y| if y < 4 { GREEN } else { BLACK }); + assert_eq!(read(&color, 4), want, "{name}: the texels"); + println!("{name}: every control point read the Constant layout's one element"); + }); + } + /// `drawPatches:patchIndexBuffer:patchIndexBufferOffset:indirectBuffer:indirectBufferOffset:` /// reads `MTLDrawPatchIndirectArguments` (two patches from patch 0, one instance, base /// instance 0), 4 bytes into its buffer, as `vkCmdDrawIndirect`'s command, the patch count diff --git a/crates/metal2vulkan/src/tests/engine_tessellation.rs b/crates/metal2vulkan/src/tests/engine_tessellation.rs index 7e5d460d5..70abb84df 100644 --- a/crates/metal2vulkan/src/tests/engine_tessellation.rs +++ b/crates/metal2vulkan/src/tests/engine_tessellation.rs @@ -320,6 +320,51 @@ fn control_points_and_patch_inputs_are_pulled() { } } +/// A control point's attribute on a layout of the `Constant` step function (rate 0) reads the +/// layout's one element, at the attribute's offset, for every control point of every patch, as +/// Metal reads it (an Apple M3 Max reads element 0 for each control point): `tess_triangle_points` +/// with the control points' `half2 uv` from buffer 2 at offset 4 compiles, pulls it with no stride +/// of buffer 2 (none is pushed or depended on), and passes L3. +#[test] +fn a_control_point_attribute_on_a_constant_layout_reads_its_one_element() { + let Some(bc) = fixture() else { return }; + let mut d = descriptor(); + d.attributes + .insert(1, VertexAttributeDescriptor::new(VertexFormat::Half2, 4, 2)); + let mut constant = VertexBufferLayoutDescriptor::new(Some(8)); + constant.step_function = VertexStepFunction::Constant; + constant.step_rate = 0; + d.layouts.insert(2, constant); + let classes = BTreeMap::from([ + (0, IoNumeric::Float), + (1, IoNumeric::Float), + (2, IoNumeric::Uint), + (5, IoNumeric::Float), + ]); + let proj = patch_fetch_projection(Some(&d), &classes).expect("projection"); + let (with, _) = profiles(); + for (stem, p) in &with { + let a = compile(&bc, "tess_triangle_points", p, Some(proj.clone())) + .unwrap_or_else(|e| panic!("{stem}: {e}")); + let pulled: Vec = a.interface.io.pulled.iter().map(|x| x.index).collect(); + assert_eq!(pulled, [0, 1, 5], "{stem}"); + let strides = |buffer| { + ( + a.interface + .push + .fields + .iter() + .any(|f| f.field == PushField::VertexBufferStride(buffer)), + a.facts + .contains(&RuntimeFact::VertexBufferStride { layout: buffer }), + ) + }; + assert_eq!(strides(0), (true, true), "{stem}: buffer 0"); + assert_eq!(strides(2), (false, false), "{stem}: buffer 2"); + link_and_validate(&a, p).unwrap_or_else(|e| panic!("{stem}: {e}")); + } +} + /// A post-tessellation function's pulled decode records the notes a vertex function's and a /// kernel's stage-in decode record, under the same rule (`fetch::decode_notes`): the per-patch /// `float4 tint` given a `Half4` format records `PrecisionNote::DeviceNanEncoding` (Metal widens