From f3f6696eb90410d9b5d3be1dcee3f1bbbae72675 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 08:39:12 -0800 Subject: [PATCH] An unstored DontCare load in a pass that draws clears, as Metal's draws read it Metal (an M3 Max, macOS 27.0.1, lane dont_care_unstored): an attachment a pass loads `DontCare` and does not store reads zero (a color) or the pass's clear value (a stencil) wherever the pass's draws read it, never its earlier texels. The runtime left such an attachment `DONT_CARE` (clear_reach::dont_care_loads skipped every attachment the pass does not store): the first instance's DONT_CARE load read 0 for the stencil (MoltenVK names no clear value there), later instances of a split pass LOAD what the first left, so the tiles it did not reach read the earlier texels, and a color the only instance leaves out (fetched through the pixel interlock) read its earlier texels everywhere. Now such an attachment (not memoryless, not separate depth and stencil textures) takes `Clear` in a pass whose draws reach a tile: zero for a color (dont_care_clear), the attachment's clear value for depth and stencil, by the first instance's load operation over the render area, and a color that instance leaves out is cleared before the pass by the existing path for cleared storage colors (cleared_ahead, or the reach pass over the render area). It is not tile-bounded and not announced: no fragment reads a tile no draw touches, so the answer is Metal's wherever it is read. `dont-care-load-tiles`' text, rustdoc and catalogue entry are scoped to stored attachments; clear_reach.rs records Metal's measured answer and the rule. --- .../metal2vulkan-runtime/src/clear_reach.rs | 37 +++++++++++++++++-- .../metal2vulkan-runtime/src/degradation.rs | 4 +- crates/metal2vulkan-runtime/src/render.rs | 6 ++- docs/API.md | 6 ++- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/clear_reach.rs b/crates/metal2vulkan-runtime/src/clear_reach.rs index 6800f21f2..5552feff0 100644 --- a/crates/metal2vulkan-runtime/src/clear_reach.rs +++ b/crates/metal2vulkan-runtime/src/clear_reach.rs @@ -24,6 +24,12 @@ //! tile holds zero before the draws (a color, every color attachment of the pass) or the //! attachment's clear value (depth, stencil); the tiles no primitive touches keep their texels. //! Which tiles those are depends on where the GPU places the vertices. +//! - **A `DontCare` load** of an attachment the pass does not store (`DontCare`) reads the same +//! wherever a fragment reads it: zero (a color) or the pass's clear value (a stencil), never its +//! earlier texels (an M3 Max, macOS 27.0.1, lane `dont_care_unstored`: an `RG16Float` second +//! color a framebuffer fetch reads and a `Stencil8` a stencil test reads, over 224x128 and +//! 128x128 targets, in a pass whose first draw reaches some tiles and a later one every tile, a +//! pass of the reading draw alone, and a pass of no fetch). //! - A `Load` keeps the texels past the render area, and a `DontCare` store stores nothing. //! //! **The runtime** gives the `Clear` load's answer and the `DontCare` load's near-equivalent, as @@ -44,6 +50,13 @@ //! pass's `tileWidth` and `tileHeight`, which set Metal's grid, as measured), a clear before the //! pass, and loads: every other tile keeps its texels, Metal's answer (a draw writes no pixel //! outside them), and a pass whose draws reach no pixel loads, as one whose draws draw nothing. +//! - A `DontCare`-loaded attachment the pass does not store (but separate depth and stencil +//! textures, and memoryless ones) clears to zero (a color) or its clear value (depth, stencil) +//! in a pass whose draws reach a tile, over the render area: by the load operation of the pass's +//! first instance, or before the pass where that instance leaves it out (as below). Metal's +//! answer wherever a fragment reads it, so no notice: no fragment reads a tile no draw touches. +//! What the pass leaves in it is a `DontCare` store's (`kept_bytes.rs`). In a pass whose draws +//! reach no tile it stays `DontCare`. //! - A `DontCare`-loaded memoryless color attachment clears to zero in a pass whose draws reach a //! tile: Metal's answer in every tile a fragment reads it (an Apple M4), and it is //! never stored, so the answer is exact. @@ -263,8 +276,11 @@ pub(super) fn dont_care_clear(pf: PixelFormat) -> vk::ClearColorValue { /// Give each `DontCare` load of `layout` that the module docs name its answer in a pass of /// `reach`: `Load` where it reaches no tile, else `Clear`, to the format's zero for a color -/// attachment ([`dont_care_clear`]) and to its clear value for depth and stencil. Which attachments took it, and whether the pass took -/// the near-equivalent (its draws reach the tiles their primitives touch): the attachment it +/// attachment ([`dont_care_clear`]) and to its clear value for depth and stencil. One of an +/// attachment the pass does not store takes `Clear` wherever the pass reaches a tile, over its +/// whole render area, by the load operation (the module docs): no fragment reads past the tiles +/// its draws touch, and no texel is kept. Which stored attachments took it, and whether the pass +/// took the near-equivalent (its draws reach the tiles their primitives touch): the attachment it /// names, for the notice. pub(super) fn dont_care_loads(layout: &mut PassLayout, reach: PassReach) -> DontCareLoads { let load = match reach { @@ -293,7 +309,14 @@ pub(super) fn dont_care_loads(layout: &mut PassLayout, reach: PassReach) -> Dont } continue; } - if color.load != LoadAction::DontCare || color.memoryless || !stores(color.store) { + if color.load != LoadAction::DontCare || color.memoryless { + continue; + } + if !stores(color.store) { + if reach != PassReach::Nothing { + color.load = LoadAction::Clear; + color.clear = dont_care_clear(color.target.texture.pixel_format()); + } continue; } color.load = load; @@ -317,7 +340,13 @@ pub(super) fn dont_care_loads(layout: &mut PassLayout, reach: PassReach) -> Dont let Some(aspect) = aspect else { continue; }; - if aspect.load != LoadAction::DontCare || aspect.memoryless || !stores(aspect.store) { + if aspect.load != LoadAction::DontCare || aspect.memoryless { + continue; + } + if !stores(aspect.store) { + if reach != PassReach::Nothing { + aspect.load = LoadAction::Clear; + } continue; } aspect.load = load; diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index f6693f9b5..86c464942 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -835,7 +835,9 @@ degradations! { /// An attachment loaded with `DontCare` and stored, in a pass that draws, is cleared /// before the draws to zero (a color) or its clear value (depth, stencil) over the render /// area and the tiles past it a clear reaches (`clear_reach.rs`), where Metal clears only - /// the tiles a draw's primitives touch and keeps the texels of the others. + /// the tiles a draw's primitives touch and keeps the texels of the others. Not one the + /// pass does not store, which its load operation clears over the render area: Metal's + /// answer wherever a fragment reads it. DontCareLoadTiles = "dont-care-load-tiles", Precision, "the tiles no draw touches hold zero or the clear value where Metal keeps their \ texels"; diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 43fb09fdd..ed18c7c7c 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -4746,7 +4746,9 @@ impl RenderLowering { }; // A `DontCare` load of an attachment the pass stores: loaded in a pass whose draws reach // no tile, else cleared to zero or its clear value, Metal's answer in each tile a draw - // reaches (`clear_reach.rs`), and only in those `tiles` bounds. + // reaches (`clear_reach.rs`), and only in those `tiles` bounds. One the pass does not + // store is cleared over the render area where the draws reach a tile, Metal's answer + // wherever a fragment reads it. let dont_care = clear_reach::dont_care_loads(&mut layout, reach); // The tiles the draws reach, which only those loads' clears read: none made for a pass // without one. @@ -4757,7 +4759,7 @@ impl RenderLowering { if let Some(named) = dont_care.named.clone() { device.degrade(Degradation::DontCareLoadTiles, || { ( - format!("{named} of a render pass, loaded with `DontCare`"), + format!("{named} of a render pass, loaded with `DontCare` and stored"), "Metal clears only the tiles a draw's primitives touch, which depend on where \ the GPU places the vertices" .to_owned(), diff --git a/docs/API.md b/docs/API.md index 6134d4b8c..02e898db6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2343,7 +2343,11 @@ bound. with `DontCare` and stored, in a pass that draws, is cleared before the draws to zero (a color) or its clear value (depth, stencil) over the render area and the tiles past it a clear reaches, where Metal clears only the tiles a draw's primitives touch and keeps the texels of the others. Cost: - the tiles no draw touches hold zero or the clear value where Metal keeps their texels. + the tiles no draw touches hold zero or the clear value where Metal keeps their texels. An + attachment loaded with `DontCare` that the pass does not store is not announced: in a pass that + draws, the runtime clears it over the render area to zero or its clear value, which is what + Metal's draws read of it wherever they read it (an M3 Max, macOS 27.0.1: a framebuffer fetch of + a second color and a stencil test, in passes split and whole). - `metal2vulkan-degrade-fresh-pattern-zeroed` (precision). `FreshPatternZeroed`: On a device that presents an Apple silicon Mac's compression (`DeviceProperties::lossless_compression`, `MacClass::AppleSilicon`; never on one that presents a Mac with an Intel or AMD GPU, whose new