tombox-xshader: bind literal static samplers through the FSSM table #329

Closed
github-bot wants to merge 4 commits from gh-pr/329/head into gh-pr/329/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 20:16 UTC · original
GDAgent:feat/xshader-fssm-static-samplers → main · +1426 −10 · 11 files · 4 commits · closed without merging

Stacked on #289 (head 714efcc); only 943e546 and b02b817 belong to this PR. Please review git diff 714efcc.. until #289 lands; I will rebase onto main then.
Library / area: tombox-xshader (new hlsl/static_sampler.rs, a strict literal check next to the S# decoder in hlsl/durango.rs, one call site in hlsl.rs).

Exports changed: None.

Builds on #289 (FSSM root-binding table reader). Complements #183; see "Relation to #183".

Problem

Console shaders that use a static sampler do not load a sampler descriptor. The compiler builds the four S# words in SGPRs from literals (s_movk_i32 / s_mov_b32) right before image_sample. Without a root signature carrying the sampler state, the translator refuses these with mimg ...: sN not a sampler descriptor (None). With #289's table fallback this is the largest first refusal on a very popular AAA title that ships without RTS0 (7,582 of 25,889 unique shaders). 7,551 of those use static samplers listed in their FSSM table.

What the table says

The table's parameter list has one kind-4 entry per static sampler in the root signature (index, shader register). The resource list names the static samplers the shader actually uses (source 4, class sampler, index, register). It carries no sampler state: no filter, address mode or border colour. The state exists only in the literal words in the ISA. The register space is not in the table either, but the shader's PSV0 sampler bindings give it (the AAA title's static samplers are in space 1).

Change

  1. Literal proof over the control-flow graph. A forward dataflow over the CFG the translator already builds proves which SGPRs hold a known literal at each sampling instruction.
    • It tracks s_mov_b32 / s_movk_i32 / s_brev_b32 / s_not_b32 literals and SGPR copies (s_mov_b32, s_mov_b64). Paths must agree at joins, and loops iterate to a fixpoint.
    • Every other SGPR write clobbers: multi-dword SMEM, VOP3B carry-out (*_co_*, v_div_scale, v_mad_u64_u32), v_readlane_b32 / v_readfirstlane_b32, VOPC/VOP3 compare sdst, and the sdst pair of any SOP1/SOP2/SOPK op the decoder does not name.
    • s_setpc, s_swappc, s_rfe, s_movrels(d), s_cbranch_join and s_call forget everything.
    • Only the four sampler SGPRs below vcc_lo are considered. Shaders with branches and waterfall loops are covered.
  2. No root signature (the #289 fallback): if the table lists exactly one used static sampler, the literal sampler binds to that register (register(sN, spaceK), space from PSV0). If it lists several, translation is refused with the four words and the candidate registers named, because the table cannot tell them apart.
  3. A root signature with static samplers (the runtime PSO root signature that d3d12_xs passes for shaders without RTS0, or an RTS0): candidates are narrowed to the root signature's visible static samplers at the table's registers. The decoded literal must then match strictly: address modes, filter reduction, comparison function, filter (min/mag/mip), anisotropy, and the border colour type (w3 >> 30) when border addressing is used. Any known field that differs refuses. Two remaining candidates with different state refuse as well.
  4. Shaders whose table lists no static samplers, or with no table, take exactly the old path (Matrix has none). The Durango selection path runs first and is unchanged.

Relation to #183

#183 resolves literal samplers against the root signature's static samplers by decoded state, for straight-line RDNA2 shaders, using a backward scan (sgpr_const_in). This PR differs in three ways:

  • It needs and uses the FSSM table, so it also works offline when no root signature exists (one used static sampler).
  • With a root signature it first narrows the candidates to the registers the shader really uses. That removes ties #183 has to refuse.
  • Its constant proof is a CFG dataflow and already models the writes raised in #183's review (VOP3B carry-out, SMEM widths, register bound below vcc_lo). The strict field comparison follows the review's list.

Both can land. Order at the call site: Durango path, then #183's RDNA2 straight-line resolver (if merged), then this table-based resolver. If #183 lands first, its strict check and this literal_mismatch should become one function; I'm happy to rebase onto it.

Evidence

The Matrix Awakens (RTS0 + table, no static samplers in any table): must be byte-identical. Translate-only comparison of the HLSL hash of every unique shader against the base 714efcc: 35,442 shaders compared, 35,382 identical HLSL, 60 failing in both with the same message, 0 differences.

A very popular AAA title (no RTS0), translate-only, --fssm-root 1, 25,889 unique shaders, base 714efcc against this branch:

  • Translated: 5,763 -> 6,612 (+849: 520 PS, 293 CS, 26 VS, 10 MS). The 5,763 shaders that translated before have byte-identical HLSL; no shader that translated before fails now.
  • Of the 7,582 shaders that the base refused with sN not a sampler descriptor, 849 now translate (exactly one used static sampler) and 5,066 are refused with the new explicit message, because their table lists 2-12 (up to 14) used static samplers and no root signature exists offline to say which one a literal selects (4 is the most common). 628 keep the old message (no static sampler in their table).
  • So the offline gain is limited. Most of the remainder needs the PSO root signature at runtime, where the narrowed strict match applies. That path is covered by synthetic tests only.

Tests

  • hlsl/static_sampler.rs (7 unit tests): literals through a straight line, s_movk + copies, a branch where both paths agree, a word set on one path only, clobbers (VOP3B carry-out into a sampler word, v_readlane, s_buffer_load_dwordx4, s_swappc, s_movreld, an unnamed SOP1 into the words) vs non-clobbers (carry-out and unnamed SOP1 into other SGPRs, s_cmpk), and loop back edges.
  • tests/fssm_bind.rs (+6):
    • one table static sampler binds register(s5, space1) without a root signature (DXC compiles), also with the literal set before a branch;
    • two table static samplers without state are refused;
    • a clobbered word keeps the old refusal;
    • the table narrows two compatible root-signature static samplers to the used register (DXC compiles);
    • a wrap or point-mip static sampler does not bind a clamp/trilinear literal (error names the field).
  • cargo test --locked -p tombox-xshader --no-fail-fast (debug, native Windows 11): 370 passed, 0 failed, 18 ignored. That includes lib 128/0/3, fssm_bind 11/11 and tests/exec.rs 128/0/2.
  • PR gate against #289's head (714efcc): 0 problems.

Not verified in a title

No title was run. The no-root-signature case is checked offline (translation + DXC) only. The root-signature case is checked only with synthetic root signatures: the AAA title's root signatures and their static-sampler states have not been seen, so the claim that the table's static-sampler registers match the title's PSO root signature is untested. Static sampler register spaces come from PSV0. The table itself has none. Native Windows 11; not run under Proton.

Merge notes

This branch and #183 both edit the sampler call site in hlsl.rs and add tests to tests/synthetic.rs, so whichever lands second needs a small rebase. No conflict with #278 or #302.

**@⁠GDAgent** on GitHub · 2026-11-23 20:16 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/329) `GDAgent:feat/xshader-fssm-static-samplers` → `main` · +1426 −10 · 11 files · 4 commits · closed without merging Stacked on #289 (head `714efcc`); only `943e546` and `b02b817` belong to this PR. Please review `git diff 714efcc..` until #289 lands; I will rebase onto `main` then. **Library / area:** `tombox-xshader` (new `hlsl/static_sampler.rs`, a strict literal check next to the S# decoder in `hlsl/durango.rs`, one call site in `hlsl.rs`). **Exports changed:** None. Builds on #289 (FSSM root-binding table reader). Complements #183; see "Relation to #183". ### Problem Console shaders that use a static sampler do not load a sampler descriptor. The compiler builds the four S# words in SGPRs from literals (`s_movk_i32` / `s_mov_b32`) right before `image_sample`. Without a root signature carrying the sampler state, the translator refuses these with `mimg ...: sN not a sampler descriptor (None)`. With #289's table fallback this is the largest first refusal on a very popular AAA title that ships without RTS0 (7,582 of 25,889 unique shaders). 7,551 of those use static samplers listed in their FSSM table. ### What the table says The table's parameter list has one kind-4 entry per static sampler in the root signature (`index`, shader register). The resource list names the static samplers the shader actually uses (source 4, class sampler, `index`, register). It carries **no sampler state**: no filter, address mode or border colour. The state exists only in the literal words in the ISA. The register space is not in the table either, but the shader's PSV0 sampler bindings give it (the AAA title's static samplers are in space 1). ### Change 1. **Literal proof over the control-flow graph.** A forward dataflow over the CFG the translator already builds proves which SGPRs hold a known literal at each sampling instruction. - It tracks `s_mov_b32` / `s_movk_i32` / `s_brev_b32` / `s_not_b32` literals and SGPR copies (`s_mov_b32`, `s_mov_b64`). Paths must agree at joins, and loops iterate to a fixpoint. - Every other SGPR write clobbers: multi-dword SMEM, VOP3B carry-out (`*_co_*`, `v_div_scale`, `v_mad_u64_u32`), `v_readlane_b32` / `v_readfirstlane_b32`, VOPC/VOP3 compare sdst, and the sdst pair of any SOP1/SOP2/SOPK op the decoder does not name. - `s_setpc`, `s_swappc`, `s_rfe`, `s_movrels(d)`, `s_cbranch_join` and `s_call` forget everything. - Only the four sampler SGPRs below vcc_lo are considered. Shaders with branches and waterfall loops are covered. 2. **No root signature (the #289 fallback):** if the table lists exactly one used static sampler, the literal sampler binds to that register (`register(sN, spaceK)`, space from PSV0). If it lists several, translation is refused with the four words and the candidate registers named, because the table cannot tell them apart. 3. **A root signature with static samplers** (the runtime PSO root signature that d3d12_xs passes for shaders without RTS0, or an RTS0): candidates are narrowed to the root signature's visible static samplers at the table's registers. The decoded literal must then match strictly: address modes, filter reduction, comparison function, filter (min/mag/mip), anisotropy, and the border colour type (`w3 >> 30`) when border addressing is used. Any known field that differs refuses. Two remaining candidates with different state refuse as well. 4. Shaders whose table lists no static samplers, or with no table, take exactly the old path (Matrix has none). The Durango selection path runs first and is unchanged. ### Relation to #183 #183 resolves literal samplers against the root signature's static samplers by decoded state, for straight-line RDNA2 shaders, using a backward scan (`sgpr_const_in`). This PR differs in three ways: - It needs and uses the FSSM table, so it also works offline when no root signature exists (one used static sampler). - With a root signature it first narrows the candidates to the registers the shader really uses. That removes ties #183 has to refuse. - Its constant proof is a CFG dataflow and already models the writes raised in #183's review (VOP3B carry-out, SMEM widths, register bound below vcc_lo). The strict field comparison follows the review's list. Both can land. Order at the call site: Durango path, then #183's RDNA2 straight-line resolver (if merged), then this table-based resolver. If #183 lands first, its strict check and this `literal_mismatch` should become one function; I'm happy to rebase onto it. ### Evidence **The Matrix Awakens (RTS0 + table, no static samplers in any table):** must be byte-identical. Translate-only comparison of the HLSL hash of every unique shader against the base `714efcc`: 35,442 shaders compared, 35,382 identical HLSL, 60 failing in both with the same message, 0 differences. **A very popular AAA title (no RTS0), translate-only, `--fssm-root 1`, 25,889 unique shaders**, base `714efcc` against this branch: - Translated: 5,763 -> 6,612 (+849: 520 PS, 293 CS, 26 VS, 10 MS). The 5,763 shaders that translated before have byte-identical HLSL; no shader that translated before fails now. - Of the 7,582 shaders that the base refused with `sN not a sampler descriptor`, 849 now translate (exactly one used static sampler) and 5,066 are refused with the new explicit message, because their table lists 2-12 (up to 14) used static samplers and no root signature exists offline to say which one a literal selects (4 is the most common). 628 keep the old message (no static sampler in their table). - So the offline gain is limited. Most of the remainder needs the PSO root signature at runtime, where the narrowed strict match applies. That path is covered by synthetic tests only. ### Tests - `hlsl/static_sampler.rs` (7 unit tests): literals through a straight line, `s_movk` + copies, a branch where both paths agree, a word set on one path only, clobbers (VOP3B carry-out into a sampler word, `v_readlane`, `s_buffer_load_dwordx4`, `s_swappc`, `s_movreld`, an unnamed SOP1 into the words) vs non-clobbers (carry-out and unnamed SOP1 into other SGPRs, `s_cmpk`), and loop back edges. - `tests/fssm_bind.rs` (+6): - one table static sampler binds `register(s5, space1)` without a root signature (DXC compiles), also with the literal set before a branch; - two table static samplers without state are refused; - a clobbered word keeps the old refusal; - the table narrows two compatible root-signature static samplers to the used register (DXC compiles); - a wrap or point-mip static sampler does not bind a clamp/trilinear literal (error names the field). - `cargo test --locked -p tombox-xshader --no-fail-fast` (debug, native Windows 11): 370 passed, 0 failed, 18 ignored. That includes lib 128/0/3, `fssm_bind` 11/11 and `tests/exec.rs` 128/0/2. - PR gate against #289's head (`714efcc`): 0 problems. ### Not verified in a title No title was run. The no-root-signature case is checked offline (translation + DXC) only. The root-signature case is checked only with synthetic root signatures: the AAA title's root signatures and their static-sampler states have not been seen, so the claim that the table's static-sampler registers match the title's PSO root signature is untested. Static sampler register spaces come from PSV0. The table itself has none. Native Windows 11; not run under Proton. ### Merge notes This branch and #183 both edit the sampler call site in `hlsl.rs` and add tests to `tests/synthetic.rs`, so whichever lands second needs a small rebase. No conflict with #278 or #302.
The console shader compiler stores the shader's root-binding layout inside
the FSSM container, next to the code, whether or not the DXBC carries an
RTS0 chunk. fssm_bind::parse reads it:

- 0xE8 / 0xF8: parameter list (pointer + 0x10, count), 5-dword entries
  (kind, dword offset, count, register, param << 8 | user-data slot).
  Kinds: 0 root constants (count = num32), 1 root descriptor (2 SGPRs),
  3 descriptor table, 4 static sampler (index, register), 5 resource heap
  base, 6 sampler heap base. 0xE8 = DEADBEEF means no table.
- 0xF0 / 0xFC: resource list, one entry per descriptor the shader uses:
  (source << 8 | class, dword offset in the table, register, byte stride,
  param << 16 | slot << 8 | descriptor dwords). Class 0 SRV, 1 sampler,
  2 CBV, 3 UAV (not the D3D12 range order). Table strides are 8 dwords for
  CBV/SRV/UAV and 4 for samplers.
- 0xD6: user SGPR count; it equals the end of the last binding.
- 0x18..0x60: exactly one hardware-stage slot is set. 0x28 (merged ES
  part of a vertex shader), 0x30 (GS) and 0x58 (MS) start user data after
  8 system SGPRs; 0x38 (hardware VS), 0x40 (PS) and none (CS) at s0.

The parser refuses anything that does not fit: unknown kinds, gaps in the
SGPR layout, a user SGPR count that does not end at the last binding,
resources that name a parameter of another kind or slot, and a register
used at two table positions (the table has no register space field).

Bindings::root_signature builds a RootSig with one single-descriptor range
per used descriptor (space 0); Bindings::disagreements compares the table
with a real root signature.

Evidence (The Matrix Awakens, all 35,442 shaders with an RTS0, native
Windows 11, CPU only): every table parses, covers every RTS0 parameter,
and every used descriptor resolves to the same range type and register in
the RTS0. The stage-slot rule matches the SGPRs the code reads in every
shader where that can be checked.
translate_with_fssm_root / translate_linked_with_fssm_root translate a
shader that has no RTS0 (and no root signature supplied by the caller)
with the root signature built from its FSSM binding table, placing the
first parameter at the user-data base the table gives. Shaders with a
root signature are translated exactly as before.

Opt-in only:
- TOMBOX_FSSM_ROOT_SIG=1 makes hlsl::translate_linked use the table when
  no root signature is known (added to the d3d12_xs translation-index
  bypass list like TOMBOX_INFER_ROOT_SIG);
- xscan --fssm-root 1 sweeps an archive with the fallback;
- xscan --fssm-check 1 translates every shader that has an RTS0 a second
  time from its table alone and reports agreement and HLSL differences;
- xshader --fssm-root 1 ignores the RTS0 and prints the decoded table.

Static samplers (kind 4) and descriptor-heap bases (kinds 5/6) are parsed
and reported, but the translator still refuses literal samplers and
heap-indexed loads; those need their own work.

Matrix Awakens cross-check (xscan --fssm-check 1, 35,442 shaders with an
RTS0): see the PR description for the per-stage table.
Console shaders that sample with a static sampler build the four S# words
in SGPRs from literals instead of loading a descriptor. Without a
root signature that carries the sampler state, the translator refused
these with "sN not a sampler descriptor".

The FSSM root-binding table lists the static samplers a shader uses
(list 2, source 4) with their shader registers. This uses it in two
cases:

- No root signature (the opt-in FSSM fallback): when the table names
  exactly one used static sampler, the literal sampler binds to that
  register. The register space comes from the shader's PSV0 sampler
  bindings. Several used static samplers are refused, because the
  table carries no sampler state to tell them apart.
- A root signature with static samplers (runtime PSO root signature
  or RTS0): the candidates are narrowed to the table's registers and
  the decoded literal must match one of them strictly (address modes,
  reduction, comparison function, filter, anisotropy, and the border
  colour type when border addressing is used). Any known field that
  differs refuses.

The words must be proven constant at the sampling instruction by a
forward dataflow over the existing control-flow graph: s_mov/s_movk
literals and SGPR copies are tracked, paths must agree at joins, and
every other SGPR write clobbers (multi-dword SMEM, VOP3B carry-out,
v_readlane/v_readfirstlane, VOPC sdst). s_movreld, s_swappc, s_setpc,
s_call and s_rfe forget everything. Shaders with branches are covered;
only the four sampler SGPRs below vcc_lo are considered.

Shaders whose table lists no static samplers take exactly the old path.
Scalar ops the decoder does not name (e.g. SOP1 0x3a in waterfall loops) forgot every known SGPR, so literal sampler words set before such a loop were lost. An unnamed SOP1/SOP2/SOPK op only writes its sdst pair; the PC, relative-move and call opcodes (SOP1 0x20-0x22, 0x2a, 0x2e-0x31; SOPK 0x11, 0x16) still forget everything. SOPK compares, setreg and waitcnt write no SGPR.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:08 +00:00
github-bot closed this pull request 2026-11-26 06:19:08 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:49 UTC · original

Applied on main in 2b3a132 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/329#issuecomment-7164355159) Applied on main in 2b3a132 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

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