From 039de2090e48e8b3bf7c738f9c2d5f2b95803e66 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 06:41:25 -0800 Subject: [PATCH] Tests: a sampled cube read by face reads its texels under the identity domains Red first for L215 (a). Under the identity domains a texturecube or texturecube_array read by face (access::read or access::sample) is refused, Unsupported{IdentityDomains} naming ImageViewShape: the read takes the binder's 2D-array view, which the identity arrays do not hold. These tests now ask for Metal's answer, the texel read(coord, face[, cube], lod) names. - engine_texture: texture_ops' cube_faces compiles and passes L3 on every profile the identity domains derive, with no ImageViewShape, its textures elements of the sampled float cube and cube-array classes, the plain sampler array declared, and an explicit-level sample where it fetches nothing. The CPU interpreter models no sampler, so values are a device's. - runtime cube_reads (device cfg, under the validation layer): an RGBA32Float and an RGBA32Uint cube (4 x 4, three levels) and an R32Sint cube array (two cubes, two levels) read at every texel of every face, level and cube, each the uploaded texel bit for bit; an RGBA8Unorm_sRGB cube read the same way equals a 2D array's fetches of the same bytes (the device's decode); and reads past the level's extent on each axis, at face 6 and past, at the level count and past, at the cube count and past, and at u32::MAX on each operand are Metal's out-of-bounds texel (zero, alpha 1 for R32Sint). - validation engine_textures: the cube_faces case through the runtime, which asserted the refusal, now asserts its values: face 3's texel, cube 1's face 4 (layer 10), then width 1, 2 cubes and 1 level. --- .../src/tests/cube_reads.rs | 457 ++++++++++++++++++ crates/metal2vulkan-runtime/src/tests/mod.rs | 1 + .../src/candidate/tests/engine_textures.rs | 24 +- .../metal2vulkan/src/tests/engine_texture.rs | 78 ++- 4 files changed, 543 insertions(+), 17 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/cube_reads.rs diff --git a/crates/metal2vulkan-runtime/src/tests/cube_reads.rs b/crates/metal2vulkan-runtime/src/tests/cube_reads.rs new file mode 100644 index 000000000..9c39cf35f --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/cube_reads.rs @@ -0,0 +1,457 @@ +//! A sampled cube's and cube array's read by face, under a device cfg on every device of the +//! cfg's kind and under the validation layer: the translator samples the texture's cube view at the +//! read texel's centre and the read's level through element 0 of the sampler arrays, the default +//! sampler (nearest, every level reached), and each read is the texel Metal's `read(coord, face, +//! lod)` (`read(coord, face, cube, lod)` of a cube array) names, bit for bit. +//! +//! `cr_float` reads an `RGBA32Float` cube and `cr_uint` an `RGBA32Uint` cube, each 4 × 4 with +//! three levels, at every texel of every face and level, the first and last texel of each row +//! and column among them; `cr_int` reads an `R32Sint` cube array of two cubes and two levels at +//! every texel of every face, cube and level. Each read is the texel uploaded there. `cr_float` +//! over an `RGBA8Unorm_sRGB` cube is compared with `cr_ref`'s read of a 2D array of six slices +//! holding the same bytes at the same levels: the device's sRGB decode, through a fetch. Each +//! kernel also reads past the level's extent on each axis (inside level 0's), at face 6 and past, +//! at the level count and past, at the cube count and past, and at `u32::MAX` on each operand: +//! each Metal's out-of-bounds texel, zero with alpha 1 for a format without alpha. +//! +//! `llvm-as` assembles the kernels; 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::tests::draw::on_device::Buffers; + use crate::{ + CommandQueue, ComputePipelineDescriptor, Device, DispatchType, FunctionDescriptor, + IndexClass, Library, PixelFormat, Region, StageSource, StorageMode, Texture, + TextureDescriptor, TextureType, TextureUsage, + }; + use metal2vulkan_devtools::{llvm_assemble_text, AssembleError}; + + /// Level 0's side of every texture. + const SIDE: u32 = 4; + + /// The threads of a threadgroup: a dispatch's reads are padded to a multiple of it. + const GROUP: u32 = 32; + + /// The bytes past the last read's texel, which no thread writes. + const SPARE: u8 = 0xA5; + + /// One read's operands: `x`, `y`, the face, the level and the cube (0 for a cube). + type At = [u32; 5]; + + /// The owned synthetic kernels: thread `i` reads at the operands of the `i`th 32 bytes of `at` + /// (`x`, `y`, face, level; cube) and writes the texel to the `i`th 16 bytes of `out`. + const KERNELS: &str = r#"; Owned synthetic kernels. +source_filename = "cube_reads.metal" + +define void @cr_float(ptr addrspace(1) %t, ptr addrspace(1) noundef %at, ptr addrspace(1) noundef %out, i32 noundef %gid) { + %s = tail call ptr addrspace(2) @air.get_read_sampler() + %z = zext i32 %gid to i64 + %i = shl i64 %z, 1 + %pa = getelementptr inbounds <4 x i32>, ptr addrspace(1) %at, i64 %i + %a = load <4 x i32>, ptr addrspace(1) %pa, align 16 + %xy = shufflevector <4 x i32> %a, <4 x i32> poison, <2 x i32> + %f = extractelement <4 x i32> %a, i64 2 + %l = extractelement <4 x i32> %a, i64 3 + %r = tail call { <4 x float>, i8 } @air.read_texture_cube.v4f32(ptr addrspace(1) %t, ptr addrspace(2) %s, <2 x i32> %xy, i32 %f, i32 %l, i32 1) + %v = extractvalue { <4 x float>, i8 } %r, 0 + %po = getelementptr inbounds <4 x float>, ptr addrspace(1) %out, i64 %z + store <4 x float> %v, ptr addrspace(1) %po, align 16 + ret void +} + +define void @cr_uint(ptr addrspace(1) %t, ptr addrspace(1) noundef %at, ptr addrspace(1) noundef %out, i32 noundef %gid) { + %s = tail call ptr addrspace(2) @air.get_read_sampler() + %z = zext i32 %gid to i64 + %i = shl i64 %z, 1 + %pa = getelementptr inbounds <4 x i32>, ptr addrspace(1) %at, i64 %i + %a = load <4 x i32>, ptr addrspace(1) %pa, align 16 + %xy = shufflevector <4 x i32> %a, <4 x i32> poison, <2 x i32> + %f = extractelement <4 x i32> %a, i64 2 + %l = extractelement <4 x i32> %a, i64 3 + %r = tail call { <4 x i32>, i8 } @air.read_texture_cube.u.v4i32(ptr addrspace(1) %t, ptr addrspace(2) %s, <2 x i32> %xy, i32 %f, i32 %l, i32 1) + %v = extractvalue { <4 x i32>, i8 } %r, 0 + %po = getelementptr inbounds <4 x i32>, ptr addrspace(1) %out, i64 %z + store <4 x i32> %v, ptr addrspace(1) %po, align 16 + ret void +} + +define void @cr_int(ptr addrspace(1) %t, ptr addrspace(1) noundef %at, ptr addrspace(1) noundef %out, i32 noundef %gid) { + %s = tail call ptr addrspace(2) @air.get_read_sampler() + %z = zext i32 %gid to i64 + %i = shl i64 %z, 1 + %pa = getelementptr inbounds <4 x i32>, ptr addrspace(1) %at, i64 %i + %a = load <4 x i32>, ptr addrspace(1) %pa, align 16 + %j = or i64 %i, 1 + %pb = getelementptr inbounds <4 x i32>, ptr addrspace(1) %at, i64 %j + %b = load <4 x i32>, ptr addrspace(1) %pb, align 16 + %xy = shufflevector <4 x i32> %a, <4 x i32> poison, <2 x i32> + %f = extractelement <4 x i32> %a, i64 2 + %l = extractelement <4 x i32> %a, i64 3 + %c = extractelement <4 x i32> %b, i64 0 + %r = tail call { <4 x i32>, i8 } @air.read_texture_cube_array.s.v4i32(ptr addrspace(1) %t, ptr addrspace(2) %s, <2 x i32> %xy, i32 %f, i32 %c, i32 %l, i32 1) + %v = extractvalue { <4 x i32>, i8 } %r, 0 + %po = getelementptr inbounds <4 x i32>, ptr addrspace(1) %out, i64 %z + store <4 x i32> %v, ptr addrspace(1) %po, align 16 + ret void +} + +define void @cr_ref(ptr addrspace(1) %t, ptr addrspace(1) noundef %at, ptr addrspace(1) noundef %out, i32 noundef %gid) { + %s = tail call ptr addrspace(2) @air.get_read_sampler() + %z = zext i32 %gid to i64 + %i = shl i64 %z, 1 + %pa = getelementptr inbounds <4 x i32>, ptr addrspace(1) %at, i64 %i + %a = load <4 x i32>, ptr addrspace(1) %pa, align 16 + %xy = shufflevector <4 x i32> %a, <4 x i32> poison, <2 x i32> + %f = extractelement <4 x i32> %a, i64 2 + %l = extractelement <4 x i32> %a, i64 3 + %r = tail call { <4 x float>, i8 } @air.read_texture_2d_array.v4f32(ptr addrspace(1) %t, ptr addrspace(2) %s, <2 x i32> %xy, i32 %f, <2 x i32> zeroinitializer, i32 %l, i32 1) + %v = extractvalue { <4 x float>, i8 } %r, 0 + %po = getelementptr inbounds <4 x float>, ptr addrspace(1) %out, i64 %z + store <4 x float> %v, ptr addrspace(1) %po, align 16 + ret void +} + +declare ptr addrspace(2) @air.get_read_sampler() +declare { <4 x float>, i8 } @air.read_texture_cube.v4f32(ptr addrspace(1), ptr addrspace(2), <2 x i32>, i32, i32, i32) +declare { <4 x i32>, i8 } @air.read_texture_cube.u.v4i32(ptr addrspace(1), ptr addrspace(2), <2 x i32>, i32, i32, i32) +declare { <4 x i32>, i8 } @air.read_texture_cube_array.s.v4i32(ptr addrspace(1), ptr addrspace(2), <2 x i32>, i32, i32, i32, i32) +declare { <4 x float>, i8 } @air.read_texture_2d_array.v4f32(ptr addrspace(1), ptr addrspace(2), <2 x i32>, i32, <2 x i32>, i32, i32) + +!air.kernel = !{!0, !10, !20, !30} +!air.version = !{!8} +!air.language_version = !{!9} +!0 = !{ptr @cr_float, !1, !2} +!1 = !{} +!2 = !{!3, !4, !5, !6} +!3 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texturecube", !"air.arg_name", !"t"} +!4 = !{i32 1, !"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", !"uint4", !"air.arg_name", !"at"} +!5 = !{i32 2, !"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", !"out"} +!6 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"gid"} +!8 = !{i32 2, i32 8, i32 0} +!9 = !{!"Metal", i32 4, i32 0, i32 0} +!10 = !{ptr @cr_uint, !1, !11} +!11 = !{!12, !4, !13, !6} +!12 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texturecube", !"air.arg_name", !"t"} +!13 = !{i32 2, !"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", !"uint4", !"air.arg_name", !"out"} +!20 = !{ptr @cr_int, !1, !21} +!21 = !{!22, !4, !23, !6} +!22 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texturecube_array", !"air.arg_name", !"t"} +!23 = !{i32 2, !"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", !"int4", !"air.arg_name", !"out"} +!30 = !{ptr @cr_ref, !1, !31} +!31 = !{!32, !4, !5, !6} +!32 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texture2d_array", !"air.arg_name", !"t"} +"#; + + /// A texture's bytes and the words a read of each texel answers. + struct Texels { + format: PixelFormat, + /// Bytes a texel. + size: u32, + /// The bytes of texel `(x, y)` of level `level` of layer `layer` (`6 · cube + face`). + bytes: fn(u32, u32, u32, u32) -> Vec, + /// The words Metal's read of that texel answers. + read: fn(u32, u32, u32, u32) -> [u32; 4], + /// The words of Metal's out-of-bounds texel. + outside: [u32; 4], + } + + fn float_texel(layer: u32, level: u32, x: u32, y: u32) -> [f32; 4] { + [ + layer as f32 + 0.25, + -2.5 * level as f32 - 1.0, + x as f32 + 0.125, + 3.0 * y as f32 + 0.5, + ] + } + + fn uint_texel(layer: u32, level: u32, x: u32, y: u32) -> [u32; 4] { + [ + 0xC000_0000 | layer << 16 | level << 8 | x, + 0x8000_0001 + y, + layer * 1000 + level, + u32::MAX - x - 16 * y, + ] + } + + fn int_texel(layer: u32, level: u32, x: u32, y: u32) -> i32 { + -1 - (layer * 10_000 + level * 1000 + y * 16 + x) as i32 + } + + fn srgb_texel(layer: u32, level: u32, x: u32, y: u32) -> Vec { + [ + layer * 40 + x * 7 + level * 3 + 1, + y * 50 + layer * 9 + 17, + x * y * 13 + level * 77 + 5, + 255 - layer * 20 - level, + ] + .map(|v| (v % 256) as u8) + .to_vec() + } + + const FLOAT: Texels = Texels { + format: PixelFormat::RGBA32Float, + size: 16, + bytes: |layer, level, x, y| { + (float_texel(layer, level, x, y).iter()) + .flat_map(|v| v.to_le_bytes()) + .collect() + }, + read: |layer, level, x, y| float_texel(layer, level, x, y).map(f32::to_bits), + outside: [0; 4], + }; + + const UINT: Texels = Texels { + format: PixelFormat::RGBA32Uint, + size: 16, + bytes: |layer, level, x, y| { + (uint_texel(layer, level, x, y).iter()) + .flat_map(|v| v.to_le_bytes()) + .collect() + }, + read: uint_texel, + outside: [0; 4], + }; + + const INT: Texels = Texels { + format: PixelFormat::R32Sint, + size: 4, + bytes: |layer, level, x, y| int_texel(layer, level, x, y).to_le_bytes().to_vec(), + read: |layer, level, x, y| [int_texel(layer, level, x, y) as u32, 0, 0, 1], + outside: [0, 0, 0, 1], + }; + + /// The sRGB texture's reads are compared with the 2D array's, not with these words. + const SRGB: Texels = Texels { + format: PixelFormat::RGBA8UnormSRGB, + size: 4, + bytes: srgb_texel, + read: |_, _, _, _| [0; 4], + outside: [0; 4], + }; + + /// Every texel of every face, level and cube of a texture of `levels` levels and `cubes` + /// cubes, then reads outside it, padded to a multiple of [`GROUP`] with reads at face 6. + fn points(levels: u32, cubes: u32) -> Vec { + let mut at = Vec::new(); + for cube in 0..cubes { + for level in 0..levels { + let w = SIDE >> level; + for face in 0..6 { + for y in 0..w { + for x in 0..w { + at.push([x, y, face, level, cube]); + } + } + } + } + } + for level in 0..levels { + let w = SIDE >> level; + at.extend([ + [w, 0, 0, level, 0], + [0, w, 5, level, 0], + [w - 1, w, 3, level, 0], + [w, w - 1, 2, level, 0], + [2 * SIDE, 0, 1, level, 0], + ]); + } + let max = u32::MAX; + at.extend([ + [0, 0, 6, 0, 0], + [1, 1, 7, 1, 0], + [0, 0, max, 0, 0], + [0, 0, 0, levels, 0], + [1, 2, 4, levels + 3, 0], + [0, 0, 4, max, 0], + [max, 0, 0, 0, 0], + [0, max, 1, 0, 0], + [max, max, max, max, 0], + ]); + if cubes > 1 { + at.extend([ + [0, 0, 0, 0, cubes], + [1, 2, 3, 1, cubes + 1], + [0, 0, 5, 0, max], + ]); + } + while at.len() % GROUP as usize != 0 { + at.push([0, 0, 6, 0, 0]); + } + at + } + + /// Whether read `a` of a texture of `levels` levels and `cubes` cubes is inside it. + fn inside(a: &At, levels: u32, cubes: u32) -> bool { + let [x, y, face, level, cube] = *a; + level < levels && x < SIDE >> level && y < SIDE >> level && face < 6 && cube < cubes + } + + /// A Shared, read-only texture of `texture_type` in `texels`' format, of `levels` levels and + /// `slices` slices (faces), at index `index`, every texel uploaded. + fn texture( + name: &str, + device: &Device, + (texture_type, texels, levels, slices): (TextureType, &Texels, u32, u32), + index: u32, + ) -> Texture { + let mut d = TextureDescriptor::new(texture_type, texels.format, SIDE, SIDE); + d.mipmap_level_count = levels; + d.array_length = match texture_type { + TextureType::CubeArray => slices / 6, + TextureType::Cube => 1, + _ => slices, + }; + d.usage = TextureUsage::SHADER_READ; + d.storage_mode = StorageMode::Shared; + let texture = (device.new_texture(&d, Some(index))) + .unwrap_or_else(|error| panic!("{name}: {texture_type:?}: {error}")); + for slice in 0..slices { + for level in 0..levels { + let w = SIDE >> level; + let bytes: Vec = (0..w) + .flat_map(|y| (0..w).flat_map(move |x| (texels.bytes)(slice, level, x, y))) + .collect(); + let row = u64::from(w * texels.size); + let region = Region { + origin: [0; 3], + size: [w, w, 1], + }; + (texture.replace_region( + region, + level, + slice, + crate::HostBytes::from(&bytes[..]), + row, + row * u64::from(w), + )) + .unwrap_or_else(|error| panic!("{name}: slice {slice}, level {level}: {error}")); + } + } + texture + } + + /// The words kernel `entry` reads of `texture` at each of `at`, the spare bytes past them + /// checked kept. + fn reads( + name: &str, + (device, library, queue, buffers): (&Device, &Library, &CommandQueue, &mut Buffers), + entry: &str, + texture: &Texture, + at: &[At], + ) -> Vec<[u32; 4]> { + let function = library.function(entry).expect("the kernel"); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(function), + )); + let state = (device + .new_compute_pipeline_state(&descriptor, |samplers| Ok(vec![0; samplers.len()]))) + .unwrap_or_else(|error| panic!("{name}: {entry}: {error}")); + let words: Vec = (at.iter()) + .flat_map(|&[x, y, face, level, cube]| [x, y, face, level, cube, 0, 0, 0]) + .flat_map(u32::to_le_bytes) + .collect(); + let at_buffer = buffers.of(device, &words); + let out = buffers.of(device, &vec![SPARE; 16 * at.len() + 16]); + 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("a state"); + encoder.set_texture(Some(texture), 0).expect("the texture"); + encoder.set_buffer(Some(&at_buffer), 0, 0).expect("at"); + encoder.set_buffer(Some(&out), 0, 1).expect("out"); + let groups = at.len() as u32 / GROUP; + (encoder.dispatch_threadgroups([groups, 1, 1], [GROUP, 1, 1])) + .unwrap_or_else(|error| panic!("{name}: {entry}: {error}")); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(name, &command_buffer); + assert_eq!(command_buffer.error(), None, "{name}: {entry}"); + let contents = out.contents().expect("a Shared buffer"); + // SAFETY: the dispatch that wrote the buffer completed, and its writes reach the host. + let bytes = unsafe { std::slice::from_raw_parts(contents.as_ptr(), 16 * at.len() + 16) }; + let (texels, spare) = bytes.split_at(16 * at.len()); + assert_eq!(spare, [SPARE; 16], "{name}: {entry}: the spare bytes"); + texels + .chunks_exact(16) + .map(|t| { + let word = |k: usize| u32::from_le_bytes([t[k], t[k + 1], t[k + 2], t[k + 3]]); + [word(0), word(4), word(8), word(12)] + }) + .collect() + } + + /// Each kernel's read at every texel of every face, level and cube is the texel there, and at + /// every operand outside the texture Metal's out-of-bounds texel; the sRGB cube's reads are the + /// 2D array's fetches of the same bytes, the out-of-bounds ones zero. + #[test] + fn every_device_reads_a_cube_by_face_as_its_texels() { + let bitcode = match llvm_assemble_text(KERNELS) { + Ok(bitcode) => bitcode, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(error) => panic!("cube_reads: {error:?}"), + }; + let library = Library::from_air(&bitcode).expect("the library"); + each_device("cube_reads", |name, _, device| { + let _textures = (device.grant_indices(IndexClass::Texture, 1, 5)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let mut buffers = Buffers::sized(&device, 1 << 20); + let queue = device.new_command_queue(4).expect("a queue"); + let mut checked = 0; + for (index, entry, texture_type, texels, levels, cubes) in [ + (1, "cr_float", TextureType::Cube, &FLOAT, 3, 1), + (2, "cr_uint", TextureType::Cube, &UINT, 3, 1), + (3, "cr_int", TextureType::CubeArray, &INT, 2, 2), + ] { + let shape = (texture_type, texels, levels, 6 * cubes); + let t = texture(name, &device, shape, index); + let at = points(levels, cubes); + let got = reads( + name, + (&device, &library, &queue, &mut buffers), + entry, + &t, + &at, + ); + for (a, got) in at.iter().zip(&got) { + let &[x, y, face, level, cube] = a; + let want = match inside(a, levels, cubes) { + true => (texels.read)(6 * cube + face, level, x, y), + false => texels.outside, + }; + assert_eq!(*got, want, "{name}: {entry}: read at {a:?}"); + } + checked += at.len(); + } + // The sRGB cube's reads are the device's decode, as the 2D array's fetches of the + // same bytes take it. + let cube = texture(name, &device, (TextureType::Cube, &SRGB, 3, 6), 4); + let array = texture(name, &device, (TextureType::Type2DArray, &SRGB, 3, 6), 5); + let at = points(3, 1); + let cx = (&device, &library, &queue, &mut buffers); + let got = reads(name, cx, "cr_float", &cube, &at); + let cx = (&device, &library, &queue, &mut buffers); + let want = reads(name, cx, "cr_ref", &array, &at); + for ((a, got), want) in at.iter().zip(&got).zip(&want) { + assert_eq!(got, want, "{name}: the sRGB cube's read at {a:?}"); + if !inside(a, 3, 1) { + assert_eq!(*got, [0; 4], "{name}: the sRGB cube's read at {a:?}"); + } + } + let decoded: std::collections::BTreeSet<[u32; 4]> = (at.iter().zip(&want)) + .filter(|(a, _)| inside(a, 3, 1)) + .map(|(_, w)| *w) + .collect(); + assert!( + decoded.len() > 100, + "{name}: the sRGB texels decode alike: {}", + decoded.len() + ); + checked += at.len(); + println!("{name}: {checked} cube reads by face, each Metal's texel"); + }); + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index af8845ff0..d26d16b9b 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -22,6 +22,7 @@ mod completion_orders; mod compute; mod copy_out; mod copy_over; +mod cube_reads; mod cube_views; mod d3d11; mod deep_threadgroups; diff --git a/crates/metal2vulkan-validation/src/candidate/tests/engine_textures.rs b/crates/metal2vulkan-validation/src/candidate/tests/engine_textures.rs index 6258b0950..b1950f451 100644 --- a/crates/metal2vulkan-validation/src/candidate/tests/engine_textures.rs +++ b/crates/metal2vulkan-validation/src/candidate/tests/engine_textures.rs @@ -179,12 +179,13 @@ fn a_read_write_texture_sees_its_own_fenced_write() { } /// Cube and cube-array reads by face and layer, and their size queries (`texture_ops.ll`'s -/// `cube_faces`), through the runtime: the translator refuses a cube texture argument the kernel -/// reads by face under the identity domains, the runtime's (`Unsupported{IdentityDomains}`, -/// `NotImplemented`), so the run is refused before it runs. +/// `cube_faces`), through the runtime, whose translator samples a sampled cube's element at the +/// read texel's centre under the identity domains: texel `(0, 0)` of face 3 of the cube, of face 4 +/// of the cube array's cube 1 (its layer 10), each layer `l` holding `(l, l + 0.5, 0, 1)`, then the +/// cube's width 1, the array's 2 cubes and the cube's level count 1. #[cfg(target_os = "linux")] #[test] -fn cube_and_cube_array_face_reads_are_refused_under_the_identity_domains() { +fn cube_and_cube_array_face_reads_read_their_faces_under_the_identity_domains() { let face = |layer: u32| [layer as f32, layer as f32 + 0.5, 0.0, 1.0]; let faces = |count: u32| -> Vec { (0..count).flat_map(face).collect() }; let case = kernel( @@ -209,16 +210,11 @@ fn cube_and_cube_array_face_reads_are_refused_under_the_identity_domains() { [1, 1, 1], buffer_output(48), ); - match run(&case, &fixture("texture_ops.ll")) { - Err(ExecutorError::NotSupported(crate::check::admission::NotSupported::Product { - outcome: - crate::engine::Outcome::Unsupported { - reason: crate::engine::UnsupportedReason::NotImplemented, - feature, - }, - .. - })) => assert_eq!(feature, "IdentityDomains"), - other => panic!("expected the identity domains' refusal: {other:?}"), + let expected: Vec = [face(3), face(10), [1.0, 2.0, 1.0, 0.0]].concat(); + let bitcode = fixture("texture_ops.ll"); + for (name, run) in runs() { + let run = run(&case, &bitcode).unwrap_or_else(|error| panic!("{name}: {error}")); + assert_eq!(run.output, floats(&expected), "{name}"); } } diff --git a/crates/metal2vulkan/src/tests/engine_texture.rs b/crates/metal2vulkan/src/tests/engine_texture.rs index 7c08aa00b..7dc5a1de7 100644 --- a/crates/metal2vulkan/src/tests/engine_texture.rs +++ b/crates/metal2vulkan/src/tests/engine_texture.rs @@ -10,8 +10,9 @@ //! - The bgra8/rgba8 storage-texture pair compiles to distinct keys and modules wherever bgra8 is //! not storage-capable, the bgra8 one swizzling (the golden hashes check the other half). //! - Texel buffers are buffer images with their range fact; cube reads use the 2D-array view -//! (`ImageViewShape`); `is_null_texture` reads a presence constant, and the census counts the -//! code only a nil texture's arm runs apart. +//! (`ImageViewShape`), and under the identity domains sample the cube element; +//! `is_null_texture` reads a presence constant, and the census counts the code only a nil +//! texture's arm runs apart. //! - Writes are guarded exactly when `robustImageAccess` is missing; reads clamp exactly under //! containment without it. //! - The image-operand legality matrix: the refusals of unmodelled forms. @@ -21,6 +22,7 @@ use std::collections::BTreeMap; use std::sync::atomic::{AtomicU64, Ordering}; +use super::engine_word_phases::identity_profiles; use crate::budget::SPIRV_WORDS; use crate::device::profile::DeviceProfile; use crate::device::requirements::VkFeature; @@ -35,8 +37,12 @@ use crate::pipeline::artifact::PrecisionNote; use crate::pipeline::compile::{compile_bitcode, EntryRef}; use crate::pipeline::facts::RuntimeFact; use crate::pipeline::interface::SlotInterface; +use crate::pipeline::link::{link, LinkInputs}; use crate::pipeline::lower_inputs::StorageImageClass; -use crate::pipeline::sym::{SlotClass, SlotRef, SpecKey, Stage}; +use crate::pipeline::sym::{ + HandleClass, ImageAccess, ImageDim, ImageHandleClass, ResourceKind, SampledType, SlotClass, + SlotRef, SpecKey, Stage, StorageFormatClass, +}; use crate::pipeline::testing::{ compile, compile_kept, engine_dir, link_and_validate, llvm_assemble, meter, profile, AssembleError, @@ -131,6 +137,7 @@ fn opcodes(words: &[u32]) -> Vec { const OP_VECTOR_SHUFFLE: u16 = 79; const OP_IMAGE_QUERY_SIZE: u16 = 104; const OP_IMAGE_QUERY_LEVELS: u16 = 106; +const OP_IMAGE_SAMPLE_EXPLICIT_LOD: u16 = 88; const OP_IMAGE_FETCH: u16 = 95; const OP_IMAGE_READ: u16 = 98; const OP_IMAGE_WRITE: u16 = 99; @@ -686,6 +693,71 @@ fn cube_reads_use_the_array_view() { } } +/// Under the identity domains a sampled cube or cube array read by face is its cube element, the +/// texture's cube view, which the read samples at the texel's centre through element 0 of the +/// plain sampler array (`emit::cube_face_texel`): `cube_faces` compiles and passes L3 on every +/// profile the identity domains derive, states no `ImageViewShape`, takes its two textures from +/// the sampled float cube and cube-array classes and its sampler from the plain array, and samples +/// at an explicit level where it fetches nothing. The texels it reads are a device's (the runtime's +/// `tests::cube_reads`, and the validation case of `cube_faces`): the CPU interpreter models no +/// sampler. +#[test] +fn an_identity_cube_read_by_face_samples_its_cube_element() { + let Some(bc) = fixture("texture_ops") else { + return; + }; + let profiles = identity_profiles(); + assert!( + !profiles.is_empty(), + "no profile the identity domains derive" + ); + let cube = |arrayed| { + ResourceKind::Heap(HandleClass::Image(ImageHandleClass { + dim: ImageDim::Cube, + arrayed, + multisampled: false, + sampled_type: SampledType::Float, + depth_compare: false, + access: ImageAccess::Sampled, + format: StorageFormatClass::NotStorage, + })) + }; + for (stem, p) in profiles { + let (a, plan, _) = + compile_kept(&bc, "cube_faces", &p).unwrap_or_else(|e| panic!("{stem}: {e}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{stem}: L3: {e}")); + assert!( + !(plan.facts.iter()).any(|f| matches!(f, RuntimeFact::ImageViewShape { .. })), + "{stem}: {:?}", + plan.facts + ); + let linked = + link(&[&a], &LinkInputs::standalone(), &p).unwrap_or_else(|e| panic!("{stem}: {e}")); + let at = |binding: (u32, u32)| -> Vec { + let mut kinds: Vec = (linked.table.bindings.iter()) + .filter(|(_, b)| **b == binding) + .map(|(s, _)| s.kind) + .collect(); + kinds.sort(); + kinds + }; + let mut want = vec![cube(false), cube(true)]; + want.sort(); + assert_eq!(at((1, 0)), want, "{stem}: the sampled classes"); + assert_eq!( + at((1, 2)), + vec![ResourceKind::Heap(HandleClass::Sampler { + compare: false, + integer: false + })], + "{stem}: the sampler arrays" + ); + let ops = opcodes(&a.module.words); + assert!(ops.contains(&OP_IMAGE_SAMPLE_EXPLICIT_LOD), "{stem}"); + assert!(!ops.contains(&OP_IMAGE_FETCH), "{stem}"); + } +} + /// `is_null_texture` reads the slot's presence constant, a `ResourcePresence` fact; a storage /// slot only queried needs no format feature; a size query at a nonzero level answers from the /// first level on every profile.