diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 000d41ef6..78106058b 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -1659,6 +1659,17 @@ impl RenderCompiled { Ok(made) } + /// For the device tests: the create flags of each `VkPipeline` the content has made, its main + /// ones first. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn made_flags(&self) -> Vec { + let named = (self.named.as_ref()).and_then(|(_, named)| named.get()); + let made = usize::from(self.primary.get().is_some()) + + usize::from(named.is_some()) + + lock(&self.later).len(); + vec![self.flags; made] + } + /// Whether the content's `VkPipeline`s render into `pass`'s attachments: its colour formats are /// the pass's colour attachments', by index, an absent one where the pass has none, its /// depth and stencil formats the pass's depth and stencil attachments', or none without one, diff --git a/crates/metal2vulkan-runtime/src/tests/draw.rs b/crates/metal2vulkan-runtime/src/tests/draw.rs index ae93f3162..813871c84 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw.rs @@ -3362,15 +3362,18 @@ define internal void @_GLOBAL__sub_I_() section "air.static_init" { }); } - /// Where device creation enabled the attachment feedback loop, every render pipeline takes the - /// loop's colour flag, `VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT`: that of a - /// fragment function that reads no texture as much as that of one that reads its pass's - /// attachment through one, since a state is made before any draw binds its textures. A - /// device without the loop makes them with no flag. Then, through the loop, a pass draws with - /// both after the first reads the attachment, and the second writes it: every pixel is the - /// second's. + /// Where device creation enabled the attachment feedback loop, a render pipeline takes the + /// loop's colour flag, `VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT`, only where + /// a draw reads its pass's attachment through the loop. In an ordinary pass, a state whose + /// fragment function reads no texture and one whose function reads a texture that no + /// attachment of the pass renders draw with their pipelines made with no flag. In a pass that + /// draws blue over its target, then, after a memory barrier, each pixel read plus green, the + /// reading draw makes its state's flagged pipeline at that draw, and a second such pass makes + /// none; every pixel reads blue plus green each time, and each reading draw counts one in + /// `Metrics::attachment_loop_reads`. A device without the loop makes every pipeline with no + /// flag and reads through a copy. #[test] - fn every_device_makes_every_render_pipeline_with_the_loop_s_flag_where_it_has_the_loop() { + fn every_device_flags_a_render_pipeline_for_the_loop_only_where_a_draw_reads_its_attachment() { use ash::vk; let Some(library) = examples() else { return; @@ -3378,73 +3381,102 @@ define internal void @_GLOBAL__sub_I_() section "air.static_init" { let plain = descriptor(&library, "colored_fragment", PixelFormat::RGBA8Unorm); let own = descriptor(&library, "own_pixel_fragment", PixelFormat::RGBA8Unorm); each_device("loop_flags", |name, _, device| { - let looped = device.core().feedback_loop; - let want = match looped { - true => vk::PipelineCreateFlags::COLOR_ATTACHMENT_FEEDBACK_LOOP_EXT, - false => vk::PipelineCreateFlags::empty(), - }; - let plain = state(name, &device, &plain); - let reading = state(name, &device, &own); - for (what, state) in [("reading no texture", &plain), ("reading one", &reading)] { - // Raw: ash's flag types are not `Debug` under the features the runtime takes. - assert_eq!( - state.inner.compiled.flags.as_raw(), - want.as_raw(), - "{name}: a state's pipelines {what}, the loop {looped}" - ); - } - if !looped { - println!("{name}: no feedback loop; every render pipeline made with no flag"); + if !device.core().attachment_reads { + println!("{name}: no attachment reads"); return; } + let looped = device.core().feedback_loop; (device.grant_indices(IndexClass::Texture, 4096, 4)) .unwrap_or_else(|error| panic!("{name}: {error}")); + let plain = state(name, &device, &plain); + let reading = state(name, &device, &own); + // Each state's pipelines made so far: those with no flag, and those with the loop's. + let flags = |state: &RenderPipelineState| { + let made = state.inner.compiled.made_flags(); + let loop_flag = vk::PipelineCreateFlags::COLOR_ATTACHMENT_FEEDBACK_LOOP_EXT; + let flagged = (made.iter()).filter(|f| f.contains(loop_flag)).count(); + (made.len() - flagged, flagged) + }; let mut buffers = Buffers::new(&device); - // Two triangles over the whole target, their colors none and blue. + // Three triangles over the whole target, their colors none, blue and green. let whole = [-1.0, -1.0, 3.0, -1.0, -1.0, 3.0]; - let positions = buffers.of(&device, &floats(&[whole; 2].concat())); - let colors: Vec = [[0.0; 4], BLUE] + let positions = buffers.of(&device, &floats(&[whole; 3].concat())); + let colors: Vec = [[0.0; 4], BLUE, [0.0, 1.0, 0.0, 0.0]] .iter() .flat_map(|c| [*c; 3].concat()) .collect(); let colors = buffers.of(&device, &floats(&colors)); let queue = device.new_command_queue(4).expect("a queue"); let color = readable(&device, PixelFormat::RGBA8Unorm, 1, 4096); + let other = readable(&device, PixelFormat::RGBA8Unorm, 1, 4097); let upload = queue.command_buffer().expect("a command buffer"); (upload.record_for_test(&[], |_, _| {})).expect("a command buffer's commands"); run(name, &upload); let counters = device.counters(); - settled(&device); - let before = counters.metrics(); + let blue = texels(|_, _| BLUE); + // An ordinary pass: the reading state over a texture no attachment renders, then blue. 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_vertex_buffer(Some(&positions), 0, 0)); draw(encoder.set_vertex_buffer(Some(&colors), 0, 1)); - draw(encoder.set_fragment_texture(Some(&color), 0)); + draw(encoder.set_fragment_texture(Some(&other), 0)); draw(encoder.set_render_pipeline_state(&reading)); draw(encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0)); draw(encoder.set_render_pipeline_state(&plain)); draw(encoder.draw_primitives(PrimitiveType::Triangle, 3, 3, 1, 0)); encoder.end_encoding(); run(name, &command_buffer); - let after = counters.metrics(); + assert_eq!(read(&color, 4), blue, "{name}: the ordinary pass's blue"); assert_eq!( - ( + (flags(&plain), flags(&reading)), + ((1, 0), (1, 0)), + "{name}: each state's pipelines with no flag and with the loop's, after an \ + ordinary pass, the loop {looped}" + ); + // Two passes whose reading draw reads the attachment, blue under it. + for pass_index in 0..2 { + settled(&device); + let before = counters.metrics(); + 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}")); + draw(encoder.set_vertex_buffer(Some(&positions), 0, 0)); + draw(encoder.set_vertex_buffer(Some(&colors), 0, 1)); + draw(encoder.set_fragment_texture(Some(&color), 0)); + draw(encoder.set_render_pipeline_state(&plain)); + draw(encoder.draw_primitives(PrimitiveType::Triangle, 3, 3, 1, 0)); + draw(encoder.memory_barrier()); + draw(encoder.set_render_pipeline_state(&reading)); + draw(encoder.draw_primitives(PrimitiveType::Triangle, 6, 3, 1, 0)); + encoder.end_encoding(); + run(name, &command_buffer); + let after = counters.metrics(); + assert_eq!( + read(&color, 4), + texels(|_, _| [0.0, 1.0, 1.0, 1.0]), + "{name} pass {pass_index}: every pixel blue read plus green" + ); + assert_eq!( after.attachment_loop_reads - before.attachment_loop_reads, - after.attachment_read_copies - before.attachment_read_copies + u64::from(looped), + "{name} pass {pass_index}: the reading draw through the loop" + ); + assert_eq!( + (flags(&plain), flags(&reading)), + ((1, 0), (1, usize::from(looped))), + "{name} pass {pass_index}: each state's pipelines with no flag and with the \ + loop's, the loop {looped}" + ); + } + match looped { + true => println!( + "{name}: an ordinary pass's pipelines made with no flag; the reading draw's \ + flagged pipeline made at its first pass's reading draw alone" ), - (1, 0), - "{name}: the reading draw through the loop, no copy" - ); - let want = texels(|_, _| [0.0, 0.0, 1.0, 1.0]); - assert_eq!( - read(&color, 4), - want, - "{name}: every pixel the second draw's blue" - ); - println!("{name}: every render pipeline made with the loop's colour flag"); + false => println!("{name}: no feedback loop; every render pipeline with no flag"), + } }); }