Skip to content

Shaders: lower s_bcnt0_i32_b32/b64 and s_ff0_i32_b32/b64 - #165

Closed
n-romanovich wants to merge 3 commits into
Marvisak:mainfrom
n-romanovich:feat/scalar-bitcount-firstbit
Closed

n-romanovich wants to merge 3 commits into
Marvisak:mainfrom
n-romanovich:feat/scalar-bitcount-firstbit

Conversation

@n-romanovich

Copy link
Copy Markdown

Implements DXIL lowering for the zero-bit counting and find-first-zero scalar ALU instructions in victory-shader:

  • s_bcnt0_i32_b32: bitwise inverts src0 and counts set bits via DXIL unaryBits.i32 (31), updating SCC (result != 0).
  • s_ff0_i32_b32: bitwise inverts src0 and finds the lowest set bit via DXIL unaryBits.i32 (32, returning -1 when all bits are 1).
  • s_bcnt0_i32_b64, s_ff0_i32_b64: shares the 64-bit split-dword lowering paths with s_bcnt1_i32_b64 and s_ff1_i32_b64 over inverted dwords, without requiring DXIL INT64.

Testing

  • Added emit::tests::scalar_bitcount_and_first_zero_execute_on_gpu in crates/victory-shader/src/emit/mod.rs covering 32-bit and 64-bit inputs, inline -1, high-dword hits, and SCC updates.
  • Validated emitted DXIL with dxil.dll and executed on D3D12 (NVIDIA GeForce RTX 5060 Ti).
  • cargo fmt -p victory-shader -- --check: passed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: passed (0 warnings).
  • cargo xtask policy: passed (asset policy: 151 files ok).
  • cargo test -p victory-shader: 70 passed, 0 failed.

@DaviTostes

Copy link
Copy Markdown

Tested 60e32ce on Arch Linux x86_64, rustc 1.98.1: pass.

No game data used.

@Sunkiller-qc

Copy link
Copy Markdown

This duplicates #148 (opened earlier, which also adds s_flbit_i32_b64/_i64), and #163 covers the same opcodes too. The lowering here looks correct, and the GPU test is a plus. It might be worth agreeing with #148's author on which one goes in, e.g. #148's lowering plus this test.

Static review, not built or run.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware check of the four instructions: all match.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 130 cases: low dwords from edge values (including 0 and 0xffffffff) and random, high dwords 0, 0xffffffff or random. SCC was read right after each instruction.

  • s_bcnt0_i32_b32 / _b64: clear-bit count, SCC = result != 0: 130/130 each.
  • s_ff0_i32_b32 / _b64: lowest clear bit, -1 when all bits are 1, SCC unchanged: 130/130 each.

(#148 covers the same four plus s_flbit_i32_b64/_i64, also matching.)

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

@DeerSilver

Copy link
Copy Markdown

Tested head 50d8a98 merged onto main ffe310e on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.

Merged onto current main, the test build fails. 6638098 ("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument to resource::track:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
  --> crates/victory-shader/src/emit/mod.rs (in the new test)
   |     resource::track(&p, &native.layout, native.stage)
   |     argument #4 of type `Option<&structure::Structured>` is missing

There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break cargo test on main. The call this PR adds needs the fourth argument. main's own tests pass Some(&st); None keeps the old three-argument behaviour.

With , None added locally to that one call, everything passes:

  • The PR's GPU test ran on the RTX 5060 (DXGI adapter 0 on this laptop; no "no SM 6.6 device" skip) and passed:
    • scalar_bitcount_and_first_zero_execute_on_gpu
  • cargo test --workspace --locked: 343 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored on main.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

@alexdeliu

Copy link
Copy Markdown

Merged onto main @ ffe310eb, this PR (50d8a98c) no longer compiles its tests. crates/victory-shader/src/emit/mod.rs:824 calls resource::track with 3 arguments. Since 6638098 it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

@n-romanovich

Copy link
Copy Markdown
Author

Thanks for testing and pointing this out, @alexdeliu!

Merged latest main and updated resource::track in the test to pass Some(&st) per 6638098. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.

@alexdeliu

Copy link
Copy Markdown

@n-romanovich rechecked cee01b43 merged onto main @ ffe310eb on the RX 9070: both CI clippy runs are clean and cargo test --workspace passes (343 tests).

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #148. s_bcnt0/s_ff0 (b32 and b64) lowering equal in effect to #148's, which also covers s_flbit_i32_b64/_i64. Its GPU test builds HostShader with the old field set and does not compile on main.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants