Skip to content

Shaders: lower tbuffer_store_format_* that need no conversion - #707

Open
mercuryVM wants to merge 4 commits into
Marvisak:mainfrom
mercuryVM:impl/tbuffer-store
Open

mercuryVM wants to merge 4 commits into
Marvisak:mainfrom
mercuryVM:impl/tbuffer-store

Conversation

@mercuryVM

@mercuryVM mercuryVM commented Nov 24, 2026 •

Copy link
Copy Markdown

What

tbuffer_store_format_{x,xy,xyz,xyzw} and their _d16_ forms are MTBUF typed stores whose format is in the instruction, the store side of the typed loads the title's vertex fetch uses. The translator refused them by name. They now lower as raw stores (rawBufferStore) of the element's dwords, when no conversion is involved:

  • Formats of 32-bit channels (uint, sint and float; 32 up to 32_32_32_32, formats 20-22, 62-64, 72-77): the data's bits are stored.
  • The _d16_ forms of the 16-bit float formats (16_16, 16_16_16_16, formats 29 and 71): the halves in the data registers are stored as they are.
  • The instruction must supply every channel of the format. The view is a raw UAV at the console descriptor's base address, as for tbuffer_load_format_*. A store by index is refused, as the loads refuse a load by index.

Everything else stays refused, each with its own reason:

  • a typed-format store of a converted or narrow format: norm, scaled and narrow integer formats need conversions whose rounding and clamping I have not checked against hardware. This also covers f32 data stored into a half format: DXIL's legacyF32ToF16 rounds toward zero on the host (see below), while the console rounds to nearest even. Shaders: narrow f32 to f16 to nearest even, as the console does #422 fixes that narrowing, and these formats can follow it.
  • a typed-format store of an element under a dword: formats like 8, 16 and 8_8 would need a read-modify-write that neighbouring lanes could race on.
  • a typed-format store of some of its format's channels: what the hardware writes in the missing channels is unverified.

One helper, Lower::format_store_words, in crates/victory-shader/src/emit/lower.rs, plus a branch in Lower::buffer.

A measurement on the side (relevant to #422)

While testing the half formats I probed legacyF32ToF16 on the RTX 5070 (driver of the day) and on WARP. Both truncate toward zero and clamp overflow to 65504:

  • 1 + 1.5·2⁻¹⁰ (a tie) gives 0x3c01, where nearest-even gives 0x3c02.
  • 1 + 1.75·2⁻¹⁰ (above the half) gives 0x3c01, where nearest-even gives 0x3c02.
  • 65520 and 70000 give 0x7bff, where nearest-even gives infinity.

That supports #422's change. I did not touch the existing lowerings that use the op here.

Overlap

I searched open and closed PRs: #342 handles partial-channel buffer_store_format_* (MUBUF, format from the descriptor), and #422 handles f32→f16 narrowing. No PR handles tbuffer_store_format_*; #163 lists them as refused. This PR touches only the format branch of Lower::buffer.

Tested

Windows 11, NVIDIA RTX 5070, AMD CPU. No game data.

  • tbuffer_store_format_writes_the_formats_layout: hand-assembled compute programs run on the hardware device and on WARP and are validated with dxil.dll. Each one stores at byte offset 16 of a raw UAV, and the test reads back dwords 4-7. Cases: 32_32_32_32 float (a NaN payload and a denormal kept bit for bit) and sint, 32_32_32 float (the fourth dword untouched), 32_32 uint, 32 float, and the d16 16_16 / 16_16_16_16 float forms.
  • tbuffer_store_format_refuses_what_it_does_not_convert: 8_8_8_8 unorm, 16_16 uint, 16 float, 32_32_32_32 with two channels, a d16 store into a 32-bit format, and f32 data into both half formats. Each is refused with the expected reason.
  • Mutation check: writing one dword instead of the element's mask fails the first test.
  • cargo test --workspace: all passed (victory-shader 71 passed). rustfmt --edition 2024 --check on both changed files: clean. cargo clippy -p victory-shader --all-targets: no warnings. cargo xtask policy: 151 files ok.

Effect on the title: none

Measured by @Parakeet-Wheat on the retail 1.0.2.1 shaders, in a comment below. The title uses no tbuffer_store_format_* (against 52,927 tbuffer_load_format_*), and victory translate --list --validate gives the same outcome on main and on this PR: 21,736 validated, 4,003 failed, 162 rejected. So this PR only completes the instruction set for the store side. It doesn't unblock anything in the game, and it can wait behind work that does.

Not tested

  • The title was not run. (Its shaders don't use these instructions, per the measurement above.)
  • No console or AMD hardware. The channel layout follows the translator's existing buffer_format table, which the typed loads already use.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Typed-format stores whose format is in the instruction (MTBUF) were
refused by name. They now lower as raw stores of the element's dwords
when no conversion is involved: formats of 32-bit channels (uint, sint,
float; 32 to 32_32_32_32) store the data's bits, and the _d16_ forms of
the 16-bit float formats (16_16, 16_16_16_16) store their halves as they
are. The instruction must supply every channel of the format, as the
typed loads' layout gives it.

Everything else stays refused with a reason: norm, scaled and narrow
integer formats (conversions), elements under a dword (a
read-modify-write lanes would race on), and f32 data for half formats,
because DXIL's f32-to-f16 op rounds toward zero (measured below) where
the console rounds to nearest even; Marvisak#422 addresses that.

GPU tests store each lowered layout on the host GPU and WARP and read
the dwords back; another test checks the refusals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Windows 11 MSVC (rustc 1.98.1):

  • cargo test -p victory-shader: 71 passed, 0 failed (including both new buffer_store_format_writes_the_formats_layout and buffer_store_format_refuses_what_it_does_not_convert unit tests)
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings)
  • cargo xtask policy: 151 files ok

Direct bit-for-bit raw store lowering for 32-bit channels and d16 16-bit float formats operates cleanly without unverified rounding or race-prone narrow read-modify-writes.

@aziaconsal2026

Copy link
Copy Markdown

Tested PR head 837bb40 on Windows 11 with Radeon 780M and WARP (Rust 1.98.1 MSVC). The original layout test uses an immediate offset of 16 bytes. This test also supplies independent vector and scalar byte offsets and checks the neighboring sentinel words.

All five combinations passed on both backends. As a negative control, ignoring the vector offset made the hardware case with vector=4 and scalar=0 write at byte 16 instead of byte 20. After restoring the PR code, all 72 tests passed with cargo test --locked -p victory-shader --features host -- --test-threads=1.

Additional regression test against 837bb40
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 4349028..25d8cfc 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -832,3 +832,31 @@ fn tbuffer_store_format_refuses_what_it_does_not_convert() {
         }
     }
 }
+
+#[test]
+fn tbuffer_store_adds_scalar_and_vector_byte_offsets() {
+    // v1 = payload, v2 = vector byte offset, s2 = scalar byte offset,
+    // v4 = sentinel. Fill the window first so stray writes are observable.
+    let mut body = Vec::new();
+    for offset in (16..44).step_by(4) {
+        body.extend(buffer_store_dword(4, 8, offset));
+    }
+    body.push(vop1(0x001, 5, vgpr(2))); // v_mov_b32 v5, v2
+    let [opcode, operands] = mtbuf_store(4, 20, 1); // 32_UINT, immediate byte offset 16
+    body.extend([opcode | (1 << 12), (operands & 0x00ff_ffff) | (2 << 24)]); // offen, soffset = s2
+    let shader = build_valu_cs(&body);
+
+    let payload = 0x1357_9bdf;
+    let sentinel = 0xa5a5_a5a5;
+    for (vector, scalar) in [(0, 0), (4, 0), (0, 8), (4, 8), (12, 8)] {
+        let mut expected = [sentinel; 7];
+        expected[((vector + scalar) / 4) as usize] = payload;
+        for (backend, words) in run_dwords(&shader, [payload, vector, scalar, sentinel], 11) {
+            assert_eq!(
+                &words[4..],
+                &expected,
+                "{backend}: vector={vector} scalar={scalar}"
+            );
+        }
+    }
+}

AI-assisted contribution using a synthetic shader.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads-up: this no longer merges cleanly with current main (7374bfb) — textual conflict in crates/victory-shader/src/emit/valu_tests.rs (the shader test file several recently-merged PRs touched). Needs a rebase before merge.

For the record, the PR head itself compiles and passes on macOS 26 arm64, rustc 1.98.1 (cargo test -p victory-shader: 56 passed / 0 failed against its own base).

Resolves the conflict at the end of valu_tests.rs (both sides added
tests) and moves Marvisak#433's ONE_AT_A_TIME lock to module scope so
run_dwords takes it too. Adds the offset test @aziaconsal2026 posted on
the pull request: vector and scalar byte offsets add to the immediate
one, and neighbouring dwords stay untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mercuryVM

Copy link
Copy Markdown
Author

Merged current main (58fed37) in 05adffb, no rebase so the reviewed commits stay. @catminou4 thanks, the valu_tests.rs conflict is resolved. #433's one-at-a-time lock now sits at module scope, so the new run_dwords helper takes it too.

@aziaconsal2026 thank you for the offset test. It's in the same commit, credited in its doc comment: vector and scalar offsets add to the immediate one, and the neighbouring dwords stay untouched. It passes on the RTX 5070 and WARP, and victory-shader passes in full (93 passed).

Note for testing: cargo test -p victory-shader does not compile on current main (#719, a call to lowers without its new rsrc2 argument; @LAKSHYA-cmd-tech has a fix branch). I ran the suite with that one-line fix applied locally, not committed here.

AI-assisted (Claude Code).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked after the rebase: merges cleanly with current main (8e79c80), and cargo test -p victory-shader --locked on macOS arm64 / rustc 1.98.1 → 70 passed / 0 failed (1 ignored, the Windows-only execution suite).

Static pass: format_store_words refuses exactly the cases it documents — partial channel sets, elements under a dword (bits % 32 != 0), conversions/narrowing, and indexed stores — and only lowers the no-conversion forms (32-bit channels as-is, _d16_ float halves packed two-per-dword). The // shortcut: comment matches the repo's convention of stating the current limitation and why.

The three new tests (tbuffer_store_format_writes_the_formats_layout, ..._refuses_what_it_does_not_convert, ..._adds_scalar_and_vector_byte_offsets) live in valu_tests.rs, which is #[cfg(all(test, windows))], so they run in the new Windows CI — I can't execute them on this Mac, but the harness helpers (try_build_valu_cs, mtbuf_store, run_dwords) follow the existing patterns in that file.

@mercuryVM

Copy link
Copy Markdown
Author

@catminou4 thank you for the re-check. I merged main again (8e79c80, with the new CI) in 5e2f089, so CI can run on this PR. Locally on that head, with the CI's own commands: cargo clippy --workspace --all-targets --locked -- -D warnings and the --all-features variant are clean, cargo test --workspace --locked passes, and victory-shader has 92 passed on the RTX 5070 and WARP.

AI-assisted (Claude Code).

@dcastell1

Copy link
Copy Markdown

Verified on native Windows, no Wine: NVIDIA GeForce RTX 5090 (driver 621.24), Windows 11 10.0.26300, Rust 1.98.1, MSVC 14.44.35207, debug build. main@8e79c80 baseline on this box: 311 passed, 0 failed, 6 ignored.
With this PR at 5e2f089a applied clean (patch -p1, no fuzz):

cargo test --workspace
314 passed; 0 failed; 6 ignored   (exit 0)

Three more passing tests than the baseline, so the lowered tbuffer_store_format_* forms and their fixtures run green here.

Tree restored afterwards (full diff against a clean checkout); no game data, the tests are synthetic. AI-assisted (opencode): applied the patch to a built tree, ran cargo test --workspace, reverted it.

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
@mercuryVM

Copy link
Copy Markdown
Author

Merged current main (ffe310e) in 7ca777e. The conflict was the GPU test lock, which main moved to module scope (pub(super)) the same way this branch had, so I took main's version; run_dwords takes it as well. The CI's commands pass locally: both clippy runs, cargo test --workspace --locked, and victory-shader 123 passed. @dcastell1 thank you for the RTX 5090 run.

AI-assisted (Claude Code).

@dcastell1

Copy link
Copy Markdown

Re-verified on the new head 7ca777e5 and its base (main@ffe310e), same box:

cargo test --workspace        # log outside the tree
345 passed; 0 failed; 6 ignored   (exit 0)

Clean main@ffe310e baseline here is 342, so the three new tests still run green on this base. Supersedes my earlier note on 5e2f089a (which was measured on 8e79c80).

Environment: Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.98.1, MSVC 14.44.35207, debug. AI-assisted (opencode).

@Parakeet-Wheat

Copy link
Copy Markdown

Retail shader check for 7ca777e, for the shader-package question in Not tested:

  • Setup: Windows 11 26300, NVIDIA GeForce RTX 3070 (8 GB, driver 610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc), dxil.dll 1.8.2502, title 1.0.2.1.
  • victory translate <exe> shaders.rpf --list --validate, main ffe310e vs this PR: no program changes outcome (21,736 validated, 4,003 failed, 162 rejected on both).
  • That's expected: victory shader-info --disasm finds no tbuffer_store_format_* in the title (against 52,927 tbuffer_load_format_*), so this PR can't affect it either way.
  • cargo test -p victory-shader --locked: 123 passed, 0 failed, 1 ignored; the tbuffer_store_* tests ran on both hardware and WARP.

Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.

@mercuryVM

Copy link
Copy Markdown
Author

@Parakeet-Wheat thank you, that answers the open question. No tbuffer_store_format_* in the title means this PR changes nothing for the game, and I've put that at the top of the description. It's only instruction-set completeness now, so it can wait behind PRs that unblock title shaders. @dcastell1 thanks for re-running on the new head.

AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants