Shaders: lower dynamic v_perm_b32 without requiring INT64 #423

Merged
pedrofort merged 1 commit from upstream-pr/423 into main 2026-11-24 08:13:05 +00:00
pedrofort commented 2026-11-24 03:13:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pedrofort on 2026-11-24T03:13:37Z.


<head></head>

Problem

The dynamic-selector lowering of v_perm_b32 combines two 32-bit operands into a 64-bit integer, unnecessarily declaring the INT64 shader capability.

The generated shift can also exceed the integer width for selector values above seven, even though subsequent selects replace those intermediate results.

Implementation

  • Select the source dword using selector bit 2.

  • Select the byte within that dword using selector bits 0–1.

  • Keep shift amounts within the valid 32-bit range.

  • Preserve the existing handling of sign replication and constant-byte selectors.

  • Leave constant-selector lowering unchanged.

Verification

Apple M5, macOS arm64, Rust 1.98.1, base commit d2a4bae.

Test | Before | After -- | -- | -- Constant selector without INT64 | PASS | PASS Dynamic selector without INT64 | FAIL | PASS Full shader unit test suite | — | 58 passed, 0 failed

The regression tests inspect the SFI0 feature flags of emitted DXIL containers.

A separate CPU arithmetic model comparison matched 1,048,576 cases, covering all 256 byte selectors across 4,096 deterministic operand pairs.

  • Library Clippy with -D warnings: clean.

  • Asset policy: 151 files OK.

  • git diff --check: clean.

Limitations

The arithmetic comparison verifies the proposed algorithm, while the Rust regression tests verify emitted feature flags. GPU execution and DXIL validation on Windows have not been performed.

All inputs are synthetic. No game assets were used.

AI-assisted implementation and test preparation (ChatGPT), with tests executed locally on Apple Silicon.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573787495 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/423). Originally posted by [pedrofort](https://github.com/pedrofort) on 2026-11-24T03:13:37Z. --- <head></head><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Problem</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The dynamic-selector lowering of<span class="Apple-converted-space"> </span></span><code><span>v_perm_b32</span></code><span><span class="Apple-converted-space"> </span>combines two 32-bit operands into a 64-bit integer, unnecessarily declaring the INT64 shader capability.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The generated shift can also exceed the integer width for selector values above seven, even though subsequent selects replace those intermediate results.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Implementation</span></h2><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Select the source dword using selector bit 2.</span></p></li><li><p><span>Select the byte within that dword using selector bits 0–1.</span></p></li><li><p><span>Keep shift amounts within the valid 32-bit range.</span></p></li><li><p><span>Preserve the existing handling of sign replication and constant-byte selectors.</span></p></li><li><p><span>Leave constant-selector lowering unchanged.</span></p></li></ul><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Verification</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Apple M5, macOS arm64, Rust 1.98.1, base commit<span class="Apple-converted-space"> </span></span><code><span>d2a4bae</span></code><span>.</span></p> Test | Before | After -- | -- | -- Constant selector without INT64 | PASS | PASS Dynamic selector without INT64 | FAIL | PASS Full shader unit test suite | — | 58 passed, 0 failed <p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The regression tests inspect the SFI0 feature flags of emitted DXIL containers.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>A separate CPU arithmetic model comparison matched 1,048,576 cases, covering all 256 byte selectors across 4,096 deterministic operand pairs.</span></p><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Library Clippy with<span class="Apple-converted-space"> </span></span><code><span>-D warnings</span></code><span>: clean.</span></p></li><li><p><span>Asset policy: 151 files OK.</span></p></li><li><p><code><span>git diff --check</span></code><span>: clean.</span></p></li></ul><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Limitations</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The arithmetic comparison verifies the proposed algorithm, while the Rust regression tests verify emitted feature flags. GPU execution and DXIL validation on Windows have not been performed.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>All inputs are synthetic. No game assets were used.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>AI-assisted implementation and test preparation (ChatGPT), with tests executed locally on Apple Silicon.</span></p>
towiice commented 2026-11-24 06:52:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by towiice on 2026-11-24T03:21:27Z.

Review: COMMENTED


Tested PR head d5704aa on Windows 11 x64 build 10.0.26300 (26H2), NVIDIA GeForce GTX 1650 SUPER (driver 610.88), Rust 1.100.0 (stable-x86_64-pc-windows-gnullvm):

  • cargo test -p victory-shader -- --test-threads=4: 71 passed, 0 failed (7.43 s).

All victory-shader tests (including the new dynamic v_perm_b32 lowering) pass cleanly.

<!-- greyfinch-sync:Marvisak/VIctory:review:6033179037 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/423#pullrequestreview-6033179037). Originally posted by [towiice](https://github.com/towiice) on 2026-11-24T03:21:27Z. **Review:** COMMENTED --- Tested PR head `d5704aa` on Windows 11 x64 build 10.0.26300 (26H2), NVIDIA GeForce GTX 1650 SUPER (driver 610.88), Rust 1.100.0 (`stable-x86_64-pc-windows-gnullvm`): - `cargo test -p victory-shader -- --test-threads=4`: **71 passed, 0 failed** (7.43 s). All `victory-shader` tests (including the new dynamic `v_perm_b32` lowering) pass cleanly.
mercuryVM commented 2026-11-24 06:52:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:33:13Z.

Review: APPROVED


Tested d5704aa on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (266 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines.

  • Same byte semantics as before. Codes 0–3 pick bytes of src1 and 4–7 bytes of src0 (sel & 4 chooses the dword, sel & 3 the byte), which is what (src0 << 32 | src1) >> (sel * 8) gave.
  • The shift is now at most 24, so codes ≥ 8 no longer produce an out-of-range shift before the later selects replace them. INT64 is no longer requested.
  • The two new tests check the SFI0 INT64 bit for both a constant and a dynamic selector.
  • Nit: the new test block is not rustfmt-formatted (words.iter()\n.flat_map, wrapped resource::track( args).

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034712381 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/423#pullrequestreview-6034712381). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T06:33:13Z. **Review:** APPROVED --- Tested d5704aa on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (266 passed, 0 failed). `victory translate <title exe> --validate --pipelines` gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. - Same byte semantics as before. Codes 0–3 pick bytes of `src1` and 4–7 bytes of `src0` (`sel & 4` chooses the dword, `sel & 3` the byte), which is what `(src0 << 32 | src1) >> (sel * 8)` gave. - The shift is now at most 24, so codes ≥ 8 no longer produce an out-of-range shift before the later selects replace them. INT64 is no longer requested. - The two new tests check the SFI0 INT64 bit for both a constant and a dynamic selector. - Nit: the new test block is not rustfmt-formatted (`words.iter()\n.flat_map`, wrapped `resource::track(` args). AI-assisted (Claude Code).
pedrofort commented 2026-11-24 06:55:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pedrofort on 2026-11-24T03:42:04Z.


Additional GPU validation on Apple M5 using Metal Shader Converter 4.0.3.

Compared the original and patched dynamic v_perm_b32 lowering using two synthetic pixel shaders covering all 256 selector values in each output byte.

Both implementations produced 1,024/1,024 matching pixels per operand set, with zero channel error and byte-identical GPU readbacks.

The patched version removes the INT64 feature requirement while preserving the tested GPU results.

Metal API and GPU validation were enabled. No game assets were used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121339109 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/423#issuecomment-7121339109). Originally posted by [pedrofort](https://github.com/pedrofort) on 2026-11-24T03:42:04Z. --- Additional GPU validation on Apple M5 using Metal Shader Converter 4.0.3. Compared the original and patched dynamic v_perm_b32 lowering using two synthetic pixel shaders covering all 256 selector values in each output byte. Both implementations produced 1,024/1,024 matching pixels per operand set, with zero channel error and byte-identical GPU readbacks. The patched version removes the INT64 feature requirement while preserving the tested GPU results. Metal API and GPU validation were enabled. No game assets were used.
upstream-sync closed this pull request 2026-11-24 09:05:29 +00:00
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
Marvisak/VIctory!423
No description provided.