diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 373fc0759..a70bbaa36 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -166,6 +166,7 @@ mod swizzle; mod take_split; mod texture; mod texture_accesses; +mod texture_buffer_words; mod texture_buffers; mod times; mod translate; diff --git a/crates/metal2vulkan-runtime/src/tests/texture_buffer_words.rs b/crates/metal2vulkan-runtime/src/tests/texture_buffer_words.rs new file mode 100644 index 000000000..945de3e14 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/texture_buffer_words.rs @@ -0,0 +1,615 @@ +//! Texture buffers of the formats the runtime holds as words, under a device cfg on every device +//! of the cfg's kind: `A8Unorm`, the four 8-bit sRGB formats, `RGB9E5Float`, the four packed +//! 16-bit formats, `BGR10A2Unorm` and the four extended-range formats. Metal makes a texture +//! buffer of each, for reading, writing and both (an Apple M3 Max under macOS 27.0.1, jobs +//! m3-max-79872 and m3-max-79879). +//! +//! The owned fixture `tb_word_formats` reads, writes, and reads then writes (`access::read_write`) +//! texture buffers of five texels, one format at a time, with eight threads: thread `t` reads +//! texel `t`, writes the value whose code is texel `t`'s, and asks the widths. Each answer is +//! checked against the CPU's decode of the texels' bytes, and each written texture buffer's bytes +//! against the codes: +//! +//! - in bounds, a read is the texel's value: exact where the format's decode is exact in `f32`, +//! within the stated tolerance for the sRGB transfer, and a write stores the code whose value +//! it was given; neighbouring threads write neighbouring texels, which share a word where a +//! texel is smaller than one, and every one keeps its code; +//! - past the width (threads 5 to 7), a read is Metal's out-of-bounds texel (zero, with alpha 1 +//! where the format has none) and a write changes nothing: the bytes after the fifth texel, the +//! rest of its word among them, keep theirs; +//! - each texture buffer's width is five. +//! +//! `llvm-as` assembles the fixture; without it the test skips visibly. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::tests::command::on_device::{each_device, finished}; + use crate::{ + Buffer, ComputePipelineDescriptor, ComputePipelineState, Device, DispatchType, + FunctionDescriptor, IndexClass, Library, MemoryClass, PixelFormat, StageSource, Texture, + TextureDescriptor, TextureType, TextureUsage, + }; + use metal2vulkan_devtools::{engine_dir, llvm_assemble, AssembleError}; + + /// The texels of each texture buffer. + const WIDTH: usize = 5; + /// The threads of the dispatch: the texels and three past them. + const THREADS: usize = 8; + /// What every byte outside the texture buffers' texels holds, before and after. + const SENTINEL: u8 = 0x5a; + /// Where the read, the written and the read-written texture buffers lie in the data buffer. + const PLACES: [u64; 3] = [1024, 2048, 3072]; + /// The data buffer's bytes. + const DATA: u64 = 4096; + + /// How a format's channels decode. + #[derive(Clone, Copy, Debug)] + enum Decode { + /// Exact in `f32`: the CPU's value is the device's, bit for bit. + Exact, + /// Through the sRGB transfer: within `SRGB_TOLERANCE` of the CPU's value. + Srgb, + } + + /// The largest difference taken from the CPU's decode of an sRGB transfer. + const SRGB_TOLERANCE: f32 = 1.0 / 4096.0; + + /// One format under test: its texel's bytes, its five texels' bytes, whether it has alpha + /// (else alpha reads 1 out of bounds), and its CPU decode. + struct Case { + format: PixelFormat, + bytes: usize, + texels: Vec, + alpha: bool, + decode: Decode, + value: fn(&[u8]) -> [f64; 4], + } + + /// The sRGB transfer's decode of `c` in `[0, 1]`, extended past 1 and mirrored below 0. + fn srgb(c: f64) -> f64 { + let m = c.abs(); + let linear = if m <= 0.04045 { + m / 12.92 + } else { + ((m + 0.055) / 1.055).powf(2.4) + }; + linear.copysign(c) + } + + /// The sRGB decode of byte `i` of `b`. + fn srgb8(b: &[u8], i: usize) -> f64 { + srgb(f64::from(b[i]) / 255.0) + } + + /// The unsigned normalized value of the `bits`-bit field at bit `at` of `word`. + fn unorm(word: u64, at: u32, bits: u32) -> f64 { + let max = (1u64 << bits) - 1; + ((word >> at) & max) as f64 / max as f64 + } + + /// The extended-range value of the 10-bit code at bit `at` of `word`. + fn xr(word: u64, at: u32) -> f64 { + (((word >> at) & 0x3ff) as f64 - 384.0) / 510.0 + } + + /// The little-endian integer of `bytes`. + fn le(bytes: &[u8]) -> u64 { + bytes + .iter() + .rev() + .fold(0u64, |word, &b| (word << 8) | u64::from(b)) + } + + /// `bytes` of each little-endian word in `words`. + fn words_le(words: &[u64], bytes: usize) -> Vec { + words + .iter() + .flat_map(|w| w.to_le_bytes()[..bytes].to_vec()) + .collect() + } + + fn cases() -> Vec { + let rgb9e5 = |r: u64, g: u64, b: u64, e: u64| r | (g << 9) | (b << 18) | (e << 27); + let b5g6r5 = |r: u64, g: u64, b: u64| b | (g << 5) | (r << 11); + let a1bgr5 = |r: u64, g: u64, b: u64, a: u64| a | (b << 1) | (g << 6) | (r << 11); + let abgr4 = |r: u64, g: u64, b: u64, a: u64| a | (b << 4) | (g << 8) | (r << 12); + let bgr5a1 = |r: u64, g: u64, b: u64, a: u64| b | (g << 5) | (r << 10) | (a << 15); + let bgr10a2 = |r: u64, g: u64, b: u64, a: u64| b | (g << 10) | (r << 20) | (a << 30); + // Bits 30 and 31 are written as ones. + let bgr10xr = |r: u64, g: u64, b: u64| b | (g << 10) | (r << 20) | (3 << 30); + // Each code in its 16-bit field's top 10 bits; the low 6 are written as zeros. + let bgra10xr = + |r: u64, g: u64, b: u64, a: u64| (b << 6) | (g << 22) | (r << 38) | (a << 54); + let rgba8 = [ + [0u8, 255, 51, 128], + [200, 7, 99, 180], + [255, 255, 255, 255], + [1, 2, 3, 4], + [128, 64, 32, 16], + ]; + let xr_codes = [ + (384, 894, 0), + (1023, 511, 640), + (100, 384, 894), + (700, 300, 450), + (894, 0, 1023), + ]; + vec![ + Case { + format: PixelFormat::A8Unorm, + bytes: 1, + texels: vec![0, 255, 51, 128, 7], + alpha: true, + decode: Decode::Exact, + value: |b| [0.0, 0.0, 0.0, f64::from(b[0]) / 255.0], + }, + Case { + format: PixelFormat::R8UnormSRGB, + bytes: 1, + texels: vec![0, 255, 51, 128, 200], + alpha: false, + decode: Decode::Srgb, + value: |b| [srgb(f64::from(b[0]) / 255.0), 0.0, 0.0, 1.0], + }, + Case { + format: PixelFormat::RG8UnormSRGB, + bytes: 2, + texels: vec![0, 255, 51, 128, 200, 7, 255, 0, 99, 180], + alpha: false, + decode: Decode::Srgb, + value: |b| { + [ + srgb(f64::from(b[0]) / 255.0), + srgb(f64::from(b[1]) / 255.0), + 0.0, + 1.0, + ] + }, + }, + Case { + format: PixelFormat::RGBA8UnormSRGB, + bytes: 4, + texels: rgba8.concat(), + alpha: true, + decode: Decode::Srgb, + value: |b| { + [ + srgb8(b, 0), + srgb8(b, 1), + srgb8(b, 2), + f64::from(b[3]) / 255.0, + ] + }, + }, + Case { + format: PixelFormat::BGRA8UnormSRGB, + bytes: 4, + texels: rgba8.concat(), + alpha: true, + decode: Decode::Srgb, + value: |b| { + [ + srgb8(b, 2), + srgb8(b, 1), + srgb8(b, 0), + f64::from(b[3]) / 255.0, + ] + }, + }, + // Each texel's shared exponent is the one Metal's encode derives: its largest + // mantissa is 256 or more. + Case { + format: PixelFormat::RGB9E5Float, + bytes: 4, + texels: words_le( + &[ + rgb9e5(300, 17, 511, 15), + rgb9e5(256, 0, 0, 1), + rgb9e5(0, 511, 128, 30), + rgb9e5(400, 400, 400, 20), + rgb9e5(511, 1, 2, 5), + ], + 4, + ), + alpha: false, + decode: Decode::Exact, + value: |b| { + let w = le(b); + let scale = 2f64.powi(((w >> 27) & 31) as i32 - 24); + let m = |at: u32| ((w >> at) & 511) as f64 * scale; + [m(0), m(9), m(18), 1.0] + }, + }, + Case { + format: PixelFormat::B5G6R5Unorm, + bytes: 2, + texels: words_le( + &[ + b5g6r5(31, 63, 31), + b5g6r5(0, 0, 0), + b5g6r5(16, 32, 8), + b5g6r5(1, 62, 30), + b5g6r5(10, 20, 5), + ], + 2, + ), + alpha: false, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [unorm(w, 11, 5), unorm(w, 5, 6), unorm(w, 0, 5), 1.0] + }, + }, + Case { + format: PixelFormat::A1BGR5Unorm, + bytes: 2, + texels: words_le( + &[ + a1bgr5(31, 0, 15, 1), + a1bgr5(0, 31, 0, 0), + a1bgr5(16, 8, 4, 1), + a1bgr5(5, 10, 20, 0), + a1bgr5(1, 2, 3, 1), + ], + 2, + ), + alpha: true, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [ + unorm(w, 11, 5), + unorm(w, 6, 5), + unorm(w, 1, 5), + unorm(w, 0, 1), + ] + }, + }, + Case { + format: PixelFormat::ABGR4Unorm, + bytes: 2, + texels: words_le( + &[ + abgr4(15, 0, 7, 3), + abgr4(0, 15, 8, 12), + abgr4(1, 2, 3, 4), + abgr4(9, 10, 11, 12), + abgr4(15, 15, 15, 15), + ], + 2, + ), + alpha: true, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [ + unorm(w, 12, 4), + unorm(w, 8, 4), + unorm(w, 4, 4), + unorm(w, 0, 4), + ] + }, + }, + Case { + format: PixelFormat::BGR5A1Unorm, + bytes: 2, + texels: words_le( + &[ + bgr5a1(31, 0, 15, 1), + bgr5a1(0, 31, 0, 0), + bgr5a1(16, 8, 4, 1), + bgr5a1(5, 10, 20, 0), + bgr5a1(1, 2, 3, 1), + ], + 2, + ), + alpha: true, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [ + unorm(w, 10, 5), + unorm(w, 5, 5), + unorm(w, 0, 5), + unorm(w, 15, 1), + ] + }, + }, + Case { + format: PixelFormat::BGR10A2Unorm, + bytes: 4, + texels: words_le( + &[ + bgr10a2(1023, 0, 511, 3), + bgr10a2(0, 1023, 1, 0), + bgr10a2(300, 600, 900, 1), + bgr10a2(5, 10, 20, 2), + bgr10a2(512, 256, 128, 3), + ], + 4, + ), + alpha: true, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [ + unorm(w, 20, 10), + unorm(w, 10, 10), + unorm(w, 0, 10), + unorm(w, 30, 2), + ] + }, + }, + Case { + format: PixelFormat::BGR10XR, + bytes: 4, + texels: words_le(&xr_codes.map(|(r, g, b)| bgr10xr(r, g, b)), 4), + alpha: false, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [xr(w, 20), xr(w, 10), xr(w, 0), 1.0] + }, + }, + Case { + format: PixelFormat::BGR10XRSRGB, + bytes: 4, + texels: words_le(&xr_codes.map(|(r, g, b)| bgr10xr(r, g, b)), 4), + alpha: false, + decode: Decode::Srgb, + value: |b| { + let w = le(b); + [srgb(xr(w, 20)), srgb(xr(w, 10)), srgb(xr(w, 0)), 1.0] + }, + }, + // Alpha codes within [384, 894], whose values Metal reads unclamped. + Case { + format: PixelFormat::BGRA10XR, + bytes: 8, + texels: words_le( + &xr_codes.map(|(r, g, b)| bgra10xr(r, g, b, 384 + (r % 511))), + 8, + ), + alpha: true, + decode: Decode::Exact, + value: |b| { + let w = le(b); + [xr(w, 38), xr(w, 22), xr(w, 6), xr(w, 54)] + }, + }, + Case { + format: PixelFormat::BGRA10XRSRGB, + bytes: 8, + texels: words_le( + &xr_codes.map(|(r, g, b)| bgra10xr(r, g, b, 384 + (g % 511))), + 8, + ), + alpha: true, + decode: Decode::Srgb, + value: |b| { + let w = le(b); + [srgb(xr(w, 38)), srgb(xr(w, 22)), srgb(xr(w, 6)), xr(w, 54)] + }, + }, + ] + } + + /// The bytes of a Shared buffer, as the host reads them. + fn bytes(buffer: &Buffer) -> Vec { + let contents = buffer.contents().expect("a Shared buffer"); + let len = usize::try_from(buffer.length()).expect("a length"); + // SAFETY: the buffer is alive, Shared, of `len` bytes, and no work in flight writes it. + unsafe { std::slice::from_raw_parts(contents.cast::().as_ptr(), len) }.to_vec() + } + + /// Writes `values` to a Shared buffer's bytes from byte `first`. + fn set_bytes(buffer: &Buffer, first: usize, values: &[u8]) { + let contents = buffer.contents().expect("a Shared buffer"); + let len = usize::try_from(buffer.length()).expect("a length"); + assert!(first + values.len() <= len, "bytes past the buffer"); + // SAFETY: the buffer is alive, Shared, and holds the bytes; no work in flight reaches it. + let all = unsafe { std::slice::from_raw_parts_mut(contents.cast::().as_ptr(), len) }; + all[first..first + values.len()].copy_from_slice(values); + } + + /// The `f32` lanes of a Shared buffer, as the host reads them. + fn floats(buffer: &Buffer) -> Vec { + (bytes(buffer).chunks_exact(4)) + .map(|c| f32::from_le_bytes(c.try_into().expect("four bytes"))) + .collect() + } + + /// One dispatch of `state` over `THREADS` threads in one threadgroup, with `textures` and + /// `buffers` at their slots, committed and finished. + fn dispatch( + device: &Device, + state: &ComputePipelineState, + textures: &[&Texture], + buffers: &[&Buffer], + ) -> Result<(), crate::Error> { + let queue = device.new_command_queue(1).expect("a queue"); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = + (command_buffer.compute_command_encoder(DispatchType::Serial)).expect("an encoder"); + encoder + .set_compute_pipeline_state(state) + .expect("the state"); + for (slot, texture) in (0..).zip(textures) { + encoder.set_texture(Some(texture), slot).expect("a texture"); + } + for (slot, buffer) in (0..).zip(buffers) { + encoder.set_buffer(Some(buffer), 0, slot).expect("a buffer"); + } + let threads = u32::try_from(THREADS).expect("threads"); + let dispatched = encoder.dispatch_threadgroups([1, 1, 1], [threads, 1, 1]); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished("texture buffer words", &command_buffer); + assert_eq!(command_buffer.error(), None); + dispatched + } + + /// The lanes `got` read against the CPU's `want`, by `decode`: None where they agree. + fn differs(got: &[f32], want: [f64; 4], decode: Decode) -> Option { + let agree = got.iter().zip(want).all(|(&g, w)| match decode { + Decode::Exact => g.to_bits() == (w as f32).to_bits(), + Decode::Srgb => (g - w as f32).abs() <= SRGB_TOLERANCE, + }); + (!agree).then(|| format!("read {got:?}, the CPU's {want:?}")) + } + + /// Runs one format's case on `device`: what differs from the CPU's answers, or why the + /// runtime did not run it. + fn run(device: &Device, library: &Library, case: &Case) -> Vec { + let mut wrong = Vec::new(); + let block = (device.take_block(MemoryClass::Shared, 16 << 10)).expect("a block"); + let place = |(at, len)| (device.new_buffer(block.id, at, len, Some(0))).expect("a buffer"); + let data = place((0, DATA)); + let values = place((4096, 256)); + let reads = place((8192, 256)); + let rw_reads = place((12288, 256)); + let widths = place((12288 + 256, 16)); + let len = WIDTH * case.bytes; + let texel = |texels: &[u8], t: usize| texels[t * case.bytes..(t + 1) * case.bytes].to_vec(); + // The read-written texture buffer starts with the texels in reverse. + let reversed: Vec = (0..WIDTH) + .rev() + .flat_map(|t| texel(&case.texels, t)) + .collect(); + set_bytes(&data, 0, &[SENTINEL; DATA as usize]); + let [src_at, dst_at, rw_at] = PLACES.map(|at| usize::try_from(at).expect("a place")); + set_bytes(&data, src_at, &case.texels); + set_bytes(&data, rw_at, &reversed); + // Thread `t` writes the value of texel `t`'s code; past the width, an arbitrary one. + let mut lanes = Vec::new(); + for t in 0..THREADS { + let value = match t < WIDTH { + true => (case.value)(&texel(&case.texels, t)), + false => [0.25, 0.5, 0.75, 1.0], + }; + lanes.extend(value.map(|v| (v as f32).to_le_bytes()).concat()); + } + set_bytes(&values, 0, &lanes); + set_bytes(&reads, 0, &[0; 256]); + set_bytes(&rw_reads, 0, &[0; 256]); + set_bytes(&widths, 0, &[0; 16]); + let make = |at: u64, usage| { + let mut d = TextureDescriptor::new(TextureType::TextureBuffer, case.format, 5, 1); + d.usage = usage; + let index = 1 + u32::try_from(at / 1024 - 1).expect("an index"); + data.new_texture(&d, Some(index), at, 256) + }; + let made = ( + make(PLACES[0], TextureUsage::SHADER_READ), + make(PLACES[1], TextureUsage::SHADER_WRITE), + make( + PLACES[2], + TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE, + ), + ); + let (src, dst, rw) = match made { + (Ok(src), Ok(dst), Ok(rw)) => (src, dst, rw), + (src, dst, rw) => { + let why = [src.err(), dst.err(), rw.err()]; + return vec![format!("not made: {why:?}")]; + } + }; + let function = library.function("tb_word_formats").expect("the kernel"); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(function), + )); + let state = match device.new_compute_pipeline_state(&descriptor, |s| Ok(vec![0; s.len()])) { + Ok(state) => state, + Err(error) => return vec![format!("no state: {error}")], + }; + if let Err(error) = dispatch( + device, + &state, + &[&src, &dst, &rw], + &[&values, &reads, &rw_reads, &widths], + ) { + return vec![format!("not dispatched: {error}")]; + } + let out_of_bounds = [0.0, 0.0, 0.0, if case.alpha { 0.0 } else { 1.0 }]; + let (reads, rw_reads) = (floats(&reads), floats(&rw_reads)); + for t in 0..THREADS { + let (want, rw_want) = match t < WIDTH { + true => ( + (case.value)(&texel(&case.texels, t)), + (case.value)(&texel(&reversed, t)), + ), + false => (out_of_bounds, out_of_bounds), + }; + let decode = if t < WIDTH { + case.decode + } else { + Decode::Exact + }; + if let Some(d) = differs(&reads[4 * t..4 * t + 4], want, decode) { + wrong.push(format!("texel {t} of the read one: {d}")); + } + if let Some(d) = differs(&rw_reads[4 * t..4 * t + 4], rw_want, decode) { + wrong.push(format!("texel {t} of the read-written one: {d}")); + } + } + let all = bytes(&data); + for (what, at, before) in [ + ("read", src_at, &case.texels), + ("written", dst_at, &case.texels), + ("read-written", rw_at, &case.texels), + ] { + if all[at..at + len] != before[..] { + let got = &all[at..at + len]; + wrong.push(format!( + "the {what} one's bytes {got:02x?}, the codes {before:02x?}" + )); + } + // The rest of the last texel's word and what follows keep theirs. + if all[at + len..at + len + 64].iter().any(|&b| b != SENTINEL) { + let got = &all[at + len..at + len + 16]; + wrong.push(format!("past the {what} one's texels: {got:02x?}")); + } + } + let widths = bytes(&widths); + let widths: Vec = (widths.chunks_exact(4).take(3)) + .map(|c| u32::from_le_bytes(c.try_into().expect("four bytes"))) + .collect(); + if widths != [5, 5, 5] { + wrong.push(format!("widths {widths:?}")); + } + wrong + } + + /// Every format the runtime holds as words, read, written, and read then written as a + /// texture buffer, answers what the CPU decodes of its bytes. + #[test] + fn every_device_reads_and_writes_texture_buffers_of_formats_held_as_words() { + let name = "tb_word_formats.ll"; + let library = match llvm_assemble(&engine_dir().join(name)) { + Ok(bitcode) => Library::from_air(&bitcode).expect("the library"), + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(error) => panic!("{name}: {error:?}"), + }; + each_device("texture buffer words", |name, _, device| { + let _grant = (device.grant_indices(IndexClass::Texture, 1, 3)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let mut failed = Vec::new(); + for case in cases() { + let wrong = run(&device, &library, &case); + match wrong.is_empty() { + true => println!("{name}: {:?}: every answer the CPU's", case.format), + false => { + for w in &wrong { + println!("{name}: {:?}: {w}", case.format); + } + failed.push(case.format); + } + } + } + assert!(failed.is_empty(), "{name}: formats that differ: {failed:?}"); + }); + } +} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN index fd594d3ac..ca115309e 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN +++ b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN @@ -398,6 +398,7 @@ fixture tail_argument_cube_gradient: metal: Apple metal version 32023.921 (metal fixture tail_argument_samplers: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture tail_argument_sparse_samples: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture tail_cases: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) +fixture tb_word_formats: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture tess_draw_bases: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture tess_indexed_patches: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture tess_primitive_ids: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.air.ll b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.air.ll new file mode 100644 index 000000000..674687a20 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.air.ll @@ -0,0 +1,117 @@ +; Generated by scripts/regen-air-fixtures from tb_word_formats.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_buffer_1d_t = type opaque +%struct._sampler_t = type opaque + +; Function Attrs: mustprogress nounwind willreturn +define void @tb_word_formats(<4 x float> addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %0, %struct._texture_buffer_1d_t addrspace(1)* %1, %struct._texture_buffer_1d_t addrspace(1)* %2, %struct._texture_buffer_1d_t addrspace(1)* %3, <4 x float> addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %4, <4 x float> addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %5, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %6, i32 noundef %7) local_unnamed_addr #0 { + %9 = tail call %struct._sampler_t addrspace(2)* @air.get_read_sampler() #4 + %10 = tail call { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly %1, %struct._sampler_t addrspace(2)* %9, i32 %7, i32 1) #5 + %11 = extractvalue { <4 x float>, i8 } %10, 0 + %12 = zext i32 %7 to i64 + %13 = getelementptr inbounds <4 x float>, <4 x float> addrspace(1)* %4, i64 %12 + store <4 x float> %11, <4 x float> addrspace(1)* %13, align 16, !tbaa !26, !alias.scope !29, !noalias !32 + %14 = getelementptr inbounds <4 x float>, <4 x float> addrspace(1)* %0, i64 %12 + %15 = load <4 x float>, <4 x float> addrspace(1)* %14, align 16, !tbaa !26, !alias.scope !37, !noalias !38 + tail call void @air.write_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture %2, i32 %7, <4 x float> %15, i32 2) #6, !alias.scope !39, !noalias !40 + %16 = tail call { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly %3, %struct._sampler_t addrspace(2)* %9, i32 %7, i32 3) #5 + %17 = extractvalue { <4 x float>, i8 } %16, 0 + %18 = getelementptr inbounds <4 x float>, <4 x float> addrspace(1)* %5, i64 %12 + store <4 x float> %17, <4 x float> addrspace(1)* %18, align 16, !tbaa !26, !alias.scope !41, !noalias !42 + tail call void @air.write_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture %3, i32 %7, <4 x float> %15, i32 3) #6, !alias.scope !39, !noalias !40 + %19 = icmp eq i32 %7, 0 + br i1 %19, label %20, label %26 + +20: ; preds = %8 + %21 = tail call i32 @air.get_width_texture_buffer_1d(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly %1) #5, !alias.scope !39, !noalias !40 + store i32 %21, i32 addrspace(1)* %6, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + %22 = tail call i32 @air.get_width_texture_buffer_1d(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly %2) #5, !alias.scope !39, !noalias !40 + %23 = getelementptr inbounds i32, i32 addrspace(1)* %6, i64 1 + store i32 %22, i32 addrspace(1)* %23, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + %24 = tail call i32 @air.get_width_texture_buffer_1d(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly %3) #5, !alias.scope !39, !noalias !40 + %25 = getelementptr inbounds i32, i32 addrspace(1)* %6, i64 2 + store i32 %24, i32 addrspace(1)* %25, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + br label %26 + +26: ; preds = %20, %8 + ret void +} + +; Function Attrs: inaccessiblememonly mustprogress nofree nounwind readonly willreturn +declare %struct._sampler_t addrspace(2)* @air.get_read_sampler() local_unnamed_addr #1 + +; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn +declare { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)*, i32, i32) local_unnamed_addr #2 + +; Function Attrs: argmemonly mustprogress nounwind willreturn +declare void @air.write_texture_buffer_1d.v4f32(%struct._texture_buffer_1d_t addrspace(1)* nocapture, i32, <4 x float>, i32) local_unnamed_addr #3 + +; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn +declare i32 @air.get_width_texture_buffer_1d(%struct._texture_buffer_1d_t addrspace(1)* nocapture readonly) local_unnamed_addr #2 + +attributes #0 = { mustprogress 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 = { inaccessiblememonly mustprogress nofree nounwind readonly willreturn } +attributes #2 = { argmemonly mustprogress nofree nounwind readonly willreturn } +attributes #3 = { argmemonly mustprogress nounwind willreturn } +attributes #4 = { inaccessiblememonly nounwind readonly willreturn } +attributes #5 = { argmemonly nounwind readonly willreturn } +attributes #6 = { argmemonly nounwind willreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9} +!air.compile_options = !{!20, !21, !22} +!llvm.ident = !{!23} +!air.version = !{!24} +!air.language_version = !{!25} + +!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 (<4 x float> addrspace(1)*, %struct._texture_buffer_1d_t addrspace(1)*, %struct._texture_buffer_1d_t addrspace(1)*, %struct._texture_buffer_1d_t addrspace(1)*, <4 x float> addrspace(1)*, <4 x float> addrspace(1)*, i32 addrspace(1)*, i32)* @tb_word_formats, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15, !16, !17, !18, !19} +!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"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", !"values"} +!13 = !{i32 1, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"src"} +!14 = !{i32 2, !"air.texture", !"air.location_index", i32 1, i32 1, !"air.write", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"dst"} +!15 = !{i32 3, !"air.texture", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"rw"} +!16 = !{i32 4, !"air.buffer", !"air.location_index", i32 1, 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", !"reads"} +!17 = !{i32 5, !"air.buffer", !"air.location_index", i32 2, 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", !"rw_reads"} +!18 = !{i32 6, !"air.buffer", !"air.location_index", i32 3, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"widths"} +!19 = !{i32 7, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"} +!20 = !{!"air.compile.denorms_disable"} +!21 = !{!"air.compile.fast_math_enable"} +!22 = !{!"air.compile.framebuffer_fetch_enable"} +!23 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!24 = !{i32 2, i32 8, i32 0} +!25 = !{!"Metal", i32 4, i32 0, i32 0} +!26 = !{!27, !27, i64 0} +!27 = !{!"omnipotent char", !28, i64 0} +!28 = !{!"Simple C++ TBAA"} +!29 = !{!30} +!30 = distinct !{!30, !31, !"air-alias-scope-arg(4)"} +!31 = distinct !{!31, !"air-alias-scopes(tb_word_formats)"} +!32 = !{!33, !34, !35, !36} +!33 = distinct !{!33, !31, !"air-alias-scope-arg(0)"} +!34 = distinct !{!34, !31, !"air-alias-scope-textures"} +!35 = distinct !{!35, !31, !"air-alias-scope-arg(5)"} +!36 = distinct !{!36, !31, !"air-alias-scope-arg(6)"} +!37 = !{!33} +!38 = !{!34, !30, !35, !36} +!39 = !{!34} +!40 = !{!33, !30, !35, !36} +!41 = !{!35} +!42 = !{!33, !34, !30, !36} +!43 = !{!44, !44, i64 0} +!44 = !{!"int", !27, i64 0} +!45 = !{!36} +!46 = !{!33, !34, !30, !35} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.ll b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.ll new file mode 100644 index 000000000..57acc3c5b --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.ll @@ -0,0 +1,114 @@ +; Generated by scripts/regen-air-fixtures from tb_word_formats.metal (llvm-dis of tb_word_formats.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: mustprogress nounwind willreturn +define void @tb_word_formats(ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) %1, ptr addrspace(1) %2, ptr addrspace(1) %3, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %4, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %5, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %6, i32 noundef %7) local_unnamed_addr #0 { + %9 = tail call ptr addrspace(2) @air.get_read_sampler() #4 + %10 = tail call { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(ptr addrspace(1) readonly captures(none) %1, ptr addrspace(2) %9, i32 %7, i32 1) #5 + %11 = extractvalue { <4 x float>, i8 } %10, 0 + %12 = zext i32 %7 to i64 + %13 = getelementptr inbounds <4 x float>, ptr addrspace(1) %4, i64 %12 + store <4 x float> %11, ptr addrspace(1) %13, align 16, !tbaa !26, !alias.scope !29, !noalias !32 + %14 = getelementptr inbounds <4 x float>, ptr addrspace(1) %0, i64 %12 + %15 = load <4 x float>, ptr addrspace(1) %14, align 16, !tbaa !26, !alias.scope !37, !noalias !38 + tail call void @air.write_texture_buffer_1d.v4f32(ptr addrspace(1) captures(none) %2, i32 %7, <4 x float> %15, i32 2) #6, !alias.scope !39, !noalias !40 + %16 = tail call { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(ptr addrspace(1) readonly captures(none) %3, ptr addrspace(2) %9, i32 %7, i32 3) #5 + %17 = extractvalue { <4 x float>, i8 } %16, 0 + %18 = getelementptr inbounds <4 x float>, ptr addrspace(1) %5, i64 %12 + store <4 x float> %17, ptr addrspace(1) %18, align 16, !tbaa !26, !alias.scope !41, !noalias !42 + tail call void @air.write_texture_buffer_1d.v4f32(ptr addrspace(1) captures(none) %3, i32 %7, <4 x float> %15, i32 3) #6, !alias.scope !39, !noalias !40 + %19 = icmp eq i32 %7, 0 + br i1 %19, label %20, label %26 + +20: ; preds = %8 + %21 = tail call i32 @air.get_width_texture_buffer_1d(ptr addrspace(1) readonly captures(none) %1) #5, !alias.scope !39, !noalias !40 + store i32 %21, ptr addrspace(1) %6, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + %22 = tail call i32 @air.get_width_texture_buffer_1d(ptr addrspace(1) readonly captures(none) %2) #5, !alias.scope !39, !noalias !40 + %23 = getelementptr inbounds i32, ptr addrspace(1) %6, i64 1 + store i32 %22, ptr addrspace(1) %23, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + %24 = tail call i32 @air.get_width_texture_buffer_1d(ptr addrspace(1) readonly captures(none) %3) #5, !alias.scope !39, !noalias !40 + %25 = getelementptr inbounds i32, ptr addrspace(1) %6, i64 2 + store i32 %24, ptr addrspace(1) %25, align 4, !tbaa !43, !alias.scope !45, !noalias !46 + br label %26 + +26: ; preds = %20, %8 + ret void +} + +; Function Attrs: mustprogress nofree nounwind willreturn memory(inaccessiblemem: read) +declare ptr addrspace(2) @air.get_read_sampler() local_unnamed_addr #1 + +; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read) +declare { <4 x float>, i8 } @air.read_texture_buffer_1d.v4f32(ptr addrspace(1) readonly captures(none), ptr addrspace(2), i32, i32) local_unnamed_addr #2 + +; Function Attrs: mustprogress nounwind willreturn memory(argmem: readwrite) +declare void @air.write_texture_buffer_1d.v4f32(ptr addrspace(1) captures(none), i32, <4 x float>, i32) local_unnamed_addr #3 + +; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read) +declare i32 @air.get_width_texture_buffer_1d(ptr addrspace(1) readonly captures(none)) local_unnamed_addr #2 + +attributes #0 = { mustprogress 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 = { mustprogress nofree nounwind willreturn memory(inaccessiblemem: read) } +attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) } +attributes #3 = { mustprogress nounwind willreturn memory(argmem: readwrite) } +attributes #4 = { nounwind willreturn memory(inaccessiblemem: read) } +attributes #5 = { nounwind willreturn memory(argmem: read) } +attributes #6 = { nounwind willreturn memory(argmem: readwrite) } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9} +!air.compile_options = !{!20, !21, !22} +!llvm.ident = !{!23} +!air.version = !{!24} +!air.language_version = !{!25} + +!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 @tb_word_formats, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15, !16, !17, !18, !19} +!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"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", !"values"} +!13 = !{i32 1, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"src"} +!14 = !{i32 2, !"air.texture", !"air.location_index", i32 1, i32 1, !"air.write", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"dst"} +!15 = !{i32 3, !"air.texture", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_name", !"texture_buffer", !"air.arg_name", !"rw"} +!16 = !{i32 4, !"air.buffer", !"air.location_index", i32 1, 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", !"reads"} +!17 = !{i32 5, !"air.buffer", !"air.location_index", i32 2, 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", !"rw_reads"} +!18 = !{i32 6, !"air.buffer", !"air.location_index", i32 3, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"widths"} +!19 = !{i32 7, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"} +!20 = !{!"air.compile.denorms_disable"} +!21 = !{!"air.compile.fast_math_enable"} +!22 = !{!"air.compile.framebuffer_fetch_enable"} +!23 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!24 = !{i32 2, i32 8, i32 0} +!25 = !{!"Metal", i32 4, i32 0, i32 0} +!26 = !{!27, !27, i64 0} +!27 = !{!"omnipotent char", !28, i64 0} +!28 = !{!"Simple C++ TBAA"} +!29 = !{!30} +!30 = distinct !{!30, !31, !"air-alias-scope-arg(4)"} +!31 = distinct !{!31, !"air-alias-scopes(tb_word_formats)"} +!32 = !{!33, !34, !35, !36} +!33 = distinct !{!33, !31, !"air-alias-scope-arg(0)"} +!34 = distinct !{!34, !31, !"air-alias-scope-textures"} +!35 = distinct !{!35, !31, !"air-alias-scope-arg(5)"} +!36 = distinct !{!36, !31, !"air-alias-scope-arg(6)"} +!37 = !{!33} +!38 = !{!34, !30, !35, !36} +!39 = !{!34} +!40 = !{!33, !30, !35, !36} +!41 = !{!35} +!42 = !{!33, !34, !30, !36} +!43 = !{!44, !44, i64 0} +!44 = !{!"int", !27, i64 0} +!45 = !{!36} +!46 = !{!33, !34, !30, !35} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.metal b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.metal new file mode 100644 index 000000000..b92843d5c --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/tb_word_formats.metal @@ -0,0 +1,28 @@ +// Owned fixture: texture buffers of the formats the runtime holds as words (A8Unorm, the 8-bit +// sRGB formats, RGB9E5Float, the packed 16-bit formats, BGR10A2Unorm and the extended-range +// formats), read and written through each access. Thread t reads texel t of src into reads[t], +// writes values[t] to texel t of dst, and reads texel t of rw into rw_reads[t] before writing +// values[t] there; thread 0 writes the three widths. A thread past the width reads Metal's +// out-of-bounds texel and writes nothing. Neighbouring threads write neighbouring texels, which +// share a word where a texel is smaller than one. Loop-free. +#include +using namespace metal; + +kernel void tb_word_formats(device const float4 *values [[buffer(0)]], + texture_buffer src [[texture(0)]], + texture_buffer dst [[texture(1)]], + texture_buffer rw [[texture(2)]], + device float4 *reads [[buffer(1)]], + device float4 *rw_reads [[buffer(2)]], + device uint *widths [[buffer(3)]], + uint t [[thread_position_in_grid]]) { + reads[t] = src.read(t); + dst.write(values[t], t); + rw_reads[t] = rw.read(t); + rw.write(values[t], t); + if (t == 0) { + widths[0] = src.get_width(); + widths[1] = dst.get_width(); + widths[2] = rw.get_width(); + } +}