From a7dae53f739c196a897b678d2cc4eeab146eee99 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 11:19:11 -0800 Subject: [PATCH] Count a heap shadow's CPU copies; the staged-copy test knows a shadow stages nothing On the RX 7900 XTX a Shared heap's 64 x 64 RGBA8Unorm texture is a shadow of its uncompressed tiles in the heap's bytes (AliasRoute::Tiled, as Metal keeps it compressed). replace_region writes those bytes and get_bytes reads them on the host (HeapShadow::write, HeapShadow::read): nothing is staged and no submission is made. first_use's a_new_heap_texture_s_staged_copy_lands_in_its_first_move_s_submission expected get_bytes's internal submission on every device and failed there (internal submissions 0, expected 1; jobs 4295, 4500). WorkCounts::heap_shadow_cpu_copies (new) counts each such CPU copy. The test reads it: where replace_region was made in the heap's bytes, get_bytes counts one more, with no internal submission and no vkQueueSubmit; elsewhere the staged path is asserted as before. heap_layouts' host-heap tiles test asserts the count (replaceRegion and getBytes: 2, no internal submission), so the count runs on lavapipe too. api_signatures.tsv gains the field; signatures --check and refusal_surface --check pass. Not run yet: the XTX first_use lane and lavapipe heap_layouts. (cherry picked from commit e49978217639f3e77c2821d8291b01ea2052b390) --- crates/metal2vulkan-runtime/src/texture.rs | 8 +++- .../metal2vulkan-runtime/src/work_counts.rs | 12 ++++++ .../src/runtime_lane/first_use.rs | 42 +++++++++++++++---- .../src/runtime_lane/heap_layouts.rs | 18 ++++++-- crates/metal2vulkan/tests/api_signatures.tsv | 1 + 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 6585eef63..29a8a690e 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -5555,7 +5555,9 @@ impl Texture { // A heap shadow's texels are the heap's bytes (but a two-plane texture's later levels'). if let Owner::HeapShadow(shadow) = &image.owner { if shadow.holds(image.plan(), &image_region) { - return shadow.write(CALL, image.plan(), &image_region, &host, bytes.slice()); + shadow.write(CALL, image.plan(), &image_region, &host, bytes.slice())?; + self.shared().counts.heap_shadow_cpu_copy(); + return Ok(()); } } // A two-plane texture's color words over its texels as they are, the alpha words kept. @@ -5865,7 +5867,9 @@ impl Texture { // A heap shadow's texels are the heap's bytes (but a two-plane texture's later levels'). if let Owner::HeapShadow(shadow) = &image.owner { if shadow.holds(image.plan(), image_region) { - return shadow.read(call, image.plan(), image_region, host, bytes); + shadow.read(call, image.plan(), image_region, host, bytes)?; + self.shared().counts.heap_shadow_cpu_copy(); + return Ok(()); } } // A shadowed view's texels are its texture's (`texture/view_shadow.rs`). diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 462e49926..bc5c6f322 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -255,6 +255,7 @@ pub(crate) struct Counters { private_textures_unfilled: AtomicU64, private_textures_zero_filled: AtomicU64, host_texel_copies: AtomicU64, + heap_shadow_cpu_copies: AtomicU64, texel_shadows_made: AtomicU64, texel_shadow_bytes: AtomicU64, texel_shadow_reads: AtomicU64, @@ -3430,6 +3431,11 @@ impl Counters { self.host_texel_copies.fetch_add(1, Ordering::Relaxed); } + /// A heap shadow's CPU copy ran in the heap's bytes (`texture/heap.rs`, `HeapShadow::pieces`). + pub(crate) fn heap_shadow_cpu_copy(&self) { + self.heap_shadow_cpu_copies.fetch_add(1, Ordering::Relaxed); + } + /// A texture's host shadow of `bytes` was made at a CPU read of it /// (`texture/host_shadow.rs`). pub(crate) fn texel_shadow_made(&self, bytes: u64) { @@ -5043,6 +5049,11 @@ pub struct WorkCounts { /// `replace_region` and `get_bytes` copies that ran on the host, with no submission /// (`texture/host_copy.rs`). pub host_texel_copies: u64, + /// `replace_region` and `get_bytes` copies of a heap's texture that is a shadow of the heap's + /// bytes (a texture Metal tiles there, in a heap over host pages or one whose memory takes no + /// image of it, and a two-plane texture's first level), made in those bytes on the host, the + /// texels' truth, with no submission and no staging (`texture/heap.rs`). + pub heap_shadow_cpu_copies: u64, /// Host shadows of textures' texels made at a CPU read of them (`texture/host_shadow.rs`): /// one a texture whose CPU reads the driver keeps off the host and that the program reads /// back, made by the submission its first read waits for either way. A texture the program @@ -6843,6 +6854,7 @@ impl Device { private_textures_zero_filled: (counters.private_textures_zero_filled) .load(Ordering::Relaxed), host_texel_copies: counters.host_texel_copies.load(Ordering::Relaxed), + heap_shadow_cpu_copies: counters.heap_shadow_cpu_copies.load(Ordering::Relaxed), texel_shadows_made: counters.texel_shadows_made.load(Ordering::Relaxed), texel_shadow_bytes: counters.texel_shadow_bytes.load(Ordering::Relaxed), texel_shadow_reads: counters.texel_shadow_reads.load(Ordering::Relaxed), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs index 4900f2062..12a9731f3 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs @@ -27,9 +27,11 @@ //! memory whose fills are not transfers its zeros are one `vkCmdFillBuffer` //! (`WorkCounts::new_buffer_zero_fills`), elsewhere copies of the device's zeros. //! - **A heap texture's `replace_region` waits for nothing**: its copy is staged for the device's -//! next submission; a later command buffer reads the later of two calls' bytes, and `get_bytes` -//! the staged bytes. A new heap texture's staged copy lands where that submission also carries -//! the texture's first move, a command buffer's or `get_bytes`'s, in one `vkQueueSubmit`. +//! next submission, or made in the heap's bytes where the texture is a shadow of them; a later +//! command buffer reads the later of two calls' bytes, and `get_bytes` the staged bytes. A new +//! heap texture's staged copy lands where that submission also carries the texture's first move, +//! a command buffer's or `get_bytes`'s, in one `vkQueueSubmit`; a shadow's `get_bytes` reads the +//! heap's bytes on the host, with no submission. //! - **Neutrals and sparse textures wait for nothing**: kernels whose texture and texel-buffer //! slots nothing is bound to are encoded and committed, and a sparse texture is made, while the //! busy work runs, with no internal submission; the kernels then complete cleanly and the sparse @@ -1340,8 +1342,13 @@ fn a_heap_texture_s_replace_region_waits_for_no_gpu_work() { /// `get_bytes`'s internal submission (`device/submit.rs`) each read the copy's bytes, in one /// `vkQueueSubmit` of one `VkSubmitInfo` (the internal one followed by the relay's call of no /// work where the runtime relays the driver's failures, `commit_work::relay_marks`: after the -/// runtime's own submission a fence alone, of no `VkSubmitInfo`). Then an empty command buffer committed after a new texture is made carries its -/// first move in its batch's one `VkSubmitInfo`. +/// runtime's own submission a fence alone, of no `VkSubmitInfo`). A texture that is a shadow of +/// the heap's bytes (its elements in Metal's tiles there, a compressed one's uncompressed tiles: +/// the RX 7900 XTX's `Shared` one here) stages nothing: `replace_region` writes those bytes and +/// `get_bytes` reads them on the host (`WorkCounts::heap_shadow_cpu_copies`), with no +/// submission, and the blit's submission carries the move from them. Then an empty command +/// buffer committed after a new texture is made carries its first move in its batch's one +/// `VkSubmitInfo`. #[test] fn a_new_heap_texture_s_staged_copy_lands_in_its_first_move_s_submission() { on_device(|device| { @@ -1363,6 +1370,14 @@ fn a_new_heap_texture_s_staged_copy_lands_in_its_first_move_s_submission() { .unwrap(); let staged = device.work_counts(); let path = if by_get_bytes { "get_bytes" } else { "a blit" }; + // A shadow of the heap's bytes copies in them on the host: nothing is staged. + let hosted = staged.heap_shadow_cpu_copies - before.heap_shadow_cpu_copies; + assert!( + hosted <= 1, + "{path} on {name}: {hosted} copies in the heap's bytes" + ); + let hosted = hosted == 1; + println!("FIRST-USE on {name}: {path}: replace_region in the heap's bytes {hosted}"); assert_eq!( ( staged.queue_submissions, @@ -1402,18 +1417,29 @@ fn a_new_heap_texture_s_staged_copy_lands_in_its_first_move_s_submission() { after.host_texel_copies, before.host_texel_copies, "{path} on {name}: a host copy" ); + // get_bytes of a shadow of the heap's bytes reads them on the host: no submission. + let read_hosted = by_get_bytes && hosted; + assert_eq!( + after.heap_shadow_cpu_copies - staged.heap_shadow_cpu_copies, + u64::from(read_hosted), + "{path} on {name}: get_bytes in the heap's bytes" + ); assert_eq!( after.internal_submissions - before.internal_submissions, - u64::from(by_get_bytes), + u64::from(by_get_bytes && !read_hosted), "{path} on {name}: internal submissions" ); - let calls = 1 + if by_get_bytes { marks } else { 0 }; + let (calls, infos) = match (by_get_bytes, read_hosted) { + (_, true) => (0, 0), + (true, false) => (1 + marks, 1), + (false, false) => (1, 1), + }; assert_eq!( ( after.queue_submissions - before.queue_submissions, after.submit_infos - before.submit_infos, ), - (calls, 1), + (calls, infos), "{path} on {name}: the move, the copy and the work in one VkSubmitInfo" ); drop((texture, heap)); diff --git a/crates/metal2vulkan-validation/src/runtime_lane/heap_layouts.rs b/crates/metal2vulkan-validation/src/runtime_lane/heap_layouts.rs index 566d1c9c9..5bb5ec252 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/heap_layouts.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/heap_layouts.rs @@ -289,8 +289,10 @@ fn layouts_counted(before: &WorkCounts, after: &WorkCounts) -> [u64; 4] { /// A plain texture in a heap over host pages lies in Metal's tiles (the module docs): its /// answered size holds them; the bytes it is made over, `replaceRegion`, a blit into it, the CPU's /// writes and a buffer's over the bytes meet its texels there, and `getBytes` and a blit out of it -/// read them; no byte between them changes. Counted tiled, with no layout notice; made with no -/// zeros (`WorkCounts::images_started_on_the_host`, `staging_submissions`, `staging_moves_queued`). +/// read them; no byte between them changes, and its CPU copies are made in the heap's bytes with +/// no submission (`WorkCounts::heap_shadow_cpu_copies`). Counted tiled, with no layout notice; +/// made with no zeros (`WorkCounts::images_started_on_the_host`, `staging_submissions`, +/// `staging_moves_queued`). #[test] fn a_host_heap_s_plain_texture_lies_in_metal_s_tiles() { on_device(|device, messages| { @@ -346,8 +348,9 @@ fn a_host_heap_s_plain_texture_lies_in_metal_s_tiles() { mapping.fill(0); // `replaceRegion` writes the texels into Metal's tiles; `getBytes` and a blit read - // them back. + // them back. Both CPU copies are made in the heap's bytes on the host. let first = named(len, 1); + let copied = device.work_counts(); texture .replace_region(plain.region(), 0, 0, &first, plain.row(), 0) .unwrap(); @@ -360,6 +363,15 @@ fn a_host_heap_s_plain_texture_lies_in_metal_s_tiles() { .get_bytes(&mut read, plain.row(), 0, plain.region(), 0, 0) .unwrap(); assert_eq!(read, first, "{what}: getBytes"); + let read_back = device.work_counts(); + assert_eq!( + ( + read_back.heap_shadow_cpu_copies - copied.heap_shadow_cpu_copies, + read_back.internal_submissions - copied.internal_submissions, + ), + (2, 0), + "{what}: replaceRegion and getBytes in the heap's bytes, with no submission" + ); assert_eq!(blit_out(&queue, &texture, &plain), first, "{what}: a blit"); // A blit into the texture lands in the tiles, no other byte. diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 3e483ed6b..ee7356249 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2704,6 +2704,7 @@ field metal2vulkan-runtime::WorkCounts.heads_held_for_reencodes pub heads_held_f field metal2vulkan-runtime::WorkCounts.heads_waiting_at_their_gate pub heads_waiting_at_their_gate: u64 field metal2vulkan-runtime::WorkCounts.heap_sample_move_images_lent pub heap_sample_move_images_lent: u64 field metal2vulkan-runtime::WorkCounts.heap_sample_move_images_made pub heap_sample_move_images_made: u64 +field metal2vulkan-runtime::WorkCounts.heap_shadow_cpu_copies pub heap_shadow_cpu_copies: u64 field metal2vulkan-runtime::WorkCounts.heap_slot_writes_deferred pub heap_slot_writes_deferred: u64 field metal2vulkan-runtime::WorkCounts.heap_slots_rewritten pub heap_slots_rewritten: u64 field metal2vulkan-runtime::WorkCounts.heap_tables_worked_out pub heap_tables_worked_out: u64