Skip to content

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

Open
RandomGithub12309 wants to merge 2 commits into
ivall:mainfrom
RandomGithub12309:shaders/pso-cache-coalescing-and-mesh-cs
Open

RandomGithub12309 wants to merge 2 commits into
ivall: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 uses hash buckets and a linked LRU order. Each eviction takes expected O(1) time under the cache lock. The cache holds up to 4,096 entries within the 64 MiB budget and compares the original shader bytes on hash collisions.
  • Concurrent misses for the same key share a compile. Distinct keys can compile in parallel and cached-only probes stay non-blocking. The NGG mesh compute pass has a separate in-memory and on-disk cache identity.

Tested

  • Intel Core Ultra 7 255HX, NVIDIA RTX 5050 Laptop GPU (driver 621.14), Windows 11 build 26300, Rust 1.100.0, Windows SDK DXC 1.8.2502.11.
  • Debug: cargo test --locked --offline --workspace --no-fail-fast -- --test-threads=4 reported 657 passed, 0 failed, 40 ignored, including the new collision/slot-reuse regression test. Two GPU tests skipped for lack of wave64. The D3D12 debug-layer test skipped because d3d12SDKLayers.dll was unavailable. An earlier overlapping run failed the file-overlay end-to-end test; it passed on the isolated full run.
  • cargo clippy --locked --offline --workspace --all-targets exited 0 with no warnings in pso.rs.

Regression run
Release builds, networking disabled, 60 seconds each. GTA VI showed 257 frames on main and 226 on the PR (88% of main). Peak process commit fell from 15,451 MB to 15,293 MB. The title field below uses the game display name. GPU name came from nvidia-smi because the Windows CIM query was unavailable.

Main (PR base):

xcl-smoke v1
commit: 60565c4738a091e3e54a679d086a299d553c21e3
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: Grand Theft Auto VI
seconds: 60
exit: 0x0
frames shown: 257 (PresentX 257)
presents per 2 s (last 3): 5 6 5
device removals: 0
crashes: 0
peak commit: 15451 MB

PR head:

xcl-smoke v1
commit: 6873d469f7dc41049471478adec59a6bfe880790
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: Grand Theft Auto VI
seconds: 60
exit: 0x0
frames shown: 226 (PresentX 226)
presents per 2 s (last 3): 7 3 6
device removals: 0
crashes: 0
peak commit: 15293 MB

Not tested
AMD/native wave64 hardware, optional XCL_SAMPLE_* data, and gameplay beyond the 60-second startup run. The smoke test does not isolate the mesh-cache path.

AI use
Antigravity Orchestrator (Gemini 4 Neon Extra High) adapted the initial cache and mesh changes, wrote unit tests and ran Windows verification. OpenAI Codex (GPT-6) implemented linked LRU eviction and its collision regression test, ran the workspace and title checks, and updated this description.

Related to ivall#156. Replaces the linearly scanned 128-entry VecDeque ShaderCache with a HashMap + monotonic LRU clock (up to 4096 entries within the 64 MiB byte cap, accounting for overwritten entries' old sizes), coalesces concurrent misses per ShaderKey while leaving cached-only probes non-blocking, and caches the compute pass of NGG console_mesh pipelines in memory and on disk.

Assisted-by: Antigravity (exact model name not exposed)
@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).

Keep stable slots with older/newer links and record the collision bucket position for each slot. Test repeated evictions after a colliding entry moves in its bucket.

Assisted-by: OpenAI Codex
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.

2 participants