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

Closed
JRamos1993 wants to merge 1 commit from upstream-pr/718 into main
JRamos1993 commented 2026-11-25 05:28:42 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JRamos1993 on 2026-11-25T05:28:42Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6597888510 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/718). Originally posted by [JRamos1993](https://github.com/JRamos1993) on 2026-11-25T05:28:42Z. --- 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.
upstream-sync closed this pull request 2026-11-25 07:06:00 +00:00
JRamos1993 commented 2026-11-25 07:06:14 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JRamos1993 on 2026-11-25T06:08:44Z.


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.
<!-- greyfinch-sync:fxceless/victory-fork:comment:7154488651 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/718#issuecomment-7154488651). Originally posted by [JRamos1993](https://github.com/JRamos1993) on 2026-11-25T06:08:44Z. --- **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

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
fxceless/victory-fork!718
No description provided.