Shaders: apply v_interp_p2_f16 modifiers in f32 before narrow16 #2282

Open
github-bot wants to merge 1 commit from github-bot/gh-2282 into main AGit
Owner

@⁠BlueCatsz on GitHub · 2026-11-25 09:39 UTC · original
BlueCatsz:fix/interp-p2-f16-modifiers-single-narrow → main · +175 −30 · 2 files · 1 commits · open

Problem

Follow-up to #1696: in crates/victory-shader/src/emit/lower.rs, Lower::interp narrowed interpolated 16-bit values via self.narrow16(f) inside its half closure and then passed the already-narrowed f16 into Lower::half_modifiers, which widened back via dx.op.legacyF16ToF32, applied omod and clamp, and narrowed a second time via dx.op.legacyF32ToF16.

That caused two bugs on v_interp_p2_f16 with omod or clamp:

  1. Double conversion before omod:
    • Values that overflow f16 before omod is applied lose finite results (e.g., 98304.0 with div:2 narrowed to 0x7C00 / 0x7BFF -> 0x77FF instead of 49152.0 / 0x7A00).
    • Values that round at a subnormal boundary before omod double-round (e.g., (2^-15 + 2^-25) * 4.0 rounded away 2^-25 into 0x0200 and produced 0x0800 instead of 0x0801).
  2. HW_REG_MODE ignored / host-dependent rounding:
    • Narrowing through dx.op.legacyF32ToF16 instead of self.narrow16 ignores self.mode16 (HW_REG_MODE bits 3:2) and rounds toward zero on WARP and AMD even in default RNE mode (e.g., 32768.0 * 2.0 -> 0x7BFF instead of 0x7C00, and clamp((2^-14 + 3 * 2^-24) * 0.5) -> 0x0201 instead of 0x0202).

Fix

  • crates/victory-shader/src/emit/lower.rs:
    • Keep Lower::interp's Some(pack2) helper in f32 (half_f32) and pass f directly to Lower::half_modifiers for v_interp_p2_f16 (flat mode == 1 packs both halves via self.narrow16(f) as before).
    • Update Lower::half_modifiers to apply omod then clamp on the f32 value and convert once via self.narrow16(f), removing legacyF16ToF32 and legacyF32ToF16.

Tests

  • crates/victory-shader/src/emit/mod.rs:
    • Added emit::tests::v_interp_p2_f16_modifiers_translate_in_both_round_modes (cross-platform lowering + Windows DXIL validator check).
    • Added emit::tests::v_interp_p2_f16_modifiers_narrow_once_in_mode16 (#[cfg(windows)] synthetic VS+PS Host::draw executing both default RNE and HW_REG_MODE RTZ cases via v_cvt_f32_u32 export; asserts ran > 0).
    • Tightened emit::tests::interpolated_f16_writes_the_op_sel_half_with_modifiers to check that emitted DXIL does not contain legacyF16ToF32 or legacyF32ToF16 (when dxc.exe is present).

Verification

  • Fork windows-latest CI run: https://github.com/BlueCatsz/VIctory/actions/runs/45586121960
    • Clippy: passed
    • Clippy, diagnostics build: passed
    • Tests: victory-shader 249 passed, 0 failed, 3 ignored (including v_interp_p2_f16_modifiers_narrow_once_in_mode16 executing on WARP); xtask 6 passed, 0 failed
  • Local Linux checks:
    • cargo test --offline --locked -p victory-shader --lib: 145 passed, 0 failed, 2 ignored
    • cargo --offline xtask policy: asset policy: 182 files ok

Disclosure

  • AI-assisted diagnosis, patch, and tests. Verified only with synthetic shaders on WARP (windows-latest CI) and Linux offline unit/policy checks (no local hardware GPU draw run, no console hardware, zero game files).
**@⁠BlueCatsz** on GitHub · 2026-11-25 09:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282) `BlueCatsz:fix/interp-p2-f16-modifiers-single-narrow` → `main` · +175 −30 · 2 files · 1 commits · open ### Problem Follow-up to #1696: in `crates/victory-shader/src/emit/lower.rs`, `Lower::interp` narrowed interpolated 16-bit values via `self.narrow16(f)` inside its `half` closure and then passed the already-narrowed `f16` into `Lower::half_modifiers`, which widened back via `dx.op.legacyF16ToF32`, applied `omod` and `clamp`, and narrowed a second time via `dx.op.legacyF32ToF16`. That caused two bugs on `v_interp_p2_f16` with `omod` or `clamp`: 1. **Double conversion before `omod`:** - Values that overflow `f16` before `omod` is applied lose finite results (e.g., `98304.0` with `div:2` narrowed to `0x7C00` / `0x7BFF -> 0x77FF` instead of `49152.0` / `0x7A00`). - Values that round at a subnormal boundary before `omod` double-round (e.g., `(2^-15 + 2^-25) * 4.0` rounded away `2^-25` into `0x0200` and produced `0x0800` instead of `0x0801`). 2. **`HW_REG_MODE` ignored / host-dependent rounding:** - Narrowing through `dx.op.legacyF32ToF16` instead of `self.narrow16` ignores `self.mode16` (`HW_REG_MODE` bits 3:2) and rounds toward zero on WARP and AMD even in default RNE mode (e.g., `32768.0 * 2.0` -> `0x7BFF` instead of `0x7C00`, and `clamp((2^-14 + 3 * 2^-24) * 0.5)` -> `0x0201` instead of `0x0202`). ### Fix - `crates/victory-shader/src/emit/lower.rs`: - Keep `Lower::interp`'s `Some(pack2)` helper in `f32` (`half_f32`) and pass `f` directly to `Lower::half_modifiers` for `v_interp_p2_f16` (flat `mode == 1` packs both halves via `self.narrow16(f)` as before). - Update `Lower::half_modifiers` to apply `omod` then `clamp` on the `f32` value and convert once via `self.narrow16(f)`, removing `legacyF16ToF32` and `legacyF32ToF16`. ### Tests - `crates/victory-shader/src/emit/mod.rs`: - Added `emit::tests::v_interp_p2_f16_modifiers_translate_in_both_round_modes` (cross-platform lowering + Windows DXIL validator check). - Added `emit::tests::v_interp_p2_f16_modifiers_narrow_once_in_mode16` (`#[cfg(windows)]` synthetic VS+PS `Host::draw` executing both default RNE and `HW_REG_MODE` RTZ cases via `v_cvt_f32_u32` export; asserts `ran > 0`). - Tightened `emit::tests::interpolated_f16_writes_the_op_sel_half_with_modifiers` to check that emitted DXIL does not contain `legacyF16ToF32` or `legacyF32ToF16` (when `dxc.exe` is present). ### Verification - Fork `windows-latest` CI run: https://github.com/BlueCatsz/VIctory/actions/runs/45586121960 - `Clippy`: passed - `Clippy, diagnostics build`: passed - `Tests`: `victory-shader` 249 passed, 0 failed, 3 ignored (including `v_interp_p2_f16_modifiers_narrow_once_in_mode16` executing on WARP); `xtask` 6 passed, 0 failed - Local Linux checks: - `cargo test --offline --locked -p victory-shader --lib`: 145 passed, 0 failed, 2 ignored - `cargo --offline xtask policy`: `asset policy: 182 files ok` ### Disclosure - AI-assisted diagnosis, patch, and tests. Verified only with synthetic shaders on WARP (`windows-latest` CI) and Linux offline unit/policy checks (no local hardware GPU draw run, no console hardware, zero game files).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:00:50 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 09:51 UTC · original

Comparison with the still-open #1338: I ran the same four-channel synthetic draw from this PR at c4c7758, its parent 940d32f, and #1338's older head e2dc6ba (test helpers transferred; production code unchanged). Hardware and WARP agreed on each revision.

Revision Round-to-nearest-even Round-toward-zero
940d32f 4 of 4 channels differed from expected 2 of 4 differed
#1338 e2dc6ba 2 of 4 differed all matched
#2282 c4c7758 all matched all matched

On #1338, the overflow lane yielded 0x7bff and the subnormal tie yielded 0x0201 even in nearest-even mode (expected 0x7c00 and 0x0202). #2282 distinguished those modes on both backends. I cross-checked the expected patterns using exact rational inputs and the representable positive binary16 values. This comparison is for these four vectors on each branch's own base; it helps distinguish the two PRs' coverage.

AI-assisted, synthetic test only.

**@⁠Dathinel** on GitHub · 2026-11-25 09:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#issuecomment-7158901398) Comparison with the still-open #1338: I ran the same four-channel synthetic draw from this PR at c4c7758, its parent 940d32f, and #1338's older head e2dc6ba (test helpers transferred; production code unchanged). Hardware and WARP agreed on each revision. | Revision | Round-to-nearest-even | Round-toward-zero | |---|---|---| | 940d32f | 4 of 4 channels differed from expected | 2 of 4 differed | | #1338 e2dc6ba | 2 of 4 differed | all matched | | #2282 c4c7758 | all matched | all matched | On #1338, the overflow lane yielded 0x7bff and the subnormal tie yielded 0x0201 even in nearest-even mode (expected 0x7c00 and 0x0202). #2282 distinguished those modes on both backends. I cross-checked the expected patterns using exact rational inputs and the representable positive binary16 values. This comparison is for these four vectors on each branch's own base; it helps distinguish the two PRs' coverage. AI-assisted, synthetic test only.
Author
Owner

@⁠Mericas77 on GitHub · 2026-11-25 10:13 UTC · original

NVIDIA hardware test report: RTX 4070, driver 621.01, Windows 11, Rust 1.98.1 MSVC. I ran head c4c7758 against main 940d32f; the head branches from 940d32f and only changes emit/lower.rs and emit/mod.rs.

This PR: v_interp_p2_f16_modifiers_narrow_once_in_mode16, v_interp_p2_f16_modifiers_translate_in_both_round_modes and interpolated_f16_writes_the_op_sel_half_with_modifiers all pass, on both hardware and WARP (no "skipped" line).

A/B: I ran the PR's mod.rs tests on top of main's production code. They fail on hardware:

hardware rtz=false texel 0: got [0x7c00, 0x7bff, 0x0800, 0x0201], want [0x7a00, 0x7c00, 0x0801, 0x0202]

So on NVIDIA, main shows the same four symptoms the PR describes:

  • overflow before omod (ch0)
  • round-toward-zero from legacyF32ToF16 in default RNE mode (ch1 and ch3)
  • double rounding at the subnormal boundary (ch2)

When I forced WARP for the "hardware" pass, it gave exactly the same values. The legacyF32ToF16 rounding is therefore not AMD- or WARP-specific: NVIDIA rounds the same way, and the PR's narrow16 path fixes it there too.

cargo test -p victory-shader --lib --locked gives 246 passed, 3 failed. The 3 failures are the ones main already has on this card: stride_tests ×2 and gathers_at_a_level_match_the_hardware.

AI-assisted. Synthetic tests only, no game data.

**@⁠Mericas77** on GitHub · 2026-11-25 10:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#issuecomment-7159394765) NVIDIA hardware test report: RTX 4070, driver 621.01, Windows 11, Rust 1.98.1 MSVC. I ran head `c4c7758` against `main` `940d32f`; the head branches from `940d32f` and only changes `emit/lower.rs` and `emit/mod.rs`. **This PR:** `v_interp_p2_f16_modifiers_narrow_once_in_mode16`, `v_interp_p2_f16_modifiers_translate_in_both_round_modes` and `interpolated_f16_writes_the_op_sel_half_with_modifiers` all pass, on both hardware and WARP (no "skipped" line). **A/B:** I ran the PR's `mod.rs` tests on top of `main`'s production code. They fail on hardware: ``` hardware rtz=false texel 0: got [0x7c00, 0x7bff, 0x0800, 0x0201], want [0x7a00, 0x7c00, 0x0801, 0x0202] ``` So on NVIDIA, `main` shows the same four symptoms the PR describes: - overflow before `omod` (ch0) - round-toward-zero from `legacyF32ToF16` in default RNE mode (ch1 and ch3) - double rounding at the subnormal boundary (ch2) When I forced WARP for the "hardware" pass, it gave exactly the same values. The `legacyF32ToF16` rounding is therefore not AMD- or WARP-specific: NVIDIA rounds the same way, and the PR's `narrow16` path fixes it there too. `cargo test -p victory-shader --lib --locked` gives 246 passed, 3 failed. The 3 failures are the ones `main` already has on this card: `stride_tests` ×2 and `gathers_at_a_level_match_the_hardware`. AI-assisted. Synthetic tests only, no game data.
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 10:25 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head c4c77581 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 10:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#issuecomment-7159669653) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head c4c77581 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 13:31 UTC · original · edited 2026-11-26 01:25 UTC

Automated triage for the maintainers.

Merge candidate. The head is rebased (7fd4de1) and merges cleanly into current main. In Lower::interp, the v_interp_p2_f16 path now keeps the interpolated value in f32. Lower::half_modifiers applies omod, then clamp, then rounds once through Lower::narrow16, which honours mode16. Before, the value went through legacyF16ToF32/legacyF32ToF16: it was rounded twice and ignored the round mode. main's own comment on half_modifiers already called for a single rounding in f32, so this makes the code match it. half_modifiers has no other caller, and the flat mode == 1 path still narrows each half as before. Independent NVIDIA and WARP runs reproduced the four wrong values on main, and the PR's values match. This PR covers more than the older #1338. CI (check) passes on this head.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 13:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#issuecomment-7163910860) · edited 2026-11-26 01:25 UTC <!-- victory-triage v1 head=7fd4de19eb58da21d089bd6026e39e7fafc70454 verdict=merge-candidate at=2026-11-26T01:25:22Z --> Automated triage for the maintainers. **Merge candidate.** The head is rebased (`7fd4de1`) and merges cleanly into current `main`. In `Lower::interp`, the `v_interp_p2_f16` path now keeps the interpolated value in f32. `Lower::half_modifiers` applies omod, then clamp, then rounds once through `Lower::narrow16`, which honours `mode16`. Before, the value went through `legacyF16ToF32`/`legacyF32ToF16`: it was rounded twice and ignored the round mode. `main`'s own comment on `half_modifiers` already called for a single rounding in f32, so this makes the code match it. `half_modifiers` has no other caller, and the flat `mode == 1` path still narrows each half as before. Independent NVIDIA and WARP runs reproduced the four wrong values on `main`, and the PR's values match. This PR covers more than the older #1338. CI (`check`) passes on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 09:42 UTC · original · approved these changes

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib v_interp_p2_f16_ — 2 passed, 0 failed (v_interp_p2_f16_modifiers_translate_in_both_round_modes and v_interp_p2_f16_modifiers_narrow_once_in_mode16)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fix applying v_interp_p2_f16 modifiers in f32 before narrowing once to f16. Looks good.

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-25 09:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#pullrequestreview-6055166597) · **approved these changes** Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC, with `dxil.dll` validator): - `cargo test -p victory-shader --lib v_interp_p2_f16_` — **2 passed, 0 failed** (`v_interp_p2_f16_modifiers_translate_in_both_round_modes` and `v_interp_p2_f16_modifiers_narrow_once_in_mode16`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean fix applying `v_interp_p2_f16` modifiers in f32 before narrowing once to f16. Looks good. AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 10:19 UTC · original · approved these changes

Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — v_interp_p2_f16 modifiers applied in f32 before packing.

**@⁠catminou4** on GitHub · 2026-11-25 10:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#pullrequestreview-6055710361) · **approved these changes** Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — v_interp_p2_f16 modifiers applied in f32 before packing.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 18:11 UTC · original · approved these changes

Looks good. Verified merged onto main (63bcca43) on an RTX 5090, Microsoft WARP, and Windows SDK dxil.dll:

  • Keeping interpolated f16 inputs in f32 (half_f32 at crates/victory-shader/src/emit/lower.rs:8134) and applying omod then clamp before a single self.narrow16(f) in half_modifiers (lower.rs:8305) eliminates double rounding, avoids premature f16 overflow before div:2, and honors HW_REG_MODE (RNE/RTZ), while shrinking emit/lower.rs to 520,292 B (< 524,288 B).
  • cargo xtask policy (191 files ok) and cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features) pass.
  • All 3 PR tests (v_interp_p2_f16_modifiers_translate_in_both_round_modes, v_interp_p2_f16_modifiers_narrow_once_in_mode16, and interpolated_f16_writes_the_op_sel_half_with_modifiers) pass on both RTX 5090 and WARP with dxil.dll validation.
  • An additional GPU draw probe confirmed clamp on negative (-0.5 * 2 -> 0x0000), above-one (0.75 * 2 -> 0x3C00), and qNaN (-> 0x0000) inputs plus op_sel[3] = 1 high-half writes preserving a 0xBEEF low-half sentinel (98304.0 * 0.5 -> 0x7A00_BEEF) in both RNE and RTZ modes on both RTX 5090 and WARP.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 18:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/2282#pullrequestreview-6078826404) · **approved these changes** Looks good. Verified merged onto `main` (`63bcca43`) on an RTX 5090, Microsoft WARP, and Windows SDK `dxil.dll`: - Keeping interpolated `f16` inputs in `f32` (`half_f32` at `crates/victory-shader/src/emit/lower.rs:8134`) and applying `omod` then `clamp` before a single `self.narrow16(f)` in `half_modifiers` (`lower.rs:8305`) eliminates double rounding, avoids premature f16 overflow before `div:2`, and honors `HW_REG_MODE` (RNE/RTZ), while shrinking `emit/lower.rs` to 520,292 B (< 524,288 B). - `cargo xtask policy` (`191 files ok`) and `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`) pass. - All 3 PR tests (`v_interp_p2_f16_modifiers_translate_in_both_round_modes`, `v_interp_p2_f16_modifiers_narrow_once_in_mode16`, and `interpolated_f16_writes_the_op_sel_half_with_modifiers`) pass on both RTX 5090 and WARP with `dxil.dll` validation. - An additional GPU draw probe confirmed `clamp` on negative (`-0.5 * 2 -> 0x0000`), above-one (`0.75 * 2 -> 0x3C00`), and `qNaN` (`-> 0x0000`) inputs plus `op_sel[3] = 1` high-half writes preserving a `0xBEEF` low-half sentinel (`98304.0 * 0.5 -> 0x7A00_BEEF`) in both RNE and RTZ modes on both RTX 5090 and WARP.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2282/head:github-bot/gh-2282'
git switch 'github-bot/gh-2282'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2282'
git switch 'github-bot/gh-2282'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2282'
git switch 'github-bot/gh-2282'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2282'
git switch 'main'
git merge --squash 'github-bot/gh-2282'
git switch 'main'
git merge --ff-only 'github-bot/gh-2282'
git switch 'main'
git merge 'github-bot/gh-2282'
git push origin 'main'
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!2282
No description provided.