Shaders: a sampler chosen by control flow between two immediate samplers (40 programs) #718

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

@⁠JRamos1993 on GitHub · 2026-11-25 05:28 UTC · original
JRamos1993:sampler-chosen-by-control-flow → main · +307 −3 · 4 files · 1 commits · closed without merging

Part of #279 (its "a descriptor chosen by control flow" row).

Problem

40 programs of the retail shader pack (37 pixel, 3 mesh) stop with emit: a descriptor chosen by control flow, so their draws are dropped.

In all 40, the sampler is a phi of two immediate samplers: each side of a branch sets the four sampler dwords in SGPRs, and the two sets differ only in dword 0, the address modes. descriptor_phi only takes heap-index sources (Loaded, or a nested Phi), so a Constant argument stops it.

Two direct fixes do not work:

  • A phi (or select) of the two static-sampler handles. The validator refuses it: "Resource handle should returned by createHandle".
  • The two samplers as one two-register range, createHandleFromBinding indexed by a phi of the registers. It validates, but D3D12 refuses the pipeline when the range is covered by two separate static samplers ("The parameter is incorrect").
    • I checked this outside the translator: a DXC-compiled SamplerState s[2] : register(s0) indexed dynamically, with two static samplers at s0 and s1, is refused.
    • The same two samplers declared separately make a pipeline.

What

The sample is made through each of the two static samplers, and each result channel is picked by a bit made at the join.

  • lower:
    • immediate_sampler_pair gives the two handles, plus an i1 phi at the join that is set on the edges that chose the second sampler.
    • In image, the sample op is emitted as before through the first handle, then once more through the second (Module::call_with). Each extracted channel is select(bit, second, first).
    • calculateLOD (op 81) does the same.
    • A phi whose immediate samplers are all the same is that one sampler.
    • Anything else (three or more samplers, mixed sources) still stops as before.
  • bitcode: Module::call_with, a copy of a call with one argument replaced.
  • Test: a_sampler_chosen_by_control_flow_samples_through_the_chosen_one, a synthetic pixel program:
    • It loads a 2×1 texture (red, green) from a table.
    • A branch on a root constant picks a wrapping or a clamping point sampler (set in SGPRs on each side).
    • It samples at u = 1.25. The draw must read red with the wrapping sampler and green with the clamping one, on hardware and on WARP.

Tested

RTX 5090, driver 621.24, Windows 11, main 310c6eb.

  • Retail pack, victory translate --validate --list:
    • 25,382 → 25,422 validated: the 40 programs (37 Ps, 3 Ms). None lost.
    • With a local DXIL hash on each validated line, all 25,382 programs that validated before emit byte-identical DXIL. Only the 40 are new.
  • Pipelines: --pipelines on the 40 creates 40 pipelines, 0 refused.
  • The new test: passes on hardware and WARP. It fails in both cases it should:
    • with the change reverted: emission stops with "a descriptor chosen by control flow", as the title's 40 do;
    • with the select swapped: it reads the other texel.
  • Checks:
    • cargo test -p victory-shader --locked --no-fail-fast -- --test-threads=4: 215 passed, 0 failed, 3 ignored.
    • cargo clippy -p victory-shader --all-targets --locked -- -D warnings, with and without --all-features: clean.
    • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4: 511 passed, 0 failed, 9 ignored.

Not tested

  • In game: the scene my save reaches does not use these 40 programs; none appears in the "does not translate" lines of my runs. So their look on screen is not checked.
  • Performance: each such sample is made twice. This only happens in these 40 programs.

AI use

Claude Code with Claude Opus 5.5 found the failure class on the retail pack, wrote the change and the test, ran the checks above and wrote this. The test uses a synthetic program. The title's shaders were only read on my machine, and nothing from them is in this PR.

**@⁠JRamos1993** on GitHub · 2026-11-25 05:28 UTC · [original](https://github.com/fxceless/victory-fork/pull/718) `JRamos1993:sampler-chosen-by-control-flow` → `main` · +307 −3 · 4 files · 1 commits · closed without merging Part of #279 (its "a descriptor chosen by control flow" row). ## Problem 40 programs of the retail shader pack (37 pixel, 3 mesh) stop with `emit: a descriptor chosen by control flow`, so their draws are dropped. In all 40, the sampler is a phi of two **immediate samplers**: each side of a branch sets the four sampler dwords in SGPRs, and the two sets differ only in dword 0, the address modes. `descriptor_phi` only takes heap-index sources (`Loaded`, or a nested `Phi`), so a `Constant` argument stops it. Two direct fixes do not work: - **A phi (or select) of the two static-sampler handles.** The validator refuses it: "Resource handle should returned by createHandle". - **The two samplers as one two-register range, `createHandleFromBinding` indexed by a phi of the registers.** It validates, but D3D12 refuses the pipeline when the range is covered by two separate static samplers ("The parameter is incorrect"). - I checked this outside the translator: a DXC-compiled `SamplerState s[2] : register(s0)` indexed dynamically, with two static samplers at `s0` and `s1`, is refused. - The same two samplers declared separately make a pipeline. ## What The sample is made through each of the two static samplers, and each result channel is picked by a bit made at the join. - **`lower`:** - `immediate_sampler_pair` gives the two handles, plus an `i1` phi at the join that is set on the edges that chose the second sampler. - In `image`, the sample op is emitted as before through the first handle, then once more through the second (`Module::call_with`). Each extracted channel is `select(bit, second, first)`. - `calculateLOD` (op 81) does the same. - A phi whose immediate samplers are all the same is that one sampler. - Anything else (three or more samplers, mixed sources) still stops as before. - **`bitcode`:** `Module::call_with`, a copy of a call with one argument replaced. - **Test:** `a_sampler_chosen_by_control_flow_samples_through_the_chosen_one`, a synthetic pixel program: - It loads a 2×1 texture (red, green) from a table. - A branch on a root constant picks a wrapping or a clamping point sampler (set in SGPRs on each side). - It samples at u = 1.25. The draw must read red with the wrapping sampler and green with the clamping one, on hardware and on WARP. ## Tested RTX 5090, driver 621.24, Windows 11, `main` `310c6eb`. - **Retail pack**, `victory translate --validate --list`: - 25,382 → **25,422 validated**: the 40 programs (37 Ps, 3 Ms). None lost. - With a local DXIL hash on each validated line, **all 25,382 programs that validated before emit byte-identical DXIL**. Only the 40 are new. - **Pipelines:** `--pipelines` on the 40 creates **40 pipelines**, 0 refused. - **The new test:** passes on hardware and WARP. It fails in both cases it should: - with the change reverted: emission stops with "a descriptor chosen by control flow", as the title's 40 do; - with the select swapped: it reads the other texel. - **Checks:** - `cargo test -p victory-shader --locked --no-fail-fast -- --test-threads=4`: 215 passed, 0 failed, 3 ignored. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4`: 511 passed, 0 failed, 9 ignored. ## Not tested - **In game:** the scene my save reaches does not use these 40 programs; none appears in the "does not translate" lines of my runs. So their look on screen is not checked. - **Performance:** each such sample is made twice. This only happens in these 40 programs. ## AI use Claude Code with Claude Opus 5.5 found the failure class on the retail pack, wrote the change and the test, ran the checks above and wrote this. The test uses a synthetic program. The title's shaders were only read on my machine, and nothing from them is in this PR.
The title's programs pick an address mode by control flow: one of two
immediate samplers, set in SGPRs on each side of a branch, reaches the
sample through a phi. `descriptor_phi` takes heap-index sources only, so
these programs stopped with "a descriptor chosen by control flow" (37 pixel
and 3 mesh programs of the retail pack).

A handle cannot be chosen by control flow (the validator: "Resource handle
should returned by createHandle"), and a shader sampler range cannot span
two static samplers (D3D12 refuses the pipeline). So the sample is made
through each of the two static samplers and each result channel picked by a
bit made at the join. A phi of one immediate sampler is that sampler.

- lower: `immediate_sampler_pair` gives the two handles and the bit;
  `image` emits the sample op once more through the second handle
  (`Module::call_with`) and selects per channel; `calculateLOD` likewise.
- bitcode: `Module::call_with`, a copy of a call with one argument replaced.
- A draw test: a branch on a root constant picks a wrapping or a clamping
  point sampler for a sample at u = 1.25 of a red and a green texel; the
  draw reads the texel of the chosen one, on hardware and WARP.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:49:36 +00:00
github-bot closed this pull request 2026-11-26 03:49:36 +00:00
Author
Owner

@⁠JRamos1993 on GitHub · 2026-11-25 06:08 UTC · original

Closing as a duplicate of #25, which I missed when I checked for existing work (I searched issues, not pull requests). Sorry for the noise.

Same 40 programs: measured on the retail pack against main 310c6eb (RTX 5090, victory translate --validate --list, local DXIL hash on each validated line):

  • #25 merged onto main validates the same 40 programs this PR does: 25,382 → 25,422.
  • It loses none, and every other program's DXIL is byte-identical to main's, as with this PR.

#25 is the better fix:

  • It branches to one sample through the chosen sampler, where this PR samples through both and selects.
  • It handles more than two samplers and nested phis.

Possibly useful for #25:

  • The two dead ends are confirmed on this machine: a phi of the two handles is refused by the validator, and a two-register range over two separate static samplers is refused by D3D12 at pipeline creation. I checked the latter with a DXC-compiled SamplerState s[2] indexed dynamically.
  • This PR's draw test (wrap vs clamp picked by a branch on a root constant) is in crates/victory-shader/src/emit/sampler_choice_tests.rs on JRamos1993:sampler-chosen-by-control-flow, if a second test is wanted.
**@⁠JRamos1993** on GitHub · 2026-11-25 06:08 UTC · [original](https://github.com/fxceless/victory-fork/pull/718#issuecomment-7154488651) **Closing as a duplicate of #25, which I missed when I checked for existing work** (I searched issues, not pull requests). Sorry for the noise. **Same 40 programs:** measured on the retail pack against `main` `310c6eb` (RTX 5090, `victory translate --validate --list`, local DXIL hash on each validated line): - #25 merged onto `main` validates **the same 40 programs** this PR does: 25,382 → 25,422. - It loses none, and every other program's DXIL is byte-identical to `main`'s, as with this PR. **#25 is the better fix:** - It branches to one sample through the chosen sampler, where this PR samples through both and selects. - It handles more than two samplers and nested phis. **Possibly useful for #25:** - The two dead ends are confirmed on this machine: a phi of the two handles is refused by the validator, and a two-register range over two separate static samplers is refused by D3D12 at pipeline creation. I checked the latter with a DXC-compiled `SamplerState s[2]` indexed dynamically. - This PR's draw test (wrap vs clamp picked by a branch on a root constant) is in `crates/victory-shader/src/emit/sampler_choice_tests.rs` on `JRamos1993:sampler-chosen-by-control-flow`, if a second test is wanted.

Pull request closed

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