Shaders: quad reads in pixel programs reach the helpers at triangle edges

The console runs pixel programs in whole-quad mode: in every quad a
triangle edge crosses, the pixels outside the triangle run as helpers, and
the compiler takes derivatives of computed values (and quad reads) by
reading the neighbour's register with DPP `quad_perm` or `ds_swizzle`. We
lowered those reads to `WaveReadLaneAt` unless the program discards. Wave
operations leave helper lanes out on the host, and on AMD a read of one
gives 0: every pixel whose quad neighbour lies outside the triangle took a
wrong derivative, along every triangle edge. WARP includes helpers in wave
reads, so the tests passed there.

Quad-local reads in pixel programs now always go through quad reads
(`QuadReadLaneAt`), as they already did after a discard. What the host
guarantees there: helper lanes execute the program so that derivatives
can be taken, and quad operations include them (wave operations do not),
so a quad read of a helper gives the value that helper computed, as the
console's DPP read of a whole-quad-mode lane does. A discarded pixel stays
a helper in the same way.

Tests: two control-flow cases draw a triangle whose diagonal crosses
quads (`ddx_fine`, `ddy_fine`, `QuadReadAcrossDiagonal`, `QuadReadLaneAt`
of a computed value, console compiler against PC DXC). "derivatives and
quad reads across a triangle edge": before, RX 6700 XT 24 of 256 texels
differ (texel (0, 0): ddx -1 instead of 15, the helper read as 0), WARP 0;
now 0 and 0. "discard, then derivatives and quad reads across a triangle
edge" discards some neighbours before the reads (the path that used quad
reads already): 0 and 0.

Whole list (translate --list --validate): 3,005 pixel programs change,
84,589 read sites (median 15 per program, at most 732); no program changes
outcome. DXIL 1,197,299,944 -> 1,203,716,752 bytes (+0.54 %) on top of
the writer folding (on main without it: 1,423,424,800 -> 1,429,871,224),
blocks unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MarvisandClaude Opus 5.5 committed 2026-11-25 14:52:53 +01:00
1 parent a1a2077a3d
commit 4015a182e3
2 files changed
+49 -12

No files matched your search

+43 -8
View File
@@ -252,6 +252,29 @@ const PS_CASES: &[(&str, &str)] = &[
return float4(ddx_fine(v), ddy_fine(v), v, 1);
}"#,
),
// Derivatives and quad reads of values each pixel computes, on a
// triangle whose edge crosses quads: the neighbours outside it are
// helpers (`EDGE`).
(
"derivatives and quad reads across a triangle edge",
r#"[RootSignature(RS)] float4 ps(V i) : SV_Target {
uint2 p = uint2(i.pos.xy);
float v = p.x * 3.0 + p.y * 5.0 + 1;
v = v * v;
return float4(ddx_fine(v), ddy_fine(v), QuadReadAcrossDiagonal(v), QuadReadLaneAt(v, 0));
}"#,
),
// The same with some neighbours discarded before the reads: helpers too.
(
"discard, then derivatives and quad reads across a triangle edge",
r#"[RootSignature(RS)] float4 ps(V i) : SV_Target {
uint2 p = uint2(i.pos.xy);
float v = p.x * 3.0 + p.y * 5.0 + 1;
v = v * v;
if (((p.x * 7 + p.y * 3) & 5) == 5) discard;
return float4(ddx_fine(v), ddy_fine(v), QuadReadAcrossDiagonal(v), QuadReadLaneAt(v, 0));
}"#,
),
(
"discard with depth and a second target",
r#"struct O { float4 c : SV_Target0; float4 d : SV_Target1; float z : SV_Depth; };
@@ -268,6 +291,9 @@ const PS_CASES: &[(&str, &str)] = &[
),
];
/// Pixel cases named with this draw a triangle whose edge crosses quads.
const EDGE: &str = "triangle edge";
/// Per-lane inputs: four dwords per lane from a fixed hash (low bits vary
/// lane to lane; a few lanes hold larger values).
fn lane_inputs(lanes: u32) -> Vec<u32> {
@@ -441,17 +467,23 @@ fn run(pick: &dyn Fn(&str) -> bool) -> Vec<String> {
));
}
}
// Pixel shapes.
// Pixel shapes: a triangle covering the target, or for cases named
// with `EDGE` one whose diagonal crosses quads.
let vertices = [
-1.0, -1.0, 0.5, 1.0, 0.0, 1.0, //
-1.0, 3.0, 0.5, 1.0, 0.0, -1.0, //
3.0, -1.0, 0.5, 1.0, 2.0, 1.0,
];
let draw = |roots| Draw {
let half = [
-1.0, -1.0, 0.5, 1.0, 0.0, 1.0, //
-1.0, 1.1, 0.5, 1.0, 0.0, -0.05, //
1.1, -1.0, 0.5, 1.0, 1.05, 1.0,
];
let draw = |roots, edge: bool| Draw {
roots,
textures: &[],
samplers: &[],
vertices: &vertices,
vertices: if edge { &half } else { &vertices },
stride: 24,
size: (16, 16),
mesh_groups: 0,
@@ -481,10 +513,13 @@ fn run(pick: &dyn Fn(&str) -> bool) -> Vec<String> {
.draw(
&root_t,
&pso_t,
&draw(vec![
(0, Root::Constants(console_user_data(&vs_c))),
(1, Root::Constants(console_user_data(&ps_c))),
]),
&draw(
vec![
(0, Root::Constants(console_user_data(&vs_c))),
(1, Root::Constants(console_user_data(&ps_c))),
],
name.contains(EDGE),
),
)
.unwrap_or_else(|e| panic!("{name}: {label}: {e}"));
let (vs_p, ps_p) = (
@@ -499,7 +534,7 @@ fn run(pick: &dyn Fn(&str) -> bool) -> Vec<String> {
.draw(
&root_p,
&pso_p,
&draw(vec![(0, Root::Constants(vec![0; 4]))]),
&draw(vec![(0, Root::Constants(vec![0; 4]))], name.contains(EDGE)),
)
.unwrap();
let bad: Vec<usize> = (0..got.len()).filter(|&t| got[t] != want[t]).collect();
+6 -4
View File
@@ -2937,11 +2937,13 @@ impl<'a> Lower<'a> {
r
}
/// Whether a quad-local read must reach discarded lanes: after a pixel's
/// discard they are the host's helpers, which only quad reads see (wave
/// reads give 0 from them on AMD).
/// Whether a quad-local read must reach helper lanes: in a pixel program
/// every quad a triangle edge crosses has some (the console's whole-quad
/// mode runs them; discarded pixels stay helpers too), and only quad
/// reads see them on the host (wave reads give 0 from them on AMD: wrong
/// derivatives of computed values along every triangle edge).
fn helpers_read(&self, quad: bool) -> bool {
quad && self.s.pixel && !self.x.st.discards.is_empty()
quad && self.s.pixel
}
/// `v_readfirstlane_b32`'s result `first` of `own` in a pixel shader: