Shaders: retain live interpolation first halves

This commit is contained in:
jaschinski97-ops committed 2026-11-24 21:57:30 +01:00
1 parent 29f9e89df6
commit cad2f292b9
2 files changed
+239 -2

No files matched your search

+92 -2
View File
@@ -369,6 +369,7 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result<Io, ShaderError> {
defs: &defs,
phis: &phis,
};
let mut first_halves = Vec::new();
for (b, block) in p.blocks.iter().enumerate() {
for (i, inst) in block.insts.iter().enumerate() {
let site = (b, i);
@@ -382,7 +383,7 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result<Io, ShaderError> {
io.interps
.push((site, Interp::Vertex { attribute, vertex }));
}
("v_interp_p1_f32", _) => {}
("v_interp_p1_f32", _) => first_halves.push((site, attribute)),
("v_interp_p2_f32", Operand::Vector { reg, .. }) => {
let Operand::Vector { reg: d, .. } = *dst else {
return Err(unsupported(at, "an interpolation operand"));
@@ -402,7 +403,9 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result<Io, ShaderError> {
}
}
Fields::Valu(v) if name.starts_with("v_interp_") => match name {
"v_interp_p1ll_f16" => {}
"v_interp_p1ll_f16" => {
first_halves.push((site, attribute(inst, &v.src[0])?));
}
"v_interp_p2_f16" => {
let attribute = attribute(inst, &v.src[0])?;
let read = |k: usize| match v.src.get(k) {
@@ -460,6 +463,49 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result<Io, ShaderError> {
}
}
}
// A loaded i/j pair normally becomes one host interpolation: its first
// half need not be evaluated. A first half also read as data (or used
// without a second half) must keep P0 + i * P10. Follow phi uses too,
// without forcing ordinary pairs to read per-vertex attributes.
let folded: std::collections::HashSet<_> = io
.interps
.iter()
.filter_map(|(s, i)| matches!(i, Interp::Interpolated { .. }).then_some(*s))
.collect();
let mut used: std::collections::HashSet<_> = p
.blocks
.iter()
.enumerate()
.flat_map(|(b, blk)| {
blk.insts
.iter()
.enumerate()
.map(move |(i, inst)| ((b, i), inst))
})
.filter(|(s, _)| !folded.contains(s))
.flat_map(|(_, inst)| inst.reads.iter().map(|r| r.1))
.collect();
loop {
let before = used.len();
for phi in p.blocks.iter().flat_map(|b| &b.phis) {
if used.contains(&phi.value) {
used.extend(&phi.args);
}
}
if used.len() == before {
break;
}
}
for (site, attribute) in first_halves {
if !io.interps.iter().any(|(s, _)| *s == site)
&& p.blocks[site.0].insts[site.1]
.writes
.iter()
.any(|w| used.contains(&w.1))
{
io.interps.push((site, Interp::Weighted { attribute }));
}
}
Ok(io)
}
@@ -725,6 +771,50 @@ mod tests {
);
}
#[test]
fn first_halves_read_through_a_phi_keep_their_partial_values() {
let p = program(&[
0xBF06_8000, // s_cmp_eq_u32 s0, 0
0xBF85_0002, // s_cbranch_scc1 -> second first half
0xC808_0000, // v_interp_p1_f32 v2, v0, attr0.x
0xBF82_0001, // s_branch -> export
0xC808_0000, // v_interp_p1_f32 v2, v0, attr0.x
0xF800_180F,
0x0202_0202, // exp mrt0, v2, v2, v2, v2 done vm
S_ENDPGM,
]);
let io = raise(&p, &pixel(vec![TEXCOORD], 2)).unwrap();
assert_eq!(io.interps.len(), 2);
assert!(
io.interps
.iter()
.all(|(_, i)| matches!(i, Interp::Weighted { .. }))
);
}
#[test]
fn a_standalone_f16_first_half_is_weighted() {
let p = program(&[
0xD742_0002,
0x0002_0000, // v_interp_p1ll_f16 v2, v0, attr0.x
0xF800_180F,
0x0202_0202,
S_ENDPGM,
]);
let io = raise(
&p,
&pixel(
vec![PsInput {
fp16: true,
..TEXCOORD
}],
2,
),
)
.unwrap();
assert!(matches!(&io.interps[..], [(_, Interp::Weighted { .. })]));
}
#[test]
fn mesh_inputs_and_primitive_export() {
let mesh = |words: &[u32]| {