shader: double support — the conversion pair and the four arithmetic forms #142

Open
xiaotu030202 wants to merge 2 commits from upstream-pr/142 into main
xiaotu030202 commented 2026-11-25 14:11:04 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T14:10:17Z.


What this adds

Six double instructions in gfx10/opcodes.rs were unreachable together, because nothing in emit had a double: TypeDef had a Float and no Double, Types had an f32 and no f64, and Cast went from SiToFp = 6 straight to Bitcast = 11, skipping LLVM's fptrunc = 7 and fpext = 8.

All three exist now, plus six instructions:

instruction what it needed
v_cvt_f32_f64 Double, f64, FPTrunc — narrow to one dword
v_cvt_f64_f32 FPExt — widen into two VGPRs
v_add_f64, v_mul_f64 one DXIL instruction each
v_min_f64, v_max_f64 f64_max_min, because DXIL has no FMin/FMax for f64

Two shaders in #132's log stop at the first of these — 5dc3b25986622949 and 6f225b641c5b0708. The rest are the other half of what that unlocked; no title has been seen failing on them.

Three refusals worth recording

A "did it lower" test would have hidden all of them behind the last one.

Flags must match usage. declared=…4, actual=…0 — and 0x4 is not the answer.

The delta the validator prints is the difference between two values, not the bit to set. d3dcommon.h has the real one:

#define D3D_SHADER_FEATURE_DOUBLES   0x00001

Every other flag in this file already matches that enum exactly (STENCIL_REF 0x200, WAVE_OPS 0x4000, INT64_OPS 0x8000, BARYCENTRICS 0x20000), which is what made the header the right authority rather than the diff. Setting 0x4 passed the first check and failed the next: Container part 'Feature Info' does not match expected for module.

FPExt only operates on FP — %10 = fpext i32 %9 to double.

A VALU source register holds an i32, so widening needs a bitcast to f32 first. This is exactly why v_cvt_f32_f16 goes through legacyF16ToF32 (integer in, float out) instead of casting the raw register.

I briefly "learned" the opposite — that widening did not need the DOUBLES bit — from a green run that was green only because the fpext was still malformed and the module therefore contained no real double. A false green that teaches the wrong lesson is worse than a red.

'dx.op.binary.f32' is not a DXILOpFuncition for DXILOpcode 'FMin'.

The f32 min/max helper calls binary.f32 with i32 bitcasts. FMin/FMax have no f64 overload at all, so f64_max_min is a compare and a select, keeping the hardware's −0 rule — of two zeros the maximum is +0 unless both are −0, the minimum is −0 unless neither is — with i64 masks (0x7FFF_FFFF_FFFF_FFFF) rather than i32 ones.

Two bugs in my own tests, both caught by checking values

Register clobbering. The first arithmetic test failed with v_add_f64(1.0, 2.0) -> 1.0. The lowering was fine: build_cs hands its four input dwords to v1..v4, and I widened the first input into v2 — clobbering the second before it was read. The sum really was a + garbage. Temporaries start at v8 now.

A test that measured nothing. My first v_cvt_f64_f32 test asserted only !shader.dxil.is_empty() — and it passed with a lowering that wrote only the low half of the pair. I found that by mutating the code, not by reading it. It is replaced by one that compares the high dword against f64::from(f).to_bits() >> 32.

Tests — values, on hardware and WARP

  • v_cvt_f32_f64_narrows_a_double_to_one_dword, …_takes_a_scalar_pair_too — VGPR pair and SGPR pair
  • v_cvt_f64_f32_widens_one_dword_into_two — high dword against Rust's f64 for 1.0, 2.0, −1.0, 0.0
  • the_double_arithmetic_forms_compute_in_double — all four forms over four input pairs, widened and narrowed through the conversion pair, so one test exercises everything here

Checked by mutation, each time by breaking the code and re-running:

features |= 0x1  ->  0x0       FAILED  "Container part 'Feature Info' …"
low half only                  FAILED  (value mismatch)
swap v_min_f64 / v_max_f64     FAILED  "v_min_f64 case 0: … want 0x3f800000"

Verification

Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK 10.0.26100.0, VICTORY_DXIL set. No game files.

$ cargo test -p victory-shader --lib -- --test-threads=1
test result: ok. 216 passed; 0 failed; 2 ignored

$ cargo clippy -p victory-shader --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok

What is not claimed

I have not seen the two shaders from #132 translate. That needs shaders.rpf and game files. Whether they now go through, or stop at the next thing they contain, is what victory translate --list would say.

Seven double instructions still do not lower: v_fma_f64, v_rcp_f64, v_rsq_f64, v_sqrt_f64, v_frexp_*_f64, v_div_scale/fmas/fixup_f64, the whole v_cmp_*_f64 family, and every ds_*_f64 LDS form. Each needs its own reading of the ISA — v_sqrt_f64 is a two-instruction sequence on some hardware — and none are here.

Why one PR instead of three

I first opened this as #138, #140 and #141, judging each worth reviewing alone. That was wrong for a feature stack: two of them cannot apply to main without the first, so reviewing them apart means reviewing a branch that does not build. This replaces all three — same content, one description, one diff. I will close those once this is up.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6606407061 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/142). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T14:10:17Z. --- ## What this adds Six double instructions in `gfx10/opcodes.rs` were unreachable together, because **nothing in `emit` had a `double`**: `TypeDef` had a `Float` and no `Double`, `Types` had an `f32` and no `f64`, and `Cast` went from `SiToFp = 6` straight to `Bitcast = 11`, skipping LLVM's `fptrunc = 7` and `fpext = 8`. All three exist now, plus six instructions: | instruction | what it needed | |---|---| | `v_cvt_f32_f64` | `Double`, `f64`, `FPTrunc` — narrow to one dword | | `v_cvt_f64_f32` | `FPExt` — widen into two VGPRs | | `v_add_f64`, `v_mul_f64` | one DXIL instruction each | | `v_min_f64`, `v_max_f64` | `f64_max_min`, because DXIL has no `FMin`/`FMax` for `f64` | Two shaders in #132's log stop at the first of these — `5dc3b25986622949` and `6f225b641c5b0708`. The rest are the other half of what that unlocked; **no title has been seen failing on them**. ## Three refusals worth recording A "did it lower" test would have hidden all of them behind the last one. **`Flags must match usage. declared=…4, actual=…0` — and `0x4` is not the answer.** The delta the validator prints is the *difference between two values*, not the bit to set. `d3dcommon.h` has the real one: ```c #define D3D_SHADER_FEATURE_DOUBLES 0x00001 ``` Every other flag in this file already matches that enum exactly (`STENCIL_REF 0x200`, `WAVE_OPS 0x4000`, `INT64_OPS 0x8000`, `BARYCENTRICS 0x20000`), which is what made the header the right authority rather than the diff. Setting `0x4` passed the first check and failed the next: `Container part 'Feature Info' does not match expected for module`. **`FPExt only operates on FP` — `%10 = fpext i32 %9 to double`.** A VALU source register holds an `i32`, so widening needs a bitcast to `f32` first. This is exactly why `v_cvt_f32_f16` goes through `legacyF16ToF32` (integer in, float out) instead of casting the raw register. I briefly "learned" the opposite — that widening did not need the `DOUBLES` bit — from a green run that was green only because the `fpext` was still malformed and the module therefore contained no real `double`. A false green that teaches the wrong lesson is worse than a red. **`'dx.op.binary.f32' is not a DXILOpFuncition for DXILOpcode 'FMin'`.** The `f32` min/max helper calls `binary.f32` with `i32` bitcasts. `FMin`/`FMax` have **no `f64` overload at all**, so `f64_max_min` is a compare and a select, keeping the hardware's `−0` rule — of two zeros the maximum is `+0` unless both are `−0`, the minimum is `−0` unless neither is — with `i64` masks (`0x7FFF_FFFF_FFFF_FFFF`) rather than `i32` ones. ## Two bugs in my own tests, both caught by checking values **Register clobbering.** The first arithmetic test failed with `v_add_f64(1.0, 2.0) -> 1.0`. The lowering was fine: `build_cs` hands its four input dwords to `v1..v4`, and I widened the first input into `v2` — clobbering the second before it was read. The sum really was `a + garbage`. Temporaries start at `v8` now. **A test that measured nothing.** My first `v_cvt_f64_f32` test asserted only `!shader.dxil.is_empty()` — and it **passed with a lowering that wrote only the low half of the pair**. I found that by mutating the code, not by reading it. It is replaced by one that compares the high dword against `f64::from(f).to_bits() >> 32`. ## Tests — values, on hardware and WARP - `v_cvt_f32_f64_narrows_a_double_to_one_dword`, `…_takes_a_scalar_pair_too` — VGPR pair and SGPR pair - `v_cvt_f64_f32_widens_one_dword_into_two` — high dword against Rust's `f64` for 1.0, 2.0, −1.0, 0.0 - `the_double_arithmetic_forms_compute_in_double` — all four forms over four input pairs, widened and narrowed through the conversion pair, so one test exercises everything here Checked by mutation, each time by breaking the code and re-running: ``` features |= 0x1 -> 0x0 FAILED "Container part 'Feature Info' …" low half only FAILED (value mismatch) swap v_min_f64 / v_max_f64 FAILED "v_min_f64 case 0: … want 0x3f800000" ``` ## Verification Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK 10.0.26100.0, `VICTORY_DXIL` set. **No game files.** ``` $ cargo test -p victory-shader --lib -- --test-threads=1 test result: ok. 216 passed; 0 failed; 2 ignored $ cargo clippy -p victory-shader --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## What is not claimed **I have not seen the two shaders from #132 translate.** That needs `shaders.rpf` and game files. Whether they now go through, or stop at the next thing they contain, is what `victory translate --list` would say. **Seven double instructions still do not lower**: `v_fma_f64`, `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64`, `v_frexp_*_f64`, `v_div_scale/fmas/fixup_f64`, the whole `v_cmp_*_f64` family, and every `ds_*_f64` LDS form. Each needs its own reading of the ISA — `v_sqrt_f64` is a two-instruction sequence on some hardware — and none are here. ## Why one PR instead of three I first opened this as #138, #140 and #141, judging each worth reviewing alone. **That was wrong for a feature stack**: two of them cannot apply to `main` without the first, so reviewing them apart means reviewing a branch that does not build. This replaces all three — same content, one description, one diff. I will close those once this is up.
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/142:upstream-pr/142'
git switch 'upstream-pr/142'

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/142'
git switch 'upstream-pr/142'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/142'
git switch 'upstream-pr/142'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/142'
git switch 'main'
git merge --squash 'upstream-pr/142'
git switch 'main'
git merge --ff-only 'upstream-pr/142'
git switch 'main'
git merge 'upstream-pr/142'
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
Zettrox/ztrx!142
No description provided.