From fdf6165ed35bb7d71d513403ca0eb859acdd820d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 16 Nov 2026 18:19:37 -0800 Subject: [PATCH] RUNTIME 2504: A staged texture copy with the guest RAM joins no rows past a 2^31 - 1 byte pitch What: copy_texture_from_host_ram / copy_texture_to_host_ram staged through the CPU (first touch, a chunk that takes no import) recorded the GPU's copy between the arena and the texture as one region at the surface's pitch, bufferRowLength = pitch / texel bytes, for any region of more than one row. A pitch past 2^31 - 1 bytes (and up to texel x (2^32 - 1)) so recorded a vkCmdCopyBufferToImage / vkCmdCopyImageToBuffer region breaking VUID-vkCmdCopyBufferToImage-bufferRowLength-09108 and VUID-vkCmdCopyImageToBuffer-bufferRowLength-09108 (bufferRowLength times the texel block size at most 2^31 - 1); a wider pitch was refused (Problem::HostRam) where the texels overflowed a u32. The copy straight on the imports already joined no rows past that pitch. How: one rule, `joining_pitch` (more than one row, a pitch of at most 2^31 - 1 bytes), now used by both paths; the staged path builds its regions with `staged_regions`: the one region of all the rows where they join, else one region per row at the row's byte of the arena's region (bufferRowLength 0, one row high). The same bytes either way; the arena's region is unchanged (the whole span, which a device that cannot make a buffer that long refuses at the host step, failing the command buffer, as before). Verified: m3-max-38558 (msi): runtime clippy (all targets, all features) and rustdoc clean; tests::host_ram_texture_rows 3/3 with the new a_staged_copy_s_rows_past_a_pitch_of_2_31_minus_1_bytes_are_a_region_each, which fails with the pre-fix rule restored in the same job (1 region, not 3). m3-max-38559 (lavapipe): runtime clippy under --cfg m2v_cpu_icd clean; tests::host_ram* 94/94. --- .../src/host_ram/texture.rs | 70 ++++++++++++++----- .../src/tests/host_ram_texture_rows.rs | 67 +++++++++++++++++- crates/metal2vulkan/src/cache/mod.rs | 2 +- 3 files changed, 118 insertions(+), 21 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/host_ram/texture.rs b/crates/metal2vulkan-runtime/src/host_ram/texture.rs index 25f5f9542..793e30088 100644 --- a/crates/metal2vulkan-runtime/src/host_ram/texture.rs +++ b/crates/metal2vulkan-runtime/src/host_ram/texture.rs @@ -52,7 +52,9 @@ //! chunk that takes no import while it runs are staged through the CPU as the buffer forms //! stage them (`StagedGather`, `StagedScatter`), the GPU's copy between the arena's region and //! the texture one `vkCmdCopyBufferToImage` or `vkCmdCopyImageToBuffer` of the region's rows at -//! the surface's pitch; an encoding again stages them again as the buffer forms do. +//! the surface's pitch (`staged_regions`: one region of them all, or one a row, `bufferRowLength` +//! 0, where the pitch is past 2^31 - 1 bytes and joins no rows); an encoding again stages them +//! again as the buffer forms do. //! - **The texture's contract**: an image of its own (not a texture over a buffer, a texture //! buffer or a texture whose blits take a shadow's copies), one sample, a colour format of //! uncompressed texels the image holds one to a texel (no block-compressed, depth, stencil, @@ -216,6 +218,49 @@ pub(crate) fn joined_rows(pieces: &[Rows], pitch: Option) -> Vec { out } +/// The pitch at which a region's `rows` rows, `bytes_per_row` bytes apart, are one region of a +/// copy, its `bufferRowLength` the pitch's texels: a region of more than one row, its pitch of at +/// most 2^31 - 1 bytes (`VUID-vkCmdCopyBufferToImage-bufferRowLength-09108`, +/// `VUID-vkCmdCopyImageToBuffer-bufferRowLength-09108`), a whole number of texels at least a +/// row's (`check_layout`: `VUID-VkBufferImageCopy-bufferRowLength-09101`). `None`: each row is a +/// region of its own, `bufferRowLength` 0. +fn joining_pitch(rows: u64, bytes_per_row: u64) -> Option { + (rows > 1 && bytes_per_row <= i32::MAX as u64).then_some(bytes_per_row) +} + +/// A staged copy's regions between the arena's region and `image` (a region of one image of +/// `texel`-byte texels, one to a texel), its rows `bytes_per_row` apart from the arena region's +/// first byte: one region of them all where they join (`joining_pitch`), else one a row at its +/// row's byte, `bufferRowLength` 0. The same bytes either way. +pub(crate) fn staged_regions( + image: ImageRegion, + bytes_per_row: u64, + texel: u64, +) -> Vec { + let [x, y, z] = image.origin; + let [width, height, depth] = image.size; + match joining_pitch(u64::from(height), bytes_per_row) { + Some(pitch) => vec![BufferImageRegion { + buffer_offset: 0, + row_length: (pitch / texel) as u32, + image_height: 0, + image, + }], + None => (0..height) + .map(|row| BufferImageRegion { + buffer_offset: u64::from(row) * bytes_per_row, + row_length: 0, + image_height: 0, + image: ImageRegion { + origin: [x, y + row, z], + size: [width, 1, depth], + ..image + }, + }) + .collect(), + } +} + impl BlitCommandEncoder { /// Copy one 2D region of `destination` from a surface laid out in the guest RAM `ram`, by the /// GPU, from the device's imports of the RAM's chunks: a runtime extension, Metal has none (a @@ -446,20 +491,12 @@ impl BlitCommandEncoder { // The first encoding's staging, which an encoding again finds set. let _ = call.staged.set(staging); // The region's rows at the surface's pitch in the arena's region, from its first. - let texels_per_row = match rows.rows { - 1 => Some(0), - _ => u32::try_from(bytes_per_row / texel).ok(), - }; - let row_length = texels_per_row.ok_or_else(refused)?; - let staged = BufferImageRegion { - buffer_offset: 0, - row_length, - image_height: 0, - image, - }; + let regions = (staged_regions(image, bytes_per_row, texel).into_iter()) + .map(|staged| buffer_image_region(&image_buffer_region(plan, staged))) + .collect(); let target = Target::Image { image: raw_image, - regions: vec![buffer_image_region(&image_buffer_region(plan, staged))], + regions, }; let from_first: Vec = (pieces.iter()) .map(|piece| Piece { @@ -503,11 +540,8 @@ impl BlitCommandEncoder { FreshUnwritten::reads(&device, texture, &read)? } }; - // Rows of one region at the surface's pitch, `bufferRowLength` its texels: a pitch of whole - // texels at least a row's (`check_layout`: `VUID-VkBufferImageCopy-bufferRowLength-09101`) - // and of at most 2^31 - 1 bytes (`VUID-vkCmdCopyBufferToImage-bufferRowLength-09108`, - // `VUID-vkCmdCopyImageToBuffer-bufferRowLength-09108`). - let pitch = (rows.rows > 1 && bytes_per_row <= i32::MAX as u64).then_some(bytes_per_row); + // Rows of one region at the surface's pitch, `bufferRowLength` its texels. + let pitch = joining_pitch(rows.rows, bytes_per_row); let mut regions_recorded = 0u64; for round in &by_round { // The round's pieces, each a row's texels from its byte of its import, in order. diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_texture_rows.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_texture_rows.rs index 126cdccc0..77d08faf7 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_texture_rows.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_texture_rows.rs @@ -1,9 +1,14 @@ //! The regions of a copy between a guest RAM and a texture on the imports //! (`host_ram/texture.rs`'s `joined_rows`): a row's piece that continues a region of the row //! before (the same import, first texel and texels, its bytes the pitch on) is one more row of -//! it; any other piece starts a region, and with no pitch every piece is one. +//! it; any other piece starts a region, and with no pitch every piece is one. And a staged copy's +//! regions between the arena and the texture (`staged_regions`): one region of its rows at a +//! pitch of at most 2^31 - 1 bytes, else one a row. -use crate::host_ram::texture::{joined_rows, Rows}; +use crate::blit_plan::BufferImageRegion; +use crate::copy_plan::ImageRegion; +use crate::host_ram::texture::{joined_rows, staged_regions, Rows}; +use metal2vulkan::vk::ImageAspectFlags; const PITCH: u64 = 1280; @@ -81,3 +86,61 @@ fn a_piece_off_the_region_s_import_texels_pitch_or_next_row_starts_its_own() { [Rows { rows: 2, ..left }, Rows { rows: 2, ..right }] ); } + +#[test] +fn a_staged_copy_s_rows_past_a_pitch_of_2_31_minus_1_bytes_are_a_region_each() { + // `VUID-vkCmdCopyBufferToImage-bufferRowLength-09108`, + // `VUID-vkCmdCopyImageToBuffer-bufferRowLength-09108`: `bufferRowLength` times the texel's + // bytes at most 2^31 - 1. + const LIMIT: u64 = (1 << 31) - 1; + const TEXEL: u64 = 4; + let image = ImageRegion { + aspect: ImageAspectFlags::COLOR, + level: 1, + layer: 2, + layers: 1, + origin: [4, 8, 0], + size: [16, 3, 1], + }; + // The widest pitch of whole texels within the limit: one region of the rows. + let within = LIMIT / TEXEL * TEXEL; + let joined = BufferImageRegion { + buffer_offset: 0, + row_length: (within / TEXEL) as u32, + image_height: 0, + image, + }; + assert_eq!(staged_regions(image, within, TEXEL), [joined]); + // A pitch of 2^31 bytes: a region a row, each at its row's byte of the arena's region. + let past = 1 << 31; + let regions = staged_regions(image, past, TEXEL); + assert_eq!(regions.len(), 3); + for (y, region) in (0u32..).zip(®ions) { + assert!(u64::from(region.row_length) * TEXEL <= LIMIT, "{region:?}"); + let row = BufferImageRegion { + buffer_offset: u64::from(y) * past, + row_length: 0, + image_height: 0, + image: ImageRegion { + origin: [4, 8 + y, 0], + size: [16, 1, 1], + ..image + }, + }; + assert_eq!(*region, row); + } + // One row: its region, whatever the pitch. + let one = ImageRegion { + size: [16, 1, 1], + ..image + }; + for pitch in [64, past, 1 << 40] { + let region = BufferImageRegion { + buffer_offset: 0, + row_length: 0, + image_height: 0, + image: one, + }; + assert_eq!(staged_regions(one, pitch, TEXEL), [region], "{pitch}"); + } +} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 41db9b683..9813fec76 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 525; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2503; +pub const RUNTIME_VERSION: u32 = 2504; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles