Files
metal2vulkan/crates/metal2vulkan-runtime/src/reader_sizes.rs
T
Anees Iqbal 61a367cb51 Weigh a self-read's two forms at the coverage least favourable to each
A draw that reads its pass's private texture at its own pixels after a barrier takes a snapshot
drawn in its subpass or splits the render pass instance; both are exact. The area rule weighed the
snapshot as if the draw covered its whole scissors (20 bytes of the split's copy a pixel) against
the split's copy alone, so a reader through no scissor over a large target always split. The
consumer's guest draws its self-reads that way: on the Iris Xe its 3662x2039 desktop's 30 readers a
frame took 10.9 to 17.4 ms of GPU time split (selfread's jobs 6068 and 5702), against about 1 ms
through snapshots.

What a draw covers is unknown when it records. Measured with both forms forced (a scratch build,
e3792c8f2e, never landed; 30 readers a frame over private BGRA8Unorm targets of 1280x720 to
3662x2039, no scissor):
- A split costs a fixed 5 to 18 us besides its copy, which is every texel here: the cut is the
  readers' and fills' scissors, the whole target (30 x W x H texels copied a frame, job 6674).
- A snapshot costs a fixed 9 to 16 us (two barriers and the second draw) and then grows with the
  pixels the draw covers.
- Snapshot over split, reader covering 1.5% / 100% of the target: Iris Xe 0.09-0.33 / 1.04-1.28
  (jobs 6656, 6674, ms01); Radeon 780M 0.11-0.24 / 2.4-2.8 (6758, um790); RX 7900 XTX
  0.25-0.43 / 1.3-2.1 (6822, gem10).

The rule now weighs each form at the coverage least favourable to it and takes the one whose cost
there is the smaller multiple of the other's: the snapshot where
READER_COST * (READER_COST + pixels * SNAPSHOT_PIXEL_COST) <= (READER_COST + bytes)^2, with the
fixed cost READER_COST a mebibyte of the copy (both forms' measured fixed costs on the iGPUs) and
SNAPSHOT_PIXEL_COST still 20 (measured about 14 on the Iris Xe, 20 on the 780M, 21 on the XTX).
Every reader the old rule gave the snapshot keeps it (the old rule is the case where the snapshot
over its whole scissors costs no more than the copy). New snapshots: a 4-byte color's readers
through scissors of up to two fifths of the target at any size (was a fifth), and at any area from
1024x768 (786,432 pixels; an 8-byte color's from 256x256). By the measurements above the rule
picks the snapshot in every no-scissor cell on all three GPUs, the form whose worst case is the
smaller multiple there; the cost is up to 1.28x (Iris Xe), 2.8x (780M) and 2.1x (XTX) for a reader
covering the whole target, against 3x to 11x saved for sparse ones. Small scissors covered whole
keep the snapshot they had, up to 2.1x on the Iris Xe (FAST-PATHS says so).

Both forms are exact, so this is a cost choice with no accuracy side: no ledger row, the
attachment-read paragraph of docs/FAST-PATHS.md restated.

- render/snapshot.rs: snapshot_within and READER_COST; the module docs' area rule.
- tests/snapshot_area.rs: the consumer's shape, the size thresholds (786,432 and 65,536 pixels
  exactly), two fifths at any size, and every old snapshot kept. Fails on the old rule.
- runtime_lane::snapshot_reads: an RGBA8Unorm reader through a quarter of the 32x32 target now
  takes the snapshot where the device takes the path (was a split).
- runtime_lane::attachment_read_costs::snapshot_area_costs: the measurement, the rule's form on
  the default device against the split with fragmentShaderPixelInterlock withheld. The composite
  workload's window readers now take the snapshot; its docs say so.

(cherry picked from commit 65d58b231d38ad2c2ac22f8e448be3a48491bf9a)
2026-11-07 20:50:32 -08:00

86 lines
3.9 KiB
Rust

//! The sizes of the draws that read their pass's texture through a snapshot or take the split by
//! the area rule (`render/snapshot.rs`'s `RenderCommandEncoder::snapshot_cheaper`), counted for a
//! diagnostic line where `METAL2VULKAN_LOG_PIPELINES` is set: how many such draws weigh which share
//! of their colors' pixels, at which texel bytes, on which side of the rule. A device counts them
//! only while the log is on, writes the line after 64 of them and after each power of two past
//! it, and once more as it ends where it counted one since; the line changes nothing it records.
//!
//! A draw's share is its scissors' pixels for each color it reads so, over the pixels of those
//! colors' levels, in steps of 0.5% (one step at 100% and above); its texel bytes are those
//! colors' bytes over their pixels, rounded down: a color's own format's bytes for a draw that
//! reads one color, or colors of one width. The rule's bound on the share
//! (`render/snapshot.rs`'s area rule) turns on the texel bytes and on the colors' size, so the
//! line shows how many draws lie near it for each width.
use std::collections::BTreeMap;
use std::fmt::Write;
/// The steps of a share: 0.5% each, the last one 100% and above.
const STEPS: u64 = 200;
/// The fewest weighed draws a line is written after, and each power of two from it.
const FIRST_LINE: u64 = 64;
/// The draws a device weighed by the area rule, by texel bytes, side and share.
#[derive(Debug, Default)]
pub(crate) struct ReaderSizes {
weighed: u64,
/// The count of each texel bytes, side of the rule (`true` within, so the snapshot) and
/// share step.
counts: BTreeMap<(u64, bool, u64), u64>,
}
impl ReaderSizes {
/// Count a draw whose scissors bound `covered` pixels over all the colors it reads so
/// (the scissors' pixels times the colors), of colors of `pixels` pixels and `bytes` bytes in
/// all, `within` the rule or not; the diagnostic line, where the count of weighed draws
/// reaches [`FIRST_LINE`] or a power of two past it.
pub(crate) fn add(
&mut self,
covered: u64,
pixels: u64,
bytes: u64,
within: bool,
) -> Option<String> {
let step = match pixels {
0 => STEPS,
p => (covered.saturating_mul(STEPS) / p).min(STEPS),
};
let texel = bytes.checked_div(pixels).unwrap_or(0);
*self.counts.entry((texel, within, step)).or_default() += 1;
self.weighed += 1;
(self.weighed >= FIRST_LINE && self.weighed.is_power_of_two()).then(|| self.line())
}
/// The line a device writes as it ends, where it weighed a draw since its last line.
pub(crate) fn last_line(&self) -> Option<String> {
let written = self.weighed >= FIRST_LINE && self.weighed.is_power_of_two();
(self.weighed > 0 && !written).then(|| self.line())
}
/// The diagnostic line: the draws weighed, then for each texel bytes and side, each share
/// step's count.
pub(crate) fn line(&self) -> String {
let mut text = format!(
"{} draws that read their pass's texture weighed by the area rule (texel bytes, side, \
then share of the colors' pixels the scissors bound, from, in 0.5% steps: count):",
self.weighed
);
let mut group = None;
for (&(texel, within, step), &count) in &self.counts {
if group != Some((texel, within)) {
let side = if within { "snapshot" } else { "split" };
let gap = if group.is_some() { ";" } else { "" };
let _ = write!(text, "{gap} {texel} B {side}");
group = Some((texel, within));
}
let label = match step {
STEPS => "100%+".to_string(),
s => format!("{}.{}%", s / 2, (s % 2) * 5),
};
let _ = write!(text, " {label}:{count}");
}
text
}
}