xshader: don't branch on the lane around a ballot's operand #670
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/670"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-12-03T07:40:19Z.
Summary
What you see. With the programs that shade ray hits running, the console ray-tracing structures light the room in ProjectScarlett's first playable scene. The right side of the view (the drawings on the wall, the bunk) is lit by indirect light while the structures are on, and goes black when they are off. I alternated them every 45 s from a still camera, all nine walks served and nothing skipped. In one run the corner switched 5 times out of 5 each way (mean grey about 40 on, 3 off). A second, shorter run did the same (37 and 21 on, 3 off). On this card those programs used to stop the GPU 11 to 20 s after the world loaded, so this could not be seen.
What was wrong. Programs translated to one thread per console lane ballot their compares as
WaveActiveBallot((exec_lo & tb_lane_bit) != 0u && cond). HLSL's&&evaluates its right side only where the left one holds, so DXC branches on the lane around the operand. When another branch on the same condition follows, and then another ballot, the program NVIDIA's driver compiles never ends on some inputs: Xid 109, one dispatch in flight, no bad address. The smallest program I reduced it to has no loop. I take it for a convergence deadlock in the driver, which I can't confirm from outside.The change. Both sides are evaluated,
and(...)instead of&&, at the five places that write a ballot. The operands only read registers and constants.It changes many programs. 3,601 of the 38,785 ProjectScarlett requests in my cache that translate on their own (1,649 more don't), all
cs_6_6, so their cached shaders recompile. CONTRIBUTING asks to agree on that first: if you'd rather land it together with another translator change, that's fine with me. None of them uses the packed form ((exec_lo & 1u), one of the five places). I changed it for the same reason and have measured nothing on it.Tested
106120cawithout the change, see below)106120cahas tooGPU, driver and backend: RTX 4070 Ti SUPER 16 GB, NVIDIA 620.73.04, vkd3d-proton (Proton Experimental), Intel CPU.
AMD, run by @NickStallman on a Radeon iGPU (1002:13c0), GE-Proton11-15, Windows DXC 1.8.2505,
tuxbox.py check --changed 106120ca(their comment):NVIDIA, run by @itractus on an RTX 5090, NVIDIA 620.85.02, GE-Proton11-15, Windows DXC 1.9.2609,
tuxbox.py check --changedon this commit cherry-picked onto106120ca(their comment). In the game with default settings, four runs of four without this change ended in Xid 109, and their one run with it didn't:No Lanes scenario: the hang needs two ballots and a branch arranged the way a title program has them, and a scenario that fails without the change would hang the card of whoever runs it. The unit test
a_resolve_ballots_without_branching_on_its_lanechecks the emitted form and fails without the change.Outside the game. I reproduced the hang with generated inputs, the same DXC and vkd3d-proton, and reduced one program to 57 lines of HLSL that still hang.
and(...)One of them wrote the same bytes before and after on the 60 inputs before its first hang. This branch's own translator output for the four programs also ran there: 29,000 inputs, no hang.
In the game. These runs did not use this branch's translator. They used
6d4f1cd7with #647 and local diagnostics, and the cached HLSL of the same programs rewritten the same way. 20 runs reached the world with the hit-shading programs running and nothing skipped. None ended with one of those programs stuck without a bad address.What I can't claim:
TUXBOX_READ_RESIDENCY=0,TUXBOX_CONSOLE_STRUCTURES=1and noTUXBOX_CONSOLE_WALKS.docs/ENVIRONMENT.mdsays only four walks can be served in ProjectScarlett. I left that line until that walk is fixed.Checks
No evidence JSON changes.
Imported from brcly/TuxBox. Originally posted by copilot-pull-request-reviewer[bot] on 2026-12-03T07:42:54Z.
Review: COMMENTED
🔵 Needs a closer look
The shared shader changes still need human hardware validation because required AMD checks are unrun and the packed-ballot path has no reported runtime measurement.
No open issues 🎉
What changed in this PR
Updates TuxBox’s shader translator to avoid lane-dependent short-circuit evaluation around ballot operands, addressing reported NVIDIA GPU hangs.
Changes:
&&with eagerand(...)at five ballot emission sites.Review effort: Balanced
Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-12-03T08:07:29Z.
AMD run, and a second NVIDIA hang that this fixes.
AMD. PR head
705be7c49bon a Radeon iGPU (1002:13c0), GE-Proton11-15, Windows DXC 1.8.2505,tuxbox.py check --changed 106120ca:That includes the wave32 guard checks (wave-guard-32-*, 33 cases each) and mask-ballots (665 cases). The shader-compiler tests pass natively too, with the new test among them.
A second hang, on an RTX 5090 (NVIDIA 620). I had been chasing a different ProjectScarlett program that ends in Xid 109: a ray-traced shadow pass that dispatches without rays and still hangs. In its translation a per-lane
if (exec_lo & tb_lane_bit) { … 1.0 / … }sits between two ballots, so I took the division branch for the trigger. Turning that division into a select did stop it. But your change alone stops it too, with that branch still in place.Method: same request, same DXC, run standalone with a fixed pseudo-random input, 1,700 groups, outside the game:
106120ca106120ca, the runtime's DXC flagsThe HLSL with this PR differs from 106120ca's only in the 120 ballot lines (
&&toand(...)); the per-lane division branch is unchanged. So in this program too, the hang follows the&&in the ballot operand. That supports your reading. With this PR applied, my division change makes no measured difference, so I'll drop it.Limits: one program, one input fill (a zero fill never hung, with or without either change), small counts. Not yet run in the game with this PR.
🤖 Generated with Claude Code
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-12-03T08:29:55Z.
Thanks @NickStallman. Another card and another program, with the two changes tested apart: that's a stronger confirmation than mine. I've added your AMD run to the description.
Imported from brcly/TuxBox. Originally posted by itractus on 2026-12-03T15:15:13Z.
Tested on an RTX 5090: with this change the Xid 109 is gone here too.
Setup: RTX 5090 32 GB, NVIDIA 620.85.02, GE-Proton11-15, Linux 7.3 (CachyOS), Ryzen 9850X3D, 61 GB RAM. ProjectScarlett 1.0.2.1,
game_scarlett_master.exee95020b2…. Offline launches with default settings (noTUXBOX_CONSOLE_STRUCTURES).mainat106120ca, and the same with this PR's commit (705be7c4) cherry-picked on top.Without this change: four runs, all ended in Xid 109 (
CTX SWITCH TIMEOUT, channel 7,game_scarlett_m). Each one 33–62 s after the firstrecorded inline compute shader … dispatchline, at 1 frame every 12–18 s, and 189–220 s after launch. One of the four had #667 applied; it ended the same way.With this change: no Xid and no device loss. The run went on for 181 s after the first inline ray dispatch (60 frames, about one every 10–20 s, so GPU-bound as in your runs) and 388 s in total. I closed it then (exit code 1). There was no fault in the kernel log or the TuxBox log.
Of the 13 inline ray programs dispatched, the five your change rewrites (
3b375136,65f82768,8d02ef34,a902033a,bd054a51, 120 to 370 ballots each) all ran. The other eight translate to the same HLSL as before. The menus ran at about 165 fps before the first ray dispatch in every run, so the slowdown starts with the ray work.The picture in the world is dark with many artifacts, which I take to be the console structures being off, as in your description.
Seen only in this run: five
skipped compute dispatch: inline ray scene binding (0x80070057, #1…#5, guest caller 0x248cbc1)lines, around a new inline programdf39fe6edispatched with scene GPU VA0x0. The earlier runs never got that far, so I don't think this change causes it.Tested:
a_resolve_ballots_without_branching_on_its_lane.cs_6_6,-O1 -disable-lifetime-markers) and passdxv.3652affbis705be7c4cherry-picked onto106120ca, with no other changes. The shader translator on the GPU passed all 71 cases, and Lanes and fixtures passed. Windows DXC is 1.9.2609.Your two NVIDIA failures don't show up here. The Python tests passed in full, including
test_a_guest_that_died_stops_the_sandbox_instead_of_waiting_out_the_timeout.lanes-selftest-queue-page-mappingsisn't in the--changedset, but it passed in a fulltuxbox.py checkof106120caon this card earlier today.results.json
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-12-03T16:17:02Z.
Thanks @itractus. That's a second RTX 5090, with default settings, and I've added your run to the description. The Python test that fails here is the one #675 is about: its author and I are both on NixOS, where it fails. I haven't found why
lanes-selftest-queue-page-mappingsfails on my card.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.