diff --git a/crates/metal2vulkan-runtime/src/tests/draw.rs b/crates/metal2vulkan-runtime/src/tests/draw.rs index ace9635a9..ae93f3162 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw.rs @@ -2039,6 +2039,55 @@ pub(crate) mod on_device { }); } + /// A vertex function reads `[[amplification_count]]` (the owned fixture + /// `engine/vertex_amplification.ll`'s `amp_vertex_store`, its state's rasterization + /// disabled): the runtime makes no state whose `maxVertexAmplificationCount` is above 1, and + /// its render encoder sets no count, so every draw's count is Metal's default 1, the + /// `SpecKey::AmplificationCount` constant the state's creation fixes. Vertex `vid` writes the + /// count and its amplification id plus one, `(1 << 16) | 1`, to word `vid * 8`, and no other + /// word is written. + #[test] + fn every_device_draws_a_vertex_function_that_reads_its_amplification_count() { + let Some(engine) = library(&engine_dir().join("vertex_amplification.ll")) else { + return; + }; + let mut d = RenderPipelineDescriptor::new(StageSource::Function(FunctionDescriptor::new( + (engine.function("amp_vertex_store")).expect("the function"), + ))); + d.color_attachments[0] = Some(ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm)); + d.rasterization_enabled = false; + each_device("draw_amplification_count", |name, _, device| { + let state = state(name, &device, &d); + let mut buffers = Buffers::new(&device); + let color = target( + &device, + PixelFormat::RGBA8Unorm, + TextureUsage::RENDER_TARGET, + ); + let queue = device.new_command_queue(4).expect("a queue"); + let out = buffers.of(&device, &[0u8; 32 * 4]); + 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(&out), 0, 0)); + draw(encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0)); + encoder.end_encoding(); + run(name, &command_buffer); + let contents = out.contents().expect("a Shared buffer"); + // SAFETY: the buffer holds 32 words, which the finished draw wrote and whose writes + // reach the host. + let words = unsafe { std::slice::from_raw_parts(contents.as_ptr().cast::(), 32) }; + let mut want = [0u32; 32]; + for vid in 0..3 { + want[vid * 8] = (1 << 16) | 1; + } + assert_eq!(words, want, "{name}: the words"); + println!("{name}: a vertex function read its amplification count, 1"); + }); + } + /// Each pixel samples its own texel through the constexpr sampler at the consumer's index. #[test] fn every_device_samples_a_texture_through_a_constexpr_sampler_in_a_draw() {