From 588a0e24a81908389d3790c19e46e47f8d487a03 Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:19:41 -0700 Subject: [PATCH 1/8] Shaders: a wave32 program's mask read picks its half of a 64-lane ballot For MaskData::Host (pixel and vertex stages, whose programs declare no WaveSize) a host wave may have 64 lanes; its upper half is a second console wave whose bits land in the ballot's second dword. mask_dword returned the first dword for every lane, so mask reads as data (s_bcnt1_i32_*, s_ff1_i32_*, v_mbcnt_*, s_cmp_eq/lg_u32/u64) gave the upper half's lanes the lower console wave's mask values. mask_dword now selects the ballot half by the reader's host lane. v_mbcnt_lo counts bits of the lane's own half below its lane in it, and v_mbcnt_hi is src1 alone on wave32, whose mask has no high half. Both are unchanged when the host wave is 32 lanes. Fixes #131. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-shader/src/emit/bitcode.rs | 6 ++ crates/victory-shader/src/emit/lower.rs | 95 ++++++++++++++++++++++- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/crates/victory-shader/src/emit/bitcode.rs b/crates/victory-shader/src/emit/bitcode.rs index 8b555adb..ee2fca6a 100644 --- a/crates/victory-shader/src/emit/bitcode.rs +++ b/crates/victory-shader/src/emit/bitcode.rs @@ -314,6 +314,12 @@ impl Module { V::Func(i) } + /// The names of the module's declared functions. + #[cfg(test)] + pub fn func_names(&self) -> impl Iterator { + self.funcs.iter().map(|f| f.name.as_str()) + } + /// An external global of type `ty` in address space `space` (DXIL /// declares thread-group shared memory this way). pub fn global(&mut self, name: &str, ty: Ty, space: u32) -> V { diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index ca5e8988..0415729d 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -37,6 +37,9 @@ pub(crate) struct Lowered { pub root_srvs: Vec, pub user_data: Vec, pub samplers: Vec<[u32; 4]>, + /// The names of the emitted functions (which dx.ops ran); tests only. + #[cfg(test)] + pub op_names: Vec, } pub(crate) struct Parts<'a> { @@ -883,6 +886,8 @@ impl<'a> Lower<'a> { let root_cbvs = self.s.root_cbvs.iter().map(|r| r.0).collect(); let root_srvs = self.s.root_srvs.iter().map(|r| r.0).collect(); let cmp_samplers = self.s.cmp_samplers; + #[cfg(test)] + let op_names = self.s.m.func_names().map(str::to_owned).collect(); Ok(Lowered { cmp_samplers, root_cbvs, @@ -890,6 +895,8 @@ impl<'a> Lower<'a> { dxil: self.s.finish(), user_data, samplers: self.samplers, + #[cfg(test)] + op_names, }) } @@ -1186,7 +1193,21 @@ impl<'a> Lower<'a> { xy } }; - Ok(if high { y } else { x }) + Ok(if high { + y + } else if self.mask_data == MaskData::Host && !self.x.native.wave64 { + // A wave32 program's host wave may still have 64 lanes, whose + // upper half is a second console wave: its bits are the ballot's + // second dword. + let lane = self.lane_index(); + let c = self.cur; + let k32 = self.s.m.i32(32); + let upper = self.s.m.cmp(c, pred::IUGE, lane, k32); + self.s.flags |= flags::WAVE_OPS; + self.s.m.select(c, upper, y, x) + } else { + x + }) } // ---- operands ---- @@ -4907,9 +4928,19 @@ impl<'a> Lower<'a> { } else if self.s.mesh.is_none() { self.cross_lane(false)?; } + // On a host wave wider than the console's the upper half is a + // second console wave: `lo` counts bits of the lane's own + // half's mask below its lane in it, and a wave32 mask's high + // half is empty. + let host_halves = self.mask_data == MaskData::Host && !self.x.native.wave64; let lane = self.lane_index(); let below = if name == "v_mbcnt_lo_u32_b32" { - lane + if host_halves { + let k31 = k(self, 31); + self.s.m.bin(c, Bin::And, lane, k31) + } else { + lane + } } else { let (k32, z) = (k(self, 32), k(self, 0)); let t = self.s.m.bin(c, Bin::Sub, lane, k32); @@ -4923,7 +4954,11 @@ impl<'a> Lower<'a> { let m = self.s.m.bin(c, Bin::Sub, m, one); let full = self.s.m.cmp(c, pred::IUGE, below, k32); let m = self.s.m.select(c, full, ones, m); - let x = self.s.m.bin(c, Bin::And, a, m); + let x = if host_halves && name == "v_mbcnt_hi_u32_b32" { + k(self, 0) + } else { + self.s.m.bin(c, Bin::And, a, m) + }; let n = self .s .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 31, &[x]); @@ -6630,6 +6665,60 @@ mod tests { } } + /// A wave32 graphics program's host wave may have 64 lanes, whose upper + /// half is a second console wave: a lane mask read as a number picks the + /// reader's half of the ballot, so the reader's lane index is needed. + #[test] + fn a_wave32_mask_read_takes_its_ballot_half() { + let words = [ + 0x7D8A_0480, // v_cmp_ne_u32 vcc, 0, v2 + 0xBE84_0F6A, // s_bcnt1_i32_b32 s4, vcc_lo + 0x7E08_0204, // v_mov_b32 v4, s4 + 0xF800_180F, + 0x0404_0404, // exp mrt0, v4, v4, v4, v4 done vm + S_ENDPGM, + ]; + let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let native = NativeProgram { + stage: HwStage::Ps, + wave64: false, + vgprs: 256, + layout: vec![], + used: vec![], + io: StageIo::Ps { + inputs: vec![], + input_addr: 0, + db_shader_control: 0, + patches: vec![], + }, + code, + }; + let p = ir::build(&native.code, false, 256).unwrap(); + let res = resource::track(&p, &native.layout, native.stage).unwrap(); + let io = io::raise(&p, &native).unwrap(); + let st = structure::structure(&p).unwrap(); + let lowered = lower(&Parts { + native: &native, + p: &p, + st: &st, + io: &io, + res: &res, + inputs: vec![], + outputs: vec![], + prims: vec![], + mesh_stride: 0, + }) + .unwrap(); + // Without the per-half pick, nothing reads the lane index here. + assert!( + lowered + .op_names + .iter() + .any(|n| n == "dx.op.waveGetLaneIndex"), + "the mask read should pick the lane's half of the ballot" + ); + } + #[test] fn buffer_formats_by_number() { // The numbers the disassembler names 8_8_8_8_UNORM, 16_16_SNORM, From 589de5b9625b0f48768164fb650a3f982ab5795d Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:05:08 -0700 Subject: [PATCH 2/8] Shaders: the ballot-half test checks the emitted ops, not just their names The wave32 mask-read test now asserts the pick's shape: an unsigned `lane >= 32` compare selecting the ballot's second dword over the first, `lane & 31` for v_mbcnt_lo, and a bit count of a constant zero for v_mbcnt_hi. A second test covers the paths that must not pick: wave64 programs (own dwords) and compute (the whole host wave is the console's). emit::bitcode::Module gains a test-only inst_lines dump to support that. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-shader/src/emit/bitcode.rs | 39 +++++ crates/victory-shader/src/emit/lower.rs | 178 ++++++++++++++++------ 2 files changed, 169 insertions(+), 48 deletions(-) diff --git a/crates/victory-shader/src/emit/bitcode.rs b/crates/victory-shader/src/emit/bitcode.rs index ee2fca6a..a17cb330 100644 --- a/crates/victory-shader/src/emit/bitcode.rs +++ b/crates/victory-shader/src/emit/bitcode.rs @@ -320,6 +320,45 @@ impl Module { self.funcs.iter().map(|f| f.name.as_str()) } + /// A line per instruction: its op and operands — `i7` an instruction, + /// `f2` a function, `k8` an integer constant, `c1` another constant, + /// `g3` a global. + #[cfg(test)] + pub fn inst_lines(&self) -> Vec { + let v = |m: &Self, x: V| -> String { + match x { + V::Inst(i) => format!("i{i}"), + V::Func(i) => format!("f{i}"), + V::Global(i) => format!("g{i}"), + V::Const(i) => match &m.consts[i as usize].1 { + Const::Int(n) => format!("k{n}"), + _ => format!("c{i}"), + }, + } + }; + self.insts + .iter() + .map(|i| match &i.op { + Op::Bin(op, a, b) => format!("bin:{op:?} {} {}", v(self, *a), v(self, *b)), + Op::Cast(op, a) => format!("cast:{op:?} {}", v(self, *a)), + Op::Cmp(p, a, b) => format!("cmp:{p} {} {}", v(self, *a), v(self, *b)), + Op::Select(c, t, f) => { + format!("select {} {} {}", v(self, *c), v(self, *t), v(self, *f)) + } + Op::Extract(a, i) => format!("extract {} {i}", v(self, *a)), + Op::Call(f, args) => { + let args = args + .iter() + .map(|&a| v(self, a)) + .collect::>() + .join(", "); + format!("call f{f} [{args}]") + } + op => format!("{op:?}"), + }) + .collect() + } + /// An external global of type `ty` in address space `space` (DXIL /// declares thread-group shared memory this way). pub fn global(&mut self, name: &str, ty: Ty, space: u32) -> V { diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 0415729d..f2781847 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -40,6 +40,9 @@ pub(crate) struct Lowered { /// The names of the emitted functions (which dx.ops ran); tests only. #[cfg(test)] pub op_names: Vec, + /// The emitted instructions, one line each; tests only. + #[cfg(test)] + pub insts: Vec, } pub(crate) struct Parts<'a> { @@ -888,6 +891,8 @@ impl<'a> Lower<'a> { let cmp_samplers = self.s.cmp_samplers; #[cfg(test)] let op_names = self.s.m.func_names().map(str::to_owned).collect(); + #[cfg(test)] + let insts = self.s.m.inst_lines(); Ok(Lowered { cmp_samplers, root_cbvs, @@ -897,6 +902,8 @@ impl<'a> Lower<'a> { samplers: self.samplers, #[cfg(test)] op_names, + #[cfg(test)] + insts, }) } @@ -6594,28 +6601,31 @@ impl<'a> Lower<'a> { #[cfg(test)] mod tests { - use super::{Numeric, Parts, buffer_format, lower}; + use super::{Lowered, Numeric, Parts, buffer_format, lower}; use crate::native::{ComputeIo, HwStage, NativeProgram, StageIo}; use crate::{io, ir, resource, structure}; const S_ENDPGM: u32 = 0xBF81_0000; - /// Lowers a compute program of constants: whether it translates. - fn lowers(words: &[u32]) -> Result<(), crate::ShaderError> { + /// Lowers `words` as a `stage` program (`wave64`: the console's wave + /// size) with no resources or inputs. + fn lower_program( + words: &[u32], + stage: HwStage, + wave64: bool, + io: StageIo, + ) -> Result { let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); let native = NativeProgram { - stage: HwStage::Cs, - wave64: false, + stage, + wave64, vgprs: 256, layout: vec![], used: vec![], - io: StageIo::Cs(ComputeIo { - rsrc2: 0, - threads: [1, 1, 1], - }), + io, code, }; - let p = ir::build(&native.code, false, 256).unwrap(); + let p = ir::build(&native.code, wave64, 256).unwrap(); let res = resource::track(&p, &native.layout, native.stage).unwrap(); let io = io::raise(&p, &native).unwrap(); let st = structure::structure(&p).unwrap(); @@ -6630,6 +6640,19 @@ mod tests { prims: vec![], mesh_stride: 0, }) + } + + /// Lowers a compute program of constants: whether it translates. + fn lowers(words: &[u32]) -> Result<(), crate::ShaderError> { + lower_program( + words, + HwStage::Cs, + false, + StageIo::Cs(ComputeIo { + rsrc2: 0, + threads: [1, 1, 1], + }), + ) .map(|_| ()) } @@ -6667,56 +6690,115 @@ mod tests { /// A wave32 graphics program's host wave may have 64 lanes, whose upper /// half is a second console wave: a lane mask read as a number picks the - /// reader's half of the ballot, so the reader's lane index is needed. + /// reader's half of the ballot. #[test] fn a_wave32_mask_read_takes_its_ballot_half() { let words = [ 0x7D8A_0480, // v_cmp_ne_u32 vcc, 0, v2 0xBE84_0F6A, // s_bcnt1_i32_b32 s4, vcc_lo + 0xBF07_806A, // s_cmp_lg_u32 vcc_lo, 0 0x7E08_0204, // v_mov_b32 v4, s4 + 0xD765_0005, + 0x0001_006A, // v_mbcnt_lo_u32_b32 v5, vcc_lo, 0 + 0xD766_0006, + 0x0002_0A6A, // v_mbcnt_hi_u32_b32 v6, vcc_lo, v5 + 0xBF85_0001, // s_cbranch_scc1 -> the export + S_ENDPGM, // no lane had its bit: done 0xF800_180F, - 0x0404_0404, // exp mrt0, v4, v4, v4, v4 done vm + 0x0406_0504, // exp mrt0, v4, v5, v6, v4 done vm S_ENDPGM, ]; - let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); - let native = NativeProgram { - stage: HwStage::Ps, - wave64: false, - vgprs: 256, - layout: vec![], - used: vec![], - io: StageIo::Ps { - inputs: vec![], - input_addr: 0, - db_shader_control: 0, - patches: vec![], - }, - code, - }; - let p = ir::build(&native.code, false, 256).unwrap(); - let res = resource::track(&p, &native.layout, native.stage).unwrap(); - let io = io::raise(&p, &native).unwrap(); - let st = structure::structure(&p).unwrap(); - let lowered = lower(&Parts { - native: &native, - p: &p, - st: &st, - io: &io, - res: &res, - inputs: vec![], - outputs: vec![], - prims: vec![], - mesh_stride: 0, - }) - .unwrap(); - // Without the per-half pick, nothing reads the lane index here. + let lowered = lower_program(&words, HwStage::Ps, false, pixel_io()).unwrap(); + let f = |name| lowered.op_names.iter().position(|n| n == name); + let (lane, ballot, bits) = ( + f("dx.op.waveGetLaneIndex").expect("no lane index for the pick"), + f("dx.op.waveActiveBallot").expect("no ballot for the mask read"), + f("dx.op.unaryBits.i32").expect("no bit count"), + ); + let l = &lowered.insts; + let lanes: Vec = l + .iter() + .enumerate() + .filter(|(_, i)| **i == format!("call f{lane} [k111]")) + .map(|(i, _)| i) + .collect(); + assert!(!lanes.is_empty(), "no lane index for the pick"); + let i_ballot = l + .iter() + .position(|i| i.starts_with(&format!("call f{ballot} [k116, "))) + .expect("no ballot"); + let (i_x, i_y) = ( + at(l, &format!("extract i{i_ballot} 0")), + at(l, &format!("extract i{i_ballot} 1")), + ); + // The pick: upper host lanes read the ballot's second dword. + let i_cmp = l + .iter() + .position(|i| { + i.starts_with("cmp:") && lanes.iter().any(|n| i.ends_with(&format!("i{n} k32"))) + }) + .expect("no `lane >= 32` compare"); + at(l, &format!("select i{i_cmp} i{i_y} i{i_x}")); + // v_mbcnt_lo counts below the lane in its own half; a wave32 mask's + // high half is empty, so v_mbcnt_hi's count is of no bits. assert!( - lowered - .op_names + lanes .iter() - .any(|n| n == "dx.op.waveGetLaneIndex"), - "the mask read should pick the lane's half of the ballot" + .any(|n| l.contains(&format!("bin:And i{n} k31"))), + "no `lane & 31` in {l:?}" ); + at(l, &format!("call f{bits} [k31, k0]")); + } + + /// The ballot-half pick is for wave32 graphics programs only: a wave64 + /// program's mask dwords are its own, and the console path (compute) is + /// the whole host wave already, so neither reads the lane index for it. + #[test] + fn only_host_wave32_reads_take_a_ballot_half() { + let words = [ + 0x7D8A_0480, // v_cmp_ne_u32 vcc, 0, v2 + 0xBE84_0F6A, // s_bcnt1_i32_b32 s4, vcc_lo + 0x7E08_0204, // v_mov_b32 v4, s4 + 0xF800_180F, + 0x0404_0404, // exp mrt0, v4, v4, v4, v4 done vm + S_ENDPGM, + ]; + let l64 = lower_program(&words, HwStage::Ps, true, pixel_io()).unwrap(); + assert!(!l64.op_names.iter().any(|n| n == "dx.op.waveGetLaneIndex")); + let words = [ + 0x7D8A_0480, // v_cmp_ne_u32 vcc, 0, v0 + 0xBE84_0F6A, // s_bcnt1_i32_b32 s4, vcc_lo + 0xBF07_8004, // s_cmp_lg_u32 s4, 0 + 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm + 0xBE85_0380, // s_mov_b32 s5, 0 + S_ENDPGM, + ]; + let cs = lower_program(&words, HwStage::Cs, false, compute_io()).unwrap(); + assert!(!cs.op_names.iter().any(|n| n == "dx.op.waveGetLaneIndex")); + } + + fn pixel_io() -> StageIo { + StageIo::Ps { + inputs: vec![], + input_addr: 0, + db_shader_control: 0, + patches: vec![], + } + } + + fn compute_io() -> StageIo { + StageIo::Cs(ComputeIo { + rsrc2: 0, + threads: [1, 1, 1], + }) + } + + /// The index of the instruction `pat` names in `lines`. + fn at(lines: &[String], pat: &str) -> usize { + lines + .iter() + .position(|l| l == pat) + .unwrap_or_else(|| panic!("no `{pat}` in {lines:?}")) } #[test] From 48f92e23c152fbef4f4434b8b9341e357e2220aa Mon Sep 17 00:00:00 2001 From: Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:10:33 -0700 Subject: [PATCH 3/8] Shaders: a second console wave's v_mbcnt_lo rank is a host lane Under MaskData::Host a wave32 program's lane numbers stay host lanes, so a v_mbcnt_lo rank of the upper half's console wave takes its 32-lane base: v_mbcnt_lo(-1) still reports the lane index, and the value composes with host-lane reads like v_readlane. The mask itself stays per console wave, so s_bcnt1, s_ff1 and s_cmp keep their own wave's count and the first wave's divergence never feeds a second wave's scalars. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-shader/src/emit/lower.rs | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index f2781847..0dd69097 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -4938,7 +4938,9 @@ impl<'a> Lower<'a> { // On a host wave wider than the console's the upper half is a // second console wave: `lo` counts bits of the lane's own // half's mask below its lane in it, and a wave32 mask's high - // half is empty. + // half is empty. The count is a lane number, and lane numbers + // stay host lanes: a second wave's rank takes its 32-lane + // base, so `v_mbcnt_lo(-1)` is still the lane index. let host_halves = self.mask_data == MaskData::Host && !self.x.native.wave64; let lane = self.lane_index(); let below = if name == "v_mbcnt_lo_u32_b32" { @@ -4969,6 +4971,12 @@ impl<'a> Lower<'a> { let n = self .s .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 31, &[x]); + let n = if host_halves && name == "v_mbcnt_lo_u32_b32" { + let base = self.s.m.bin(c, Bin::And, lane, k32); + self.s.m.bin(c, Bin::Add, n, base) + } else { + n + }; self.s.m.bin(c, Bin::Add, n, b) } _ => return Err(name), @@ -6690,7 +6698,8 @@ mod tests { /// A wave32 graphics program's host wave may have 64 lanes, whose upper /// half is a second console wave: a lane mask read as a number picks the - /// reader's half of the ballot. + /// reader's half of the ballot, and `v_mbcnt_lo` reports its rank as a + /// host lane number. #[test] fn a_wave32_mask_read_takes_its_ballot_half() { let words = [ @@ -6702,10 +6711,12 @@ mod tests { 0x0001_006A, // v_mbcnt_lo_u32_b32 v5, vcc_lo, 0 0xD766_0006, 0x0002_0A6A, // v_mbcnt_hi_u32_b32 v6, vcc_lo, v5 + 0xD765_0007, + 0x0001_00C1, // v_mbcnt_lo_u32_b32 v7, -1, 0 0xBF85_0001, // s_cbranch_scc1 -> the export S_ENDPGM, // no lane had its bit: done 0xF800_180F, - 0x0406_0504, // exp mrt0, v4, v5, v6, v4 done vm + 0x0706_0504, // exp mrt0, v4, v5, v6, v7 done vm S_ENDPGM, ]; let lowered = lower_program(&words, HwStage::Ps, false, pixel_io()).unwrap(); @@ -6748,6 +6759,29 @@ mod tests { "no `lane & 31` in {l:?}" ); at(l, &format!("call f{bits} [k31, k0]")); + // The count is a lane number, so a second wave's rank takes its + // 32-lane base: `v_mbcnt_lo(-1)` is the host lane index. + let bases: Vec = l + .iter() + .enumerate() + .filter(|(i, s)| { + s.starts_with("bin:And ") + && lanes.iter().any(|n| **s == format!("bin:And i{n} k32")) + }) + .map(|(i, _)| i) + .collect(); + assert!(!bases.is_empty(), "no `lane & 32` wave base in {l:?}"); + for base in &bases { + assert!( + l.iter().any(|s| { + s.starts_with("bin:Add ") + && s["bin:Add ".len()..] + .split(' ') + .any(|a| a == format!("i{base}")) + }), + "the wave base `i{base}` is never added in {l:?}" + ); + } } /// The ballot-half pick is for wave32 graphics programs only: a wave64 From 38234626c7c17cb6966a51c2ecba89e0bd1f78a7 Mon Sep 17 00:00:00 2001 From: Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:20:29 -0700 Subject: [PATCH 4/8] Shaders: lane-indexed ops on a wide host wave use the own console wave For MaskData::Host a wave32 program's 64-lane host wave is two console waves, but the lane-indexed ops either refused or read the wrong wave: - v_readlane at a constant lane errored ("a cross-lane operation outside compute"), even for wave64 programs where the lane is already a host lane; on wave32 the console's lane k of the lane's own wave is host lane k | (lane & 32). - v_writelane refused on every pixel and vertex program; on host lanes the equality is already host-space for a computed lane. - v_readfirstlane read the host wave's first active lane - the first console wave's lane - for lanes 32-63; it now reads the low set bit of the lane's own half of the exec ballot, renumbered to a host lane. Closes #593. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-shader/src/emit/lower.rs | 171 ++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 11 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 0dd69097..efcdddb4 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -1824,6 +1824,28 @@ impl<'a> Lower<'a> { .collect() } + /// The lane's own console wave's 32-lane base on a 64-lane host wave + /// (0 or 32): the renumbering a console lane number takes to stay a + /// host lane. + fn lane_base(&mut self) -> V { + let lane = self.lane_index(); + let k32 = self.s.m.i32(32); + self.s.m.bin(self.cur, Bin::And, lane, k32) + } + + /// A lane operand as a host lane: a constant one is the console's, so + /// on a host wave wider than the program's each half is a console wave + /// and console lane `k` of the lane's own wave is `k | (lane & 32)`. + /// On a wave64 program lanes already match one to one. + fn console_lane(&mut self, lane: V) -> V { + if matches!(lane, V::Const(_)) && !self.x.native.wave64 { + let base = self.lane_base(); + self.s.m.bin(self.cur, Bin::Or, lane, base) + } else { + lane + } + } + /// Code that depends on the lane layout: wave operations, and in compute /// the console's wave size (lanes are thread ids in group order there). /// Pixel lanes match only within a quad. @@ -3996,11 +4018,13 @@ impl<'a> Lower<'a> { let (a, lane) = (s(self, 0)?, s(self, 1)?); // A computed lane in a pixel or vertex program is a host lane // (from a mask or `v_mbcnt`); a constant one is the console's. - if self.mask_data == MaskData::Host && !matches!(lane, V::Const(_)) { + let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; + self.console_lane(lane) } else { self.cross_lane(false)?; - } + lane + }; let r = self.read_lane(a, lane); int(self, r) } @@ -4014,7 +4038,15 @@ impl<'a> Lower<'a> { Some(o) => self.i32_of(o)?, None => self.s.m.i32(0), }; - self.cross_lane(false)?; + // The lane space is `v_readlane`'s: a host lane, or a + // console's constant lane of the lane's own wave. + let lane = if self.mask_data == MaskData::Host { + self.s.flags |= flags::WAVE_OPS; + self.console_lane(lane) + } else { + self.cross_lane(false)?; + lane + }; let me = self.lane_index(); let is = self.s.m.cmp(c, pred::EQ, me, lane); let r = self.s.m.select(c, is, a, old); @@ -4023,14 +4055,28 @@ impl<'a> Lower<'a> { "v_readfirstlane_b32" => { let a = s(self, 0)?; self.s.flags |= flags::WAVE_OPS; - let r = self.s.op( - c, - "waveReadLaneFirst.i32", - Attr::NoUnwind, - self.s.t.i32, - 118, - &[a], - ); + let r = if self.mask_data == MaskData::Host && !self.x.native.wave64 { + // The first lane of the lane's own console wave: the low + // set bit of its half of the exec ballot, renumbered to + // a host lane. + let exec = inst.read(Reg::S(EXEC)).ok_or(LANE_MASK)?; + let d = self.mask_dword(exec)?; + let f = self + .s + .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 32, &[d]); + let base = self.lane_base(); + let first = self.s.m.bin(c, Bin::Or, f, base); + self.read_lane(a, first) + } else { + self.s.op( + c, + "waveReadLaneFirst.i32", + Attr::NoUnwind, + self.s.t.i32, + 118, + &[a], + ) + }; int(self, r) } _ => { @@ -6784,6 +6830,109 @@ mod tests { } } + /// On a 64-lane host wave a wave32 program's lane-indexed ops use the + /// lane's own console wave: a constant lane operand takes the wave's + /// 32-lane base, and the first lane comes from its half's ballot. + #[test] + fn a_wave32_lane_op_uses_its_own_console_wave() { + let words = [ + 0xD760_0004, + 0x0001_0B02, // v_readlane_b32 v4, v2, 5 + 0xD761_0005, + 0x0001_0A87, // v_writelane_b32 v5, 7, 5 + 0x7E0C_0502, // v_readfirstlane_b32 v6, v2 + 0x360E_0B04, // v_and_b32 v7, v4, v5 + 0x360E_0D07, // v_and_b32 v7, v7, v6 + 0x7D8A_0E80, // v_cmp_ne_u32 vcc, 0, v7 + 0xBF07_806A, // s_cmp_lg_u32 vcc_lo, 0 + 0xBF85_0001, // s_cbranch_scc1 -> the export + S_ENDPGM, + 0xF800_180F, + 0x0707_0707, // exp mrt0, v7, v7, v7, v7 done vm + S_ENDPGM, + ]; + let lowered = lower_program(&words, HwStage::Ps, false, pixel_io()).unwrap(); + let l = &lowered.insts; + let lane_calls: Vec = l + .iter() + .enumerate() + .filter(|(_, i)| i.starts_with("call f") && i.ends_with("[k111]")) + .map(|(i, _)| i) + .collect(); + assert!(!lane_calls.is_empty(), "no lane index in {l:?}"); + let bases: Vec = lane_calls + .iter() + .flat_map(|n| { + l.iter() + .enumerate() + .filter(|(_, i)| **i == format!("bin:And i{n} k32")) + .map(|(i, _)| i) + .collect::>() + }) + .collect(); + let used = |v: usize| -> bool { + l.iter() + .any(|i| i.ends_with(&format!("i{v}]")) || i.ends_with(&format!(" i{v}"))) + }; + // Console lane 5 of the lane's own wave is host lane 5 | (lane & 32), + // for both v_readlane (readLaneAt) and v_writelane (lane equality). + let ors: Vec = bases + .iter() + .filter_map(|b| l.iter().position(|i| i == &format!("bin:Or k5 i{b}"))) + .collect(); + assert_eq!(ors, Vec::from([2, 6]), "no `5 | base` host lanes in {l:?}"); + for or in &ors { + assert!(used(*or), "the host lane `i{or}` is never used in {l:?}"); + } + assert!( + l.iter() + .any(|i| i.starts_with("call f") && i.contains(&format!("k117, "))), + "no waveReadLaneAt in {l:?}" + ); + // v_readfirstlane takes the low set bit of the lane's own half of + // the exec ballot, renumbered to a host lane. + let ballot = l + .iter() + .position(|i| i.starts_with("call f") && i.contains("[k116, ")) + .expect("no exec ballot"); + let (x, y) = ( + at(l, &format!("extract i{ballot} 0")), + at(l, &format!("extract i{ballot} 1")), + ); + let own = l + .iter() + .position(|i| i.starts_with("select ") && i.ends_with(&format!("i{y} i{x}"))) + .expect("no own-half pick"); + let first = l + .iter() + .position(|i| i.starts_with("call f") && i.contains(&format!("k32, i{own}"))) + .expect("no first bit of the own half"); + let host_first = bases + .iter() + .find_map(|b| l.iter().position(|i| i == &format!("bin:Or i{first} i{b}"))) + .expect("the first lane is not renumbered"); + assert!(used(host_first), "no read at the first host lane in {l:?}"); + + // On a wave64 program console lanes are host lanes one to one: the + // same reads need no renumbering at all. + let l64 = lower_program(&words, HwStage::Ps, true, pixel_io()).unwrap(); + let l = &l64.insts; + assert!( + !l.iter().any(|i| i.starts_with("bin:Or ")), + "a wave64 lane operand is renumbered in {l:?}" + ); + assert!( + l.iter() + .any(|i| i.starts_with("call f") && i.contains("k117, ") && i.contains("k5]")), + "no direct waveReadLaneAt of lane 5 in {l:?}" + ); + assert!( + l.iter() + .any(|i| i.starts_with("call f") && i.contains("k118, ")), + "a wave64 first lane is not waveReadLaneFirst in {l:?}" + ); + } + /// The ballot-half pick is for wave32 graphics programs only: a wave64 /// program's mask dwords are its own, and the console path (compute) is /// the whole host wave already, so neither reads the lane index for it. From bed0cc1baa35594b20cb910eff499a75775f46aa Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 18:28:41 +0100 Subject: [PATCH 5/8] Shaders: cross-lane operations lower in pixel and vertex programs `Lower::cross_lane` refused every wave operation outside compute unless it was one of the quad-only ones in a pixel shader, which dropped whole draws of the 48 archive pixel programs that use `ds_bpermute`, `ds_swizzle`, a constant-lane `v_readlane`/`v_writelane` or `v_mbcnt` of a mask read as data ("a cross-lane operation outside compute"). The lowering already emits wave intrinsics that every stage has (SM 6.6: waveGetLaneIndex, waveActiveBallot, waveReadLaneAt, wavePrefixCountBits are valid in pixel and vertex shaders), and they work in host lane space, which is the console's when the host wave is the program's (32 or 64). Pixel helper lanes are host lanes too: they take part in quads and wave operations, and what they compute is never written. Mesh shaders remain refused (no wave size in SM 6.6), and compute keeps its console-lane relabelling. Test: `cross_lane_operations_lower_in_a_pixel_program` lowers one synthetic pixel program per op (constant-lane readlane/writelane, mbcnt of an all-ones mask, ds_bpermute with an offset, ds_swizzle of a quad) and asserts each translates to DXIL; with the previous gate the permute case is refused with exactly that error. --- crates/victory-shader/src/emit/lower.rs | 20 +++++-- crates/victory-shader/src/emit/mod.rs | 79 +++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index efd1ff9d..ad60d095 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -61,11 +61,14 @@ pub(crate) struct Lowered { /// Test builds: the property words of each handle annotation, in order. #[cfg(test)] pub annotations: Vec<(u32, u32)>, - /// The names of the emitted functions (which dx.ops ran); tests only. + /// The names of the emitted functions (which dx.ops ran); tests only, + /// kept from #599 for tests of lane ops (none reads them here yet). #[cfg(test)] + #[allow(dead_code)] pub op_names: Vec, - /// The emitted instructions, one line each; tests only. + /// The emitted instructions, one line each; tests only (see above). #[cfg(test)] + #[allow(dead_code)] pub insts: Vec, } @@ -2441,9 +2444,16 @@ impl<'a> Lower<'a> { // memory if mesh programs turn out to need these. return Err("a cross-lane operation in a mesh shader"); } - if self.s.threads.is_none() && !(quad && self.s.pixel) { - return Err("a cross-lane operation outside compute"); - } + // Compute, pixel and vertex programs all have the console's lanes on + // the host: a compute lane is relabelled to its console lane, and a + // pixel or vertex wave is lane for lane the console's when the host + // wave is the program's (the target is 32-64 lanes). `quad` marks the + // operations whose lanes all lie in one quad; the host has quads in + // every stage they are used in. A pixel shader's helper lanes are host + // lanes too: they take part in quads and in wave operations, so a read + // of one is what the host makes it, and what they compute is never + // written. + let _ = quad; debug_assert!(self.s.threads.is_none() || self.console_lanes); self.s.flags |= flags::WAVE_OPS; Ok(()) diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 158b354a..d07c1a30 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -2151,4 +2151,83 @@ mod barycentric_tests { translate(opcode); } } + /// Cross-lane operations outside compute: a pixel program's lane-indexed + /// reads and writes, its mask bit count, its byte permute and its + /// swizzle all lower. They were refused with "a cross-lane operation + /// outside compute", which dropped every draw of the program. + #[test] + fn cross_lane_operations_lower_in_a_pixel_program() { + let target = SigElement { + name: "SV_Target".into(), + index: 0, + kind: 16, + comp: 9, + interp: 0, + row: Some(0), + col: 0, + cols: 1, + usage: 1, + half: false, + }; + let sig = super::container::signature(&[target], false); + let bytes = crate::dxbc::build(&[(FourCC::OSG1, &sig)]); + let c = Container::parse(&bytes).unwrap(); + let pixel = |words: &[u32]| -> Result { + let native = NativeProgram { + stage: HwStage::Ps, + wave64: false, + vgprs: 8, + layout: vec![], + used: vec![], + io: StageIo::Ps { + inputs: vec![], + input_addr: 0, + db_shader_control: 0, + patches: vec![], + }, + code: words.iter().flat_map(|w| w.to_le_bytes()).collect(), + }; + let p = ir::build(&native.patched_code(), native.wave64, native.vgprs).unwrap(); + let st = structure::structure(&p).unwrap(); + let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap(); + let io = io::raise(&p, &native).unwrap(); + emit(&c, &native, &p, &st, &io, &res, Target::ANY) + }; + // exp mrt0 of v, done vm, then s_endpgm. + let export = |reg: u32| [0xF800_180F, reg << 24 | reg << 16 | reg << 8 | reg, S_ENDPGM]; + let cases: [(&str, Vec); 5] = [ + ( + // v_readlane_b32 v4, v2, 5 + "v_readlane_b32 of a constant lane", + [vec![0x7E04_02F2, 0xD760_0004, 0x0001_0B02], export(4).to_vec()].concat(), + ), + ( + // v_writelane_b32 v5, 7, 5 + "v_writelane_b32 of a constant lane", + [vec![0x7E04_02F2, 0xD761_0005, 0x0001_0A87], export(5).to_vec()].concat(), + ), + ( + // v_mbcnt_lo_u32_b32 v5, -1, 0: the lane index + "v_mbcnt_lo_u32_b32 of an all-ones mask", + [vec![0xD765_0005, 0x0001_00C1], export(5).to_vec()].concat(), + ), + ( + // ds_bpermute_b32 v3, v2, v1 offset:12 + "ds_bpermute_b32", + [vec![0xDACC_000C, 0x0300_0102], export(3).to_vec()].concat(), + ), + ( + // ds_swizzle_b32 v3, v2 offset:0x8000 (a quad pattern) + "ds_swizzle_b32 of a quad", + [vec![0xD8D4_8000, 0x0300_0102], export(3).to_vec()].concat(), + ), + ]; + for (name, words) in cases { + let shader = match pixel(&words) { + Ok(shader) => shader, + Err(e) => panic!("{name}: refused: {e:?}"), + }; + assert!(!shader.dxil.is_empty(), "{name}: no DXIL"); + } + } } From b857fbfc67897d360218805cf76b3843304ad3c9 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 19:18:21 +0100 Subject: [PATCH 6/8] Shaders: lane-indexed review fixes (replay lane mapping, two refusals, tighter test) - `rel_offset`'s `v_readlane_b32` replay now maps a constant lane as the main arm does (`console_lane_operand`: the console lane of the lane's own wave), so a descriptor address taken through a lane read cannot come from the other half's offset. Computed host lanes are unchanged. - A constant lane a host wave narrower than the program's does not hold (a wave64 program's lane 40 on a 32-lane host) is refused for graphics stages: `WaveReadLaneAt` of a missing lane is undefined, and no half exchange exists outside compute. - `ds_swizzle_b32` keeps only its quad-local patterns in a pixel shader: a pattern that reads another quad can name a helper lane, and SM 6.6 wave operations leave those out (helper participation is 6.7). `ds_bpermute` is unaffected (it zeroes a lane the ballot does not hold). - The pixel cross-lane test moves `s4` to `v4` before exporting it (`v_readlane_b32` writes an SGPR): the export now sees the reading itself. --- crates/victory-shader/src/emit/lower.rs | 35 ++++++++++++++- crates/victory-shader/src/emit/mod.rs | 59 ++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 080f77f9..93abd680 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -2466,6 +2466,21 @@ impl<'a> Lower<'a> { } } + /// Refuses a constant lane that a host wave narrower than the program's + /// does not hold (a wave64 program's lane 40 on a 32-lane host): a + /// `WaveReadLaneAt` of a lane the wave does not have is undefined. No + /// emulation exists for those lanes yet (compute exchanges halves; + /// graphics stages have no such path). + fn lane_operand_checked(&self, lane: V) -> Result<(), Why> { + if self.s.threads.is_none() + && self.x.native.wave64 + && matches!(lane, V::Const(k) if k >= self.x.target.lanes_max) + { + return Err("a wave64 lane on a host wave narrower than the program's"); + } + Ok(()) + } + /// Code that depends on the lane layout: wave operations, and in compute /// the console's wave size (lanes are thread ids in group order there). /// Pixel lanes match only within a quad. @@ -2803,6 +2818,15 @@ impl<'a> Lower<'a> { // console's, so quad-local swizzles read through quad operations. let mesh_quad = self.s.mesh.is_some() && quad; if !mesh_quad { + // A pixel shader has helper lanes at a primitive's edge, and a + // wave operation leaves them out: a swizzle that reads another + // quad can name one, and `WaveReadLaneAt` of a helper lane is + // undefined in SM 6.6 (helper participation is 6.7). Only the + // quad-local patterns are taken; `ds_bpermute` is safe because + // it zeroes a lane its ballot does not hold. + if self.s.pixel && !quad { + return Err("a swizzle across quads in a pixel shader"); + } self.cross_lane(quad)?; } // The source sits in the address field. @@ -4653,11 +4677,16 @@ impl<'a> Lower<'a> { } else { let a = self.rel_offset_op(inst, op0, base, local)?; let lane = self.operand(inst, op1)?; - if self.mask_data == MaskData::Host && !matches!(lane, V::Const(_)) { + // The lane space is the main arm's: a computed + // one is a host lane, a constant one the console's + // of the lane's own wave. + let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; + self.console_lane_operand(lane) } else { self.cross_lane(false)?; - } + lane + }; if self.console_cf { self.console_read_lane(a, lane) } else { @@ -5922,6 +5951,7 @@ impl<'a> Lower<'a> { let (a, lane) = (s(self, 0)?, s(self, 1)?); // A computed lane in a pixel or vertex program is a host lane // (from a mask or `v_mbcnt`); a constant one is the console's. + self.lane_operand_checked(lane)?; let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; self.console_lane_operand(lane) @@ -5950,6 +5980,7 @@ impl<'a> Lower<'a> { }; // The lane space is `v_readlane`'s: a host lane, or a // console's constant lane of the lane's own wave. + self.lane_operand_checked(lane)?; let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; self.console_lane_operand(lane) diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index f6ed2b7a..8e0d69e9 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -2212,9 +2212,13 @@ mod barycentric_tests { let export = |reg: u32| [0xF800_180F, reg << 24 | reg << 16 | reg << 8 | reg, S_ENDPGM]; let cases: [(&str, Vec); 5] = [ ( - // v_readlane_b32 v4, v2, 5 + // v_readlane_b32 s4, v2, 5; v_mov_b32 v4, s4 "v_readlane_b32 of a constant lane", - [vec![0x7E04_02F2, 0xD760_0004, 0x0001_0B02], export(4).to_vec()].concat(), + [ + vec![0x7E04_02F2, 0xD760_0004, 0x0001_0B02, 0x7E08_0204], + export(4).to_vec(), + ] + .concat(), ), ( // v_writelane_b32 v5, 7, 5 @@ -2245,4 +2249,55 @@ mod barycentric_tests { assert!(!shader.dxil.is_empty(), "{name}: no DXIL"); } } + /// A swizzle across quads stays refused in a pixel shader: a helper lane + /// at a primitive's edge can be its source, and wave operations leave + /// those out in SM 6.6. (`lane_operand_checked`'s wave64 refusal has no + /// test: a synthetic wave64 pixel program needs the console's EXEC + /// conventions first, which no test builds yet.) + #[test] + fn a_pixel_swizzle_across_quads_is_refused() { + let target = SigElement { + name: "SV_Target".into(), + index: 0, + kind: 16, + comp: 9, + interp: 0, + row: Some(0), + col: 0, + cols: 1, + usage: 1, + half: false, + }; + let sig = super::container::signature(&[target], false); + let bytes = crate::dxbc::build(&[(FourCC::OSG1, &sig)]); + let c = Container::parse(&bytes).unwrap(); + let lower = |words: &[u32], wave64: bool, lanes: Target| -> Result { + let native = NativeProgram { + stage: HwStage::Ps, + wave64, + vgprs: 8, + layout: vec![], + used: vec![], + io: StageIo::Ps { + inputs: vec![], + input_addr: 0, + db_shader_control: 0, + patches: vec![], + }, + code: words.iter().flat_map(|w| w.to_le_bytes()).collect(), + }; + let p = ir::build(&native.patched_code(), native.wave64, native.vgprs).unwrap(); + let st = structure::structure(&p).unwrap(); + let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap(); + let io = io::raise(&p, &native).unwrap(); + emit(&c, &native, &p, &st, &io, &res, lanes) + }; + // ds_swizzle_b32 v3, v2 offset:0x7C00 (xor 31: lanes of other quads). + let swizzle = [0xD8D4_7C00, 0x0300_0102, 0xF800_180F, 0x0303_0303, S_ENDPGM]; + let why = lower(&swizzle, false, Target::ANY).expect_err("a cross-quad swizzle"); + assert!( + format!("{why:?}").contains("a swizzle across quads in a pixel shader"), + "{why:?}" + ); + } } From 7490a4ab6628570ce88f0da8724c57e215a27777 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 19:24:21 +0100 Subject: [PATCH 7/8] Shaders: the pixel test's DXIL check compiles clean on Linux too --- crates/victory-shader/src/emit/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 8e0d69e9..e47e0af4 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -2153,13 +2153,17 @@ mod barycentric_tests { let res = resource::track(&p, &native.layout, native.stage, None).unwrap(); let io = io::raise(&p, &native).unwrap(); let st = structure::structure(&p).unwrap(); - let mut host = emit(&c, &native, &p, &st, &io, &res, Target::ANY).unwrap(); + let host = emit(&c, &native, &p, &st, &io, &res, Target::ANY).unwrap(); #[cfg(windows)] if let Ok(mut v) = super::validator::Validator::load( super::validator::Validator::default_path().as_deref(), ) { - v.validate(&mut host.dxil).unwrap(); + let mut copy = host.dxil.clone(); + v.validate(&mut copy).unwrap(); } + // Off Windows there is no dxil.dll: the DXIL is what is checked. + #[cfg(not(windows))] + assert!(!host.dxil.is_empty()); } for opcode in [0xCC21_000B, 0xCC22_000B] { From b77c8cd50b814d353389520ddac7ea956f142f63 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 19:36:33 +0100 Subject: [PATCH 8/8] Shaders: prove the lane a graphics cross-lane op reads, or refuse it Two review findings changed which lane a value comes from in the default translation; both are now closed by proof or refusal instead of being deferred. - A lane operand of a pixel or vertex program must be a constant: it is mapped to the console lane of the lane's own wave (`k | (lane & 32)` on a 64-lane host). A computed one is refused ("a computed lane in a pixel or vertex program"): nothing in the value lattice marks whether it is a host lane (`v_mbcnt`'s rank is) or an index within a 32-lane dword (a bit scan of a mask read as data is, and that one was off by 32 in the upper half). Compute keeps its own rule, where console lanes relabel the wave. - The address replay of a `v_readfirstlane_b32` that ran in every lane now reads the first lane of the lane's own console wave in a graphics program on a wider host (`lane_base`), not lane 0: the load's EXEC does not change which lane the definition read. Before this branch both cases were refused in graphics stages, so the change only ever narrows what is accepted: no program that translated before translates differently. --- crates/victory-shader/src/emit/lower.rs | 42 ++++++++++++++++++------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 93abd680..c0acbaf1 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -2453,11 +2453,23 @@ impl<'a> Lower<'a> { self.s.m.bin(self.cur, Bin::And, lane, k32) } - /// A lane operand as a host lane: a constant one is the console's, so - /// on a host wave wider than the program's each half is a console wave - /// and console lane `k` of the lane's own wave is `k | (lane & 32)`. - /// On a wave64 program lanes already match one to one. - fn console_lane_operand(&mut self, lane: V) -> V { + /// A lane operand of a graphics program: a constant one is the console's, + /// so on a host wave wider than the program's each half is a console wave + /// and console lane `k` of the lane's own wave is `k | (lane & 32)`. A + /// computed one is refused: nothing marks whether it is a host lane + /// (`v_mbcnt`'s rank is) or an index within a 32-lane dword (a bit scan + /// of a mask read as data is), and lowering the wrong one reads another + /// lane. Compute keeps its own rule (console lanes relabel the wave). + fn console_lane_operand(&mut self, lane: V) -> Result { + if self.s.threads.is_none() && !matches!(lane, V::Const(_)) { + return Err("a computed lane in a pixel or vertex program"); + } + Ok(self.console_lane_operand_const(lane)) + } + + /// [`console_lane_operand`](Self::console_lane_operand)'s mapping, for a + /// lane already known to be the console's. + fn console_lane_operand_const(&mut self, lane: V) -> V { if matches!(lane, V::Const(_)) && !self.x.native.wave64 { let base = self.lane_base(); self.s.m.bin(self.cur, Bin::Or, lane, base) @@ -4638,11 +4650,17 @@ impl<'a> Lower<'a> { self.rel_offset(sv, base, local)? } else if !self.split && self.runs_in_every_lane(v) { // The definition ran in every lane, so the lane it - // read was lane 0: the load's EXEC (possibly - // narrower, and not the definition's) does not - // change which lane that was. + // read was the wave's first: lane 0, or on a host + // wave wider than a graphics program's the base of + // the lane's own console wave. The load's EXEC + // (possibly narrower, and not the definition's) + // does not change which lane that was. let a = self.rel_offset_op(inst, op0, base, local)?; - let z = self.s.m.i32(0); + let z = if self.s.threads.is_none() && !self.x.native.wave64 { + self.lane_base() + } else { + self.s.m.i32(0) + }; self.read_lane(a, z) } else if self.console_cf { // The console wave's first lane, as `valu_op` @@ -4682,7 +4700,7 @@ impl<'a> Lower<'a> { // of the lane's own wave. let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; - self.console_lane_operand(lane) + self.console_lane_operand(lane)? } else { self.cross_lane(false)?; lane @@ -5954,7 +5972,7 @@ impl<'a> Lower<'a> { self.lane_operand_checked(lane)?; let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; - self.console_lane_operand(lane) + self.console_lane_operand(lane)? } else { self.cross_lane(false)?; // A constant lane is the console's own, and the console's @@ -5983,7 +6001,7 @@ impl<'a> Lower<'a> { self.lane_operand_checked(lane)?; let lane = if self.mask_data == MaskData::Host { self.s.flags |= flags::WAVE_OPS; - self.console_lane_operand(lane) + self.console_lane_operand(lane)? } else { lane };