xshader: resolve RDNA2 literal static samplers #183

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

@⁠shdann on GitHub · 2026-11-22 20:24 UTC · original
shdann:codex/rdna2-static-sampler → main · +98 −7 · 3 files · 1 commits · closed without merging

Library / area: tombox-xshader, production RDNA2 texture sampling

Exports changed: None

Source of the behaviour: A hand-written RDNA2 pixel shader with a literal four-SGPR sampler descriptor and synthetic root-signature metadata. The implementation reuses TomBox's existing sampler descriptor decoder and static-sampler selection rules. Leonida Vulkan was used as an independent rendering reference.

Change

The production translator rejects the literal sampler in this fixture with s16 not a sampler descriptor. For straight-line RDNA2 shaders, prove all four descriptor words constant and resolve the binding against the shader-visible static samplers in the root signature. An incompatible or unproven binding still fails. Account for overlapping multi-SGPR scalar loads when proving a word constant. The existing Durango selection path is preserved.

This change stands on current main 27122f7 by itself. The commit contains the resolver and synthetic Rust tests.

Verification

Windows 11 x64, NVIDIA GeForce RTX 4090, driver 621.01. A production d3d12_xs build from commit c4c97e7 translated both synthetic shaders with DXC, created the graphics PSO through the production Xbox entry point, and submitted the 64×64 offscreen draw without a test bridge. Leonida Vulkan f847748c used the same pixel shader ISA and equivalent texture and sampler inputs. Vulkan validation reported zero errors.

Comparison Bit-identical RGBA32F pixels Max absolute error
TomBox DX12 vs Leonida Vulkan 4,096 / 4,096 0
TomBox DX12 vs CPU reference 4,096 / 4,096 0
Leonida Vulkan vs CPU reference 4,096 / 4,096 0

The pipeline rejected both an incompatible sampler and a root signature that bound the texture at the wrong register. Neither negative control produced a readback.

  • cargo build --locked -p d3d12_xs: passed.

  • cargo check --locked -p tombox-xshader --all-targets: passed.

  • cargo test --locked -p tombox-xshader --lib --test synthetic: 110 passed, 2 ignored. The new cases failed before their respective fixes.

  • cargo test --locked -p tombox-xshader --test exec image_mimg_sample: passed on WARP.

  • PR gate and git diff --check: passed.

  • No game or console files and no binaries included.

  • Incompatible and unproven bindings remain errors.

AI-assisted with Codex.

**@⁠shdann** on GitHub · 2026-11-22 20:24 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183) `shdann:codex/rdna2-static-sampler` → `main` · +98 −7 · 3 files · 1 commits · closed without merging **Library / area:** `tombox-xshader`, production RDNA2 texture sampling **Exports changed:** None **Source of the behaviour:** A hand-written RDNA2 pixel shader with a literal four-SGPR sampler descriptor and synthetic root-signature metadata. The implementation reuses TomBox's existing sampler descriptor decoder and static-sampler selection rules. Leonida Vulkan was used as an independent rendering reference. ### Change The production translator rejects the literal sampler in this fixture with `s16 not a sampler descriptor`. For straight-line RDNA2 shaders, prove all four descriptor words constant and resolve the binding against the shader-visible static samplers in the root signature. An incompatible or unproven binding still fails. Account for overlapping multi-SGPR scalar loads when proving a word constant. The existing Durango selection path is preserved. This change stands on current main `27122f7` by itself. The commit contains the resolver and synthetic Rust tests. ### Verification Windows 11 x64, NVIDIA GeForce RTX 4090, driver **621.01**. A production `d3d12_xs` build from commit `c4c97e7` translated both synthetic shaders with DXC, created the graphics PSO through the production Xbox entry point, and submitted the 64×64 offscreen draw without a test bridge. Leonida Vulkan `f847748c` used the same pixel shader ISA and equivalent texture and sampler inputs. Vulkan validation reported zero errors. | Comparison | Bit-identical RGBA32F pixels | Max absolute error | | --- | ---: | ---: | | TomBox DX12 vs Leonida Vulkan | **4,096 / 4,096** | **0** | | TomBox DX12 vs CPU reference | 4,096 / 4,096 | 0 | | Leonida Vulkan vs CPU reference | 4,096 / 4,096 | 0 | The pipeline rejected both an incompatible sampler and a root signature that bound the texture at the wrong register. Neither negative control produced a readback. - `cargo build --locked -p d3d12_xs`: passed. - `cargo check --locked -p tombox-xshader --all-targets`: passed. - `cargo test --locked -p tombox-xshader --lib --test synthetic`: 110 passed, 2 ignored. The new cases failed before their respective fixes. - `cargo test --locked -p tombox-xshader --test exec image_mimg_sample`: passed on WARP. - PR gate and `git diff --check`: passed. - [x] No game or console files and no binaries included. - [x] Incompatible and unproven bindings remain errors. AI-assisted with Codex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:05 +00:00
github-bot closed this pull request 2026-11-26 06:13:05 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 20:25 UTC · original · edited 2026-11-22 20:32 UTC

Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T20:32:13.318886Z c4c97e7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review" or "@⁠codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 20:25 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#issuecomment-7085238831) · edited 2026-11-22 20:32 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"c4c97e78c8e35a8f42a1407580cba8ebf6ba60bc","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-22T20:32:13.318886Z">2026-11-22T20:32:13.318886Z</relative-time> | `c4c97e7` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review" or "@⁠codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 22:43 UTC · original

Thanks. It passes our tests, and I'd also prepared a small fix so the mismatch error names all four descriptor words. But I'm holding it, because the automated review on this PR raised three points that conflict with the lead's condition: the sampler has to be proven constant, and anything that doesn't match must be refused.

  1. Constant proof: sgpr_const_in checks only Insn.dst, so a VOP3B carry-out SGPR destination that overwrites a sampler word isn't seen. Please include those destinations, and add a test where one overwrites the word.
  2. Filter: pick_static_sampler scores filter mode softly, so a point literal can bind a linear static sampler. For a literal sampler, any known field that differs (filter, address, compare, reduction) should refuse.
  3. Border colour: word 3 is proven constant but then discarded. With border addressing, its border-colour bits (d[3] >> 30) must match the static sampler's border colour, or the shader is refused.

When refusing, please name the four words in the error, e.g. literal sampler s16 words [..] matches no static sampler: why. Please also replace ss > 102 with a named check that the four SGPRs stay below vcc_lo.

**@⁠omonkeyz** on GitHub · 2026-11-22 22:43 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#issuecomment-7087035684) Thanks. It passes our tests, and I'd also prepared a small fix so the mismatch error names all four descriptor words. But I'm holding it, because the automated review on this PR raised three points that conflict with the lead's condition: the sampler has to be proven constant, and anything that doesn't match must be refused. 1. **Constant proof:** `sgpr_const_in` checks only `Insn.dst`, so a VOP3B carry-out SGPR destination that overwrites a sampler word isn't seen. Please include those destinations, and add a test where one overwrites the word. 2. **Filter:** `pick_static_sampler` scores filter mode softly, so a point literal can bind a linear static sampler. For a literal sampler, any known field that differs (filter, address, compare, reduction) should refuse. 3. **Border colour:** word 3 is proven constant but then discarded. With border addressing, its border-colour bits (`d[3] >> 30`) must match the static sampler's border colour, or the shader is refused. When refusing, please name the four words in the error, e.g. `literal sampler s16 words [..] matches no static sampler: why`. Please also replace `ss > 102` with a named check that the four SGPRs stay below vcc_lo.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:08 UTC · original

Applied locally on main with your authorship kept (cherry-picked, 4f61662); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#issuecomment-7103541965) Applied locally on main with your authorship kept (cherry-picked, 4f61662); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

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

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

**@⁠omonkeyz** on GitHub · 2026-11-25 13:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#issuecomment-7163356303) Applied on main in 4f61662 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.
@ -163,2 +160,4 @@
};
let cands: Vec<_> = rs.static_samplers.iter().filter(|s| s.visibility == 0 || s.visibility == vis).collect();
if self.dg().is_none() {
if cands.is_empty() || ss > 102 || self.insns.iter().any(|ins| {
Author
Owner

@⁠Copilot on GitHub · 2026-11-22 20:31 UTC · original

The ss > 102 guard is a magic number and the intent isn’t obvious to future readers. It looks like you’re ensuring a 4-dword sampler descriptor (ss..ss+3) stays within normal SGPRs (i.e., doesn’t overlap vcc_lo/vcc_hi), but that assumption should be expressed directly (and ideally named/commented).

**@⁠Copilot** on GitHub · 2026-11-22 20:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#discussion_r4659898678) The `ss > 102` guard is a magic number and the intent isn’t obvious to future readers. It looks like you’re ensuring a 4-dword sampler descriptor (ss..ss+3) stays within normal SGPRs (i.e., doesn’t overlap vcc_lo/vcc_hi), but that assumption should be expressed directly (and ideally named/commented).
@ -165,0 +167,4 @@
}) || self.insns[..idx].iter().any(|ins| ins.mnemonic.starts_with("s_endpgm")) {
return Ok(None);
}
let words = [0, 1, 2, 3].map(|word| Self::sgpr_const_in(&self.insns, 0, idx, (ss + word) as u8));
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 20:32 UTC · original

P2 Badge Account for VOP3B scalar destinations in the constant proof

When a VOP3B instruction overwrites a sampler SGPR after its literal initialization, this proof can still accept the old value. sgpr_const_in only inspects Insn.dst, but VOP3B carry-out destinations are absent from that list and are extracted from the raw instruction during emission. Inserting v_add_co_u32 v0, s16, v2, v3 immediately before the new fixture's sample still resolves the original clamp sampler, even though the emitted operation overwrites s16:s17. Account for these scalar side destinations, or reject such cases, before treating the descriptor as constant.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 20:32 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#discussion_r4659899909) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Account for VOP3B scalar destinations in the constant proof** When a VOP3B instruction overwrites a sampler SGPR after its literal initialization, this proof can still accept the old value. `sgpr_const_in` only inspects `Insn.dst`, but VOP3B carry-out destinations are absent from that list and are extracted from the raw instruction during emission. Inserting `v_add_co_u32 v0, s16, v2, v3` immediately before the new fixture's sample still resolves the original clamp sampler, even though the emitted operation overwrites s16:s17. Account for these scalar side destinations, or reject such cases, before treating the descriptor as constant. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
@ -165,0 +173,4 @@
};
let decoded = decode_ssharp(w0, Some(w1), Some(w2), is_compare_sample(&self.insns[idx].mnemonic));
let owned: Vec<StaticSampler> = cands.iter().map(|s| **s).collect();
let pick = pick_static_sampler(&decoded, &owned)
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 20:32 UTC · original

P2 Badge Reject static samplers with a mismatching known filter

For a literal point sampler whose root signature contains only a linear sampler with matching address modes and reduction, this selects the incompatible binding. pick_static_sampler treats filtering as a soft score and immediately accepts a sole hard match. Changing only the new fixture's root sampler filter from 0 to 0x15 therefore still makes translate succeed, although literal word 2 is zero and specifies point filtering. The generated ordinary image_sample silently uses linear filtering instead. Require the known RDNA2 filter state to match rather than accepting the existing selector's approximate match.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 20:32 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#discussion_r4659899914) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Reject static samplers with a mismatching known filter** For a literal point sampler whose root signature contains only a linear sampler with matching address modes and reduction, this selects the incompatible binding. `pick_static_sampler` treats filtering as a soft score and immediately accepts a sole hard match. Changing only the new fixture's root sampler `filter` from `0` to `0x15` therefore still makes `translate` succeed, although literal word 2 is zero and specifies point filtering. The generated ordinary `image_sample` silently uses linear filtering instead. Require the known RDNA2 filter state to match rather than accepting the existing selector's approximate match. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
@ -165,0 +168,4 @@
return Ok(None);
}
let words = [0, 1, 2, 3].map(|word| Self::sgpr_const_in(&self.insns, 0, idx, (ss + word) as u8));
let [Some(w0), Some(w1), Some(w2), Some(_)] = words else {
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 20:32 UTC · original

P2 Badge Match the fourth descriptor word's border-color state

When the literal sampler uses border addressing, proving word 3 constant but discarding its value permits a different border color to be bound. title_heap::sampler_desc distinguishes border colors using d[3] >> 30, but neither decode_ssharp nor the selector receives this word. For example, a literal with word 0 0x16d and word 3 zero is accepted against a sole static sampler with border addressing and border_color = 2, replacing transparent black with opaque white for out-of-range samples. Preserve and compare the border-color state, or reject descriptors whose state cannot be matched.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 20:32 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#discussion_r4659899919) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Match the fourth descriptor word's border-color state** When the literal sampler uses border addressing, proving word 3 constant but discarding its value permits a different border color to be bound. `title_heap::sampler_desc` distinguishes border colors using `d[3] >> 30`, but neither `decode_ssharp` nor the selector receives this word. For example, a literal with word 0 `0x16d` and word 3 zero is accepted against a sole static sampler with border addressing and `border_color = 2`, replacing transparent black with opaque white for out-of-range samples. Preserve and compare the border-color state, or reject descriptors whose state cannot be matched. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-22 20:31 UTC · original · reviewed

A reviewer flagged an active readability/maintainability issue in crates/tombox-xshader/src/hlsl/durango.rs where the ss > 102 magic-number guard lacks an explicit named rationale for the sampler-descriptor/SGPR boundary assumption, requiring clarification before approval.

1 open issue
What changed in this PR

Resolves RDNA2 literal (inline 4-SGPR) sampler descriptor translation failures by proving descriptor words constant in straight-line shaders and binding them to shader-visible static samplers from the root signature, with synthetic regression coverage.

Changes:

  • Add synthetic RDNA2 tests for successful literal-sampler resolution and expected rejection for incompatible/unproven bindings.
  • Extend static-sampler resolution beyond the existing Durango path to support straight-line RDNA2 literal sampler descriptors.
  • Improve SGPR constant tracking to handle overlapping multi-SGPR SMEM destinations during constant-proving.
File Description
crates/​tombox-xshader/​tests/​synthetic.rs Adds synthetic RDNA2 fixtures and assertions validating literal sampler binding to root-signature static samplers and negative control rejections.
crates/​tombox-xshader/​src/​hlsl/​durango.rs Generalizes static-sampler selection and implements an RDNA2 straight-line literal-sampler resolution path used during HLSL emission.
crates/​tombox-xshader/​src/​hlsl.rs Updates SGPR constant-proving (including wide/overlapping SMEM destinations) and wires the static-sampler resolver into the main HLSL translation flow.

Review effort: Balanced
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-22 20:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#pullrequestreview-6014390555) · **reviewed** <!-- ccr-overview-v2 --> ### 🟡 Changes recommended A reviewer flagged an active readability/maintainability issue in `crates/tombox-xshader/src/hlsl/durango.rs` where the `ss > 102` magic-number guard lacks an explicit named rationale for the sampler-descriptor/SGPR boundary assumption, requiring clarification before approval. <details open> <summary><strong>1 open issue</strong></summary> - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> [Magic SGPR limit in sampler descriptor bounds check](https://git.gtahub.st/github/omonkeyz--xbox-emulator/pulls/183) · New </details> <details> <summary><strong>What changed in this PR</strong></summary> Resolves RDNA2 literal (inline 4-SGPR) sampler descriptor translation failures by proving descriptor words constant in straight-line shaders and binding them to shader-visible static samplers from the root signature, with synthetic regression coverage. **Changes:** - Add synthetic RDNA2 tests for successful literal-sampler resolution and expected rejection for incompatible/unproven bindings. - Extend static-sampler resolution beyond the existing Durango path to support straight-line RDNA2 literal sampler descriptors. - Improve SGPR constant tracking to handle overlapping multi-SGPR SMEM destinations during constant-proving. | File | Description | | ---- | ----------- | | crates/​tombox-xshader/​tests/​synthetic.rs | Adds synthetic RDNA2 fixtures and assertions validating literal sampler binding to root-signature static samplers and negative control rejections. | | crates/​tombox-xshader/​src/​hlsl/​durango.rs | Generalizes static-sampler selection and implements an RDNA2 straight-line literal-sampler resolution path used during HLSL emission. | | crates/​tombox-xshader/​src/​hlsl.rs | Updates SGPR constant-proving (including wide/overlapping SMEM destinations) and wires the static-sampler resolver into the main HLSL translation flow. | </details> <sub>Review effort: Balanced</sub><br><sub>Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.</sub>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 20:32 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c4c97e78c8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 20:32 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/183#pullrequestreview-6014391918) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `c4c97e78c8` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>

Pull request closed

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