upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/shader_validation.md
diff options
context:
space:
mode:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-23 00:19:13 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-23 00:19:13 +0100
commitfcda84bb0d2c6abb99e42409f413025d00ddb4be (patch)
tree27a5ee092be10547ffa5b4447ee050c623c62850 /docs/shader_validation.md
parent3a1ed28bebf374321149bc4bbffc005ea6f691c4 (diff)
parent3986b50821272791333c00df80bdb8526a8fdb9c (diff)
Merge hw4-ps-oracle into hw4-ps-formats (#3499)
docs/shader_validation.md tools/ps_oracle.py AI-assisted.
Diffstat (limited to 'docs/shader_validation.md')
-rw-r--r--docs/shader_validation.md150
1 files changed, 148 insertions, 2 deletions
diff --git a/docs/shader_validation.md b/docs/shader_validation.md
index 463a1a9df..e2d6a34f2 100644
--- a/docs/shader_validation.md
+++ b/docs/shader_validation.md
@@ -616,6 +616,47 @@ Over 5,000 programs the reference and translator match bit for bit; breaking the
616A mutation check (inst offset dropped, V# stride narrowed, the SMEM pointer's high word dropped) makes 616A mutation check (inst offset dropped, V# stride narrowed, the SMEM pointer's high word dropped) makes
61776, 45 and 42 of 150 programs fail, so the runner does catch address errors. 61776, 45 and 42 of 150 programs fail, so the runner does catch address errors.
618 618
619### Sum-of-absolute-differences family (`tools/sad_diff.py`)
620
621The subset interpreter `cpu_ref.py` has no SAD ops, so the family gets its own runner, like the memory
622instructions above. The family is seven opcodes — `v_sad_u8`, `v_sad_hi_u8`, `v_sad_u16`, `v_sad_u32`,
623`v_msad_u8` and the packed quad forms `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`. `tools/sad_diff.py`
624carries a small reference written from the ISA pseudocode recorded in `docs/design.md`'s contract rows
625(the "Sum of absolute differences" row hardware-verified on gfx1035 through SHADER-137's `int_hw_probe`
626run; the `v_qsad` row bit-exact on an RTX 3060 in #623) and compares it with the translation run in
627`spirv_mini_interp`, the four exported mrt0 words, bit for bit (everything here is integer, so a
628mismatch can never be a rounding convention).
629
630Programs are straight-line: seeds in v8..v15 and s4..s7, then family ops whose destinations stay inside
631v8..v11 (the quad ops write pairs inside that), then `exp mrt0 v8, v9, v10, v11`, so every result is
632observed and later ops chain on earlier ones. Every operand position takes every source kind the
633encoding allows: VGPR, SGPR, a hex literal, an inline constant and `null` for the 32-bit sources; the
634quad ops' 64-bit S0/S2 take VGPR/SGPR pairs, a hex literal (zero high word) or an inline constant
635(sign-extended) — the 64-bit constant rule of the contract table, pinned both ways: a hex literal reads
636the same as the register pair holding its word and a zero high word, while a negative inline constant
637sign-extends, so the two constant forms differ in the high word.
638
639Two modes run one after the other: a sweep over the whole source-kind space (every (S0, S1, S2) kind
640combination, 125 per plain op, 80 per quad op, 785 programs) with values drawn from byte/half edge
641words, and random seeded programs that widen the value coverage over the same kind space. Not
642generated, on purpose: `clamp` (the contract rows say the translator rejects it; pinned as expected
643rejections in `--self-test`), `op_sel`/`neg`/`abs` (the ISA does not define them for these opcodes),
644`v_mqsad_u32_u8` (open PRs #1926/#2163 are on that op) and `null` as a 64-bit quad source (PR #2650's
645subject).
646
647```powershell
648python -I tools/sad_diff.py --programs 2000 --seed 7
649python -I tools/sad_diff.py --self-test
650python -I tests/test_sad_diff.py
651```
652
653Across seeds 1, 2, 3, 7, 11 and 12 (the last pair with `--max-ops 12`) 12,810 programs match bit for
654bit, zero mismatches and zero skips; the seed-7 headline run alone is 785 + 2,000 programs with about
6551,050 to 1,175 cases per op. Two mutations of the translator's SAD rule — the per-byte `UMax`/`UMin`
656pair replaced by a wrapped subtraction, and the `v_sad_hi_u8` shift dropped — make 36 and 17 of 60
657programs fail (`--programs 60 --seed 2 --max-ops 5 --no-sweep`), so the runner does catch arithmetic
658errors.
659
619### Images on the GPU (`tools/image_fuzz.py`) 660### Images on the GPU (`tools/image_fuzz.py`)
620 661
621`tools/image_fuzz.py` starts on the image path, where the T# format, the view type and the binding layout 662`tools/image_fuzz.py` starts on the image path, where the T# format, the view type and the binding layout
@@ -624,6 +665,13 @@ T# in `s[8:15]`), runs it through `runtime/test/vk_image_fuzz.cpp` against a syn
624format, and compares every RGBA32F pixel with an oracle that models the RDNA2 texel fetch for that format: a 665format, and compares every RGBA32F pixel with an oracle that models the RDNA2 texel fetch for that format: a
625fetch is a memory read, so there is no filter, border or LOD to model. 666fetch is a memory read, so there is no filter, border or LOD to model.
626 667
668The comparison is bit exact (any NaN matches any NaN) with one exception. The translated shader fetches a UNORM
669texel through a float view, so the 8-bit to float conversion is the driver's. RDNA2 returns the correctly rounded
670c / 255. llvmpipe multiplies by an f32 1/255, which is one ulp above that for 126 of the 256 byte values (27 gives
6710x3dd8d8da, not 0x3dd8d8d9), and a run stops at its first mismatch, so on llvmpipe no case after the first
672`R8G8B8A8_UNORM` one ran. The translator oracle therefore accepts one ulp on a UNORM8 texel component, never on
673another format or on a sentinel, and the summary says how many components needed it. `--oracle hw` stays bit exact.
674
627```sh 675```sh
628cmake --build runtime/build --target vk_image_fuzz 676cmake --build runtime/build --target vk_image_fuzz
629python -I tools/image_fuzz.py --programs 8 --seed 1 [--show] 677python -I tools/image_fuzz.py --programs 8 --seed 1 [--show]
@@ -1149,6 +1197,15 @@ python -I tools/fmisc_diff.py --programs 3000 --seed 11 --max-ops 8
1149python -I tools/fmisc_diff.py --oracle hw --programs 500 --seed 1 1197python -I tools/fmisc_diff.py --oracle hw --programs 500 --seed 1
1150python -I tests/test_fmisc_diff.py 1198python -I tests/test_fmisc_diff.py
1151``` 1199```
1200**Default-mode DX9 multiply regression.** `v_mul_legacy_f32` now explicitly flushes each f32 operand before the
1201DX9 `0 * anything => +0` override, and flushes the nonzero product before its VOP3 output modifier. The output is
1202flushed again before the active omod's `-0 => +0` rule. Previously it emitted an unguarded `OpFMul`, so on a Vulkan
1203driver that keeps f32 denormals, a subnormal input times a large number or an underflowing normal product yielded
1204nonzero results even though the inferred RDNA2 `MODE.denorm_mode_32 = 0` requires zero. The standalone semantic
1205test `test_valu_mul_legacy_denorm.py` checks the exact bit patterns and the two GPU fixtures
1206`ps_mul_legacy_denorm` / `ps_mul_legacy_denorm_mods` exercise signed input/output FTZ, legacy zero override, and
1207output scaling. This is a translation for the **inferred game MODE**, not a claim of correctly handling dynamic
1208denormal-preserving or IEEE modes; native gfx1013 measurements of these new fixtures were not available.
1152 1209
1153 1210
1154### f16 transcendentals on the hardware (`tools/f16_trans_sweep.py`, FUZZ-29) 1211### f16 transcendentals on the hardware (`tools/f16_trans_sweep.py`, FUZZ-29)
@@ -1401,8 +1458,6 @@ RDNA2 rounds once. Every tie input gives the single-rounded word (A `0x3c01`, B
1401E `0x7bff`, F `0x3c01`, G `0x3c01`), so the translator is one f16 ulp off on exactly these inputs (SHADER-135; the 1458E `0x7bff`, F `0x3c01`, G `0x3c01`), so the translator is one f16 ulp off on exactly these inputs (SHADER-135; the
1402translator is not changed here). On the RTX 5060, a translator copy changed to use 1459translator is not changed here). On the RTX 5060, a translator copy changed to use
1403`fma_mix_odd` in nearest mode gave the single-rounded word for all seven inputs on the `vulkan` side as well. 1460`fma_mix_odd` in nearest mode gave the single-rounded word for all seven inputs on the `vulkan` side as well.
1404| NVIDIA GeForce RTX 5060 (Blackwell), Windows 11 | 621.24 | single | single, 8 of 8 words | not AMD |
1405| AMD Radeon gfx1036 (RDNA2), Linux / Windows | | single | single, 8 of 8 words | single, 8 of 8 words |
1406 1461
1407Measurement on AMD RDNA2 (gfx1036) confirmed single rounding across all 7 discriminating cases (finding 7079901079). 1462Measurement on AMD RDNA2 (gfx1036) confirmed single rounding across all 7 discriminating cases (finding 7079901079).
1408Using `fma_mix_odd` produces the exact hardware single-rounded words in both nearest-even and toward-zero modes. 1463Using `fma_mix_odd` produces the exact hardware single-rounded words in both nearest-even and toward-zero modes.
@@ -1454,6 +1509,31 @@ on k + 0.5, the error's sign gives floor(p) + 1 or floor(p), and a zero error ke
1454probe then exits 0 on the same iGPU: all 134 words equal the translator's in all ten runs (wave32 and wave64, five 1509probe then exits 0 on the same iGPU: all 134 words equal the translator's in all ten runs (wave32 and wave64, five
1455MODEs), including the 28 near ties. 1510MODEs), including the 28 near ties.
1456 1511
1512### `v_mad_f32`, `v_mac_f32`, `v_mad_legacy_f32` denormal flush on RDNA1 (`tools/mad_denorm_probe.py`, SHADER-340)
1513
1514`docs/design.md` ("Denormal flush in `v_mad_f32`, `v_mac_f32`, `v_mad_legacy_f32`") had no hardware measurement: GFX10.3+
1515dropped these opcodes, and flushing only after `omod` (`div:2`/`mul:2`/`mul:4`) was an unverified assumption. The probe
1516covers 48 cases across six groups: subnormal operands in each source slot (`input`, 15), normal operands with a subnormal
1517product (`product`, 7), normal operands/products with a subnormal sum (`result`, 6), `omod`/`clamp` crossing the
1518subnormal threshold (`omod`, 8), `v_mad_legacy_f32`'s `0 * anything -> +0` with subnormals (`legacy`, 5), and controls
1519(`control`, 7). A `v_mul_f32(FLT_MIN, 0.5)` sanity kernel checks that `MODE.denorm32=3` actually keeps f32 denormals on
1520the device (`0x00000000` with `denorm32=0`, `0x00400000` with `denorm32=3`).
1521
1522```
1523python -I tools/mad_denorm_probe.py # the translator, on the CPU interpreter
1524python -I tools/mad_denorm_probe.py --oracle hw # also the native instruction on an AMD GPU
1525```
1526
1527Results (48 cases per run, wave32 and wave64 across 5 MODEs; `tests/test_mad_denorm_probe.py` checks the translator side):
1528
1529| GPU | Driver | `game` / `dx10clamp0` / `denorm32_kept` | `ieee1` / `ieee1_dx10clamp0` (non-`omod` + `omod` ignored) |
1530|---|---|---|---|
1531| AMD Radeon RX 5600 XT (`gfx1010`, RDNA1), Windows, HIP backend (`amdhip64_6.dll`), AMD clang (`rocm-sdk-core 10.0.0`) | 32.0.21045.29003 | 48 of 48 (wave32 and wave64) | 48 of 48 (wave32 and wave64) |
1532
1533Two findings from the RDNA1 measurement:
1534- **Subnormal inputs, products, sums, and legacy rules flush regardless of `MODE.denorm32`** (40 of 40 non-`omod` cases match bit-for-bit across all 5 MODEs, including `denorm32=3`). Under `MODE.IEEE=1`, hardware ignores `mul:2`/`mul:4`/`div:2` (`FLT_MIN div:2` gives `FLT_MIN`, as for other f32 VALU ops).
1535- **Hardware flushes both before and after `omod`.** Before SHADER-340, the translator flushed only after `clamp_mods`, so `(1.5*2^-126 - 2^-126) mul:2` gave `0x00800000` (`FLT_MIN`) and `mul:4` gave `0x01000000`, whereas the RX 5600 XT returns `+0` (`0x00000000`) in `game`, `dx10clamp0`, and `denorm32_kept`. `Gen.flush_f32_mods` now flushes the sum before `clamp_mods` and flushes again after it (`FLT_MIN div:2` → `+0`, `-FLT_MIN div:2` → `-0`), leaving non-modified `v_mad*`/`v_mac*` instructions byte-identical.
1536
1457### f16 output modifiers, clamp and `v_fma_mix*` operands on RDNA2 (SHADER-135) 1537### f16 output modifiers, clamp and `v_fma_mix*` operands on RDNA2 (SHADER-135)
1458 1538
1459*Measured on an RDNA2 iGPU (gfx1036), AMD Windows driver 32.0.21044.64, HIP backend, AMD clang 23.0.0git, `main` 1539*Measured on an RDNA2 iGPU (gfx1036), AMD Windows driver 32.0.21044.64, HIP backend, AMD clang 23.0.0git, `main`
@@ -1606,6 +1686,72 @@ the hardware does not support is not refused here.
1606- **Constant operands under `op_sel` / `op_sel_hi`:** Integer constants under VOP3P are uniformly treated as 32-bit dwords `C`. When `op_sel[i] == 0`, bits `[15:0]` are selected; when `op_sel[i] == 1`, bits `[31:16]` are selected. Positive inline constants zero-extend (`5` → `0x00000005`: lo=`0x0005`, hi=`0x0000`; `64` → `0x00000040`: lo=`0x0040`, hi=`0x0000`). Negative inline constants sign-extend (`-1` → `0xFFFFFFFF`: lo=`0xFFFF`, hi=`0xFFFF`; `-16` → `0xFFFFFFF0`: lo=`0xFFF0`, hi=`0xFFFF`). 32-bit literals preserve their full dword (`0x12345678`: lo=`0x5678`, hi=`0x1234`; `0x80017fff`: lo=`0x7fff`, hi=`0x8001`). Float inline tokens (`0.5`, `1.0`, `-2.0`, `4.0`) assemble as their 32-bit IEEE 754 bit patterns (`1.0` → `0x3f800000`: lo=`0x0000`, hi=`0x3f80`). 1686- **Constant operands under `op_sel` / `op_sel_hi`:** Integer constants under VOP3P are uniformly treated as 32-bit dwords `C`. When `op_sel[i] == 0`, bits `[15:0]` are selected; when `op_sel[i] == 1`, bits `[31:16]` are selected. Positive inline constants zero-extend (`5` → `0x00000005`: lo=`0x0005`, hi=`0x0000`; `64` → `0x00000040`: lo=`0x0040`, hi=`0x0000`). Negative inline constants sign-extend (`-1` → `0xFFFFFFFF`: lo=`0xFFFF`, hi=`0xFFFF`; `-16` → `0xFFFFFFF0`: lo=`0xFFF0`, hi=`0xFFFF`). 32-bit literals preserve their full dword (`0x12345678`: lo=`0x5678`, hi=`0x1234`; `0x80017fff`: lo=`0x7fff`, hi=`0x8001`). Float inline tokens (`0.5`, `1.0`, `-2.0`, `4.0`) assemble as their 32-bit IEEE 754 bit patterns (`1.0` → `0x3f800000`: lo=`0x0000`, hi=`0x3f80`).
1607- **`clamp` saturation:** Unsigned add/mad saturate to `0xFFFF`; unsigned sub saturates underflow to `0x0000`; signed add/sub/mad saturate to `[-32768, 32767]`. 1687- **`clamp` saturation:** Unsigned add/mad saturate to `0xFFFF`; unsigned sub saturates underflow to `0x0000`; signed add/sub/mad saturate to `[-32768, 32767]`.
1608- **Silicon vs Translator parity:** 380 of 380 kernels match bit-for-bit between native hardware and `tools/rdna2spirv.py` lowered to SPIR-V (0 mismatches, 0 wave32/wave64 differences). 1688- **Silicon vs Translator parity:** 380 of 380 kernels match bit-for-bit between native hardware and `tools/rdna2spirv.py` lowered to SPIR-V (0 mismatches, 0 wave32/wave64 differences).
1689### Pixel-shader ISA on the hardware (`tools/ps_oracle.py`, EGEN-TOOL-PS-ORACLE)
1690
1691`tools/hw_oracle` only launches compute kernels, so what only a pixel shader has (`v_interp_*` and the parameter
1692words the SPI writes into LDS, the barycentric VGPRs, exports and their formats, helper lanes) could not be
1693measured there. `tools/ps_oracle.py` runs hand-written RDNA2 pixel-shader ISA in a real draw on an AMD GPU, through
1694Vulkan's `VK_KHR_pipeline_binary`:
1695
16961. `vk_harness` builds a pipeline from a template vertex + pixel shader and writes its binary (`VKH_BINARY_OUT`).
1697 On the AMD Windows driver that is one PAL ELF: `.text` with `_amdgpu_gs_main` (the vertex stage) and
1698 `_amdgpu_ps_main`, and a `.note` with the PAL metadata in msgpack, from which the driver programs the register
1699 counts and the SPI setup (`spi_ps_input_ena/addr`, `spi_ps_input_cntl` per parameter, `spi_shader_col_format`).
17002. The code under test is assembled with AMD clang (`LEONIDA_AMD_CLANG`, as for hw_oracle) and replaces
1701 `_amdgpu_ps_main`; `.text` grows if needed and the pixel stage's VGPR/SGPR counts are raised to the registers
1702 the code names. `--set PATH=JSON` changes a metadata entry as well, e.g.
1703 `.graphics_registers/.spi_ps_input_cntl/0/.fp16_interp_mode=true`.
17043. `vk_harness` builds the pipeline from the patched binary (`VKH_BINARY_IN`), draws one full-screen triangle into
1705 an RGBA32F target and reads every pixel's four mrt0 dwords back bit for bit.
1706
1707The template sets what the code finds when it starts, so `--show-template` prints the template's own ISA and SPI
1708metadata to copy the conventions from. With the built-in `interp2` template on gfx1036 the pixel stage is wave64,
1709the perspective I/J barycentrics are in v0/v1, M0 comes from s2 (`s_mov_b32 m0, s2`), and parameters 0 and 1 are
1710the vertex shader's locations 0 = (u, v, 0, 1) and 1 = (u, v, 0, 0), u and v going 0..2 across the screen.
1711
1712```powershell
1713$env:LEONIDA_VK_DEVICE = "radeon"
1714python -I tools/ps_oracle.py --build <runtime build> --template interp2 --show-template
1715python -I tools/ps_oracle.py --build <runtime build> --template interp2 --asm body.s --size 4 1 [--json out.json]
1716```
1717
1718Checked on an AMD Radeon iGPU (gfx1036, Windows driver 26.10.1): the driver accepts a patched binary (there is no
1719check over the code), the patched code runs as the pixel shader, and `v_interp_p1/p2_f32` of `attr0.x` at the four
1720pixel centres of a 4 x 1 target gives u = 0.125, 0.375, 0.625, 0.875, with `v_interp_mov_f32` reading the
1721parameter words P0 = 0 and P10 = 2.0 (`tests/test_ps_oracle.py`, which runs it on hardware when
1722`LEONIDA_PS_ORACLE_BUILD` names a runtime build). On other drivers the binary is not a single PAL ELF (the NVIDIA
1723driver returns two blobs) and the tool exits 77. Limits: one draw of one triangle, mrt0 as RGBA32F only; the vertex
1724stage is the template's, compiled by the driver; the SPI behaviour comes from the driver's metadata, so a `--set`
1725the hardware does not support is not refused here.
1726
1727**On Linux (RADV).** Mesa's RADV returns the pipeline as one binary per stage, as `radv_shader_serialize` writes it
1728(Mesa 26.3 `radv_pipeline_cache.c`, structs in `radv_shader.h`): a type word, `struct ac_shader_config` (`num_sgprs`,
1729`num_vgprs`, ..., `rsrc1`), `struct radv_shader_info`, `total_size`, then `code_size`, `exec_size` and the other
1730sizes, then stats, code, IR and disassembly. The info struct's size changes between Mesa versions, so the tool finds
1731the fields from their values: the size block whose sizes add up to `total_size`, the pixel stage from the
1732`wave_size, ., stage = 4 (fragment), next_stage = none` bytes in the info, and `rsrc1` as the config word whose
1733VGPRS field is `(num_vgprs - 1) / (wave32 ? 8 : 4)` (`radv_postprocess_binary_config`) and whose FLOAT_MODE equals
1734`float_mode`. A layout where these do not agree is refused (exit 77), not guessed. The code is replaced, the sizes
1735are fixed, and `num_vgprs`/`num_sgprs` and rsrc1's VGPRS are raised; RADV builds an imported binary with
1736`radv_shader_create_uncached` (no cache lookup by key) and programs `SPI_SHADER_PGM_RSRC1_PS` from `rsrc1`. Two
1737limits: `--set` is refused (RADV keeps the SPI setup in its own structs, so a template has to ask for what it
1738needs), and only GFX10 (RDNA1/RDNA2) devices are taken, from the chip name RADV puts in the device name; GFX11 and
1739later interpolate with `lds_param_load` instead of `v_interp_*`, so e.g. an RX 9070 XT (`RADV GFX1201`) gets
1740`unsupported architecture` and exit 77.
1741
1742Mesa optimises varyings across stages, so a template's parameter layout can differ from the Windows driver's:
1743RADV moves `interp2`'s `in0 + in1` into the vertex shader and the pixel shader reads one parameter, attr0 =
1744(2u, 2v, 0, 1); `--show-template` shows the layout a run gets.
1745
1746```sh
1747LEONIDA_VK_DEVICE=raphael python -I tools/ps_oracle.py --build <runtime build> --template interp2 --show-template
1748```
1749
1750Checked on the Ryzen 7 9800X3D iGPU (`RADV RAPHAEL_MENDOCINO`, gfx1036, wave64 pixel stage, Mesa 26.3.1, CachyOS):
1751the Windows check's `attr0.x` code (with `v40`, past the template's 8 VGPRs) gives u = 0.25, 0.75, 1.25, 1.75,
1752P0 = 0 and P10 = 4.0 for the single parameter, and a second patch with another constant changes the result (nothing
1753is cached).
1754
1609 1755
1610## Coverage plan and cost 1756## Coverage plan and cost
1611 1757