diff --git a/crates/metal2vulkan-validation/src/candidate/platform/runtime_multisample.rs b/crates/metal2vulkan-validation/src/candidate/platform/runtime_multisample.rs index ad3f21423..8c5744e3a 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/runtime_multisample.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/runtime_multisample.rs @@ -3,8 +3,9 @@ //! neither does the runtime, so each slice is filled by one draw of the owned `sample_fill` //! fixture (`fixtures/public/engine/sample_fill.metal`) whose fragment runs once per sample //! (`sample_id`) and writes that sample's texel from a buffer: a color texel through an -//! unsigned-integer view of its texel size (the texture made with `PixelFormatView` usage), so -//! its bits cross unconverted, and a `Depth32Float` one as `depth(any)` under an always-passing +//! unsigned-integer view of its texel size (the texture made with `PixelFormatView` usage, +//! [`color_view`], which takes a texture index of the run's grant, as shaders reach it), so its +//! bits cross unconverted, and a `Depth32Float` one as `depth(any)` under an always-passing //! depth test that writes. A depth outside `[0, 1]` is refused, typed, as `runtime_depth` //! refuses it. A stencil aspect holds one value in every sample (`uniform_stencil`), which the //! case's pass clears it to. After the work, a dispatch of the fixture's kernels reads every @@ -36,8 +37,10 @@ const SAMPLE_FILL: &str = include_str!("../../../fixtures/public/engine/sample_f /// One slice of a multisampled texture and the texels it is filled with, read as `read` (a color /// view or depth): every sample of each pixel, pixels row-major, as the case's bytes lay them out. +/// A color texture renders through `view`, its [`color_view`]; a depth one has none. pub(super) struct Fill { pub(super) texture: rt::Texture, + pub(super) view: Option, pub(super) slice: u32, pub(super) read: SampleRead, pub(super) texels: Vec, @@ -145,11 +148,9 @@ impl Filling { let format = view_format(reading); descriptor.fragment = Some(stage(library, "sample_fill_color")?); descriptor.color_attachments[0] = Some(ColorAttachmentDescriptor::new(format)); - let slices = made.array_length.max(1); - let view = fill - .texture - .new_texture_view(format, made.texture_type, 0..1, 0..slices, None) - .map_err(|e| error("a multisampled fill's view", e))?; + let view = fill.view.clone().ok_or_else(|| { + ExecutorError::Failed("a multisampled color fill has no view".into()) + })?; (widened(&fill.texels, reading.texel_bytes())?, Some(view)) } SampleRead::Depth => { @@ -180,15 +181,33 @@ impl Filling { } } +/// The unsigned-integer view of the multisampled color `texture` that its fill renders through and +/// its read reads, of `read`'s texel size, at texture index `at` of the run's grant: shaders reach +/// it, so it takes an index (`Refusal::TextureIndex`). +pub(super) fn color_view( + texture: &rt::Texture, + read: ReadbackView, + at: u32, +) -> Result { + let made = texture.descriptor(); + texture.new_texture_view( + view_format(read), + made.texture_type, + 0..1, + 0..made.array_length.max(1), + Some(at), + ) +} + /// Every sample of the 2D multisampled `texture`, read as `read`, in the attachment layout. A -/// color texture is read through a view made at `view_index` of the run's grant; a depth or -/// stencil one is bound itself, at its own index (`view_index` then `None`). +/// color texture is read through `view`, its [`color_view`]; a depth or stencil one is bound +/// itself, at its own index (`view` then `None`). pub(super) fn read_back( device: &rt::Device, device_type: DeviceType, texture: &rt::Texture, read: SampleRead, - view_index: Option, + view: Option<&rt::Texture>, error: &impl Fn(&str, rt::Error) -> ExecutorError, ) -> Result, ExecutorError> { let made = texture.descriptor(); @@ -204,19 +223,14 @@ pub(super) fn read_back( let state = runtime::made_within(|numbered| { device.new_compute_pipeline_state(&descriptor, |samplers| numbered(samplers)) })?; - let view = match read { - SampleRead::Color(view) => Some( - texture - .new_texture_view( - view_format(view), - made.texture_type, - 0..1, - 0..made.array_length.max(1), - view_index, - ) - .map_err(|e| error("the multisampled read's view", e))?, - ), - SampleRead::Depth | SampleRead::Stencil => None, + let bound = match (read, view) { + (SampleRead::Color(_), Some(view)) => view, + (SampleRead::Color(_), None) => { + return Err(ExecutorError::Failed( + "a multisampled color read has no view".into(), + )) + } + (SampleRead::Depth | SampleRead::Stencil, _) => texture, }; let length = readback_bytes(dimensions, samples, read).map_err(ExecutorError::Failed)?; let words = Placed::new(device, "the multisampled read's words", &vec![0; length])?; @@ -239,7 +253,7 @@ pub(super) fn read_back( .set_compute_pipeline_state(&state) .map_err(fail("the multisampled read's pipeline state"))?; encoder - .set_texture(Some(view.as_ref().unwrap_or(texture)), 0) + .set_texture(Some(bound), 0) .map_err(fail("the multisampled read's texture"))?; encoder .set_buffer(Some(&words.buffer), 0, 0) @@ -288,7 +302,7 @@ fn shape(width: u32, samples: u32, words: u32) -> Vec { /// The `uint4` each sample's texel of `texel` bytes is to the fill's fragment: its little-endian /// words, the last zero-extended, then zeros. fn widened(texels: &[u8], texel: usize) -> Result, ExecutorError> { - if texel == 0 || texels.len() % texel != 0 { + if texel == 0 || !texels.len().is_multiple_of(texel) { return Err(ExecutorError::Failed(format!( "a multisampled fill of {} bytes is no whole number of {texel}-byte texels", texels.len() diff --git a/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs b/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs index 6cc8fd9dc..97e34b92b 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/runtime_render.rs @@ -394,12 +394,12 @@ impl Stages { } /// The pass's color attachments, by Metal index, and its depth/stencil attachment's aspect; in a -/// pass of more samples, the texture index each color attachment's per-sample read binds its view -/// at. +/// pass of more samples, each color attachment's unsigned-integer view, which its fill renders +/// through and its per-sample read reads (`runtime_multisample::color_view`). struct Targets { textures: BTreeMap, depth_stencil: Option, - views: BTreeMap, + views: BTreeMap, _grant: Option, } @@ -486,13 +486,23 @@ impl Targets { (index, next()?), )?; if target.samples > 1 { + let read = sample_read(target.format).map_err(ExecutorError::Failed)?; + let SampleRead::Color(reading) = read else { + return Err(ExecutorError::Failed(format!( + "color target {index} reads back as no color" + ))); + }; + let view = runtime_multisample::color_view(&texture, reading, next()?).map_err( + |error| runtime_error(&format!("color target {index}'s view"), error), + )?; fills.push(runtime_multisample::Fill { texture: texture.clone(), + view: Some(view.clone()), slice: 0, - read: sample_read(target.format).map_err(ExecutorError::Failed)?, + read, texels: target.bytes.clone(), }); - views.insert(index, next()?); + views.insert(index, view); textures.insert(index, texture); continue; } @@ -623,7 +633,7 @@ impl Targets { let texture = self.textures.get(&index).ok_or_else(|| { ExecutorError::Failed(format!("color target {index} is not made")) })?; - if let Some(&view) = self.views.get(&index) { + if let Some(view) = self.views.get(&index) { let read = sample_read(authored.format).map_err(ExecutorError::Failed)?; let bytes = runtime_multisample::read_back( device, @@ -710,6 +720,7 @@ impl DepthStencil { _ => { let fill = runtime_multisample::Fill { texture: made.texture.clone(), + view: None, slice: 0, read: SampleRead::Depth, texels: depths.clone(), diff --git a/crates/metal2vulkan-validation/src/candidate/platform/runtime_textures.rs b/crates/metal2vulkan-validation/src/candidate/platform/runtime_textures.rs index e536ccb15..76f865a46 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/runtime_textures.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/runtime_textures.rs @@ -23,7 +23,7 @@ use crate::candidate::engine::argument_buffers::FieldKey; use crate::candidate::engine::{resources, ComputeCase, RuntimeKernel}; use crate::case::{ArgumentBufferSamplerResource, SamplerResource, TextureFormat}; use crate::check::admission::ExecutorError; -use crate::executor_contract::multisample::sample_read; +use crate::executor_contract::multisample::{sample_read, SampleRead}; use crate::gpu::DeviceType; use crate::literal::LiteralTexture; use metal2vulkan::metal::TextureUsage; @@ -148,11 +148,22 @@ pub(super) fn make( indices: &mut Indices, error: impl Fn(&str, rt::Error) -> ExecutorError, ) -> Result { - let count = wanted.textures.len() + wanted.members.len(); + // A multisampled color texture's fill renders through a view, which takes an index too. + let views = wanted + .textures + .values() + .chain(wanted.members.values()) + .filter(|texture| { + texture.sample_count > 1 + && matches!(sample_read(texture.format), Ok(SampleRead::Color(_))) + }) + .count(); + let count = wanted.textures.len() + wanted.members.len() + views; let (grant, mut at) = texture_indices(device, indices, count, &error)?; let mut fills = Fills::default(); let mut textures = BTreeMap::new(); - for ((&binding, texture), index) in wanted.textures.iter().zip(&mut at) { + for (&binding, texture) in wanted.textures.iter() { + let index = next_index(&mut at)?; let usage = wanted .texture_usages .get(&binding) @@ -162,18 +173,19 @@ pub(super) fn make( })?; textures.insert( binding, - made_texture(device, texture, usage, index, &error, &mut fills)?, + made_texture(device, texture, usage, index, &mut at, &error, &mut fills)?, ); } let mut members = BTreeMap::new(); - for ((&key, texture), index) in wanted.members.iter().zip(&mut at) { + for (&key, texture) in wanted.members.iter() { + let index = next_index(&mut at)?; let usage = wanted.member_usages.get(&key).copied().ok_or_else(|| { ExecutorError::Failed(format!( "argument-buffer texture {}+{} has no declared usage", key.0, key.1 )) })?; - let made = made_texture(device, texture, usage, index, &error, &mut fills)?; + let made = made_texture(device, texture, usage, index, &mut at, &error, &mut fills)?; members.insert(key, made); } if !fills.depths.is_empty() { @@ -212,13 +224,21 @@ struct Fills { samples: Vec, } +/// The next texture index of the run's grant `at`. +fn next_index(at: &mut std::ops::Range) -> Result { + at.next() + .ok_or_else(|| ExecutorError::Failed("the run's textures outnumber their indices".into())) +} + /// `texture` made with `usage` at `index`, every level's slices written from its bytes; a 32-bit -/// float depth's and a multisampled texture's are pushed to `fills` instead, which the run renders. +/// float depth's and a multisampled texture's are pushed to `fills` instead, which the run renders, +/// a multisampled color one through its view at the next of `views`. fn made_texture( device: &rt::Device, texture: &LiteralTexture, usage: rt::TextureUsage, index: u32, + views: &mut std::ops::Range, error: &impl Fn(&str, rt::Error) -> ExecutorError, fills: &mut Fills, ) -> Result { @@ -255,6 +275,13 @@ fn made_texture( .map_err(|e| error(&what, e))?; if multisampled { let read = sample_read(texture.format).map_err(ExecutorError::Failed)?; + let view = match read { + SampleRead::Color(reading) => Some( + runtime_multisample::color_view(&made, reading, next_index(views)?) + .map_err(|e| error(&format!("{what}'s view"), e))?, + ), + SampleRead::Depth | SampleRead::Stencil => None, + }; let slice = layout.width as usize * layout.height as usize * layout.sample_count as usize @@ -269,6 +296,7 @@ fn made_texture( for (at, texels) in texture.bytes.chunks_exact(slice).take(slices).enumerate() { fills.samples.push(runtime_multisample::Fill { texture: made.clone(), + view: view.clone(), slice: at as u32, read, texels: texels.to_vec(), diff --git a/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs b/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs index 1c843d2d1..e23ab3f0b 100644 --- a/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs +++ b/crates/metal2vulkan-validation/src/candidate/tests/engine_render.rs @@ -481,6 +481,30 @@ fn each_sample_of_a_multisampled_target_holds_its_own_invocation() { dimensions: [1, 1], }; let (case, companion) = with_companion(&authored, &fragment); + // Each sample's `(sample, 0, 1)`, after its coverage word. + let indices = |output: &[u8]| -> Vec<[u32; 3]> { + let words: Vec = output + .as_chunks::<4>() + .0 + .iter() + .map(|word| u32::from_le_bytes(*word)) + .collect(); + words + .as_chunks::<4>() + .0 + .iter() + .map(|sample| [sample[1], sample[2], sample[3]]) + .collect() + }; + let expected = [[0, 0, 1], [1, 0, 1], [2, 0, 1], [3, 0, 1]]; + #[cfg(target_os = "linux")] + { + assert!(crate::candidate::engine::runs_render_through_runtime( + &authored, &case + )); + let through = run_on_runtime(&case, &fragment, Some(&companion)).unwrap(); + assert_eq!(indices(&through.output), expected, "through the runtime"); + } let run = run( &case, RenderLibraries { @@ -489,24 +513,11 @@ fn each_sample_of_a_multisampled_target_holds_its_own_invocation() { }, ) .unwrap(); - let words: Vec = run - .output - .chunks_exact(4) - .map(|word| u32::from_le_bytes(word.try_into().unwrap())) - .collect(); - let indices: Vec<[u32; 3]> = words - .chunks_exact(4) - .map(|sample| [sample[1], sample[2], sample[3]]) - .collect(); - assert_eq!(indices, [[0, 0, 1], [1, 0, 1], [2, 0, 1], [3, 0, 1]]); - #[cfg(target_os = "linux")] - { - assert!(crate::candidate::engine::runs_render_through_runtime( - &authored, &case - )); - let through = run_on_runtime(&case, &fragment, Some(&companion)).unwrap(); - assert_eq!(through.output, run.output, "through the runtime"); - } + assert_eq!( + indices(&run.output), + expected, + "on the executor's own context" + ); } /// A tessellation case drawn through the render executor's patch path: the factor buffer bound