Skip to content

Translator: border address modes in the image_gather4_l emulation (SHADER-348) - #2025

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
undeadhuman37:shader-348-gather-border
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
undeadhuman37:shader-348-gather-border

Conversation

@undeadhuman37

Copy link
Copy Markdown
Contributor

SHADER-348, claimed: #1902 (comment)
Found by @throny (finding); exposed by #1567.

What and why

Since #1567 tracks s_mov_b64/s_movk_i32/SGPR copies, the S# word 0 of 16 corpus gathers is a literal, and the literal path refused CLAMP mode 6 (clamp border) with Unsupported: image_gather4_l address mode 6 (border). Before #1567 the same S# went down the run-time path, which treated modes 4..7 as clamp-to-edge. Following "fix, don't disable", this adds the border-colour rule instead of restoring the approximation.

In the image_gather4_l(_o) emulation (four OpImageFetch in gather order):

  • Modes 6 (clamp border) and 7 (mirror once border): a tap whose coordinate, before the clamp, is outside [0, n-1] on either axis takes the S# border colour's channel instead of the fetched texel. Mode 7 is mirror once in range.
  • Border colour: BORDER_COLOR_TYPE = S# word 3 [31:30] (Mesa gfx10-rsrc.json SQ_IMG_SAMP_WORD3; values SQ_TEX_BORDER_COLOR_* in gfx6.json): 0 transparent black, 1 opaque black (alpha 1), 2 opaque white. An integer format gets 1, as Vulkan's INT_OPAQUE_*.
  • Literal and run-time S#s both work. A run-time S# selects on the mode and on word 3 at run time. Type 3 (a colour from the border-colour table, BORDER_COLOR_PTR) is Unsupported on a literal S# and transparent black at run time.
  • Unchanged: half-border modes 4 and 5 stay Unsupported on a literal S# and clamp-to-edge at run time (not in the corpus), and FORCE_UNNORMALIZED is unchanged.

One thing worth knowing: llvm-mc prints a word 3 of exactly 0x40000000/0x80000000/0xC0000000 as the inline floats 2.0/-2.0/-2.0, which sgpr_imm doesn't record, so those S#s take the run-time path. That path gives the right colour; only the table case differs (transparent black instead of Unsupported).

Tests

  • GPU: new runtime/test/shaders/ps_gather_border.asm, in check.py's TEXTURE_CONSTANTS (so scripts/run_gpu_tests.py runs it too), with expected values derived by hand in its header. It has four cases: modes 6/6 literal (opaque white, R and G), modes 7/7 at run time (opaque white, a mirrored tap that lands outside), and modes 6/6 at run time (transparent black). Clamp-to-edge would give a different value in every case.
    • Tested be94645 on RTX 4070 SUPER, driver 621.01, Windows 11: vk_texture_test vs_ids.spv ps_gather_border.spv … 0 92 c00000 14500000 0 gives all 4096 pixels = (32776, 34935, 34680, 28679) as expected. ps_gather_wrap still passes there.
  • Translator (tests/test_translate.py): test_gather4_l_literal_border_mode_selects_the_border_colour covers modes 6/7 on each axis × transparent black, opaque black (R and A) and opaque white. Also ..._border_colour_from_the_table_is_unsupported and ..._run_time_sampler_reads_the_border_type_from_word3. The old "border is Unsupported" test now covers half-border and FORCE_UNNORMALIZED only, and the run-time mode test counts the range checks.
  • python -m pytest tests/test_translate.py: 500 passed, 2 skipped (llvm-mc 21.1.8, SPIRV-Tools from Vulkan SDK 1.4.363.1)
  • python -m unittest discover -s tests/semantic: 1644 OK
  • python -I tests/test_run_gpu_tests.py OK; compileall and make_game.py --help OK
  • docs/design.md "Other additions" row: the rule, its sources and the GPU check.

I don't have the game, so I couldn't recount the corpus. @throny, your watch should show 26,503/26,503 again with this; I'd appreciate a count.

AI-assisted (Claude Code). No game files used.

🤖 Generated with Claude Code

…ADER-348)

Since ItsNotRexed#1567 made the game's S# words literals, 16 corpus gathers with
CLAMP mode 6 (clamp border) failed as Unsupported. Modes 6 and 7 (mirror
once border) now give a tap outside [0, n-1] the S# border colour's
channel (BORDER_COLOR_TYPE, word 3 [31:30]: transparent black, opaque
black, opaque white), for literal and run-time S#s; a table border
colour stays Unsupported on a literal S#, and half-border modes 4 and 5
keep their old handling. GPU test ps_gather_border, translator tests and
the contract row.

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

throny commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Corpus recount for SHADER-348, done on be946459. I used my own Xbox Series 1.0.2.1 copy (--mcpu gfx1013, rdna2spirv.py --no-asm --wave64-emulate, SPIRV-Tools 2026.4, Windows, Python 3.13). Counts only.

translated spirv-val valid failing
base 6a6704df 0 / 16 0 / 16 16 (image_gather4_l address mode 6 (border))
this PR be946459 16 / 16 16 / 16 0

These are the 16 shaders from the #1567 regression finding. All of them use mode 6, and none needs a half-border mode or BORDER_COLOR_TYPE 3, which this PR still refuses on a literal S#. A full-corpus run of this PR against 6a6704df (all 26,503) comes next, to check that nothing else changes. I'll add it here in about an hour.

I read the translator diff and it looks right to me. One question for the contract row: at run time, BORDER_COLOR_TYPE 3 (table) falls back to transparent black. Should docs/design.md say so explicitly, as it does for modes 4 and 5?

AI-assisted. No GPU run from me; your ps_gather_border result on the 4070 SUPER covers that.

…roximations (SHADER-348)

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

Copy link
Copy Markdown
Contributor Author

Thanks @throny, 16/16 is what I hoped for. On your question: the row did say it ("Type 3 … is Unsupported on a literal S# and transparent black at run time"), but you're right that it belongs with the other approximations. In 48339619 the sentence that names the remaining approximations now lists it next to modes 4/5 and FORCE_UNNORMALIZED, and the gather_addr_modes docstring says the same. It's a docs-only change; the gather tests still pass (70). I'll wait for your full-corpus run.

@undeadhuman37

Copy link
Copy Markdown
Contributor Author

Thanks for the recount. Yes: 4833961 (now the head) says it in docs/design.md. A table border colour (BORDER_COLOR_TYPE 3) on a run-time S# gives transparent black, and the contract row now lists it with the other run-time approximations, as it does for modes 4 and 5. A literal S# with type 3 is still Unsupported.

AI-assisted.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent Radeon verification (AI-assisted, Codex) at be946459, against 4eb6155d. I checked the subsequent 48339619 diff, which only changes the design notes and the gather function docstring.

On Windows 11 with a Ryzen 7 9800X3D Radeon iGPU (AMD driver 25.10.30.02), stock vk_texture_test matched all 4,096 pixels each for ps_gather_border and ps_gather_wrap. Khronos validation 1.4.363 was active for both runs and emitted zero diagnostics. The same three synthetic modules assembled with LLVM 21.1.8. Head translated and passed spirv-val for 3/3 (Vulkan 1.2, SPIRV-Tools 2026.4). Base passed 2/3 and rejected the border fixture at address mode 6. Focused translator tests passed 44/44 with uv CPython 3.12.13.

These are single-mip UNORM GPU fixtures. The Radeon result adds a second vendor to the author's RTX 4070 SUPER test and complements the separate 16-shader corpus recount.

@Flyander Flyander 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.

Border taps do not apply Vulkan format component substitution to border colors. For R8, alpha gather with transparent black should return 1 after substitution, but this path selects 0; with opaque white, missing G should return 0, but this path selects 1. Please apply format-specific border component replacement, then missing-component substitution, before selecting the gather channel. Khronos spec: https://docs.vulkan.org/spec/latest/chapters/textures.html

@crastinator

Copy link
Copy Markdown
Contributor

Tested 4833961 merged into main 4eb6155d (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New test passes natively: ps_gather_border (4096/4096 pixels exact).
  • Whole run_gpu_tests.py: 168/181 native, 168/181 with LEONIDA_W64_EMULATE=1. The skips are the D3D12-only fixtures and ps_w64x_readlane (RADV can't require a 64-wide fragment subgroup), the same as on main.
  • ctest: 36/36.
  • tests/test_translate.py: 520 passed, 2 skipped.

No game files. AI-assisted (Claude Code).

@Mikelesi

Mikelesi commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested be94645, Fedora 44, llvm-mc 22.1.8, NVIDIA RTX 4070 (driver 620.70.06):

  • main at 6a6704d: 26,487 / 26,503 translated and spirv-val valid; all 16 rejections are image_gather4_l address mode 6 (border).
  • The same 16 with this PR: 16/16 translated, 16/16 pass spirv-val, both with --wave64-emulate and without. With main, 0/16.
  • vk_pipeline_check on those 16 (all compute): 16 modules ok, 16 pipelines ok, 0 failed.

So the corpus is back to 26,503 / 26,503. I re-translated only the 16 rejected shaders with this branch, not the whole corpus.

AI-assisted (Claude Code).

@Impre-visible

Copy link
Copy Markdown

Tested 4833961 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18), llvm-mc 23.1.5, SPIRV-Tools v2026.4: pass, with my own 1.0.2.1 copy (counts only). This is the "32-wide GPU; game copy" check from the coordination list.

Corpus. A full make_game.py shaders stage at main 4eb6155 (26,503 listings, --wave64-emulate on) translated 26,487 and refused 16, all 16 with image_gather4_l address mode 6 (border), all compute. Those 16 listings, retranslated alone:

translator --wave64-emulate native
main 4eb6155 0 of 16 translated not run
this PR 48339619 16 of 16 translated, 16 pass spirv-val 16 of 16, 16 pass spirv-val
  • vk_pipeline_check on this GPU for the 16 emulated modules from this PR: 16/16 shader modules and 16/16 compute pipelines created, 0 failed.
  • So with this PR the corpus count on this copy goes from 26,487 to 26,503 of 26,503 (the other 26,487 were not retranslated with this branch: it is based on 6a6704d, behind the main I built).

Repository suite, native build of runtime/ from this head:

  • New fixture ps_gather_border: 4096/4096 pixels exact, natively and with LEONIDA_W64_EMULATE=1
  • tests/semantic: 1644 OK
  • build.py: 186 translated, 186 pass spirv-val; check.py: 123 of 158 pass, 35 skipped
  • run_gpu_tests.py: 179/179 translated and pass spirv-val; 134/174 passed, 40 skipped, 0 failed; with LEONIDA_W64_EMULATE=1: 165/174 passed, 9 skipped (8 fixtures run by the d3d12 tests, 1 needs a 64-wide fragment subgroup), 0 failed

Not covered here: whether the border colour the 16 shaders get matches the console (that needs their output, not their validity), and an AMD device.

AI-assisted run (Claude). Counts only; nothing from the game leaves this machine.

@esteban-dcp

Copy link
Copy Markdown

Tested 48339619 (the PR head as is, built from its own runtime/), Linux 7.3.5, scripts/run_gpu_tests.py, 0 failures in all three runs:

GPU Driver Result
NVIDIA GeForce RTX 4050 Laptop (Ada, subgroup 32) 620.73.04 134/174 passed, 40 skipped
same GPU, LEONIDA_W64_EMULATE=1 620.73.04 165/174 passed, 9 skipped
AMD Radeon 740M (RDNA3, RADV, subgroup 32..64) Mesa 26.3.1 165/174 passed, 9 skipped

The new ps_gather_border reports 4096/4096 pixels exact in all three runs. 179 shaders translate and pass spirv-val; tests/semantic 1644 OK, tests/test_translate.py 500 passed (2 skipped).

No game files.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@undeadhuman37

Copy link
Copy Markdown
Contributor Author

Full-corpus counts from my own Xbox Series 1.0.2.1 copy (rdna2spirv.py --wave64-emulate, 14 shards merged, Windows). Counts only.

translated spirv-val valid
main 4eb6155 26,487 / 26,503 26,487
this PR 26,503 / 26,503 26,503

That's the same 16 shaders as @throny's recount, and no shader that translated before is lost.

AI-assisted.

@undeadhuman37

Copy link
Copy Markdown
Contributor Author

@Flyander thanks, you're right. Fixed in ba3aca5.

A border tap now takes the border colour only for channels the texture's format has. The format is the T# FORMAT, word 1 [28:20], with its component count from the translator's FORMATS. A channel the format lacks reads 0 for R/G/B and 1 for A, which is Vulkan's border replacement (Table 2) followed by component substitution, and also what a fetched texel of that format reads. So your two cases now give R8 alpha with transparent black = 1 and R8 green with opaque white = 0. A literal T# decides this at translation time and a run-time T# at run time. A format outside FORMATS keeps all four channels as before.

I used the format rather than the T#'s DST_SEL because the emulation's other taps are Vulkan fetches through the view, so the border taps should match them.

Tests (RTX 4070 SUPER, driver 621.01, Windows 11):

  • New test_gather4_l_border_channel_missing_from_the_format_is_substituted covers R8 and RG32F with alpha, green, red and blue, and ..._run_time_texture_reads_its_format_for_the_border covers a run-time T#. 4 of the 6 cases fail on the previous head; the other two expect the border's own value.
  • tests/test_translate.py: 506 passed, 2 skipped. tests/semantic: OK. compileall and make_game.py --help: OK.
  • GPU: ps_gather_border (RGBA8, so unchanged) and ps_gather_wrap still give 4096/4096 exact.
  • The docs/design.md row says it.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 48339619 (Vonsek4, AI-assisted):

  • GPU, scripts/run_gpu_tests.py --filter gather (runtime built from main with MSVC/Ninja): ps_gather_border 4096/4096 exact, ps_gather_wrap 4096/4096 exact, ps_gather_sampler_lod pass, on both machines:
    • AMD Radeon Graphics (RDNA2 gfx1036, wave64), driver 32.0.21045.16010, with LEONIDA_VK_DEVICE=radeon
    • NVIDIA RTX 5080 (32-wide), driver 32.0.16.1692
    • Windows 11 on both
  • Game copy (1.0.2.1, counts only): every corpus shader that contains image_gather4_l (4,049) translates and passes spirv-val with this branch, 4,049/4,049 in 4 shards. main (49475796) also translates all 4,049 on my copy, so I can't reproduce the 16 Unsupported from the description there. Either they come from a different main or a different disassembly, or main already takes another path for them.

Note: this and #2144 (SHADER-369, which replaces OpSMod in gather_addr) touch the same function, so whichever lands second needs a small rebase.

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.

10 participants