rt test: a heap makes texture buffers in place at offsets 0, 16 and 4096 (red)

A guest's viewer asks heapTextureSizeAndAlignWithDescriptor: of a texture-buffer descriptor and
makes the texture in a heap. rt refuses both with Refusal::TextureBufferNotOverBuffer.

The test asks for what Metal does on an Apple M3 Max (m3-max-80369 and m3-max-80398):
- a 4-texel R32Uint texture buffer is sized 16 bytes at an alignment of 16;
- a Shared and a Private heap make it at offsets 0, 16 and 4096;
- it reads the bytes a buffer placed at the same offset wrote, because placement keeps a reused
  region's bytes;
- tb_atomic adds 3 to each texel, and the sums land in place, linear, in the heap's bytes, with
  the sentinels past them unchanged.

Red here: heap_texture_size_and_align refuses the descriptor.
This commit is contained in:
Anees Iqbal committed 2026-12-02 16:24:50 -08:00
1 parent 2e5503c0f7
commit 4786da9503
1 file changed
+129 -1
@@ -20,6 +20,9 @@
//! - an `RGBA8Unorm` texture buffer over a buffer placed in a Private heap, filled by a blit from a
//! Shared buffer, reads back the filled texels through a blit out of it, through its buffer's
//! bytes and through a kernel, and the bytes around them keep theirs;
//! - `R32Uint` texture buffers a Shared and a Private heap make at offsets 0, 16 and 4096
//! (`Device::new_heap_texture`), sized and aligned as Metal answers, read the bytes a buffer
//! placed there wrote and leave the sums `tb_atomic` adds in place, linear, in the heap's bytes;
//! - blit copies between two texture buffers and between a texture buffer and a 2D texture, both
//! ways, move the region's texels and no other byte;
//! - what Metal refuses (an offset off the device's alignment, a `bytesPerRow` below the row,
@@ -38,7 +41,7 @@ mod on_device {
use crate::{
Buffer, BufferLayout, ComputePipelineDescriptor, ComputePipelineState, Device,
DeviceOptions, DispatchType, Error, FunctionDescriptor, Gap, HeapRange, IndexClass,
Instance, Library, MemoryClass, PixelFormat, Refusal, Region, StageSource,
Instance, Library, MemoryClass, PixelFormat, Refusal, Region, SizeAndAlign, StageSource,
StorageElementCase, StorageMode, Texture, TextureBacking, TextureDescriptor, TexturePlace,
TextureSwizzle, TextureSwizzleChannels, TextureType, TextureUsage, TextureViewKind,
};
@@ -350,6 +353,131 @@ declare <4 x i32> @air.atomic_load_explicit_texture_buffer_1d.u.v4i32(ptr addrsp
each_device_with("texture buffer atomics", edit, case);
}
/// Words of a buffer placed in a heap of either class: a Shared one's as the host reads them,
/// a Private one's through a blit into `staging`, a Shared buffer at least as long.
fn heap_words(device: &Device, held: &Buffer, staging: &Buffer) -> Vec<u32> {
if held.contents().is_some() {
return words(held);
}
let queue = device.new_command_queue(1).expect("a queue");
let command_buffer = queue.command_buffer().expect("a command buffer");
let mut blit = (command_buffer.blit_command_encoder()).expect("a blit encoder");
(blit.copy_from_buffer_to_buffer(held, 0, staging, 0, held.length())).expect("the copy");
blit.end_encoding();
command_buffer.commit().expect("a commit");
finished("heap words", &command_buffer);
assert_eq!(command_buffer.error(), None);
words(staging)[..words_in(held)].to_vec()
}
/// Writes `values` to a buffer placed in a heap of either class: a Shared one's through the
/// host, a Private one's through a blit from `staging`.
fn set_heap_words(device: &Device, held: &Buffer, staging: &Buffer, values: &[u32]) {
if held.contents().is_some() {
return set_words(held, 0, values);
}
set_words(staging, 0, values);
let queue = device.new_command_queue(1).expect("a queue");
let command_buffer = queue.command_buffer().expect("a command buffer");
let mut blit = (command_buffer.blit_command_encoder()).expect("a blit encoder");
(blit.copy_from_buffer_to_buffer(staging, 0, held, 0, held.length())).expect("the copy");
blit.end_encoding();
command_buffer.commit().expect("a commit");
finished("heap words", &command_buffer);
assert_eq!(command_buffer.error(), None);
}
/// A buffer's words.
fn words_in(buffer: &Buffer) -> usize {
usize::try_from(buffer.length() / 4).expect("a length")
}
/// Texture buffers placed in a heap, as Metal's heap makes them (`newTextureWithDescriptor:`
/// of a texture buffer's descriptor), in a Shared and a Private heap at offsets 0, 16 and
/// 4096. The heap answers a 4-texel `R32Uint` texture buffer 16 bytes at an alignment of 16,
/// as an Apple M3 Max does. A buffer placed at the same offset first writes the texels and
/// sentinels past them; the texture buffer placed over those bytes reads them, as Metal's
/// placement keeps a reused region's bytes, and `tb_atomic` adds 3 to each texel. The buffer
/// then holds the sums at the texture buffer's offset and the sentinels past them: the
/// texture buffer lies in place, linear, in the heap's bytes.
#[test]
fn every_device_makes_texture_buffers_in_place_in_a_heap() {
let name = "tb_atomic";
let Some(library) = library(name, llvm_assemble_text(ATOMIC_KERNEL)) else {
return;
};
each_device("heap texture buffers", |name, _, device| {
// One index for each placement, as a texture's index is free again only once it is gone.
let _grant = (device.grant_indices(IndexClass::Texture, 1, 6))
.unwrap_or_else(|error| panic!("{name}: {error}"));
let mut index = 0;
let io = (device.take_block(MemoryClass::Shared, BLOCK)).expect("a block");
let out = (device.new_buffer(io.id, 0, 64, Some(0))).expect("a buffer");
let staging = (device.new_buffer(io.id, 4096, 64, Some(0))).expect("a buffer");
let kernel = state(&device, &library, "tb_atomic");
let usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE;
let alignment = (device.minimum_texture_buffer_alignment(PixelFormat::R32Uint))
.unwrap_or_else(|error| panic!("{name}: {error}"));
for class in [MemoryClass::Shared, MemoryClass::Private] {
let block = (device.take_block(class, BLOCK)).expect("a block");
let heap = HeapRange {
block: block.id,
offset: 0,
len: BLOCK,
};
let mut d = descriptor(PixelFormat::R32Uint, 4, usage);
d.storage_mode = match class {
MemoryClass::Shared => StorageMode::Shared,
MemoryClass::Private => StorageMode::Private,
};
let answer = (device.heap_texture_size_and_align(&d))
.unwrap_or_else(|error| panic!("{name}: {class:?}: the size: {error}"));
assert_eq!(
answer,
SizeAndAlign {
size: 16,
align: 16
},
"{name}: {class:?}: the device's texel-buffer alignment is {alignment}"
);
for at in [0, 16, 4096] {
let held = (device.new_heap_buffer(heap, at, 32))
.unwrap_or_else(|error| panic!("{name}: the heap buffer: {error}"));
let laid = [10, 20, 30, 40, SENTINEL, SENTINEL, SENTINEL, SENTINEL];
set_heap_words(&device, &held, &staging, &laid);
index += 1;
let texels = (device.new_heap_texture(&d, Some(index), heap, at))
.unwrap_or_else(|error| panic!("{name}: {class:?} at {at}: {error}"));
assert_eq!(texels.descriptor().storage_mode, d.storage_mode);
set_words(&out, 0, &[0; 16]);
(dispatch(&device, &kernel, &[&texels], &[&out], 4))
.unwrap_or_else(|error| panic!("{name}: the dispatch: {error:?}"));
let answers = words(&out);
assert_eq!(
answers[..4],
[10, 20, 30, 40],
"{name}: {class:?} at {at}: the texels the heap's bytes held"
);
assert_eq!(
answers[8..12],
[13, 23, 33, 43],
"{name}: {class:?} at {at}: the texels loaded"
);
assert_eq!(
heap_words(&device, &held, &staging),
[13, 23, 33, 43, SENTINEL, SENTINEL, SENTINEL, SENTINEL],
"{name}: {class:?} at {at}: the heap's bytes"
);
drop((texels, held));
}
println!(
"{name}: {class:?} heap texture buffers in place at 0, 16 and 4096 (the device's \
texel-buffer alignment {alignment})"
);
}
});
}
#[test]
fn every_device_updates_a_texture_s_texels_by_an_atomic_declared_with_its_format() {
let kernel = (ATOMIC_KERNEL.replace("texture_buffer_1d", "texture_1d"))