From 3468ca51033d8aaa2881e2e887cd817abc352c07 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 25 Oct 2026 02:09:23 +0300 Subject: [PATCH] Let a two-plane texture's CPU copies run where every other texture's do The steps this packet landed as, in order: - Record what a block-compressed texture's CPU copies cost, and where Content: the tree of `development` at c117f5a080, unchanged. --- .../src/tests/cpu_copy_layouts.rs | 190 ++++++++++++++++-- crates/metal2vulkan-runtime/src/texture.rs | 34 +++- crates/metal2vulkan-runtime/src/two_planes.rs | 10 +- 3 files changed, 216 insertions(+), 18 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/cpu_copy_layouts.rs b/crates/metal2vulkan-runtime/src/tests/cpu_copy_layouts.rs index 0d20a2571..6ea665af3 100644 --- a/crates/metal2vulkan-runtime/src/tests/cpu_copy_layouts.rs +++ b/crates/metal2vulkan-runtime/src/tests/cpu_copy_layouts.rs @@ -6,6 +6,12 @@ //! reads and writes a packed layout in place; on its staging queue, `texture/staging_copy.rs`; or //! through a submission), which the tests print. //! +//! And a two-plane extended-range texture's copies, whose texels hold a colour word the call moves +//! beside an alpha word it keeps (`two_planes.rs`), on the path every other texture's copies take: +//! where the device copies texels on the host they read the texels, lay the words over them and +//! write them back there, waiting for no submission, as Metal's calls of such a texture wait for +//! no GPU work. +//! //! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. @@ -67,6 +73,17 @@ fn texture(device: &Device, [width, height, depth]: [u64; 3]) -> Texture { texture } +/// A `Shared` two-plane extended-range texture of `width` x `height`, read and written by a +/// shader as the cases that sample one make it (`two_planes.rs`): its colour words are what a CPU +/// copy of it moves, 4 bytes a texel, and its alpha words lie in the image beside them. +fn two_plane(device: &Device, [width, height]: [u64; 2]) -> Texture { + let format = PixelFormat::RGB10A82PXR10; + let mut descriptor = TextureDescriptor::texture_2d(format, width, height, false); + descriptor.usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE; + descriptor.resource_options = ResourceOptions::shared(); + device.new_texture(&descriptor).unwrap() +} + /// Texels of a texture of `size`, packed: byte `i` is `i * 7 + 1 + seed` modulo 251, so no two /// bytes of a row or of neighbouring rows are alike, and two seeds' bytes differ everywhere. fn pattern([width, height, depth]: [u64; 3], seed: u64) -> Vec { @@ -95,14 +112,16 @@ fn rows( }) } -/// The reads and writes the device made on the host and on its staging queue while `run` ran. -fn paths(device: &Device, run: impl FnOnce()) -> [u64; 2] { +/// The reads and writes the device made on the host and on its staging queue, and the internal +/// submissions it waited for, while `run` ran. +fn paths(device: &Device, run: impl FnOnce()) -> [u64; 3] { let before = device.work_counts(); run(); let after = device.work_counts(); [ after.host_texel_copies - before.host_texel_copies, after.staging_submissions - before.staging_submissions, + after.internal_submissions - before.internal_submissions, ] } @@ -111,16 +130,16 @@ fn paths(device: &Device, run: impl FnOnce()) -> [u64; 2] { fn get_bytes_reads_each_layout_its_rows_alone() { let scratch = Scratch::new("get-bytes-layouts"); let device = device(&scratch.0); - let mut counted = [0; 2]; + let mut counted = [0; 3]; for (size, region, bytes_per_row, bytes_per_image, len) in CASES { let texture = texture(&device, size); let mut bytes = vec![UNTOUCHED; len]; - let [host, staged] = paths(&device, || { + let [host, staged, waited] = paths(&device, || { texture .get_bytes(&mut bytes, bytes_per_row, bytes_per_image, region, 0, 0) .unwrap() }); - counted = [counted[0] + host, counted[1] + staged]; + counted = [counted[0] + host, counted[1] + staged, counted[2] + waited]; let texels = pattern(size, 0); let mut wanted = vec![UNTOUCHED; len]; let row = 4 * region.size.width as usize; @@ -132,9 +151,10 @@ fn get_bytes_reads_each_layout_its_rows_alone() { "{size:?} {region:?}, {bytes_per_row} and {bytes_per_image} apart" ); } - let [host, staged] = counted; + let [host, staged, waited] = counted; eprintln!( - "{}: {host} of {} reads on the host, {staged} on the staging queue", + "{}: {host} of {} reads on the host, {staged} on the staging queue, \ + {waited} internal submissions waited for", device.name(), CASES.len() ); @@ -147,7 +167,7 @@ fn get_bytes_reads_each_layout_its_rows_alone() { fn replace_region_writes_each_layout_its_rows_alone() { let scratch = Scratch::new("replace-region-layouts"); let device = device(&scratch.0); - let mut counted = [0; 2]; + let mut counted = [0; 3]; for (size, region, bytes_per_row, bytes_per_image, len) in CASES { let texture = texture(&device, size); let written = pattern(size, 100); @@ -159,12 +179,12 @@ fn replace_region_writes_each_layout_its_rows_alone() { bytes[at..at + row].copy_from_slice(&written[texel..texel + row]); wanted[texel..texel + row].copy_from_slice(&written[texel..texel + row]); } - let [host, staged] = paths(&device, || { + let [host, staged, waited] = paths(&device, || { texture .replace_region(region, 0, 0, &bytes, bytes_per_row, bytes_per_image) .unwrap() }); - counted = [counted[0] + host, counted[1] + staged]; + counted = [counted[0] + host, counted[1] + staged, counted[2] + waited]; let mut back = vec![0; wanted.len()]; let whole = Region::new_3d(0, 0, 0, size[0], size[1], size[2]); let row = 4 * size[0]; @@ -174,10 +194,156 @@ fn replace_region_writes_each_layout_its_rows_alone() { "{size:?} {region:?}, {bytes_per_row} and {bytes_per_image} apart" ); } - let [host, staged] = counted; + let [host, staged, waited] = counted; eprintln!( - "{}: {host} of {} writes on the host, {staged} on the staging queue", + "{}: {host} of {} writes on the host, {staged} on the staging queue, \ + {waited} internal submissions waited for", device.name(), CASES.len() ); } + +/// A two-plane extended-range texture's CPU copies take the path every other texture's take: +/// where the device copies a texture's texels on the host, `replaceRegion` reads the region's +/// texels there, lays its colour words over their first words and writes them back, and +/// `getBytes` reads them there -- no submission, so no wait, as Metal's calls of such a texture +/// wait for no GPU work. Both read each other's bytes: the words a `getBytes` sees are the words +/// the `replaceRegion` wrote, and a region's write leaves every word outside it as it was. +#[test] +fn two_plane_cpu_copies_wait_for_no_submission_where_a_textures_do() { + let scratch = Scratch::new("two-plane-cpu-copies"); + let device = device(&scratch.0); + // Whether this device copies a texture's texels on the host at all, which its two-plane + // textures copy on too (both are `Shared` color images of its own memory). + let plain = texture(&device, [7, 5, 1]); + let [plain_host, _, plain_waited] = paths(&device, || { + let whole = Region::new_3d(0, 0, 0, 7, 5, 1); + (plain.replace_region(whole, 0, 0, &pattern([7, 5, 1], 3), 28, 140)).unwrap() + }); + let size = [16, 16, 1]; + let texture = two_plane(&device, [size[0], size[1]]); + let whole = Region::new_3d(0, 0, 0, size[0], size[1], size[2]); + let (row, image) = (4 * size[0], 4 * size[0] * size[1]); + // The whole level's colour words. + let written = pattern(size, 7); + let [host, staged, waited] = paths(&device, || { + (texture.replace_region(whole, 0, 0, &written, row, image)).unwrap() + }); + let mut back = vec![UNTOUCHED; written.len()]; + let [read_host, read_staged, read_waited] = paths(&device, || { + (texture.get_bytes(&mut back, row, image, whole, 0, 0)).unwrap() + }); + assert_eq!(back, written, "the whole level's colour words"); + // Part of the level, the words outside it as they were. + let part = region([2, 1, 0], [3, 2, 1]); + let (part_row, words) = (4 * part.size.width, pattern(size, 100)); + let mut bytes = vec![UNTOUCHED; (part_row * part.size.height) as usize]; + let mut wanted = written.clone(); + for (texel, at) in rows(size, part, (part_row, 0)) { + let span = part_row as usize; + bytes[at..at + span].copy_from_slice(&words[texel..texel + span]); + wanted[texel..texel + span].copy_from_slice(&words[texel..texel + span]); + } + (texture.replace_region(part, 0, 0, &bytes, part_row, 0)).unwrap(); + let mut back = vec![UNTOUCHED; wanted.len()]; + (texture.get_bytes(&mut back, row, image, whole, 0, 0)).unwrap(); + assert_eq!(back, wanted, "{part:?} alone"); + eprintln!( + "{}: a two-plane texture's write took {host} copies on the host, {staged} on the staging \ + queue and {waited} internal submissions, its read {read_host}, {read_staged} and \ + {read_waited}; a plain texture's write took {plain_host} on the host and {plain_waited} \ + internal submissions", + device.name() + ); + // A device that copies a plain texture's texels on the host copies a two-plane texture's + // there too, and waits for nothing: the call read the texels and wrote them back through two + // submissions it waited for before. + if plain_host > 0 { + assert!( + host > 0 && read_host > 0, + "a two-plane texture's CPU copies left the host path a plain texture's take: \ + either the device has no `HOST_IMAGE_TRANSFER` for the image's `R32G32_UINT`, or \ + the copies regressed onto a submission" + ); + assert_eq!( + [waited, read_waited], + [0, 0], + "a two-plane texture's CPU copies waited for an internal submission" + ); + } +} +/// A block-compressed texture's `replaceRegion` waits for no submission on any device: the copy +/// is staged for the device's next submission (`texture/first_move.rs`) where the image takes no +/// host copies, and runs on the host where it does. Its `getBytes` waits for none either, but +/// only where the device copies such an image's blocks on the host: a driver that copies a +/// host-transfer image out of its memory as the memory is bound takes block formats off that +/// path (`RuntimeDriverDefect::HostTransferImagesCopiedAtBind`, MoltenVK), and there the read +/// goes through a staging buffer by a submission it waits for -- one a call, which is what the +/// suite's `compressed-moves` cases spend their blocked time on. +/// +/// The levels here are small and few, so the staged copies stay under the bound past which a +/// `replaceRegion` makes a submission of its own (`first_move.rs`, `STAGED_BYTES` and +/// `STAGED_COPIES`). +#[test] +fn block_cpu_copies_stage_their_writes_and_read_where_the_device_copies_on_the_host() { + let scratch = Scratch::new("block-cpu-copies"); + let device = device(&scratch.0); + let format = PixelFormat::BC1RGBA; + let mut descriptor = TextureDescriptor::texture_2d(format, 13, 7, true); + descriptor.usage = TextureUsage::SHADER_READ; + descriptor.resource_options = ResourceOptions::shared(); + let Ok(texture) = device.new_texture(&descriptor) else { + eprintln!("{}: no `BC1_RGBA` texture", device.name()); + return; + }; + let levels = texture.mipmap_level_count(); + let mut written = [0; 3]; + let mut read = [0; 3]; + for level in 0..levels { + let (width, height) = ((13u64 >> level).max(1), (7u64 >> level).max(1)); + let (blocks, lines) = (width.div_ceil(4), height.div_ceil(4)); + // Rows two blocks past their blocks, as `compressed_moves`' `put` step pads them. + let row = 8 * (blocks + 2); + let bytes: Vec = (0..row * lines).map(|i| (i % 251) as u8).collect(); + let region = Region::new_2d(0, 0, width, height); + let put = paths(&device, || { + (texture.replace_region(region, level, 0, &bytes, row, 0)).unwrap() + }); + let mut back = vec![UNTOUCHED; bytes.len()]; + let get = paths(&device, || { + (texture.get_bytes(&mut back, row, 0, region, level, 0)).unwrap() + }); + // Each row's blocks, the padding past them left as the caller's bytes held it. + let mut wanted = vec![UNTOUCHED; bytes.len()]; + for line in 0..lines { + let (at, blocks) = ((line * row) as usize, (8 * blocks) as usize); + wanted[at..at + blocks].copy_from_slice(&bytes[at..at + blocks]); + } + assert_eq!(back, wanted, "level {level} read back"); + for (counted, one) in [(&mut written, put), (&mut read, get)] { + for (total, count) in counted.iter_mut().zip(one) { + *total += count; + } + } + } + let [host, staged, waited] = written; + let [read_host, read_staged, read_waited] = read; + eprintln!( + "{}: {levels} levels' writes took {host} copies on the host, {staged} on the staging \ + queue and {waited} internal submissions, their reads {read_host}, {read_staged} and \ + {read_waited}", + device.name() + ); + assert_eq!( + waited, 0, + "a block-compressed texture's `replaceRegion` waited for an internal submission" + ); + // Where the device reads such an image's blocks on the host, so does `getBytes`. + if read_host > 0 { + assert_eq!( + [read_host, read_waited], + [levels, 0], + "a block-compressed texture's `getBytes` waited for an internal submission" + ); + } +} diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index ffbf7f9ce..6a6f47a02 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -5708,10 +5708,23 @@ impl Texture { /// The color words of `region` of a two-plane texture's image into `words`, packed 4 bytes a /// texel: each texel's first word. + /// + /// Read on the host where the image copies there (`texture/host_copy.rs`), with no submission + /// and no wait, as Metal's `getBytes` of such a texture waits for no GPU work; else through a + /// staging buffer, by a submission waited for. fn read_color_words(&self, region: &ImageRegion, words: &mut [u8]) -> Result<(), Error> { let Storage::Image(image) = &self.0.storage else { return Err(internal("a two-plane texture's copy without its image")); }; + // A shadowed view's texels are its texture's (`texture/view_shadow.rs`). + let truth = view_shadow::truth(image); + if truth.takes_host_copies() { + let mut texels = vec![0; 2 * words.len()]; + if truth.host_read(buffer_image_copy(region), &mut texels)? { + two_planes::narrow(&texels, words); + return Ok(()); + } + } let mut staging = self.color_texels(image, region, words.len() as u64)?; two_planes::narrow(staging.bytes(), words); Ok(()) @@ -5720,17 +5733,34 @@ impl Texture { /// `words`, packed 4 bytes a texel, over the first word of each texel of `region` of a /// two-plane texture's `image`: its texels read first and written back, the alpha words as /// they were. + /// + /// Read and written back on the host where the image copies there (`texture/host_copy.rs`), + /// with no submission and no wait, as Metal's `replaceRegion` of such a texture waits for no + /// GPU work; else through a staging buffer, by submissions waited for. fn write_color_words( &self, image: &Arc, region: &ImageRegion, words: &[u8], ) -> Result<(), Error> { + // A shadowed view's texels are its texture's (`texture/view_shadow.rs`). + let truth = view_shadow::truth(image); + let copy = buffer_image_copy(region); + if truth.takes_host_copies() { + let mut texels = vec![0; 2 * words.len()]; + if truth.host_read(copy, &mut texels)? { + two_planes::widen(words, &mut texels); + // A write the image declines (it presents a stand-in now, its texture's bytes + // detached meanwhile) goes through the staging buffer below, which reads its + // texels again. + if truth.host_write(copy, &texels)? { + return Ok(()); + } + } + } let mut staging = self.color_texels(image, region, words.len() as u64)?; two_planes::widen(words, staging.bytes()); - let truth = view_shadow::truth(image); let (buffer, raw) = (staging.raw, truth.raw.get()); - let copy = buffer_image_copy(region); truth.submit_copy(|commands, command_buffer| { transfer_barrier(commands, command_buffer, true); // SAFETY: the internal command buffer is recording; the staging buffer and the image diff --git a/crates/metal2vulkan-runtime/src/two_planes.rs b/crates/metal2vulkan-runtime/src/two_planes.rs index 4af820285..7847feca6 100644 --- a/crates/metal2vulkan-runtime/src/two_planes.rs +++ b/crates/metal2vulkan-runtime/src/two_planes.rs @@ -26,10 +26,12 @@ //! **Held** as one `R32G32_UINT` image (`metal::format::ExtendedRange::Planes`): each texel's //! color word, then its alpha byte in the second word. //! -//! - **CPU copies** go through a staging buffer of the region's texels, 8 bytes each, repacked -//! on the host ([`narrow`], [`widen`]): `getBytes` takes each texel's first word; `replaceRegion` -//! reads the region's texels first and writes them back with their first words replaced, so the -//! alpha words stay. +//! - **CPU copies** move the region's texels, 8 bytes each, repacked on the host ([`narrow`], +//! [`widen`]): `getBytes` takes each texel's first word; `replaceRegion` reads the region's +//! texels first and writes them back with their first words replaced, so the alpha words stay. +//! They move on the host where the image copies there (`texture/host_copy.rs`), with no +//! submission and so no wait, as Metal's `getBytes` and `replaceRegion` of such a texture wait +//! for no GPU work; else through a staging buffer, by a submission each waited for. //! - **Blits between a buffer and the texture** go through a scratch buffer of the encoder in //! bands of rows ([`bands`]), one blit command: per band, one copy of its texels out of the //! image, one dispatch of `bind::plane_words_module` moving each texel's first word into the