Shader: decode the packed typed-buffer formats #25

Merged
artor410 merged 1 commit from upstream-pr/25 into main 2026-11-24 07:46:26 +00:00
artor410 commented 2026-11-23 23:18:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by artor410 on 2026-11-23T23:18:27Z.


Summary

Typed buffer loads (tbuffer_load_format_*) in the 10_11_11, 11_11_10 and 10_10_10_2 families (GFX10 unified formats 30-49) were refused, so any shader using them failed translation. This fills in their channel layouts and decodes the unsigned small floats.

 buffer_format(30..=36)  // 10_11_11
-  None
+  [11, 11, 10]           // R11G11B10, channels from bit 0
 buffer_format(37..=43)  // 11_11_10
-  None
+  [10, 11, 11]
 buffer_format(44..=49)  // 10_10_10_2
-  None
+  [2, 10, 10, 10]        // mirror of 2_10_10_10

 format_load, Numeric::Float
-  legacyF16ToF32(field)
+  legacyF16ToF32(field << half_shift(w))   // 4 for 11-bit, 5 for 10-bit, 0 for 16-bit

An unsigned 11/10-bit float is a half's exponent (5 bits, bias 15) plus the top of its mantissa without the sign, so shifting it into half position widens exactly, denormals, infinities and NaNs included. The other numerics (unorm, snorm, scaled, int) go through the existing generic per-width code.

Sources, cited in the code: format numbers from LLVM's UfmtGFX10 (SIDefines.h; the GFX10 buffer table has no 5_9_9_9), channel order from Mesa's ac_formats.c (R11G11B10_FLOAT is 10_11_11; 10_10_10_2 has its 2-bit channel in the low bits).

Evidence

  • Before: buffer_format(36) (and 30-49) returned None, so the load failed translation with "a buffer format".
    After: new unit tests in emit::lower::tests:
    • packed_buffer_formats_by_number: the ids map to the layouts and numerics above.
    • small_floats_widen_as_halves: spec values through the shift: 11-bit 0x001 = 2^-20, 0x03F = largest denormal, 0x3C0 = 1.0, 0x7BF = 65024, 0x7C0 = inf, 0x7C1 = NaN; the same for 10-bit; 16-bit unchanged.

cargo test -p victory-shader passes (59). cargo clippy adds no warnings and cargo xtask policy is clean.

Not covered: I can't run diff_tests (no console compiler here), so the emitted DXIL is untested on a GPU and against the game. The snorm -1 clamp on the 2-bit channel is the existing generic code and isn't tested here either. A typed loads diff case with R11G11B10_FLOAT and the 10_10_10_2 formats would cover both.

Merge Danger

Door: two-way

Blast Radius: small

Only shaders that used to fail translation on these formats change. The 16-bit float path keeps a shift of 0.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6570861414 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/25). Originally posted by [artor410](https://github.com/artor410) on 2026-11-23T23:18:27Z. --- ## Summary Typed buffer loads (`tbuffer_load_format_*`) in the 10_11_11, 11_11_10 and 10_10_10_2 families (GFX10 unified formats 30-49) were refused, so any shader using them failed translation. This fills in their channel layouts and decodes the unsigned small floats. ```diff buffer_format(30..=36) // 10_11_11 - None + [11, 11, 10] // R11G11B10, channels from bit 0 buffer_format(37..=43) // 11_11_10 - None + [10, 11, 11] buffer_format(44..=49) // 10_10_10_2 - None + [2, 10, 10, 10] // mirror of 2_10_10_10 format_load, Numeric::Float - legacyF16ToF32(field) + legacyF16ToF32(field << half_shift(w)) // 4 for 11-bit, 5 for 10-bit, 0 for 16-bit ``` An unsigned 11/10-bit float is a half's exponent (5 bits, bias 15) plus the top of its mantissa without the sign, so shifting it into half position widens exactly, denormals, infinities and NaNs included. The other numerics (unorm, snorm, scaled, int) go through the existing generic per-width code. Sources, cited in the code: format numbers from LLVM's `UfmtGFX10` (`SIDefines.h`; the GFX10 buffer table has no 5_9_9_9), channel order from Mesa's `ac_formats.c` (R11G11B10_FLOAT is 10_11_11; 10_10_10_2 has its 2-bit channel in the low bits). ## Evidence - **Before:** `buffer_format(36)` (and 30-49) returned `None`, so the load failed translation with "a buffer format". **After:** new unit tests in `emit::lower::tests`: - `packed_buffer_formats_by_number`: the ids map to the layouts and numerics above. - `small_floats_widen_as_halves`: spec values through the shift: 11-bit `0x001` = 2^-20, `0x03F` = largest denormal, `0x3C0` = 1.0, `0x7BF` = 65024, `0x7C0` = inf, `0x7C1` = NaN; the same for 10-bit; 16-bit unchanged. `cargo test -p victory-shader` passes (59). `cargo clippy` adds no warnings and `cargo xtask policy` is clean. Not covered: I can't run `diff_tests` (no console compiler here), so the emitted DXIL is untested on a GPU and against the game. The snorm -1 clamp on the 2-bit channel is the existing generic code and isn't tested here either. A `typed loads` diff case with R11G11B10_FLOAT and the 10_10_10_2 formats would cover both. ## Merge Danger **Door:** two-way **Blast Radius:** small Only shaders that used to fail translation on these formats change. The 16-bit float path keeps a shift of 0. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:54:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:07:13Z.


Tested f1485bc stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test --workspace: 157 passed, 0 failed, 3 ignored; the merge with #39 is clean.
  • No test file appears in the diff, so this run shows the packed typed-buffer paths do not disturb the existing shader tests rather than covering the new formats themselves; a synthetic decode case would pin them.
  • Review note: the diff against main also carries a 133-line change in crates/victory-loader/src/threads.rs that does not look related to the typed-buffer work — worth confirming it is intended in this PR.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117723589 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/25#issuecomment-7117723589). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:07:13Z. --- Tested `f1485bc` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test --workspace`: **157 passed, 0 failed, 3 ignored**; the merge with #39 is clean. - No test file appears in the diff, so this run shows the packed typed-buffer paths do not disturb the existing shader tests rather than covering the new formats themselves; a synthetic decode case would pin them. - Review note: the diff against `main` also carries a 133-line change in `crates/victory-loader/src/threads.rs` that does not look related to the typed-buffer work — worth confirming it is intended in this PR. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
tedin7 commented 2026-11-24 06:54:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tedin7 on 2026-11-24T00:48:00Z.


checked: PR head f1485bc (merge-base 8c26061), Linux x86_64 (Debian, kernel 6.12), rustc/cargo 1.98.1 (from rust-toolchain.toml), throwaway worktree, no game files used.

Ran (shader crate only):

  • cargo test -j4 -p victory-shader: 53 passed, 0 failed (includes the new packed_buffer_formats_by_number, small_floats_widen_as_halves and the edited buffer_formats_by_number).
  • cargo clippy -j4 -p victory-shader --tests: no new warnings. One constant_color is never used dead-code warning in emit/mod.rs tests, which this PR does not touch.
  • Fail-before check: I put origin/main's lower.rs back (without half_shift) and added only the new packed_buffer_formats_by_number test. It fails: buffer_format(30) returns None, expected Some(([11, 11, 10], Unorm)). small_floats_widen_as_halves cannot be run against the old code because it calls the new half_shift.

Cross-checks of the claims:

  • Format numbers: I fetched LLVM's SIDefines.h (UfmtGFX10) and counted the enum. 10_11_11 starts at 30 (FLOAT is 36), 11_11_10 at 37 (FLOAT 43), 10_10_10_2 at 44, 2_10_10_10 at 50, 32_32_32_32_FLOAT is 77. This matches the table in the PR.
  • Channel order: Mesa ac_formats.c maps PIPE_FORMAT_R11G11B10_FLOAT to the 10_11_11 format and picks 2_10_10_10 for channel sizes (10,10,10,2) and 10_10_10_2 for (2,10,10,10), listed from channel 0. That agrees with the widths [11,11,10] and [2,10,10,10] listed from the lowest bits.
  • Small floats: shifting an 11-bit (5-bit exponent, 6-bit mantissa) value left by 4 and a 10-bit value left by 5 gives a half with the same exponent and the mantissa in the top bits, no sign. I worked through denormals, infinity and NaN by hand and they widen exactly. The test values agree.

Review notes (all minor):

  • small_floats_widen_as_halves checks the shift amount against a small Rust model of a half. It does not exercise the emitted shl plus legacyF16ToF32 IR, and no test lowers a typed buffer load with one of the newly accepted formats. A tiny compute program using format 36 or 49, translated through emit, would cover the decode path itself.
  • The 2-bit Snorm/Sint channel goes through the existing generic path (max 1, sign-extended from bit 1, clamped to -1); that reads correctly to me, but nothing tests it.
  • Conflicts: GitHub currently shows this PR as conflicting. Against origin/main 9aef3d9 the only conflict is the use super::{...} line of the lower.rs test module (main's new tests import Parts, lower etc.); the resolution is the union of the imports. A trial merge also conflicts in lower.rs with #127, #116 and #110 (I did not inspect those hunks) and is clean with #48, #54, #69, #73, #101, #126.

Not verified: any Windows or GPU runtime, DXIL validation of the emitted shader, or real titles. Only the unit tests and the cross-checks above were run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118445389 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/25#issuecomment-7118445389). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T00:48:00Z. --- checked: PR head `f1485bc` (merge-base `8c26061`), Linux x86_64 (Debian, kernel 6.12), rustc/cargo 1.98.1 (from rust-toolchain.toml), throwaway worktree, no game files used. Ran (shader crate only): - `cargo test -j4 -p victory-shader`: 53 passed, 0 failed (includes the new `packed_buffer_formats_by_number`, `small_floats_widen_as_halves` and the edited `buffer_formats_by_number`). - `cargo clippy -j4 -p victory-shader --tests`: no new warnings. One `constant_color is never used` dead-code warning in `emit/mod.rs` tests, which this PR does not touch. - Fail-before check: I put `origin/main`'s `lower.rs` back (without `half_shift`) and added only the new `packed_buffer_formats_by_number` test. It fails: `buffer_format(30)` returns `None`, expected `Some(([11, 11, 10], Unorm))`. `small_floats_widen_as_halves` cannot be run against the old code because it calls the new `half_shift`. Cross-checks of the claims: - Format numbers: I fetched LLVM's `SIDefines.h` (`UfmtGFX10`) and counted the enum. 10_11_11 starts at 30 (FLOAT is 36), 11_11_10 at 37 (FLOAT 43), 10_10_10_2 at 44, 2_10_10_10 at 50, 32_32_32_32_FLOAT is 77. This matches the table in the PR. - Channel order: Mesa `ac_formats.c` maps `PIPE_FORMAT_R11G11B10_FLOAT` to the 10_11_11 format and picks 2_10_10_10 for channel sizes (10,10,10,2) and 10_10_10_2 for (2,10,10,10), listed from channel 0. That agrees with the widths `[11,11,10]` and `[2,10,10,10]` listed from the lowest bits. - Small floats: shifting an 11-bit (5-bit exponent, 6-bit mantissa) value left by 4 and a 10-bit value left by 5 gives a half with the same exponent and the mantissa in the top bits, no sign. I worked through denormals, infinity and NaN by hand and they widen exactly. The test values agree. Review notes (all minor): - `small_floats_widen_as_halves` checks the shift amount against a small Rust model of a half. It does not exercise the emitted `shl` plus `legacyF16ToF32` IR, and no test lowers a typed buffer load with one of the newly accepted formats. A tiny compute program using format 36 or 49, translated through `emit`, would cover the decode path itself. - The 2-bit Snorm/Sint channel goes through the existing generic path (max 1, sign-extended from bit 1, clamped to -1); that reads correctly to me, but nothing tests it. - Conflicts: GitHub currently shows this PR as conflicting. Against `origin/main` `9aef3d9` the only conflict is the `use super::{...}` line of the `lower.rs` test module (main's new tests import `Parts`, `lower` etc.); the resolution is the union of the imports. A trial merge also conflicts in `lower.rs` with #127, #116 and #110 (I did not inspect those hunks) and is clean with #48, #54, #69, #73, #101, #126. Not verified: any Windows or GPU runtime, DXIL validation of the emitted shader, or real titles. Only the unit tests and the cross-checks above were run.
artor410 commented 2026-11-24 06:55:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by artor410 on 2026-11-24T02:03:51Z.


Thanks @DaviTostes and @tedin7! Rebased onto the latest main; the PR now touches only crates/victory-shader/src/emit/lower.rs and the stray loader diff is gone.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119718183 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/25#issuecomment-7119718183). Originally posted by [artor410](https://github.com/artor410) on 2026-11-24T02:03:51Z. --- Thanks @DaviTostes and @tedin7! Rebased onto the latest main; the PR now touches only crates/victory-shader/src/emit/lower.rs and the stray loader diff is gone.
upstream-sync closed this pull request 2026-11-24 08:01:45 +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!25
No description provided.