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

Merged
tedin7 merged 1 commit from upstream-pr/1365 into main 2026-11-24 09:45:05 +00:00
tedin7 commented 2026-11-24 09:24:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T09:24:01Z.


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6578628436 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1365). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T09:24:01Z. --- ## 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.
upstream-sync closed this pull request 2026-11-24 10:02:32 +00:00
tedin7 commented 2026-11-24 10:02:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T09:29:19Z.


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)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127483807 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1365#issuecomment-7127483807). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T09:29:19Z. --- 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`)
chatgpt-codex-connector[bot commented 2026-11-24 10:02:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T09:29:28Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127487571 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1365#issuecomment-7127487571). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T09:29:28Z. --- <!-- 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>
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
R0Y00/Xbox2Windows!1358
No description provided.