Shaders: immediate samplers chosen by control flow #1008

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

@⁠ThiagoAlexsander on GitHub · 2026-11-24 14:56 UTC · original
ThiagoAlexsander:shader/sampler-choice → main · +337 −68 · 3 files · 1 commits · closed without merging

A uniform branch that leaves one of two constant sampler descriptors in the same SGPRs was refused with a descriptor chosen by control flow: the join (Source::Phi in handle) only took heap loads. In the title's programs the two choices differ only in their address modes (wrap, or clamp to the last texel), for example in PS_TexturedDeferred, PS_TexturedLit_8 and the terrain mesh programs MST_DS_TerrainTransform.

Change

  • Static samplers are one register each, and D3D12 refuses a pipeline whose shader declares one sampler range over several of them (E_INVALIDARG on the hardware device and on WARP, though dxil.dll accepts the module). So the join cannot index them as an HLSL sampler array would.
  • New sampler_choice: for a sampler phi whose every edge is a constant, one static-sampler handle per distinct choice, and the choice's position as an i32 phi at the join. It is uniform, as the edge is.
  • The sampling instruction (sample, sampleBias, sampleLevel, sampleGrad, sampleCmp*, textureGather, calculateLOD) runs once per handle, and the new picked selects each channel by that position. With one handle nothing changes: the match arms now name their op and it is emitted after the match, with the same arguments.
  • A phi mixing constants and heap loads is still refused, as before.
  • The existing constant-sampler code moved into immediate_sampler, unchanged.

Results

victory translate --validate over the title's files (main ffe310e against this branch, Windows SDK dxil.dll 10.0.26100), compared program by program:

main this branch
vertex/pixel validated 12,167 12,203
mesh validated 8,152 8,155
compute validated 1,417 1,417
a descriptor chosen by control flow 41 1

39 programs gain (36 pixel, 3 mesh), and none lose. One PS_PuddleDetect variant now stops later, at a cross-lane operation outside compute (#599 territory). The remaining a descriptor chosen by control flow is a compute program whose heap-loaded descriptor goes through two nested joins, a separate case.

Testing

  • New emit::sampler_tests: a hand-written wave32 compute program whose group id picks between the two samplers with a uniform branch, then samples a 2×1 texture past its right edge. It is lowered, validated by dxil.dll, and run on the hardware device (RTX 5080) and WARP. Group 0 reads the first texel (wrap) and group 1 the last (clamp). On main both tests fail at lowering.
  • cargo test --workspace --locked: 344 passed, 0 failed, 6 ignored (342 on main plus the two new tests).
  • cargo clippy --workspace --all-targets --locked -- -D warnings, with and without --all-features: clean. rustfmt --edition 2024 --check: no new hunks (the three in lower.rs's tests and one in mod.rs are on main too). cargo xtask policy: 155 files ok.
  • Not run in the title: the in-game pipelines of the 39 programs were not checked. Their handles are the single-register static samplers every immediate sampler already uses, so the root signatures they need are unchanged.

No game data is in the test or the change.

AI use: Claude Code (Claude Opus 5.5) found the case with the census, wrote the change and the tests, and ran everything above. I reviewed the diff and the results.

🤖 Generated with Claude Code

**@⁠ThiagoAlexsander** on GitHub · 2026-11-24 14:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/1008) `ThiagoAlexsander:shader/sampler-choice` → `main` · +337 −68 · 3 files · 1 commits · closed without merging A uniform branch that leaves one of two constant sampler descriptors in the same SGPRs was refused with `a descriptor chosen by control flow`: the join (`Source::Phi` in `handle`) only took heap loads. In the title's programs the two choices differ only in their address modes (wrap, or clamp to the last texel), for example in `PS_TexturedDeferred`, `PS_TexturedLit_8` and the terrain mesh programs `MST_DS_TerrainTransform`. ## Change - Static samplers are one register each, and D3D12 refuses a pipeline whose shader declares one sampler range over several of them (`E_INVALIDARG` on the hardware device and on WARP, though `dxil.dll` accepts the module). So the join cannot index them as an HLSL sampler array would. - New `sampler_choice`: for a sampler phi whose every edge is a constant, one static-sampler handle per distinct choice, and the choice's position as an `i32` phi at the join. It is uniform, as the edge is. - The sampling instruction (`sample`, `sampleBias`, `sampleLevel`, `sampleGrad`, `sampleCmp*`, `textureGather`, `calculateLOD`) runs once per handle, and the new `picked` selects each channel by that position. With one handle nothing changes: the match arms now name their op and it is emitted after the match, with the same arguments. - A phi mixing constants and heap loads is still refused, as before. - The existing constant-sampler code moved into `immediate_sampler`, unchanged. ## Results `victory translate --validate` over the title's files (`main` `ffe310e` against this branch, Windows SDK `dxil.dll` 10.0.26100), compared program by program: | | `main` | this branch | |---|---|---| | vertex/pixel validated | 12,167 | 12,203 | | mesh validated | 8,152 | 8,155 | | compute validated | 1,417 | 1,417 | | `a descriptor chosen by control flow` | 41 | 1 | 39 programs gain (36 pixel, 3 mesh), and none lose. One `PS_PuddleDetect` variant now stops later, at `a cross-lane operation outside compute` (#599 territory). The remaining `a descriptor chosen by control flow` is a compute program whose heap-loaded descriptor goes through two nested joins, a separate case. ## Testing - New `emit::sampler_tests`: a hand-written wave32 compute program whose group id picks between the two samplers with a uniform branch, then samples a 2×1 texture past its right edge. It is lowered, validated by `dxil.dll`, and run on the hardware device (RTX 5080) and WARP. Group 0 reads the first texel (wrap) and group 1 the last (clamp). On `main` both tests fail at lowering. - `cargo test --workspace --locked`: 344 passed, 0 failed, 6 ignored (342 on `main` plus the two new tests). - `cargo clippy --workspace --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. `rustfmt --edition 2024 --check`: no new hunks (the three in `lower.rs`'s tests and one in `mod.rs` are on `main` too). `cargo xtask policy`: 155 files ok. - Not run in the title: the in-game pipelines of the 39 programs were not checked. Their handles are the single-register static samplers every immediate sampler already uses, so the root signatures they need are unchanged. No game data is in the test or the change. AI use: Claude Code (Claude Opus 5.5) found the case with the census, wrote the change and the tests, and ran everything above. I reviewed the diff and the results. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A uniform branch that leaves one of two constant sampler descriptors in
the same SGPRs (the title's programs pick wrap or clamp this way) was
refused with "a descriptor chosen by control flow": only heap loads were
joined. Static samplers are one register each, and D3D12 refuses a shader
sampler range spread over several, so the join cannot index them. The
sampling instruction now runs once per choice, each with its own static
sampler, and the choice's position, a phi at the join, picks the result.
The choice is uniform, so each lane gets what the console computes; the
cost is one extra fetch per choice.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:50 +00:00
github-bot closed this pull request 2026-11-26 04:16:50 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:12 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 7101c98: cargo test -p victory-shader passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1008#issuecomment-7135645040) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 7101c98: `cargo test -p victory-shader` passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:30 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

An immediate sampler picked by a uniform branch is in main (emit/lower.rs handle(), Source::Phi of two constants: samples with both and selects by the edge taken; sampler_choices).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1008#issuecomment-7148572641) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. An immediate sampler picked by a uniform branch is in main (emit/lower.rs handle(), Source::Phi of two constants: samples with both and selects by the edge taken; sampler_choices). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1008
No description provided.