shader: lower v_cvt_pk_u8_f32 in the direct C++ compute path #2998

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/2998 into cpp-migration 2026-11-27 04:36:04 +00:00
legacy-code-archaeologist commented 2026-11-27 03:52:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:52:26Z.


Change

The direct compute frontend now lowers v_cvt_pk_u8_f32. Before, it refused it with direct DXIL lowering for v_cvt_pk_u8_f32. Refs #2997, part of the f32-ALU coverage in #2935.

The lowering computes D = (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]). The conversion is the one gfx2hlsl.py (op_v_cvt_pk_u8_f32) uses, measured on RDNA 2 (docs/experiments.md, tools/rdna_cvt_pk_u8.py): round to nearest even, saturated to [0, 255]. NaN, both zeros and all negative values give 0; +inf gives 255.

It follows the same expression:

  • an integer positive test, (bits - 1) < 0x7f800000, which fast-math cannot fold;
  • DXIL FMin (opcode 36) with 255.0, then Round_ne (opcode 26), then fptoui;
  • the merge of the resulting byte into S2.

Source and output modifiers stay refused.

Verification

Commit e56abe74 on cpp-migration e0d83759 (rebased after #2990 and #2994 merged; the controls were rerun on it), Windows 10.0.26100.9711, dev preset, NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable.

  • ctest --preset dev --no-tests=error -R '^native_cvt_pk_u8_|^native_direct_container$' -V: 2 passed, 1 skipped.
    • native_cvt_pk_u8_controls passed. It compiles the program and checks the module with dxc -dumpbin: one FMin and one Round_ne per case, and no FMax. It also refuses a negated S0 and checks that the half-up, truncating and negative-passing references differ. The pre-commit review found that a first version used opcode 35 (FMax), and the module check was added for that.
    • native_cvt_pk_u8_hardware skipped (77). It needs shader model 6.6 with wave32, so the readback is not measured here.
  • The CPU reference agrees with model_rne_sat of tools/rdna_cvt_pk_u8.py for all 16 inputs. 14 of them are rows of tests/data/rdna_cvt_pk_u8_measured.tsv; 1e30 and 127.4999 rely on the model only.
  • python -B tests/docs_links_check.py: ok.

Evidence and follow-up

The fixture has 16 cases, all over S2 = 0xa5a5a5a5:

  • ties 0.5, 1.5, 2.5 and 254.5;
  • 255.5, 1e30 and +inf;
  • -0.0, -1.0 and -inf;
  • a quiet NaN;
  • the smallest subnormal;
  • 127.4999, 0.0 and 255.0;
  • all four byte selects, plus select 6.

The VOP3 word was assembled with LLVM 15.0.7. The other words were checked with tools/disasm_rdna2.py --hex. The note is docs/experiments/native-direct-cvt-pk-u8.md.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5520875204 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2998). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T03:52:26Z. --- ## Change The direct compute frontend now lowers `v_cvt_pk_u8_f32`. Before, it refused it with `direct DXIL lowering for v_cvt_pk_u8_f32`. Refs #2997, part of the f32-ALU coverage in #2935. The lowering computes D = (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]). The conversion is the one `gfx2hlsl.py` (`op_v_cvt_pk_u8_f32`) uses, measured on RDNA 2 (`docs/experiments.md`, `tools/rdna_cvt_pk_u8.py`): round to nearest even, saturated to [0, 255]. NaN, both zeros and all negative values give 0; +inf gives 255. It follows the same expression: - an integer positive test, (bits - 1) < 0x7f800000, which fast-math cannot fold; - DXIL `FMin` (opcode 36) with 255.0, then `Round_ne` (opcode 26), then `fptoui`; - the merge of the resulting byte into S2. Source and output modifiers stay refused. ## Verification Commit `e56abe74` on `cpp-migration` `e0d83759` (rebased after #2990 and #2994 merged; the controls were rerun on it), Windows 10.0.26100.9711, `dev` preset, NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable. - `ctest --preset dev --no-tests=error -R '^native_cvt_pk_u8_|^native_direct_container$' -V`: 2 passed, 1 skipped. - `native_cvt_pk_u8_controls` passed. It compiles the program and checks the module with `dxc -dumpbin`: one `FMin` and one `Round_ne` per case, and no `FMax`. It also refuses a negated S0 and checks that the half-up, truncating and negative-passing references differ. The pre-commit review found that a first version used opcode 35 (`FMax`), and the module check was added for that. - **`native_cvt_pk_u8_hardware` skipped (77).** It needs shader model 6.6 with wave32, so **the readback is not measured here.** - The CPU reference agrees with `model_rne_sat` of `tools/rdna_cvt_pk_u8.py` for all 16 inputs. 14 of them are rows of `tests/data/rdna_cvt_pk_u8_measured.tsv`; 1e30 and 127.4999 rely on the model only. - `python -B tests/docs_links_check.py`: ok. ## Evidence and follow-up The fixture has 16 cases, all over S2 = 0xa5a5a5a5: - ties 0.5, 1.5, 2.5 and 254.5; - 255.5, 1e30 and +inf; - -0.0, -1.0 and -inf; - a quiet NaN; - the smallest subnormal; - 127.4999, 0.0 and 255.0; - all four byte selects, plus select 6. The VOP3 word was assembled with LLVM 15.0.7. The other words were checked with `tools/disasm_rdna2.py --hex`. The note is `docs/experiments/native-direct-cvt-pk-u8.md`. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
R0Y00 commented 2026-11-27 03:59:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T03:59:07Z.

Review: COMMENTED


Integration check for e56abe74: both this PR and #2996 (e08d9510) target cpp-migration, but a merge trial of the two heads conflicts in src/gfx1013_direct_compute.inl. Please land them sequentially. If #2996 lands first, rebase this PR on the resulting cpp-migration and rerun both opcode fixtures before merging.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083299593 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2998#pullrequestreview-6083299593). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T03:59:07Z. **Review:** COMMENTED --- Integration check for `e56abe74`: both this PR and #2996 (`e08d9510`) target `cpp-migration`, but a merge trial of the two heads conflicts in `src/gfx1013_direct_compute.inl`. Please land them sequentially. If #2996 lands first, rebase this PR on the resulting `cpp-migration` and rerun both opcode fixtures before merging.
legacy-code-archaeologist commented 2026-11-27 04:01:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T04:01:41Z.


Thanks. All three of my open integer-ALU PRs inserted their branch in front of v_bfi_b32 in src/gfx1013_direct_compute.inl, so any two of them conflicted. I moved the insertion points so they no longer touch the same lines:

  • #2986 stays in front of v_bfi_b32 (a21837d1);
  • #2996 now sits in front of v_bfe_u32 (78ef2da5);
  • this PR sits in front of s_orn2_b32 (c7d668be).

Merge trials on cpp-migration e0d83759: each pair of heads merges cleanly, and so does an octopus merge of all three. On the octopus merge, compile_dxil builds, and native_align_bfm_controls, native_cvt_pk16_controls, native_cvt_pk_u8_controls, native_sad_controls, native_signed_forms_controls, native_shift64_controls and native_direct_container pass (7/7). The code of each lowering is unchanged; only its position moved. They can land in any order now.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7209460059 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2998#issuecomment-7209460059). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T04:01:41Z. --- Thanks. All three of my open integer-ALU PRs inserted their branch in front of `v_bfi_b32` in `src/gfx1013_direct_compute.inl`, so any two of them conflicted. I moved the insertion points so they no longer touch the same lines: - #2986 stays in front of `v_bfi_b32` (`a21837d1`); - #2996 now sits in front of `v_bfe_u32` (`78ef2da5`); - this PR sits in front of `s_orn2_b32` (`c7d668be`). Merge trials on `cpp-migration` `e0d83759`: each pair of heads merges cleanly, and so does an octopus merge of all three. On the octopus merge, `compile_dxil` builds, and `native_align_bfm_controls`, `native_cvt_pk16_controls`, `native_cvt_pk_u8_controls`, `native_sad_controls`, `native_signed_forms_controls`, `native_shift64_controls` and `native_direct_container` pass (7/7). The code of each lowering is unchanged; only its position moved. They can land in any order now. AI-assisted.
KelboLive commented 2026-11-27 04:14:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T04:14:44Z.

Review: COMMENTED


Solid lowering — correct round-to-nearest-even semantics, proper saturation, and the DXIL opcode checks in the test fixture are a great defense against regression (catching the earlier opcode 35 → 36 mix-up).

Findings

✅ What works well

  • Conversion path is correct: (bits - 1) < 0x7f800000 positive test → FMin(x, 255.0) → Round_ne → fptoui → merge into S2. This faithfully replicates the RDNA2-measured gfx2hlsl.py behavior.
  • Magic number clarity: The constant 2139095040 is 0x7f800000, the smallest IEEE 754 float with exponent 255 (i.e. NaN/inf boundary). The (bits - 1) < 0x7f800000 form is correct for unsigned comparison: it's true for all positive finite floats and false for NaN/inf/-0.0/+inf. A comment with the hex form would help readers.
  • Byte merge: xor(shl(255, amount), 0xFFFFFFFF) = (0xFF << amount) ^ 0xFFFFFFFF = ~(0xFF << amount) — correct mask. Then and(S2, mask) | (byte << amount) does the right byte-place update.
  • Test fixture coverage: 16 cases including ties (0.5, 1.5, 2.5, 254.5), overflow (255.5, 1e30, +inf), negatives (-0, -1, -inf), NaN, subnormal, boundary (127.4999, 0, 255), and byte-select edge case (select = 6 → low 2 bits = 2). The mutant references (half-up, truncating, negative-passing) are independent.
  • DXIL opcode assertion: Checking that -dumpbin contains exactly len(CASES) occurrences of opcode 36 (FMin) and opcode 26 (Round_ne), but zero of opcode 35 (FMax), is an excellent compile-time verification. This catches the earlier mistake of using the wrong opcode.

⚠️ Minor observations

  1. 2139095040 magic number: Consider // 0x7f800000 = UINT32 min for positive finite floats (NaN/inf boundary) as a comment. The decimal form is harder to parse at a glance.

  2. float("bitcast i32 " + a + " to float"): This bitcasts the raw 32-bit value from the VGPR to a float register for the FP comparison. Correct, but if the VGPR value is a denormal or subnormal, the bitcast is still valid (DXIL handles subnormals). No issue, just noting that edge cases are covered by the (bits - 1) < 0x7f800000 test being false only for NaN/inf/-0/+inf, not for subnormals (which are positive finite and pass through).

  3. native_cvt_pk_u8_hardware skip rationale: The note about needing shader model 6.6 with wave32 is clear. The test uses SKIP_RETURN_CODE 77 so it won't fail CI. This is well-documented.

Recommendation

Clean and well-tested. Ready to land with or without the decimal→hex comment.

🏷 One thought: if this family (v_cvt_pk_*) gets more members (e.g. v_cvt_pk_u16_f32), consider whether a shared helper function in gfx1013_direct_compute.inl for the byte/word-place merge pattern would reduce duplication. Not necessary for this PR.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083418459 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2998#pullrequestreview-6083418459). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T04:14:44Z. **Review:** COMMENTED --- Solid lowering — correct round-to-nearest-even semantics, proper saturation, and the DXIL opcode checks in the test fixture are a great defense against regression (catching the earlier opcode 35 → 36 mix-up). ## Findings ### ✅ What works well - **Conversion path is correct**: `(bits - 1) < 0x7f800000` positive test → `FMin(x, 255.0)` → `Round_ne` → `fptoui` → merge into S2. This faithfully replicates the RDNA2-measured `gfx2hlsl.py` behavior. - **Magic number clarity**: The constant `2139095040` is `0x7f800000`, the smallest IEEE 754 float with exponent 255 (i.e. NaN/inf boundary). The `(bits - 1) < 0x7f800000` form is correct for unsigned comparison: it's true for all positive finite floats and false for NaN/inf/-0.0/+inf. A comment with the hex form would help readers. - **Byte merge**: `xor(shl(255, amount), 0xFFFFFFFF)` = `(0xFF << amount) ^ 0xFFFFFFFF` = `~(0xFF << amount)` — correct mask. Then `and(S2, mask) | (byte << amount)` does the right byte-place update. - **Test fixture coverage**: 16 cases including ties (0.5, 1.5, 2.5, 254.5), overflow (255.5, 1e30, +inf), negatives (-0, -1, -inf), NaN, subnormal, boundary (127.4999, 0, 255), and byte-select edge case (select = 6 → low 2 bits = 2). The mutant references (half-up, truncating, negative-passing) are independent. - **DXIL opcode assertion**: Checking that `-dumpbin` contains exactly `len(CASES)` occurrences of opcode 36 (`FMin`) and opcode 26 (`Round_ne`), but *zero* of opcode 35 (`FMax`), is an excellent compile-time verification. This catches the earlier mistake of using the wrong opcode. ### ⚠️ Minor observations 1. **`2139095040` magic number**: Consider `// 0x7f800000 = UINT32 min for positive finite floats (NaN/inf boundary)` as a comment. The decimal form is harder to parse at a glance. 2. **`float("bitcast i32 " + a + " to float")`**: This bitcasts the raw 32-bit value from the VGPR to a float register for the FP comparison. Correct, but if the VGPR value is a denormal or subnormal, the bitcast is still valid (DXIL handles subnormals). No issue, just noting that edge cases are covered by the `(bits - 1) < 0x7f800000` test being false only for NaN/inf/-0/+inf, not for subnormals (which are positive finite and pass through). 3. **`native_cvt_pk_u8_hardware` skip rationale**: The note about needing shader model 6.6 with wave32 is clear. The test uses `SKIP_RETURN_CODE 77` so it won't fail CI. This is well-documented. ### Recommendation Clean and well-tested. Ready to land with or without the decimal→hex comment. 🏷 One thought: if this family (`v_cvt_pk_*`) gets more members (e.g. `v_cvt_pk_u16_f32`), consider whether a shared helper function in `gfx1013_direct_compute.inl` for the byte/word-place merge pattern would reduce duplication. Not necessary for this PR.
MasterDomino commented 2026-11-27 04:36:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T04:36:01Z.

Review: APPROVED


Review of head c7d668be. CI on the head is green. This is the wave32 SM 6.6 readback you asked for. Approving and merging.

Code read:

  • Positive test: (bits - 1) <u 0x7f800000 holds from the smallest subnormal to +inf. NaN, both zeros and negatives give 0.
  • Conversion: FMin(x, 255.0) (opcode 36), then Round_ne (26), then fptoui.
  • Byte merge: the byte goes into S2 at 8 * S1[1:0] under ~(0xff << amount).

Run on AMD: Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2986, #2996, #3003 and #2975 merged onto cpp-migration e0d83759; all five merge cleanly with each other. ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$' gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). native_cvt_pk_u8_hardware and native_cvt_pk_u8_controls passed. This also covers R0Y00's request to rerun both opcode fixtures together with #2996: both pass on the combined tree.

Mutation here: using Round_z (29) instead of Round_ne fails native_cvt_pk_u8_controls on the opcode count and native_cvt_pk_u8_hardware on the ties. With it restored, both pass.

I will close #2997 by hand.

AI-assisted; the commands above were run locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083536129 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2998#pullrequestreview-6083536129). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-27T04:36:01Z. **Review:** APPROVED --- Review of head `c7d668be`. CI on the head is green. **This is the wave32 SM 6.6 readback you asked for.** Approving and merging. **Code read:** - **Positive test:** `(bits - 1) <u 0x7f800000` holds from the smallest subnormal to +inf. NaN, both zeros and negatives give 0. - **Conversion:** `FMin(x, 255.0)` (opcode 36), then `Round_ne` (26), then `fptoui`. - **Byte merge:** the byte goes into S2 at `8 * S1[1:0]` under `~(0xff << amount)`. **Run on AMD:** Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2986, #2996, #3003 and #2975 merged onto `cpp-migration` `e0d83759`; all five merge cleanly with each other. `ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$'` gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). `native_cvt_pk_u8_hardware` and `native_cvt_pk_u8_controls` **passed**. This also covers R0Y00's request to rerun both opcode fixtures together with #2996: both pass on the combined tree. **Mutation here:** using `Round_z` (29) instead of `Round_ne` fails `native_cvt_pk_u8_controls` on the opcode count and `native_cvt_pk_u8_hardware` on the ties. With it restored, both pass. I will close #2997 by hand. AI-assisted; the commands above were run locally.
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
R0Y00/Xbox2Windows!2998
No description provided.