The translator's sample kernel named its heap slot under a stage that does not exist (Stage::Kernel is the compute stage's); fixed with the other error the first build (lavapipe job m3-max-84536) reported. The runtime README states the measured layout, the form, the refusals left with their causes, and names this caller on the stencil-export known gap; API.md documents own_draw and SamplesToBytes and catalogues PrecisionNote::RoundedSampleDepthCopy. The device test gains a 128 x 128 Depth16Unorm row at 4 samples whose 65,536 units are every code once, checks every unit of the copy back out, and asserts the counts: 2 sample copies a row, and 8 stencil bit draws for a stencil row on a device without stencil export, else none.
364 lines
14 KiB
Rust
364 lines
14 KiB
Rust
//! A buffer copied into every sample of a multisample `Stencil8` or `Depth16Unorm` texture through
|
|
//! its plane option, and back out (`copyFromBuffer:...toTexture:...options:` and
|
|
//! `copyFromTexture:...toBuffer:...options:`), under a device cfg, on every device of the cfg's
|
|
//! kind.
|
|
//!
|
|
//! Metal's answer, as measured: Apple's documentation says nothing of a multisample texture in
|
|
//! either copy. Copy unit `i` of the buffer (a stencil's byte, a 16-bit depth's two) lands at texel
|
|
//! `(x, y)` of the region, sample `s`, where `i = (y * width + x) * samples + s`, rows
|
|
//! `bytesPerRow` apart; the samples outside the region keep what they held; and the copy back out
|
|
//! returns the buffer bit for bit. Measured on an Apple M3 Max (macOS 27.0.1), and on a Radeon Pro
|
|
//! 5300M and an Intel UHD Graphics 630 (macOS 26.7.1), each plainly and under Metal's API
|
|
//! validation layer, which reported nothing; the conformance rows `buffer-depth-samples-*` hold
|
|
//! the M3 Max's recording.
|
|
//!
|
|
//! Each row: a Private texture of `RenderTarget | ShaderRead` usage cleared (depth 0.25, stencil
|
|
//! 0x5a) by a pass that draws nothing; the copy in; every sample read by the owned `rt6_samples`
|
|
//! kernels (`rt6_read_stencil`, `rt6_read_depth`, one threadgroup a sample); the copy out into a
|
|
//! buffer holding 0xee. The rows: an 8 x 8 texture whole at 4 and at 2 samples, and a 4 x 3 region
|
|
//! at (2, 1) of one at 4, each where the device makes a texture of that sample count; and a
|
|
//! 128 x 128 `Depth16Unorm` texture at 4 samples whole, whose 65,536 units are every code once
|
|
//! (`(i * 7919 + 13) mod 65536`, 7919 odd): each code goes in and back out bit for bit
|
|
//! (`Texture::SAMPLE_DEPTH_COPY_NOTE`).
|
|
//!
|
|
//! Each row's two copies count 2 in `Metrics::sample_copies`, and a stencil row's copy in counts
|
|
//! 8 in `Metrics::stencil_bit_draws` on a device without `VK_EXT_shader_stencil_export` (the
|
|
//! stencil drawn by bits) and none on one with it; the test prints the form each device took.
|
|
//!
|
|
//! `llvm-as` assembles the fixture; without it the test skips visibly.
|
|
|
|
use crate::tests::command::on_device::each_device;
|
|
use crate::tests::draw::on_device::{run, Buffers};
|
|
use crate::{
|
|
BlitOption, Buffer, BufferLayout, ComputePipelineDescriptor, ComputePipelineState, Device,
|
|
DispatchType, FunctionDescriptor, IndexClass, Library, LoadAction, PixelFormat,
|
|
RenderPassDescriptor, StageSource, StorageMode, StoreAction, TextureDescriptor, TexturePlace,
|
|
TextureType, TextureUsage,
|
|
};
|
|
use metal2vulkan_devtools::{engine_dir, llvm_assemble, AssembleError};
|
|
|
|
/// The texture's side, but the all-codes row's.
|
|
const SIDE: u32 = 8;
|
|
/// The all-codes row's side: 128 x 128 texels of 4 samples hold 65,536 units.
|
|
const ALL_CODES: u32 = 128;
|
|
/// What the pass clears to.
|
|
const CLEAR_DEPTH: f64 = 0.25;
|
|
const CLEAR_STENCIL: u8 = 0x5a;
|
|
/// The byte the copy-back buffer holds before the copy.
|
|
const UNWRITTEN: u8 = 0xee;
|
|
|
|
/// One row: the format, the samples, the texture's side, and the region's origin and size.
|
|
struct Row {
|
|
format: PixelFormat,
|
|
samples: u32,
|
|
side: u32,
|
|
origin: [u32; 2],
|
|
size: [u32; 2],
|
|
}
|
|
|
|
const ROWS: [Row; 7] = [
|
|
whole(PixelFormat::Stencil8, 4, SIDE),
|
|
whole(PixelFormat::Stencil8, 2, SIDE),
|
|
part(PixelFormat::Stencil8),
|
|
whole(PixelFormat::Depth16Unorm, 4, SIDE),
|
|
whole(PixelFormat::Depth16Unorm, 2, SIDE),
|
|
part(PixelFormat::Depth16Unorm),
|
|
whole(PixelFormat::Depth16Unorm, 4, ALL_CODES),
|
|
];
|
|
|
|
const fn whole(format: PixelFormat, samples: u32, side: u32) -> Row {
|
|
Row {
|
|
format,
|
|
samples,
|
|
side,
|
|
origin: [0, 0],
|
|
size: [side, side],
|
|
}
|
|
}
|
|
|
|
const fn part(format: PixelFormat) -> Row {
|
|
Row {
|
|
format,
|
|
samples: 4,
|
|
side: SIDE,
|
|
origin: [2, 1],
|
|
size: [4, 3],
|
|
}
|
|
}
|
|
|
|
impl Row {
|
|
fn stencil(&self) -> bool {
|
|
self.format == PixelFormat::Stencil8
|
|
}
|
|
|
|
fn option(&self) -> BlitOption {
|
|
match self.stencil() {
|
|
true => BlitOption::StencilFromDepthStencil,
|
|
false => BlitOption::DepthFromDepthStencil,
|
|
}
|
|
}
|
|
|
|
/// A copy unit's bytes.
|
|
fn unit(&self) -> usize {
|
|
if self.stencil() {
|
|
1
|
|
} else {
|
|
2
|
|
}
|
|
}
|
|
|
|
/// The units the region holds: one a sample.
|
|
fn units(&self) -> usize {
|
|
(self.size[0] * self.size[1] * self.samples) as usize
|
|
}
|
|
|
|
/// Unit `i`'s value: distinct numbers of the unit's width, as the conformance rows use.
|
|
fn value(&self, i: usize) -> u32 {
|
|
match self.stencil() {
|
|
true => ((i * 37 + 11) & 0xff) as u32,
|
|
false => ((i * 7919 + 13) & 0xffff) as u32,
|
|
}
|
|
}
|
|
|
|
fn input(&self) -> Vec<u8> {
|
|
(0..self.units())
|
|
.flat_map(|i| self.value(i).to_le_bytes()[..self.unit()].to_vec())
|
|
.collect()
|
|
}
|
|
|
|
/// The buffer's layout: tight rows of the region's samples.
|
|
fn layout(&self) -> BufferLayout {
|
|
let row = u64::from(self.size[0] * self.samples) * self.unit() as u64;
|
|
BufferLayout {
|
|
offset: 0,
|
|
bytes_per_row: row,
|
|
bytes_per_image: row * u64::from(self.size[1]),
|
|
}
|
|
}
|
|
|
|
fn name(&self) -> String {
|
|
format!(
|
|
"{:?} at {} samples, {} x {} at ({}, {}) of {side} x {side}",
|
|
self.format,
|
|
self.samples,
|
|
self.size[0],
|
|
self.size[1],
|
|
self.origin[0],
|
|
self.origin[1],
|
|
side = self.side
|
|
)
|
|
}
|
|
}
|
|
|
|
/// The owned fixture `rt6_samples`' library, or `None` where `llvm-as` is missing.
|
|
fn library() -> Option<Library> {
|
|
match llvm_assemble(&engine_dir().join("rt6_samples.ll")) {
|
|
Ok(bitcode) => Some(Library::from_air(&bitcode).expect("the library")),
|
|
Err(AssembleError::Missing(why)) => {
|
|
println!("SKIPPED: {why}");
|
|
None
|
|
}
|
|
Err(error) => panic!("rt6_samples.ll: {error:?}"),
|
|
}
|
|
}
|
|
|
|
fn kernel(name: &str, device: &Device, library: &Library, kernel: &str) -> ComputePipelineState {
|
|
let function = library.function(kernel).expect("the kernel");
|
|
let descriptor =
|
|
ComputePipelineDescriptor::new(StageSource::Function(FunctionDescriptor::new(function)));
|
|
(device.new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers))
|
|
.unwrap_or_else(|error| panic!("{name}: {kernel}: {error}"))
|
|
}
|
|
|
|
/// A Shared buffer's bytes, as the host reads them.
|
|
fn read(buffer: &Buffer) -> Vec<u8> {
|
|
let contents = buffer.contents().expect("a Shared buffer");
|
|
// SAFETY: the buffer is alive, `length` bytes long, and no GPU work uses it.
|
|
unsafe { std::slice::from_raw_parts(contents.as_ptr(), buffer.length() as usize) }.to_vec()
|
|
}
|
|
|
|
/// Whether the word a kernel read of a sample is the unit `value`: a stencil's value, or a 16-bit
|
|
/// depth's code as the float the depth reads as.
|
|
fn holds(stencil: bool, word: u32, value: u32) -> bool {
|
|
match stencil {
|
|
true => word & 0xff == value,
|
|
false => (f64::from(f32::from_bits(word)) - f64::from(value) / 65535.0).abs() <= 1e-7,
|
|
}
|
|
}
|
|
|
|
/// Runs `row` on `device`, its texture at index `index`, and checks Metal's answer.
|
|
fn copy_row(name: &str, device: &Device, library: &Library, row: &Row, index: u32) {
|
|
let what = format!("{name}: {}", row.name());
|
|
let side = row.side;
|
|
let mut d = TextureDescriptor::new(TextureType::Type2DMultisample, row.format, side, side);
|
|
d.sample_count = row.samples;
|
|
d.usage = TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ;
|
|
d.storage_mode = StorageMode::Private;
|
|
let texture =
|
|
(device.new_texture(&d, Some(index))).unwrap_or_else(|error| panic!("{what}: {error}"));
|
|
let input = row.input();
|
|
let samples = (side * side * row.samples) as usize;
|
|
let mut buffers = Buffers::new(device);
|
|
let source = buffers.of(device, &input);
|
|
let back = buffers.of(device, &vec![UNWRITTEN; input.len()]);
|
|
let words = buffers.of(device, &vec![0xab; 4 * samples]);
|
|
let state = kernel(
|
|
name,
|
|
device,
|
|
library,
|
|
match row.stencil() {
|
|
true => "rt6_read_stencil",
|
|
false => "rt6_read_depth",
|
|
},
|
|
);
|
|
|
|
let mut pass = RenderPassDescriptor::default();
|
|
if row.stencil() {
|
|
let s = &mut pass.stencil_attachment;
|
|
s.texture = Some(texture.clone());
|
|
s.load_action = LoadAction::Clear;
|
|
s.store_action = StoreAction::Store;
|
|
s.clear_stencil = u32::from(CLEAR_STENCIL);
|
|
} else {
|
|
let z = &mut pass.depth_attachment;
|
|
z.texture = Some(texture.clone());
|
|
z.load_action = LoadAction::Clear;
|
|
z.store_action = StoreAction::Store;
|
|
z.clear_depth = CLEAR_DEPTH;
|
|
}
|
|
let queue = device.new_command_queue(4).expect("a queue");
|
|
let command_buffer = queue.command_buffer().expect("a command buffer");
|
|
(command_buffer.render_command_encoder(&pass))
|
|
.unwrap_or_else(|error| panic!("{what}: the clear: {error}"))
|
|
.end_encoding();
|
|
let at = TexturePlace {
|
|
slice: 0,
|
|
level: 0,
|
|
origin: [row.origin[0], row.origin[1], 0],
|
|
};
|
|
let size = [row.size[0], row.size[1], 1];
|
|
let mut blit =
|
|
(command_buffer.blit_command_encoder()).unwrap_or_else(|error| panic!("{what}: {error}"));
|
|
(blit.copy_from_buffer_with_options(&source, row.layout(), size, &texture, at, row.option()))
|
|
.unwrap_or_else(|error| panic!("{what}: the copy in: {error}"));
|
|
blit.end_encoding();
|
|
let mut encoder = (command_buffer.compute_command_encoder(DispatchType::Serial))
|
|
.unwrap_or_else(|error| panic!("{what}: {error}"));
|
|
let ok = |result: Result<(), crate::Error>, step: &str| {
|
|
result.unwrap_or_else(|error| panic!("{what}: {step}: {error}"))
|
|
};
|
|
ok(encoder.set_compute_pipeline_state(&state), "the kernel");
|
|
ok(encoder.set_texture(Some(&texture), 0), "the texture");
|
|
ok(encoder.set_buffer(Some(&words), 0, 0), "the words");
|
|
let shape: Vec<u8> = [side, row.samples]
|
|
.iter()
|
|
.flat_map(|word| word.to_le_bytes())
|
|
.collect();
|
|
ok(encoder.set_bytes(&shape, 1), "the shape");
|
|
ok(
|
|
encoder.dispatch_threadgroups([side, side, row.samples], [1, 1, 1]),
|
|
"the reads",
|
|
);
|
|
encoder.end_encoding();
|
|
let mut blit =
|
|
(command_buffer.blit_command_encoder()).unwrap_or_else(|error| panic!("{what}: {error}"));
|
|
(blit.copy_from_texture_to_buffer_with_options(
|
|
&texture,
|
|
at,
|
|
size,
|
|
&back,
|
|
row.layout(),
|
|
row.option(),
|
|
))
|
|
.unwrap_or_else(|error| panic!("{what}: the copy out: {error}"));
|
|
blit.end_encoding();
|
|
let before = device.counters().metrics();
|
|
run(&what, &command_buffer);
|
|
let after = device.counters().metrics();
|
|
|
|
let back = read(&back);
|
|
let moved: Vec<usize> = (0..row.units())
|
|
.filter(|&i| back[i * row.unit()..][..row.unit()] != input[i * row.unit()..][..row.unit()])
|
|
.collect();
|
|
assert!(
|
|
moved.is_empty(),
|
|
"{what}: the copy back out: {} of {} units differ, the first (unit, in, out) {:?}",
|
|
moved.len(),
|
|
row.units(),
|
|
moved.first().map(|&i| {
|
|
let unit = |bytes: &[u8]| bytes[i * row.unit()..][..row.unit()].to_vec();
|
|
(i, unit(&input), unit(&back))
|
|
})
|
|
);
|
|
let copies = after.sample_copies - before.sample_copies;
|
|
assert_eq!(copies, 2, "{what}: the copies' count");
|
|
let bits = after.stencil_bit_draws - before.stencil_bit_draws;
|
|
let exports = device.translator().profile().fragment().stencil_export;
|
|
let want_bits = match row.stencil() && !exports {
|
|
true => 8,
|
|
false => 0,
|
|
};
|
|
assert_eq!(bits, want_bits, "{what}: the stencil's draws by bits");
|
|
if row.stencil() {
|
|
println!("M2V-FORM {what}: the stencil copied in with {bits} draws by bits (stencil export {exports})");
|
|
}
|
|
let words: Vec<u32> = (read(&words).as_chunks::<4>().0.iter())
|
|
.map(|word| u32::from_le_bytes(*word))
|
|
.collect();
|
|
let clear = match row.stencil() {
|
|
true => u32::from(CLEAR_STENCIL),
|
|
false => (CLEAR_DEPTH * 65535.0).round() as u32,
|
|
};
|
|
let n = row.samples;
|
|
let mut wrong = Vec::new();
|
|
for y in 0..side {
|
|
for x in 0..side {
|
|
for s in 0..n {
|
|
let (rx, ry) = (x.wrapping_sub(row.origin[0]), y.wrapping_sub(row.origin[1]));
|
|
let want = match rx < row.size[0] && ry < row.size[1] {
|
|
true => row.value(((ry * row.size[0] + rx) * n + s) as usize),
|
|
false => clear,
|
|
};
|
|
let word = words[((y * side + x) * n + s) as usize];
|
|
if !holds(row.stencil(), word, want) {
|
|
wrong.push((x, y, s, word, want));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert!(
|
|
wrong.is_empty(),
|
|
"{what}: {} of {samples} samples are not Metal's, the first (x, y, sample, read, unit) {:?}",
|
|
wrong.len(),
|
|
wrong.first()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn every_device_copies_bytes_into_and_out_of_each_sample_of_a_depth_or_stencil_texture() {
|
|
let Some(library) = library() else {
|
|
return;
|
|
};
|
|
each_device("multisample_bytes", |name, _, device| {
|
|
let _textures = (device.grant_indices(IndexClass::Texture, 4096, ROWS.len() as u32))
|
|
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
|
let mut ran = Vec::new();
|
|
for (at, row) in ROWS.iter().enumerate() {
|
|
if !device.supports_texture_sample_count(row.samples) {
|
|
println!(
|
|
"{name}: {} not run: the device makes no texture of {} samples",
|
|
row.name(),
|
|
row.samples
|
|
);
|
|
continue;
|
|
}
|
|
copy_row(name, &device, &library, row, 4096 + at as u32);
|
|
ran.push(row.name());
|
|
}
|
|
println!(
|
|
"{name}: each sample written and read back as Metal does: {}",
|
|
ran.join("; ")
|
|
);
|
|
});
|
|
}
|