From e08ad161e650e7c802a39c2e21dc79e8cd2e0757 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 06:17:54 -0800 Subject: [PATCH] WIP: a DontCare color of a texture's own image keeps its texels through a pooled backing (cherry picked from commit 568dd0d59b0ae6b30a6308a2f7587cfe7c4a41dd) --- .../examples/host_ab/runtime.rs | 1 + .../src/attachment_stand_in.rs | 55 ++++-- .../metal2vulkan-runtime/src/degradation.rs | 15 +- crates/metal2vulkan-runtime/src/kept_bytes.rs | 90 +++++++-- crates/metal2vulkan-runtime/src/queue.rs | 9 +- crates/metal2vulkan-runtime/src/render.rs | 3 +- .../metal2vulkan-runtime/src/render_target.rs | 14 +- .../src/tests/deferred_calls.rs | 10 +- .../metal2vulkan-runtime/src/work_counts.rs | 12 ++ .../src/runtime_lane/dont_care_stores.rs | 178 +++++++++++------- docs/API.md | 24 ++- 11 files changed, 283 insertions(+), 128 deletions(-) diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index 8d3f5e272..5217c4842 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -1767,6 +1767,7 @@ impl Gpu for Runtime { c.linear_passes_through_copies, ), ("kept_bytes_passes", c.kept_bytes_passes), + ("kept_texels_passes", c.kept_texels_passes), ("color_group_passes", c.color_group_passes), ("color_group_twins", c.color_group_twins), ("color_group_draws_skipped", c.color_group_draws_skipped), diff --git a/crates/metal2vulkan-runtime/src/attachment_stand_in.rs b/crates/metal2vulkan-runtime/src/attachment_stand_in.rs index a726bf18e..fdfa26c6c 100644 --- a/crates/metal2vulkan-runtime/src/attachment_stand_in.rs +++ b/crates/metal2vulkan-runtime/src/attachment_stand_in.rs @@ -155,6 +155,12 @@ pub(super) enum Cause { /// copied into where the pass loads it and never back, so the texture keeps the samples it /// held before the pass, as Metal's does. KeptSamples, + /// The image a pass renders a texture's own image through where it stores it `DontCare` and + /// the device's own `DONT_CARE` would not leave the texture's earlier texels, which Metal + /// leaves (`kept_bytes.rs`): one its queue keeps for its passes of the attachment's shape + /// ([`pooled_backing`]), copied into where the pass loads it and never back. No notice names + /// it: the texture keeps its texels, Metal's answer. + KeptTexels, } impl StandIn { @@ -245,7 +251,7 @@ pub(super) fn kept_backing( Ok(made) } -/// The most memoryless color backings a queue keeps ([`pooled_backing`]), and the most bytes of +/// The most color backings a queue keeps ([`pooled_backing`]), and the most bytes of /// device memory their images take together: past either, the backing passes took least recently /// goes (but the one just made, which alone may take more). The count is above the shapes a /// frame cycles through: a compositor's frames on the consumer's macOS guest render 11 to 14 @@ -255,24 +261,27 @@ pub(super) fn kept_backing( const MOST_POOLED_BACKINGS: usize = 32; const MOST_POOLED_BYTES: u64 = 256 << 20; -/// A memoryless color attachment's backing its queue keeps ([`pooled_backing`]): the shape it -/// backs (the attachment's pixel format, the format its pass renders it in, its samples, layers -/// and extent), the bytes its image takes, and the pool's count of takes at its last take. +/// A color attachment's backing its queue keeps ([`pooled_backing`]): the shape it backs (the +/// attachment's pixel format, the format its pass renders it in, its samples, layers and extent), +/// whether it was made for copies from its attachment (`stand_in`'s `copies`), the bytes its image +/// takes, and the pool's count of takes at its last take. struct PooledBacking { pixel_format: PixelFormat, format: metal2vulkan::vk::Format, samples: u32, layers: u32, extent: [u32; 2], + copies: bool, bytes: u64, taken: u64, backing: Target, } impl PooledBacking { - /// Whether it backs an attachment of `own`'s shape. - fn backs(&self, own: &Target) -> bool { - self.pixel_format == own.texture.pixel_format() + /// Whether it backs an attachment of `own`'s shape, copied from it where `copies`. + fn backs(&self, own: &Target, copies: bool) -> bool { + (self.copies || !copies) + && self.pixel_format == own.texture.pixel_format() && self.format == own.format && self.samples == own.samples && self.layers == own.layers @@ -280,7 +289,7 @@ impl PooledBacking { } } -/// The memoryless color backings one command queue keeps for its passes ([`pooled_backing`]): at +/// The color backings one command queue keeps for its passes ([`pooled_backing`]): at /// most [`MOST_POOLED_BACKINGS`], over at most [`MOST_POOLED_BYTES`] but for the one just made. /// They go with the queue; each command buffer that rendered into one holds it until it /// completes. @@ -314,23 +323,28 @@ impl BackingPool { /// The backing of `own`, a memoryless color attachment of a pass of `maker`'s queue that renders /// into an image of its own (a framebuffer fetch through the pixel interlock reads it, or the pass -/// splits): one the queue keeps of `own`'s pixel format, format, samples, layers and extent that -/// is none of `taken` (the images the pass renders its other attachments into), else one made now -/// and kept ([`BackingPool`]). Metal neither loads nor stores a memoryless texture -/// (`render_pass::check_memoryless`), and a pass gives a memoryless color's every texel a draw can -/// read a value before its draws: its `Clear` load, or the zero its `DontCare` load clears to in a -/// pass that draws (`clear_reach::dont_care_loads`), before the pass where the pass leaves it out -/// of its subpass (`render.rs`). A fresh image's texels were undefined there as well. So no pass -/// reads what another left in the backing, and the passes of one queue that take it are ordered by -/// their uses of it, as by any texture's: one image serves every pass of the shape, across -/// command buffers and frames (`WorkCounts::pooled_backings_made`). +/// splits), or a color a pass stores `DontCare` that renders through one so that its texture +/// keeps its texels (`kept_bytes.rs`), made for the copy into it where `copies` (the pass loads +/// it): one the queue keeps of `own`'s pixel format, format, samples, layers and extent (made for +/// copies where `copies`) that is none of `taken` (the images the pass renders its other +/// attachments into), else one made now and kept ([`BackingPool`]). Metal neither loads nor stores +/// a memoryless texture (`render_pass::check_memoryless`), and a pass gives a memoryless color's +/// every texel a draw can read a value before its draws: its `Clear` load, or the zero its +/// `DontCare` load clears to in a pass that draws (`clear_reach::dont_care_loads`), before the pass +/// where the pass leaves it out of its subpass (`render.rs`); a `DontCare` store's backing is +/// given the same before its pass, or the texture's texels over the render area where the pass +/// loads it, and nothing reads what its pass leaves in it. A fresh image's texels were undefined +/// there as well. So no pass reads what another left in the backing, and the passes of one queue +/// that take it are ordered by their uses of it, as by any texture's: one image serves every pass +/// of the shape, across command buffers and frames (`WorkCounts::pooled_backings_made`). pub(super) fn pooled_backing( maker: &CommandBuffer, own: &Target, taken: &[Texture], + copies: bool, ) -> Result { let queue = maker.command_queue(); - let free = |k: &PooledBacking| k.backs(own) && !taken.contains(&k.backing.texture); + let free = |k: &PooledBacking| k.backs(own, copies) && !taken.contains(&k.backing.texture); { let mut pool = lock(queue.memoryless_backings()); pool.takes += 1; @@ -346,7 +360,7 @@ pub(super) fn pooled_backing( } } // Made with no lock held; the pool may keep one another thread made meanwhile beside it. - let made = stand_in(maker, own, false)?; + let made = stand_in(maker, own, copies)?; maker.device().shared().counts.pooled_backing_made(); let mut pool = lock(queue.memoryless_backings()); pool.takes += 1; @@ -357,6 +371,7 @@ pub(super) fn pooled_backing( samples: own.samples, layers: own.layers, extent: own.extent, + copies, bytes: made.texture.placed_bytes().unwrap_or(0), taken: takes, backing: made.clone(), diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index ac14c5f22..2103b79aa 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -803,7 +803,9 @@ degradations! { /// device stores it (`STORE`), Metal's answer where its tiler overflows /// (`render_target.rs`). Not a texture over a buffer the pass renders through an image it /// copies nothing back from (a shadow, a stand-in: `kept_bytes.rs`), which keeps the - /// buffer's bytes, Metal's answer; only one whose image the pass renders in place. + /// buffer's bytes, Metal's answer, nor a texture's own image stored `DontCare` that the + /// pass renders through a backing its queue keeps (`kept_bytes.rs`), which keeps its + /// earlier texels, Metal's answer; only one whose image the pass renders in place. DontCareStore = "dont-care-store", Precision, "the attachment's texels after the pass are each its earlier texel or the one the \ pass rendered, both Metal's answers, but not always the one Metal's tiler keeps"; @@ -4453,11 +4455,13 @@ impl Device { // call renders into a backing of its own, which copies nothing back: its texture keeps // its texels, as Metal's keeps them there (`render.rs`). So does one whose samples a // resolve after the pass reads, rendered through the image its texture keeps, from which - // the pass copies nothing back either (`resolve::keep_samples`). + // the pass copies nothing back either (`resolve::keep_samples`), and a texture's own image + // stored `DontCare` through a backing its queue keeps (`kept_bytes.rs`). let kept = |color: &ColorTarget| { (color.clears_and_resolves_across_formats() && color.stand_in.as_ref().is_some_and(|s| !s.copies())) - || (color.stand_in.as_ref()).is_some_and(|s| s.cause == Cause::KeptSamples) + || (color.stand_in.as_ref()) + .is_some_and(|s| matches!(s.cause, Cause::KeptSamples | Cause::KeptTexels)) }; // A texture over a buffer rendered through an image the pass copies nothing back from // where it does not store it (a shadow, `render/shadows.rs`; a stand-in, which copies @@ -4957,11 +4961,12 @@ impl Device { }); } else if matches!( stand_in.cause, - Cause::Linear | Cause::KeptBytes | Cause::KeptSamples + Cause::Linear | Cause::KeptBytes | Cause::KeptSamples | Cause::KeptTexels ) { // Announced where `linear_attachment.rs` or `kept_bytes.rs` chose the kept image. // An image that keeps an attachment's samples for a resolve after the pass makes - // the pass Metal's, which no notice names (`resolve::keep_samples`). + // the pass Metal's, which no notice names (`resolve::keep_samples`), as does a + // backing that keeps a texture's texels (`kept_bytes.rs`). } else if memoryless && (index.is_some_and(fetched) || (index.is_none() && shadow) || !chosen) { diff --git a/crates/metal2vulkan-runtime/src/kept_bytes.rs b/crates/metal2vulkan-runtime/src/kept_bytes.rs index f75dca739..dc32be4b6 100644 --- a/crates/metal2vulkan-runtime/src/kept_bytes.rs +++ b/crates/metal2vulkan-runtime/src/kept_bytes.rs @@ -1,4 +1,5 @@ -//! Textures over buffers that a render pass does not store: the buffer keeps its bytes. +//! Color attachments a render pass does not store: a texture over a buffer keeps the buffer's +//! bytes, and a texture's own image (`DontCare`) its earlier texels. //! //! **Metal's answer** (the DontCare-store probe, an M3 Max under macOS 27.0.1, each case 20 times //! alike): after a pass whose color attachment over a buffer is stored `DontCare`, the buffer @@ -59,6 +60,31 @@ //! would then have to render apart, and where its image or its format copies no texels and the //! pass loads it: there the pass renders in place, and `dont-care-store` announces the texels it //! rendered there, Metal's answer where its tiler overflows. +//! +//! **A texture's own image** keeps its earlier texels too. Metal's documentation leaves them open +//! (`MTLStoreAction.dontCare`: "You need to assume that GPU discarded the texture's contents"), and +//! what Apple's GPU leaves is measured: the attachment's texels from before the pass, the pass's +//! clear and its draws both gone (the conformance case `pass-sequence-color-actions`, an M3 Max +//! under macOS 27.0.1, eight runs alike, whose two passes over one texture the host wrote first +//! read the host's texels back after a `DontCare` store under either load action of the first and +//! either load action and render area of the second; and `dont_care_stores`' passes on MoltenVK, +//! which hands the store to Metal). Vulkan's `DONT_CARE` leaves the texels undefined, and a driver +//! may leave neither answer: NVIDIA's left a pass's clear under its draw. So a pass that stores a +//! color attachment `DontCare` renders it, where the texture's image is its own (not a buffer's +//! bytes, nor a shadow of them), through a backing its queue keeps for its passes of the +//! attachment's shape (`attachment_stand_in::pooled_backing`, [`Cause::KeptTexels`]): a `Load` +//! copies the texture's texels over the render area into it before the pass (what the pass's +//! blending and tile dispatches read there), a `Clear` or a `DontCare` load is its own first +//! operation, as above, and nothing is copied back. Its store +//! is `DONT_CARE` (`render_target.rs`). Not where the pass hands the attachment to Metal's own +//! `DontCare` (`HandedToMetal`, at no cost), nor in the cases above that render in place, nor for a +//! memoryless texture (none of its texels outlive a pass in Metal), nor one the pass resolves +//! (`MultisampleResolve`, which `resolve::keep_samples` keeps where its resolve reads the samples), +//! nor where a format of the device's makes no stand-in image or, loaded, copies none: those +//! render in place, and `dont-care-store` announces them. The texture keeps its texels, Metal's +//! answer, so nothing is announced; each such pass is counted (`WorkCounts::kept_texels_passes`). +//! The cost is the backing's memory, one image of the attachment's shape per queue (bounded with +//! the queue's memoryless backings, `attachment_stand_in::BackingPool`), and a `Load`'s copy. use super::attachment_stand_in::{self, Cause, StandIn}; use super::command_buffer::CommandBuffer; @@ -77,13 +103,25 @@ fn unstored(color: &ColorTarget) -> bool { ) } +/// Whether `color`, a texture's own image (neither over a buffer's bytes nor a shadow of them), is +/// one a pass renders through a backing so that it keeps its texels (the module docs): stored +/// `DontCare`, resolving nothing, and not memoryless. +fn keeps_texels(color: &ColorTarget) -> bool { + color.store == StoreAction::DontCare + && !color.memoryless + && color.resolve.is_none() + && color.copied_resolve.is_none() + && !color.target.texture.over_buffer() +} + /// Render each color attachment among `colors`, a pass's as it ends, that the pass does not /// store and whose texture's image lies over its buffer's bytes, through the image its texture -/// keeps for `maker`'s queue (the module docs): but where the pass hands it to the driver's -/// `DONT_CARE` as Metal's own `DontCare` (`handed`, `HandedToMetal`), a draw took a view of its -/// image (`viewed`), the pass executes render commands (`executes`), or another color in place -/// is bound by `VK_NV_linear_color_attachment`'s rule and the pass cannot render it apart -/// (`taken`, as `linear_attachment::settle` asks it). Announced, and counted. +/// keeps for `maker`'s queue, and each the pass stores `DontCare` whose texture's image is its +/// own, through a backing the queue keeps (the module docs): but where the pass hands it to the +/// driver's `DONT_CARE` as Metal's own `DontCare` (`handed`, `HandedToMetal`), a draw took a view +/// of its image (`viewed`), the pass executes render commands (`executes`), or another color in +/// place is bound by `VK_NV_linear_color_attachment`'s rule and the pass cannot render it apart +/// (`taken`, as `linear_attachment::settle` asks it). The first announced; both counted. pub(super) fn settle( maker: &CommandBuffer, colors: &mut [Option], @@ -112,23 +150,46 @@ pub(super) fn settle( } } let queue = maker.command_queue().id(); - let mut kept = false; - for (index, color) in colors.iter_mut().enumerate() { - let Some(color) = color else { + let (mut kept, mut kept_texels) = (false, false); + for index in 0..colors.len() { + // The images the pass renders its other colors into, which no backing of this one is. + let taken_images: Vec<_> = (colors.iter().flatten()) + .filter(|color| color.stand_in.is_some()) + .map(|color| color.target.texture.clone()) + .collect(); + let Some(color) = colors[index].as_mut() else { continue; }; + let over_buffer = color.target.texture.image_over_buffer(); if !unstored(color) || color.stand_in.is_some() - || !color.target.texture.image_over_buffer() + || !(over_buffer || keeps_texels(color)) || handed(index) || viewed(index) || held.iter().any(|&other| other != index) { continue; } - if color.load == LoadAction::Load - && attachment_stand_in::check_copies(&color.target).is_err() - { + let loads = color.load == LoadAction::Load; + if loads && attachment_stand_in::check_copies(&color.target).is_err() { + continue; + } + if !over_buffer { + let own = color.target.clone(); + // A format the device makes no stand-in of keeps the pass in place. + let backing = + match attachment_stand_in::pooled_backing(maker, &own, &taken_images, loads) { + Ok(backing) => backing, + Err(Error::NotImplemented(RuntimeGap::Render)) => continue, + Err(error) => return Err(error), + }; + color.target = backing; + color.stand_in = Some(StandIn { + own, + cause: Cause::KeptTexels, + area: None, + }); + kept_texels = true; continue; } let own = color.target.clone(); @@ -164,5 +225,8 @@ pub(super) fn settle( if kept { device.shared().counts.kept_bytes_pass(); } + if kept_texels { + device.shared().counts.kept_texels_pass(); + } Ok(()) } diff --git a/crates/metal2vulkan-runtime/src/queue.rs b/crates/metal2vulkan-runtime/src/queue.rs index 554fdceb7..7df2be667 100644 --- a/crates/metal2vulkan-runtime/src/queue.rs +++ b/crates/metal2vulkan-runtime/src/queue.rs @@ -149,8 +149,9 @@ struct QueueShared { /// so far, which the completion turns place in its order (`gpu_time.rs`). A leaf lock: /// taken under a command buffer's, never held while another is taken. latest_gpu_end: Mutex, - /// The backings of memoryless attachments its passes render into images of their own - /// (`attachment_stand_in::pooled_backing`). A leaf lock: never held while another is taken. + /// The backings of memoryless attachments its passes render into images of their own, and of + /// colors they store `DontCare` that render so (`attachment_stand_in::pooled_backing`). A leaf + /// lock: never held while another is taken. memoryless_backings: Mutex, /// The kept images its merges' keeps made, kept for its later keeps /// (`texture/kept_image.rs`). @@ -623,8 +624,8 @@ impl CommandQueue { (self.vk_queue(), long) } - /// The backings of memoryless attachments the queue keeps for its passes - /// (`attachment_stand_in::pooled_backing`). + /// The backings of memoryless attachments, and of colors stored `DontCare`, the queue keeps + /// for its passes (`attachment_stand_in::pooled_backing`). pub(super) fn memoryless_backings(&self) -> &Mutex { &self.0.memoryless_backings } diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index ec9caa49f..58916ffff 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -6593,6 +6593,7 @@ impl RenderLowering { maker, own, &standing_images(&self.layout, &standing), + false, )?, true => attachment_stand_in::stand_in(maker, own, copies)?, }; @@ -6700,7 +6701,7 @@ impl RenderLowering { .any(|s| matches!(s, Standing::Color(i, ..) if *i == index)); if color.stand_in.is_none() && !stood && memoryless(own) { let taken = standing_images(&self.layout, &standing); - let target = attachment_stand_in::pooled_backing(maker, own, &taken)?; + let target = attachment_stand_in::pooled_backing(maker, own, &taken, false)?; let stand_in = StandIn { own: own.clone(), cause: Cause::Backing, diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs index 4980e7d06..798cdde49 100644 --- a/crates/metal2vulkan-runtime/src/render_target.rs +++ b/crates/metal2vulkan-runtime/src/render_target.rs @@ -105,7 +105,9 @@ //! hands it so ([`handed_dont_care`], `DriverFact::DontCareStoreIsMetals`). A texture over a //! buffer, which Metal leaves holding the buffer's bytes, renders through an image the pass //! copies nothing back from (its shadow, or the image it keeps: `kept_bytes.rs`): the buffer's -//! bytes, exactly. A resolve attachment is written whole over the render area (`DONT_CARE` then +//! bytes, exactly. So does a texture's own image a pass stores `DontCare` and does not hand to +//! Metal's own, through a backing its queue keeps (`kept_bytes.rs`): its earlier texels, +//! exactly. The operations below are those of an attachment such a pass renders in place. A resolve attachment is written whole over the render area (`DONT_CARE` then //! `STORE`), but an aspect of a depth/stencil resolve that does not resolve keeps its texels. //! An aspect of the attached texture's Metal format that no attachment names keeps its texels //! (loaded and stored); an aspect only the Vulkan format has (the depth of a `Stencil8` texture @@ -1263,10 +1265,10 @@ pub(super) fn final_pass( } /// The operations of color attachment `color` of the draws' subpass in an instance at `at`, its -/// `DontCare` `dont_care`'s, but the image a texture over a buffer keeps for -/// a pass that does not store it, which nothing reads after the pass (`kept_bytes.rs`). An -/// attachment the exact resolve reads after the pass keeps its samples for it, and one whose -/// texels are copied as its resolve its texels. +/// `DontCare` `dont_care`'s, but the image a texture over a buffer keeps for a pass that does not +/// store it, and the backing of a texture's own image a pass stores `DontCare`, which nothing +/// reads after the pass (`kept_bytes.rs`). An attachment the exact resolve reads after the pass +/// keeps its samples for it, and one whose texels are copied as its resolve its texels. fn color_ops(color: &ColorTarget, at: Position, dont_care: DontCare) -> Ops { let exact = color.resolve.as_ref().is_some_and(|r| r.shader.is_some()) || color.copied_resolve.is_some(); @@ -1274,7 +1276,7 @@ fn color_ops(color: &ColorTarget, at: Position, dont_care: DontCare) -> Ops { || color .stand_in .as_ref() - .is_some_and(|s| s.cause == Cause::KeptBytes); + .is_some_and(|s| matches!(s.cause, Cause::KeptBytes | Cause::KeptTexels)); Ops { load: load_at(color.load, at), store: match exact { diff --git a/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs b/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs index e70d81cd5..884223a6d 100644 --- a/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs +++ b/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs @@ -808,8 +808,11 @@ fn an_end_returns_before_its_lowering() { /// A kind the device refuses while lowering, met as the end is lowered after its call, refuses /// the end: `CallRefused` names `endEncoding` and its index, the notice its error, and the pass -/// runs nothing. Where the device announces no such kind at the pass's end under `AtCall` (its -/// `DONT_CARE` keeps one of Metal's answers), there is nothing to refuse. +/// runs nothing. The kind is `dont-care-store`, met where a pass resolves its single-sampled +/// attachment and stores nothing of it (`MultisampleResolve`), which renders in place (a +/// `DontCare` store alone renders through a backing that keeps the texture's texels, +/// `kept_bytes.rs`, and announces nothing). Where the device announces no such kind at the +/// pass's end under `AtCall` (it hands the store to Metal's own), there is nothing to refuse. #[test] fn a_kind_refused_as_the_end_is_lowered_refuses_the_end() { let scratch = Scratch::new("deferred-end-refused"); @@ -822,7 +825,8 @@ fn a_kind_refused_as_the_end_is_lowered_refuses_the_end() { let command_buffer = queue.command_buffer().unwrap(); let target = target(&device); let mut descriptor = pass(&target); - descriptor.color_attachments[0].store_action = StoreAction::DontCare; + descriptor.color_attachments[0].store_action = StoreAction::MultisampleResolve; + descriptor.color_attachments[0].resolve_texture = Some(self::target(&device)); let mut encoder = command_buffer.render_command_encoder(&descriptor).unwrap(); rectangle(&state, &mut encoder); encoder diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 2c3faeb02..4b55a73ce 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -289,6 +289,7 @@ pub(crate) struct Counters { linear_merges_recorded: AtomicU64, linear_merges_taken_over: AtomicU64, kept_bytes_passes: AtomicU64, + kept_texels_passes: AtomicU64, color_group_passes: AtomicU64, color_group_twins: AtomicU64, color_group_draws_skipped: AtomicU64, @@ -2497,6 +2498,12 @@ impl Counters { self.kept_bytes_passes.fetch_add(1, Ordering::Relaxed); } + /// A pass rendered a texture's own image it stores `DontCare` through a backing its queue + /// keeps (`kept_bytes.rs`). + pub(crate) fn kept_texels_pass(&self) { + self.kept_texels_passes.fetch_add(1, Ordering::Relaxed); + } + /// A render encoder drew one of its pass's later color groups (`render/color_groups.rs`). pub(crate) fn color_group_pass(&self) { self.color_group_passes.fetch_add(1, Ordering::Relaxed); @@ -5280,6 +5287,10 @@ pub struct WorkCounts { /// (`DontCare`, `MultisampleResolve`), through the image its texture keeps, so that the buffer /// keeps its bytes (`kept_bytes.rs`). pub kept_bytes_passes: u64, + /// Passes that rendered a color attachment of a texture's own image, which they store + /// `DontCare`, through a backing their queue keeps, so that the texture keeps its earlier + /// texels (`kept_bytes.rs`). + pub kept_texels_passes: u64, /// Passes a render encoder drew past its first for a pass of more color attachments than the /// device draws at once, one per later color group, each of that group's colors over a twin /// of the depth and stencil (`render/color_groups.rs`). @@ -6880,6 +6891,7 @@ impl Device { linear_merges_recorded: counters.linear_merges_recorded.load(Ordering::Relaxed), linear_merges_taken_over: counters.linear_merges_taken_over.load(Ordering::Relaxed), kept_bytes_passes: counters.kept_bytes_passes.load(Ordering::Relaxed), + kept_texels_passes: counters.kept_texels_passes.load(Ordering::Relaxed), color_group_passes: counters.color_group_passes.load(Ordering::Relaxed), color_group_twins: counters.color_group_twins.load(Ordering::Relaxed), color_group_draws_skipped: counters.color_group_draws_skipped.load(Ordering::Relaxed), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/dont_care_stores.rs b/crates/metal2vulkan-validation/src/runtime_lane/dont_care_stores.rs index a34cff77c..ee733e976 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/dont_care_stores.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/dont_care_stores.rs @@ -5,9 +5,19 @@ //! Metal keeps such an attachment's earlier texels on Apple's GPUs unless the tiler overflows //! (an M4 with macOS 27.0.1), but a pass that clears it, draws nothing, and resolves it alone, //! which leaves the clear where Metal compresses the texture. Vulkan leaves the texels undefined. -//! On a driver with the fact (MoltenVK 1.4.1 on the Apple M4), which hands the store to Metal -//! unchanged, a pass recorded as one render pass instance keeps the texels and announces no -//! `dont-care-store`; every other driver announces it once, and each of its +//! +//! A color attachment of a texture's own image stored `DontCare` keeps its earlier texels +//! exactly on every driver, and announces no `dont-care-store`: on a driver with the fact +//! (MoltenVK on the Apple M4 and M3 Max), which hands the store to Metal unchanged, in a pass +//! recorded as one render pass instance; everywhere else, and in a pass the driver does not run +//! unbroken (a barrier over the render targets between two draws, inside the instance, where +//! MoltenVK restarts its Metal pass; one over buffers, which splits the pass into two +//! instances), through a backing its queue keeps (`kept_bytes.rs`), each such pass counted +//! (`WorkCounts::kept_texels_passes`). +//! +//! A `MultisampleResolve` store and a depth or stencil attachment stored `DontCare` render in +//! place: on a driver with the fact, a pass recorded as one render pass instance keeps the texels +//! and announces no `dont-care-store`; every other driver announces it once, and each of its //! texels must be one of Metal's two answers, the earlier texel or the one the same pass stores //! with `Store` (Metal's under its tiler's overflow), as the notice's exactness (`OneOfMetals`) //! states; the counts are printed (`DONT-CARE-TEXELS`). Only a driver measured to keep one of the @@ -15,12 +25,10 @@ //! the attachment, and each of its texels must be one of the two too. //! //! The negative cases announce it on every driver, each on a device of its own (the notice is -//! once per device), their texels printed: a barrier over the render targets between two draws, -//! inside the instance, where MoltenVK restarts its Metal pass and stores the attachment; one -//! over buffers, which splits the pass into two instances, the first storing; a pass that clears -//! a 4x attachment, draws nothing and resolves it; and a pass at custom sample positions. A pass -//! whose render area (the descriptor's target size) is smaller than its attachment is a positive -//! case: the runtime's framebuffer is the render area, so MoltenVK renders its whole framebuffer. +//! once per device), their texels printed: a pass that clears a 4x attachment, draws nothing and +//! resolves it; and a pass at custom sample positions. A pass whose render area (the descriptor's +//! target size) is smaller than its attachment is a positive case: the runtime's framebuffer is +//! the render area, so MoltenVK renders its whole framebuffer. //! //! The draws are the owned fixture `rt6_restart`'s (`clear_reach`'s lane): a triangle over the //! whole target at depth 0.25, writing (1/255, 0, 0, 1), the depth always passing and written, @@ -235,19 +243,27 @@ fn announces(messages: &Messages, case: &str, announced: bool, first: &BTreeSet< ); } -/// Passes recorded as one render pass instance keep their attachments' earlier texels under -/// `DontCare` and `MultisampleResolve` on a driver that stores them as Metal's own (our probes' -/// answers), and announce no `dont-care-store` there: a color attachment cleared and drawn, loaded -/// and drawn, cleared and not drawn, and cleared and drawn over a render area smaller than it; a 4x -/// `RGBA8Unorm` and a 4x `BGRA8Unorm_sRGB` attachment cleared, drawn and resolved, or stored -/// `DontCare`; a `Depth32Float` and a `Depth32Float_Stencil8` attachment cleared and drawn; and a -/// 4x `Depth32Float` attachment cleared, drawn and resolved. Every other driver announces it once, -/// and prints whether it kept them. The resolve textures hold the draw everywhere. +/// Passes kept apart from one by the backing (`WorkCounts::kept_texels_passes`) since the device +/// was made. +fn kept_texels(lane: &Lane) -> u64 { + lane.device.work_counts().kept_texels_passes +} + +/// A color attachment of a texture's own image stored `DontCare` keeps its earlier texels +/// exactly on every driver (Metal's answer, `kept_bytes.rs`), and announces no +/// `dont-care-store`: cleared and drawn, loaded and drawn, cleared and not drawn, cleared and +/// drawn over a render area smaller than it, and cleared and drawn twice with a barrier over the +/// render targets or over buffers between the draws. Each pass renders through a backing its +/// queue keeps, counted, but where the driver stores `DONT_CARE` as Metal's own and the pass is +/// one render pass instance it runs unbroken (MoltenVK, but for the barriers), handed to Metal. +/// The controls stored `Store` keep the draw where it covers the texel. #[test] -fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { +fn a_dont_care_color_keeps_its_earlier_texels_on_every_driver() { let (messages, _cache, lane) = lane(); let pipeline = lane.pipeline(&[RGBA8], (None, None), 1); let old = OLD.repeat((SIDE * SIDE) as usize); + // Whether the device renders a pass of one unbroken instance through the backing. + let backed = u64::from(!metals(&lane)); for (case, load, draw, area) in [ ("color_clear_draw", LoadAction::Clear, Draw::Full, None), ("color_load_draw", LoadAction::Load, Draw::Full, None), @@ -270,9 +286,70 @@ fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { lane.read(&texture, (0, 0), 4, BlitOption::None) }; let stored = run(StoreAction::Store); + if draw == Draw::Full && area.is_none() { + assert_eq!( + stored, + DRAWN.repeat((SIDE * SIDE) as usize), + "{case}: the control" + ); + } + let before = kept_texels(&lane); let texels = run(StoreAction::DontCare); - kept(&lane, case, &texels, (&old, &stored), 4); + assert!(texels == old, "{case}: the earlier texels are not kept"); + assert_eq!( + kept_texels(&lane) - before, + backed, + "{case}: the backed passes" + ); + eprintln!( + "KEPT-TEXELS {case} on {}: kept, {} pass(es) through the backing", + lane.device.name(), + kept_texels(&lane) - before + ); } + for (case, scope) in [ + ("barrier_render_targets", BarrierScope::RENDER_TARGETS), + ("barrier_buffers", BarrierScope::BUFFERS), + ] { + let texture = lane.old_color(RGBA8, PLAIN, TARGET); + let descriptor = color_pass(&texture, LoadAction::Clear, StoreAction::DontCare, None); + let before = kept_texels(&lane); + encode(&lane, &descriptor, &pipeline, |encoder| { + triangle(encoder); + encoder.memory_barrier_with_scope( + scope, + RenderStages::FRAGMENT, + RenderStages::FRAGMENT, + ); + triangle(encoder); + }); + let texels = lane.read(&texture, (0, 0), 4, BlitOption::None); + assert!(texels == old, "{case}: the earlier texels are not kept"); + // No driver runs these passes unbroken: each renders through the backing. + assert_eq!(kept_texels(&lane) - before, 1, "{case}: the backed passes"); + eprintln!("KEPT-TEXELS {case} on {}: kept", lane.device.name()); + } + // A barrier over buffers ends the pass's render pass instance (`render-pass-split`), and + // `color_clear_draw_small_area` clears over a render area smaller than its attachment. + let mut first = made(&lane, &[(&[RGBA8], (None, None), 1)], &[], true); + first.insert(Degradation::RenderPassSplit); + first.extend(tie_cleared(&lane.device, false)); + announces(&messages, "the kept colors", false, &first); + quiet(lane, &messages); +} + +/// Passes recorded as one render pass instance keep their attachments' earlier texels under +/// `DontCare` and `MultisampleResolve` on a driver that stores them as Metal's own (our probes' +/// answers), and announce no `dont-care-store` there: a 4x `RGBA8Unorm` and a 4x +/// `BGRA8Unorm_sRGB` attachment cleared, drawn and resolved, or stored `DontCare`; a +/// `Depth32Float` and a `Depth32Float_Stencil8` attachment cleared and drawn; and a 4x +/// `Depth32Float` attachment cleared, drawn and resolved. Every other driver announces it once, +/// and prints whether it kept them, but for the 4x colors stored `DontCare`, which keep them on +/// every driver (rendered through a backing, `kept_bytes.rs`). The resolve textures hold the draw +/// everywhere. +#[test] +fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { + let (messages, _cache, lane) = lane(); for pf in [RGBA8, PixelFormat::BGRA8UnormSRGB] { let pipeline = lane.pipeline(&[pf], (None, None), 4); let reference = resolved(&lane, &earlier_msaa(&lane, pf), pf); @@ -294,7 +371,15 @@ fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { _ => StoreAction::Store, }); let (texels, resolve) = run(store); - kept(&lane, &case, &texels, (&reference, &stored), 4); + match store { + StoreAction::DontCare => { + assert!( + texels == reference, + "{case}: the earlier texels are not kept" + ); + } + _ => kept(&lane, &case, &texels, (&reference, &stored), 4), + } if let (Some(resolve), true) = (resolve, pf == RGBA8) { assert_eq!( lane.read(&resolve, (0, 0), 4, BlitOption::None), @@ -387,8 +472,7 @@ fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { } let depth = Some(depth); let combined = Some(combined); - let pipelines: [(&[PixelFormat], _, u64); 6] = [ - (&[RGBA8], (None, None), 1), + let pipelines: [(&[PixelFormat], _, u64); 5] = [ (&[RGBA8], (None, None), 4), (&[PixelFormat::BGRA8UnormSRGB], (None, None), 4), (&[], (depth, None), 1), @@ -401,58 +485,18 @@ fn dont_care_stores_keep_the_texels_where_the_driver_stores_them_as_metal_s() { &[RGBA8, PixelFormat::BGRA8UnormSRGB], true, ); - // `color_clear_draw_small_area` clears over a render area smaller than its attachment. - first.extend(tie_cleared(&lane.device, false)); + // The 4x colors' clears cover their attachments. + first.extend(tie_cleared(&lane.device, true)); announces(&messages, "the kept passes", !metals(&lane), &first); quiet(lane, &messages); } /// Passes a driver does not store as Metal's own announce `dont-care-store` on every driver, each -/// on a device of its own, their texels printed: a barrier over the render targets between two -/// draws (MoltenVK restarts its Metal pass there, storing the attachment); a barrier over -/// buffers, which splits the pass into two render pass instances, the first storing; a 4x -/// attachment cleared, not drawn and resolved (Metal leaves the clear where it compresses the -/// texture); and a 4x pass at custom sample positions, where the device takes them. +/// on a device of its own, their texels printed: a 4x attachment cleared, not drawn and resolved +/// (Metal leaves the clear where it compresses the texture); and a 4x pass at custom sample +/// positions, where the device takes them. #[test] fn dont_care_stores_a_driver_may_store_otherwise_are_announced() { - let old = OLD.repeat((SIDE * SIDE) as usize); - for (case, scope) in [ - ("barrier_render_targets", BarrierScope::RENDER_TARGETS), - ("barrier_buffers", BarrierScope::BUFFERS), - ] { - let (messages, _cache, lane) = lane(); - let pipeline = lane.pipeline(&[RGBA8], (None, None), 1); - let texture = lane.old_color(RGBA8, PLAIN, TARGET); - let descriptor = color_pass(&texture, LoadAction::Clear, StoreAction::DontCare, None); - encode(&lane, &descriptor, &pipeline, |encoder| { - triangle(encoder); - encoder.memory_barrier_with_scope( - scope, - RenderStages::FRAGMENT, - RenderStages::FRAGMENT, - ); - triangle(encoder); - }); - let texels = lane.read(&texture, (0, 0), 4, BlitOption::None); - eprintln!( - "{case} on {}: the earlier texels {}", - lane.device.name(), - if texels == old { - "are kept" - } else { - "are not kept" - } - ); - // A barrier over buffers ends the pass's render pass instance (`render-pass-split`). - let mut first = made(&lane, &[(&[RGBA8], (None, None), 1)], &[], true); - first.extend(forced(&[( - Degradation::RenderPassSplit, - scope == BarrierScope::BUFFERS, - )])); - first.extend(tie_cleared(&lane.device, true)); - announces(&messages, case, true, &first); - quiet(lane, &messages); - } { let case = "ms4_clear_nodraw_resolve"; let (messages, _cache, lane) = lane(); diff --git a/docs/API.md b/docs/API.md index 90d61dc24..c6f47780d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1941,9 +1941,15 @@ bound. overflow Apple's tiler leaves the rendered texels, which no call can predict or detect), and the runtime gives the buffer's bytes, exactly, through an image the pass copies nothing back from (`dont-care-stand-in` below, or the texture's shadow), or by handing the attachment to Metal's - own `DontCare` where the driver stores it so; only where the pass cannot (a draw took a view of - the texture's own image before its store action was known, the pass executes render commands, - or the device copies no image of the format) does the pass render in place, announced so. Cost: + own `DontCare` where the driver stores it so. Nor a texture's own image stored `DontCare`: + Metal leaves its earlier texels (an M3 Max, the pass's clear and draws both gone, under every + load action), and the runtime gives them, exactly, through a backing of the attachment's shape + its queue keeps, into which a `Load` copies the texels over the render area first and from + which nothing is copied back, or by handing the attachment to Metal's own `DontCare`; it is + counted (`WorkCounts::kept_texels_passes`) and not announced. Only where the pass cannot (a + draw took a view of the texture's own image before its store action was known, the pass + executes render commands, the attachment is resolved, or the device copies or makes no image + of the format) does the pass render in place, announced so. Cost: the attachment's texels after the pass are each its earlier texel or the one the pass rendered, both Metal's answers, but not always the one Metal's tiler keeps. Within Metal's contract: `MTLStoreAction.dontCare` lets the GPU leave arbitrary data ("You need to assume that GPU @@ -4087,12 +4093,12 @@ each item named states the rule in full. **Render passes** - **A texture over a buffer that a pass stores `DontCare` keeps the buffer's bytes**, in every - texel, with or without memory barriers between the pass's draws: Metal's answer, but that the - first pass in a process to overflow Apple's tiler (some 2 to 4 million triangle-tile entries) - leaves the rendered texels in every texel, which no call can predict or detect. The runtime does - not model the overflow, but where it hands the attachment to Metal's own `DontCare` (MoltenVK), - which decides it. A pass that renders such a texture in place otherwise announces - `dont-care-store`. + texel, with or without memory barriers between the pass's draws, **and a texture's own image + keeps its earlier texels**: Metal's answer, but that the first pass in a process to overflow + Apple's tiler (some 2 to 4 million triangle-tile entries) leaves the rendered texels in every + texel, which no call can predict or detect. The runtime does not model the overflow, but where + it hands the attachment to Metal's own `DontCare` (MoltenVK), which decides it. A pass that + renders such a texture in place otherwise announces `dont-care-store`. - **Two mesh draws of one pass with no barrier between them run in order.** Where a later draw's object or mesh function reads a buffer an earlier draw wrote, and no `memoryBarrier` stands