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