From 598e8cb26f6da9bc284ea1e15a5b9ee9cfdac946 Mon Sep 17 00:00:00 2001 From: Guilherme da Silva Fernandes Almeida <153012160+guialmm@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:00:38 -0300 Subject: [PATCH] Shaders: readfirstlane takes each program wave's lane on hardware waves holding more than four On a hardware wave wider than the program wave, readfirstlane took one WaveActiveMax over a uint4, one component per program wave, which covers at most four program waves. A 32-lane NVIDIA wave holds 16 two-lane or 8 four-lane test waves, so every wave past the fourth read the same value (WaveEmulationTests, wave 2 and 4 without group lanes, from #9). The same max now repeats four program waves at a time up to WaveGetLaneCount() / VW_WAVE; console waves (32 or 64 lanes) still take a single max. --- .../Translation/Runtime/rdna_prelude.hlsli | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli b/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli index ed5afa3e..ee1e147e 100644 --- a/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli +++ b/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli @@ -451,12 +451,19 @@ uint readfirstlane(uint x, bool active) { // first lane with EXEC set (not jus #endif uint l = m.x != 0 ? firstbitlow(m.x) : m.y != 0 ? 32 + firstbitlow(m.y) : 0u; #if VW_EMULATE && !VW_GROUP_WAVE && !VW_GROUP_LANES - // A wider hardware wave holds at most 4 program waves (128 lanes, or WARP's 4 for test waves): a max per program - // wave, one component each. + // A wider hardware wave holds WaveGetLaneCount() / VW_WAVE program waves: a max per program wave, one component + // each, four program waves per max. Console waves need one (at most 4 in 128 lanes); test-sized waves on a real + // GPU need more (16 two-lane waves in a 32-lane wave), where a single max gave every wave past the fourth the same. if ((m.x | m.y) == 0u) return vw_read(x, 0u); - uint s = vw_origin() / VW_WAVE, v = vw_lane() == l ? x : 0u; - uint4 r = WaveActiveMax(uint4(s == 0u ? v : 0u, s == 1u ? v : 0u, s == 2u ? v : 0u, s == 3u ? v : 0u)); - return s == 0u ? r.x : s == 1u ? r.y : s == 2u ? r.z : r.w; + uint s = vw_origin() / VW_WAVE, v = vw_lane() == l ? x : 0u, first = 0u; + [unroll] for (uint base = 0u; base < 128u / VW_WAVE; base += 4u) { + if (base < WaveGetLaneCount() / VW_WAVE) { + uint k = s - base; // above 3 (wrapped) for the program waves outside these four + uint4 r = WaveActiveMax(uint4(k == 0u ? v : 0u, k == 1u ? v : 0u, k == 2u ? v : 0u, k == 3u ? v : 0u)); + if (k < 4u) first = k == 0u ? r.x : k == 1u ? r.y : k == 2u ? r.z : r.w; + } + } + return first; #else return vw_read(x, l); #endif