xshader: initial MODE from PGM_RSRC1, fixture scan for its FSSM location #309

Closed
QuentinDanblon wants to merge 3 commits from upstream-pr/309 into main
QuentinDanblon commented 2026-11-23 17:31:29 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-23T17:31:29Z.


Library / area: tombox-xshader (hlsl.rs, hlsl/mode.rs, tests/exec.rs), d3d12_xs/src/shader_xlat.rs (one log line per shader hash)

Exports changed (exports.txt rows): none

Source of the behaviour:

  • docs/BLOCKERS.md, "f16 output modifiers depend on the initial MODE" (gfx1036 measurement): an omod on an f16 VALU result acts only with MODE IEEE 0 and f16/f64 denormals flushed.
  • AMD RDNA2 ISA reference: MODE register fields (FP_ROUND [3:0], FP_DENORM [7:4], DX10_CLAMP [8], IEEE [9]) and round modes.
  • LLVM AMDGPU (AMDGPUUsage compute_pgm_rsrc1 table, SIDefines.h) and Mesa (MIT, sid.h S_00B848_*): PGM_RSRC1 field layout, register granules, and that the CP loads FLOAT_MODE / DX10_CLAMP / IEEE_MODE into MODE at wave launch. Facts only, no code taken.

What it does

The translator always assumed MODE 0xf0 at entry. This PR lets the initial MODE come from the stage's PGM_RSRC1, and reports when it does not have it.

  • mode::mode_from_pgm_rsrc1 maps FLOAT_MODE [19:12] to MODE [7:0], DX10_CLAMP [21] to MODE [8], IEEE_MODE [23] to MODE [9]. PRIV, DEBUG_MODE and the register counts are dropped.
  • reachable / analyze start from that MODE, and tb_hw1 (the MODE copy read by s_getreg and the f16 omod check) is initialised with it. The f32 and f16/f64 denormal paths follow the same value.
  • New entry point hlsl::translate_with_pgm_rsrc1(ex, feeds_ps, rsrc1) for a caller that has the value.
  • Every other entry point asks the container. The reader picks a location by the sub-version dword at FSSM byte 0x10 (as #257 does for COMPUTE_PGM_RSRC2), refuses Durango blobs and any location at or past the ISA. Its location table is empty: where PGM_RSRC1 sits in the FSSM container is not proven, so no container yields a value today and MODE 0xf0 stays the default. Behaviour on main does not change, except for the log line below.
  • Without PGM_RSRC1, a shader with an f16 VOP3 result carrying an omod gets a line in a new Translated.unknowns: Vec<String>, and d3d12_xs logs it once per shader hash (shader_xlat "UNKNOWN hash=...").

Finding the location: a fixture test, for whoever has fixtures

hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture, same gating and fixture walk as the #257 RSRC2 test (tombox_fixtures_shaders, analysis/shaders, TOMBOX_SHADER_DUMPS). For each fixture of any stage it counts the VGPRs and SGPRs the ISA names and the wave size, then lists every dword before the ISA preamble that fits as PGM_RSRC1:

  • VGPRS [5:0]: (VGPRS + 1) × granule covers the VGPRs used (granule 8 in wave32, 4 in wave64);
  • SGPRS [9:6]: 0 (GFX10+ rule) or (SGPRS + 1) × 8 covers the SGPRs used (older rule; the console compiler is not LLVM, so both are accepted);
  • PRIORITY, PRIV, DEBUG_MODE clear for every stage; BULKY, CDBG_USER, [28:27] clear for compute;
  • round bits [15:12] zero (round to nearest even, the D3D rule).

It prints the dword 4 bytes before #257's RSRC2 (0x12c for sub-version 5, 0x134 for sub-version 6; the hardware registers RSRC1/RSRC2 are consecutive), and per sub-version the offsets that fit every fixture. It asserts no location. It fails only if the reader reads Durango, if a table entry does not fit every fixture, or with "not proven" under two FSSM fixtures. I have no fixtures, so here it is ignored (forced: "not proven: 0 FSSM fixtures").

If someone with fixtures (the people who ran the #257 test) runs
cargo test -p tombox-xshader --release --lib pgm_rsrc1_candidates -- --nocapture
and 0x12c / 0x134 fit every fixture, PGM_RSRC1_AT can be filled in a separate PR.

Commit 3 (23b199c): one fixture run should settle the container layout

The scan now also looks for RSRC2 on every stage and probes two layouts, so a single run on real fixtures can decide where the registers sit. Printing only, no location is asserted, nothing goes into a production table.

  • Per stage: offsets that fit every fixture are now aggregated per (sub-version, stage) and per (sub-version, stage slot), not only per sub-version. An intersection across compute and graphics stages could be empty even if 0x134 is right for compute.
  • RSRC2 by user SGPR count: #289 reads the user SGPR count at byte 0xD6 and checks it against the binding table. COMPUTE_PGM_RSRC2 and SPI_SHADER_PGM_RSRC2_PS/VS/GS/HS carry the same count in USER_SGPR [5:1] (plus USER_SGPR_MSB, bit 27, for the graphics stages). The scan lists every dword before the ISA whose USER_SGPR equals byte 0xD6 (or 0xD6 + 8 for the merged slots) with the must-be-zero bits clear, per stage, and runs the RSRC1 filter on the dword just before it.
  • AMDHSA kernel descriptor hypothesis (LLVM AMDGPUUsage, Code Object V3 descriptor): if the container embeds it, RSRC2 at R gives RSRC1 at R-4, RSRC3 at R-8, GROUP_SEGMENT_FIXED_SIZE at R-52 (to compare with LDS_SIZE and the static LDS reach), and an entry offset at R-36 that should land on the ISA (0x4c for RSRC2 at 0x138 and the ISA at 0x150). With RSRC2 at 0x130 in sub-version 5 the descriptor would overlap #289's table at 0xE8..0x100; the scan flags it.
  • Code-region hypothesis (public notes of other projects, not verified): a u64 code offset and size at 0xC0/0xC8, relative to +0x10, with COMPUTE_PGM_RSRC2 as the first dword of the region followed by the group size. The scan compares the three dwords after R with the PSV0 numthreads.
  • Stage slots (#289, 0x18..0x58): the difference between the set slot's value and the ISA and each candidate is printed, to see whether the registers sit at a fixed position from the slot.
  • A "k of n" summary per (sub-version, stage, hypothesis, R) tells which layout holds.

Command for anyone with fixtures (TOMBOX_SHADER_DUMPS=<dir> if they are not under analysis/shaders):

cargo test --release -p tombox-xshader --lib hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture -- --exact --nocapture --include-ignored 2> rsrc_scan.txt

Tests: two new unit tests on synthetic containers (USER_SGPR match with MSB and +8, must-be-zero bits, LDS, per-stage aggregation keeping 0x138/0x134 for compute when a pixel shader has RSRC2 elsewhere; a full descriptor at 0x104, the 0x130 overlap, a code region with 64x1x1, a merged GS). Mutation: ignoring byte 0xD6 makes both fail. tombox-xshader lib on WARP: 122 passed, 0 failed, 4 ignored. Gate: 0 problems.

Limits: byte 0xD6 is read even when 0xE8 holds no table, where its meaning is not established. PS, GS and HS have no reserved RSRC2 bits on gfx10.3, so their filter rests on USER_SGPR and TRAP/EXCP. Whether a Vertex shader without a slot uses the VS or the GS registers (NGG) is a guess, printed per fixture.

Not covered, not claimed

  • The RSRC1 location in any container.
  • Only the f16 omod is reported as depending on the unknown MODE. f32 denormal handling also depends on it and is not reported.
  • A shader with few registers is weak evidence for the scan (many small dwords fit). The register count is a lower bound, so the filter is more permissive, never stricter.
  • Fields above bit 23 differ between SPI_SHADER_PGM_RSRC1_* stages and are only checked for compute.

For you

  • Conflicts with #257 (hlsl.rs, next to mode::analyze) and #306 (Translated gets unknowns here and gather_lod_blind there). Both are side-by-side additions. I can rebase on whichever lands first.
  • unknowns is generic; #306's gather_lod_blind is the same idea for one case. If you prefer one mechanism, I can fold #306's into unknowns, in whichever PR lands second.
  • Noticed while working near resinfo, not changed here: image_mimg_query in tests/exec.rs expects 3 array layers at level 1, while the gfx1036 measurement in docs/BLOCKERS.md ("image_get_resinfo array layers are not minified", max(1, layers >> (BASE_LEVEL + level))) gives 1. The test encodes the current unminified behaviour. I did not touch it; it moves when that OPEN item is done.

Tests

On ba84a79 (base f89e410), WARP and RTX 5090 (TOMBOX_EXEC_HW=1), Windows 11.

  • f16_omod_follows_initial_mode_from_pgm_rsrc1 (tests/exec.rs), 58 cases: v_add_f16 / v_mul_f16 with each omod and clamp under 7 PGM_RSRC1 values (f16 denormals kept or flushed, IEEE on or off, DX10_CLAMP, PRIV, DEBUG_MODE and register counts set, all zero), an s_setreg after a flushing or keeping RSRC1, v_add_f32 of a denormal under 4 FLOAT_MODE values, and the unknowns line present only without PGM_RSRC1. Passes on both.

  • hlsl::mode unit tests: RSRC1 to MODE mapping; the reader on synthetic sub-version 5/6 containers (table hit, unknown sub-version, location past the ISA, short blob, Durango refused, production table empty); the reachable set seeded by the initial MODE; rsrc1_scan_checks_register_granules_and_reserved_bits_on_synthetic_containers.

  • Mutations: initialising tb_hw1 with 0xf0 again makes 26 of the 58 cases fail; swapping the wave32/wave64 granules makes the scan unit test fail. Both restored.

  • Full tombox-xshader suite on WARP: 350 passed, 0 failed, 19 ignored.

  • pr_gate.py --base origin/main: 0 problems.

  • cargo test -p tombox-xshader passes for what is listed above

  • No game or console files and no binaries included

  • No fake success: nothing is read from an unproven location; the unknown MODE is logged

🤖 Generated with Claude Code

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6565053276 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/309). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-23T17:31:29Z. --- **Library / area:** `tombox-xshader` (`hlsl.rs`, `hlsl/mode.rs`, `tests/exec.rs`), `d3d12_xs/src/shader_xlat.rs` (one log line per shader hash) **Exports changed (exports.txt rows):** none **Source of the behaviour:** - `docs/BLOCKERS.md`, "f16 output modifiers depend on the initial MODE" (gfx1036 measurement): an omod on an f16 VALU result acts only with MODE IEEE 0 and f16/f64 denormals flushed. - AMD RDNA2 ISA reference: MODE register fields (FP_ROUND [3:0], FP_DENORM [7:4], DX10_CLAMP [8], IEEE [9]) and round modes. - LLVM AMDGPU (`AMDGPUUsage` compute_pgm_rsrc1 table, `SIDefines.h`) and Mesa (MIT, `sid.h` `S_00B848_*`): PGM_RSRC1 field layout, register granules, and that the CP loads FLOAT_MODE / DX10_CLAMP / IEEE_MODE into MODE at wave launch. Facts only, no code taken. ### What it does The translator always assumed MODE `0xf0` at entry. This PR lets the initial MODE come from the stage's PGM_RSRC1, and reports when it does not have it. - `mode::mode_from_pgm_rsrc1` maps FLOAT_MODE [19:12] to MODE [7:0], DX10_CLAMP [21] to MODE [8], IEEE_MODE [23] to MODE [9]. PRIV, DEBUG_MODE and the register counts are dropped. - `reachable` / `analyze` start from that MODE, and `tb_hw1` (the MODE copy read by `s_getreg` and the f16 omod check) is initialised with it. The f32 and f16/f64 denormal paths follow the same value. - New entry point `hlsl::translate_with_pgm_rsrc1(ex, feeds_ps, rsrc1)` for a caller that has the value. - Every other entry point asks the container. The reader picks a location by the sub-version dword at FSSM byte 0x10 (as #257 does for COMPUTE_PGM_RSRC2), refuses Durango blobs and any location at or past the ISA. **Its location table is empty**: where PGM_RSRC1 sits in the FSSM container is not proven, so no container yields a value today and MODE `0xf0` stays the default. Behaviour on main does not change, except for the log line below. - Without PGM_RSRC1, a shader with an f16 VOP3 result carrying an omod gets a line in a new `Translated.unknowns: Vec<String>`, and `d3d12_xs` logs it once per shader hash (`shader_xlat` "UNKNOWN hash=..."). ### Finding the location: a fixture test, for whoever has fixtures `hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture`, same gating and fixture walk as the #257 RSRC2 test (`tombox_fixtures_shaders`, `analysis/shaders`, `TOMBOX_SHADER_DUMPS`). For each fixture of any stage it counts the VGPRs and SGPRs the ISA names and the wave size, then lists every dword before the ISA preamble that fits as PGM_RSRC1: - VGPRS [5:0]: (VGPRS + 1) × granule covers the VGPRs used (granule 8 in wave32, 4 in wave64); - SGPRS [9:6]: 0 (GFX10+ rule) or (SGPRS + 1) × 8 covers the SGPRs used (older rule; the console compiler is not LLVM, so both are accepted); - PRIORITY, PRIV, DEBUG_MODE clear for every stage; BULKY, CDBG_USER, [28:27] clear for compute; - round bits [15:12] zero (round to nearest even, the D3D rule). It prints the dword 4 bytes before #257's RSRC2 (0x12c for sub-version 5, 0x134 for sub-version 6; the hardware registers RSRC1/RSRC2 are consecutive), and per sub-version the offsets that fit every fixture. It asserts no location. It fails only if the reader reads Durango, if a table entry does not fit every fixture, or with "not proven" under two FSSM fixtures. I have no fixtures, so here it is ignored (forced: "not proven: 0 FSSM fixtures"). If someone with fixtures (the people who ran the #257 test) runs `cargo test -p tombox-xshader --release --lib pgm_rsrc1_candidates -- --nocapture` and 0x12c / 0x134 fit every fixture, `PGM_RSRC1_AT` can be filled in a separate PR. ### Commit 3 (`23b199c`): one fixture run should settle the container layout The scan now also looks for RSRC2 on every stage and probes two layouts, so a single run on real fixtures can decide where the registers sit. Printing only, no location is asserted, nothing goes into a production table. - **Per stage**: offsets that fit every fixture are now aggregated per (sub-version, stage) and per (sub-version, stage slot), not only per sub-version. An intersection across compute and graphics stages could be empty even if 0x134 is right for compute. - **RSRC2 by user SGPR count**: #289 reads the user SGPR count at byte 0xD6 and checks it against the binding table. COMPUTE_PGM_RSRC2 and SPI_SHADER_PGM_RSRC2_PS/VS/GS/HS carry the same count in USER_SGPR [5:1] (plus USER_SGPR_MSB, bit 27, for the graphics stages). The scan lists every dword before the ISA whose USER_SGPR equals byte 0xD6 (or 0xD6 + 8 for the merged slots) with the must-be-zero bits clear, per stage, and runs the RSRC1 filter on the dword just before it. - **AMDHSA kernel descriptor hypothesis** (LLVM AMDGPUUsage, Code Object V3 descriptor): if the container embeds it, RSRC2 at R gives RSRC1 at R-4, RSRC3 at R-8, GROUP_SEGMENT_FIXED_SIZE at R-52 (to compare with LDS_SIZE and the static LDS reach), and an entry offset at R-36 that should land on the ISA (0x4c for RSRC2 at 0x138 and the ISA at 0x150). With RSRC2 at 0x130 in sub-version 5 the descriptor would overlap #289's table at 0xE8..0x100; the scan flags it. - **Code-region hypothesis** (public notes of other projects, not verified): a u64 code offset and size at 0xC0/0xC8, relative to +0x10, with COMPUTE_PGM_RSRC2 as the first dword of the region followed by the group size. The scan compares the three dwords after R with the PSV0 numthreads. - **Stage slots** (#289, 0x18..0x58): the difference between the set slot's value and the ISA and each candidate is printed, to see whether the registers sit at a fixed position from the slot. - A "k of n" summary per (sub-version, stage, hypothesis, R) tells which layout holds. Command for anyone with fixtures (`TOMBOX_SHADER_DUMPS=<dir>` if they are not under `analysis/shaders`): ``` cargo test --release -p tombox-xshader --lib hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture -- --exact --nocapture --include-ignored 2> rsrc_scan.txt ``` Tests: two new unit tests on synthetic containers (USER_SGPR match with MSB and +8, must-be-zero bits, LDS, per-stage aggregation keeping 0x138/0x134 for compute when a pixel shader has RSRC2 elsewhere; a full descriptor at 0x104, the 0x130 overlap, a code region with 64x1x1, a merged GS). Mutation: ignoring byte 0xD6 makes both fail. `tombox-xshader` lib on WARP: 122 passed, 0 failed, 4 ignored. Gate: 0 problems. Limits: byte 0xD6 is read even when 0xE8 holds no table, where its meaning is not established. PS, GS and HS have no reserved RSRC2 bits on gfx10.3, so their filter rests on USER_SGPR and TRAP/EXCP. Whether a Vertex shader without a slot uses the VS or the GS registers (NGG) is a guess, printed per fixture. ### Not covered, not claimed - The RSRC1 location in any container. - Only the f16 omod is reported as depending on the unknown MODE. f32 denormal handling also depends on it and is not reported. - A shader with few registers is weak evidence for the scan (many small dwords fit). The register count is a lower bound, so the filter is more permissive, never stricter. - Fields above bit 23 differ between `SPI_SHADER_PGM_RSRC1_*` stages and are only checked for compute. ### For you - Conflicts with #257 (`hlsl.rs`, next to `mode::analyze`) and #306 (`Translated` gets `unknowns` here and `gather_lod_blind` there). Both are side-by-side additions. I can rebase on whichever lands first. - `unknowns` is generic; #306's `gather_lod_blind` is the same idea for one case. If you prefer one mechanism, I can fold #306's into `unknowns`, in whichever PR lands second. - Noticed while working near resinfo, not changed here: `image_mimg_query` in `tests/exec.rs` expects 3 array layers at level 1, while the gfx1036 measurement in `docs/BLOCKERS.md` ("image_get_resinfo array layers are not minified", `max(1, layers >> (BASE_LEVEL + level))`) gives 1. The test encodes the current unminified behaviour. I did not touch it; it moves when that OPEN item is done. ### Tests On `ba84a79` (base `f89e410`), WARP and RTX 5090 (`TOMBOX_EXEC_HW=1`), Windows 11. - `f16_omod_follows_initial_mode_from_pgm_rsrc1` (`tests/exec.rs`), 58 cases: `v_add_f16` / `v_mul_f16` with each omod and clamp under 7 PGM_RSRC1 values (f16 denormals kept or flushed, IEEE on or off, DX10_CLAMP, PRIV, DEBUG_MODE and register counts set, all zero), an `s_setreg` after a flushing or keeping RSRC1, `v_add_f32` of a denormal under 4 FLOAT_MODE values, and the `unknowns` line present only without PGM_RSRC1. Passes on both. - `hlsl::mode` unit tests: RSRC1 to MODE mapping; the reader on synthetic sub-version 5/6 containers (table hit, unknown sub-version, location past the ISA, short blob, Durango refused, production table empty); the reachable set seeded by the initial MODE; `rsrc1_scan_checks_register_granules_and_reserved_bits_on_synthetic_containers`. - Mutations: initialising `tb_hw1` with `0xf0` again makes 26 of the 58 cases fail; swapping the wave32/wave64 granules makes the scan unit test fail. Both restored. - Full `tombox-xshader` suite on WARP: 350 passed, 0 failed, 19 ignored. - `pr_gate.py --base origin/main`: 0 problems. - [x] `cargo test -p tombox-xshader` passes for what is listed above - [x] No game or console files and no binaries included - [x] No fake success: nothing is read from an unproven location; the unknown MODE is logged 🤖 Generated with [Claude Code](https://claude.com/claude-code)
QuentinDanblon commented 2026-11-23 18:13:32 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-23T18:13:32Z.


@GDAgent, a request since you have the fixtures (and ran the #257 test): could you run the scan on this branch (23b199c) and attach or paste the summary at the end of the output?

cargo test --release -p tombox-xshader --lib hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture -- --exact --nocapture --include-ignored 2> rsrc_scan.txt

(TOMBOX_SHADER_DUMPS=<dir> if they are not under analysis/shaders.) It needs neither #289 nor #257. It uses the user SGPR byte at 0xD6 from #289 as a second, independent key for RSRC2 on every stage, and checks two layouts (AMDHSA kernel descriptor vs code region at 0xC0/0xC8). One run should tell where PGM_RSRC1 sits, whether 0x130 holds in sub-version 5, and where the graphics-stage RSRC2 is, which BLOCKERS lists as not checked. The only things it prints are offsets and decoded register fields.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7109844457 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/309#issuecomment-7109844457). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-23T18:13:32Z. --- @GDAgent, a request since you have the fixtures (and ran the #257 test): could you run the scan on this branch (`23b199c`) and attach or paste the summary at the end of the output? ``` cargo test --release -p tombox-xshader --lib hlsl::mode::tests::pgm_rsrc1_candidates_in_every_fssm_fixture -- --exact --nocapture --include-ignored 2> rsrc_scan.txt ``` (`TOMBOX_SHADER_DUMPS=<dir>` if they are not under `analysis/shaders`.) It needs neither #289 nor #257. It uses the user SGPR byte at 0xD6 from #289 as a second, independent key for RSRC2 on every stage, and checks two layouts (AMDHSA kernel descriptor vs code region at 0xC0/0xC8). One run should tell where PGM_RSRC1 sits, whether 0x130 holds in sub-version 5, and where the graphics-stage RSRC2 is, which BLOCKERS lists as not checked. The only things it prints are offsets and decoded register fields.
omonkeyz commented 2026-11-25 13:49:14 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:49:14Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7164337279 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/309#issuecomment-7164337279). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:49:14Z. --- Applied on main in 4c7c544 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

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
omonkeyz/xbox-emulator!309
No description provided.