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

+147
View File
@@ -1624,6 +1624,153 @@ mod barycentric_tests {
validate(&shader);
}
#[test]
fn a_standalone_first_half_reads_the_vertices() {
let shader = first_half_pixel(false);
let (_, inputs) = parts(&shader);
assert_eq!(inputs.iter().filter(|e| e.kind == 28).count(), 1);
assert_eq!(texcoord_mode(&shader), 1);
validate(&shader);
}
#[test]
fn a_first_half_read_beside_its_pair_keeps_its_partial_value() {
let shader = first_half_pixel(true);
assert_eq!(texcoord_mode(&shader), 1);
validate(&shader);
}
fn first_half_pixel(paired: bool) -> HostShader {
let [e0, e1] = exp_mrt0(2);
let mut words = vec![0xC808_0000]; // v_interp_p1_f32 v2, v0, attr0.x
if paired {
words.extend([
0x7E06_0302, // v_mov_b32 v3, v2 (keep the partial value)
0xC809_0001, // v_interp_p2_f32 v2, v1, attr0.x
v_add_f32(2, 2, 3),
]);
}
words.extend([e0, e1, 0xBF81_0000]);
pixel(&words, PERSP_CENTER).unwrap()
}
#[test]
#[cfg(windows)]
fn first_half_values_render_on_hardware_and_warp() {
use super::bitcode::{Attr, Term};
use super::host::{Draw, Host};
use super::shader::Shader;
let _one = super::valu_tests::ONE_AT_A_TIME
.lock()
.unwrap_or_else(|e| e.into_inner());
// Pass POSITION and TEXCOORD through, with TEXCOORD in the same
// output row as the translated pixel shader's input signature.
let mut s = Shader::new(false);
s.inputs = vec![element("POSITION", 0, 0), element("TEXCOORD", 0, 1)];
for e in &mut s.inputs {
e.usage = 15;
}
s.outputs = vec![element("TEXCOORD", 0, 0), element("SV_Position", 3, 1)];
for (i, e) in s.outputs.iter_mut().enumerate() {
e.usage = 15;
e.interp = if i == 1 { 4 } else { 2 };
}
for row in 0..2 {
for col in 0..4 {
let args = [s.m.i32(row), s.m.i32(0), s.m.i8(col), s.m.undef(s.t.i32)];
let v = s.op(s.entry, "loadInput.f32", Attr::ReadNone, s.t.f32, 4, &args);
let args = [s.m.i32(1 - row), s.m.i32(0), s.m.i8(col), v];
s.op(
s.entry,
"storeOutput.f32",
Attr::NoUnwind,
s.t.void,
5,
&args,
);
}
}
s.m.terminate(s.entry, Term::Ret);
let vs = HostShader {
stage: HwStage::Vs,
dxil: s.finish(),
user_data: Vec::new(),
samplers: Vec::new(),
static_samplers: Vec::new(),
root_cbvs: Vec::new(),
root_srvs: Vec::new(),
root_uavs: Vec::new(),
root_indexed_views: Vec::new(),
};
validate(&vs);
// One triangle covers an 8x8 target, with attribute values 2, 6, 10.
let vertices = [
-1.0, 1.0, 0.5, 1.0, 2.0, 0.0, 0.0, 0.0, -1.0, -3.0, 0.5, 1.0, 6.0, 0.0, 0.0, 0.0, 3.0,
1.0, 0.5, 1.0, 10.0, 0.0, 0.0, 0.0,
];
let mut ran = 0;
for (backend, warp) in [("hardware", false), ("WARP", true)] {
let Ok(host) = Host::new(warp) else { continue };
let mut options =
windows::Win32::Graphics::Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS3::default();
// SAFETY: a correctly sized output for the feature query.
unsafe {
host.device
.CheckFeatureSupport(
windows::Win32::Graphics::Direct3D12::D3D12_FEATURE_D3D12_OPTIONS3,
(&raw mut options).cast(),
size_of_val(&options) as u32,
)
.unwrap();
}
if !options.BarycentricsSupported.as_bool() {
eprintln!("no barycentrics on {backend}; skipped");
continue;
}
for paired in [false, true] {
let ps = first_half_pixel(paired);
validate(&ps);
let (root, pso) = host.pipeline(&vs, &ps).expect("interpolation pipeline");
let out = host
.draw(
&root,
&pso,
&Draw {
roots: Vec::new(),
textures: &[],
samplers: &[],
vertices: &vertices,
stride: 32,
size: (8, 8),
mesh_groups: 0,
},
)
.expect("draw partial interpolation");
for (n, texel) in out.iter().enumerate() {
let i = (n / 8) as f32 / 16.0 + 0.5 / 16.0;
let j = (n % 8) as f32 / 16.0 + 0.5 / 16.0;
let partial = 2.0 + i * 4.0;
let want = if paired {
partial + 2.0 + i * 4.0 + j * 8.0
} else {
partial
};
for &got in texel {
assert!(
(got - want).abs() < 1e-5,
"{backend} paired={paired} pixel={n}: got {got}, want {want}"
);
}
}
}
ran += 1;
eprintln!("partial interpolation renders correctly on {backend}");
}
if ran == 0 {
eprintln!("no SM 6.6 host with barycentrics; GPU checks skipped");
}
}
/// Both: the attribute is interpolated and J is used as a value.
#[test]
fn interpolation_and_a_barycentric_value_together() {
+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]| {