Skip to content

Shaders: O(1) LRU shader cache, compile coalescing, and mesh CS caching - #255

Closed
RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:shaders/pso-cache-coalescing-and-mesh-cs
Closed

RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:shaders/pso-cache-coalescing-and-mesh-cs

Conversation

@RandomGithub12309

@RandomGithub12309 RandomGithub12309 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Part of #156. Reuse shader translations across PSO workers and mesh/pixel shader pairings.

What is in it

  • crates/xcl/src/gpu/recomp/pso.rs:
    • Replace the VecDeque shader cache with hash buckets and a doubly-linked intrusive LRU order (expected O(1) lookup and eviction under the cache lock, cap 4,096 entries within the 64 MiB budget, full shader-byte verification on hash collisions).
    • Coalesce concurrent misses for the same (cache, key, blob) via an InFlight leader/waiter (Mutex + Condvar + panic-safe Drop guard) so concurrent waiters share both successful builds and compile failures instead of retrying failing compiles serially, while distinct keys compile in parallel and cached_only probes stay non-blocking.
    • Cache the NGG mesh compute pass (console_mesh, both HLSL and XCL_DIRECT_DXIL paths) under a separate CacheStage::Mesh in-memory and v2 on-disk cache identity.
    • Add unit tests for O(1) LRU eviction, collision-bucket slot repair, byte-accounting overwrite, concurrent success and failure coalescing (shader_cache_coalesces_concurrent_same_key_compiles, shader_cache_coalesces_concurrent_same_key_failures), and mesh vs compute disk-cache isolation.

Tested

  • Hardware: NVIDIA GeForce RTX 5050 Laptop GPU, Intel Core Ultra 7 255HX, Windows 11 Build 26300, rustc 1.100.0 (0ef1bc386 2026-11-09) stable-x86_64-pc-windows-msvc, Windows SDK DXC 1.8.2502.11 (debug for unit tests, release for smoke.ps1).
  • cargo test -p xcl --lib gpu::recomp::pso -- --test-threads=1 --nocapture at 80c5ca5: 20 passed, 0 failed, 1 ignored (0 skipped).
  • cargo test --workspace -- --test-threads=4 --skip fuzz_validate_and_gpu at 80c5ca5: xcl lib 944 passed, 0 failed, 25 ignored (SKIPPED on this adapter: wave64-only GPU tests and pso_measurement_compiles_an_exact_synthetic_record because Windows Developer Mode is off; gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_{0..3} excluded due to pre-existing 0x8007000E CreateComputePipelineState OOM on this adapter).
  • Game checked: 60 s scripts/smoke.ps1 release build at 80c5ca5 vs main (8e87fc7).

Regression run

xcl-smoke v1
commit: 8e87fc7b62a87ea589ccb9ea6182f514016cf7db
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1603 (PresentX 1603)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14543 MB
xcl-smoke v1
commit: 80c5ca5b640efd81d21bbfa73f19bef1e92047b5
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1587 (PresentX 1587)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14770 MB

Not tested

  • AMD/native wave64 hardware, optional XCL_SAMPLE_* data, an end-to-end repeated console_mesh cache-hit test on a real MS container, and gameplay beyond the 60 s smoke window.

AI use

  • Antigravity (Gemini 4 Neon) adapted the initial cache and mesh changes, squashed and rebased onto main, added shared failure coalescing and its test, ran the tests and scripts/smoke.ps1 on the hardware above, and updated this description. OpenAI Codex (GPT-6) implemented the linked LRU eviction and collision-bucket repair test in the earlier revision.

@PHeitor1

Copy link
Copy Markdown

Tested on RTX 5060 Ti 8 GB, driver 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), Rust 1.100.0 msvc, debug. cargo test --workspace --no-fail-fast -- --test-threads=4 on the head e336d82: 656 passed, 0 failed, 40 ignored (main 60565c4: 651 / 0 / 40). SKIPPED: only the usual two no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).

One review note: evict_oldest scans every entry of every bucket for the minimum last_used, so eviction is O(n), not O(1) as the title says, and it runs under the global cache mutex. With the cap raised from 128 to 4096, each insert past the cap scans up to 4096 entries while every PSO worker waits on that lock. Lookups are O(1) now, so it is still a win over the VecDeque, but either the title should say so or eviction needs an ordered structure (e.g. a BTreeMap<u64 /*tick*/, ShaderKey> beside the map, or an intrusive LRU list).

Run and written with Claude Code (Claude Opus 5.5).

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tested 6873d46 merged onto main 4017f8f (clean with git merge-tree). No blocker found; one behaviour to know about.

  • @PHeitor1's eviction note is fixed at this head. Eviction takes the oldest entry off the list; nothing is scanned. An insert past the cap took 1.6 µs with a cap of 128, 1.7 µs with 4,096 and 2.0 µs with 65,536 (scratch test, debug).
  • The new tests hold the new code. With the bucket index of the moved entry not repaired after swap_remove, eviction_repairs_collision_bucket_after_slot_reuse fails. With the shared flight taken out, shader_cache_coalesces_concurrent_same_key_compiles fails with 4 compiles instead of 1.
  • Controls on main. Most new tests use the new API and do not build there. The two that can be ported, shader_cache_overwrite_updates_byte_accounting and console_mesh_rejects_non_mesh_blob_before_caching, pass on main too: they guard behaviour main already has.
  • A failing compile is not shared. Four threads miss one key whose compile fails after 100 ms: 4 compiles on both builds, but the last error comes back after 402 ms on the merge and 100 ms on main. The waiters queue behind the flight and then each run the failing compile again. Handing them the first error would avoid it, if failing translations shared by several pipelines matter.
  • The file names of the on-disk translations for vertex, pixel and compute stay as they were ({stage:?} prints the same names), so existing caches are still found. Read, not run.

Tested

  • RTX 5070 Ti (NVIDIA 610.74), Ryzen 9 9950X, Windows 11 26200, Rust 1.99, debug. PR head 6873d46 merged onto 4017f8f as 6f6d885; main 4017f8f as the control.
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4 on the merge: 658 passed, 0 failed, 40 ignored (lib 618 passed, 20 ignored). SKIPPED at run time: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64 on this GPU).
  • cargo test -p xcl --lib -- shader_cache_tests: 14 passed, 0 failed on the merge.
  • The two mutations and the scratch tests above are not in the PR; each mutation was built and run on the merge.

Not tested

  • The mesh path: no test, the PR's or mine, makes a second console_mesh call for the same MS container and sees it served from the cache.
  • Any title. The description's smoke pair shows 226 frames against 257 on main, one run each; we have no title to repeat it.
  • AMD and Intel GPUs, release builds.

🤖 Claude Fable 5.2

@AC-Storm-YT

Copy link
Copy Markdown

Test run on AMD RDNA4 – commit 6873d46 (this PR's head, as is, not merged with main), clean tree

Tested

  • AMD Radeon RX 9070 XT (RDNA4, wave64), driver 32.0.32029.2010 – the tests ran on it (backend 'real' – AMD Radeon RX 9070 XT); AMD Ryzen 7 9800X3D; Windows 11 Pro build 26200; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug; DXC 1.8.2502.11
  • cargo build --workspace: ok
  • cargo test --workspace -- --test-threads=4: 657 passed, 0 failed, 40 ignored. No SKIPPED tests.
  • The six new tests pass: shader_cache_with_max_entries_keeps_more_and_evicts_lru, eviction_repairs_collision_bucket_after_slot_reuse, shader_cache_overwrite_updates_byte_accounting, shader_cache_coalesces_concurrent_same_key_compiles, translated_disk_cache_isolates_mesh_stage_from_compute, console_mesh_rejects_non_mesh_blob_before_caching

Not tested

  • No retail title or smoke.ps1 run, so nothing about shader-cache behaviour in a game
  • Release build; this commit merged with current main

AI use: run and report by Claude Code (Claude Opus 5.5).

Replace the VecDeque shader cache with a hash-indexed doubly-linked O(1) LRU cache (cap 4096), coalesce concurrent same-key shader compilations (including shared failure results), and cache console_mesh compute translations under a separate Mesh stage.
@RandomGithub12309
RandomGithub12309 force-pushed the shaders/pso-cache-coalescing-and-mesh-cs branch from 6873d46 to 80c5ca5 Compare November 25, 2026 00:56
@RandomGithub12309

RandomGithub12309 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated at 80c5ca5 (squashed and rebased onto main 8e87fc7), though #1836 (80f4819) has since merged onto main covering O(1) ShaderCache (cap 4,096), in-flight compile coalescing, and Stage::Mesh caching.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants