shader: lower subgroup ballot find-lowest-bit in the SPIR-V bridge #1365

Manually merged
github merged 1 commit from gh-pr/1365/head into gh-pr/1365/base 2026-11-26 23:01:27 +00:00
Owner

@⁠tedin7 on GitHub · 2026-11-24 09:24 UTC · original
tedin7:x2w-spirv-ballot-findlsb → main · +214 −10 · 3 files · 1 commits · merged 2026-11-24 09:45 UTC

Change

Lowers OpGroupNonUniformBallotFindLSB %uint %scope %ballot in the named SPIR-V bridge profile (tools/spirv_ballot_extract.py), next to the existing ballot bit extraction. SPIRV-Cross cannot convert this operation to HLSL ("Cannot trivially implement BallotFindLSB in HLSL"), so a module that uses it did not reach DXC at all.

Each of the four ballot words goes through GLSL FindILsb, offset by 0, 32, 64 and 96. An empty word stays all ones and the smallest candidate wins, so an empty ballot gives 0xffffffff. The lowering has no branches, loads or stores. It needs a Subgroup scope constant and a %uint result (anything else is rejected or left alone), reuses an existing GLSL.std.450 import or adds one, and keeps the reserved %x2w_ballot_ ID check.

AI-assisted. Related: the ballot bit extraction lowering (#656). No overlapping open PR touches these files.

Verification

  • python3 -B tests/spirv_window/ballot.py spirv-as spirv-val spirv-cross passes on this branch and fails on origin/main with the same tools (the find-lowest-bit operation is left unlowered).
  • tests/spirv_window/{sampler,compare_sampler,check}.py, tests/docs_links_check.py and tests/bidi_controls_check.py pass.
  • The lowered module validates with spirv-val --target-env vulkan1.2 and converts with SPIRV-Cross at shader models 60 and 66 (package 2021.01.15). The module also holds a bit extraction, so the two lowerings' constants are checked together.
  • GPU readback: not run on this Linux host (no DXC or adapter). The CTest is spirv_ballot_extract_warp in the pr preset. I ran the lowered SPIR-V in a small CPU interpreter instead: all 256 words match the bit-scan reference.

Evidence and follow-up

The fixture is generated in the test from the existing ballot fixture's header, no external data. Thread i ballots bit i (128 positions, every word and bit 31/32 boundary) and, in a second output range, two bits at 5i+3 and 7i+11, with an empty ballot on every eighth thread (expected 0xffffffff). The reference scans the 128 bits one by one, independently of the lowering. The last 64 output words are sentinels and must stay untouched. The ballot operand is built from the thread index, so the result does not depend on the adapter's wave width.

Negative controls, each compiled and run on the adapter, must be caught at their first differing word and with the predicted mismatch count: wrong offset (word 64, 58 words), swapped words (word 32, 115), highest instead of lowest bit (word 128, 24), no empty-word fill (word 64, 107). Counts come from a model of each faulty lowering and were reproduced by the CPU interpreter.

Rejection cases: Workgroup scope, a non-scope constant, a non-constant and an unknown scope ID raise; a non-%uint result is left unchanged; a used %x2w_ballot_ ID raises.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
**@⁠tedin7** on GitHub · 2026-11-24 09:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1365) `tedin7:x2w-spirv-ballot-findlsb` → `main` · +214 −10 · 3 files · 1 commits · merged 2026-11-24 09:45 UTC ## Change Lowers `OpGroupNonUniformBallotFindLSB %uint %scope %ballot` in the named SPIR-V bridge profile (`tools/spirv_ballot_extract.py`), next to the existing ballot bit extraction. SPIRV-Cross cannot convert this operation to HLSL ("Cannot trivially implement BallotFindLSB in HLSL"), so a module that uses it did not reach DXC at all. Each of the four ballot words goes through GLSL `FindILsb`, offset by 0, 32, 64 and 96. An empty word stays all ones and the smallest candidate wins, so an empty ballot gives `0xffffffff`. The lowering has no branches, loads or stores. It needs a Subgroup scope constant and a `%uint` result (anything else is rejected or left alone), reuses an existing `GLSL.std.450` import or adds one, and keeps the reserved `%x2w_ballot_` ID check. AI-assisted. Related: the ballot bit extraction lowering (#656). No overlapping open PR touches these files. ## Verification - `python3 -B tests/spirv_window/ballot.py spirv-as spirv-val spirv-cross` passes on this branch and fails on `origin/main` with the same tools (the find-lowest-bit operation is left unlowered). - `tests/spirv_window/{sampler,compare_sampler,check}.py`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. - The lowered module validates with `spirv-val --target-env vulkan1.2` and converts with SPIRV-Cross at shader models 60 and 66 (package 2021.01.15). The module also holds a bit extraction, so the two lowerings' constants are checked together. - GPU readback: not run on this Linux host (no DXC or adapter). The CTest is `spirv_ballot_extract_warp` in the `pr` preset. I ran the lowered SPIR-V in a small CPU interpreter instead: all 256 words match the bit-scan reference. ## Evidence and follow-up The fixture is generated in the test from the existing ballot fixture's header, no external data. Thread i ballots bit i (128 positions, every word and bit 31/32 boundary) and, in a second output range, two bits at 5i+3 and 7i+11, with an empty ballot on every eighth thread (expected `0xffffffff`). The reference scans the 128 bits one by one, independently of the lowering. The last 64 output words are sentinels and must stay untouched. The ballot operand is built from the thread index, so the result does not depend on the adapter's wave width. Negative controls, each compiled and run on the adapter, must be caught at their first differing word and with the predicted mismatch count: wrong offset (word 64, 58 words), swapped words (word 32, 115), highest instead of lowest bit (word 128, 24), no empty-word fill (word 64, 107). Counts come from a model of each faulty lowering and were reproduced by the CPU interpreter. Rejection cases: Workgroup scope, a non-scope constant, a non-constant and an unknown scope ID raise; a non-`%uint` result is left unchanged; a used `%x2w_ballot_` ID raises. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
Lower OpGroupNonUniformBallotFindLSB through GLSL FindILsb on the four ballot words with offsets 0/32/64/96 and an all-ones result for an empty ballot. SPIRV-Cross cannot convert it to HLSL. The readback fixture compares 256 words with a bit-scan reference and four faulty lowerings are caught at their first differing word.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:01:25 +00:00
github manually merged commit e99dfccdc1 into gh-pr/1365/base 2026-11-26 23:01:27 +00:00
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 09:29 UTC · original

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit e99dfccdc13ef43edaeea5b21c66b4e976d6c458:

  • Debug (--preset dev): ctest --preset dev -R '^spirv_ballot_extract_warp$' --no-tests=error --output-on-failure: 1/1 passed (0.90s)
  • Release /O2 (--preset release): ctest --preset release -R '^spirv_ballot_extract_warp$' --no-tests=error --output-on-failure: 1/1 passed (0.84s)
**@⁠tedin7** on GitHub · 2026-11-24 09:29 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1365#issuecomment-7127483807) Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `e99dfccdc13ef43edaeea5b21c66b4e976d6c458`: - **Debug (`--preset dev`)**: `ctest --preset dev -R '^spirv_ballot_extract_warp$' --no-tests=error --output-on-failure`: **1/1 passed** (`0.90s`) - **Release `/O2` (`--preset release`)**: `ctest --preset release -R '^spirv_ballot_extract_warp$' --no-tests=error --output-on-failure`: **1/1 passed** (`0.84s`)
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-24 09:29 UTC · original · edited 2026-11-24 09:34 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-24T09:34:22.430284Z e99dfcc Draft marked ready
ℹ️ 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-24 09:29 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1365#issuecomment-7127487571) · edited 2026-11-24 09:34 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"e99dfccdc13ef43edaeea5b21c66b4e976d6c458","has_any_findings":false,"has_unresolved_findings":false,"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-24T09:34:22.430284Z">2026-11-24T09:34:22.430284Z</relative-time> | `e99dfcc` | Draft marked ready | <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>
This discussion has been locked. Commenting is limited to contributors.
No description provided.