shader: bound the vertex entry mask by the highest running lane #2201
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2201
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2201"
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?
@Nawid3333 on GitHub · 2026-11-10 22:03 UTC · original
Nawid3333:shader/vertex-entry-lane-bound→main· +236 −3 · 7 files · 4 commits · openThe NGG prologue of most vertex shaders (720 of the 770 stored vertex modules that contain a select, table below) rebuilds EXEC from the vertex count in
s3:exec = -1 >> (-s3 & 63)(e.g. d3e1ae284c4dea3b, already a fixture inCheckRasterization). The vertex entry fills that count withpopcount(Ballot(true)). Every vector write after the prologue is thenselect(own bit of that mask, new, old), and constant propagation cannot remove the select, because nothing says that a running lane lies below the count. AMD's shader compiler keeps it as well (below).I extracted the SPIR-V modules from the shader stores of five games and counted the
OpSelectwhose condition is that bit:All 720 modules build the mask in the same way: a 64-bit right shift of all ones by a constant minus the count, and the word chosen by
lane < 32.Changes.
LaneBound: one past the highest running subgroup lane, capped at the guest wave size (OpGroupNonUniformBallotFindMSBofBallot(true), plus one,UMin). The vertex entry puts it intos3in place of the popcount. When the running lanes are a dense prefix the two are equal, sos3, the EXEC words and every scalar reader keep their values (a wave32 program on a 64-lane subgroup with 40 running lanes gets 32 either way). When they are not dense, the popcount put running lanes outside the prefix; the bound cannot.LaneMaskProjectionfolds the own-lane bit of a mask to true when the mask depends only onLaneBoundand immediates and covers lanes0..bound-1for every bound from 1 to the wave size. It evaluates the mask words for each bound (and, or, 32-bit subtract and right shift, bit-field extract, 64-bit right shift and word extract) instead of matching one instruction shape, so the& 0xff,64 - countand0 - countvariants of the prologue need no separate rule; anything it cannot evaluate stays as it is. In wave64 the word must be selected byLaneId < 32.s3nowhere else has no subgroup instruction left.A full-wave constant in
s3would fold as well, but it leaves EXEC word bits for lanes that do not run, which the partial-wave waterfall case ofCheckRasterization(3, 6 and 33 vertices) rules out.In the game. Demon's Souls (PPSA01341) on an RX 9070 XT, one run with main
782a85c62and one with the first commit of this branch (both with my lab input player on top): both boot and draw the logo screens without a fatal error; the main run also reached the language selection. Six vertex shaders were compiled by both runs. Paired by the instructions the fold cannot touch (stores, access chains, float arithmetic, conversions, image accesses, extended instructions other thanUMin), and compiled with AMD's offline compiler (rga -s vk-spv-offline -c gfx1201):OpSelectPer shader, native: 73 -> 55, 81 -> 62, 100 -> 82, 140 -> 111, 111 -> 82, 178 -> 139. The changed modules showed that each still read the lane id: the prologue also builds a first mask from the count in
s3[15:8]with a bit-field extract. The second commit evaluates that form too and the third adds test cases for it; the second is covered by the tests below and has not run in a game.On larger shaders the share is smaller. The same compiler on 12 stored vertex modules per game, taken evenly over the size-sorted modules that have the pattern, as stored and with the one condition replaced by true: Demon's Souls 7,509 -> 6,500 native instructions (-13.4 %), ASTRO BOT 26,553 -> 22,694 (-14.5 %), Fallen Order 10,650 -> 9,305 (-12.6 %).
Tests.
TestVertexEntryMaskCoversRunningLanes(shader_cfg_tests) compiles the captured prologue in wave32 and wave64 with one register written under each of its two masks: the module reads no lane id and has no subgroup instruction; a shader that exportss3keepsBallotFindMSB; a mask one lane short of the bound and an EXEC write after the prologue stay predicated. On main with only this test it fails ata running vertex lane stayed predicated by the entry mask of its own lane bound.TestLaneMaskProjectiongains three covering masks (all fold:0 - bound, thes3[15:8]count with64 - count, and a bit-field extract of the bound itself), and masks that must stay: one that in wave32 misses only bound 1, one that misses only the last bound, a wave64 word selected at lane 31, prefixes of half the bound built with a bit-field extract at bit 1 and with a 32-bit shift by 1, and a prefix of the bound's lowest bit (a 1-bit extract). Six deliberate bugs (loop from 2, loop without the last bound, selector constant ignored, extract offset ignored, extract width ignored, shift amount ignored) each fail one of them.shader_recompiler_compute_tests:--polygon-mode-onlypasses (it contains the waterfall case in both wave sizes); the full run has the same 1,057 passing checks and the same 22 failing checks as main782a85c62. Both runs need lab-only patches to start on this GPU (#2038, optional feedback-loop state, D24S8 replaced by D32S8, one skipped case that crashes the driver); they are not part of the PR.Written with Claude Code, OpenAI Codex and GLM (AI): the change, the tests and the measurements, run on my PC; reviewed by me.
@coderabbitai[bot] on GitHub · 2026-11-10 22:11 UTC · original · edited 2026-11-11 01:52 UTC
No actionable comments were generated in the recent review.
Walkthrough
Vertex entry initialization now uses
LaneBoundto set the low lane-bound bits in SGPR3. The SPIR-V emitter computes the bound from a subgroup ballot and caps it at the configured wave size. Constant propagation evaluates supported lane-mask expressions across possible bounds and folds comparisons when each active lane is covered. Tests exercise vertex entry masks and lane-mask projection for wave32 and wave64.Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Minimal · up to
166b7The change bounds the vertex entry lane mask by the highest running lane and folds covering lane-mask predicates. No concrete merge-blocking risk was identified. The author reports hardware results that were not independently verified here.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 8 functions across 5 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
1e870eeb-176e-456a-8e8f-610103bcea71Reviewing files that changed from the base of the PR and between 7a896f229fbe329e3cf0d168e0f9fdf99abedcf0 and
166b7923fb.src/graphics/shader/recompiler/ir/passes/ConstantPropagation.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nawid3333 on GitHub · 2026-11-11 01:55 UTC · original
Rebased onto current main (
7d4e0d9b3) and added one commit that hardens the evaluator. The prologue forms fold as before.Fourth commit. The projection now accepts one
LaneBoundinstruction per mask, evaluates at most 64 new nodes per bound, and rejects aCompositeExtractU64index other than 0 or 1. Two tests: a mask built from two different lane bounds and a mask behind an 81-step chain both stay instructions. Before this commit a hand-built mask of two differentLaneBoundvalues could fold wrongly; the translator emits exactly one per vertex program and none in other stages, so no real shader was affected.More checks on real shaders, without game runs. I recompiled 1,041 guest programs dumped from Demon's Souls, ASTRO BOT, Ghost of Yōtei and Assassin's Creed Black Flag Resynced with main and with this branch, using the test fixture's default options:
INotEqual32; in 8 the count keeps a reader (three of the shaders of point 2 below, and one shader that reads bits 27:24).shader_recompiler_compute_testson an RX 9070 XT gives the same result list as main.shader_cfg_tests, which CI does not build or run, passes with this branch on Windows and Linux fork runners. On the macOS runner it stops inTestGpuProducedWritableDescriptoron main and on this branch alike, after this PR's two tests have passed.Two points for your decision.
remaining &= ~ballot(handled)). On a host with non-dense vertex lanes that mask keeps the bit of a lane that does not run, with the count and with the bound alike, so the loop cannot end there. Whether any real driver gives vertex shaders such subgroups I have not measured.@Nawid3333 on GitHub · 2026-11-11 01:57 UTC · original
Game tests are following
@Nawid3333 on GitHub · 2026-11-11 17:52 UTC · original
Game tests of the final head
166b7923fagainst its base7d4e0d9b3, on an RX 9070 XT (Windows 11, driver 26.10.1). Both builds carry the same local input-player commits, so each game got the same scripted input with both (logs:Source build dfc78dc= this branch,dfc78dc-dirty= the same tree with the six changed files from7d4e0d9b3):No run crashed or stopped early: all six were ended by the time limit, none of the logs has a fatal error, and all six show GPU work.
I compared the SPIR-V modules in each run's shader store byte by byte, per stage (base / this branch / byte-identical):
Every pixel and compute module that both builds produced is byte-identical, and so are the few modules of other stages; the remaining ones are in only one of the two stores. Every vertex module differs, which is the change of this PR. So in these three games the branch changes vertex shaders only, and no game stopped with it.
Not covered: a pixel-by-pixel comparison of the rendered frames.
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.