Conformance: quad-helpers, a fragment's quad and simdgroup operations over quads with helpers

The owned fixture quad_helpers and the case quad-helpers. Five quad functions (quad_any and
quad_all of the helper query, quad_ballot of it, quad_is_first, quad_active_threads_mask) are
each drawn over 64 small triangles, one in each 8 by 8 cell of a 64 by 64 target, whose edges
cross quads. Three simdgroup functions (sum, min and max; and, or and xor; any, all, and the
lane counts of the ballot and the active mask) are each drawn over one triangle that covers
three pixels of a single quad, once for each quad lane the helper can take. Those are the draw's
only fragments, so the simdgroup holds that quad alone and no answer names a lane.

Each function runs its operations first, in control flow uniform over the draw, and then takes
a derivative of a value the answers feed. A helper therefore still has a gradient to compute when
the operations run, and MSL 4.1 sections 6.10.2 and 6.10.3 make it an active thread of each.

A translator test, a_fragment_simdgroup_operation_keeps_the_mode, checks that a fragment entry
with a simdgroup operation, with or without a derivative beside it, states
MaximallyReconvergesKHR on every maximal-reconvergence profile.

The fixture's companions and Metal's recording follow from the M3.
This commit is contained in:
Anees Iqbal committed 2026-11-23 16:00:37 -08:00
1 parent 72a52c85e9
commit 326213f839
6 files changed
+841 -1

No files matched your search

@@ -335,6 +335,7 @@ one.
| `function_bindings` | What a kernel's reflection says each argument is: the binding, its subprotocol's fields, and an argument buffer's struct type member by member | run on Metal |
| `indirect_commands` | What an indirect command buffer's CPU-encoded compute and render commands ran: both dispatch kinds, ordinary and indexed draws, the resets, the barriers, an attribute stride, a stage-in region, and the descriptor properties no execution needs | run on Metal |
| `reconvergence` | The nearest and trilinear samples at the implicit level of detail and a fine derivative pair, after each construct a fragment reconverges from and through a texture a ternary on two handles chooses; the trilinear sample is a site of its own, since its bits are the device's filter | run on Metal |
| `quad_helpers` | What a fragment's quad votes, ballot, `is_first` and active mask, and its simdgroup sums, minimum, maximum, bitwise reductions, votes and lane counts answer over quads a triangle covers in part, each before a derivative its helpers still compute; the simdgroup functions over one partly covered quad alone | run on Metal |
A carried answer is our own measurement of this chip, stated in the shape the case emits, and its
case forbids re-recording with the reason why. Record mode refuses such a case rather than
@@ -145,6 +145,7 @@ pub mod pixel_samplers;
pub mod primitive_type_crossings;
pub mod pvrtc_cpu_order;
pub mod pvrtc_rows;
pub mod quad_helpers;
pub mod rate_map_degenerate_forms;
pub mod rate_map_sweeps;
pub mod rate_maps;
@@ -347,6 +348,9 @@ pub fn all() -> Vec<Box<dyn Case>> {
for case in reconvergence::all() {
cases.push(Box::new(case));
}
for case in quad_helpers::all() {
cases.push(Box::new(case));
}
for case in int_attributes::all() {
cases.push(Box::new(case));
}
@@ -0,0 +1,630 @@
//! A fragment's quad and simdgroup operations over quads a triangle covers in part, whose
//! uncovered pixels run as helpers.
//!
//! The owned fixture `quad_helpers` holds one fragment function per quad operation, each drawn in
//! a pass of its own over 64 small triangles, one in each 8 by 8 cell of a 64 by 64 `RGBA16Float`
//! target (`qh_cells`, `reconverge_select`'s `rs_cells`), whose edges cross quads, so a quad on an
//! edge runs one to three helpers:
//!
//! - `qh_any`: `quad_any` of the helper query, 1 in a quad with a helper;
//! - `qh_all`: `quad_all` of its negation, 0 in a quad with a helper;
//! - `qh_ballot`: `quad_ballot` of the helper query, the quad's helper lanes;
//! - `qh_is_first`: `quad_is_first`, quad lane 0 where the helpers take part;
//! - `qh_active`: `quad_active_threads_mask`, every lane where the helpers take part.
//!
//! Each pixel writes `(answer, dfdx(v + answer), dfdy(v + answer), quad lane)`, v a per-quad
//! plane of data words whose fine and coarse derivatives are alike.
//!
//! Three simdgroup functions follow, each drawn in four passes of one triangle over three pixels
//! of the quad at (2, 2), the helper at quad lane 0 to 3 (`qh_one`): those three pixels and the
//! helper are the draw's only fragments, so the simdgroup that shades them holds that quad alone.
//! Which pixels share a fragment simdgroup, and its lane order, are the device's (the runtime's
//! `fragment-simdgroup-grouping`; MSL states neither), so only answers that name no lane are
//! here: `qs_arith` (`simd_sum`, `simd_min`, `simd_max` of a byte of the pixel's hash), `qs_bits`
//! (`simd_and`, `simd_or`, `simd_xor` of 11 bits of it) and `qs_votes` (`simd_any` of the helper
//! query and `simd_all` of its negation, and how many bits the low 32 of `simd_ballot` of the
//! query and of `simd_active_threads_mask` set). `simd_is_first`, `simd_broadcast_first` and the
//! prefix sums and products name a lane and are not here. Each pixel writes the three answers and
//! `dfdx` of v plus their sum. A pass reads back the 8 by 8 corner of the target the quad lies in.
//!
//! **What Metal defines.** An active thread is a thread that is executing, the simdgroup and quad
//! functions act on the active threads, and a helper thread is active until it has computed its
//! gradients (MSL 4.1 sections 6.10.2 and 6.10.3: "Helper threads only execute to compute
//! gradients for quad-groups in a fragment shader and then become inactive"). Each function runs
//! its operations in control flow uniform over the draw and then takes a derivative of a value
//! their answers feed, so every helper still has a gradient to compute when they run, and is one
//! of their active threads. After a function's last derivative a helper's part is not Metal's to
//! state, and no operation here stands there.
//!
//! The clear value, -1 in every channel, is part of the answer: a pixel a draw does not cover
//! holds it, and so does a helper's, whose outputs Metal discards.
//!
//! Nothing here can fault a GPU: one draw of at most 64 small triangles a pass, one 64 by 64
//! target, and no loop in the fixture.
use crate::allowance::Allowance;
use crate::case::{call, drawn_or_refused, made, Abort, Case, Failure, Input, MetalSource, Side};
use crate::degradation::Degradation;
use crate::fixture::{Fixture, METAL_TEXT, RUNTIME_TEXT};
use crate::recording::ShaderRef;
use crate::render::{
ColorState, PassColor, Plane, ReadAt, RenderPass, RenderPipelineSpec, RenderStep, RenderWork,
StageFunction, TargetSpec,
};
use crate::transcript::{
Aspect, ByteLayout, Digest, Element, Gaps, Node, Observed, Region, Sites, Value,
};
use metal2vulkan::metal::{PixelFormat, PrimitiveType};
/// The fixture every function of the case lives in.
const FIXTURE: &str = "quad_helpers";
/// The target's side in pixels.
const SIDE: u32 = 64;
/// The side of the corner a one-quad pass reads back: the quad at (2, 2) and the pixels around it.
const CORNER: u32 = 8;
/// The target's format: four halves a pixel.
const FORMAT: PixelFormat = PixelFormat::RGBA16Float;
/// Bytes one texel takes.
const TEXEL: u32 = 8;
/// Every channel of the clear value.
const CLEAR: f64 = -1.0;
/// The quad functions, each drawn over `qh_cells`' 64 triangles.
const QUAD: [&str; 5] = ["qh_any", "qh_all", "qh_ballot", "qh_is_first", "qh_active"];
/// The simdgroup functions, each drawn over `qh_one`'s triangle with the helper at each quad lane.
const SIMD: [&str; 3] = ["qs_arith", "qs_bits", "qs_votes"];
/// One pass: its site, its fragment function, its vertex function, its first vertex and vertex
/// count, and the side of the square at the target's origin it reads back.
#[derive(Clone, Debug, PartialEq, Eq)]
struct Pass {
site: String,
fragment: &'static str,
vertex: &'static str,
first: u32,
vertices: u32,
side: u32,
}
impl Pass {
/// The bytes its read takes.
fn bytes(&self) -> u32 {
self.side * self.side * TEXEL
}
}
/// The case's passes, in order: each quad function over the cells, then each simdgroup function
/// once for each quad lane the helper takes.
fn passes() -> Vec<Pass> {
let quad = QUAD.iter().map(|&fragment| Pass {
site: fragment.to_owned(),
fragment,
vertex: "qh_cells",
first: 0,
vertices: 3 * 64,
side: SIDE,
});
let simd = SIMD.iter().flat_map(|&fragment| {
(0..4u32).map(move |lane| Pass {
site: format!("{fragment}-helper-{lane}"),
fragment,
vertex: "qh_one",
first: 3 * lane,
vertices: 3,
side: CORNER,
})
});
quad.chain(simd).collect()
}
/// The one colour attachment every pipeline writes.
const COLORS: [Option<ColorState>; 1] = [Some(ColorState::new(FORMAT))];
/// The target every pass renders into: private, since a blit reads it back.
fn target_spec() -> TargetSpec {
TargetSpec::read(FORMAT, [SIDE, SIDE])
}
/// The pipeline of a fragment function and the vertex function its passes draw with.
fn pipeline_spec(vertex: &'static str, fragment: &'static str) -> RenderPipelineSpec<'static> {
RenderPipelineSpec::of(
StageFunction::named(vertex),
Some(StageFunction::named(fragment)),
&COLORS,
)
}
/// The vertex function each fragment function's passes draw with.
fn vertex_of(fragment: &str) -> &'static str {
match QUAD.contains(&fragment) {
true => "qh_cells",
false => "qh_one",
}
}
/// Every fragment function, quad functions first.
fn fragments() -> impl Iterator<Item = &'static str> {
QUAD.into_iter().chain(SIMD)
}
/// The 256 data words every fragment reads at buffer 0: word i a hash of i.
fn words() -> Vec<u32> {
(0..256u32)
.map(|i| {
let mut x = i.wrapping_mul(0x9E37_79B1);
x ^= x >> 16;
x = x.wrapping_mul(0x85EB_CA6B);
x ^ (x >> 13)
})
.collect()
}
/// The data words' bytes.
fn data_bytes() -> Vec<u8> {
words().iter().flat_map(|word| word.to_le_bytes()).collect()
}
/// The layout of a square of `side` texels at the target's origin, a row of the layout a row of
/// pixels and its element 4x + c channel c of pixel x.
fn layout(side: u32) -> ByteLayout {
ByteLayout {
region: Region::Texture {
level: 0,
slice: 0,
aspect: Aspect::Color,
sample: 0,
origin: [0, 0, 0],
size: [side, side, 1],
},
element: Element::F16,
row_offset: 0,
row_elements: side * 4,
row_pitch: side * TEXEL,
rows: side,
image_pitch: side * side * TEXEL,
images: 1,
gaps: Gaps::Compared,
}
}
/// The case.
pub struct QuadHelpers;
impl QuadHelpers {
/// The fixture under the entry point `entry`.
fn fixture(entry: &'static str) -> Fixture {
Fixture {
name: FIXTURE,
entry,
}
}
/// Every entry the case's pipelines name, once each, vertex functions first.
fn entries() -> Vec<&'static str> {
["qh_cells", "qh_one"]
.into_iter()
.chain(fragments())
.collect()
}
}
impl Case for QuadHelpers {
fn name(&self) -> &'static str {
"quad-helpers"
}
fn metal_source(&self) -> MetalSource {
MetalSource::Run
}
fn allowances(&self) -> Vec<Allowance> {
// Declarations only, so the notices site stays readable; neither loosens a value. Our side
// records `quads-split-by-clipping` for every fragment function with a quad or simdgroup
// operation, and `fragment-simdgroup-grouping` for every one with a simdgroup operation,
// on every device. A value either moves stays divergent, a ledger row.
[
Degradation::QuadsSplitByClipping,
Degradation::FragmentSimdgroupGrouping,
]
.into_iter()
.map(|kind| crate::replay::notice_allowance(kind.code()))
.collect()
}
fn shaders(&self) -> Result<Vec<ShaderRef>, String> {
let mut shaders = Vec::new();
for entry in Self::entries() {
for extension in [RUNTIME_TEXT, METAL_TEXT] {
shaders.push(ShaderRef {
fixture: format!("{FIXTURE}{extension}"),
entry: entry.to_owned(),
source_sha256: crate::sha256_hex(
Self::fixture(entry).text(extension)?.as_bytes(),
),
});
}
}
Ok(shaders)
}
fn inputs(&self) -> Result<Vec<Input>, String> {
let fixture = Self::fixture(QUAD[0]);
Ok(vec![
Input {
name: "air".to_owned(),
digest: Digest::sha256(fixture.text(RUNTIME_TEXT)?.as_bytes()),
},
Input {
name: "air-typed".to_owned(),
digest: Digest::sha256(fixture.text(METAL_TEXT)?.as_bytes()),
},
Input::bytes("target", &target_spec().words()),
Input::bytes(
"pipelines",
&fragments()
.flat_map(|fragment| pipeline_spec(vertex_of(fragment), fragment).words())
.collect::<Vec<u8>>(),
),
Input::bytes(
"passes",
passes()
.iter()
.map(|pass| {
format!(
"{}:{}:{}:{}:{}",
pass.site, pass.vertex, pass.first, pass.vertices, pass.side
)
})
.collect::<Vec<String>>()
.join(",")
.as_bytes(),
),
Input::bytes("buffer:0", &data_bytes()),
Input::bytes("clear", &CLEAR.to_le_bytes()),
])
}
fn run(&self, side: &mut dyn Side, sites: &mut Sites) -> Result<(), Abort> {
let loaded = Self::fixture(QUAD[0]).loaded().map_err(|why| Abort {
at: "library".to_owned(),
failure: Failure::named("FixtureNotRead", why),
})?;
let library = made(sites, "library", side.library(&loaded.source()))?;
// One target for every pass: each pass clears it, and the pieces of one command buffer
// run in the order the case lists them, so a row sees only its own draw.
let target = made(sites, "target", side.target(&target_spec()))?;
let data = made(sites, "buffer/0", side.buffer(&data_bytes()))?;
let passes = passes();
let total: usize = passes.iter().map(|pass| pass.bytes() as usize).sum();
let read_back = made(sites, "buffer/read-back", side.zeroed_buffer(total))?;
// One pipeline a fragment function, each in force for the passes after it until the next:
// `passes` lists each function's passes together, in `fragments`' order.
let mut work = Vec::with_capacity(passes.len() * 2);
let mut offset = 0u64;
let mut current = None;
for pass in &passes {
let pipeline = match current {
Some((fragment, pipeline)) if fragment == pass.fragment => pipeline,
_ => {
let spec = pipeline_spec(pass.vertex, pass.fragment);
let pipeline = made(
sites,
&format!("{}/pipeline", pass.fragment),
side.render_pipeline(library, &spec),
)?;
current = Some((pass.fragment, pipeline));
pipeline
}
};
// Every pass in one command buffer: a pass and a blit each, the blits writing one
// slice of the read-back buffer apiece, so the case costs one commit and one wait.
let steps = vec![
RenderStep::Pipeline(pipeline),
RenderStep::FragmentBuffer {
buffer: data,
offset: 0,
index: 0,
},
RenderStep::Draw {
primitive: PrimitiveType::Triangle,
first: pass.first,
count: pass.vertices,
instances: 1,
},
];
work.push(RenderWork::Pass(
RenderPass::clearing(vec![PassColor::cleared(target, [CLEAR; 4])]).drawing(steps),
));
work.push(RenderWork::ReadTexture {
texture: target,
into: read_back,
offset,
row_pitch: pass.side * TEXEL,
size: [pass.side, pass.side],
plane: Plane::Whole,
at: ReadAt::BASE,
});
offset += u64::from(pass.bytes());
}
let outcome = call(sites, "render", side.render(&work), |outcome| {
outcome.status.clone()
})?;
let bytes = call(
sites,
"read-back",
side.read_buffer(read_back, 0, total),
|_| Value::Absent,
)?;
let mut first = 0usize;
for (at, pass) in passes.iter().enumerate() {
let end = first + pass.bytes() as usize;
let row = Node::branch(pass.site.clone(), Vec::new()).with(Node::leaf(
"texels",
Observed::bytes(bytes[first..end].to_vec(), layout(pass.side)),
));
// A row whose pass or read our side refused has no texels: the refusal is the row's
// answer and compares as one.
let refused = (outcome.refusal(2 * at)).or_else(|| outcome.refusal(2 * at + 1));
sites.push(drawn_or_refused(row, refused));
first = end;
}
Ok(())
}
}
/// The cases the suite holds.
pub fn all() -> Vec<QuadHelpers> {
vec![QuadHelpers]
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
/// The fixture's hash of `(x, y)`.
fn hash(x: u32, y: u32) -> u32 {
let mut h = x.wrapping_mul(0x9E37_79B1) ^ y.wrapping_mul(0x85EB_CA77);
h ^= h >> 15;
h = h.wrapping_mul(0x2C1B_3C6D);
h ^ (h >> 12)
}
/// The value of half `bits`, for the halves these targets hold (no NaN or infinity).
fn half(bits: u16) -> f32 {
let sign = if bits & 0x8000 != 0 { -1.0 } else { 1.0 };
let exponent = i32::from((bits >> 10) & 31);
let fraction = f32::from(bits & 1023);
sign * match exponent {
0 => fraction * 2f32.powi(-24),
_ => (1.0 + fraction / 1024.0) * 2f32.powi(exponent - 15),
}
}
/// The fixture's v at pixel `(x, y)`.
fn v(words: &[u32], x: u32, y: u32) -> f32 {
let word = |at: u32| words[(at & 255) as usize];
let h = hash(x >> 1, y >> 1);
let a = (word(h) & 255) as f32;
let b = (2 + (word(h >> 8) & 7)) as f32;
let c = (2 + (word(h >> 16) & 7)) as f32;
a + b * (x & 1) as f32 + c * (y & 1) as f32
}
/// The quad lane of pixel `(x, y)`.
fn lane(x: u32, y: u32) -> u32 {
(x & 1) | (y & 1) << 1
}
/// The texels of `bytes`, a square of `side` texels, as four channels each.
fn texels(bytes: &[u8], side: u32) -> Vec<[f32; 4]> {
assert_eq!(bytes.len(), (side * side * TEXEL) as usize);
bytes
.chunks(TEXEL as usize)
.map(|texel| {
let mut c = [0f32; 4];
for (at, b) in texel.chunks(2).enumerate() {
c[at] = half(u16::from_le_bytes([b[0], b[1]]));
}
c
})
.collect()
}
/// What quad function `fragment` writes at each covered pixel of a quad whose helper lanes
/// are `helpers`, where the helpers take part: one texel a quad lane, `None` at a helper.
fn quad_model(
fragment: &str,
words: &[u32],
qx: u32,
qy: u32,
helpers: u32,
) -> [Option<[f32; 4]>; 4] {
let answer = |l: u32| -> f32 {
match fragment {
"qh_any" => (helpers != 0) as u32 as f32,
"qh_all" => (helpers == 0) as u32 as f32,
"qh_ballot" => helpers as f32,
"qh_is_first" => (l == 0) as u32 as f32,
"qh_active" => 15.0,
other => unreachable!("no quad function {other}"),
}
};
let u = |x: u32, y: u32| v(words, x, y) + answer(lane(x, y));
let mut out = [None; 4];
for (l, slot) in out.iter_mut().enumerate() {
let l = l as u32;
if helpers & (1 << l) != 0 {
continue;
}
let (x, y) = (2 * qx + (l & 1), 2 * qy + (l >> 1));
*slot = Some([
answer(l),
u(x | 1, y) - u(x & !1, y),
u(x, y | 1) - u(x, y & !1),
l as f32,
]);
}
out
}
/// What simdgroup function `fragment` writes at covered pixel `(x, y)` of the quad at (2, 2),
/// where the helper takes part: the same wherever the helper lies.
fn simd_model(fragment: &str, words: &[u32], x: u32, y: u32) -> [f32; 4] {
let w = |x: u32, y: u32| words[(hash(x, y) & 255) as usize];
let quad: Vec<u32> = (0..4).map(|l| w(2 + (l & 1), 2 + (l >> 1))).collect();
let r = match fragment {
"qs_arith" => {
let bytes: Vec<u32> = quad.iter().map(|w| w & 255).collect();
[
bytes.iter().sum::<u32>(),
*bytes.iter().min().unwrap_or(&0),
*bytes.iter().max().unwrap_or(&0),
]
}
"qs_bits" => {
let bits: Vec<u32> = quad.iter().map(|w| w & 2047).collect();
[
bits.iter().fold(2047, |a, b| a & b),
bits.iter().fold(0, |a, b| a | b),
bits.iter().fold(0, |a, b| a ^ b),
]
}
// Any lane a helper, not every lane not one; one bit of the ballot; four lanes active.
"qs_votes" => [1, 1, 4],
other => unreachable!("no simdgroup function {other}"),
};
let sum = (r[0] + r[1] + r[2]) as f32;
let dfdx = (v(words, x | 1, y) + sum) - (v(words, x & !1, y) + sum);
[r[0] as f32, r[1] as f32, r[2] as f32, dfdx]
}
/// The case's bookkeeping holds: its sites are distinct, the fixture defines every entry a
/// pipeline names, the passes' reads fill the read-back buffer exactly, and every value the
/// model writes is an integer a half holds exactly.
#[test]
fn the_passes_the_layouts_and_the_fixture_agree() {
let passes = passes();
let sites: BTreeSet<&str> = passes.iter().map(|pass| pass.site.as_str()).collect();
assert_eq!(sites.len(), passes.len());
let text = QuadHelpers::fixture(QUAD[0])
.text(RUNTIME_TEXT)
.expect("the fixture's text");
for entry in QuadHelpers::entries() {
assert!(
text.contains(&format!("@{entry}(")),
"{entry} is not in {FIXTURE}"
);
}
for pass in &passes {
assert_eq!(
pass.bytes() as usize,
layout(pass.side).extent(),
"{}",
pass.site
);
}
let words = words();
for fragment in SIMD {
for (x, y) in [(2, 2), (3, 3)] {
for value in simd_model(fragment, &words, x, y) {
assert!(
value.fract() == 0.0 && value.abs() < 2048.0,
"{fragment}: {value}"
);
}
}
}
}
/// Every texel Metal recorded is the model's, where a quad's helpers are active threads of its
/// quad and simdgroup operations: each covered pixel of a quad holds what the quad's covered
/// pixels and helpers together answer, and every other pixel holds the clear value.
///
/// It reads the recording, so it runs where there is no GPU, and says whether Metal's answer
/// is the one the module describes; it never stands in for that answer.
#[test]
fn metal_s_recorded_answers_count_the_helpers() {
let words = words();
let Ok(recording) = crate::recording::read(QuadHelpers.name()) else {
println!("quad-helpers: no recording in the tree yet");
return;
};
let mut checked = 0usize;
let mut differ = Vec::new();
for recorded in &recording.answers {
for outcome in &recorded.outcomes {
for pass in passes() {
let Some(Value::Bytes(bytes)) = outcome
.transcript
.root
.child(&pass.site)
.and_then(|node| node.child("texels"))
.map(|node| &node.observed.value)
else {
continue;
};
checked += 1;
let got = texels(bytes, pass.side);
let at = |x: u32, y: u32| got[(y * pass.side + x) as usize];
let clear = [CLEAR as f32; 4];
let mut expected = vec![clear; got.len()];
if pass.side == SIDE {
// A quad's helpers are the lanes the draw left at the clear value; a quad
// with no covered lane did not run.
for qy in 0..SIDE / 2 {
for qx in 0..SIDE / 2 {
let helpers = (0..4u32)
.filter(|&l| at(2 * qx + (l & 1), 2 * qy + (l >> 1)) == clear)
.fold(0, |m, l| m | 1 << l);
if helpers == 15 {
continue;
}
let model = quad_model(pass.fragment, &words, qx, qy, helpers);
for (l, texel) in (0u32..).zip(model) {
if let Some(texel) = texel {
let (x, y) = (2 * qx + (l & 1), 2 * qy + (l >> 1));
expected[(y * SIDE + x) as usize] = texel;
}
}
}
}
} else {
let helper = pass.first / 3;
for l in (0..4u32).filter(|&l| l != helper) {
let (x, y) = (2 + (l & 1), 2 + (l >> 1));
expected[(y * CORNER + x) as usize] =
simd_model(pass.fragment, &words, x, y);
}
}
for (i, (g, e)) in got.iter().zip(&expected).enumerate() {
if g != e {
let (x, y) = (i as u32 % pass.side, i as u32 / pass.side);
differ.push(format!(
"{}: ({x}, {y}) holds {g:?}, the model {e:?}",
pass.site
));
}
}
}
}
}
for line in differ.iter().take(20) {
println!("QUAD-HELPERS-DIFFERS: {line}");
}
assert!(
differ.is_empty(),
"{} texels differ from the model",
differ.len()
);
println!("{checked} recorded passes read as the model says");
}
}
@@ -60,6 +60,7 @@ threadgroup's threads); an authored case that executes a loop declares it
| `fragment_fetch` | `fetch_blend`, `fetch_scalar`, `fetch_ordered` | framebuffer fetch through input attachments (float, `uint`, and `half` color inputs, a scalar one); `fetch_ordered` reads in a raster order group (inside the interlock where the device has pixel interlock) | `fetch_blend`: `color = dst · factor + (extra.xy, x, 0.25)`, `ids = ids + (1, 2, 3, 4)`; ordered only where the device has rasterization-order color attachment access |
| `reconverge_sample` | `rc_cover`, `rc_if`, `rc_loop`, `rc_break`, `rc_switch`, `rc_discard`, `rc_grid`, `rc_continue_quad`, `rc_latches` | an 11-level `r16_float` source (level n holds n + 1) sampled at the implicit level of detail, nearest and trilinear, and a fine `dfdx`/`dfdy` pair, after an if/else on data, a loop of the pixel's own trip count, a break to the loop's merge, a switch, a discard; inside selections and a loop uniform over 8 by 8 blocks (`rc_grid`), and in a uniform loop's second iteration after one pixel per quad ended the first early, by a continue (`rc_continue_quad`) or an if/else at the body's end (`rc_latches`); `rc_cover` draws one triangle over the target | each case writes (nearest sample, `dfdx(v)`, `dfdy(v)`, trilinear sample) of its lane's value v; the conformance family `reconvergence` compares each with Metal's |
| `reconverge_select` | `rs_cover`, `rs_cells`, `rc_select`, `rc_select_quad`, `rc_pick` | reconverge_sample's tail through a texture chosen by a ternary on two texture handles (11-level `r16_float` sources, level n holding n + 1 and 21 + n), inside a selection uniform over 8 by 8 blocks: per pixel (`rc_select`), per 4 by 4 block (`rc_select_quad`), or by a value the whole draw shares (`rc_pick`); `rs_cover` draws one triangle over the target, `rs_cells` a small triangle in each 8 by 8 cell | each written pixel holds (nearest sample, `dfdx(v)`, `dfdy(v)`, trilinear sample) of the chosen texture; the conformance family `reconvergence` compares each with Metal's |
| `quad_helpers` | `qh_cells`, `qh_one`, `qh_any`, `qh_all`, `qh_ballot`, `qh_is_first`, `qh_active`, `qs_arith`, `qs_bits`, `qs_votes` | a fragment's `quad_any` and `quad_all` of the helper query, its `quad_ballot`, `quad_is_first` and `quad_active_threads_mask`, and its `simd_sum`, `simd_min`, `simd_max`, `simd_and`, `simd_or`, `simd_xor`, `simd_any`, `simd_all` and the low 32 bits of `simd_ballot` and `simd_active_threads_mask` counted, each before a fine derivative of a value its answer feeds, so the helpers still compute one; `qh_cells` draws a small triangle in each 8 by 8 cell of a 64 by 64 target, `qh_one` one triangle over three pixels of the quad at (2, 2), helper at quad lane `vertex_id / 3` | each covered pixel holds its quad's or simdgroup's answer counting the helpers, and the derivative of v plus it; the conformance family `quad_helpers` compares each with Metal's |
| `handles` | `select_read`, `phi_read`, `select_null_write`, `array_read`, `sampler_select`, `fragment_select_sample`, `fragment_select_lod`, `argument_buffer_read`, `argument_buffer_array_write`, `argument_buffer_struct_array`, `argument_buffer_depth_read`, `argument_buffer_sample`, `argument_buffer_sample_int`, `argument_buffer_fragment_sample`, `argument_buffer_runtime_sampler`, `argument_buffer_sample_compare`, `argument_buffer_sample_border` | texture and sampler handles as values: selected, phi'd, null, and arrayed textures and samplers dispatched by a switch at the use; implicit-LOD fragment samples through a selected texture with hoisted gradients; an LOD query through one, every candidate queried before the switch; argument-buffer textures (the heap): one read, an element of a texture-array member written, an element of an array of argument buffers tested for null and read, and a depth texture read through the float class and written to a storage texture; heap textures sampled through constexpr samplers (a separate heap sampler): a linear and a pixel (fetch) sample copied to a storage texture, an integer texture's nearest sample, a fragment's implicit-LOD samples of a cube, a 2D, a depth, and a dynamically picked texture-array element, and an opaque-black border sample; a heap depth texture compared through the depth-comparison class; a heap texture sampled through a runtime sampler, which the binder writes with the bound sampler | structural and L3 only; the switch's values are future authored cases (`select_read`: the texel of `a` or `b` by `(gid & 1) == pick`, plus its width, height, and 0 for `is_null`) |
| `fragment_rog` | `rog_buffer`, `rog_texture`, `rog_output`, `rog_discard`, `rog_loop`, `rog_sample`, `discard_then_simd` | raster order groups on a device buffer, a read_write texture, a color output, and a buffer read after a discard; a loop in a group (its forced exit returns to the interlocked entry); a sample-rate group (sample interlock only where the module runs per sample); `discard_then_simd` is a demote-semantics discard before a simdgroup reduction (refused under predication) | every group entry runs its body inside `PixelInterlockOrderedEXT` where the device has pixel interlock, else is refused; ordering is a future authored case |
| `fragment_rog_order` | `rog_order_layers`, `rog_order_output` | a raster order group on a color output the function does not read, over eight overlapping fullscreen triangles, triangle `k` writing `(v, 1 - v, v / 2, 1 / 2)` with `v = (k + 1) / 8`; blended `d' = s + d / 2`, which does not commute | Metal's target holds the triangles' order's answer, (1.7529297, 0.2421875, 0.87597656, 1) in `RGBA16Float`, in one draw, eight, and two of four (measured on an M4); the runtime lane `raster_orders` asserts it on every device (no authored case) |
@@ -0,0 +1,165 @@
// Owned fixture (written for this repository): a fragment's quad votes, ballot, `is_first` and
// active mask, and its simdgroup reductions and votes whose answers name no lane, over quads a
// triangle covers in part, so each such quad runs its uncovered pixels as helpers.
// - `qh_cells` draws a small triangle in each 8 by 8 cell of a 64 by 64 target, as
// reconverge_select's `rs_cells` does, whose edges cross quads: a quad on an edge runs one to
// three helpers. `qh_one` draws one triangle over three pixels of the quad at (2, 2) (vertices
// 3k to 3k + 2: the helper is quad lane k), the only fragments of its draw, so the simdgroup
// that shades them holds that quad alone.
// - Each fragment function runs its group operations first, in control flow uniform over the
// draw, and then takes a derivative of a value the answers feed. A helper still has that
// derivative to compute when the operations run, so it is an active thread of each (MSL 4.1
// sections 6.10.2 and 6.10.3).
// - v = a + b * (x & 1) + c * (y & 1), with a, b (2 to 9) and c (2 to 9) words of the pixel's
// quad's hash in buffer 0, so the fine and the coarse derivative of v alike are b across and c
// down. The quad functions write (answer, dfdx(v + answer), dfdy(v + answer), quad lane), the
// simdgroup functions three answers and dfdx of v plus their sum. Every value is a small
// integer, exact in a half.
// Loop-free.
#include <metal_stdlib>
using namespace metal;
struct QhVertex {
float4 position [[position]];
};
// A target pixel's place in clip space, for the 64 by 64 target.
static float4 qh_clip(float2 at) {
return float4(at.x / 32.0f - 1.0f, 1.0f - at.y / 32.0f, 0.0f, 1.0f);
}
// Small triangles, one in each 8 by 8 cell of a 64 by 64 target (vertex 3k + c is corner c of
// cell k).
vertex QhVertex qh_cells(uint id [[vertex_id]]) {
uint cell = id / 3u;
uint corner = id % 3u;
float2 corners[3] = {float2(1.3f, 0.7f), float2(6.6f, 1.9f), float2(2.1f, 6.8f)};
QhVertex out;
out.position = qh_clip(float2(float(cell % 8u), float(cell / 8u)) * 8.0f + corners[corner]);
return out;
}
// One triangle over three pixels of the quad whose top-left pixel is (2, 2), leaving out quad
// lane k = id / 3 (column k & 1, row k >> 1). Its edges pass at least a third of a pixel from
// every pixel centre: x >= -0.15, y <= 2.15 and y >= x - 0.5 in the quad's own coordinates leave
// out (1, 0), mirrored across the quad's middle for the other lanes.
vertex QhVertex qh_one(uint id [[vertex_id]]) {
uint lane = id / 3u;
float2 corners[3] = {float2(-0.15f, -0.65f), float2(-0.15f, 2.15f), float2(2.65f, 2.15f)};
float2 at = corners[id % 3u];
if ((lane & 1u) == 0u) {
at.x = 2.0f - at.x;
}
if ((lane >> 1) != 0u) {
at.y = 2.0f - at.y;
}
QhVertex out;
out.position = qh_clip(at + 2.0f);
return out;
}
static uint qh_hash(uint2 q) {
uint h = (q.x * 0x9E3779B1u) ^ (q.y * 0x85EBCA77u);
h ^= h >> 15;
h *= 0x2C1B3C6Du;
h ^= h >> 12;
return h;
}
// v of pixel p: a + b * (x & 1) + c * (y & 1), from the words of its quad's hash.
static float qh_v(uint2 p, device const uint *data) {
uint h = qh_hash(p >> 1u);
float a = float(data[h & 255u] & 255u);
float b = float(2u + (data[(h >> 8) & 255u] & 7u));
float c = float(2u + (data[(h >> 16) & 255u] & 7u));
return a + b * float(p.x & 1u) + c * float(p.y & 1u);
}
// A quad function's texel: its answer r, then the fine pair of v + r, which the helpers compute
// after r, and the pixel's quad lane.
static float4 qh_texel(float r, float v, ushort lane) {
float u = v + r;
return float4(r, dfdx(u), dfdy(u), float(lane));
}
static float qh_bits(quad_vote vote) {
return float(uint(quad_vote::vote_t(vote)));
}
// Whether any lane of the quad is a helper.
fragment float4 qh_any(float4 position [[position]], ushort lane [[thread_index_in_quadgroup]],
device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
float r = float(quad_any(quad_is_helper_thread()));
return qh_texel(r, qh_v(p, data), lane);
}
// Whether every lane of the quad is not a helper.
fragment float4 qh_all(float4 position [[position]], ushort lane [[thread_index_in_quadgroup]],
device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
float r = float(quad_all(!quad_is_helper_thread()));
return qh_texel(r, qh_v(p, data), lane);
}
// The quad's helper lanes, as its ballot's four bits.
fragment float4 qh_ballot(float4 position [[position]], ushort lane [[thread_index_in_quadgroup]],
device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
float r = qh_bits(quad_ballot(quad_is_helper_thread()));
return qh_texel(r, qh_v(p, data), lane);
}
// Whether the pixel is its quad's first active lane.
fragment float4 qh_is_first(float4 position [[position]],
ushort lane [[thread_index_in_quadgroup]],
device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
float r = float(quad_is_first());
return qh_texel(r, qh_v(p, data), lane);
}
// The quad's active lanes.
fragment float4 qh_active(float4 position [[position]], ushort lane [[thread_index_in_quadgroup]],
device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
float r = qh_bits(quad_active_threads_mask());
return qh_texel(r, qh_v(p, data), lane);
}
// A simdgroup function's texel: three answers, then dfdx of v plus their sum, which the helpers
// compute after them.
static float4 qs_texel(float3 r, float v) {
return float4(r, dfdx(v + r.x + r.y + r.z));
}
// The low 32 bits of a simdgroup vote, the bits of a 32-wide simdgroup's lanes, counted.
static float qs_count(simd_vote vote) {
return float(popcount(uint(static_cast<simd_vote::vote_t>(vote))));
}
// The simdgroup's sum, minimum and maximum of a byte of the pixel's hash.
fragment float4 qs_arith(float4 position [[position]], device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
uint w = data[qh_hash(p) & 255u] & 255u;
float3 r = float3(float(simd_sum(w)), float(simd_min(w)), float(simd_max(w)));
return qs_texel(r, qh_v(p, data));
}
// The simdgroup's and, or and xor of 11 bits of the pixel's hash.
fragment float4 qs_bits(float4 position [[position]], device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
uint w = data[qh_hash(p) & 255u] & 2047u;
float3 r = float3(float(simd_and(w)), float(simd_or(w)), float(simd_xor(w)));
return qs_texel(r, qh_v(p, data));
}
// Whether any lane of the simdgroup is a helper (1) and every lane is not (2), how many lanes its
// ballot of the helper query sets, and how many it holds active.
fragment float4 qs_votes(float4 position [[position]], device const uint *data [[buffer(0)]]) {
uint2 p = uint2(position.xy);
bool h = simd_is_helper_thread();
float votes = float(uint(simd_any(h)) | uint(simd_all(!h)) << 1);
float3 r = float3(votes, qs_count(simd_ballot(h)), qs_count(simd_active_threads_mask()));
return qs_texel(r, qh_v(p, data));
}
@@ -3,7 +3,7 @@
//! records `Guaranteed` with no `DeviceReconvergence` on every committed profile, but where one of
//! them lies in a loop a back edge re-enters from inside a selection, where it takes the mode the
//! profile gives a tangled entry and, without maximal reconvergence, the note. A fragment
//! function with a quad operation keeps its mode.
//! function with a quad or simdgroup operation keeps its mode.
//!
//! `llvm-as` assembles the fixtures; without it the tests skip visibly.
@@ -254,3 +254,42 @@ fn a_fragment_quad_operation_keeps_the_mode() {
}
assert!(compiled > 0, "no profile compiled a quad operation");
}
/// A fragment function with a simdgroup operation keeps the mode wherever it compiles, a
/// derivative beside it or not: without `MaximallyReconvergesKHR` Vulkan may leave a helper out
/// of every group operation but a derivative or a quad one (section 9.31, "Helper Invocations"),
/// where Metal counts a helper that still has a gradient to compute as an active thread.
#[test]
fn a_fragment_simdgroup_operation_keeps_the_mode() {
let cases: [(&str, &[&str]); 4] = [
("quad_helpers", &["qs_arith", "qs_bits", "qs_votes"]),
("fragment_rog", &["discard_then_simd"]),
("gsm2_fragment_lanes", &["fragment_lane_shuffles"]),
(
"gsm2_helper_threads",
&["fragment_helper_threads_beside_a_simdgroup_vote"],
),
];
let profiles = committed_profiles().expect("profiles");
for (fixture_stem, entries) in cases {
let Some(bc) = fixture(fixture_stem) else {
return;
};
for &entry in entries {
let mut compiled = 0;
for (stem, p) in &profiles {
if !p.fields().subgroup.maximal_reconvergence || compile(&bc, entry, p).is_err() {
continue;
}
compiled += 1;
let (mode, reconvergence, noted, _) = reading(&bc, entry, stem, p);
assert_eq!(
(mode, reconvergence, noted),
("maximal", Reconvergence::Guaranteed, false),
"{stem} {fixture_stem} {entry}"
);
}
assert!(compiled > 0, "no profile compiled {fixture_stem} {entry}");
}
}
}