Shaders: declare each dx.op call once #1824

Open
github-bot wants to merge 2 commits from github-bot/gh-1824 into main AGit
Owner

@⁠slatefield on GitHub · 2026-11-24 23:02 UTC · original
slatefield:shader/dxop-declaration-cache → main · +70 −11 · 1 files · 2 commits · open

every dx.op call redeclared the same function: format! for the name, signature type, ptr type, i32 constant, 4 hashed lookups, and it cloned the args vec even when there was no sampler choice to patch.

now declarations sit in a 48 entry move to front cache so hot ops stay at the front. hot case 281 -> 88 ns/call. 8000 instruction dx.op-heavy program lowers 6.45 -> 4.53 ms (2000 instruction one: 1.57 -> 1.05).

output is byte identical, written_bytes_unchanged still pins the same bytes. worst case (200 distinct ops in one program) is 325 -> 402 ns, bounded by the cap.

**@⁠slatefield** on GitHub · 2026-11-24 23:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824) `slatefield:shader/dxop-declaration-cache` → `main` · +70 −11 · 1 files · 2 commits · open every dx.op call redeclared the same function: format! for the name, signature type, ptr type, i32 constant, 4 hashed lookups, and it cloned the args vec even when there was no sampler choice to patch. now declarations sit in a 48 entry move to front cache so hot ops stay at the front. hot case 281 -> 88 ns/call. 8000 instruction dx.op-heavy program lowers 6.45 -> 4.53 ms (2000 instruction one: 1.57 -> 1.05). output is byte identical, `written_bytes_unchanged` still pins the same bytes. worst case (200 distinct ops in one program) is 325 -> 402 ns, bounded by the cap.
Every dx.op call rebuilt its declaration: a format!, the signature type,
the pointer type, the opcode constant's type, four hashed lookups, and it
cloned the argument vector even when no sampler choice had to be patched.
A program repeats the same few operations, so keep the declarations in a
48-entry move-to-front list and build the call's arguments once.

An 8000-instruction dx.op-heavy program lowers in 4.53 ms instead of
6.45 ms; the emitted bitcode is unchanged, which the pinned-bytes test
checks.
upipibi caught this: dx.op.unary.f32 is one function for sin, cos, round,
floor and the rest, so a declaration cache keyed on the name and the
signature handed all of them the first call's opcode constant.
v_sin_f32(0.25) came out as round(0.25 * 2pi). The opcode is part of the
key now.

118 lib tests pass on Linux, windows-target clippy clean. The tests that
caught it (semantics_tests, valu_tests) are host tests and need a GPU run
to confirm.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:45:47 +00:00
Author
Owner

@⁠DesertGamer on GitHub · 2026-11-24 23:06 UTC · original

VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

**@⁠DesertGamer** on GitHub · 2026-11-24 23:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7147025361) ### VIctory V2: community PRs, merged and tested Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch. **Where it stands today:** launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1). :warning: Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk. Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help: https://DesertGamer/VIctory-V2 *Unofficial community fork of Marvisak/VIctory, GPL-3.0.*
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 23:10 UTC · original

This changes results, not just speed. RTX 3070 (driver 616.56) plus WARP, Windows 11, debug, 48de010 (on main 70564ae). cargo test -p victory-shader --locked: 195 passed, 14 failed, 2 ignored. main fails only #1744's 3. The 11 new ones fail identically on the hardware and on WARP:

  • semantics_tests::{byte_packing, rounding, min_max_and_clamp, min_max_and_selects_take_denormals_on_the_bits, med3_orders_signed_zeros, transcendental_specials}_…
  • valu_tests::{v_cvt_u16_and_i16_f16_saturate, v_med3_f16/f32_matches_rdna2_with_nan, v_sin_and_v_cos_f16/f32_reduce_large_turns}

For example v_sin_f32(0.25) returns 2, which is round(0.25 · 2π). v_med3_f32(0, 0, -1) returns -1, and v_cvt_pk_u8_f32(0, …) writes 255.

The cause: the op_decls lookup matches name, attr, ret and the argument types, but not opcode. On a hit it returns the cached code, the opcode constant of the first call that declared that function. Every dx.op sharing a function (unary.f32 for sin, cos, round, frc…; binary for min / max…) then runs the first one's opcode. Comparing opcode in the position closure, or building self.m.i32(opcode) on every call and caching only f, should fix it. written_bytes_unchanged probably uses only one opcode per function, so it can't see this.

AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-24 23:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7147103886) This changes results, not just speed. RTX 3070 (driver 616.56) plus WARP, Windows 11, debug, `48de010` (on `main` `70564ae`). `cargo test -p victory-shader --locked`: 195 passed, **14 failed**, 2 ignored. `main` fails only #1744's 3. The 11 new ones fail identically on the hardware and on WARP: - `semantics_tests::{byte_packing, rounding, min_max_and_clamp, min_max_and_selects_take_denormals_on_the_bits, med3_orders_signed_zeros, transcendental_specials}_…` - `valu_tests::{v_cvt_u16_and_i16_f16_saturate, v_med3_f16/f32_matches_rdna2_with_nan, v_sin_and_v_cos_f16/f32_reduce_large_turns}` For example `v_sin_f32(0.25)` returns 2, which is `round(0.25 · 2π)`. `v_med3_f32(0, 0, -1)` returns -1, and `v_cvt_pk_u8_f32(0, …)` writes 255. The cause: the `op_decls` lookup matches `name`, `attr`, `ret` and the argument types, but not `opcode`. On a hit it returns the cached `code`, the opcode constant of the first call that declared that function. Every `dx.op` sharing a function (`unary.f32` for sin, cos, round, frc…; `binary` for min / max…) then runs the first one's opcode. Comparing `opcode` in the `position` closure, or building `self.m.i32(opcode)` on every call and caching only `f`, should fix it. `written_bytes_unchanged` probably uses only one opcode per function, so it can't see this. AI-assisted (Claude Code). Synthetic tests only.
Author
Owner

@⁠slatefield on GitHub · 2026-11-25 00:37 UTC · original

Good catch, that one is real. dx.op.unary.f32 is a single function for sin, cos, round, floor and the rest, so keying the declaration on the name, attributes, return and argument types put them all under one entry and every hit returned the first call's opcode constant. The opcode is part of the cache key now (OpDecl.opcode, compared in the position closure and stored on insert), so each opcode gets its own entry and its own constant; the function and the hashed lookups are still shared when the same op passes again, which is the common case.

Pushed as 4fbaa83 on the branch. cargo test -p victory-shader --lib 118 passed, 1 ignored on Linux and the Windows-target clippy with --all-targets --all-features is clean. Your 11 failing host tests are the ones that would confirm it on real hardware or WARP: semantics_tests::{byte_packing, rounding, min_max_and_clamp, ..., transcendental_specials} and the valu_tests transcendentals. I could not rerun them here, no D3D12 host. If you still have the 3070 and WARP setup, a rerun of cargo test -p victory-shader --locked would settle it; I do not want this merged on my say-so alone.

written_bytes_unchanged not seeing it is right, it pins one opcode per function shape.

**@⁠slatefield** on GitHub · 2026-11-25 00:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7148702918) Good catch, that one is real. `dx.op.unary.f32` is a single function for sin, cos, round, floor and the rest, so keying the declaration on the name, attributes, return and argument types put them all under one entry and every hit returned the first call's opcode constant. The opcode is part of the cache key now (`OpDecl.opcode`, compared in the position closure and stored on insert), so each opcode gets its own entry and its own constant; the function and the hashed lookups are still shared when the same op passes again, which is the common case. Pushed as 4fbaa83 on the branch. `cargo test -p victory-shader --lib` 118 passed, 1 ignored on Linux and the Windows-target clippy with `--all-targets --all-features` is clean. Your 11 failing host tests are the ones that would confirm it on real hardware or WARP: `semantics_tests::{byte_packing, rounding, min_max_and_clamp, ..., transcendental_specials}` and the `valu_tests` transcendentals. I could not rerun them here, no D3D12 host. If you still have the 3070 and WARP setup, a rerun of `cargo test -p victory-shader --locked` would settle it; I do not want this merged on my say-so alone. `written_bytes_unchanged` not seeing it is right, it pins one opcode per function shape.
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 02:37 UTC · original

Re-checked 4fbaa83 on the RTX 3070 (driver 616.56) and WARP: with the opcode now part of the declaration cache key, cargo test -p victory-shader --locked gives 206 passed, with only #1744's 3 NVIDIA failures. The 11 wrong-opcode failures from before are gone.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 02:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7150955169) Re-checked `4fbaa83` on the RTX 3070 (driver 616.56) and WARP: with the opcode now part of the declaration cache key, `cargo test -p victory-shader --locked` gives 206 passed, with only #1744's 3 NVIDIA failures. The 11 wrong-opcode failures from before are gone. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:31 UTC · original · edited 2026-11-25 14:53 UTC

Automated triage for the maintainers.

Merge candidate. Shader::op (crates/victory-shader/src/emit/shader.rs) now keeps up to 48 dx.op declarations, most recent first. They are keyed by name, attribute, return type, argument types and opcode, so a repeated operation skips the format!, the signature type and the hashed lookups. The cache is per Shader, so per program. The opcode is part of the key because dx.op.unary.f32 and friends are one function for many opcodes. The call itself is unchanged, including the sampler-choice duplicate.

On your reply: the two missing points are covered now. On the GPU run: upipibi re-ran this head (4fbaa83) on an RTX 3070 and WARP, and only #1744's three known failures remain, so the 11 wrong-opcode failures from the first head are gone. I missed that report in the last note. On the measurement: your synthetic numbers (Shader::op 281 → 88 ns, a one-dx.op-per-instruction program 6.45 → 4.53 ms) show where the saving comes from, and sinan-cmyk's program-by-program run on the pack shows no change in what translates or validates. A translate timing over the pack at main and at this head would still be useful for the maintainer, but it is no longer a blocker.

CI: no check runs on the head commit.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7154882891) · edited 2026-11-25 14:53 UTC <!-- victory-triage v1 head=4fbaa83516ad5eb3d67eb5f01d7358179f436d4f verdict=merge-candidate at=2026-11-25T14:56:30Z --> Automated triage for the maintainers. **Merge candidate.** `Shader::op` (`crates/victory-shader/src/emit/shader.rs`) now keeps up to 48 `dx.op` declarations, most recent first. They are keyed by name, attribute, return type, argument types and opcode, so a repeated operation skips the `format!`, the signature type and the hashed lookups. The cache is per `Shader`, so per program. The opcode is part of the key because `dx.op.unary.f32` and friends are one function for many opcodes. The call itself is unchanged, including the sampler-choice duplicate. On your reply: the two missing points are covered now. On the GPU run: upipibi re-ran this head (`4fbaa83`) on an RTX 3070 and WARP, and only #1744's three known failures remain, so the 11 wrong-opcode failures from the first head are gone. I missed that report in the last note. On the measurement: your synthetic numbers (`Shader::op` 281 → 88 ns, a one-`dx.op`-per-instruction program 6.45 → 4.53 ms) show where the saving comes from, and sinan-cmyk's program-by-program run on the pack shows no change in what translates or validates. A `translate` timing over the pack at `main` and at this head would still be useful for the maintainer, but it is no longer a blocker. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 12:24 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head 4fbaa835 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 12:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7162340459) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 4fbaa835 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠slatefield on GitHub · 2026-11-25 12:59 UTC · original

The triage asks for a translation-time measurement before this is a candidate, and I cannot produce one: no game data on my machine. If either of you still has the tree, the run that would settle it is a translate over the pack at main 940d32f and at this head (4fbaa83), same command and same machine, e.g.

cargo build --release -p victory
time target/release/victory translate --cache <game dir>\shaders.rpf <exe> ...    # twice each, warm index

or --list if the cache write dominates. What matters is the delta between the two heads, not the absolute number.

For reference, the per-call microbenchmark I measured while writing it: Shader::op 281 -> 88 ns in the hot case, and a synthetic 8000-instruction program that is one dx.op per instruction lowers in 6.45 -> 4.53 ms. Those are synthetic; a pack number from someone with the files is worth more than both.

**@⁠slatefield** on GitHub · 2026-11-25 12:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#issuecomment-7163140350) The triage asks for a translation-time measurement before this is a candidate, and I cannot produce one: no game data on my machine. If either of you still has the tree, the run that would settle it is a `translate` over the pack at `main` `940d32f` and at this head (4fbaa83), same command and same machine, e.g. cargo build --release -p victory time target/release/victory translate --cache <game dir>\shaders.rpf <exe> ... # twice each, warm index or `--list` if the cache write dominates. What matters is the delta between the two heads, not the absolute number. For reference, the per-call microbenchmark I measured while writing it: `Shader::op` 281 -> 88 ns in the hot case, and a synthetic 8000-instruction program that is one `dx.op` per instruction lowers in 6.45 -> 4.53 ms. Those are synthetic; a pack number from someone with the files is worth more than both.
Author
Owner

@⁠Melow-code on GitHub · 2026-11-24 23:04 UTC · original · approved these changes

The move-to-front cache is a good fit: dx.op opcodes cluster by shader stage, so the hot set stays small. The 281→88 ns/call and 6.45→4.53 ms on an 8000-instruction program are solid wins. The worst case (200 distinct ops, 402 vs 325 ns) is acceptable — no real shader uses that many distinct opcodes.

One thing to verify: the cache matches on the full signature (return type + parameter types + opcode), not just the opcode number, correct? Two calls to the same opcode with different overloads (e.g. dx.op.createHandle with different resource types) must get separate declarations.

**@⁠Melow-code** on GitHub · 2026-11-24 23:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#pullrequestreview-6048812729) · **approved these changes** The move-to-front cache is a good fit: dx.op opcodes cluster by shader stage, so the hot set stays small. The 281→88 ns/call and 6.45→4.53 ms on an 8000-instruction program are solid wins. The worst case (200 distinct ops, 402 vs 325 ns) is acceptable — no real shader uses that many distinct opcodes. One thing to verify: the cache matches on the full signature (return type + parameter types + opcode), not just the opcode number, correct? Two calls to the same opcode with different overloads (e.g. `dx.op.createHandle` with different resource types) must get separate declarations.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 23:52 UTC · original · approved these changes

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 118 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows.

Each dx.op call declared once: dedup of debug-op declarations — avoids duplicate dx.op metadata.

**@⁠catminou4** on GitHub · 2026-11-24 23:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#pullrequestreview-6049332866) · **approved these changes** Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 118 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows. Each dx.op call declared once: dedup of debug-op declarations — avoids duplicate dx.op metadata.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 18:47 UTC · original · approved these changes

Verified 4fbaa83516ad5eb3d67eb5f01d7358179f436d4f cleanly merged onto main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP + Windows SDK dxil.dll):

  • Cache key soundness: OpDecl (crates/victory-shader/src/emit/shader.rs:163-171, 259-299) keys the 48-entry move-to-front cache by name (including overload suffix), attr, ret, opcode, and all argument types, fixing the shared-callee opcode collision (dx.op.unary.f32 for sin/cos/round/abs). No emit/ callers pass conflicting Attr or signatures for a shared dx.op.* name.
  • Policy & Clippy: cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed (asset policy: 191 files ok); cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • Tests: emit::shader::tests:: (1 passed), emit::tests::constant_color_validates (1 passed), v_sin_and_v_cos_f16_reduce_large_turns, v_sin_and_v_cos_f32_reduce_large_turns, transcendental_specials_match_the_hardware, and rounding_matches_the_hardware all pass on WARP and RTX 5090 (v_sin_and_v_cos_f32_reduce_large_turns 2.80s, transcendental_specials_match_the_hardware 1.56s on RTX 5090).

Ready to merge.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 18:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/1824#pullrequestreview-6079150201) · **approved these changes** Verified `4fbaa83516ad5eb3d67eb5f01d7358179f436d4f` cleanly merged onto `main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP + Windows SDK `dxil.dll`): - **Cache key soundness**: `OpDecl` (`crates/victory-shader/src/emit/shader.rs:163-171, 259-299`) keys the 48-entry move-to-front cache by `name` (including overload suffix), `attr`, `ret`, `opcode`, and all argument types, fixing the shared-callee opcode collision (`dx.op.unary.f32` for `sin`/`cos`/`round`/`abs`). No `emit/` callers pass conflicting `Attr` or signatures for a shared `dx.op.*` name. - **Policy & Clippy**: `cargo test -p xtask --locked policy::tests::repository_passes_policy`: `1 passed` (`asset policy: 191 files ok`); `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`): clean. - **Tests**: `emit::shader::tests::` (`1 passed`), `emit::tests::constant_color_validates` (`1 passed`), `v_sin_and_v_cos_f16_reduce_large_turns`, `v_sin_and_v_cos_f32_reduce_large_turns`, `transcendental_specials_match_the_hardware`, and `rounding_matches_the_hardware` all pass on WARP and RTX 5090 (`v_sin_and_v_cos_f32_reduce_large_turns` `2.80s`, `transcendental_specials_match_the_hardware` `1.56s` on RTX 5090). Ready to merge.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1824/head:github-bot/gh-1824'
git switch 'github-bot/gh-1824'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1824'
git switch 'github-bot/gh-1824'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1824'
git switch 'github-bot/gh-1824'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1824'
git switch 'main'
git merge --squash 'github-bot/gh-1824'
git switch 'main'
git merge --ff-only 'github-bot/gh-1824'
git switch 'main'
git merge 'github-bot/gh-1824'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1824
No description provided.