From d33079bd05b80f7d9c6006170e9bebb1e9cc7fd3 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 10:33:15 -0800 Subject: [PATCH] Read a shadowed attachment through a snapshot after a barrier, not a split A texture over host pages that renders is always a shadow (in_place.rs: ShadowCause::RenderTarget), the consumer's guest's IOSurface framebuffer among them, and Texture::snapshot_color took only an image of its own or a linear image over the bytes. So every draw that read such a target at its own pixels after a barrier over the render targets split the render pass instance, on every device: a keep of the whole shadow for the merge, a copy of every texel from the bytes into the copy the reader reads, and a store and load of the attachment, at each read (the guest's selfread probe on the Iris Xe: 30 splits, 31 whole copies in and 30 keeps a frame, 98 ms). The shadow is the image the pass renders, so the snapshot path serves it as it serves an image of its own: - the shadow image takes the feedback loop usage where that costs it nothing (Device::if_free, as with_free_usage gives an image of its own), counted as WorkCounts::feedback_loop_shadow_images; - Texture::snapshot_color takes Storage::Shadow's image; - RenderCommandEncoder::over_bytes (the snapshot at any area) takes a texture that renders its bytes through a shadow too (Texture::renders_bytes), as the split's copy there reads the bytes into the reader's copy after a keep and a merge. Every other check of render/snapshot.rs stands; the reads are the snapshot's, exact as for an image of its own. The pass's shadow plan is unchanged: the attachment is copied in before the instance and merged back as the encoder ends, as before, without the keeps and copies of the splits. Tests: runtime_lane::snapshot_reads::a_surfaces_readers_take_the_snapshot_at_any_area now expects a snapshot for each reader of a surface over host pages wherever the device takes the path and the image the pass renders took the usage (the shadow's, or a linear alias's), its bytes unchanged. The self_reads lane prints the images made with the usage. The WorkCounts field is in the api_signatures table and host_ab's list; the FAST-PATHS row on attachment reads names shadows. (cherry picked from commit 60b9a69d393a097f8658af8bcb5a8d6ef3576441) --- .../examples/host_ab/runtime.rs | 1 + .../src/render/snapshot.rs | 13 ++++++------ crates/metal2vulkan-runtime/src/texture.rs | 21 +++++++++++++++++++ .../src/texture/attached_reads.rs | 15 +++++++------ .../metal2vulkan-runtime/src/work_counts.rs | 18 ++++++++++++++++ .../src/runtime_lane/attachment_read_costs.rs | 9 +++++--- .../src/runtime_lane/snapshot_reads.rs | 20 ++++++++++-------- crates/metal2vulkan/tests/api_signatures.tsv | 1 + docs/FAST-PATHS.md | 12 +++++------ 9 files changed, 80 insertions(+), 30 deletions(-) diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index 5217c4842..da8758e41 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -2051,6 +2051,7 @@ impl Gpu for Runtime { ("reading_draws_snapshotted", c.reading_draws_snapshotted), ("snapshot_inline_splits", c.snapshot_inline_splits), ("feedback_loop_alias_images", c.feedback_loop_alias_images), + ("feedback_loop_shadow_images", c.feedback_loop_shadow_images), ("depth_attachments_moved", c.depth_attachments_moved), ("depth_instances_held", c.depth_instances_held), ("held_depths_released", c.held_depths_released), diff --git a/crates/metal2vulkan-runtime/src/render/snapshot.rs b/crates/metal2vulkan-runtime/src/render/snapshot.rs index bb6f2e7d6..c14b7a758 100644 --- a/crates/metal2vulkan-runtime/src/render/snapshot.rs +++ b/crates/metal2vulkan-runtime/src/render/snapshot.rs @@ -359,11 +359,12 @@ impl RenderLowering { within } - /// Whether every one of the pass's `colors` renders a texture whose image lies over its - /// buffer's bytes (`Texture::image_over_buffer`, as a surface over host pages does, the - /// consumer's guest's framebuffer), whose reader takes the snapshot whatever its area: the - /// split's copy reads the bytes into the shadow, where the snapshot draw reads only the - /// pixels the draw covers. On the Iris Xe + /// Whether every one of the pass's `colors` renders a texture over its buffer's bytes + /// (`Texture::renders_bytes`: its image lies over them, or it is their shadow, as a surface + /// over host pages that renders is, the consumer's guest's framebuffer), whose reader takes + /// the snapshot whatever its area: the split's copy reads the bytes into the copy the reader + /// reads (through a shadow, after the instances before wrote theirs back through a keep and a + /// merge), where the snapshot draw reads only the pixels the draw covers. On the Iris Xe /// (`runtime_lane::attachment_read_costs::snapshot_crossover`, a 1920x1080 surface) the /// snapshot took 0.56 to 0.99 times the split's GPU time for readers through scissors of /// 960x540 and up, covering 1.5% to 100% of them, at most 1.08 times through the whole @@ -372,7 +373,7 @@ impl RenderLowering { fn over_bytes(&self, colors: &[usize]) -> bool { colors.iter().all(|&index| { let color = self.layout.colors.get(index).and_then(Option::as_ref); - color.is_some_and(|color| color.target.texture.image_over_buffer()) + color.is_some_and(|color| color.target.texture.renders_bytes()) }) } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index dbd29364d..ce6c443c7 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -2371,6 +2371,20 @@ impl Device { { return Err(Error::NotImplemented(RuntimeGap::Textures)); } + // The shadow is the image a pass renders where it renders the texture: it takes the + // feedback loop usage a draw takes that reads the texture its own pass renders through a + // snapshot (`feedback_candidate`, `texture/attached_reads.rs`) where that costs it + // nothing, as an image of its own does (`Device::with_free_usage`). + let plan = match feedback_candidate(&plan, self) { + Some(candidate) => self.if_free(plan, candidate, layout.texture_type)?, + None => plan, + }; + if plan + .usage + .contains(ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT) + { + self.shared().counts.feedback_loop_shadow_image(); + } // Where the device copies a shadow back faster by a dispatch, the image takes the // integer view the dispatch reads (`shadow_copy_back.rs`). Both views are 2D: a 1D // texture over an IOSurface plane's row (`linear_plan.rs`) takes neither. @@ -4298,6 +4312,13 @@ impl Texture { matches!(self.0.storage, Storage::Alias { .. }) && self.linear_image().unwrap_or(false) } + /// Whether a pass that renders the texture renders its buffer's bytes: in place, its image + /// lying over them now ([`image_over_buffer`](Self::image_over_buffer)), or through the + /// shadow image the encoders copy them into and back (`shadow.rs`), which the pass renders. + pub(crate) fn renders_bytes(&self) -> bool { + self.image_over_buffer() || matches!(self.0.storage, Storage::Shadow { .. }) + } + /// Whether the texture's image lies over its buffer's bytes now: an aliasing image, not /// presenting a stand-in while its bytes are detached (`detach.rs`), so a pass that renders it /// writes those bytes. diff --git a/crates/metal2vulkan-runtime/src/texture/attached_reads.rs b/crates/metal2vulkan-runtime/src/texture/attached_reads.rs index adf00301b..cbd7a8ef2 100644 --- a/crates/metal2vulkan-runtime/src/texture/attached_reads.rs +++ b/crates/metal2vulkan-runtime/src/texture/attached_reads.rs @@ -91,10 +91,12 @@ //! split, where every check of `render/snapshot.rs` holds: drawn twice, it first copies each //! fragment's texel into a snapshot of the texture inside the pixel interlock, and then reads //! the snapshot (`WorkCounts::reading_draws_snapshotted`). The texture's image takes feedback -//! loop usage where that costs it nothing ([`feedback_candidate`]): an image of its own, or the +//! loop usage where that costs it nothing ([`feedback_candidate`]): an image of its own, the //! linear image that aliases a buffer's bytes (`Device::storage_over_buffer` tries the usage //! first, its memory being the buffer's, and counts the images made with it, -//! `WorkCounts::feedback_loop_alias_images`), as a surface over host pages is. The instance +//! `WorkCounts::feedback_loop_alias_images`), or the shadow image a texture over a buffer's +//! bytes renders through (`shadow.rs`, `WorkCounts::feedback_loop_shadow_images`), as a surface +//! over host pages that renders is (`in_place.rs`). The instance //! holds its attachment in the feedback loop layout, the bytes too while it renders them, when a host //! access of them races the pass in Metal as well. Not the attachment itself: "In that case the overlap //! is defined to occur at a per-pixel granularity" (the Render Pass chapter), but at its own @@ -273,9 +275,10 @@ impl Texture { impl Texture { /// The index of the color attachment of `layout` a fragment slot binding of the texture - /// reads through a snapshot (the module docs), where it can: the texture is its own image or - /// a linear image aliasing a buffer's bytes while they lie in their pages (not a shadow, a - /// texture buffer or a view read through another), a single-sampled 2D texture whose image + /// reads through a snapshot (the module docs), where it can: the texture is its own image, a + /// linear image aliasing a buffer's bytes while they lie in their pages, or the shadow image + /// the encoders copy a buffer's bytes into and back (`shadow.rs`), which its passes render (not + /// a texture buffer or a view read through another), a single-sampled 2D texture whose image /// has feedback loop and sampled usage /// ([`feedback_candidate`]) and whose sampled binding takes no shadow; and the attachment /// renders that image in place (no stand-in or sample planes), one layer of it, at the @@ -283,7 +286,7 @@ impl Texture { /// coordinate is the attachment's texel at that pixel. pub(crate) fn snapshot_color(&self, layout: &PassLayout) -> Option { let image = match &self.0.storage { - Storage::Image(image) => image, + Storage::Image(image) | Storage::Shadow { image, .. } => image, Storage::Alias { image, .. } if self.image_over_buffer() => image, _ => return None, }; diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index bc5c6f322..435303829 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -459,6 +459,7 @@ pub(crate) struct Counters { reading_draws_snapshotted: AtomicU64, snapshot_inline_splits: AtomicU64, feedback_loop_alias_images: AtomicU64, + feedback_loop_shadow_images: AtomicU64, depth_attachments_moved: AtomicU64, depth_instances_held: AtomicU64, held_depths_released: AtomicU64, @@ -3351,6 +3352,13 @@ impl Counters { .fetch_add(1, Ordering::Relaxed); } + /// The device made the image of a texture over a buffer's bytes that the encoders copy them + /// into and back (`shadow.rs`) with feedback loop usage (`texture::feedback_candidate`). + pub(crate) fn feedback_loop_shadow_image(&self) { + self.feedback_loop_shadow_images + .fetch_add(1, Ordering::Relaxed); + } + /// A render pass instance holds its multisampled depth/stencil attachment in the attachment /// layout (`render_target::depth_layout`). pub(crate) fn depth_attachment_moved(&self) { @@ -6292,6 +6300,13 @@ pub struct WorkCounts { /// its pass renders it (`render/snapshot.rs`): one per image made. Where the device makes no /// such image with the usage, it makes one without, and the texture's readers split. pub feedback_loop_alias_images: u64, + /// Images of textures over a buffer's bytes that the encoders copy them into and back (the + /// texture's shadow, `shadow.rs`: a texture over host pages that renders, among others) that + /// the device made with feedback loop usage where it cost them nothing + /// (`texture::feedback_candidate`), so a draw may read the texture through a snapshot while + /// its pass renders that image (`render/snapshot.rs`): one per image made. Where the usage + /// costs the image, it is made without, and the texture's readers split. + pub feedback_loop_shadow_images: u64, /// Render pass instances that held their multisampled depth/stencil attachment in /// `DEPTH_STENCIL_ATTACHMENT_OPTIMAL`, moved there from `GENERAL` just before each began and /// back just after it ended, on a device measured to render it faster so @@ -7131,6 +7146,9 @@ impl Device { reading_draws_snapshotted: counters.reading_draws_snapshotted.load(Ordering::Relaxed), snapshot_inline_splits: counters.snapshot_inline_splits.load(Ordering::Relaxed), feedback_loop_alias_images: counters.feedback_loop_alias_images.load(Ordering::Relaxed), + feedback_loop_shadow_images: counters + .feedback_loop_shadow_images + .load(Ordering::Relaxed), depth_attachments_moved: counters.depth_attachments_moved.load(Ordering::Relaxed), depth_instances_held: counters.depth_instances_held.load(Ordering::Relaxed), held_depths_released: counters.held_depths_released.load(Ordering::Relaxed), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs b/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs index d1c830774..3ec6f8a29 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs @@ -52,8 +52,8 @@ //! its own pixels (`tob_read`), through no scissor; each target also without the barriers and //! readers, the baseline. Printed per case (`SELF-READ-COST`): the GPU time per frame, and per //! frame the splits, snapshots, render pass instances begun, shadow copies, texels copied in -//! (and of them those of a surface's shadow copied in again), keeps and their texels, merges, and -//! the shadows made of render targets over host pages. +//! (and of them those of a surface's shadow copied in again), keeps and their texels, merges, the +//! shadows made of render targets over host pages, and the images made with feedback loop usage. //! //! Ignored by default: measurements, not checks. Run them by name with `--run-ignored all`. @@ -978,7 +978,8 @@ fn self_reads_on(device: &Device, policy: HostPageTexturePolicy) { {}x{}: GPU {:.3} ms/frame (median); per frame: splits {:.2}, snapshotted \ {:.2}, instances {:.2}, shadow copies {:.2}, bounded {:.2}, texels copied in \ {:.0}, of a surface again {:.0}, keeps {:.2}, texels kept {:.0}, merges {:.2}; \ - render-target shadows over host pages {}", + render-target shadows over host pages {}, images made with feedback loop \ + usage: shadows {}, aliases {}", SELF_PAIRS, if baseline { "baseline" } else { "self-read" }, SELF_RECT[0], @@ -1007,6 +1008,8 @@ fn self_reads_on(device: &Device, policy: HostPageTexturePolicy) { per(kept(&before), kept(&after)), per(before.shadow_merges_fetched, after.shadow_merges_fetched), after.host_texture_shadows_render_target, + after.feedback_loop_shadow_images, + after.feedback_loop_alias_images, ); } } diff --git a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs index f2f6f25d4..a2969254a 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs @@ -34,11 +34,11 @@ //! of 20 against 1024 of 8), a snapshot for each where the device takes the path. //! - [`a_surfaces_readers_take_the_snapshot_at_any_area`]: the same draws over a surface over //! host pages, an `RGBA8Unorm` texture of an IOSurface plane over a no-copy buffer, through the -//! whole target's scissor: a snapshot for each where the device takes the path and the -//! texture's image aliases the bytes with feedback loop usage -//! (`WorkCounts::feedback_loop_alias_images`; `RenderCommandEncoder::snapshot_cheaper` weighs no -//! area for it), else a split (NVIDIA's drivers, measured on an RTX 3090 and an RTX 5080, make -//! the linear image without the usage). +//! whole target's scissor: a snapshot for each where the device takes the path and the image +//! the pass renders took feedback loop usage, the texture's shadow (a surface over host pages +//! that renders is one, `in_place.rs`; `WorkCounts::feedback_loop_shadow_images`) or a linear +//! image aliasing the bytes (`WorkCounts::feedback_loop_alias_images`), as +//! `RenderCommandEncoder::snapshot_cheaper` weighs no area for either; else a split. //! - [`each_form_reads_what_the_split_reads`]: the first test's draws over a target of each of //! `RGBA8Unorm`, `BGRA8Unorm`, `R8Unorm`, `RG8Unorm` (8-bit codes), `RGBA16Float` (half-float //! codes) and `RGB10A2Unorm` (bits): the bytes the split leaves, byte for byte, with a snapshot @@ -179,8 +179,9 @@ fn readers( } /// How [`readers_over`]' target was made: whether its image aliases its bytes (made as no -/// shadow, `WorkCounts::shadowed_textures`), and whether with feedback loop usage -/// (`WorkCounts::feedback_loop_alias_images`). +/// shadow, `WorkCounts::shadowed_textures`), and whether the image its passes render took +/// feedback loop usage (`WorkCounts::feedback_loop_alias_images`, or +/// `feedback_loop_shadow_images` for a shadow). #[derive(Debug)] struct Made { aliased: bool, @@ -230,7 +231,8 @@ fn readers_over( let counts = device.work_counts(); let made = Made { aliased: counts.shadowed_textures == made.shadowed_textures, - feedback: counts.feedback_loop_alias_images > made.feedback_loop_alias_images, + feedback: counts.feedback_loop_alias_images > made.feedback_loop_alias_images + || counts.feedback_loop_shadow_images > made.feedback_loop_shadow_images, }; let back = device .new_buffer(SIDE * row, ResourceOptions::shared()) @@ -539,7 +541,7 @@ fn a_surfaces_readers_take_the_snapshot_at_any_area() { let takes = snapshots(device); eprintln!("snapshot reads, surface, whole scissor: {made:?}, {moved:?}"); holds("surface, whole scissor", &bytes, 6 * FILL); - let expected = if takes && made.aliased && made.feedback { + let expected = if takes && made.feedback { (2, 0) } else { (0, 2) diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 2d279186c..fe79a2127 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2654,6 +2654,7 @@ field metal2vulkan-runtime::WorkCounts.encoders_reencoded pub encoders_reencoded field metal2vulkan-runtime::WorkCounts.event_signals_behind_merge pub event_signals_behind_merge: u64 field metal2vulkan-runtime::WorkCounts.event_waits_met pub event_waits_met: u64 field metal2vulkan-runtime::WorkCounts.feedback_loop_alias_images pub feedback_loop_alias_images: u64 +field metal2vulkan-runtime::WorkCounts.feedback_loop_shadow_images pub feedback_loop_shadow_images: u64 field metal2vulkan-runtime::WorkCounts.fence_waits pub fence_waits: u64 field metal2vulkan-runtime::WorkCounts.fences_made pub fences_made: u64 field metal2vulkan-runtime::WorkCounts.fills_by_transfers pub fills_by_transfers: u64 diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 2db8a075c..d11884972 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -203,12 +203,12 @@ recorded here. instance (`texture/attached_reads.rs`): the texels around the draw's scissors where it reads at its own pixels alone, else every texel or what the instances since drew. After a barrier inside the instance, an own-pixel read takes a snapshot drawn in its subpass (`render/snapshot.rs`, - docs/API.md "Snapshot reads") or splits the instance; both are exact. Over a texture whose image - lies over its buffer's bytes (a linear image over an ordinary buffer's packed rows) it takes the - snapshot at any area, where - the device makes that linear image with feedback loop usage - (`WorkCounts::feedback_loop_alias_images`; NVIDIA's drivers make it without, and a surface's - readers split there): on the Iris Xe (`runtime_lane::attachment_read_costs::snapshot_crossover`, + docs/API.md "Snapshot reads") or splits the instance; both are exact. Over a texture over its + buffer's bytes (a linear image over an ordinary buffer's packed rows, or the shadow a texture + over host pages that renders is) it takes the snapshot at any area, where the device makes that + image with feedback loop usage (`WorkCounts::feedback_loop_alias_images`, + `feedback_loop_shadow_images`; NVIDIA's drivers make the linear one without, and such a + texture's readers split there): on the Iris Xe (`runtime_lane::attachment_read_costs::snapshot_crossover`, a 1920x1080 surface) 0.56 to 0.99 times the split's GPU time through scissors of 960x540 and up, at most 1.08 times through the whole target and 1.2 times through a 320x180 scissor it covers half of or more.