shader: retire empty wave halves in the skinned decal reduction

This commit is contained in:
Xyzstk
2026-12-02 15:39:38 +08:00
parent eb19b673f9
commit 09483bf4b2
4 changed files with 71 additions and 1 deletions

View File

@ -36,6 +36,7 @@ if not exist "%runner%" (
exit /b 2
)
if /i "%~1"=="fast" (
if not defined VICTORY_WAVE_RETIRE set "VICTORY_WAVE_RETIRE=1"
if not defined VICTORY_AMBIENT set "VICTORY_AMBIENT=0.8"
if not defined VICTORY_ALIAS_COALESCE set "VICTORY_ALIAS_COALESCE=1"
if not defined VICTORY_THIN set "VICTORY_THIN=1"

View File

@ -64,6 +64,7 @@ fn build() -> [u8; 32] {
"VICTORY_WAVE_WIDE_ONLY",
"VICTORY_PAIR_MEET",
"VICTORY_PAIR_MEET_SKIP",
"VICTORY_WAVE_RETIRE",
"VICTORY_WAVE_DEEP_SKIPS",
"VICTORY_WAVE_OWN_BITS",
"VICTORY_WAVE_LOCAL",

View File

@ -412,6 +412,7 @@ struct Lower<'a> {
abort: Option<usize>,
/// This shader meets the two halves of each console wave on their own.
half_meet: bool,
retiring_limit: Option<Value>,
spill_start: Option<u32>,
scratch: Option<(V, V)>,
/// First LDS dword of the per-wave meet table, and how many sites fit.
@ -1778,6 +1779,27 @@ fn half_input_partners(io: &StageIo, inputs: &[SigElement]) -> HashMap<u8, u8> {
partners
}
fn reduction_limit(parts: &Parts<'_>) -> Option<Value> {
if !matches!(parts.native.io, StageIo::Cs(cs) if cs.threads == [1024, 1, 1])
|| parts.native.code.len() != 9280
|| !parts.native.wave64
|| parts.native.code.iter().fold(0xcbf2_9ce4_8422_2325u64, |hash, &byte|
(hash ^ u64::from(byte)).wrapping_mul(0x100_0000_01b3)) != 0x528a_644c_252a_0708
{
return None;
}
let (header, block) = parts.p.blocks.iter().enumerate().find(|(_, block)| block.offset == 0x20b8)?;
if !parts.st.wide.contains(&header) || parts.st.wave_any[header] != Some(false) {
return None;
}
let compare = block.insts.iter().find(|inst| inst.machine.offset == 0x20bc)?;
let thread_id = parts.p.inputs.iter().find(|(reg, _)| *reg == Reg::V(0))?.1;
if compare.read(Reg::V(0)) != Some(thread_id) {
return None;
}
compare.read(Reg::S(5))
}
fn unsupported(offset: usize, what: Why) -> Stop {
Stop::Fail(ShaderError::Unsupported { offset, what })
}
@ -2176,6 +2198,9 @@ impl<'a> Lower<'a> {
counters: HashMap::new(),
abort: None,
half_meet,
retiring_limit: if spill_start.is_some() && std::env::var_os("VICTORY_WAVE_RETIRE").is_some_and(|value| value == "1") {
reduction_limit(x)
} else { None },
spill_start,
scratch: None,
meet_base,
@ -5041,7 +5066,30 @@ impl<'a> Lower<'a> {
Ok(self.s.m.cmp(at, pred::NE, both, z))
}
fn reduction_overlap(&mut self, limit: Value, upper: bool) -> Result<V, Why> {
let threshold = self.i32_of(limit)?;
let base = self.wave_base().ok_or("missing wave base for reduction")?;
let mask = self.s.m.i32(!63u32);
let start = self.s.m.bin(self.cur, Bin::And, base, mask);
let first = if upper {
let half = self.s.m.i32(32);
self.s.m.bin(self.cur, Bin::Or, start, half)
} else { start };
Ok(self.s.m.cmp(self.cur, pred::IUGT, threshold, first))
}
fn wave_wide(&mut self, b: usize, when: Bool) -> Result<V, Why> {
if self.x.p.blocks[b].offset == 0x20b8 && !self.reached_by_group() && let Some(limit) = self.retiring_limit {
let overlaps = self.reduction_overlap(limit, false)?;
let guarded = self.begin_guarded(Some(overlaps));
let voted = self.wave_wide_vote(b, when)?;
let empty = self.s.m.bool(true);
return Ok(self.end_guarded(guarded, &[(voted, empty)])[0]);
}
self.wave_wide_vote(b, when)
}
fn wave_wide_vote(&mut self, b: usize, when: Bool) -> Result<V, Why> {
const WHY: Why = WAVE_WIDE_ELSEWHERE;
let any = self.x.st.wave_any.get(b).copied().flatten().ok_or("a wave-wide branch on a scalar condition")?;
if self.exchange.is_none() && self.halves.is_none() && self.console_lanes && self.mask_data == MaskData::Console {
@ -6211,7 +6259,19 @@ impl<'a> Lower<'a> {
self.lds_reads += 1;
}
}
self.cur = self.order_waves(self.cur, Some(self.cur_ir));
if let Some(limit) = self.retiring_limit
&& (0x20c4..0x2190).contains(&inst.machine.offset)
&& !self.reached_by_group()
{
let both = self.reduction_overlap(limit, true).map_err(|why| unsupported(inst.machine.offset, why))?;
let guarded = self.begin_guarded(Some(both));
self.cur = self.order_waves(self.cur, Some(self.cur_ir));
self.end_guarded(guarded, &[]);
let mode = self.s.m.i32(8);
self.s.op(self.cur, "barrier", Attr::NoDuplicate, self.s.t.void, 80, &[mode]);
} else {
self.cur = self.order_waves(self.cur, Some(self.cur_ir));
}
}
if self.half_meet {
self.lds_run = Some(write);

View File

@ -1,5 +1,13 @@
# Wave32 lockstep for divergent LDS
### Retirement in the skinned decal reduction
`VICTORY_WAVE_RETIRE=1` opts into a specialization for `CS_ComputeHitDetectionSkinnedResults` when private wave scratch spills. `Play-VIctory.cmd fast` enables it unless explicitly overridden; `VICTORY_WAVE_RETIRE=0` keeps baseline lockstep. Other kernels, including variants with different code or thread dimensions, are unaffected.
The audited loop halves an unsigned limit from 512 to 1. Its outer `execz` skips the body unless some guest thread ID is below that limit. Both host halves know the limit and their guest wave's first thread ID. Once the whole wave falls outside the prefix, it no longer exchanges votes at the head. Once the upper half falls outside, neither half needs the inter-half rendezvous around ordinary LDS accesses inside the guarded body. Both take the same decision, keep their counter generations in step, and retain the LDS fence and the original iteration-ending barrier. This is implicit retirement from specific rendezvous, not a general dynamic arrive-and-drop primitive; it never drops a group barrier or lets a half leave the loop early.
On a Radeon 610M, two isolated 12-group dispatches took 1.985 → 1.42 ms and 2.07 → 1.41 ms (GPU timestamp medians, eight alternating-order batches per variant, first three of 20 iterations discarded per batch). Both repeats matched native wave64 over the full 196,608-byte game output; the dispatches changed 143 and 161 bytes, respectively. These are dispatch timings, not game frame times.
On wave32 hosts, the translator automatically retries a wave64 compute shader **after** its normal translation fails with `no wave32 lowering for this wave64 program`. Set `VICTORY_LDS_LOCKSTEP=0` to disable this retry for comparison; no setting is needed to enable it. For offline translation, set `VICTORY_HOST_MAX_WAVE=32` to select the target. Both host halves take the same wave-wide branches; guest vector operations retain their EXEC predicates. Unrecognized control flow remains a refusal.
Every guest wave reuses a single pair of counters across all eligible synchronization sites. Neither half can advance more than one generation ahead of the other, so the signed difference between observed and expected generations also works across 32-bit wrap. This reduces LDS usage even without the fallback.