shader: lower gfx1013 v_sin_f32 and v_cos_f32 in pixel_aot #351

Merged
denn0x merged 4 commits from upstream-pr/357 into main 2026-11-22 09:55:31 +00:00
denn0x commented 2026-11-21 23:13:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T23:13:07Z.


Change

Advances shader translation coverage. This is an independent problem: v_sin_f32 and v_cos_f32 are the remaining unported f32 transcendentals in pixel_aot. #274 lists them as left for a separate change, and no open PR lowers them. There is no prerequisite PR. The shared interface is the ALU table and the VOP arithmetic pattern in tools/pixel_aot.py, which #274, #288 and #310 also extend by name. Each adds names to the same pattern, so whichever lands later needs a one-line merge.

Semantics. Both instructions take revolutions: sin(2πx) and cos(2πx). LLVM 15.0.7 (SITargetLowering::LowerTrig) selects llvm.sin as the instruction applied to x · 1/(2π). It adds a fract only for subtargets with FeatureTrigReducedRange, which are Southern, Sea and Volcanic Islands, not GFX9 or GFX10.

Lowering.

  • turns(x) removes the nearest integer number of turns (x - round(x)) when |x| > 0.5. That is exact in binary32 and keeps the radian argument in [-π, π].
  • sin32 and cos32 then call HLSL sin and cos on that argument times 2π.
  • The D3D11.3 functional specification bounds sincos error to 0.0008 absolute only within [-100π, 100π].
  • Inputs of at most half a turn pass unchanged, so -0 keeps its sign.
  • Source negation, absolute value and clamp use the existing single-source ALU path.

Fixture

TRIG_BODY (tests/pixel_aot_check.py):

  • Encodings: v_sin_f32_e32, v_cos_f32_e32, v_sin_f32_e64 with -|x|, and v_cos_f32_e64 with -x and clamp.
  • Quantization: each result is quantized to 1/32 steps by exact operations (×32, +0.5, floor, ×1/32). The check stays bitwise even though the specification allows sin and cos to differ by up to 0.0008. trig_reference reduces each input in double precision and asserts that every drawn reference lies at least 0.1 step from a boundary. The smallest margin is 0.127 steps.
  • Constant sets: four, together covering all four quadrants. Each set also has one input of millions of turns: -4000000.25, 4000000.25, -4000000.5 and 2000000.75.
  • Negative controls: each must report pixel_mismatch for all four sets:
    • sine and cosine swapped in the source program;
    • the generated (turns(value) * 6.28318548f) replaced with (value) (radians instead of turns).
  • Measured, not required: a third variant drops the reduction and keeps value * 2π. Its count is printed as trig_unreduced_mismatches.
  • Translator controls: a sine or cosine write into a pending sample destination before vmcnt(0) is rejected.
  • Scanner and coverage: check_binary assembles the program with LLVM 15 and compares the decoded lowering. tests/form_coverage_check.py asserts the four new forms (VOP1 v_sin_f32, v_cos_f32; VOP3 v_sin_f32_e64, v_cos_f32_e64) and that they add exactly four covered gfx1013 forms. tests/rdna2_tools_check.py adds a scanner case. With main's tools/pixel_aot.py, that case fails on translator_accepted.

Verification

Tested at 466106a on b860dd5. The head 8206536 changes only docs/experiments.md, recording the results below. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, DXC 1.8.2502.11, Python 3.13. D3D12 debug layer enabled on both backends.

  • AMD Radeon RX 9070 XT (vendor 1002, device 7550), driver 32.0.32015.7029
  • WARP: Microsoft Basic Render Driver 10.0.26100.9818
ctest --preset dev --no-tests=error -R '^pixel_aot_(hardware|warp)$' -V
129: trig_unreduced_mismatches=0/4
1/2 Test #129: pixel_aot_hardware ...............   Passed   12.66 sec
132: trig_unreduced_mismatches=0/4
2/2 Test #132: pixel_aot_warp ...................   Passed    5.25 sec
100% tests passed, 0 tests failed out of 2
  • Every draw on both backends: max_absolute_error=0 and debug_errors=0 debug_warnings=0. The four reference draws pass, and both required controls mismatch on all four sets.
  • Full suite: ctest --preset dev --no-tests=error -E '_hardware$' gives 118 passed, 0 failed. bvh_wave64_warp and wave_shuffle_warp report their usual capability skips.
  • Pure-Python checks: tests/form_coverage_check.py, tests/rdna2_tools_check.py and tests/docs_links_check.py pass.
  • Linux LLVM 15 job (pixel_aot_check.py binary, ffx_dispatch_aot_check.py and rdna2_tools_check.py with LLVM): passed at 466106a in the fork's Windows checks run 44960410427, together with the Windows build-and-test and Linux Python jobs.

After rebasing onto 4f6c39d (head 5c8fe20), to resolve the conflicts with #362:

  • Conflicts: docs/experiments.md keeps both paragraphs. tests/form_coverage_check.py drops this PR's old s_nop not in ACCEPTED line, since #362 now accepts s_nop, and keeps the trig assertions.
  • Pixel tests: pixel_aot_hardware and pixel_aot_warp pass again, with trig_unreduced_mismatches=0/4.
  • Full suite: 143 passed, 1 failed and 2 capability skips. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 probe in #369, which fails the same way on main.
  • Pure-Python checks: the three listed above pass.

Measured, not established:

  • The unreduced variant also matched all four sets on both adapters, so the reduction makes no measured difference on these two.
  • In the DXIL from DXC 1.8, the 2π multiply carries no dx.precise flag, unlike Sin and Cos. One explanation is that both drivers fold it into their own revolution scaling. That is a hypothesis; the drivers were not inspected.
  • The reduction is kept because it is exact and does not rely on that folding. If you'd rather lower to plain sin(x * 2π), I can change it.

Not measured: the instruction's own precision on a gfx1013 GPU, infinities, NaN payloads and the sign of a zero result. The quantized compare cannot observe the last.

Evidence and follow-up

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the lowering, the draw and its controls.

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 (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535648750 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/357). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T23:13:07Z. --- ## Change Advances shader translation coverage. This is an independent problem: `v_sin_f32` and `v_cos_f32` are the remaining unported f32 transcendentals in `pixel_aot`. #274 lists them as left for a separate change, and no open PR lowers them. There is no prerequisite PR. The shared interface is the `ALU` table and the VOP arithmetic pattern in `tools/pixel_aot.py`, which #274, #288 and #310 also extend by name. Each adds names to the same pattern, so whichever lands later needs a one-line merge. **Semantics.** Both instructions take revolutions: sin(2πx) and cos(2πx). LLVM 15.0.7 (`SITargetLowering::LowerTrig`) selects `llvm.sin` as the instruction applied to x · 1/(2π). It adds a `fract` only for subtargets with `FeatureTrigReducedRange`, which are Southern, Sea and Volcanic Islands, not GFX9 or GFX10. **Lowering.** - `turns(x)` removes the nearest integer number of turns (`x - round(x)`) when |x| > 0.5. That is exact in binary32 and keeps the radian argument in [-π, π]. - `sin32` and `cos32` then call HLSL `sin` and `cos` on that argument times 2π. - The D3D11.3 functional specification bounds `sincos` error to 0.0008 absolute only within [-100π, 100π]. - Inputs of at most half a turn pass unchanged, so -0 keeps its sign. - Source negation, absolute value and `clamp` use the existing single-source ALU path. ## Fixture `TRIG_BODY` (`tests/pixel_aot_check.py`): - **Encodings:** `v_sin_f32_e32`, `v_cos_f32_e32`, `v_sin_f32_e64` with `-|x|`, and `v_cos_f32_e64` with `-x` and `clamp`. - **Quantization:** each result is quantized to 1/32 steps by exact operations (×32, +0.5, floor, ×1/32). The check stays bitwise even though the specification allows sin and cos to differ by up to 0.0008. `trig_reference` reduces each input in double precision and asserts that every drawn reference lies at least 0.1 step from a boundary. The smallest margin is 0.127 steps. - **Constant sets:** four, together covering all four quadrants. Each set also has one input of millions of turns: -4000000.25, 4000000.25, -4000000.5 and 2000000.75. - **Negative controls:** each must report `pixel_mismatch` for all four sets: - sine and cosine swapped in the source program; - the generated `(turns(value) * 6.28318548f)` replaced with `(value)` (radians instead of turns). - **Measured, not required:** a third variant drops the reduction and keeps `value * 2π`. Its count is printed as `trig_unreduced_mismatches`. - **Translator controls:** a sine or cosine write into a pending sample destination before `vmcnt(0)` is rejected. - **Scanner and coverage:** `check_binary` assembles the program with LLVM 15 and compares the decoded lowering. `tests/form_coverage_check.py` asserts the four new forms (VOP1 `v_sin_f32`, `v_cos_f32`; VOP3 `v_sin_f32_e64`, `v_cos_f32_e64`) and that they add exactly four covered gfx1013 forms. `tests/rdna2_tools_check.py` adds a scanner case. With `main`'s `tools/pixel_aot.py`, that case fails on `translator_accepted`. ## Verification Tested at `466106a` on `b860dd5`. The head `8206536` changes only `docs/experiments.md`, recording the results below. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, DXC 1.8.2502.11, Python 3.13. D3D12 debug layer enabled on both backends. - AMD Radeon RX 9070 XT (vendor 1002, device 7550), driver 32.0.32015.7029 - WARP: Microsoft Basic Render Driver 10.0.26100.9818 ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_(hardware|warp)$' -V ``` ```text 129: trig_unreduced_mismatches=0/4 1/2 Test #129: pixel_aot_hardware ............... Passed 12.66 sec 132: trig_unreduced_mismatches=0/4 2/2 Test #132: pixel_aot_warp ................... Passed 5.25 sec 100% tests passed, 0 tests failed out of 2 ``` - **Every draw on both backends:** `max_absolute_error=0` and `debug_errors=0 debug_warnings=0`. The four reference draws pass, and both required controls mismatch on all four sets. - **Full suite:** `ctest --preset dev --no-tests=error -E '_hardware$'` gives 118 passed, 0 failed. `bvh_wave64_warp` and `wave_shuffle_warp` report their usual capability skips. - **Pure-Python checks:** `tests/form_coverage_check.py`, `tests/rdna2_tools_check.py` and `tests/docs_links_check.py` pass. - **Linux LLVM 15 job** (`pixel_aot_check.py binary`, `ffx_dispatch_aot_check.py` and `rdna2_tools_check.py` with LLVM): passed at `466106a` in the fork's `Windows checks` run [44960410427](https://github.com/denn0x/Xbox2Windows/actions/runs/44960410427), together with the Windows build-and-test and Linux Python jobs. **After rebasing onto `4f6c39d`** (head `5c8fe20`), to resolve the conflicts with #362: - **Conflicts:** `docs/experiments.md` keeps both paragraphs. `tests/form_coverage_check.py` drops this PR's old `s_nop not in ACCEPTED` line, since #362 now accepts `s_nop`, and keeps the trig assertions. - **Pixel tests:** `pixel_aot_hardware` and `pixel_aot_warp` pass again, with `trig_unreduced_mismatches=0/4`. - **Full suite:** 143 passed, 1 failed and 2 capability skips. The failure is `gpu_alias_lookup_writes_hardware`, the RDNA 4 probe in #369, which fails the same way on `main`. - **Pure-Python checks:** the three listed above pass. **Measured, not established:** - The unreduced variant also matched all four sets on both adapters, so the reduction makes no measured difference on these two. - In the DXIL from DXC 1.8, the 2π multiply carries no `dx.precise` flag, unlike `Sin` and `Cos`. One explanation is that both drivers fold it into their own revolution scaling. That is a hypothesis; the drivers were not inspected. - The reduction is kept because it is exact and does not rely on that folding. If you'd rather lower to plain `sin(x * 2π)`, I can change it. **Not measured:** the instruction's own precision on a gfx1013 GPU, infinities, NaN payloads and the sign of a zero result. The quantized compare cannot observe the last. ## Evidence and follow-up The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the lowering, the draw and its controls. - [LLVM 15.0.7 `LowerTrig`](https://github.com/llvm/llvm-project/blob/llvmorg-15.0.7/llvm/lib/Target/AMDGPU/SIISelLowering.cpp#L9262) - [`FeatureTrigReducedRange`](https://github.com/llvm/llvm-project/blob/llvmorg-15.0.7/llvm/lib/Target/AMDGPU/AMDGPU.td#L794) - [D3D11.3 functional specification](https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm) (`sincos`) ## 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 (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:42:37 +00:00
denn0x commented 2026-11-24 06:48:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T02:49:10Z.


Rebased onto 1606f7b as e3d8ffa and 347bfe6. The conflicts were with the v_mad_f32/v_mac_f32 support that landed on main, and I kept both sides:

  • the ftz32/mad32 helpers and the turns/sin32/cos32 helpers;
  • sin|cos and mad|mac in the VOP opcode pattern;
  • the trig fixture and check_trig_draws next to affine_coordinates and check_affine_lod_draw.

ctest --preset dev --no-tests=error -R pixel_aot -V gives pixel_aot_hardware (RX 9070 XT) and pixel_aot_warp passed, with trig_unreduced_mismatches=0/4 as before. Fork CI run 44977654122 passed all three jobs, including the gfx1013 LLVM job.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073145353 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/357#issuecomment-7073145353). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T02:49:10Z. --- Rebased onto `1606f7b` as `e3d8ffa` and `347bfe6`. The conflicts were with the `v_mad_f32`/`v_mac_f32` support that landed on `main`, and I kept both sides: - the `ftz32`/`mad32` helpers and the `turns`/`sin32`/`cos32` helpers; - `sin|cos` and `mad|mac` in the VOP opcode pattern; - the trig fixture and `check_trig_draws` next to `affine_coordinates` and `check_affine_lod_draw`. `ctest --preset dev --no-tests=error -R pixel_aot -V` gives `pixel_aot_hardware` (RX 9070 XT) and `pixel_aot_warp` passed, with `trig_unreduced_mismatches=0/4` as before. Fork CI run [44977654122](https://github.com/denn0x/Xbox2Windows/actions/runs/44977654122) passed all three jobs, including the gfx1013 LLVM job. _AI-assisted (Claude Code)._
denn0x commented 2026-11-24 06:48:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T03:50:12Z.


Rebased onto fb7547c to clear a conflict in tests/pixel_aot_check.py with the classification fixture that landed on main. The resolution keeps both sides:

  • the trig fixture joins the binary fixture list, before main's CLASS_BODY entry;
  • check_trig_draws now runs right after check_half_draw, which main moved further down in check_render.

There are no other changes. Re-measured at d7e23e9 (same environment as the body):

ctest --preset dev --no-tests=error -R pixel_aot -V
pixel_aot_hardware: Passed (trig_unreduced_mismatches=0/4)
pixel_aot_warp:     Passed (trig_unreduced_mismatches=0/4)

Full suite: 164 passed, 1 failed and 2 capability skips out of 167. The failure is gpu_alias_lookup_writes_hardware (#369), the same as on main.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073813210 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/357#issuecomment-7073813210). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T03:50:12Z. --- Rebased onto `fb7547c` to clear a conflict in `tests/pixel_aot_check.py` with the classification fixture that landed on `main`. The resolution keeps both sides: - the trig fixture joins the binary fixture list, before `main`'s `CLASS_BODY` entry; - `check_trig_draws` now runs right after `check_half_draw`, which `main` moved further down in `check_render`. There are no other changes. Re-measured at `d7e23e9` (same environment as the body): ```text ctest --preset dev --no-tests=error -R pixel_aot -V pixel_aot_hardware: Passed (trig_unreduced_mismatches=0/4) pixel_aot_warp: Passed (trig_unreduced_mismatches=0/4) ``` Full suite: 164 passed, 1 failed and 2 capability skips out of 167. The failure is `gpu_alias_lookup_writes_hardware` (#369), the same as on `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
R0Y00/Xbox2Windows!351
No description provided.