Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions d3d12/crates/leonida-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,8 +2415,9 @@ impl<'a> Lower<'a> {
s.threads = Some([32 * waves, 1, 1]);
s.lds_dwords = cs.lds_bytes() / 4;
if waves > 1 {
halves = Some(s.lds_dwords);
s.lds_dwords += 1;
// Paired host groups are always 1-D (`[32 * waves, 1, 1]`), so host
// wave `k` already holds flat ids `32*k..32*k+31` (console base `64*k`).
halves = Some(NO_COUNTER);
}
console_lanes = true;
s.wave_size = Some(32);
Expand Down Expand Up @@ -2569,20 +2570,20 @@ impl<'a> Lower<'a> {
eprintln!("WAVE64-WHY group={group} lds={} [{}]", s.lds_dwords > 0, why.join(", "));
}
}
// A group of one dimension whose LDS leaves no dword for a counter (all of D3D12's
// 32 KiB: the title's culling program of 1,024 threads) takes its console threads
// from the flat id, as a host does that gives each wave a run of consecutive ids (it
// is only the other shapes, whose waves are tiles, that the counter is for).
let no_room = s.lds_dwords >= 8192 && cs.threads[1] == 1 && cs.threads[2] == 1;
// A 1-D group gives each host wave a consecutive run of flat ids, so it
// takes its console threads from the flat id without an LDS counter,
// entry barrier, or atomic (2-D/3-D shapes, whose waves may be tiled,
// keep the counter).
let one_d = cs.threads[1] == 1 && cs.threads[2] == 1;
if wave == 32 && x.native.wave64 {
// console threads go to host waves in the order the waves take them (`wave_base`)
if no_room {
if one_d {
halves = Some(NO_COUNTER);
} else {
halves = Some(s.lds_dwords);
s.lds_dwords += 1;
}
} else if console_lanes && group > wave && ticket_on() && !no_room {
} else if console_lanes && group > wave && ticket_on() && !one_d {
// the flat id rounded down is not a host wave's base on every host (#1243)
ticket = Some(s.lds_dwords);
s.lds_dwords += 1;
Expand Down Expand Up @@ -5812,10 +5813,20 @@ impl<'a> Lower<'a> {
let lane = self.s.op(e, "waveGetLaneIndex", Attr::ReadOnly, i32t, 111, &[]);
let half = self.s.m.i32(if self.upper { 32 } else { 0 });
let t = self.s.m.bin(e, Bin::Add, lane, half);
return match self.wave_base() {
Some(base) => self.s.m.bin(e, Bin::Add, base, t),
None => t,
};
self.s.flags |= flags::WAVE_OPS;
if let Some(base) = self.wave_base() {
return self.s.m.bin(e, Bin::Add, base, t);
}
if self.halves == Some(NO_COUNTER) {
let flat = self.s.op(e, "flattenedThreadIdInGroup.i32", Attr::ReadNone, i32t, 96, &[]);
let first = self.s.op(e, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[flat]);
let mask = self.s.m.i32(!31u32);
let host_base = self.s.m.bin(e, Bin::And, first, mask);
let one = self.s.m.i32(1);
let base = self.s.m.bin(e, Bin::Shl, host_base, one);
return self.s.m.bin(e, Bin::Add, base, t);
}
return t;
}
if let Some(base) = self.wave_base() {
let rank = self.wave_rank.expect("made with the base");
Expand All @@ -5827,6 +5838,7 @@ impl<'a> Lower<'a> {
let wave = self.s.wave_size.unwrap_or(32);
let mask = self.s.m.i32(!(wave - 1));
let base = self.s.m.bin(e, Bin::And, first, mask);
self.s.flags |= flags::WAVE_OPS;
self.s.m.bin(e, Bin::Add, base, lane)
}

Expand Down
76 changes: 76 additions & 0 deletions d3d12/crates/leonida-shader/src/emit/wave64_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,3 +1923,79 @@ fn a_loop_the_lanes_leave_one_by_one_is_left_by_each_host_wave_together_when_ask
Err(_) => eprintln!("no dxil.dll: neither form validated"),
}
}

/// A 1-D wave64 group (`[128, 1, 1]`) on a 32-lane host needs no entry ticket
/// barrier or LDS atomic: each host wave already has a consecutive run of flat
/// thread IDs, and every console thread 0..127 writes its own slot.
#[test]
fn one_d_wave64_group_skips_entry_ticket_barrier_and_keeps_thread_ids() {
// v_mbcnt_lo_u32_b32 v3, exec_lo, 0 (triggers console_lanes on a 32-lane host)
// v_add_nc_u32 v5, 1, v0
// v_lshlrev_b32 v4, 2, v0
// buffer_store_dword v5, v4, s[8:11], 0 offen
let words = [
0xD765_0003,
0x0001_007E,
0x4A0A_0081, // v_add_nc_u32 v5, 1, v0
0x3408_0082, // v_lshlrev_b32 v4, 2, v0
0x38 << 26 | 0x1C << 18 | 1 << 12,
0x80 << 24 | 2 << 16 | 5 << 8 | 4, // buffer_store_dword v5, v4, s[8:11], 0 offen
];
let mut native = native_of(&words, true, 128);
native.io = StageIo::Cs(ComputeIo {
rsrc2: 5 << 1, // 5 user SGPRs, thread id x in v0
threads: [128, 1, 1],
});
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
let bytes = crate::dxbc::build(&[]);
let c = crate::dxbc::Container::parse(&bytes).expect("container");
let was = lower::TEST_HOST_WAVE.replace(Some(32));
lower::TEST_GROUP_SYNCS.set(0);
let shader = lowered(&native, &p, &HashSet::new()).expect("lower 1D halves");
let halves_syncs = lower::TEST_GROUP_SYNCS.get();
lower::TEST_GROUP_SYNCS.set(0);
let (paired_shader, _) = super::paired(&c, &native, &p, false).expect("lower 1D paired");
let paired_syncs = lower::TEST_GROUP_SYNCS.get();
lower::TEST_HOST_WAVE.set(was);

assert_eq!(
halves_syncs, 1,
"1-D halves lowering should emit only the v_mbcnt exchange barrier (no entry ticket barrier)"
);
assert_eq!(
paired_syncs, 0,
"1-D paired lowering should emit zero group barriers (no entry ticket barrier)"
);

if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
for s in [&shader, &paired_shader] {
let mut copy = s.dxil.clone();
v.validate(&mut copy).expect("DXIL validation failed");
}
}

let _one = super::valu_tests::ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
let Ok(host) = Host::new(false) else {
return;
};
let want: Vec<u32> = (1..=128).collect();
for (label, s) in [("halves", &shader), ("paired", &paired_shader)] {
let Ok((root, pso)) = host.compute_pipeline(s) else {
continue;
};
let ud = vec![0u32; s.user_data_dwords().max(1) as usize];
let out = host
.dispatch(
&root,
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &[View::RawUav(128)],
samplers: &[],
groups: [1, 1, 1],
},
)
.expect("dispatch");
assert_eq!(out[0], want, "{label}: every 1-D console thread 0..127 must write its own slot");
}
}