Tests: an older heap texture read before a newer one over its bytes is first written

A newer texture placed over an older one's bytes takes its move home from
UNDEFINED in the upload ahead of its first command buffer. Vulkan lets that
move write the image's memory: RADV initializes DCC, and ANV ambiguates the
CCS, each inside the image's own range. If that command buffer reads the older
texture before writing the newer one, the move can change bytes the read
still needs.

The test takes that shape: for four newer shapes, each smaller than the older
512 by 512 RGBA16Float texture, A is written in one command buffer, which
completes. B is then placed over A's bytes, and a second command buffer reads
A, then writes B. Each read of A must be A's write.

No lab driver showed the loss, so the test lands as coverage, and its comment
says so:
- Radeon 780M (RADV): m3-max-78630.
- Iris Xe (ANV, Mesa 26.2.6): m3-max-78670.
On each, every one of the four shapes read A's write.
This commit is contained in:
Anees Iqbal committed 2026-12-02 07:52:39 -08:00
1 parent 5d6ac696d0
commit 9f680a352e
1 file changed
+95
@@ -860,4 +860,99 @@ pub(crate) mod on_device {
assert!(lines.is_empty(), "{name}: {lines:#?}");
});
}
/// The newer textures [`moved_over`] places over an older one's bytes: each one's format,
/// width and height, every one smaller than the older, so whatever a driver keeps past its
/// texels (a compression state) lies over the older one's texels.
const NEWER: [(PixelFormat, u32, u32); 4] = [
(PixelFormat::RGBA8Unorm, 256, 256),
(PixelFormat::RGBA16Float, 256, 128),
(PixelFormat::R32Float, 256, 256),
(PixelFormat::RGBA32Float, 128, 128),
];
/// An older texture's bytes read before a newer one over them is first written, as Metal's
/// aliasing lets a program read them: for each of the [`NEWER`], A (512 by 512 `RGBA16Float`)
/// is written in one command buffer and completes; then B is placed over A's bytes, and a
/// second command buffer reads A, then writes B. A line for each read of A that is not A's
/// write, and a printed line for each pair.
fn moved_over(name: &str, device: &Device, kernels: &Kernels) -> Vec<String> {
let len = MemoryClass::Private.block_len();
let block = (device.take_block(MemoryClass::Private, len))
.unwrap_or_else(|error| panic!("{name}: {error}"));
let heap = HeapRange {
block: block.id,
offset: 0,
len,
};
let first = 4520;
let _grant = (device.grant_indices(IndexClass::Texture, first, 4))
.unwrap_or_else(|error| panic!("{name}: {error}"));
let queue = device.new_command_queue(4).expect("a queue");
let mut lines = Vec::new();
for (at, (format, width, height)) in NEWER.into_iter().enumerate() {
let t = 60 + 2 * at as u32;
let a = place(
name,
device,
heap,
(t, PixelFormat::RGBA16Float, 512, 512),
first,
);
let mut buffers = Buffers::apart(name, device, 3, BIG);
let a_in = input(&a);
let a_buffer = buffers.of(name, device, &a_in);
let a_out = buffers.of(name, device, &vec![f32::NAN; a_in.len()]);
let cb = queue.command_buffer().expect("a command buffer");
write(name, &cb, kernels, &a, &a_buffer);
cb.commit().expect("a commit");
finished(name, &cb);
assert_eq!(cb.error(), None, "{name}: A's write");
// B over A's bytes, made after A's write completed: its first command buffer reads A,
// then writes B.
let b = place(
name,
device,
heap,
(t + 1, format, width, height),
first + 2,
);
let b_in = input(&b);
let b_buffer = buffers.of(name, device, &b_in);
let cb = queue.command_buffer().expect("a command buffer");
read(name, &cb, kernels, &a, &a_out);
write(name, &cb, kernels, &b, &b_buffer);
cb.commit().expect("a commit");
finished(name, &cb);
assert_eq!(cb.error(), None, "{name}: A's read, then B's write");
let what = format!("A read before B ({format:?} {width}x{height}) is first written");
let lost = wrong(&what, &a, &a_out, &reads(&a, &a_in));
println!(
"heap alias: {name}: {what}: {}",
lost.as_deref().unwrap_or("A reads its write")
);
lines.extend(lost);
drop((a, b, a_buffer, a_out, b_buffer, buffers, cb));
}
drop(queue);
(device.release_block(block)).unwrap_or_else(|error| panic!("{name}: {error}"));
lines
}
/// A newer texture placed over an older one's bytes, its first command buffer reading the
/// older one before writing it: each read of the older one is its write. No lab driver has
/// shown the loss the newer one's move home from undefined may make there: the Radeon 780M
/// (RADV) and the Iris Xe (ANV, Mesa 26.2.6) each read the older one's write for every shape.
/// The test is coverage of Metal's aliasing contract on that order.
#[test]
fn every_device_reads_an_older_texture_before_a_newer_one_over_its_bytes_is_written() {
let Some(libraries) = libraries() else {
return;
};
each_device("heap_alias_moved_over", |name, _, device| {
let kernels = kernels(name, &device, &libraries);
let lines = moved_over(name, &device, &kernels);
assert!(lines.is_empty(), "{name}: {lines:#?}");
});
}
}