Skip to content

rdna2spirv: the PS FRONT_FACE VGPR is 0xffffffff / 0 — all 345 game shaders that read it test it against integer 0 (SHADER-892) - #5237

Open
rafaelpssz wants to merge 2 commits into
ItsNotRexed:mainfrom
rafaelpssz:fix/front-face-mask
Open

rafaelpssz wants to merge 2 commits into
ItsNotRexed:mainfrom
rafaelpssz:fix/front-face-mask

Conversation

@rafaelpssz

@rafaelpssz rafaelpssz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

SHADER-892 (claim); finding by @Vonsek.

The question

On RDNA2, the pixel shader's FRONT_FACE system VGPR holds 0xffffffff (front) / 0 (back) when SPI_BARYC_CNTL.FRONT_FACE_ALL_BITS = 1, and +1.0 / −1.0 when it is 0 (measured on gfx1036, #5123). Nobody knew which one the console driver programs. The translator wrote the float form.

The count (my own 1.0.2.1 copy, full corpus, counts only)

The FRONT_FACE VGPR is the one right after the SV_Position components the shader reads (the translator's own layout). I followed it from the entry to its first uses.

pixel shaders
declare SV_IsFrontFace 1,984
never read the VGPR 1,639
read it 345
… first use an integer compare against 0 345: 282 v_cmp_ne_u32_e32, 62 v_cmp_ne_u32_e64, 1 v_cmp_eq_u32_e32
… a float compare, bit op or anything else 0

+1.0 (0x3F800000) and −1.0 (0xBF800000) are both non-zero, so in the float form != 0 says "front" for every face. The game's code only works with the mask form, so the console programs FRONT_FACE_ALL_BITS = 1. With the old translation, all 345 shaders treated back faces as front faces.

The change

  • tools/rdna2spirv.py stores OpSelect %uint FrontFacing 0xffffffff 0 into the VGPR. Before, it stored the bitcast of OpSelect %float FrontFacing 1.0 -1.0. The layout comment says so too.
  • docs/design.md ("PS system inputs") records the form and the count.
  • tests/test_ps_front_face.py translates a hand-written PS that reads x/y of SV_Position and then tests the facing VGPR (v4) with v_cmp_ne_u32 vcc, 0, v4. It checks that the select stores 0xffffffff / 0 into v4 and that the float form is gone. It fails on the old code.

Tests

  • The new test fails on the old code and passes now. tests/test_shader_meta.py, test_docs_design_rows.py and test_docs_links.py pass.
  • python -m unittest discover -s tests/semantic: the only failures (3 failures, 2 errors) are test_loop_ballot_barrier, the same as on main (the known wave64-emulation regression, tests/semantic: fix wave64 emulation regression in test_loop_ballot_barrier #4363).
  • Corpus regression set: 712 pixel shaders that declare SV_IsFrontFace and have a v_cmp* with an immediate 0 on the FRONT_FACE register number anywhere in the listing. This is a superset of the 345 that includes register reuse, used as a translation set, not as a count. All 712 translate and pass spirv-val, both on main and with this change.
  • GPU (by reviewers, both windings, culling off):
    • @Kev-Ball, RTX 3080 Ti, Linux: back faces read 1.0 with the parent, the old bug, and 0.0 with this PR. Front faces read 1.0 on both.
    • @CryingLaughing, RTX 4080, Windows, validation on: with this PR, front = v4 == 0xffffffff and back = v4 == 0. That matches a Vulkan FrontFacing reference shader byte for byte over 4,096 pixels.

Thanks

By @rafaelpssz, with Claude Code (AI-assisted).

@Kev-Ball

Copy link
Copy Markdown

Ran the two-winding draw requested in the PR on CachyOS, RTX 3080 Ti, NVIDIA 620.73.04. I translated the PR’s hand-written test_ps_front_face.py fixture unchanged with both translators, validated both modules with spirv-val, then used vk_harness to draw a fullscreen triangle into RGBA32F. The second draw reversed the vertex order. Culling was disabled.

Translator Back-facing draw Front-facing draw
Parent 3faac557 1.0 1.0
PR fa7517a2 0.0 1.0

Each value held in all four channels at all 4,096 pixels. The clear value was −1.0, so the zero readback came from covered fragments. This reproduces the old back-face error and verifies the translated fixture’s integer comparison on NVIDIA Vulkan. The fixture reads the predicate result rather than exporting the raw VGPR bits.

AI-assisted. Synthetic inputs only.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at fa7517a26 on Windows 11: pytest tests/test_ps_front_face.py -q -> 1 passed; git diff --check clean.

The PS FRONT_FACE VGPR is now 0xffffffff/0 rather than a normalized float, which is the form all 345 game shaders that read it actually test against integer 0; the new test pins the two values and the integer compare, and the docs row was updated. No blocking issue.

@CryingLaughing

Copy link
Copy Markdown
Contributor

Windows RTX 4080 cross-check at fa7517a2 (Windows 11 build 26200, NVIDIA 621.01, Khronos validation 1.4.357).

One indexed draw rendered both triangle windings with culling disabled. The translated pixel shader records v4 != 0, v4 == 0xffffffff, and v4 == 0 as R, G, and B. Alpha is 1 on covered pixels.

Translator Front RGBA (192 pixels) Back RGBA (192 pixels)
Parent 3faac557 (1, 0, 0, 1) (1, 0, 0, 1)
PR fa7517a2 (1, 1, 0, 1) (0, 0, 1, 1)

The PR readback matched a separate Vulkan FrontFacing reference shader byte for byte across all 4,096 pixels, including 3,712 clear pixels. Both translated modules passed spirv-val for Vulkan 1.2. No validation warnings or errors. Synthetic inputs only. AI-assisted with Codex.

@rafaelpssz

Copy link
Copy Markdown
Contributor Author

Thanks @Kev-Ball and @CryingLaughing. That's the hardware check I couldn't run safely here: back faces read as front with the parent and correctly with this head, on both drivers, and CryingLaughing's run matches a plain FrontFacing reference byte for byte. Both runs are now in the description, and you're both (and @Kam471) in Thanks.

@sovljanskii

Copy link
Copy Markdown
Contributor

Apple M4 check at fa7517a2: pass, and the third GPU vendor for the winding test after NVIDIA on Linux and Windows. Setup: macOS 27.2.1, MoltenVK 1.4.2, AppleClang 21, base main 3faac557; the PR applies as a fast-forward.

Two windings on the M4. I translated the PR's hand-written fixture (tests/test_ps_front_face.py, unchanged lines and signature) with both translators. Each was drawn in vk_harness behind two hand-written fullscreen-triangle vertex shaders, one with the vertex order reversed. Culling is off, and the clear value is −1.0.

Translator first winding (4,096 px) reversed winding (4,096 px)
parent 3faac557 1.0 1.0
PR fa7517a2 0.0 1.0

Corpus slice from my own 1.0.2.1 copy (counts only): --shard 0/64, 415 shaders.

  • All 415 translate and pass spirv-val with both translators.
  • 30 of the 187 PS modules change, and the 385 others are byte-identical.
  • The changed set is exactly the 30 that use BuiltIn FrontFacing, which are also the 30 whose signature declares SV_IsFrontFace. None of them keeps the float form.

Other checks

  • Full GPU suite with main's binaries: 152/217 passed, 58 skipped on both, with every test unchanged.
  • test_ps_front_face.py, test_shader_meta.py and the docs tests: OK on Python 3.15.2 and 3.12.16.
  • With main's rdna2spirv.py, the new test fails (1).

AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At fa7517a all 12,101 cached pixel listings were checked against native sidecars and DXBC signatures. Of 1,984 SV_IsFrontFace declarations, 345 read the assigned input before its first write. Every first read compared against integer zero (282 ne e32, 62 ne e64, one eq e32). A direct-branch/fall-through traversal reproduced those counts at instruction granularity. This rules out the old ±1.0 bits for those uses, while 1/0 and all-bits/0 give identical comparison results. The exact front word draws on the separate hardware evidence.

Could you document the selection rule for the 712-module validation subset? Counting eq/ne integer-zero compares anywhere in the declaring listings gave 454 shaders, and including all v_cmp forms reading the register gave 1,296. Both include register reuse. The 345 first-use figure reproduced independently; the 712-module validation run was not repeated. AI-assisted CPU review.

rafaelpssz added a commit to rafaelpssz/Leonida that referenced this pull request Nov 23, 2026
…0 is the hardware's other form

From @kacper-serewis's review of ItsNotRexed#5237: an integer compare against 0 can't
tell 0xffffffff/0 from 1/0; the mask form is the other one the hardware
gives (ItsNotRexed#5123).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaelpssz

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, both points are fair.

  1. 1/0 vs all-bits/0. Right: an integer compare against 0 rules out ±1.0 but can't tell the two non-float forms apart. All-bits comes from the hardware side: rdna2spirv: the PS position-w register is 1 / FragCoord.w (measured on RDNA2); ps_sysvals_probe #5123 found exactly two forms on gfx1036, ±1.0 and 0xffffffff/0, so all-bits is the only one left. 92475381 says so in the design.md row.
  2. The 712. The selection rule was coarser than the 345: every pixel shader that declares SV_IsFrontFace and has, anywhere in its listing, a v_cmp* with an immediate 0 on the FRONT_FACE register number (0, v<ff> or v<ff>, 0). It doesn't check that the register still holds the input, so register reuse is included. That's a superset of the 345, used only as a translation and spirv-val regression set (712/712 both before and after), not as a count of readers. I'll add that wording to the description.

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU verification on Linux (Python 3.13.5 + spirv-as/spirv-val, head 92475381, base main 3faac557):

  • git diff --check main...HEAD and python3 -I -m py_compile tools/rdna2spirv.py tests/test_ps_front_face.py — clean.
  • python3 -I tests/test_ps_front_face.py -v — 1/1 OK (reproduced on main 3faac557: main emits %ffm1 = OpFNegate + float select instead of %ffu = OpSelect %uint ... 0xffffffff 0).
  • python3 -I tests/test_shader_meta.py -v — 19/19 OK; tests/test_design_contract_rows.py (4/4 OK), tests/test_docs_table_rows.py (2/2 OK).
  • Verified tools/rdna2spirv.py + spirv-val --target-env vulkan1.3 on the synthetic SV_IsFrontFace fixture for both wave32 and --wave64-emulate — both PASS.

One small doc typo introduced in 92475381 (docs/design.md line 278):

...all-bits is the form the hardware gives besides ±1.0 (#5123).0 (Mesa: `load_front_face` = `front_face > 0`). Only enabled inputs are packed...

It looks like the replacement left a stray .0 (Mesa: \load_front_face` = `front_face > 0`).fragment from the old±1.0 (Mesa: ...)sentence right after(#5123). Dropping that dangling .0 (Mesa: ...).` tail will make the row clean.

@amarovndres

Copy link
Copy Markdown

Tested fa7517a2 on Windows 11 Pro (build 26200), Python 3, SPIRV-Tools from Vulkan SDK 1.4.363 on PATH. CPU only. Pass.

  • python -I tests/test_ps_front_face.py: 1 test OK.
  • Control, this tree with main 3faac557's tools/rdna2spirv.py: FAILED (failures=1), so the test catches the FRONT_FACE change.
  • Only this PR's new test was run, not the whole tests/semantic suite.

AI-assisted (Claude Code). Hand-written shaders only, no game files.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 9247538 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_ps_front_face.py -> Ran 1 test, OK
  • Negative control (tools/rdna2spirv.py swapped to upstream/main): FAILED (failures=1)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 92475381 (merge base 3faac557) on Windows 11 10.0.26300 with an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56. Result: pass.

  • Runtime built from this head: MSVC 19.44 (BuildTools 2022), Ninja, RelWithDebInfo, -DLEONIDA_VULKAN_HEADERS_FETCH=ON (no Vulkan SDK; the loader comes from the NVIDIA driver). glslangValidator and llvm-mc 21 came from WSL Ubuntu. Build exit 0.
  • ctest -j 3: 177/177 passed.
  • scripts/run_gpu_tests.py --build <this build>: 170/217 passed, 47 skipped, 0 failed.
  • No GPU test reads the FRONT_FACE VGPR, so this run checks that nothing else changed, not the new value.
  • Every other test gives the same result as on main 3faac557 (ctest 177/177, GPU 170/217).
  • FUZZ-8 sweep of this head: clean (posted on Coordinating who works on what (30) (moved to #5361) #5256).
  • As on main, run_gpu_tests.py records cs_fmt_d16_xyz twice and skips cs_tbuffer_store. That's main's runner bug (fixed by run_gpu_tests: record cs_tbuffer_store's result; fail a run that records a test twice #4784), not this PR.

Not run: other GPUs/vendors, Linux, the Vulkan validation layer (not installed). No game files.

AI-assisted (Claude Code).

rafaelpssz and others added 2 commits November 23, 2026 12:23
… game's shaders test (SHADER-892)

RDNA2's FRONT_FACE system VGPR is 0xffffffff (front) / 0 (back) with
SPI_BARYC_CNTL.FRONT_FACE_ALL_BITS = 1 and +1.0 / -1.0 without it (ItsNotRexed#5123);
which one the console programs wasn't known. Over the 1.0.2.1 corpus, 1,984
pixel shaders declare SV_IsFrontFace, 345 read the VGPR, and every first use
is an integer compare against 0 (282 v_cmp_ne_u32_e32, 62 v_cmp_ne_u32_e64,
1 v_cmp_eq_u32_e32) - no float compare, bit op or other use. +1.0 and -1.0
are both non-zero, so with the float form the translator wrote, all 345
saw every face as front. It now stores the mask form.

tests/test_ps_front_face.py translates a hand-written PS that tests the VGPR
the same way and checks the stored values (fails on the old code). The
712 corpus PS that compare the FRONT_FACE register translate and pass
spirv-val before and after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…0 is the hardware's other form

From @kacper-serewis's review of ItsNotRexed#5237: an integer compare against 0 can't
tell 0xffffffff/0 from 1/0; the mask form is the other one the hardware
gives (ItsNotRexed#5123).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaelpssz

Copy link
Copy Markdown
Contributor Author

Rebased onto main 1cc6e3c1. #4048 rewrote the "PS system inputs" row, so the FRONT_FACE sentences are re-applied to its new text, with no change in content. test_ps_front_face and the docs tests pass.

@duudiis

duudiis commented Nov 23, 2026

Copy link
Copy Markdown

Tested eea1754e merged onto main edc722c6 on RTX 5070 (Blackwell), NVIDIA Windows driver 32.0.16.2124 (621.24), Windows 11 Pro 26300, CPython 3.11.0; no llvm-mc, no SPIRV-Tools, with the CI runtime-windows test programs of main 7b7d73f (the runner is newer than those programs): pass, with notes

  • python -I scripts/run_gpu_tests.py --build <runtime-windows> --disasm-dir tests/semantic/fixtures/disasm --assembler python: main 81/96 passed, 15 skipped, merge 81/96 passed, 15 skipped, 0 failed on both. The 15 skips are the wave64 tests (no 64-wide subgroup on this GPU).
  • Same command with LEONIDA_W64_EMULATE=1: main 96/96, merge 96/96.
  • Every test has the same result on main and on the merge in both modes. No regressions.
  • Note: none of these GPU fixtures reads the FRONT_FACE VGPR (no SV_IsFrontFace / FrontFacing in tests/semantic), so this run shows the rest of the translator is unchanged on the rebased head. It does not measure the new value; the two-winding draws in the description (on earlier heads) cover that.
  • PR's own CPU test: python -m pytest -q tests/test_ps_front_face.py tests/test_shader_meta.py: 28 passed. With main's tools/rdna2spirv.py swapped in, test_front_face_is_stored_as_all_bits_or_zero fails (1 failed), so the test catches the change.
  • python -m pytest -q tests/test_docs_*.py: 33 passed, 3 failed on the merge and the same 3 on main edc722c6 (test_docs_cli_flags x2 on docs/embedded_shaders.md / docs/dev-tools.md, and test_docs_links on docs/dev-tools.md). None of these involves files this PR changes. test_docs_design_rows passes.
  • Not run: the two-winding FRONT_FACE draw (it needs a custom vk_harness draw that this runner doesn't have), spirv-val (not installed), the Vulkan validation layer.

AI-assisted (Claude Code).

@carlos-seiffert

Copy link
Copy Markdown

Tested eea1754 (merged onto main 86a7807) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 86a7807 (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 86a7807 this PR on main
full GPU suite 191/240 passed, 48 skipped, 1 fail 191/240 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the translator change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/test_ps_front_face.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

@SquishyGithub

Copy link
Copy Markdown

Tested eea1754ea merged into main 37f215107 on NVIDIA GeForce RTX 3070 (Ampere), driver 620.70.06, Vulkan 1.4.357, subgroup 32..32, Linux (Bazzite 45; Fedora 45 distrobox, GCC 16.2.1, LLVM 23.1.5, SPIRV-Tools 2026.2, Python 3.15.2): pass

No fixture in runtime/test/shaders or scripts/run_gpu_tests.py reads the FRONT_FACE VGPR, so on hardware this checks for regressions only.

@morodirule

Copy link
Copy Markdown

Independent corpus recount on my own 1.0.2.1 copy (all 108 manifest files present at expected sizes; 26,503 disasm listings incl. 769 hidden .FS files, which a plain glob misses): exact match on every number using the translator's own layout walk (front VGPR = v(2 + popcount(pos mask)) when ISG1 declares sv==9, first source-use per listing).

  • declare SV_IsFrontFace: 1,984 (same)
  • never read VGPR: 1,639 (same)
  • read: 345 (same)
  • first-use v_cmp_ne_u32 imm0: 344 (your 282 e32 + 62 e64)
  • first-use v_cmp_eq_u32 imm0: 1 (same)
  • float compare / bit op / anything else: 0 (same)

Zero anomalies across the corpus. One methodology note for future counters: 13 of the readers live in dot-prefixed .FS listings that Python 3.11+ glob('*') and ls both skip — include hidden files or the read count comes out 332 instead of 345. Counts only, no game data. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.