Files
metal2vulkan/crates/metal2vulkan-runtime/src/shadow.rs
T
Anees Iqbal a6de987639 Runtime: the narrowed attachment-read counter and its per-draw rectangle go; the narrower form is not taken
The UHD 630 numbers answer the question attachment_read_copy_bytes_after_barrier_narrowed was
added to size (9db78abe7d): a form whose memory barrier forgets only the rectangle bounding the
pass's draws' scissors would remove at least half the attachment-read copy bytes, or it is not a
lane. On Intel's Windows driver 101.2145 for the UHD Graphics 630 (Windows 11, the consumer's
x86-64 guest, no feedback loop), whole-boot counts at a test tree with the split:
- desktop boot (130 s, 15,678 copies, 72,587,189,668 bytes): after barrier 56,148,113,276,
  narrowed 25,369,581,904, so the form would remove 30,778,531,372 bytes, 42.4%;
- 40 s 1080p H.264 playback (25,551 copies, 138,558,378,988 bytes): after barrier
  75,057,769,016, narrowed 39,566,419,316, so it would remove 35,491,349,700 bytes, 25.6%.
Both under the planner's 50% bar, so the form is not built and the count that sized it is
deleted (rule 4), with what fed it: shadow::Held's dropped areas, rendered rectangle and
may-write flag, Held::drew and Held::narrowed, held_texels, Area::meet, the per-draw update in
draw() and the scissor_of helper it shared with drawn_through (inlined back as before). Held keeps
one flag for the moment counters. The other five split counters (own pixel, whole texture, first
in pass, after barrier, beyond held) stay: the consumer prints them.

The README's sentence on the counter becomes the measured shares (card, driver, OS; no job ids).
The signatures table loses the field. The device test and the host test drop the narrowed column;
the host test of the narrowed texels goes with the code it tested.

Verified: pending (clippy x3, rustdoc, lavapipe rt suite).
2026-12-04 18:12:41 -08:00

317 lines
13 KiB
Rust

//! The copy form of attachment reads: what a draw that reads, through a texture, an attachment its
//! own pass renders copies first into the image's shadow, on a device that reads attachments
//! without the attachment feedback loop (`floor::Capabilities::feedback_loop`).
//!
//! An image a draw may read so has one shadow, an image of its format, extent, levels and layers
//! whose home is shader-read-only, made at the first draw that reads it and freed with it. A
//! texture that reaches the rendered level and layer reads the shadow through its second element
//! (`crate::arrays`); one that reaches neither reads the image itself, at home. A draw copies into
//! the shadow what it reads that the shadow does not hold, with no look-ahead: everything it reads
//! at the pass's first draw that reads the image, and later only what is new, as one area of each
//! read where what is new is one. What the shadow holds counts from the pass's first copy into it:
//! a memory barrier after draws, which orders what they rendered before later reads, leaves it
//! holding nothing, so the next draw that reads the image copies what it reads again.
//!
//! Where the pass cleared the attachment and the draw is the pass's first, the rendered level and
//! layer holds only the clear colour inside Metal's render area: where every read of that level
//! and layer lies in it alone and inside that area, the shadow's is cleared to the same colour
//! (the pass's clear value, the shadow of the attachment's format) in place of the copy, and the
//! rest of what the draw reads is copied. A draw of the pass recorded before, of any pipeline,
//! leaves the copy.
//!
//! A slot the fragment function reads only at its own pixel (`StageModule::own_pixel_textures`)
//! reads the rectangle of the draw's scissor, a pixel wider each way for its helper invocations,
//! clamped into its level; any other slot reads every level and layer its texture reaches.
//!
//! The copies' bytes are counted by read (at the fragment's own pixel, or all a texture reaches)
//! and by when they come ([`Moment`]).
/// The most areas a shadow's [`Held`] keeps: past them it forgets the oldest, which a later read
/// copies again.
const HELD: usize = 8;
/// A box of an image's texels: one level, a range of its layers, and a rectangle of the level,
/// each `[first, end)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Area {
pub(crate) level: u32,
pub(crate) layers: [u32; 2],
pub(crate) x: [u32; 2],
pub(crate) y: [u32; 2],
}
impl Area {
/// Whether it holds no texel.
pub(crate) fn is_empty(&self) -> bool {
[self.layers, self.x, self.y]
.iter()
.any(|&[first, end]| first >= end)
}
/// Its texels.
pub(crate) fn texels(&self) -> u64 {
[self.layers, self.x, self.y]
.iter()
.map(|&[first, end]| u64::from(end.saturating_sub(first)))
.product()
}
/// Whether it holds a texel of level `level`'s layer `layer`.
pub(crate) fn reaches(&self, (level, layer): (u32, u32)) -> bool {
!self.is_empty() && self.level == level && self.layers[0] <= layer && layer < self.layers[1]
}
/// Whether it holds every texel of `other`.
fn contains(&self, other: &Area) -> bool {
self.level == other.level
&& covers(self.layers, other.layers)
&& covers(self.x, other.x)
&& covers(self.y, other.y)
}
/// What of it to copy where `held` is copied already: what is left of it past `held` where
/// that is one area (empty where `held` holds all of it), else all of it.
fn past(&self, held: &Area) -> Area {
if held.level != self.level || !covers(held.layers, self.layers) {
return *self;
}
match (covers(held.x, self.x), covers(held.y, self.y)) {
(true, _) => less(self.y, held.y).map_or(*self, |y| Area { y, ..*self }),
(false, true) => less(self.x, held.x).map_or(*self, |x| Area { x, ..*self }),
(false, false) => *self,
}
}
}
/// Whether `outer` holds every value of `inner`.
fn covers(outer: [u32; 2], inner: [u32; 2]) -> bool {
outer[0] <= inner[0] && inner[1] <= outer[1]
}
/// `a` but `b`, where what is left is one range, empty where `b` covers `a`; `None` where `b`
/// lies inside `a` and leaves a range on each side.
fn less(a: [u32; 2], b: [u32; 2]) -> Option<[u32; 2]> {
let [first, end] = a;
if b[1] <= first || end <= b[0] {
return Some(a);
}
match (b[0] <= first, end <= b[1]) {
(true, true) => Some([first, first]),
(true, false) => Some([b[1], end]),
(false, true) => Some([first, b[0]]),
(false, false) => None,
}
}
/// What a draw reads through a slot its fragment function reads only at its own pixel, of a
/// texture whose first level and layer are `level` and `layer` of its image, that level `size`
/// texels: the rectangle bounding the draw's `scissor` (per axis `[first, end)`), a pixel wider
/// each way for its helper invocations, clamped into the level as the read's coordinate is.
/// Empty for an empty scissor, which draws no fragment.
pub(crate) fn own_pixel(level: u32, layer: u32, size: [u32; 2], scissor: [[u32; 2]; 2]) -> Area {
let span = |[first, end]: [u32; 2], size: u32| match first < end && size > 0 {
true => {
let last = size - 1;
[first.saturating_sub(1).min(last), end.min(last) + 1]
}
false => [0, 0],
};
let (x, y) = (span(scissor[0], size[0]), span(scissor[1], size[1]));
let layers = match x[0] < x[1] && y[0] < y[1] {
true => [layer, layer + 1],
false => [layer, layer],
};
Area {
level,
layers,
x,
y,
}
}
/// What a draw reads through any other slot: every level and layer of a texture whose first
/// level and layer are `level` and `layer` of its image, of `levels` levels and `layers` layers,
/// the image's first level `size` texels. One area a level.
pub(crate) fn whole(level: u32, levels: u32, layer: u32, layers: u32, size: [u32; 2]) -> Vec<Area> {
(level..level + levels)
.map(|level| Area {
level,
layers: [layer, layer + layers],
x: [0, (size[0] >> level).max(1)],
y: [0, (size[1] >> level).max(1)],
})
.collect()
}
/// Whether a clear of `rendered`, the level and layer a pass cleared that no draw of the pass has
/// rendered since, gives the shadow what the image holds of each of `areas` that reaches it: one
/// does, and each such lies in that level and layer alone and inside Metal's render area `area`
/// (`[width, height]` from the level's origin), which holds only the clear colour.
pub(crate) fn clears(areas: &[Area], rendered: (u32, u32), area: [u32; 2]) -> bool {
let layer = rendered.1;
let mut reaching = areas.iter().filter(|a| a.reaches(rendered)).peekable();
reaching.peek().is_some()
&& reaching
.all(|a| a.layers == [layer, layer + 1] && a.x[1] <= area[0] && a.y[1] <= area[1])
}
/// The subresources `areas` reach, each a level and a range of its layers, the ranges of one level
/// joined where they meet or overlap: what a copy's barriers move, each subresource once.
pub(crate) fn ranges(areas: &[Area]) -> Vec<(u32, [u32; 2])> {
let mut ranges: Vec<(u32, [u32; 2])> = (areas.iter())
.filter(|area| !area.is_empty())
.map(|area| (area.level, area.layers))
.collect();
ranges.sort_unstable();
let mut joined: Vec<(u32, [u32; 2])> = Vec::new();
for (level, layers) in ranges {
match joined.last_mut() {
Some((last, span)) if *last == level && layers[0] <= span[1] => {
span[1] = span[1].max(layers[1]);
}
_ => joined.push((level, layers)),
}
}
joined
}
/// `ranges` parted at the rendered `level` and `layer`: whether one of them holds it, and the
/// rest of them without it, which lie at home.
pub(crate) fn parted(
ranges: &[(u32, [u32; 2])],
(level, layer): (u32, u32),
) -> (bool, Vec<(u32, [u32; 2])>) {
let mut rendered = false;
let mut rest = Vec::new();
for &(at, [first, end]) in ranges {
if at == level && (first..end).contains(&layer) {
rendered = true;
rest.extend(
[[first, layer], [layer + 1, end]]
.into_iter()
.filter(|[first, end]| first < end)
.map(|span| (at, span)),
);
} else {
rest.push((at, [first, end]));
}
}
(rendered, rest)
}
/// When a draw copies into a shadow, for its copies' counters
/// ([`Metrics::attachment_read_copy_bytes_first_in_pass`](crate::Metrics::attachment_read_copy_bytes_first_in_pass)
/// and the two beside it), taken before the draw plans them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Moment {
/// The shadow has held nothing since its pass began.
FirstInPass,
/// A memory barrier after draws, or a draw that reads a colour attachment at its own pixel
/// after draws, left it holding nothing.
AfterBarrier,
/// It holds something, and the draw reads what it does not hold.
BeyondHeld,
}
/// The bytes of a draw's copies into its pass's shadows, parted for their counters: by read and by
/// [`Moment`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Copied {
/// Of reads at the fragment's own pixel ([`own_pixel`]).
pub(crate) own_pixel: u64,
/// Of reads of all a texture reaches ([`whole`]).
pub(crate) whole_texture: u64,
/// Of each [`Moment`].
pub(crate) first_in_pass: u64,
pub(crate) after_barrier: u64,
pub(crate) beyond_held: u64,
}
impl Copied {
/// A copy of `bytes` for a read at the fragment's own pixel or not, at `moment`.
pub(crate) fn add(&mut self, bytes: u64, own_pixel: bool, moment: Moment) {
*match own_pixel {
true => &mut self.own_pixel,
false => &mut self.whole_texture,
} += bytes;
*match moment {
Moment::FirstInPass => &mut self.first_in_pass,
Moment::AfterBarrier => &mut self.after_barrier,
Moment::BeyondHeld => &mut self.beyond_held,
} += bytes;
}
/// All the copies' bytes.
pub(crate) fn bytes(&self) -> u64 {
self.own_pixel + self.whole_texture
}
}
/// What a shadow holds of its image since the pass's first copy into it or its last memory
/// barrier after draws: areas, each copied whole, at most [`HELD`] of them; and, for the copies'
/// counters, whether a memory barrier left it holding nothing since it last held something.
#[derive(Debug, Default)]
pub(crate) struct Held {
areas: Vec<Area>,
/// A memory barrier after draws left it holding nothing, and it has held nothing since.
dropped: bool,
}
impl Held {
/// The copies a draw that reads `reads` of the image makes first: for each read, what of it
/// the shadow does not hold, the least area one held area leaves of it, or all of it; the
/// shadow holds every read from then.
pub(crate) fn plan(&mut self, reads: &[Area]) -> Vec<Area> {
let mut copies = Vec::new();
for read in reads.iter().filter(|read| !read.is_empty()) {
let copy = (self.areas.iter())
.map(|held| read.past(held))
.min_by_key(Area::texels)
.unwrap_or(*read);
if !copy.is_empty() {
copies.push(copy);
self.hold(*read);
}
}
copies
}
/// Whether the shadow holds every read of `reads`: whether [`plan`](Self::plan) copies none.
pub(crate) fn holds(&self, reads: &[Area]) -> bool {
(reads.iter().filter(|read| !read.is_empty()))
.all(|read| (self.areas.iter()).any(|held| read.past(held).is_empty()))
}
/// The shadow holds `area`: a held area inside it is dropped, and past [`HELD`] the oldest.
fn hold(&mut self, area: Area) {
self.areas.retain(|held| !area.contains(held));
if self.areas.len() == HELD {
self.areas.remove(0);
}
self.areas.push(area);
}
/// A memory barrier after draws: the shadow holds nothing of what they rendered.
pub(crate) fn clear(&mut self) {
if !self.areas.is_empty() {
self.dropped = true;
self.areas.clear();
}
}
/// When a draw's copies come, taken before the draw plans them.
pub(crate) fn moment(&self) -> Moment {
match (self.areas.is_empty(), self.dropped) {
(false, _) => Moment::BeyondHeld,
(true, false) => Moment::FirstInPass,
(true, true) => Moment::AfterBarrier,
}
}
/// The draw's copies made the shadow hold something after it held nothing
/// ([`Moment::FirstInPass`] or [`Moment::AfterBarrier`]).
pub(crate) fn began(&mut self) {
self.dropped = false;
}
}