From eb6c13f52be2a78a7022fc58f67c3e197312f46d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 12:32:37 -0800 Subject: [PATCH] Give a render target in place over host pages the storage usage its snapshot reads fetch through On the Iris Xe (m3-max-4845, the self_reads lane, a 3662x2039 BGRA8 surface over host pages, 30 pairs of a 600x400 draw, a render-target barrier and a draw reading the target at its own pixel), a surface in place (557d9f0905) split every reading draw: 30 splits a frame, 30 copies of the whole surface into the reader's copy (224M texels copied in), 22.4 and 27.2 ms of GPU a frame, where its shadow took the snapshot (cut A, 60b9a69d39): 30 snapshotted draws, no split, 4.7 and 4.3 ms. The in-place image took the feedback loop usage (aliases 1) but not the storage usage, which a snapshot's fetch of the attachment in the pixel interlock reads through (RenderLowering::keeps_own_attachment), so snapshot_wanted fell back to the split: the image was the texture's declared plan's, where a shadow takes undeclared usages (with_undeclared_usage). A texture over host pages that renders, in place, now takes the storage usage too (framebuffer_fetch::storage_candidate over the feedback loop plan, with the view in another format a fetch stores through where the device needs one: ANV's RGBA twin of a BGRA format) where the device's check passed for a shader's writes over the pages' class and the texel is one the check's kernel stores (4, 8 or 16 bytes): with that usage a shader writes the texels in place, which the check's writes cover. Tried first, then the feedback loop plan, then the declared plan: the first the device makes the image in place with. Elsewhere its readers split as before. A rebind asks the writes verdict of an image with storage usage already. Tests: tests::in_place_storage (new, device): a render target in place over host pages has the storage usage only where the check passed for writes there, with the feedback loop usage and a fetch through storage. runtime_lane::snapshot_reads::a_surfaces_readers_take_the_snapshot_at_any_area expects a snapshot for an image in place only where it took the storage usage (Pointer writes: Passed), as for a shadow. Docs: in_place_image.rs (its usage), API.md (textures over host pages in place), FAST-PATHS.md (the attachment-read row: images over host pages, and the shadow only where a texture is one). Verified: cargo fmt; the build, clippy, lavapipe and the Iris A/B and snapshot lane follow in the tip's evidence. (cherry picked from commit e4d61153702293e23bd874cf0830ff22d6be307a) --- .../src/tests/in_place_storage.rs | 66 +++++++++++++++++++ crates/metal2vulkan-runtime/src/tests/mod.rs | 2 + crates/metal2vulkan-runtime/src/texture.rs | 13 +++- .../src/texture/in_place_image.rs | 36 +++++++--- .../src/runtime_lane/snapshot_reads.rs | 25 +++++-- docs/API.md | 7 +- docs/FAST-PATHS.md | 12 ++-- 7 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/in_place_storage.rs diff --git a/crates/metal2vulkan-runtime/src/tests/in_place_storage.rs b/crates/metal2vulkan-runtime/src/tests/in_place_storage.rs new file mode 100644 index 000000000..35be0294b --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/in_place_storage.rs @@ -0,0 +1,66 @@ +//! A render target over host pages in place (`texture/in_place_image.rs`) takes the storage usage +//! a snapshot's fetch of it in the pixel interlock reads through (`render/snapshot.rs`) only where +//! the device's check passed for a shader's writes over such pages, and for a texel the check's +//! kernel stores: with that usage a shader writes its texels in place. Its image's plan says which +//! usages it took. +//! +//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, +//! `m2v_cpu_icd`), on the kind of device the cfg selects. The device's shader cache is a fresh +//! directory, so its check runs. + +use super::blit_holds::{device, over_host, Host, Scratch}; +use crate::framebuffer_fetch::through_storage; +use crate::TextureDescriptor; +use metal2vulkan::metal::{PixelFormat, TextureUsage}; +use metal2vulkan::vk::ImageUsageFlags; +use std::sync::atomic::AtomicUsize; +use std::sync::Arc; +use std::time::Duration; + +/// The textures' side, in texels. +const SIDE: u64 = 64; + +#[test] +fn a_render_target_in_place_takes_the_storage_usage_only_where_writes_passed() { + let host = Host::new(); + let scratch = Scratch::new("in-place-storage"); + let device = device(&scratch.0); + let runs = Arc::new(AtomicUsize::new(0)); + let Some(buffer) = over_host(&device, &host, &runs) else { + return; + }; + let settled = device.settle_in_place_checks(Duration::from_secs(120)); + let report = device.in_place_check_report(); + let passed = |line: &str| report.iter().any(|l| l == line); + let fetches = through_storage(device.compile_device().profile()); + for (format, texel) in [ + (PixelFormat::RGBA8Unorm, 4), + (PixelFormat::BGRA8Unorm, 4), + (PixelFormat::RGBA16Float, 8), + ] { + let mut descriptor = TextureDescriptor::texture_2d(format, SIDE, SIDE, false); + descriptor.usage = TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ; + let texture = buffer.new_texture(&descriptor, 0, SIDE * texel).unwrap(); + let in_place = texture.shadow().is_none() && texture.linear_image().unwrap(); + let usage = texture.image_plan().unwrap().usage; + let storage = usage.contains(ImageUsageFlags::STORAGE); + let feedback = usage.contains(ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT); + println!( + "IN-PLACE-STORAGE on {}: {format:?}: settled {settled}, fetched through storage \ + {fetches}, in place {in_place}, feedback loop usage {feedback}, storage usage \ + {storage}; {report:?}", + device.name() + ); + if in_place { + assert!( + passed("Pointer renders: Passed"), + "{format:?}: in place where the check did not pass for render targets" + ); + assert!( + !storage || (passed("Pointer writes: Passed") && feedback && fetches), + "{format:?}: the storage usage where the check did not pass for a shader's \ + writes, or with no fetch to read through it" + ); + } + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 653f4e29b..5916b4973 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -199,6 +199,8 @@ mod image_plan; mod in_place; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod in_place_check; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod in_place_storage; mod inbox; mod indirect_command_buffer; #[cfg(any(m2v_gpu, m2v_cpu_icd))] diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 5f7d8245d..e1a1c2aae 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -2296,7 +2296,18 @@ impl Device { let pages = (class, texels.texel_bytes()); match in_place::in_place(policy, usage, pages, verdict) { Ok(()) => { - match self.in_place_image(&plan, descriptor, &texels, class, &facts)? { + // A texture that renders takes the storage usage too where the check + // passed for a shader's writes over the class and its texel is one the + // check's kernel stores: a draw that reads it through a snapshot + // fetches it in the pixel interlock through that usage + // (`render/snapshot.rs`), and with it a shader's writes reach the + // texels in place, as the check's writes do. + let storable = in_place::Access::Renders.takes(usage) + && !in_place::Access::Writes.takes(usage) + && in_place::STORED_TEXEL_BYTES.contains(&pages.1) + && verdict(class, in_place::Access::Writes) == Verdict::Passed; + let made = (&texels, class, storable); + match self.in_place_image(&plan, descriptor, made, &facts)? { Ok((image, pitched)) => { if !checking { let counts = &self.shared().counts; diff --git a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs index f0c4bfdad..c3d8a680a 100644 --- a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs +++ b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs @@ -29,7 +29,12 @@ //! since the image has no attachment usage; their copies into it touch its texels alone. One that //! renders also takes the feedback loop usage a draw takes that reads the texture its own pass //! renders through a snapshot (`texture::feedback_candidate`, `render/snapshot.rs`) where the -//! device makes the image with it, as an aliasing image over a buffer of the runtime's does: +//! device makes the image with it, as an aliasing image over a buffer of the runtime's does, +//! and with it the storage usage the snapshot's fetch of the attachment in the pixel interlock +//! reads through (`framebuffer_fetch::storage_candidate`, which may list a view in another +//! format), where the device's check passed for a shader's writes over the pages' class too and +//! the texel is one its kernel stores (with that usage a shader writes the texels in place, as +//! one declared to does): the first of those plans the device makes the image in place with, //! else the declared plan's image. //! - **A rebind** (`rebind.rs`) makes it again over the new pages, its texels as far from their //! first byte as from the old pages', as a no-copy heap's images are @@ -46,6 +51,7 @@ use crate::buffer::{Buffer, InPlaceWatch, ReadApart}; use crate::buffer_texture::Texels; use crate::device::lock; use crate::error::{vulkan, Error, RuntimeGap}; +use crate::framebuffer_fetch::storage_candidate; use crate::image_plan::{ImageFacts, ImagePlan}; use crate::in_place::{Access, PagesClass, Refusal}; use crate::linear_plan::{check_linear_limits, linear_lack, BufferLayout, LinearLack}; @@ -206,8 +212,8 @@ impl Texture { impl Device { /// The image in place of a texture of `plan` (its declared usage's, with the feedback loop - /// usage where the device makes it so: the module docs) made with `descriptor` over `texels`, - /// host pages of `class`, with whether it is a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch + /// usage where the device makes it so, and the storage usage too where `storable`: the module + /// docs) made with `descriptor` over `texels`, host pages of `class`, with whether it is a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch /// made for host allocations; why not where the device makes none, the declared plan's /// refusal (the module docs). Over a no-copy buffer's pages (or a no-copy heap's) it is noted /// for a rebind to make again. Its first move is noted, for the device's next submission that @@ -216,8 +222,7 @@ impl Device { &self, plan: &ImagePlan, descriptor: &TextureDescriptor, - texels: &Texels, - class: PagesClass, + (texels, class, storable): (&Texels, PagesClass, bool), facts: &ImageFacts<'_>, ) -> Result { let layout = texels.layout; @@ -238,11 +243,22 @@ impl Device { self.host_in_place(plan, descriptor, texels, class, facts) } }; - let with_feedback = match feedback_candidate(plan, self) { - Some(candidate) => make(&candidate)?.ok(), - None => None, - }; - let (image, pitched) = match with_feedback { + // The wider plans, widest first: the feedback loop usage, and with it the storage usage + // where `storable`. + let profile = self.compile_device().profile(); + let feedback = feedback_candidate(plan, self); + let storage = feedback + .as_ref() + .filter(|_| storable) + .and_then(|feedback| storage_candidate(feedback, profile)); + let mut made = None; + for wider in storage.iter().chain(&feedback) { + if let Ok(image) = make(wider)? { + made = Some(image); + break; + } + } + let (image, pitched) = match made { Some(made) => made, None => match make(plan)? { Ok(made) => made, diff --git a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs index 0a4d4d773..2059a6573 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs @@ -36,8 +36,11 @@ //! 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 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 +//! that renders is one where the device's check did not pass for it, `in_place.rs`; +//! `WorkCounts::feedback_loop_shadow_images`) or a linear image aliasing the bytes +//! (`WorkCounts::feedback_loop_alias_images`), with the storage usage the snapshot's fetch reads +//! through, which one over host pages takes where the check passed for a shader's writes there +//! (`Pointer writes: Passed`), 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 @@ -180,10 +183,14 @@ fn readers( /// How [`readers_over`]' target was made: whether the image its passes render took feedback loop /// usage (`WorkCounts::feedback_loop_alias_images` for an image aliasing its bytes, or -/// `feedback_loop_shadow_images` for a shadow). +/// `feedback_loop_shadow_images` for a shadow), and whether a snapshot's fetch reads it there: a +/// shadow's, or an image over host pages in place where the device's check passed for a shader's +/// writes over such pages (`Pointer writes: Passed`), with which it takes the storage usage the +/// fetch reads through (`texture/in_place_image.rs`). #[derive(Debug)] struct Made { feedback: bool, + fetched: bool, } /// [`readers`]' pass over a new target of `format`: private, or where `surface` a surface over @@ -227,9 +234,15 @@ fn readers_over( } }; let counts = device.work_counts(); + let alias = counts.feedback_loop_alias_images > made.feedback_loop_alias_images; + let shadow = counts.feedback_loop_shadow_images > made.feedback_loop_shadow_images; + // An image in place over host pages takes the storage usage the snapshot's fetch reads + // through where the device's check passed for a shader's writes over such pages. + let writes = + (device.in_place_check_report().iter()).any(|line| line == "Pointer writes: Passed"); let made = Made { - feedback: counts.feedback_loop_alias_images > made.feedback_loop_alias_images - || counts.feedback_loop_shadow_images > made.feedback_loop_shadow_images, + feedback: alias || shadow, + fetched: shadow || (alias && (!surface || writes)), }; let back = device .new_buffer(SIDE * row, ResourceOptions::shared()) @@ -538,7 +551,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.feedback { + let expected = if takes && made.feedback && made.fetched { (2, 0) } else { (0, 2) diff --git a/docs/API.md b/docs/API.md index f56e26867..5572bf226 100644 --- a/docs/API.md +++ b/docs/API.md @@ -331,8 +331,11 @@ from `VK_QUEUE_FAMILY_FOREIGN_EXT`) wherever the device makes such an image and passed for that kind of pages and each of its accesses (`WorkCounts::host_textures_in_place`, `dma_buf_textures_in_place`, `fresh_page_textures_in_place`, and `host_textures_in_place_written` for one a shader writes), on a discrete GPU as on any other. A texture that renders takes the -feedback loop usage where the device makes the image with it, so a draw that reads it after a -render-target barrier reads it through a snapshot (`WorkCounts::feedback_loop_alias_images`). The +feedback loop usage where the device makes the image with it, and the storage usage too where the +check passed for a shader's writes over that kind of pages and its texels are of 4, 8 or 16 bytes +(with it a shader writes the texels in place, as one declared to), so a draw that reads it after +a render-target barrier reads it through a snapshot (`WorkCounts::feedback_loop_alias_images`); +without the storage usage such a draw splits its pass. The check makes such images of every texel size, small and 3840x2160, over each kind of pages while a CPU thread writes beside their texels, and passes a kind only where every first move kept every byte; for a texture that renders it checks images made with `RenderTarget` usage apart, through diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 9149c01cf..8692513f3 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -204,11 +204,13 @@ recorded here. 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 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`, + buffer's bytes (a linear image over an ordinary buffer's packed rows or over host pages, or the + shadow a texture over host pages that renders is where it is one) 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), one over host pages with the + storage usage too, which it takes where the device's check passed for a shader's writes there + (docs/API.md): 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.