xshader: tighten mesh/ray-query bounds and descriptor size tracking #515

Merged
glovepost merged 1 commit from upstream-pr/515 into main 2026-12-01 14:36:47 +00:00
glovepost commented 2026-11-29 18:44:35 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T18:44:35Z.


Summary

Tightens bounds and operand-tracking checks in tuxbox-xshader:

  • Mesh vertex-count triangle drop (tools/shader-compiler/src/hlsl/mesh.rs): Marks first.dropPrimitive when any output index meets or exceeds the group-wide emitted vertex count sizes.x, avoiding out-of-range vertex reads when wave-local evaluate counts exceed the group count.
  • Mesh sampler waterfall null-move skip (tools/shader-compiler/src/hlsl/mesh.rs): Skips leading s_mov_b32 null, ... instructions at loop headers when matching sampler_waterfalls.
  • 16-bit interpolation operand field (tools/shader-compiler/src/hlsl/operand_fields.rs): Treats src[0] (the interpolation attribute index) of v_interp_p1ll_f16, v_interp_p1lv_f16, and v_interp_p2_f16 as a non-register field in unused_field.
  • Wave32 clear_size tracking (tools/shader-compiler/src/hlsl/descriptor_size.rs): Uses self.one_word_vector_mask() in clear_size so wave32 compares and VOP3 carry-outs (_co_, v_mad_u64_u32, v_mad_i64_i32, v_div_scale_f32, v_div_scale_f64) do not treat the next SGPR as clobbered when every lane mask is proven one word (while continuing to treat it as clobbered when vcc_hi/exec_hi is touched, e.g. s_mov_b32 vcc_hi, 0).
  • Unit tests (tools/shader-compiler/src/tests/hlsl/): Adds coverage in mesh.rs, operand_fields.rs, descriptor_size.rs (including wave32 compare + s_mov_b32 vcc_hi, 0 regression test), and ray_query.rs (a_first_hit_walk_bounds_its_proceed_loop).

Prepared with AI assistance; all changes were reviewed and verified by the contributor.

Tested

Run the checks relevant to this change.

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below

ProjectScarlett was not run with this updated commit (ccc82ec2). Earlier revision (fcc3649d) passed GPU checks (65/65 check_translator_gpu.py and 126/126 tuxbox.py verify on NVIDIA 10de:1e84 and AMD 1002:13c0), and GPU checks were not re-run after dropping narrow_saveexec and the guessed-stride hunk.

Host verification on rebased origin/main (97f21fad):

DXC=<dxc> python3 tuxbox.py check --no-gpu
  -> 9/9 passed (728 Python tests OK, 1 skipped; shader compiler 653 lib + 3 bin + 2 integration; DXBC 118; XG layout 24 passed, 1 ignored; XG entry point 7; storage codec 22; ACP HAL 9; D3D12 Windows typecheck passed)

Checks

CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of tuxbox-d3d12.

<!-- greyfinch-sync:brcly/TuxBox:issue:5553891870 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-29T18:44:35Z. --- ## Summary Tightens bounds and operand-tracking checks in `tuxbox-xshader`: - **Mesh vertex-count triangle drop (`tools/shader-compiler/src/hlsl/mesh.rs`):** Marks `first.dropPrimitive` when any output index meets or exceeds the group-wide emitted vertex count `sizes.x`, avoiding out-of-range vertex reads when wave-local evaluate counts exceed the group count. - **Mesh sampler waterfall null-move skip (`tools/shader-compiler/src/hlsl/mesh.rs`):** Skips leading `s_mov_b32 null, ...` instructions at loop headers when matching `sampler_waterfalls`. - **16-bit interpolation operand field (`tools/shader-compiler/src/hlsl/operand_fields.rs`):** Treats `src[0]` (the interpolation attribute index) of `v_interp_p1ll_f16`, `v_interp_p1lv_f16`, and `v_interp_p2_f16` as a non-register field in `unused_field`. - **Wave32 `clear_size` tracking (`tools/shader-compiler/src/hlsl/descriptor_size.rs`):** Uses `self.one_word_vector_mask()` in `clear_size` so wave32 compares and VOP3 carry-outs (`_co_`, `v_mad_u64_u32`, `v_mad_i64_i32`, `v_div_scale_f32`, `v_div_scale_f64`) do not treat the next SGPR as clobbered when every lane mask is proven one word (while continuing to treat it as clobbered when `vcc_hi`/`exec_hi` is touched, e.g. `s_mov_b32 vcc_hi, 0`). - **Unit tests (`tools/shader-compiler/src/tests/hlsl/`):** Adds coverage in `mesh.rs`, `operand_fields.rs`, `descriptor_size.rs` (including wave32 compare + `s_mov_b32 vcc_hi, 0` regression test), and `ray_query.rs` (`a_first_hit_walk_bounds_its_proceed_loop`). Prepared with AI assistance; all changes were reviewed and verified by the contributor. ## Tested Run the [checks relevant to this change](../CONTRIBUTING.md#test-what-your-change-touches). - [x] The checks relevant to this change passed - [ ] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below ProjectScarlett was **not** run with this updated commit (`ccc82ec2`). Earlier revision (`fcc3649d`) passed GPU checks (`65/65` `check_translator_gpu.py` and `126/126` `tuxbox.py verify` on NVIDIA `10de:1e84` and AMD `1002:13c0`), and GPU checks were not re-run after dropping `narrow_saveexec` and the guessed-stride hunk. Host verification on rebased `origin/main` (`97f21fad`): ```text DXC=<dxc> python3 tuxbox.py check --no-gpu -> 9/9 passed (728 Python tests OK, 1 skipped; shader compiler 653 lib + 3 bin + 2 integration; DXBC 118; XG layout 24 passed, 1 ignored; XG entry point 7; storage codec 22; ACP HAL 9; D3D12 Windows typecheck passed) ``` ## Checks CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of `tuxbox-d3d12`.
NickStallman commented 2026-11-29 19:51:07 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-29T19:51:07Z.


I was looking at three ProjectScarlett programs (CS_SSSResolve, CS_SSSResolve_MaterialClassification, CS_SSSResolveVarianceReduction) refused on main with "texture size field … used outside its size computation". Your wave32 clear_size change lets all three translate on fcc3649d, and the output passes DXC and DXV. That's CPU only; I haven't run them on a GPU or in game.

One problem: clear_size uses one_word_vector_mask() || narrow_saveexec, while the emitter leaves the next register alone only under one_word_vector_mask(). A 32-lane program that uses vcc_hi as a scratch register has the second without the first. Adding s_mov_b32 vcc_hi, 0 (0xbeeb0380) before the saveexec in your a_wave32_compare_does_not_clobber_adjacent_size_register makes it translate with s3 = 0u; after the compare, so the tracked width is overwritten and the size is wrong, where main refuses.

Using one_word_vector_mask() alone should fix it (I haven't run that edit on your branch). With only that condition added to main, for v_cmp*, the three programs translate to the same HLSL as yours.

Could the guessed-stride count be a separate change, or come with a test on a view whose stride differs from the guess? #315 refused it because a host may divide the view's size by the declared element.

<!-- greyfinch-sync:brcly/TuxBox:comment:7261608932 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515#issuecomment-7261608932). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-29T19:51:07Z. --- I was looking at three ProjectScarlett programs (`CS_SSSResolve`, `CS_SSSResolve_MaterialClassification`, `CS_SSSResolveVarianceReduction`) refused on main with "texture size field … used outside its size computation". Your wave32 `clear_size` change lets all three translate on fcc3649d, and the output passes DXC and DXV. That's CPU only; I haven't run them on a GPU or in game. One problem: `clear_size` uses `one_word_vector_mask() || narrow_saveexec`, while the emitter leaves the next register alone only under `one_word_vector_mask()`. A 32-lane program that uses `vcc_hi` as a scratch register has the second without the first. Adding `s_mov_b32 vcc_hi, 0` (0xbeeb0380) before the saveexec in your `a_wave32_compare_does_not_clobber_adjacent_size_register` makes it translate with `s3 = 0u;` after the compare, so the tracked width is overwritten and the size is wrong, where main refuses. Using `one_word_vector_mask()` alone should fix it (I haven't run that edit on your branch). With only that condition added to main, for `v_cmp*`, the three programs translate to the same HLSL as yours. Could the guessed-stride count be a separate change, or come with a test on a view whose stride differs from the guess? #315 refused it because a host may divide the view's size by the declared element.
glovepost commented 2026-11-29 22:04:29 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T22:04:29Z.


@NickStallman Thanks for checking those ProjectScarlett shaders and catching the narrow_saveexec / vcc_hi bug!

I've updated #515 and rebased onto main:

  1. clear_size (tools/shader-compiler/src/hlsl/descriptor_size.rs): Dropped narrow_saveexec (and removed it from Emitter / setup.rs, keeping narrow_masks private) so clear_size uses self.one_word_vector_mask() alone.
  2. Regression test (tools/shader-compiler/src/tests/hlsl/descriptor_size.rs): Extended a_wave32_compare_does_not_clobber_adjacent_size_register with your exact reproducer (s_mov_b32 vcc_hi, 0 / 0xbeeb_0380 before saveexec32) confirming one_word_masks stays false and the shader is refused instead of silently clobbering s3.
  3. Guessed-stride buffer count: Removed the guessed-stride emit_buffer_count change per #315 so only descriptors with a reflected stride return their element count.

Host checks (tuxbox.py check --no-gpu) passed 9/9. ProjectScarlett was not re-run with this updated branch yet.

<!-- greyfinch-sync:brcly/TuxBox:comment:7263380409 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515#issuecomment-7263380409). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-29T22:04:29Z. --- @NickStallman Thanks for checking those ProjectScarlett shaders and catching the `narrow_saveexec` / `vcc_hi` bug! I've updated #515 and rebased onto `main`: 1. **`clear_size` (`tools/shader-compiler/src/hlsl/descriptor_size.rs`):** Dropped `narrow_saveexec` (and removed it from `Emitter` / `setup.rs`, keeping `narrow_masks` private) so `clear_size` uses `self.one_word_vector_mask()` alone. 2. **Regression test (`tools/shader-compiler/src/tests/hlsl/descriptor_size.rs`):** Extended `a_wave32_compare_does_not_clobber_adjacent_size_register` with your exact reproducer (`s_mov_b32 vcc_hi, 0` / `0xbeeb_0380` before `saveexec32`) confirming `one_word_masks` stays false and the shader is refused instead of silently clobbering `s3`. 3. **Guessed-stride buffer count:** Removed the guessed-stride `emit_buffer_count` change per #315 so only descriptors with a reflected stride return their element count. Host checks (`tuxbox.py check --no-gpu`) passed `9/9`. ProjectScarlett was not re-run with this updated branch yet.
glovepost commented 2026-11-30 00:19:04 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-30T00:19:04Z.


Rebased onto main (f482572a, commit 41441f38ed06) with no functional changes from ccc82ec2, and ran the changed GPU checks on NVIDIA:

  • Command: TUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>
  • GPU / driver / backend: NVIDIA GeForce RTX 2070 SUPER (10de:1e84), driver 620.73.04, GE-Proton11-14
  • Results: Lanes and fixtures on the GPU (lanes-bindless-compute, lanes-compute-root, lanes-compute-root-recovery, lanes-raster-tables, lanes-shader-cache) and Shader translator on the GPU all passed (13 passed, 0 failed, 0 not run).
  • Still missing: AMD GPU checks. ProjectScarlett was not run on this commit.

Evidence: TuxBox check of 41441f38ed06, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not run

<!-- greyfinch-sync:brcly/TuxBox:comment:7265161092 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515#issuecomment-7265161092). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-30T00:19:04Z. --- Rebased onto `main` (`f482572a`, commit `41441f38ed06`) with no functional changes from `ccc82ec2`, and ran the changed GPU checks on NVIDIA: - **Command:** `TUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>` - **GPU / driver / backend:** NVIDIA GeForce RTX 2070 SUPER (`10de:1e84`), driver `620.73.04`, `GE-Proton11-14` - **Results:** `Lanes and fixtures on the GPU` (`lanes-bindless-compute`, `lanes-compute-root`, `lanes-compute-root-recovery`, `lanes-raster-tables`, `lanes-shader-cache`) and `Shader translator on the GPU` all passed (**13 passed, 0 failed, 0 not run**). - **Still missing:** AMD GPU checks. ProjectScarlett was not run on this commit. `Evidence: TuxBox check of 41441f38ed06, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not run`
NickStallman commented 2026-11-30 00:48:48 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T00:48:48Z.


The AMD check you were missing, on the head you pushed: AMD iGPU (1002:13c0, RADV, Mesa 26.0.8, GE-Proton11-14, Linux), full python tuxbox.py check of 41441f38: 199 pass, 0 fail. ProjectScarlett wasn't run on it.

Evidence: TuxBox check of 41441f38ed, no uncommitted changes, GPU 1002:13c0: 14 passed, 0 failed, 0 not run

<!-- greyfinch-sync:brcly/TuxBox:comment:7265598054 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515#issuecomment-7265598054). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-30T00:48:48Z. --- The AMD check you were missing, on the head you pushed: AMD iGPU (`1002:13c0`, RADV, Mesa 26.0.8, GE-Proton11-14, Linux), full `python tuxbox.py check` of 41441f38: 199 pass, 0 fail. ProjectScarlett wasn't run on it. Evidence: TuxBox check of 41441f38ed06, no uncommitted changes, GPU 1002:13c0: 14 passed, 0 failed, 0 not run
brcly commented 2026-11-30 04:12:25 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T04:12:25Z.


Heads-up: main just landed a large cleanup (up to e5d2e913), so this PR needs a rebase.

  • Formatting: the whole workspace is now formatted with rustfmt (see the new rustfmt.toml).
  • Lints: every crate denies unwrap(), lossy as casts and undocumented unsafe outside tests.
  • CI: it now also runs cargo fmt --check, rustdoc with warnings denied, and Miri on the providers' unit tests.

The least painful way is to format your branch first, so the rebase only shows real changes:

git fetch origin
git checkout origin/main -- rustfmt.toml
cargo fmt --all && git commit -am "rustfmt"
git rebase origin/main

Then run python tuxbox.py check --no-gpu, which now includes the format step.

If clippy flags something new:

  • A cast: use From for widening, TryFrom for narrowing, and a mask for bit fields.
  • An unwrap(): use ?, or expect with the reason it can't fail.
  • An unsafe block: it needs a // SAFETY: comment.

CONTRIBUTING's "Code checks" section lists the commands. Thanks!

<!-- greyfinch-sync:brcly/TuxBox:comment:7268563831 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/515#issuecomment-7268563831). Originally posted by [brcly](https://github.com/brcly) on 2026-11-30T04:12:25Z. --- Heads-up: main just landed a large cleanup (up to e5d2e913), so this PR needs a rebase. - **Formatting:** the whole workspace is now formatted with rustfmt (see the new `rustfmt.toml`). - **Lints:** every crate denies `unwrap()`, lossy `as` casts and undocumented `unsafe` outside tests. - **CI:** it now also runs `cargo fmt --check`, rustdoc with warnings denied, and Miri on the providers' unit tests. The least painful way is to format your branch first, so the rebase only shows real changes: ```sh git fetch origin git checkout origin/main -- rustfmt.toml cargo fmt --all && git commit -am "rustfmt" git rebase origin/main ``` Then run `python tuxbox.py check --no-gpu`, which now includes the format step. If clippy flags something new: - **A cast:** use `From` for widening, `TryFrom` for narrowing, and a mask for bit fields. - **An `unwrap()`:** use `?`, or `expect` with the reason it can't fail. - **An `unsafe` block:** it needs a `// SAFETY:` comment. CONTRIBUTING's "Code checks" section lists the commands. Thanks!
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
brcly/TuxBox!515
No description provided.