Fix FXC SM5.1 compilation of the shared normalization helper #63

Closed
github-bot wants to merge 1 commit from gh-pr/63/head into gh-pr/63/base
Owner

@⁠philip-roy-jones on GitHub · 2026-11-21 22:06 UTC · original
fix/fxc-sm51-normalization → main · +72 −1 · 3 files · 1 commits · closed without merging

Library / area: tombox-xshader HLSL emission and the Windows FXC path

Exports changed (exports.txt rows): None

Source of the behaviour: HLSL introduced uint64_t in Shader Model 6.0. The translator's normalized conversions use round-to-nearest-even after exact scaling. Microsoft HLSL scalar types · Existing normalization oracle

On upstream 0fc72956f9ff742a275534c27d9e5a7a9886a0c3, a hand-assembled pixel shader that loads one 32-bit word from b0, space1 and exports it selects ps_5_1. FXC rejects the shared tb_nrm declaration before eliminating the unused helper. The shader scan and the runtime centroid test both reported this failure. #56 · #59

uint64_t p = (uint64_t)((a & 0x7fffffu) | 0x800000u) * (uint64_t)k;
fxc.exe /nologo /T ps_5_1 /E main /Fo upstream-plain.cso upstream-plain.hlsl
upstream-plain.hlsl(13,136-143): error X3000: unrecognized identifier 'uint64_t'
upstream-plain.hlsl(13,145): error X3000: unrecognized identifier 'p'

The emitter selects SM5.1 for register spaces in VS, PS, CS and GS, and for pixel shaders with UAVs. On Windows, d3d12_xs can use D3DCompile for those profiles, including when TOMBOX_SHADER_COMPILER=fxc is set. xscan --compile 1 --compiler fxc invokes the SDK executable. DXC maps the same profiles to SM6.2. Profile selection · Runtime compiler selection

tb_nrm now splits the 24-bit significand × 16-bit scale at bit 16. The upper part and the low 16 bits determine whether the exact product is at a rounding tie. Both pieces fit in SM5 uint registers.

Verification

Windows x64, Windows SDK 10.0.26100.0, FXC 10.0.26100.8249, DXC 1.8.2502.11 and D3D12 WARP. The branch commit is e9488eab22d70e48b63737bbdce2111c5f424561.

Self-authored shader Upstream FXC Branch FXC DXC control on both revisions
ps_5_1 CBV passthrough X3000 676 bytes Byte-identical DXIL
Same shader with live v_cvt_pknorm_u16_f32 X3000 1,680 bytes Both compile. Upstream DXIL contains mul nuw nsw i64 by 65535.
cs_5_1 dword store to u0, space1 X3000 692 bytes Both compile
ps_5_0 passthrough in space 0 X3000 644 bytes Both compile
image_bvh64_intersect_ray with a CBV-supplied node and ray X3000 X3000 in the BVH address helper Byte-identical DXIL

The BVH control still needs DXC for its native 64-bit addresses, even though the emitter reports ps_5_1. Its WARP execution test also passes.

Command Upstream Branch
cargo test --locked -p tombox-xshader --no-fail-fast 134 passed, 12 ignored 135 passed, 12 ignored
cargo test --locked -p d3d12_xs --lib lowered_pixel_shader_compiles_with_fxc_in_both_variants 1 failed, X3000 1 passed
cargo test --locked -p d3d12_xs --lib fxc 1 passed, 1 failed 2 passed, including SM6 profile rejection
cargo test --locked -p tombox-xshader --bin xscan fxc_compiles_sm51_pixel_exports 1 failed, X3000, with just this test applied 1 passed
xscan.exe <input>.dxbc --compile 1 --compiler fxc --jobs 1, fresh cache for plain, norm and store 0/3 compiled 3/3 compiled

cargo test --locked -p tombox-xshader --test exec vector_vop3 -- --exact --nocapture passed 325 constant-input cases and 325 runtime-input cases on WARP, including adjacent float32 values whose exact scaled results straddle 1.5. As an additional readback check, the original DXC helper and the branch FXC/DXC helpers each matched a float64 ties-to-even reference on 393,251 runtime inputs. Those include the floats on either side of every SNORM16 and UNORM16 rounding boundary.

Reproduce the SDK compiler runs with synthetic inputs

Save this Python script as make_inputs.py in a scratch directory and run python make_inputs.py. The fixtures contain only the instructions and metadata below.

from pathlib import Path
import struct
import zlib

def words(*values):
    return struct.pack('<' + 'I' * len(values), *values)

def sig(name, sysval, ctype, mask, rw):
    return words(1, 8, 0, 40, 0, sysval, ctype, 0, mask | rw << 8, 0) + name + b'\0'

def container(code, stage=0, root=None):
    section = words(0, 0, 0, 0, *code)
    fssm = b'FSSM' + bytes(0xc0 - 4) + words(0xd0, 0, len(section), 0) + section
    xdat = b'XDATNOBC' + zlib.compress(fssm)
    dxil = words((stage << 16) | 0x60, (24 + len(xdat) + 3) // 4) + b'DXIL' + words(0x100, 20, 4) + xdat
    rts = root if root is not None else words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 2, 1, 0, 1, 0, 0)
    chunks = [(b'DXIL', dxil)]
    if stage == 0:
        chunks += [(b'ISG1', sig(b'SV_Position', 1, 3, 15, 0)), (b'OSG1', sig(b'SV_Target', 64, 1, 1, 0))]
    chunks += [(b'RTS0', rts)]
    offsets, payload = [], b''
    for tag, data in chunks:
        offsets.append(32 + 4 * len(chunks) + len(payload))
        payload += tag + words(len(data)) + data + bytes(-len(data) % 4)
    return b'DXBC' + bytes(16) + words(1, 32 + len(offsets)*4 + len(payload), len(offsets), *offsets) + payload

load = [0xf4080004, 0xfa000000, 0xbf8cc07f, 0xf4200100, 0xfa000000, 0xbf8cc07f, 0x7e000204]
export = [0xf8001801, 0, 0xbf810000]
for name, middle in [('plain', []), ('norm', [0xd7690000, 0x00020100])]:
    code = load + middle + export
    (Path(__file__).parent / (name + '.dxbc')).write_bytes(container(code))

folder = Path(__file__).parent
store = [0xf4080002, 0xfa000000, 0xbf8cc07f, 0x7e000287, 0xe0701000, 0x80000001, 0xbf810000]
uav_table = words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 1, 1, 0, 1, 0, 0)
(folder / 'store.dxbc').write_bytes(container(store, stage=5, root=uav_table))
(folder / 'space0.dxbc').write_bytes(container(load + export, root=words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 2, 1, 0, 0, 0, 0)))
bvh = [0xf4080804, 0xfa000000, 0xbf8cc07f, 0xf4280010, 0xfa000000, 0xf4300410, 0xfa000010, 0xbf8cc07f]
bvh.extend(0x7e000200 | ((n+1) << 17) | (16+n) for n in range(12))
bvh.extend([0xf19c0f01, 0x00001401, 0xbf8c3f70, 0x7e000314, *export])
(folder / 'bvh64.dxbc').write_bytes(container(bvh))

Build the binaries in each revision with cargo build --locked -p tombox-xshader --bin xscan --bin xshader. From the fixture directory, set $bin to that build's debug binary directory and $tree to upstream or branch. Put the SDK's x64 compiler directory on PATH.

foreach ($name in 'plain','norm','store','space0','bvh64') {
    & "$bin\xshader.exe" "$name.dxbc" --hlsl-out "$tree-$name.hlsl"
    $fxProfile = switch ($name) { 'store' { 'cs_5_1' } 'space0' { 'ps_5_0' } default { 'ps_5_1' } }
    $dxProfile = if ($name -eq 'store') { 'cs_6_2' } else { 'ps_6_2' }
    fxc.exe /nologo /T $fxProfile /E main /Fo "$tree-$name.cso" "$tree-$name.hlsl"
    dxc.exe -T $dxProfile -E main -Fo "$tree-$name.dxil" -Fc "$tree-$name.ll" "$tree-$name.hlsl"
}
  • cargo test -p tombox-xshader passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠philip-roy-jones** on GitHub · 2026-11-21 22:06 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/63) `fix/fxc-sm51-normalization` → `main` · +72 −1 · 3 files · 1 commits · closed without merging **Library / area:** `tombox-xshader` HLSL emission and the Windows FXC path **Exports changed (exports.txt rows):** None **Source of the behaviour:** HLSL introduced `uint64_t` in Shader Model 6.0. The translator's normalized conversions use round-to-nearest-even after exact scaling. [Microsoft HLSL scalar types](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-scalar) · [Existing normalization oracle](https://github.com/omonkeyz/xbox-emulator/blob/0fc72956f9ff742a275534c27d9e5a7a9886a0c3/crates/tombox-xshader/tests/isa_sweep/ops_v.rs#L662) On upstream `0fc72956f9ff742a275534c27d9e5a7a9886a0c3`, a hand-assembled pixel shader that loads one 32-bit word from `b0, space1` and exports it selects `ps_5_1`. FXC rejects the shared `tb_nrm` declaration before eliminating the unused helper. The shader scan and the runtime centroid test both reported this failure. [#56](https://git.gtahub.st/github/omonkeyz--xbox-emulator/pulls/56) · [#59](https://git.gtahub.st/github/omonkeyz--xbox-emulator/pulls/59) ```hlsl uint64_t p = (uint64_t)((a & 0x7fffffu) | 0x800000u) * (uint64_t)k; ``` ```text fxc.exe /nologo /T ps_5_1 /E main /Fo upstream-plain.cso upstream-plain.hlsl upstream-plain.hlsl(13,136-143): error X3000: unrecognized identifier 'uint64_t' upstream-plain.hlsl(13,145): error X3000: unrecognized identifier 'p' ``` The emitter selects SM5.1 for register spaces in VS, PS, CS and GS, and for pixel shaders with UAVs. On Windows, `d3d12_xs` can use `D3DCompile` for those profiles, including when `TOMBOX_SHADER_COMPILER=fxc` is set. `xscan --compile 1 --compiler fxc` invokes the SDK executable. DXC maps the same profiles to SM6.2. [Profile selection](https://github.com/omonkeyz/xbox-emulator/blob/0fc72956f9ff742a275534c27d9e5a7a9886a0c3/crates/tombox-xshader/src/hlsl.rs#L989) · [Runtime compiler selection](https://github.com/omonkeyz/xbox-emulator/blob/0fc72956f9ff742a275534c27d9e5a7a9886a0c3/crates/compat/d3d12_xs/src/shader_xlat.rs#L199) `tb_nrm` now splits the 24-bit significand × 16-bit scale at bit 16. The upper part and the low 16 bits determine whether the exact product is at a rounding tie. Both pieces fit in SM5 `uint` registers. ### Verification Windows x64, Windows SDK 10.0.26100.0, FXC 10.0.26100.8249, DXC 1.8.2502.11 and D3D12 WARP. The branch commit is `e9488eab22d70e48b63737bbdce2111c5f424561`. | Self-authored shader | Upstream FXC | Branch FXC | DXC control on both revisions | | --- | --- | --- | --- | | `ps_5_1` CBV passthrough | X3000 | 676 bytes | Byte-identical DXIL | | Same shader with live `v_cvt_pknorm_u16_f32` | X3000 | 1,680 bytes | Both compile. Upstream DXIL contains `mul nuw nsw i64` by 65535. | | `cs_5_1` dword store to `u0, space1` | X3000 | 692 bytes | Both compile | | `ps_5_0` passthrough in space 0 | X3000 | 644 bytes | Both compile | | `image_bvh64_intersect_ray` with a CBV-supplied node and ray | X3000 | X3000 in the BVH address helper | Byte-identical DXIL | The BVH control still needs DXC for its native 64-bit addresses, even though the emitter reports `ps_5_1`. Its WARP execution test also passes. | Command | Upstream | Branch | | --- | --- | --- | | `cargo test --locked -p tombox-xshader --no-fail-fast` | 134 passed, 12 ignored | 135 passed, 12 ignored | | `cargo test --locked -p d3d12_xs --lib lowered_pixel_shader_compiles_with_fxc_in_both_variants` | 1 failed, X3000 | 1 passed | | `cargo test --locked -p d3d12_xs --lib fxc` | 1 passed, 1 failed | 2 passed, including SM6 profile rejection | | `cargo test --locked -p tombox-xshader --bin xscan fxc_compiles_sm51_pixel_exports` | 1 failed, X3000, with just this test applied | 1 passed | | `xscan.exe <input>.dxbc --compile 1 --compiler fxc --jobs 1`, fresh cache for `plain`, `norm` and `store` | 0/3 compiled | 3/3 compiled | `cargo test --locked -p tombox-xshader --test exec vector_vop3 -- --exact --nocapture` passed 325 constant-input cases and 325 runtime-input cases on WARP, including adjacent float32 values whose exact scaled results straddle 1.5. As an additional readback check, the original DXC helper and the branch FXC/DXC helpers each matched a float64 ties-to-even reference on 393,251 runtime inputs. Those include the floats on either side of every SNORM16 and UNORM16 rounding boundary. <details> <summary>Reproduce the SDK compiler runs with synthetic inputs</summary> Save this Python script as `make_inputs.py` in a scratch directory and run `python make_inputs.py`. The fixtures contain only the instructions and metadata below. ```python from pathlib import Path import struct import zlib def words(*values): return struct.pack('<' + 'I' * len(values), *values) def sig(name, sysval, ctype, mask, rw): return words(1, 8, 0, 40, 0, sysval, ctype, 0, mask | rw << 8, 0) + name + b'\0' def container(code, stage=0, root=None): section = words(0, 0, 0, 0, *code) fssm = b'FSSM' + bytes(0xc0 - 4) + words(0xd0, 0, len(section), 0) + section xdat = b'XDATNOBC' + zlib.compress(fssm) dxil = words((stage << 16) | 0x60, (24 + len(xdat) + 3) // 4) + b'DXIL' + words(0x100, 20, 4) + xdat rts = root if root is not None else words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 2, 1, 0, 1, 0, 0) chunks = [(b'DXIL', dxil)] if stage == 0: chunks += [(b'ISG1', sig(b'SV_Position', 1, 3, 15, 0)), (b'OSG1', sig(b'SV_Target', 64, 1, 1, 0))] chunks += [(b'RTS0', rts)] offsets, payload = [], b'' for tag, data in chunks: offsets.append(32 + 4 * len(chunks) + len(payload)) payload += tag + words(len(data)) + data + bytes(-len(data) % 4) return b'DXBC' + bytes(16) + words(1, 32 + len(offsets)*4 + len(payload), len(offsets), *offsets) + payload load = [0xf4080004, 0xfa000000, 0xbf8cc07f, 0xf4200100, 0xfa000000, 0xbf8cc07f, 0x7e000204] export = [0xf8001801, 0, 0xbf810000] for name, middle in [('plain', []), ('norm', [0xd7690000, 0x00020100])]: code = load + middle + export (Path(__file__).parent / (name + '.dxbc')).write_bytes(container(code)) folder = Path(__file__).parent store = [0xf4080002, 0xfa000000, 0xbf8cc07f, 0x7e000287, 0xe0701000, 0x80000001, 0xbf810000] uav_table = words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 1, 1, 0, 1, 0, 0) (folder / 'store.dxbc').write_bytes(container(store, stage=5, root=uav_table)) (folder / 'space0.dxbc').write_bytes(container(load + export, root=words(2, 1, 24, 0, 0, 0, 0, 0, 36, 1, 44, 2, 1, 0, 0, 0, 0))) bvh = [0xf4080804, 0xfa000000, 0xbf8cc07f, 0xf4280010, 0xfa000000, 0xf4300410, 0xfa000010, 0xbf8cc07f] bvh.extend(0x7e000200 | ((n+1) << 17) | (16+n) for n in range(12)) bvh.extend([0xf19c0f01, 0x00001401, 0xbf8c3f70, 0x7e000314, *export]) (folder / 'bvh64.dxbc').write_bytes(container(bvh)) ``` Build the binaries in each revision with `cargo build --locked -p tombox-xshader --bin xscan --bin xshader`. From the fixture directory, set `$bin` to that build's debug binary directory and `$tree` to `upstream` or `branch`. Put the SDK's x64 compiler directory on PATH. ```powershell foreach ($name in 'plain','norm','store','space0','bvh64') { & "$bin\xshader.exe" "$name.dxbc" --hlsl-out "$tree-$name.hlsl" $fxProfile = switch ($name) { 'store' { 'cs_5_1' } 'space0' { 'ps_5_0' } default { 'ps_5_1' } } $dxProfile = if ($name -eq 'store') { 'cs_6_2' } else { 'ps_6_2' } fxc.exe /nologo /T $fxProfile /E main /Fo "$tree-$name.cso" "$tree-$name.hlsl" dxc.exe -T $dxProfile -E main -Fo "$tree-$name.dxil" -Fc "$tree-$name.ll" "$tree-$name.hlsl" } ``` </details> - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
Split the exact significand product at bit 16 so the shared HLSL helper compiles on SM5 and still rounds ties to even. Compile a synthetic register-space pixel shader with D3DCompile and check adjacent SNORM and UNORM inputs on WARP.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:08:57 +00:00
github-bot closed this pull request 2026-11-26 06:08:57 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.