diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 26f4d56..c5fbd80 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -3313,7 +3313,12 @@ impl<'a> Lower<'a> { return Ok(h); } let index = self.heap_index(*load, *first, sampler)?; - let h = self.s.heap_handle(self.cur, index, sampler, false, props); + // A waterfall loop's load at the first lane's offset reads + // each lane's own entry (see `resource::Waterfall`). + let per_lane = self.x.res.waterfall.loads.contains(load); + let h = self + .s + .heap_handle(self.cur, index, sampler, per_lane, props); self.handles.insert(key, h); Ok(h) } @@ -7071,12 +7076,12 @@ mod tests { 0xBE83_0380, // s_mov_b32 s3, 0 S_ENDPGM, ]; + for words in [add, sub] { lowers(&words, 0).unwrap(); } } - fn perm_features(selector: u32) -> u64 { // Synthetic compute shader: // v0 is the thread ID and can supply a dynamic selector. diff --git a/crates/victory-shader/src/emit/waterfall_tests.rs b/crates/victory-shader/src/emit/waterfall_tests.rs index 72e0b95..104fc6d 100644 --- a/crates/victory-shader/src/emit/waterfall_tests.rs +++ b/crates/victory-shader/src/emit/waterfall_tests.rs @@ -220,6 +220,19 @@ fn lanes_load_from_the_buffer_their_nested_arms_chose() { } } +#[test] +fn lanes_sample_the_entry_their_offset_names_in_the_console_waterfall() { + use crate::resource::tests::WATERFALL_INDEXED_WAVE32; + let shader = build(WATERFALL_INDEXED_WAVE32, false); + let results = sampled(&shader); + assert!(!results.is_empty()); + for (label, red) in results { + // Even lanes hold entry 0's offset, odd lanes entry 1's. + let want: Vec = (0..32).map(|l| [1.0, 2.0][l & 1]).collect(); + assert_eq!(red, want, "{label}"); + } +} + #[test] fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() { use crate::resource::tests::cmpx_waterfall; diff --git a/crates/victory-shader/src/resource.rs b/crates/victory-shader/src/resource.rs index 0276aa3..62ddd5d 100644 --- a/crates/victory-shader/src/resource.rs +++ b/crates/victory-shader/src/resource.rs @@ -133,6 +133,9 @@ pub struct Waterfall { /// The `v_readfirstlane_b32` results they compare, each the lane's own /// source dword. pub reads: Vec, + /// Descriptor loads at a table offset among `reads` (the console + /// compiler's form): each lane loads its own entry. + pub loads: Vec, } /// Traces every descriptor and scalar address of `p`, which runs as @@ -162,6 +165,10 @@ pub fn track( // Group accesses in the same loop so a shared mask can select several // descriptors together. let mut first_lane_at = std::collections::BTreeMap::, Vec)>::new(); + // The console compiler's waterfall reads the first lane's table offset + // and loads that entry: accesses, offset reads and loads by block. + let mut offset_at = + std::collections::BTreeMap::, Vec, Vec)>::new(); for (b, block) in p.blocks.iter().enumerate() { for (i, inst) in block.insts.iter().enumerate() { let site = (b, i); @@ -170,9 +177,17 @@ pub fn track( what, }; let mut first_lane = Vec::new(); + let mut offset = Vec::new(); let mut descriptor = |op: &Operand, role| -> Result<(), ShaderError> { let source = t.descriptor(inst, op, b).map_err(fail)?; first_lane.extend(t.first_lane_words(inst, op).unwrap_or_default()); + if let Source::Loaded { load, .. } = source + && let Some((_, a)) = out.scalar.iter().find(|s| s.0 == load) + && let Some(Term::Value(v)) = a.dynamic.map(|d| t.origin(d)) + && t.first_lane_source(v).is_some() + { + offset.push((v, load)); + } out.descriptors.push(Descriptor { site, role, source }); Ok(()) }; @@ -214,6 +229,14 @@ pub fn track( accesses.push(i); reads.extend(first_lane); } + if !offset.is_empty() { + let (accesses, reads, loads) = offset_at.entry(b).or_default(); + accesses.push(i); + for (v, load) in offset { + reads.push(v); + loads.push(load); + } + } } } // The proof needs the per-lane form. Without it nothing is emitted, and @@ -233,6 +256,17 @@ pub fn track( out.waterfall.compares.extend(proof.compares); out.waterfall.reads.extend(proof.reads); } + // Unproven, an offset read stays the first lane's (one uniform entry), + // which is what the hardware reads outside a waterfall loop. + for (block, (accesses, mut reads, loads)) in offset_at { + reads.sort_unstable(); + reads.dedup(); + if let Ok(proof) = t.waterfall(block, &accesses, &reads) { + out.waterfall.compares.extend(proof.compares); + out.waterfall.reads.extend(proof.reads); + out.waterfall.loads.extend(loads); + } + } Ok(out) } @@ -2233,6 +2267,64 @@ pub(crate) mod tests { 0xBF81_0000, // s_endpgm ]; + /// The console compiler's waterfall (Grand Theft Auto VI's ray-tracing + /// resolve and others): each lane holds its entry's byte offset in the + /// table at s0 (v7 = (lane & 1) * 32); the loop reads the first active + /// lane's offset, narrows EXEC to the lanes with the same one + /// (`v_cmpx_eq_u32`), loads that entry with `s_load_dwordx8` and samples, + /// then drops those lanes (`s_andn2_wrexec_b32`). The sample goes to the + /// buffer at dword 16 of the table, at lane * 4. + pub(crate) const WATERFALL_INDEXED_WAVE32: &[u32] = &[ + 0xBE84_0300, // s_mov_b32 s4, s0 + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xBE86_0301, // s_mov_b32 s6, s1 + 0xBE87_0380, // s_mov_b32 s7, 0 + 0xF408_0203, + 0xFA00_0000, // s_load_dwordx4 s[8:11], s[6:7], 0x0 + 0xF408_0A02, + 0xFA00_0040, // s_load_dwordx4 s[40:43], s[4:5], 0x40 + 0x7E04_0D00, // v_cvt_f32_u32 v2, v0 + 0x7E06_0302, // v_mov_b32 v3, v2 + 0x3420_0082, // v_lshlrev_b32 v16, 2, v0 + 0x360E_0081, // v_and_b32 v7, 1, v0 + 0x340E_0E85, // v_lshlrev_b32 v7, 5, v7 + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0xBE97_037E, // s_mov_b32 s23, exec_lo + 0xBE95_037E, // s_mov_b32 s21, exec_lo + 0x7E28_0507, // 0x40: v_readfirstlane_b32 s20, v7 + 0x7DA4_0E14, // v_cmpx_eq_u32 exec_lo, s20, v7 + 0xF40C_0302, + 0x2800_0000, // s_load_dwordx8 s[12:19], s[4:5], s20 + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0xF09C_0F08, + 0x0043_0C02, // image_sample_lz v[12:15], v[2:3], s[12:19], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D + 0xBF8C_3F70, // s_waitcnt vmcnt(0) + 0xBE95_4715, // s_andn2_wrexec_b32 s21, s21 + 0xBF89_FFF6, // s_cbranch_execnz -> 0x40 + 0xBEFE_0317, // s_mov_b32 exec_lo, s23 + 0xE070_1000, + 0x800A_0C10, // buffer_store_dword v12, v16, s[40:43], 0 offen + 0xBF81_0000, // s_endpgm + ]; + + #[test] + fn the_console_waterfall_over_a_first_lane_offset_is_one() { + let r = waterfall(WATERFALL_INDEXED_WAVE32, false).unwrap(); + let image = r + .descriptors + .iter() + .find(|d| d.role == Role::Resource) + .unwrap(); + let Source::Loaded { load, first: 0 } = image.source else { + panic!("{:?}", image.source) + }; + let (_, address) = r.scalar.iter().find(|s| s.0 == load).unwrap(); + assert!(address.dynamic.is_some(), "{address:?}"); + assert_eq!(r.waterfall.compares.len(), 1); + assert_eq!(r.waterfall.reads.len(), 1); + assert_eq!(r.waterfall.loads, [load]); + } + /// [`WATERFALL_JOINED_WAVE32`] with other sources on each path: lanes /// below 16 load table entry (lane & 1) with two `global_load_dwordx4`, /// the others pick per lane with `v_cndmask_b32` (lanes below 24 the