From ef2a4e9da8bd5819bd586cdf03420fd72fed8c7b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 4 Nov 2026 23:13:46 -0800 Subject: [PATCH] Check in-place YCbCr gathers against Metal's rows and the lane's model Evidence for the in-place gather: a fixture, four conformance cases and a device lane test. - fixtures/public/engine/ycg_sample_gather.metal (ours, compiled by scripts/regen-air-fixtures): `ycg_float` and `ycg_half` sample, gather red or gather blue as lane `i % 3` asks, through one runtime sampler, the shape whose gather used to keep the slot off the in-place form. - conformance ycbcr_gathers: `ycbcr-gathers-{edge,zero,white,repeat}`, one linear sampler state each, over a 420v and an x420 surface of 12 x 6 noise texels, float and half, at positions a third of a texel apart from a texel before the texture to one past it (texel centres included, where a gather's footprint turns). Recorded on Metal and replayed on the runtime in a following commit. - runtime_lane::ycbcr_in_place: on a device whose memory is the host's, a dispatch of `ycg_float` binds the planes and converts nothing, its samples are Metal's filter and its gathers Metal's texels at the four taps (the lane's model), and no filter notice is announced. The golden tables and the compile census gain the fixture's rows when they are re-blessed on development. (cherry picked from commit a561a4b56456c0d1d4dc4ca419dc9cb6cfe1f3f7) --- crates/metal2vulkan-conformance/src/cases.rs | 2 + .../src/cases/ycbcr_gathers.rs | 382 ++++++++++++++++++ .../fixtures/public/engine/TOOLCHAIN | 1 + .../public/engine/ycg_sample_gather.air.ll | 153 +++++++ .../public/engine/ycg_sample_gather.ll | 150 +++++++ .../public/engine/ycg_sample_gather.metal | 50 +++ .../src/runtime_lane/ycbcr_in_place.rs | 120 +++++- 7 files changed, 857 insertions(+), 1 deletion(-) create mode 100644 crates/metal2vulkan-conformance/src/cases/ycbcr_gathers.rs create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.air.ll create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.ll create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.metal diff --git a/crates/metal2vulkan-conformance/src/cases.rs b/crates/metal2vulkan-conformance/src/cases.rs index 027686067..352b2f884 100644 --- a/crates/metal2vulkan-conformance/src/cases.rs +++ b/crates/metal2vulkan-conformance/src/cases.rs @@ -193,6 +193,7 @@ pub mod visibility; pub mod visible_tables; pub mod wide_int_attributes; pub mod wide_points; +pub mod ycbcr_gathers; pub mod ycbcr_minify; pub mod ycbcr_textures; @@ -478,6 +479,7 @@ pub fn all() -> Vec> { for case in ycbcr_minify::all() { cases.push(Box::new(case)); } + cases.extend(ycbcr_gathers::all()); cases.extend(compute_threadgroups::all()); cases.extend(buffer_blits::all()); cases.extend(blit_encoder_commands::all()); diff --git a/crates/metal2vulkan-conformance/src/cases/ycbcr_gathers.rs b/crates/metal2vulkan-conformance/src/cases/ycbcr_gathers.rs new file mode 100644 index 000000000..4ac6e9a39 --- /dev/null +++ b/crates/metal2vulkan-conformance/src/cases/ycbcr_gathers.rs @@ -0,0 +1,382 @@ +//! Two-plane YCbCr textures sampled and gathered by one function: Metal's filter and Metal's taps +//! of the texels it reads. +//! +//! A function that both samples and gathers a `YCBCR8_420_2P` or `YCBCR10_420_2P` texture through +//! one runtime sampler is the shape of a probe that asks each lane for a sample or a gather: the +//! fixture `ycg_sample_gather`, whose lane `i % 3` is a sample, a gather of red or a gather of +//! blue at position `i / 3`. Where the device's memory is the host's, the runtime reads such a +//! texture's planes in place (`metal2vulkan`'s `lower::sampler::in_place`), and that form gathers +//! as well as samples, so the slot keeps it: a sample is Metal's filter of the texels Metal reads +//! and a gather is those texels at the footprint's four taps. Elsewhere the slot binds the +//! texture's image of converted texels, which a gather reads exactly and a filter filters as the +//! device's sampler does. +//! +//! Each case is one sampler state, linear (a gather follows no filter), addressed by one mode on +//! both axes: clamp to the edge, clamp to zero, the opaque-white border, and repeat (which a 4:2:0 +//! texture samples as clamp to the edge). Each runs the `float` and the `half` kernel over a `420v` +//! surface and an `x420` one, 12 by 6 texels of noise codes, at positions a third of a texel apart +//! from a texel before the texture to a texel past it, the texel centres among them, where a +//! gather's footprint turns. +//! +//! # Why it cannot fault a GPU +//! +//! Two 576-byte surfaces, one sampler and four dispatches of 3,024 threads each, with no loop. + +use crate::allowance::Allowance; +use crate::case::{ + made, Abort, Bindings, Case, Failure, Input, MetalSource, PlaneTexture, SamplerSpec, Side, +}; +use crate::fixture::{Fixture, METAL_TEXT, RUNTIME_TEXT}; +use crate::recording::ShaderRef; +use crate::transcript::{ByteLayout, Digest, Element, Gaps, Node, Observed, Region, Sites, Value}; +use metal2vulkan::metal::{ + PixelFormat, SamplerAddressMode, SamplerBorderColor, SamplerMinMagFilter, +}; + +/// The fixture whose kernels sample and gather, and their entries: `float4`s and `half4`s. +const FIXTURE: &str = "ycg_sample_gather"; +const ENTRIES: [(&str, &str, Element); 2] = [ + ("ycg_float", "float", Element::F32), + ("ycg_half", "half", Element::F16), +]; + +/// The texture's extent in texels: even, so both planes' rows hold whole pairs. +const W: u64 = 12; +const H: u64 = 6; +/// The bytes between rows of either plane. +const ROW: u64 = 64; +/// The chroma plane's first byte. +const CHROMA_AT: u64 = ROW * H; +/// The surface buffer's length: the chroma plane's rows end it. +const LENGTH: usize = (CHROMA_AT + ROW * H / 2) as usize; + +/// The surfaces: their OSType, their format, and the seed of their codes. +const SURFACES: [([u8; 4], PixelFormat, u64); 2] = [ + (*b"420v", PixelFormat::YCBCR84202P, 3), + (*b"x420", PixelFormat::YCBCR104202P, 4), +]; + +/// Positions a third of a texel apart on each axis, from a texel before the texture to a texel +/// past it. +const COLUMNS: u64 = 3 * (W + 2); +const ROWS: u64 = 3 * (H + 2); +const POSITIONS: u64 = COLUMNS * ROWS; + +/// The lanes of one position: a sample, a gather of red, a gather of blue. +const LANES: u64 = 3; + +/// The threads in a threadgroup of each dispatch. +const GROUP: u32 = 64; + +/// The byte the read-back buffers hold before the dispatches. +const BLANK: u8 = 0xab; + +/// One case: the sampler's address mode on both axes. +pub struct YCbCrGathers { + name: &'static str, + mode: SamplerAddressMode, +} + +/// Every case of the family. +pub fn all() -> Vec> { + [ + ("ycbcr-gathers-edge", SamplerAddressMode::ClampToEdge), + ("ycbcr-gathers-zero", SamplerAddressMode::ClampToZero), + ( + "ycbcr-gathers-white", + SamplerAddressMode::ClampToBorderColor, + ), + ("ycbcr-gathers-repeat", SamplerAddressMode::Repeat), + ] + .into_iter() + .map(|(name, mode)| Box::new(YCbCrGathers { name, mode }) as Box) + .collect() +} + +/// A 64-bit LCG, the generator of the surfaces' codes. +struct Random(u64); + +impl Random { + fn next(&mut self) -> u16 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + (self.0 >> 33) as u16 + } +} + +/// The bytes of a surface of `format` from generator `seed`: every luma code and chroma pair's +/// codes noise (a 10-bit code in the high bits of its little-endian word, its low bits noise too, +/// which Metal does not read), every byte past a plane's codes `0xa5`. +fn surface_bytes(format: PixelFormat, seed: u64) -> Vec { + let unit = match format { + PixelFormat::YCBCR104202P => 2, + _ => 1, + }; + let mut random = Random(seed); + let mut bytes = vec![0xa5; LENGTH]; + let mut put = |at: u64, word: u16| { + let at = at as usize; + match unit { + 1 => bytes[at] = word as u8, + _ => bytes[at..at + 2].copy_from_slice(&word.to_le_bytes()), + } + }; + for y in 0..H { + for x in 0..W { + put(y * ROW + x * unit, random.next()); + } + } + for y in 0..H / 2 { + for x in 0..W { + put(CHROMA_AT + y * ROW + x * unit, random.next()); + } + } + bytes +} + +/// The positions, row by row, as the `float2`s the kernels read: normalized, at texel coordinates +/// `-1 + (k + 1/2) / 3` on each axis. +fn positions() -> Vec { + let at = |k: u64, n: u64| ((-1.0 + (k as f64 + 0.5) / 3.0) / n as f64) as f32; + let mut bytes = Vec::with_capacity(POSITIONS as usize * 8); + for j in 0..ROWS { + for i in 0..COLUMNS { + bytes.extend(at(i, W).to_le_bytes()); + bytes.extend(at(j, H).to_le_bytes()); + } + } + bytes +} + +/// The layout a kernel's results are read under: one row a position, its three lanes' four +/// channels, so a diff names the position and the lane. +fn layout(element: Element) -> ByteLayout { + let row_pitch = LANES as u32 * 4 * element.size(); + let length = u64::from(row_pitch) * POSITIONS; + ByteLayout { + region: Region::Buffer { offset: 0, length }, + element, + row_offset: 0, + row_elements: LANES as u32 * 4, + row_pitch, + rows: POSITIONS as u32, + image_pitch: length as u32, + images: 1, + // A position's lanes fill its row, so there is nothing between them. + gaps: Gaps::Ignored, + } +} + +impl YCbCrGathers { + /// The sampler: linear on both filters, the case's mode on both axes, the opaque-white border. + fn sampler_spec(&self) -> SamplerSpec { + SamplerSpec::filtered(SamplerMinMagFilter::Linear) + .addressed(self.mode) + .bordered(SamplerBorderColor::OpaqueWhite) + } +} + +impl Case for YCbCrGathers { + fn name(&self) -> &'static str { + self.name + } + + fn metal_source(&self) -> MetalSource { + MetalSource::Run + } + + fn allowances(&self) -> Vec { + // One declaration and no tolerance: the buffers the kernels write into are `Shared` + // ([`crate::replay::shared_memory_notice`]). It loosens the notices site alone, so every + // value still compares exactly. + vec![crate::replay::shared_memory_notice()] + } + + fn shaders(&self) -> Result, String> { + let mut shaders = Vec::new(); + for (entry, _, _) in ENTRIES { + for extension in [RUNTIME_TEXT, METAL_TEXT] { + let fixture = Fixture { + name: FIXTURE, + entry, + }; + shaders.push(ShaderRef { + fixture: format!("{FIXTURE}{extension}"), + entry: entry.to_owned(), + source_sha256: crate::sha256_hex(fixture.text(extension)?.as_bytes()), + }); + } + } + Ok(shaders) + } + + fn inputs(&self) -> Result, String> { + let fixture = Fixture { + name: FIXTURE, + entry: ENTRIES[0].0, + }; + let mut inputs = vec![ + Input { + name: "air:kernels".to_owned(), + digest: Digest::sha256(fixture.text(RUNTIME_TEXT)?.as_bytes()), + }, + Input { + name: "air-typed:kernels".to_owned(), + digest: Digest::sha256(fixture.text(METAL_TEXT)?.as_bytes()), + }, + ]; + for (ostype, format, seed) in SURFACES { + let name = String::from_utf8_lossy(&ostype).into_owned(); + inputs.push(Input::bytes( + format!("format:{name}"), + &format.as_raw().to_le_bytes(), + )); + inputs.push(Input::bytes( + format!("surface:{name}"), + &surface_bytes(format, seed), + )); + } + inputs.push(Input::bytes("sampler", &self.sampler_spec().words())); + inputs.push(Input::bytes("positions", &positions())); + inputs.push(Input::bytes("buffer:blank", &[BLANK])); + Ok(inputs) + } + + fn run(&self, side: &mut dyn Side, sites: &mut Sites) -> Result<(), Abort> { + let fixture = Fixture { + name: FIXTURE, + entry: ENTRIES[0].0, + }; + let source = fixture.loaded().map_err(|why| Abort { + at: "library".to_owned(), + failure: Failure::named("FixtureNotRead", why), + })?; + let library = made(sites, "library", side.library(&source.source()))?; + let mut pipelines = Vec::new(); + for (entry, kind, element) in ENTRIES { + let at = format!("pipeline/{kind}"); + let pipeline = made(sites, &at, side.compute_pipeline(library, entry))?; + pipelines.push((pipeline, kind, element)); + } + let sampler = made(sites, "sampler", side.sampler(&self.sampler_spec()))?; + let positions = made(sites, "positions", side.buffer(&positions()))?; + for (ostype, format, seed) in SURFACES { + let name = String::from_utf8_lossy(&ostype).into_owned(); + let surface = made( + sites, + &format!("surface/{name}"), + side.buffer(&surface_bytes(format, seed)), + )?; + let texture = made( + sites, + &format!("texture/{name}"), + side.plane_texture( + surface, + &PlaneTexture { + ostype: Some(ostype), + format: format.as_raw(), + size: [W as u32, H as u32], + plane: 0, + planes: [(0, ROW), (CHROMA_AT, ROW)], + }, + ), + )?; + for &(pipeline, kind, element) in &pipelines { + let length = (LANES * POSITIONS) as usize * 4 * element.size() as usize; + let at = format!("{name}/{kind}"); + let output = made( + sites, + &format!("output/{at}"), + side.buffer(&vec![BLANK; length]), + )?; + let textures = [texture]; + let buffers = [output, positions]; + let samplers = [sampler]; + let command_buffer = made( + sites, + &format!("dispatch/{at}"), + side.dispatch_threads( + pipeline, + &Bindings::new(&textures, &buffers).sampled(&samplers), + &[], + [(LANES * POSITIONS) as u32, 1, 1], + [GROUP, 1, 1], + ), + )?; + let status = made(sites, &format!("status/{at}"), side.wait(command_buffer))?; + if status != Value::unit_variant("Completed") { + return Err(Abort { + at: format!("status/{at}"), + failure: Failure::named("CommandBufferStatus", crate::diff::print(&status)), + }); + } + let bytes = made( + sites, + &format!("read-back/{at}"), + side.read_buffer(output, 0, length), + )?; + let digest = Digest::sha256(&bytes); + sites.push(Node::leaf( + format!("samples/{at}"), + Observed::bytes(bytes, layout(element)).digested(digest), + )); + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The four cases ask four sampler states, each a distinct input, over surfaces whose rows + /// hold their codes: a luma row's `W` codes, a chroma row's `W / 2` pairs. + #[test] + fn the_cases_ask_four_states_over_two_surfaces() { + let cases = all(); + assert_eq!(cases.len(), 4); + let mut samplers = std::collections::BTreeSet::new(); + for case in &cases { + let inputs = case.inputs().expect("inputs"); + let sampler = inputs + .iter() + .find(|i| i.name == "sampler") + .expect("sampler"); + assert!( + samplers.insert(format!("{:?}", sampler.digest)), + "{}", + case.name() + ); + } + for (_, format, seed) in SURFACES { + let unit = if format == PixelFormat::YCBCR104202P { + 2 + } else { + 1 + }; + assert!(W * unit <= ROW, "{format:?}: a row's codes"); + assert_eq!(surface_bytes(format, seed).len(), LENGTH); + } + } + + /// The positions start a texel before the texture and end a texel past it, and every third + /// one on each axis is a texel's centre, where a gather's footprint turns. + #[test] + fn the_positions_cross_the_edges_and_the_centres() { + let bytes = positions(); + assert_eq!(bytes.len() as u64, POSITIONS * 8); + let x = |i: usize| f32::from_le_bytes(bytes[8 * i..8 * i + 4].try_into().unwrap()); + let texel = |i: usize| f64::from(x(i)) * W as f64; + assert!(texel(0) < 0.0, "{}", texel(0)); + assert!( + texel(COLUMNS as usize - 1) > W as f64, + "{}", + texel(COLUMNS as usize - 1) + ); + let centre = texel(1) - 0.5; + assert!((centre - centre.round()).abs() < 1e-5, "{centre}"); + } +} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN index baf9c1d76..c8842445a 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN +++ b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN @@ -369,5 +369,6 @@ fixture xr_two_planes: metal: Apple metal version 32023.921 (metalfe-32023.921.6 fixture xrb_color2: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture xrb_computed: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture xrb_dual: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) +fixture ycg_sample_gather: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture zwd_shapes: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture zws_compositor: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.air.ll b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.air.ll new file mode 100644 index 000000000..c4ed46644 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.air.ll @@ -0,0 +1,153 @@ +; Generated by scripts/regen-air-fixtures from ycg_sample_gather.metal (typed pointers; xcrun metal-as reassembles it). Do not edit. +; ModuleID = '-' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +%struct._texture_2d_t = type opaque +%struct._sampler_t = type opaque + +; Function Attrs: convergent mustprogress nofree nounwind willreturn +define void @ycg_float(%struct._texture_2d_t addrspace(1)* %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <4 x float> addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %2, <2 x float> addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %3, i32 noundef %4) local_unnamed_addr #0 { + %6 = udiv i32 %4, 3 + %7 = zext i32 %6 to i64 + %8 = getelementptr inbounds <2 x float>, <2 x float> addrspace(1)* %3, i64 %7 + %9 = load <2 x float>, <2 x float> addrspace(1)* %8, align 8, !tbaa !27, !alias.scope !30, !noalias !33 + %10 = urem i32 %4, 3 + switch i32 %10, label %15 [ + i32 1, label %11 + i32 2, label %13 + ] + +11: ; preds = %5 + %12 = tail call { <4 x float>, i8 } @air.gather_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 0, i32 0) #4, !alias.scope !37, !noalias !38 + br label %17 + +13: ; preds = %5 + %14 = tail call { <4 x float>, i8 } @air.gather_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 2, i32 0) #4, !alias.scope !37, !noalias !38 + br label %17 + +15: ; preds = %5 + %16 = tail call { <4 x float>, i8 } @air.sample_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0) #5, !alias.scope !37, !noalias !38 + br label %17 + +17: ; preds = %15, %13, %11 + %18 = phi { <4 x float>, i8 } [ %16, %15 ], [ %14, %13 ], [ %12, %11 ] + %19 = extractvalue { <4 x float>, i8 } %18, 0 + %20 = zext i32 %4 to i64 + %21 = getelementptr inbounds <4 x float>, <4 x float> addrspace(1)* %2, i64 %20 + store <4 x float> %19, <4 x float> addrspace(1)* %21, align 16, !tbaa !27, !alias.scope !39, !noalias !40 + ret void +} + +; Function Attrs: convergent mustprogress nofree nounwind willreturn +define void @ycg_half(%struct._texture_2d_t addrspace(1)* %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <4 x half> addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %2, <2 x float> addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %3, i32 noundef %4) local_unnamed_addr #1 { + %6 = udiv i32 %4, 3 + %7 = zext i32 %6 to i64 + %8 = getelementptr inbounds <2 x float>, <2 x float> addrspace(1)* %3, i64 %7 + %9 = load <2 x float>, <2 x float> addrspace(1)* %8, align 8, !tbaa !27, !alias.scope !41, !noalias !44 + %10 = urem i32 %4, 3 + switch i32 %10, label %15 [ + i32 1, label %11 + i32 2, label %13 + ] + +11: ; preds = %5 + %12 = tail call { <4 x half>, i8 } @air.gather_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 0, i32 0) #4, !alias.scope !48, !noalias !49 + br label %17 + +13: ; preds = %5 + %14 = tail call { <4 x half>, i8 } @air.gather_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 2, i32 0) #4, !alias.scope !48, !noalias !49 + br label %17 + +15: ; preds = %5 + %16 = tail call { <4 x half>, i8 } @air.sample_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly %0, %struct._sampler_t addrspace(2)* nocapture readonly %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0) #5, !alias.scope !48, !noalias !49 + br label %17 + +17: ; preds = %15, %13, %11 + %18 = phi { <4 x half>, i8 } [ %16, %15 ], [ %14, %13 ], [ %12, %11 ] + %19 = extractvalue { <4 x half>, i8 } %18, 0 + %20 = zext i32 %4 to i64 + %21 = getelementptr inbounds <4 x half>, <4 x half> addrspace(1)* %2, i64 %20 + store <4 x half> %19, <4 x half> addrspace(1)* %21, align 8, !tbaa !27, !alias.scope !50, !noalias !51 + ret void +} + +; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn +declare { <4 x float>, i8 } @air.gather_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)* nocapture readonly, <2 x float>, i1, <2 x i32>, i32, i32) local_unnamed_addr #2 + +; Function Attrs: argmemonly convergent mustprogress nofree nounwind readonly willreturn +declare { <4 x float>, i8 } @air.sample_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)* nocapture readonly, <2 x float>, i1, <2 x i32>, i1, float, float, i32) local_unnamed_addr #3 + +; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn +declare { <4 x half>, i8 } @air.gather_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)* nocapture readonly, <2 x float>, i1, <2 x i32>, i32, i32) local_unnamed_addr #2 + +; Function Attrs: argmemonly convergent mustprogress nofree nounwind readonly willreturn +declare { <4 x half>, i8 } @air.sample_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)* nocapture readonly, <2 x float>, i1, <2 x i32>, i1, float, float, i32) local_unnamed_addr #3 + +attributes #0 = { convergent mustprogress nofree nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #1 = { convergent mustprogress nofree nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #2 = { argmemonly mustprogress nofree nounwind readonly willreturn } +attributes #3 = { argmemonly convergent mustprogress nofree nounwind readonly willreturn } +attributes #4 = { argmemonly nounwind readonly willreturn } +attributes #5 = { argmemonly convergent nounwind readonly willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9, !17} +!air.compile_options = !{!21, !22, !23} +!llvm.ident = !{!24} +!air.version = !{!25} +!air.language_version = !{!26} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{void (%struct._texture_2d_t addrspace(1)*, %struct._sampler_t addrspace(2)*, <4 x float> addrspace(1)*, <2 x float> addrspace(1)*, i32)* @ycg_float, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15, !16} +!12 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} +!13 = !{i32 1, !"air.sampler", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"sampler", !"air.arg_name", !"s"} +!14 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"o"} +!15 = !{i32 3, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"p"} +!16 = !{i32 4, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"} +!17 = !{void (%struct._texture_2d_t addrspace(1)*, %struct._sampler_t addrspace(2)*, <4 x half> addrspace(1)*, <2 x float> addrspace(1)*, i32)* @ycg_half, !10, !18} +!18 = !{!19, !13, !20, !15, !16} +!19 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} +!20 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"half4", !"air.arg_name", !"o"} +!21 = !{!"air.compile.denorms_disable"} +!22 = !{!"air.compile.fast_math_enable"} +!23 = !{!"air.compile.framebuffer_fetch_enable"} +!24 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!25 = !{i32 2, i32 8, i32 0} +!26 = !{!"Metal", i32 4, i32 0, i32 0} +!27 = !{!28, !28, i64 0} +!28 = !{!"omnipotent char", !29, i64 0} +!29 = !{!"Simple C++ TBAA"} +!30 = !{!31} +!31 = distinct !{!31, !32, !"air-alias-scope-arg(3)"} +!32 = distinct !{!32, !"air-alias-scopes(ycg_float)"} +!33 = !{!34, !35, !36} +!34 = distinct !{!34, !32, !"air-alias-scope-textures"} +!35 = distinct !{!35, !32, !"air-alias-scope-samplers"} +!36 = distinct !{!36, !32, !"air-alias-scope-arg(2)"} +!37 = !{!34, !35} +!38 = !{!36, !31} +!39 = !{!36} +!40 = !{!34, !35, !31} +!41 = !{!42} +!42 = distinct !{!42, !43, !"air-alias-scope-arg(3)"} +!43 = distinct !{!43, !"air-alias-scopes(ycg_half)"} +!44 = !{!45, !46, !47} +!45 = distinct !{!45, !43, !"air-alias-scope-textures"} +!46 = distinct !{!46, !43, !"air-alias-scope-samplers"} +!47 = distinct !{!47, !43, !"air-alias-scope-arg(2)"} +!48 = !{!45, !46} +!49 = !{!47, !42} +!50 = !{!47} +!51 = !{!45, !46, !42} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.ll b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.ll new file mode 100644 index 000000000..250c658e6 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.ll @@ -0,0 +1,150 @@ +; Generated by scripts/regen-air-fixtures from ycg_sample_gather.metal (llvm-dis of ycg_sample_gather.air). Do not edit. +; ModuleID = '' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +; Function Attrs: convergent mustprogress nofree nounwind willreturn +define void @ycg_float(ptr addrspace(1) %0, ptr addrspace(2) readonly captures(none) %1, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %2, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %3, i32 noundef %4) local_unnamed_addr #0 { + %6 = udiv i32 %4, 3 + %7 = zext i32 %6 to i64 + %8 = getelementptr inbounds <2 x float>, ptr addrspace(1) %3, i64 %7 + %9 = load <2 x float>, ptr addrspace(1) %8, align 8, !tbaa !27, !alias.scope !30, !noalias !33 + %10 = urem i32 %4, 3 + switch i32 %10, label %15 [ + i32 1, label %11 + i32 2, label %13 + ] + +11: ; preds = %5 + %12 = tail call { <4 x float>, i8 } @air.gather_texture_2d.v4f32(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 0, i32 0) #4, !alias.scope !37, !noalias !38 + br label %17 + +13: ; preds = %5 + %14 = tail call { <4 x float>, i8 } @air.gather_texture_2d.v4f32(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 2, i32 0) #4, !alias.scope !37, !noalias !38 + br label %17 + +15: ; preds = %5 + %16 = tail call { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0) #5, !alias.scope !37, !noalias !38 + br label %17 + +17: ; preds = %15, %13, %11 + %18 = phi { <4 x float>, i8 } [ %16, %15 ], [ %14, %13 ], [ %12, %11 ] + %19 = extractvalue { <4 x float>, i8 } %18, 0 + %20 = zext i32 %4 to i64 + %21 = getelementptr inbounds <4 x float>, ptr addrspace(1) %2, i64 %20 + store <4 x float> %19, ptr addrspace(1) %21, align 16, !tbaa !27, !alias.scope !39, !noalias !40 + ret void +} + +; Function Attrs: convergent mustprogress nofree nounwind willreturn +define void @ycg_half(ptr addrspace(1) %0, ptr addrspace(2) readonly captures(none) %1, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %2, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %3, i32 noundef %4) local_unnamed_addr #1 { + %6 = udiv i32 %4, 3 + %7 = zext i32 %6 to i64 + %8 = getelementptr inbounds <2 x float>, ptr addrspace(1) %3, i64 %7 + %9 = load <2 x float>, ptr addrspace(1) %8, align 8, !tbaa !27, !alias.scope !41, !noalias !44 + %10 = urem i32 %4, 3 + switch i32 %10, label %15 [ + i32 1, label %11 + i32 2, label %13 + ] + +11: ; preds = %5 + %12 = tail call { <4 x half>, i8 } @air.gather_texture_2d.v4f16(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 0, i32 0) #4, !alias.scope !48, !noalias !49 + br label %17 + +13: ; preds = %5 + %14 = tail call { <4 x half>, i8 } @air.gather_texture_2d.v4f16(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i32 2, i32 0) #4, !alias.scope !48, !noalias !49 + br label %17 + +15: ; preds = %5 + %16 = tail call { <4 x half>, i8 } @air.sample_texture_2d.v4f16(ptr addrspace(1) readonly captures(none) %0, ptr addrspace(2) readonly captures(none) %1, <2 x float> %9, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0) #5, !alias.scope !48, !noalias !49 + br label %17 + +17: ; preds = %15, %13, %11 + %18 = phi { <4 x half>, i8 } [ %16, %15 ], [ %14, %13 ], [ %12, %11 ] + %19 = extractvalue { <4 x half>, i8 } %18, 0 + %20 = zext i32 %4 to i64 + %21 = getelementptr inbounds <4 x half>, ptr addrspace(1) %2, i64 %20 + store <4 x half> %19, ptr addrspace(1) %21, align 8, !tbaa !27, !alias.scope !50, !noalias !51 + ret void +} + +; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read) +declare { <4 x float>, i8 } @air.gather_texture_2d.v4f32(ptr addrspace(1) readonly captures(none), ptr addrspace(2) readonly captures(none), <2 x float>, i1, <2 x i32>, i32, i32) local_unnamed_addr #2 + +; Function Attrs: convergent mustprogress nofree nounwind willreturn memory(argmem: read) +declare { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1) readonly captures(none), ptr addrspace(2) readonly captures(none), <2 x float>, i1, <2 x i32>, i1, float, float, i32) local_unnamed_addr #3 + +; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read) +declare { <4 x half>, i8 } @air.gather_texture_2d.v4f16(ptr addrspace(1) readonly captures(none), ptr addrspace(2) readonly captures(none), <2 x float>, i1, <2 x i32>, i32, i32) local_unnamed_addr #2 + +; Function Attrs: convergent mustprogress nofree nounwind willreturn memory(argmem: read) +declare { <4 x half>, i8 } @air.sample_texture_2d.v4f16(ptr addrspace(1) readonly captures(none), ptr addrspace(2) readonly captures(none), <2 x float>, i1, <2 x i32>, i1, float, float, i32) local_unnamed_addr #3 + +attributes #0 = { convergent mustprogress nofree nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #1 = { convergent mustprogress nofree nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) } +attributes #3 = { convergent mustprogress nofree nounwind willreturn memory(argmem: read) } +attributes #4 = { nounwind willreturn memory(argmem: read) } +attributes #5 = { convergent nounwind willreturn memory(argmem: read) } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9, !17} +!air.compile_options = !{!21, !22, !23} +!llvm.ident = !{!24} +!air.version = !{!25} +!air.language_version = !{!26} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{ptr @ycg_float, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15, !16} +!12 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} +!13 = !{i32 1, !"air.sampler", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"sampler", !"air.arg_name", !"s"} +!14 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"o"} +!15 = !{i32 3, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"p"} +!16 = !{i32 4, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"} +!17 = !{ptr @ycg_half, !10, !18} +!18 = !{!19, !13, !20, !15, !16} +!19 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} +!20 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"half4", !"air.arg_name", !"o"} +!21 = !{!"air.compile.denorms_disable"} +!22 = !{!"air.compile.fast_math_enable"} +!23 = !{!"air.compile.framebuffer_fetch_enable"} +!24 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!25 = !{i32 2, i32 8, i32 0} +!26 = !{!"Metal", i32 4, i32 0, i32 0} +!27 = !{!28, !28, i64 0} +!28 = !{!"omnipotent char", !29, i64 0} +!29 = !{!"Simple C++ TBAA"} +!30 = !{!31} +!31 = distinct !{!31, !32, !"air-alias-scope-arg(3)"} +!32 = distinct !{!32, !"air-alias-scopes(ycg_float)"} +!33 = !{!34, !35, !36} +!34 = distinct !{!34, !32, !"air-alias-scope-textures"} +!35 = distinct !{!35, !32, !"air-alias-scope-samplers"} +!36 = distinct !{!36, !32, !"air-alias-scope-arg(2)"} +!37 = !{!34, !35} +!38 = !{!36, !31} +!39 = !{!36} +!40 = !{!34, !35, !31} +!41 = !{!42} +!42 = distinct !{!42, !43, !"air-alias-scope-arg(3)"} +!43 = distinct !{!43, !"air-alias-scopes(ycg_half)"} +!44 = !{!45, !46, !47} +!45 = distinct !{!45, !43, !"air-alias-scope-textures"} +!46 = distinct !{!46, !43, !"air-alias-scope-samplers"} +!47 = distinct !{!47, !43, !"air-alias-scope-arg(2)"} +!48 = !{!45, !46} +!49 = !{!47, !42} +!50 = !{!47} +!51 = !{!45, !46, !42} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.metal b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.metal new file mode 100644 index 000000000..d4f2669df --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/ycg_sample_gather.metal @@ -0,0 +1,50 @@ +// ycg_sample_gather.metal (metal2vulkan): kernels that sample or gather a texture through the +// runtime sampler at `[[sampler(0)]]`, as a probe that asks each lane for one of the three, written +// from scratch for the conformance suite's two-plane YCbCr gather cases (`ycbcr_gathers`). Thread +// `i` writes `o[i]` (buffer 0) at the normalized position `p[i / 3]` (buffer 1): lane `i % 3` 0 a +// sample, 1 a gather of red, 2 a gather of blue. `ycg_float` reads a `float` texture into +// `float4`s, `ycg_half` a `half` one into `half4`s. +#include +using namespace metal; + +kernel void ycg_float(texture2d t [[texture(0)]], + sampler s [[sampler(0)]], + device float4 *o [[buffer(0)]], + device const float2 *p [[buffer(1)]], + uint i [[thread_position_in_grid]]) { + float2 at = p[i / 3]; + float4 v; + switch (i % 3) { + case 1: + v = t.gather(s, at, int2(0), component::x); + break; + case 2: + v = t.gather(s, at, int2(0), component::z); + break; + default: + v = t.sample(s, at); + break; + } + o[i] = v; +} + +kernel void ycg_half(texture2d t [[texture(0)]], + sampler s [[sampler(0)]], + device half4 *o [[buffer(0)]], + device const float2 *p [[buffer(1)]], + uint i [[thread_position_in_grid]]) { + float2 at = p[i / 3]; + half4 v; + switch (i % 3) { + case 1: + v = t.gather(s, at, int2(0), component::x); + break; + case 2: + v = t.gather(s, at, int2(0), component::z); + break; + default: + v = t.sample(s, at); + break; + } + o[i] = v; +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_in_place.rs b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_in_place.rs index 44fbee456..ea702c49f 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_in_place.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_in_place.rs @@ -29,7 +29,7 @@ use metal2vulkan::device::DevicePolicy; use metal2vulkan::metal::format::ycbcr_format; use metal2vulkan::metal::{PixelFormat, SamplerMinMagFilter, TextureUsage}; use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource}; -use metal2vulkan_devtools::llvm_assemble_text; +use metal2vulkan_devtools::{engine_dir, llvm_assemble, llvm_assemble_text}; use metal2vulkan_runtime::{ Buffer, CommandBufferStatus, ComputePipelineState, Degradation, Device, Error, IoSurfacePlane, IoSurfaceYCbCr, PipelineOptions, ResourceOptions, SamplerState, Size, SurfaceYCbCrMatrix, @@ -557,6 +557,124 @@ fn a_ycbcr_texture_reads_its_planes_in_place_under_the_defaults() { }); } +/// A function that samples one texture and gathers it through one sampler (the fixture +/// `ycg_sample_gather`'s `ycg_float`: lane `i % 3` a sample, a gather of red, a gather of blue, +/// at position `i / 3`) reads the planes in place on a device whose memory is the host's: each +/// dispatch binds the planes and converts nothing, a linear sample at [`ycbcr_textures::BETWEEN`] +/// past a texel is Metal's filter of Metal's texels, and a gather there is Metal's texels at the +/// footprint's four taps, `(x, y + 1)`, `(x + 1, y + 1)`, `(x + 1, y)`, `(x, y)` clamped to the +/// edge; the filter's notices (`ycbcr-filter-not-exact`, `float-sample-filter`) are not announced. +#[test] +fn a_ycbcr_texture_sampled_and_gathered_by_one_function_reads_its_planes_in_place() { + on_device(|device, messages| { + if !reads_in_place(device) { + eprintln!("{}: reads no planes in place", device.name()); + return; + } + let bitcode = llvm_assemble(&engine_dir().join("ycg_sample_gather.ll")).unwrap(); + let library = device.new_library_with_air(&bitcode).unwrap(); + let function = library.new_function_with_name("ycg_float").unwrap(); + let state = device + .new_compute_pipeline_state( + &ComputePipelineDescriptor::new(StageSource::Function( + function.function_descriptor(), + )), + &PipelineOptions::default(), + ) + .unwrap(); + let linear = sampler(device, SamplerMinMagFilter::Linear); + let [a, b] = ycbcr_textures::BETWEEN; + for (seed, surface) in (1..).zip(SURFACES) { + let what = format!("{:?} {:?}", surface.format, surface.conversion); + let bytes = surface.bytes(seed); + let buffer = buffer_with(device, &bytes).unwrap(); + let texture = surface.texture(&buffer, 0).unwrap(); + let model = surface.model(&bytes); + let [w, h] = surface.size; + let n = w * h; + let positions: Vec = (0..n) + .flat_map(|i| { + let at = |q: u64, k: u32, size: u64| { + ((q as f32 + 0.5 + k as f32 / 256.0) / size as f32).to_le_bytes() + }; + [at(i % w, a, w), at(i / w, b, h)] + }) + .flatten() + .collect(); + let positions = buffer_with(device, &positions).unwrap(); + let out = device + .new_buffer(3 * 16 * n, ResourceOptions::shared()) + .unwrap(); + let before = device.work_counts(); + let queue = device.new_command_queue().unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer.compute_command_encoder().unwrap(); + encoder.set_compute_pipeline_state(&state).unwrap(); + encoder.set_texture(Some(&texture), 0).unwrap(); + encoder.set_buffer(Some(&out), 0, 0).unwrap(); + encoder.set_buffer(Some(&positions), 0, 1).unwrap(); + encoder.set_sampler_state(Some(&linear), 0).unwrap(); + encoder + .dispatch_threads(Size::new(3 * n, 1, 1), Size::new(64, 1, 1)) + .unwrap(); + encoder.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.status(), CommandBufferStatus::Completed); + let work = ycbcr_work(&before, &device.work_counts()); + assert_eq!( + work[2..], + [1, 0, 0], + "{what}: the planes bound, nothing converted" + ); + let raw = read_buffer(&out, 0, 3 * 16 * n).unwrap(); + let lanes: Vec<[f32; 4]> = raw + .as_chunks::<16>() + .0 + .iter() + .map(|t| { + std::array::from_fn(|k| { + f32::from_le_bytes(t[4 * k..4 * k + 4].try_into().unwrap()) + }) + }) + .collect(); + for i in 0..n { + let (x, y) = (i % w, i / w); + let step = |c: usize| filtered(&model, surface.size, (x, y), c); + let sample: [f32; 4] = + [step(0), step(1), step(2), 2048].map(|k| f32::from(k) / 2048.0); + assert_eq!( + lanes[3 * i as usize], + sample, + "{what}: sample past texel {:?}", + (x, y) + ); + let (x1, y1) = ((x + 1).min(w - 1), (y + 1).min(h - 1)); + for (lane, c) in [(1, 0), (2, 2)] { + let tap = + |tx: u64, ty: u64| f32::from(model[(ty * w + tx) as usize][c]) / 2048.0; + let want = [tap(x, y1), tap(x1, y1), tap(x1, y), tap(x, y)]; + assert_eq!( + lanes[(3 * i + lane) as usize], + want, + "{what}: gather of channel {c} past texel {:?}", + (x, y) + ); + } + } + } + let notices = counts(&messages.take_notices()); + eprintln!("YCBCR-IN-PLACE-GATHER notices {notices:?}"); + for kind in [ + Degradation::FloatSampleFilter, + Degradation::HalfSampleFilter, + Degradation::YCbCrFilterNotExact, + ] { + assert_eq!(notices.get(&kind), None, "{notices:?}"); + } + }); +} + /// A texture whose planes reach past their buffer (Metal reads zeros there) and one made with a /// swizzle keep their image on a device whose memory is the host's, counted by the reason /// (`ycbcr_images_kept_reach`, `ycbcr_images_kept_swizzle`), and the first still samples Metal's