Shaders: VICTORY_F16_NEAREST rounds packed and mixed-precision 16-bit results too, and follows the program's round mode #239

Open
murillojndem wants to merge 2 commits from upstream-pr/239 into main
murillojndem commented 2026-11-30 01:43:53 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-30T01:43:53Z.


VICTORY_F16_NEAREST=1 makes 16-bit float results round as the console rounds them, instead of as the host's f32tof16 does: that one rounds toward zero on AMD and NVIDIA. The switch was read only for valu16_op's results (v_add_f16, v_cvt_f16_f32 and the like). Packed results (v_pk_add_f16, v_pk_mul_f16, v_pk_fma_f16 ...) and the mixed-precision ones (v_fma_mixlo_f16, v_fma_mixhi_f16) still went through the host's conversion, so with the switch on, an RX 7900 XT still rounded them toward zero. The console also rounds 16-bit results as its MODE register says (bits 3:2, set by s_round_mode or by an s_setreg of MODE), and the translation ignored both: with the switch on, a program that asked for rounding toward zero got rounding to nearest. (A refused s_setreg is an instruction without a side effect or a result, so the program is translated without it.)

Change (emit/lower.rs):

  • round_modes gives the 16-bit round mode in force at each instruction: nearest even where the program starts, then as s_round_mode, or an s_setreg of MODE with a constant, sets it; unknown after an s_setreg of a register's value, or where paths that set it differently join. Lower::inst takes it into mode16.
  • narrow converts a 16-bit result. Without the switch it is the host's conversion, as before. With it, f16_toward_zero where the mode is toward zero, else f16_nearest, both whatever the host's conversion does.
  • valu16_op, packed16, the v_fma_mix*_f16 results and the f32 wave sum of halves (half_sum_here) use narrow. The exact wave sum of halves (add_halves), which rounds to nearest with or without the switch, rounds toward zero where the mode says so.

Without the switch the translation is the same as before, except for that exact wave sum under a toward-zero mode.

The round-mode pass follows upstream Marvisak/VIctory 77053a0 (Marvis), which 570a941 removed with the rest of that commit. The conversions are this repository's f16_nearest and f16_toward_zero, and the switch stays opt-in as here.

Tests, in emit::wave64_tests, on the hardware device:

  • a_packed_16_bit_sum_rounds_to_nearest_when_asked_to: v_pk_add_f16 of 0x3c03 + 0x4000 in both halves, the tie of the existing a_16_bit_sum_rounds_to_nearest_when_asked_to, which the console rounds up to 0x4202. With the switch on, main gives 0x42014201 on the RX 7900 XT; this change gives 0x42024202.
  • a_mixed_precision_16_bit_result_rounds_to_nearest_when_asked_to: v_fma_mixlo_f16 of 0x3c03 * 1.0 + 0x4000 into the low half gives 0x12344202, the high half kept. A mutant with the host's conversion at that site gives 0x12344201.
  • a_16_bit_sum_rounds_toward_zero_where_the_program_sets_it: s_round_mode 0xc, or s_setreg_imm32_b32 hwreg(HW_REG_MODE, 2, 2), 3, then the same v_add_f16: 0x4201 with the switch on. Main gives 0x4202 for both, and so does a mutant that leaves s_setreg out. Without the switch the test takes the host's value, which is 0x4201 on the RX 7900 XT.
  • victory-shader --lib on the RX 7900 XT and WARP with one test thread: 240 passed, 2 failed, 4 ignored (main: 237, 2, 4). The 2 failures (emit::rect::tests::rectangles_are_drawn_whole and the_rectangle_stage_is_a_geometry_shader) fail on main too.
  • Clippy with -D warnings, with and without --all-features (and for victory), and cargo xtask policy pass. rustfmt reports the same differences as on main. It merges with my open PRs.

Limits:

  • Toward zero is the f32 result's, truncated. Where the f32 operation rounded up onto a half (a sum of very different magnitudes, a multiply-add), that is one half above the console's.
  • With the switch on, rounding toward +infinity or -infinity, and a mode not known at an instruction, round to nearest even.
  • A program starts in nearest even here. The console starts it in the mode its register settings give (PGM_RSRC1.FLOAT_MODE), which the translation does not read.
  • The 16-bit interpolation results are left to #81, which changes that code.
  • The switch stays off by default, as its comment says, until a game run tries it.

Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5557489824 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/239). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-30T01:43:53Z. --- `VICTORY_F16_NEAREST=1` makes 16-bit float results round as the console rounds them, instead of as the host's `f32tof16` does: that one rounds toward zero on AMD and NVIDIA. The switch was read only for `valu16_op`'s results (`v_add_f16`, `v_cvt_f16_f32` and the like). Packed results (`v_pk_add_f16`, `v_pk_mul_f16`, `v_pk_fma_f16` ...) and the mixed-precision ones (`v_fma_mixlo_f16`, `v_fma_mixhi_f16`) still went through the host's conversion, so with the switch on, an RX 7900 XT still rounded them toward zero. The console also rounds 16-bit results as its MODE register says (bits 3:2, set by `s_round_mode` or by an `s_setreg` of MODE), and the translation ignored both: with the switch on, a program that asked for rounding toward zero got rounding to nearest. (A refused `s_setreg` is an instruction without a side effect or a result, so the program is translated without it.) Change (`emit/lower.rs`): - `round_modes` gives the 16-bit round mode in force at each instruction: nearest even where the program starts, then as `s_round_mode`, or an `s_setreg` of MODE with a constant, sets it; unknown after an `s_setreg` of a register's value, or where paths that set it differently join. `Lower::inst` takes it into `mode16`. - `narrow` converts a 16-bit result. Without the switch it is the host's conversion, as before. With it, `f16_toward_zero` where the mode is toward zero, else `f16_nearest`, both whatever the host's conversion does. - `valu16_op`, `packed16`, the `v_fma_mix*_f16` results and the f32 wave sum of halves (`half_sum_here`) use `narrow`. The exact wave sum of halves (`add_halves`), which rounds to nearest with or without the switch, rounds toward zero where the mode says so. Without the switch the translation is the same as before, except for that exact wave sum under a toward-zero mode. The round-mode pass follows upstream Marvisak/VIctory 77053a0 (Marvis), which 570a941 removed with the rest of that commit. The conversions are this repository's `f16_nearest` and `f16_toward_zero`, and the switch stays opt-in as here. Tests, in `emit::wave64_tests`, on the hardware device: - `a_packed_16_bit_sum_rounds_to_nearest_when_asked_to`: `v_pk_add_f16` of 0x3c03 + 0x4000 in both halves, the tie of the existing `a_16_bit_sum_rounds_to_nearest_when_asked_to`, which the console rounds up to 0x4202. With the switch on, main gives 0x42014201 on the RX 7900 XT; this change gives 0x42024202. - `a_mixed_precision_16_bit_result_rounds_to_nearest_when_asked_to`: `v_fma_mixlo_f16` of 0x3c03 * 1.0 + 0x4000 into the low half gives 0x12344202, the high half kept. A mutant with the host's conversion at that site gives 0x12344201. - `a_16_bit_sum_rounds_toward_zero_where_the_program_sets_it`: `s_round_mode 0xc`, or `s_setreg_imm32_b32 hwreg(HW_REG_MODE, 2, 2), 3`, then the same `v_add_f16`: 0x4201 with the switch on. Main gives 0x4202 for both, and so does a mutant that leaves `s_setreg` out. Without the switch the test takes the host's value, which is 0x4201 on the RX 7900 XT. - `victory-shader --lib` on the RX 7900 XT and WARP with one test thread: 240 passed, 2 failed, 4 ignored (main: 237, 2, 4). The 2 failures (`emit::rect::tests::rectangles_are_drawn_whole` and `the_rectangle_stage_is_a_geometry_shader`) fail on main too. - Clippy with `-D warnings`, with and without `--all-features` (and for `victory`), and `cargo xtask policy` pass. rustfmt reports the same differences as on main. It merges with my open PRs. Limits: - Toward zero is the f32 result's, truncated. Where the f32 operation rounded up onto a half (a sum of very different magnitudes, a multiply-add), that is one half above the console's. - With the switch on, rounding toward +infinity or -infinity, and a mode not known at an instruction, round to nearest even. - A program starts in nearest even here. The console starts it in the mode its register settings give (PGM_RSRC1.FLOAT_MODE), which the translation does not read. - The 16-bit interpolation results are left to #81, which changes that code. - The switch stays off by default, as its comment says, until a game run tries it. Generated with [Claude Code](https://claude.com/claude-code)
Zippp69 commented 2026-11-30 16:08:42 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by Zippp69 on 2026-11-30T16:08:42Z.


Review of 733e248

Fail on PSO creation errors after a device opens. stored_on_hardware() treats a failed compute_pipeline() as an unavailable device. That lets all three new rounding tests pass without readback if PSO creation regresses. Keep the skip for an unavailable device and fail the test if PSO creation fails after the device opens. Four focused rounding tests ran successfully with device readback on this head. wave64_tests.rs

<!-- greyfinch-sync:DexNom/VIctory:comment:7284186028 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/239#issuecomment-7284186028). Originally posted by [Zippp69](https://github.com/Zippp69) on 2026-11-30T16:08:42Z. --- Review of `733e248` **Fail on PSO creation errors after a device opens.** `stored_on_hardware()` treats a failed `compute_pipeline()` as an unavailable device. That lets all three new rounding tests pass without readback if PSO creation regresses. Keep the skip for an unavailable device and fail the test if PSO creation fails after the device opens. Four focused rounding tests ran successfully with device readback on this head. [wave64_tests.rs](https://github.com/DexNom/VIctory/blob/733e2481781488b3cdd577cc249007bc3ae123bb/crates/victory-shader/src/emit/wave64_tests.rs#L366-L396)
Zippp69 commented 2026-11-30 17:37:39 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by Zippp69 on 2026-11-30T17:37:39Z.


Tested patch for the PSO test failure. Follow-up to the rounding test review at 733e248. Keep the skip when device creation fails. Once the device opens, report PSO creation failure as a test failure.

let host = Host::new(false).ok()?;
let (root, pso) = host
    .compute_pipeline(&shader)
    .unwrap_or_else(|e| panic!("compute_pipeline failed: {e}"));

Four focused rounding tests passed with device readback after the change. One pre-existing test selected by the filter was ignored. wave64_tests.rs

<!-- greyfinch-sync:DexNom/VIctory:comment:7286602853 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/239#issuecomment-7286602853). Originally posted by [Zippp69](https://github.com/Zippp69) on 2026-11-30T17:37:39Z. --- **Tested patch for the PSO test failure.** Follow-up to [the rounding test review](https://github.com/DexNom/VIctory/pull/239#issuecomment-7284186028) at `733e248`. Keep the skip when device creation fails. Once the device opens, report PSO creation failure as a test failure. ```rust let host = Host::new(false).ok()?; let (root, pso) = host .compute_pipeline(&shader) .unwrap_or_else(|e| panic!("compute_pipeline failed: {e}")); ``` Four focused rounding tests passed with device readback after the change. One pre-existing test selected by the filter was ignored. [wave64_tests.rs](https://github.com/DexNom/VIctory/blob/733e2481781488b3cdd577cc249007bc3ae123bb/crates/victory-shader/src/emit/wave64_tests.rs#L361-L395)
murillojndem commented 2026-11-30 18:42:06 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-30T18:42:06Z.


Thanks, applied in 54f2c5f. stored_on_hardware now returns None only when Host::new fails. Once the device opens, a compute_pipeline error panics, and the three callers print "no SM 6.6 hardware device; not run".

Checked with a mutation that truncates the DXIL just before compute_pipeline: on 733e248 the three tests printed "not run" and passed, and with 54f2c5f they fail with "compute_pipeline failed". Without the mutation the four rounding tests pass with readback on an RX 7900 XT and on WARP (VICTORY_WARP=1).

The older skips in wave64_tests.rs, a_16_bit_sum_rounds_to_nearest_when_asked_to among them, come from main and keep the behavior they have there.

<!-- greyfinch-sync:DexNom/VIctory:comment:7288238181 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/239#issuecomment-7288238181). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-30T18:42:06Z. --- Thanks, applied in 54f2c5f. `stored_on_hardware` now returns `None` only when `Host::new` fails. Once the device opens, a `compute_pipeline` error panics, and the three callers print "no SM 6.6 hardware device; not run". Checked with a mutation that truncates the DXIL just before `compute_pipeline`: on 733e248 the three tests printed "not run" and passed, and with 54f2c5f they fail with "compute_pipeline failed". Without the mutation the four rounding tests pass with readback on an RX 7900 XT and on WARP (`VICTORY_WARP=1`). The older skips in `wave64_tests.rs`, `a_16_bit_sum_rounds_to_nearest_when_asked_to` among them, come from main and keep the behavior they have there.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/239:upstream-pr/239'
git switch 'upstream-pr/239'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/239'
git switch 'upstream-pr/239'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/239'
git switch 'upstream-pr/239'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/239'
git switch 'main'
git merge --squash 'upstream-pr/239'
git switch 'main'
git merge --ff-only 'upstream-pr/239'
git switch 'main'
git merge 'upstream-pr/239'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
DexNom/VIctory!239
No description provided.