rt: the linear texture alignment answer is the device's linear row rounding alone

minimum_linear_texture_alignment no longer requires the linear image's memory alignment
(VkMemoryRequirements::alignment) to divide the answer. The answer is the smallest power of two P
at which the device lays every probed width's rows at the row rounded up to P, the largest over
the usages, and Metal's 16 where no P explains a format's pitches. The in-place test is
unchanged: a texture is in place only where its place in the buffer's memory is a multiple of the
image's memory alignment too, and is copied (over a Managed buffer's device copy, refused)
elsewhere. Nothing changes on NVIDIA, AMD or lavapipe, whose linear images' memory alignment
equals their row rounding (probe jobs 43742-43744). On the UHD 630, whose linear rows round to 64
bytes and whose linear images' memory aligns to 4,096 (probe 43758), the answer goes from 16 (no
P held) to 64, so a guest that pads its rows to the answer gets the device's own pitch.

Tests: tests::over places its textures at the answer rounded up to the linear image's memory
alignment (a test-only Device::linear_memory_alignment), and checks at widths 1, 3, 17, 64, 65 and
100 that a texture whose pitch is its row rounded up to the answer is in place; it prints how many
of its five formats answer 16. README and the getter's doc state what the number is and that
offsets are honoured in place only at the image's memory alignment.
This commit is contained in:
Anees Iqbal committed 2026-11-17 20:17:09 -08:00
1 parent 1ada6e8b1c
commit 8db961145b
3 files changed
+76 -33

No files matched your search

+10 -6
View File
@@ -297,14 +297,18 @@ made, with no lock, and never rewritten for layouts.
group's execution are one of the allowed outcomes. A guest that waits for a completion before
writing has, by that fact, a command buffer not in a held group.
- `Device::minimum_linear_texture_alignment(pixel_format)` answers
`minimumLinearTextureAlignmentForPixelFormat:` from the device's own linear images: the
`minimumLinearTextureAlignmentForPixelFormat:` with the device's linear row rounding: the
smallest power of two from 16 to 4,096 at which the device lays the rows of a 3-row linear
image of each width of 1, 3, 17, 64, 65, 100, 129, 1,000 and 1,920 texels at the row rounded up
to it, and of which the image's memory alignment is a factor; the largest over shader read,
read and render, and read and write, where the device makes such images; 16 where no
candidate holds. Each image is made, read and destroyed, never bound, once per format for the
device's life. A texture at an offset in its block and a pitch that are multiples of the
answer is in place at the widths tried; the answer promises nothing for other widths.
to it; the largest over shader read, read and render, and read and write, where the device
makes such images; 16 where no candidate holds, and a texture of that format over a buffer is
then never in place. Each image is made, read and destroyed, never bound, once per format for
the device's life. The answer leaves out the image's memory alignment
(`VkMemoryRequirements::alignment`): a texture whose pitch is a multiple of the answer is in
place, at the widths tried, only where its place in the buffer's memory is a multiple of that
alignment too, and copied elsewhere (over a Managed buffer's device copy, refused). NVIDIA and
AMD give the same number for both; the UHD 630 rounds rows to 64 bytes and aligns its linear
images' memory to 4,096. The answer promises nothing for widths not tried.
- The staging region is 16 MiB of the Shared type in an allocation of its own, made at the first
copy, its pages written then, and kept for the device's life, so no copy allocates memory and
the host writes resident pages. It is a ring: each copy takes a span at a multiple of 256 bytes,
+42 -13
View File
@@ -1,7 +1,10 @@
//! Textures over a buffer: under a device cfg, on every device of the cfg's kind, the linear
//! alignment answer is a power of two from 16 to 4,096, the same when asked again; an RGBA8Unorm
//! texture over a Shared buffer at an offset and pitch of the answer, its rows longer than their
//! texels, is made in place: it allocates nothing and stages nothing, its image's bytes are the
//! alignment answer is a power of two from 16 to 4,096, the same when asked again, and the count
//! of formats that answer 16 is printed; at each width tried, a texture over a Shared buffer whose
//! pitch is its row rounded up to the answer, at a place of the linear image's memory alignment,
//! is in place; an RGBA8Unorm texture over a Shared buffer at a pitch of the answer, its rows
//! longer than their texels, at such a place, is made in place: it allocates nothing and stages
//! nothing, its image's bytes are the
//! host's in the buffer as they change, `replace_region` and `get_bytes` write and read the
//! buffer, and a blit's write to the texture reaches the host in the buffer. Over a Managed buffer
//! the image is the bytes shaders reach, which `replace_region` changes. Rows past the buffer,
@@ -19,9 +22,17 @@ mod on_device {
const WIDTH: u32 = 100;
const HEIGHT: u32 = 10;
const ROW: u64 = WIDTH as u64 * 4;
/// The buffer's offset in its block: a multiple of every answer.
/// The buffer's offset in its block: a multiple of every answer and memory alignment.
const AT: u64 = 65_536;
const LEN: u64 = 65_536;
/// The formats whose answers are printed.
const FORMATS: [PixelFormat; 5] = [
PixelFormat::R8Unorm,
PixelFormat::RGBA8Unorm,
PixelFormat::R32Float,
PixelFormat::RGBA16Float,
PixelFormat::RGBA32Float,
];
fn whole() -> Region {
Region {
@@ -70,13 +81,7 @@ mod on_device {
fn every_device_holds_a_texture_over_a_buffer_in_place() {
each_device("over", |name, _, device| {
let mut answers = Vec::new();
for pf in [
PixelFormat::R8Unorm,
PixelFormat::RGBA8Unorm,
PixelFormat::R32Float,
PixelFormat::RGBA16Float,
PixelFormat::RGBA32Float,
] {
for pf in FORMATS {
let answer = (device.minimum_linear_texture_alignment(pf))
.unwrap_or_else(|error| panic!("{name}: {pf:?}: {error}"));
assert!(
@@ -85,11 +90,22 @@ mod on_device {
);
answers.push((pf, answer));
}
println!("over: {name}: minimum linear texture alignments {answers:?}");
let sixteen = answers.iter().filter(|&&(_, answer)| answer == 16).count();
println!(
"over: {name}: minimum linear texture alignments {answers:?}; {sixteen} of {} \
formats answer 16",
FORMATS.len()
);
let align = answers[1].1;
let again = (device.minimum_linear_texture_alignment(PixelFormat::RGBA8Unorm))
.unwrap_or_else(|error| panic!("{name}: {error}"));
assert_eq!(again, align, "{name}: asked again");
// The image's memory alignment, which the answer leaves out: a place of it.
let mut d =
TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, WIDTH, HEIGHT);
d.usage = TextureUsage::SHADER_READ;
let memory = (device.linear_memory_alignment(&d)).expect("a linear image");
let offset = align.next_multiple_of(memory);
let counters = device.counters();
let core = device.core();
@@ -99,8 +115,21 @@ mod on_device {
.unwrap_or_else(|error| panic!("{name}: {error}"));
let mut now: Vec<u8> = (0..LEN).map(|i| (i % 253) as u8).collect();
write(&buffer, &now);
// At each width tried, the row rounded up to the answer is the device's own pitch.
for width in [1, 3, 17, 64, 65, 100] {
let row = u64::from(width) * 4;
let mut w = d;
w.width = width;
let memory = (device.linear_memory_alignment(&w)).expect("a linear image");
let at = align.next_multiple_of(memory);
let texture = (buffer.new_texture(&w, None, at, row.next_multiple_of(align)))
.unwrap_or_else(|error| panic!("{name}: width {width}: {error}"));
assert!(
texture.copied_over().is_none(),
"{name}: width {width} at the answer's pitch is in place"
);
}
let pitch = (ROW + 1).next_multiple_of(align);
let offset = align;
// Read and written by shaders, at indices of a grant.
(device.grant_indices(IndexClass::StorageTexture, 1, 4)).expect("a grant");
let mut d =
+24 -14
View File
@@ -440,15 +440,19 @@ impl Device {
heap_fit(core, descriptor, &requirements, &dedicated)
}
/// `minimumLinearTextureAlignmentForPixelFormat:`: what a texture over a buffer's offset and
/// rows are to be multiples of for the device's own linear image to hold it in place
/// ([`Buffer::new_texture`]). It is the smallest power of two from 16 to 4,096 at which the
/// device lays the rows of a linear image of the format, of every width tried, at the row
/// rounded up to it, and of which the image's memory alignment is a factor; the largest over
/// reading, reading and rendering, and reading and a shader's writes, where the device makes
/// such an image; Metal's 16 where no candidate holds. The device answers: each image is
/// made, read with `vkGetImageSubresourceLayout` and destroyed, never bound, once per format
/// for the device's life. The answer differs by device, as Metal's does by GPU.
/// `minimumLinearTextureAlignmentForPixelFormat:`: the device's linear row rounding, the
/// smallest power of two from 16 to 4,096 at which the device lays the rows of a linear image
/// of the format, of every width tried, at the row rounded up to it; the largest over reading,
/// reading and rendering, and reading and a shader's writes, where the device makes such an
/// image; Metal's 16 where no candidate holds, so that a texture of the format over a buffer
/// is never in place. The device answers: each image is made, read with
/// `vkGetImageSubresourceLayout` and destroyed, never bound, once per format for the device's
/// life. The answer differs by device, as Metal's does by GPU.
///
/// A texture over a buffer ([`Buffer::new_texture`]) whose pitch is a multiple of the answer
/// is in place where its place in the buffer's memory is also a multiple of the linear
/// image's memory alignment (`VkMemoryRequirements::alignment`), which the answer does not
/// include; at any other place it is copied, or over a Managed buffer's device copy refused.
///
/// Err: [`Gap::TextureFormat`] for a format no texture is made of.
pub fn minimum_linear_texture_alignment(
@@ -480,18 +484,16 @@ impl Device {
let Some(unit) = plan.unit.filter(|unit| unit.block == [1, 1]) else {
break;
};
let Some((pitch, align)) = linear_layout(core, &d, &shape, &plan) else {
let Some((pitch, _)) = linear_layout(core, &d, &shape, &plan) else {
break;
};
rows.push((u64::from(width * unit.bytes), pitch, align));
rows.push((u64::from(width * unit.bytes), pitch));
}
if rows.len() < WIDTHS.len() {
continue;
}
let holds = |candidate: u64| {
(rows.iter()).all(|&(row, pitch, align)| {
pitch == row.next_multiple_of(candidate) && candidate.is_multiple_of(align)
})
(rows.iter()).all(|&(row, pitch)| pitch == row.next_multiple_of(candidate))
};
let found = (4..=12).map(|power| 1u64 << power).find(|&p| holds(p));
answer = answer.max(found);
@@ -501,6 +503,14 @@ impl Device {
Ok(answer)
}
/// For the device tests: the memory alignment of the device's linear image of `d`, `None`
/// where it makes none.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn linear_memory_alignment(&self, d: &TextureDescriptor) -> Option<u64> {
let (shape, plan, _) = self.prepare(d).ok()?;
linear_layout(self.core(), d, &shape, &plan).map(|(_, align)| align)
}
/// `MTLHeap`'s `newTextureWithDescriptor:`, at `index`: a texture as [`Device::new_texture`]
/// makes it, its image bound at `offset` in `heap`, where the consumer's own placement put it
/// by [`Device::heap_texture_size_and_align`]. Nothing is allocated, and the bytes are those