Skip to content

Shaders: speed up DXBC container MD5 hashing by 16-43% - #2808

Open
brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/dxbc-md5-hash
Open

brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/dxbc-md5-hash

Conversation

@brainbudt

Copy link
Copy Markdown

Every emitted DXBC container is MD5-hashed, and the compressor previously selected word schedules and rotation amounts inside its 64-step loop while allocating a temporary buffer to finalize each digest.

What changed

Benchmark (8,652-byte synthetic 5-part container, median of 5 alternating release runs)

Host Operation Before After Gain
macOS ARM64 Build + DXBC hash 18.31 µs 10.53 µs -42.5%
Linux x86-64 Build + DXBC hash 16.84 µs 14.15 µs -16.0%

Verification

  • Byte-for-byte identical output across 4,096 payload lengths (9,443,207 bytes compared) and 1,513 parser input pairs on both macOS ARM64 and Linux x86-64.
  • Windows MSVC cargo check -p victory-shader --target x86_64-pc-windows-msvc --tests and all-target Clippy -D warnings passed (default and all features).

Reviewed with mistral-loop: 2 rounds with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

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

Checked the nonstandard padding carefully: the D3D12 container hash puts the bit count FIRST (bytes 0-3), then data tail + 0x80, and 2*len+1 at bytes 60-63. The rewrite reproduces both cases exactly — rest < 56 single block [bitcount][rest][0x80][zeros][len<<1|1] and rest ≥ 56 with the 0x80-terminated first block then [bitcount][zeros][len<<1|1]. Round specialization uses the canonical schedule (7/12/17/22, 5/9/14/20, 4/11/16/23, 6/10/15/21). The malformed-fields test is a useful parse-coverage bonus. LGTM.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and benchmarked 7d7efe35 on Windows 11 against main (13f91ca).

Code review & byte-exactness

  • Pure safe Rust (as_chunks::<64>() / as_chunks::<4>()), unrolling all 4 MD5 rounds and removing the temporary tail Vec allocation while preserving DXBC's non-standard padding (len << 3 and len << 1 | 1) across both rest.len() < 56 and rest.len() >= 56 boundaries.
  • Independently cross-checked every length 0..=256 (all 64-byte remainder classes plus block boundaries 55, 56, 63, 64, 119, 120, 128) as well as 511, 512, 513, 4095, 4096 against 13f91ca: 100% byte-identical digests.

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0)

  • RTX 5090 (emit::container dxbc::): 6/6 passed; WARP + dxil.dll full victory-shader --features host: 270 passed, 0 failed (skipping only the 3 hardware-only synthetic_tests).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree vs 13f91ca.
  • container_throughput (--release, 5-run median, 8,652 B 5-part container):
    • build + hash: 10,264.1 ns/op (13f91ca) -> 9,385.8 ns/op (-8.6% / 1.09× on MSVC, with build() container allocation dominating the remainder).

@cloph-dsp

Copy link
Copy Markdown

Local verification on Windows / rustc 1.100.0 / MSVC, no game data, no GPU, no clippy/rustup, CARGO_BUILD_JOBS=2.

  • cargo test --locked -p victory-shader --lib container --quiet -> 3 passed, 1 ignored benchmark, 0 failed (273 filtered out).
  • Targeted: cargo test --locked -p victory-shader --lib reports_malformed_fields --quiet -> 1 passed.
  • git diff --check origin/main..HEAD clean.
  • Negative control: odd_parts_match_wire_bytes, dxbc_hash_padding_boundaries and reports_malformed_fields are all absent from origin/main, so the wire-byte, digest-boundary and malformed-field coverage is new here.
  • The rewritten MD5 block scheduler keeps the same D3D12 container digest; the synthetic tests cover exact wire bytes, padding at 56/64-byte boundaries and seven malformed field cases.
  • Full victory-shader lib was not run: emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index crashes on unmodified base 13f91ca on this machine (pre-existing).

AI-assisted (Hy4 flash).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This speeds up the DXBC container hash in emit/container.rs: hash builds its padded tail in a stack array instead of a Vec, and md5_block is unrolled by round. I checked the unrolled step order, the per-round message schedules ((5i+1), (3i+5), 7i mod 16 give the same words as the global index), and both tail cases (rest < 56 and >= 56) against the old code. They match, and dxbc_hash_padding_boundaries pins the old digests at the block boundaries. The new reports_malformed_fields test covers existing parse errors. The benchmark is #[ignore]d.

CI: passing.


Generated by Claude Code

@Dukestar69 Dukestar69 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 merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib emit::container::tests: 3 passed, 0 failed, 1 ignored (odd_parts_match_wire_bytes, md5_core_matches_the_standard, dxbc_hash_padding_boundaries).
  • cargo test -p victory-shader --lib dxbc::tests: 3 passed, 0 failed (including reports_malformed_fields).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
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.

6 participants