diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index 3f991634a..4844474fb 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -335,18 +335,21 @@ made, with no lock, and never rewritten for layouts. SDK says Managed storage has no effect on Apple silicon; the documented contract, not that answer, is the rule here. - `Device::minimum_linear_texture_alignment(pixel_format)` answers - `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; 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 + `minimumLinearTextureAlignmentForPixelFormat:` with the device's linear row rounding: the smallest + power of two from the format's texel bytes 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; 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. 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. + (`VkMemoryRequirements::alignment`): a texture whose pitch is its row rounded up to 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. NVIDIA gives the same number for both, as RADV (Mesa 26.2.6) + on the RX 550 does for texels of up to 4 bytes (256), where it answers 512 and 1,024 for 8- and + 16-byte texels over a 256-byte alignment; the UHD 630 rounds rows to 64 bytes and aligns its + linear images' memory to 4,096; ANV (Mesa 26.2.6) on the Iris Xe of RPL-P lays each row at the row + itself with its memory aligned to the texel, so it answers the texel's bytes (4 for `RGBA8Unorm`, + 1 for `R8Unorm`), below Metal's 16. 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, diff --git a/crates/metal2vulkan-rt/src/tests/over.rs b/crates/metal2vulkan-rt/src/tests/over.rs index b6ae009f1..dad2aecd8 100644 --- a/crates/metal2vulkan-rt/src/tests/over.rs +++ b/crates/metal2vulkan-rt/src/tests/over.rs @@ -1,9 +1,9 @@ //! 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, and the count -//! of formats that answer 16 is printed; at each width tried, a texture over a Shared buffer whose +//! alignment answer is a power of two from the format's texel bytes 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 (live +//! is in place; an RGBA8Unorm texture over a Shared buffer at its row rounded up to the answer +//! (longer than its texels where the device pads rows), at such a place, is made in place: it allocates nothing (live //! allocations counted once the block-maker thread holds a ready block of each class, the count //! that a plain texture's allocation moves) 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 @@ -29,13 +29,13 @@ mod on_device { /// 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, + /// The formats whose answers are printed, each with its texel bytes. + const FORMATS: [(PixelFormat, u64); 5] = [ + (PixelFormat::R8Unorm, 1), + (PixelFormat::RGBA8Unorm, 4), + (PixelFormat::R32Float, 4), + (PixelFormat::RGBA16Float, 8), + (PixelFormat::RGBA32Float, 16), ]; fn whole() -> Region { @@ -100,11 +100,11 @@ 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 FORMATS { + for (pf, texel) in FORMATS { let answer = (device.minimum_linear_texture_alignment(pf)) .unwrap_or_else(|error| panic!("{name}: {pf:?}: {error}")); assert!( - answer.is_power_of_two() && (16..=4096).contains(&answer), + answer.is_power_of_two() && (texel..=4096).contains(&answer), "{name}: {pf:?}: {answer}" ); answers.push((pf, answer)); @@ -151,7 +151,7 @@ mod on_device { "{name}: width {width} at the answer's pitch is in place" ); } - let pitch = (ROW + 1).next_multiple_of(align); + let pitch = ROW.next_multiple_of(align); // Read and written by shaders, at indices of a grant. (device.grant_indices(IndexClass::StorageTexture, 1, 4)).expect("a grant"); let mut d = diff --git a/crates/metal2vulkan-rt/src/tests/texture.rs b/crates/metal2vulkan-rt/src/tests/texture.rs index 3ca89884a..61492cc67 100644 --- a/crates/metal2vulkan-rt/src/tests/texture.rs +++ b/crates/metal2vulkan-rt/src/tests/texture.rs @@ -10,6 +10,8 @@ //! again. //! - A view's components are Metal's swizzle over the format's own mapping: zero and one stay, //! and a channel source reads through the format's mapping. +//! - The linear row rounding starts at the texel's bytes: rows laid at the row itself answer the +//! texel's bytes, rows padded to a power of two answer it, and rows padded otherwise answer none. //! - Under a device cfg, on every device of the cfg's kind: bytes written with `replace_region` //! read back with `get_bytes`, the same, for a level of a 2D texture, rows apart at a pitch with //! padding, a 3D region two images deep, a slice of an array, a cube's face, a depth texture, @@ -21,7 +23,7 @@ use crate::arrays::Grants; use crate::error::{Error, Refusal}; -use crate::texture::{components, shape}; +use crate::texture::{components, linear_rounding, shape}; use crate::{IndexClass, PixelFormat, TextureDescriptor, TextureType}; use ash::vk; use metal2vulkan::metal::{TextureSwizzle, TextureSwizzleChannels}; @@ -494,3 +496,30 @@ mod on_device { } } } + +/// The linear row rounding of a format of `texel` bytes over the minimum linear texture +/// alignment's widths, where the device lays a row of `row` bytes at `pitch(row)`. +fn rounding(texel: u64, pitch: impl Fn(u64) -> u64) -> Option { + let rows: Vec<(u64, u64)> = [1, 3, 17, 64, 65, 100, 129, 1000, 1920] + .map(|width| { + let row = width * texel; + (row, pitch(row)) + }) + .to_vec(); + linear_rounding(&rows, texel) +} + +/// A device that lays a linear row at the row itself (ANV, Mesa 26.2.6, on the Iris Xe of RPL-P: +/// every format and width the over test asks) answers each format's texel bytes, below 16 for all +/// but the 16-byte texels; one that pads rows to a power of two (RADV on Polaris: 256 bytes, 512 +/// for 8-byte texels) answers that power; one whose rows have a floor besides their rounding +/// answers none, which the device then reports as 16. +#[test] +fn the_linear_row_rounding_starts_at_the_texel() { + for texel in [1, 2, 4, 8, 16] { + assert_eq!(rounding(texel, |row| row), Some(texel), "{texel}"); + } + assert_eq!(rounding(4, |row| row.next_multiple_of(256)), Some(256)); + assert_eq!(rounding(8, |row| row.next_multiple_of(512)), Some(512)); + assert_eq!(rounding(4, |row| row.max(64)), None); +} diff --git a/crates/metal2vulkan-rt/src/texture.rs b/crates/metal2vulkan-rt/src/texture.rs index 679f3116a..dc87b080e 100644 --- a/crates/metal2vulkan-rt/src/texture.rs +++ b/crates/metal2vulkan-rt/src/texture.rs @@ -448,16 +448,17 @@ impl Device { } /// `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. + /// smallest power of two from the format's texel bytes 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, and is below Metal's 16 + /// on a device that lays a linear row at the row itself. /// - /// 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 + /// A texture over a buffer ([`Buffer::new_texture`]) whose pitch is its row rounded up to 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. /// @@ -480,6 +481,7 @@ impl Device { let mut answer = None; for usage in usages { let mut rows = Vec::with_capacity(WIDTHS.len()); + let mut texel = 1; for width in WIDTHS { let mut d = TextureDescriptor::new(TextureType::Type2D, pixel_format, width, 3); d.usage = usage; @@ -494,16 +496,13 @@ impl Device { let Some((pitch, _)) = linear_layout(core, &d, &shape, &plan) else { break; }; + texel = u64::from(unit.bytes); rows.push((u64::from(width * unit.bytes), pitch)); } if rows.len() < WIDTHS.len() { continue; } - let holds = |candidate: u64| { - (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); + answer = answer.max(linear_rounding(&rows, texel)); } let answer = answer.unwrap_or(16); lock(&core.linear_alignments).insert(pixel_format, answer); @@ -959,6 +958,18 @@ fn linear_extent(core: &Core, shape: &Shape, plan: &Plan) -> Option Option { + let first = texel.next_power_of_two().trailing_zeros(); + (first..=12).map(|power| 1u64 << power).find(|&candidate| { + (rows.iter()).all(|&(row, pitch)| pitch == row.next_multiple_of(candidate)) + }) +} + fn linear_layout( core: &Core, d: &TextureDescriptor,