Shaders: support image_sample_b with a16 and _cl LOD-clamp variants #73

Closed
github-bot wants to merge 2 commits from gh-pr/73/head into gh-pr/73/base
Owner

@⁠Yaekai on GitHub · 2026-11-24 00:13 UTC · original
Yaekai:feat/shader-image-sample-a16-cl → main · +63 −40 · 2 files · 2 commits · closed without merging

In GFX10/RDNA2 MIMG instructions with a16=1, offset (_o), bias (_b), and z-compare (_c) remain full 32-bit VGPRs at the start of vaddr, while gradients, coordinates, LOD, and clamp are packed in 16-bit pairs starting after them (matching gfx10::fields::image_addr_dwords).

Changes

  • Split image address reading in crates/victory-shader/src/emit/lower.rs into full 32-bit prefix VGPRs (addr_u32) and a16-packed trailing components (addr_packed), removing the a16 + image_sample_b bailout.
  • Added support for _cl LOD-clamp variants (image_sample_cl, image_sample_b_cl, image_sample_d_cl, image_sample_c_cl) by passing the trailing clamp component to DXIL's clamp argument.
  • Moved the unsupported cube sample check (unorm / gradients / get_lod) ahead of coordinate reads so cube gradient samples report the right reason instead of an out-of-bounds address.

Verification

  • cargo test -p victory-shader: 57/57 passed.
  • victory translate --validate on GTA VI shaders.rpf (26,084 programs):
    • a biased image sample with 16-bit coordinates: 108 → 0
    • image_sample_d_cl: 27 → 0
    • Emitted VS/PS DXIL programs: 12,272 → 12,316 (+44 complete shaders, all passing dxil.dll validation with 0 new validator errors).
**@⁠Yaekai** on GitHub · 2026-11-24 00:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/73) `Yaekai:feat/shader-image-sample-a16-cl` → `main` · +63 −40 · 2 files · 2 commits · closed without merging In GFX10/RDNA2 MIMG instructions with `a16=1`, `offset` (`_o`), `bias` (`_b`), and `z-compare` (`_c`) remain full 32-bit VGPRs at the start of `vaddr`, while gradients, coordinates, LOD, and clamp are packed in 16-bit pairs starting after them (matching `gfx10::fields::image_addr_dwords`). ### Changes - Split image address reading in `crates/victory-shader/src/emit/lower.rs` into full 32-bit prefix VGPRs (`addr_u32`) and `a16`-packed trailing components (`addr_packed`), removing the `a16` + `image_sample_b` bailout. - Added support for `_cl` LOD-clamp variants (`image_sample_cl`, `image_sample_b_cl`, `image_sample_d_cl`, `image_sample_c_cl`) by passing the trailing clamp component to DXIL's `clamp` argument. - Moved the unsupported cube sample check (`unorm` / gradients / `get_lod`) ahead of coordinate reads so cube gradient samples report the right reason instead of an out-of-bounds address. ### Verification - `cargo test -p victory-shader`: 57/57 passed. - `victory translate --validate` on GTA VI `shaders.rpf` (26,084 programs): - `a biased image sample with 16-bit coordinates`: `108` → `0` - `image_sample_d_cl`: `27` → `0` - Emitted VS/PS DXIL programs: `12,272` → `12,316` (**+44** complete shaders, all passing `dxil.dll` validation with 0 new validator errors).
In GFX10/RDNA2 MIMG instructions with a16=1, offset (_o), bias (_b) and z-compare (_c) remain full 32-bit VGPRs at the start of vaddr, while gradients, coordinates, LOD and clamp are packed in 16-bit pairs starting after them (matching gfx10::fields::image_addr_dwords).

- Split address reading into full 32-bit prefix VGPRs and a16-packed trailing components, removing the a16+image_sample_b bailout.
- Support _cl LOD-clamp variants (image_sample_cl, image_sample_b_cl, image_sample_d_cl, image_sample_c_cl) by passing the trailing clamp component to DXIL's clamp parameter.
- Check unsupported cube sample modes before reading coordinates so cube gradient samples report the right error instead of an out-of-bounds address.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:49:14 +00:00
github-bot closed this pull request 2026-11-26 03:49:14 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:59 UTC · original

Tested 6c8af09 on Arch Linux x86_64, rustc 1.98.1: pass, no regression only.

  • cargo test -p victory-shader: 51 passed, 0 failed (2 test binaries). The diff adds no test for the image_sample_b a16 / _cl LOD-clamp variants, so the new paths are unexercised; a fixture per variant would cover them.
  • The full workspace still needs #112 for the victory-gpu E0425.

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/73#issuecomment-7118648252) Tested `6c8af09` on **Arch Linux x86_64, rustc 1.98.1**: **pass, no regression only**. - `cargo test -p victory-shader`: **51 passed, 0 failed** (2 test binaries). The diff adds no test for the `image_sample_b` a16 / `_cl` LOD-clamp variants, so the new paths are unexercised; a fixture per variant would cover them. - The full workspace still needs #112 for the `victory-gpu` E0425. No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-24 01:29 UTC · original

With a16 set, the LOD bias is an f16 in the low half of its own dword, and the upper 16 bits are undefined. See LLVM's SITargetLowering::lowerImage: "Bias is of type half but occupies full 32-bit" (D111754/D116038). Here the whole dword is read as an f32 (crates/victory-shader/src/emit/lower.rs:5555-5556), so an a16 image_sample_b* gets a garbage bias. Converting the low half with legacyF16ToF32 would fix it. #160 does it that way and points out the disagreement. #136 has the same code (lower.rs:5660-5661).

Static review, not built or run.

**@⁠Sunkiller-qc** on GitHub · 2026-11-24 01:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/73#issuecomment-7119150703) With `a16` set, the LOD bias is an f16 in the low half of its own dword, and the upper 16 bits are undefined. See LLVM's `SITargetLowering::lowerImage`: "Bias is of type half but occupies full 32-bit" (D111754/D116038). Here the whole dword is read as an f32 (`crates/victory-shader/src/emit/lower.rs:5555-5556`), so an a16 `image_sample_b*` gets a garbage bias. Converting the low half with `legacyF16ToF32` would fix it. #160 does it that way and points out the disagreement. #136 has the same code (`lower.rs:5660-5661`). Static review, not built or run.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-24 01:43 UTC · original

A note from comparing this with #160, which covers the same a16 bias and _cl forms: the two read the a16 bias differently. Here the bias dword is taken as a full f32 (addr_u32), in #160 as an f16 in the low half of that dword. LLVM's SITargetLowering::lowerImage has:

// Special handling of bias when A16 is on. Bias is of type half but
// occupies full 32-bit.
SDValue Bias = DAG.getBuildVector(
    MVT::v2f16, DL,
    {Op.getOperand(ArgOffset + I), DAG.getPOISON(MVT::f16)});

so with a16 the bias keeps its own dword but holds a half in the low 16 bits (the upper half is undefined); the offset and the comparison reference stay 32-bit. Read as an f32, an f16 bias comes out as a denormal near zero, so the sample would lose its bias without failing.

One related point I got wrong myself and fixed in #160: on GFX10.3 a16 does not make the derivatives 16-bit (only the _g16 opcodes do); LLVM's gfx10_asm_mimg.s has image_sample_d ... 2D a16 with five address dwords. Worth checking here if _d forms with a16 pass through this code.

From the sources only, not run on hardware. No game data used.

**@⁠r4tedx** on GitHub · 2026-11-24 01:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/73#issuecomment-7119382833) A note from comparing this with #160, which covers the same a16 bias and `_cl` forms: the two read the a16 bias differently. Here the bias dword is taken as a full f32 (`addr_u32`), in #160 as an f16 in the low half of that dword. LLVM's `SITargetLowering::lowerImage` has: ```cpp // Special handling of bias when A16 is on. Bias is of type half but // occupies full 32-bit. SDValue Bias = DAG.getBuildVector( MVT::v2f16, DL, {Op.getOperand(ArgOffset + I), DAG.getPOISON(MVT::f16)}); ``` so with a16 the bias keeps its own dword but holds a half in the low 16 bits (the upper half is undefined); the offset and the comparison reference stay 32-bit. Read as an f32, an f16 bias comes out as a denormal near zero, so the sample would lose its bias without failing. One related point I got wrong myself and fixed in #160: on GFX10.3 a16 does not make the derivatives 16-bit (only the `_g16` opcodes do); LLVM's `gfx10_asm_mimg.s` has `image_sample_d ... 2D a16` with five address dwords. Worth checking here if `_d` forms with a16 pass through this code. From the sources only, not run on hardware. No game data used.
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 02:31 UTC · original

Thanks @⁠Sunkiller-qc and @⁠r4tedx for the great catches! Updated and rebased onto d2a4bae in 3eab56a:

  • With a16, LOD bias is now masked to its low 16 bits and converted via legacyF16ToF32 instead of being bitcast as f32.
  • Non-_g16 image_sample_d* derivatives now stay 32-bit (addr_u32 + as_float, and image_addr_dwords in gfx10/fields.rs only halves derivative dwords for _g16 opcodes).
  • Re-verified cargo test -p victory-shader (71/71 passed) and victory translate --validate on shaders.rpf (0 new validator errors).
**@⁠Yaekai** on GitHub · 2026-11-24 02:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/73#issuecomment-7120229206) Thanks @⁠Sunkiller-qc and @⁠r4tedx for the great catches! Updated and rebased onto `d2a4bae` in `3eab56a`: - With `a16`, LOD bias is now masked to its low 16 bits and converted via `legacyF16ToF32` instead of being bitcast as `f32`. - Non-`_g16` `image_sample_d*` derivatives now stay 32-bit (`addr_u32` + `as_float`, and `image_addr_dwords` in `gfx10/fields.rs` only halves derivative dwords for `_g16` opcodes). - Re-verified `cargo test -p victory-shader` (71/71 passed) and `victory translate --validate` on `shaders.rpf` (0 new validator errors).
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:23 UTC · original

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae:

VS/PS validated CS validated MS validated cargo test -p victory-shader
main 12172 1670 8129 ok
#569 12208 1670 8129 ok
#73 12208 1670 8129 ok
#318 12208 1670 8129 ok
#306 12316 1693 8138 ok

All four clear the 108 "biased image sample with 16-bit coordinates" programs identically. #73 and #318 also clear the 27 image_sample_d_cl refusals, but those programs then stop on another reason (emitted count unchanged). #306 additionally clears 15 of the 16 validator error kinds (only the mesh 32 KiB output size remains, see #459); it overlaps #554/#565/#381. Windows 11, dxil.dll from SDK 10.0.26100.

**@⁠slint6614-dev** on GitHub · 2026-11-24 07:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/73#issuecomment-7124941658) Measured on the full retail shader pack (26,503 containers) with `victory translate --validate`, against main d2a4bae: | | VS/PS validated | CS validated | MS validated | `cargo test -p victory-shader` | |---|---|---|---|---| | main | 12172 | 1670 | 8129 | ok | | #569 | 12208 | 1670 | 8129 | ok | | #73 | 12208 | 1670 | 8129 | ok | | #318 | 12208 | 1670 | 8129 | ok | | #306 | 12316 | 1693 | 8138 | ok | All four clear the 108 "biased image sample with 16-bit coordinates" programs identically. #73 and #318 also clear the 27 `image_sample_d_cl` refusals, but those programs then stop on another reason (emitted count unchanged). #306 additionally clears 15 of the 16 validator error kinds (only the mesh 32 KiB output size remains, see #459); it overlaps #554/#565/#381. Windows 11, dxil.dll from SDK 10.0.26100.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!73
No description provided.