Skip to content

Shaders: declare each dx.op call once - #1824

Open
slatefield wants to merge 2 commits into
Marvisak:mainfrom
slatefield:shader/dxop-declaration-cache
Open

slatefield wants to merge 2 commits into
Marvisak:mainfrom
slatefield:shader/dxop-declaration-cache

Conversation

@slatefield

Copy link
Copy Markdown

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.

@Melow-code Melow-code 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.

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.

@DesertGamer

Copy link
Copy Markdown

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.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

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.

@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.

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.

Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 24, 2026
Translation speed (compile-storm CPU): Marvisak#1623 inline instruction words
(decode -18..-45%), Marvisak#1834 VALU names without heap vector, Marvisak#1833 live-set
bit walk, Marvisak#1802 relative shift bounds, Marvisak#1824 dx.op declarations once.
Per-draw/per-list CPU: Marvisak#1825 view-stride table once per list, Marvisak#1826
object names only where logged, Marvisak#1800 capped-heap log spam once per 64Ki.
Tooling: Marvisak#1809 list pipeline keys without cache init.
Locking: Marvisak#1680 pipeline library loads without the global mutex.

Held: Marvisak#1819 (2^20->2^14 loop bound) breaks transcendental convergence
tests (sin/cos/exp/sqrt) — accuracy-lowering, rejected per policy.
Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 24, 2026
The cache keyed declarations on (name, attributes, return and argument
types) but returned the cached opcode constant with them. Operations
sharing one DXIL name with different opcodes (unaryBits.i32 is
first-bit-low/high and first-bit-high/low: 31/32/33/34) got the first
opcode used in the program, miscompiling every later one (med3, min/max,
cvt_pk_u8, sin/cos and gather GPU tests failed with hardware and WARP
agreeing against the model). Key on the opcode as well.
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.
@slatefield

Copy link
Copy Markdown
Author

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.

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.

5 participants