Skip to content

perf(shaders): accelerate precompilation, warm cache & fix preparation timer - #2928

Open
POMXARK wants to merge 6 commits into
xcl-dev:mainfrom
POMXARK:perf/shader-precompile-and-prep-ui
Open

POMXARK wants to merge 6 commits into
xcl-dev:mainfrom
POMXARK:perf/shader-precompile-and-prep-ui

Conversation

@POMXARK

@POMXARK POMXARK commented Nov 27, 2026 •

Copy link
Copy Markdown

Speeds up offline/startup shader precompilation, eliminates warm-cache re-work, fixes the preparation progress timer, and validates warm-index DXIL containers before skipping.

What is in it

  • crates/xcl/src/shaders.rs:
    • Deduplicate repeated recomp::translate calls (seen_programs) and repeated HLSL compiles/disk checks (seen_hlsl_jobs, e.g. shared mesh passthrough shaders) inside precompile().
    • Add a persistent warm-cache record index (precompile-index-v1-<scope>.bin) that validates only its referenced .dxil entries via recomp::dxc::validated_cache_keys, skipping xbox::parse + translation on valid hits while automatically recompiling corrupt/missing entries.
    • Honor XCL_DIRECT_DXIL=all for standalone Vertex/Pixel shaders in precompile() via pso::console_to_dxil_in, matching runtime behavior.
    • Use an RAII guard around dxc::set_memory_reserve(2) during precompile() so both CLI and pre-entry launch preparation benefit cleanly and restore the prior reserve on exit.
  • crates/xcl/src/gpu/recomp/dxc.rs:
    • Add validated_cache_keys (reusing cached_file / valid_dxil_container without scanning unrelated files), cache_key_for, pub fn toolchain_identity(), thread-local IDxcUtils reuse in compile_linked(), bounded canonical-path caching in normalized_cache_dir, and zero-syscall directory creation on hot write paths.
  • crates/xcl/src/hle/d3d12x/present/preparation.rs:
    • Replace the always-visible "No shader progress for 0 s" text with real Elapsed, throughput (records/s), and ETA, surfacing "No shader progress for N s" only when quiet >= 5.
  • crates/xcl/tests/shaders_e2e.rs:
    • Add shaders_precompile_recovers_corrupt_same_length_cached_dxil verifying recovery when a cached .dxil is overwritten with same-length zero bytes while a warm index is present.

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: Windows 11, NVIDIA GeForce RTX 5080 (driver 32.0.16.2124 / 621.24), AMD Ryzen 7 5700X (8C/16T), rustc 1.91.1 (ed61e7d7e 2025-11-07), DXC/DXIL 1.8.2502.11, debug tests & release build (--locked).
  • cargo test -p xcl --lib dxc -- --test-threads=4: 45 passed, 0 failed, 3 ignored (dump_dxc_lowering, blob_dxc_cost, dxc_time_levels)
  • cargo test -p xcl --lib preparation -- --test-threads=4: 5 passed, 0 failed, 0 ignored
  • cargo test -p xcl --lib shaders -- --test-threads=4: 17 passed, 0 failed, 0 ignored
  • cargo test -p xcl --test shaders_e2e -- --test-threads=4: 9 passed, 0 failed, 0 ignored (includes shaders_precompile_recovers_corrupt_same_length_cached_dxil)
  • Warm-I/O & 300-record precompile A/B on an anonymized RPF9 corpus (--precompile --limit 300, NVMe SSD):
    • Cold default: 18.33 s (main) -> 15.40 s (6c1a9ff warm-index + container validation)
    • Warm 300 records (475 hits, 337 .dxil / 9.3 MB): 15.24 s (main) -> 0.63 s median (0.677 / 0.631 / 0.632 s)
    • Cold XCL_DIRECT_DXIL=all: 18.57 s -> 12.68 s (-31.7%)
  • Larger warm-I/O check (--precompile --limit 1500, 2,711 hits across 2,988 .dxil / 242.5 MB on HDD): 110.79 s (main) -> 4.01 s cold OS page cache / 0.72 s (0.722 / 0.714 s) warm OS page cache (compiled 0, already cached 2711, errors 0).

Regression run
main (af2cecc408) and pre-cache runs timed out at 120s startup timeout while compiling the 26,503-shader pack on cold cache (XCL printed no log path before entering the guest). Full scripts/smoke.ps1 run pending cold shader cache completion (or test-only label).

Not tested

  • Full cargo test --workspace -- --test-threads=4 (only targeted dxc, preparation, shaders, and shaders_e2e suites were run).
  • Full 26,503-record cold shader preparation end-to-end completion and non-NVIDIA GPUs.

AI use

  • Antigravity (Google DeepMind) assisted with bottleneck profiling, implementation, unit/E2E tests, and benchmark runs.

…n timer

- crates/xcl/src/shaders.rs — add in-memory program/HLSL deduplication, persistent precompile-index-v1 warm-cache record fast-skip with atomic flushing, XCL_DIRECT_DXIL=all VS/PS support in precompile(), and RAII 2 GB memory reserve
- crates/xcl/src/gpu/recomp/dxc.rs — add existing_cache_keys single-read_dir scan, cache_key_for, thread-local IDxcUtils reuse, canonicalized cache path caching, zero-syscall cache dir write, and swap-returning set_memory_reserve
- crates/xcl/src/hle/d3d12x/present/preparation.rs — show real Elapsed time, throughput (records/s) and ETA on the preparation screen, and only show 'No shader progress for N s' when quiet >= 5s

Tests: Windows 11 RTX 5080 cargo test -p xcl --lib dxc (45 pass), preparation (5 pass), shaders (17 pass); 300-record cold XCL_DIRECT_DXIL=all 18.57s -> 12.68s (-31.7%)
@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Reproduced a false warm-cache hit on exact clean head 1d3b766db198b67732f2e6cc379e57ddaa220cdd on Windows 11 26200.9974 / RTX 5070 Ti Laptop (621.24), debug, Rust 1.97.1 MSVC, DXC/DXIL 1.8.2502.11. I used one synthetic pixel shader built with the existing shaders_e2e container fixture and ran xcl shaders <fixture folder> --precompile with an isolated XCL cache.

Cache state before the run Compiled / already cached File after the run
Empty 1 / 0 Valid 2,828-byte DXIL container
Warm index and valid DXIL 0 / 1 Original SHA-256
Warm index, DXIL missing 1 / 0 Restored original SHA-256
Warm index, DXIL replaced by 2,828 zero bytes 0 / 1 Corrupt file unchanged
Same corrupt file, only the warm index moved aside 1 / 0 Restored original SHA-256

Every run exited 0 with zero reported shader errors. existing_cache_keys checks the filename and minimum length, then the index-hit branch skips the normal container validation. The same-length damage case is a useful CLI regression if the warm index is meant to preserve corrupt-cache recovery. Test scope was standalone precompile with a synthetic graphics shader. OpenAI Codex (6.2sol) assisted with verification.

@ettehn

ettehn commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up with a tested fix on top of 1d3b766: load the index first, collect its referenced DXIL keys, then accept a key only when the existing cached_file validator accepts its container. This keeps the early skip of parsing and translating a shader record on a sound hit. It reads each referenced DXIL container at startup and leaves unrelated cache entries unread.

Warm-cache timing added: on the same Windows machine, offline --precompile --limit 300 (default HLSL path, no PSO builds), 4 interleaved warm runs per binary, same seeded cache with 337 DXIL files (9.75 MB). Median wall time: 1,372.75 ms original vs 1,397.55 ms with validation (+24.8 ms, +1.8%). The ranges were 1,369.9–1,380.7 ms and 1,394.8–1,409.6 ms. Every run reported 0 compiled, 475 cached, 0 errors. This measures the small sample with a warm OS file cache; the full pack and cold-storage I/O are still unmeasured.

I added an end-to-end test using the existing synthetic pixel shader fixture. It checks a cold compile, a valid warm hit, then overwrites the cached DXIL with zeros without changing its length. The test fails on the original head (compiled 0, already cached 1) and passes with validation (compiled 1, already cached 0, original DXIL bytes restored). On Windows / RTX 5070 Ti Laptop, shaders_e2e passed 9/9 and the cache_tests precompile_index library filters passed 36/36 with the local fix. OpenAI Codex (6.2sol) assisted with the patch and verification.

@andrewDoing

Copy link
Copy Markdown

I reproduced a valid-file warm-index mismatch on 1d3b766db198b67732f2e6cc379e57ddaa220cdd, separate from the corrupt-file case. A clean build precompiled a synthetic pixel shader; the same source with #2930's emitter change then reported a warm hit but did not create the new DXIL key that its cold run produced. The two minimal-test DXIL payloads happened to be identical, so this demonstrates missed preparation, not incorrect pixels.

Including the existing XCL_TRANSLATOR_SOURCE_ID in the index scope addressed that cross-build case. Two runtime options also matter: a branching PS under XCL_RECOMP_NO_STRUCTURED=1 and a barrier-containing mesh shader under XCL_SAFE_BARRIER=0 selected new DXIL keys, but a source-ID-only index still reported the old entries warm.

A local candidate scopes the index by translator source identity and the effective safe-barrier setting, and reuses the existing conservative cache policy for other diagnostic overrides. It leaves runtime shader-cache eligibility unchanged. A standalone synthetic regression passed 19 CLI invocations across two independently built emitter revisions, including unset versus explicit 1, and 0 versus whitespace-padded 0. Same-configuration repeats remained warm. An independent agent reran the 19 checks and reviewed the candidate. Retail behavior and performance remain unmeasured.

AI use: OpenAI Codex with GPT-6.2 Astra constructed the probes, tested the candidate and independently reviewed it; Codex coordinated and prepared this report.

@POMXARK

POMXARK commented Nov 27, 2026

Copy link
Copy Markdown
Author

@ettehn Thanks for catching and reproducing this! Pushed 6c1a9ff (6c1a9ffb31137dcbd2b09b6f5ae4e0da3cbd635b) with that exact fix:

  1. Validate only referenced warm-index DXIL entries (dxc.rs / shaders.rs)

    • Replaced existing_cache_keys (which only checked filename + len >= 36) with recomp::dxc::validated_cache_keys(dir, &referenced).
    • precompile() now loads precompile-index-v1-<scope>.bin first and validates only the keys referenced by the index via the existing cached_file (valid_dxil_container) check, leaving unrelated files in the cache directory untouched. Any corrupt or zero-filled .dxil of identical byte length is rejected from warm-hits and cleanly recompiled/overwritten.
  2. Regression tests & verification (Windows 11, RTX 5080, rustc 1.91.1)

    • Added E2E test shaders_precompile_recovers_corrupt_same_length_cached_dxil in crates/xcl/tests/shaders_e2e.rs (cold compile -> valid warm hit -> overwrite .dxil with same-length zero bytes -> verify compiled 1, already cached 0, errors 0 and restored DXBC/DXIL bytes).
    • Updated unit test validated_cache_keys_and_memory_reserve_swap_behave_cleanly in dxc.rs to reject same-length zeroed files and ignore unreferenced cache keys.
    • Results after the last edit:
      • cargo test -p xcl --lib dxc -- --test-threads=4: 45 passed, 0 failed, 3 ignored
      • cargo test -p xcl --lib preparation -- --test-threads=4: 5 passed, 0 failed
      • cargo test -p xcl --lib shaders -- --test-threads=4: 17 passed, 0 failed
      • cargo test -p xcl --test shaders_e2e -- --test-threads=4: 9 passed, 0 failed
  3. Measured warm-I/O validation cost

    • 300 records (NVMe SSD, 475 hits across 337 .dxil files / 9.3 MB):
      • 0.677 s / 0.631 s / 0.632 s total wall time (compiled 0, already cached 475, dominated by ~0.55 s initial RPF9 read) vs 15.24 s unindexed warm baseline (01_xcl on the same 300-record cache).
    • 1,500 records (USB HDD, 2,711 hits across 2,988 .dxil files / 242.5 MB, 38.1 KB index):
      • Cold OS page cache: 4.01 s (compiled 0, already cached 2711, errors 0)
      • Warm OS page cache: 0.722 s / 0.714 s
      • Unindexed warm baseline (main): 110.79 s

@andrewDoing

Copy link
Copy Markdown

I retested the valid-file index cases from my earlier comment on updated head 6c1a9ffb31137dcbd2b09b6f5ae4e0da3cbd635b. The new damaged-file validation addresses a separate case; these intact entries still produced stale warm-index hits:

  • XCL_RECOMP_NO_STRUCTURED=1 on the synthetic branching pixel shader: the warm run reported 1 cached, while a fresh or index-bypassing run generated a different DXIL key.
  • XCL_SAFE_BARRIER=0 on the synthetic mesh shader: the warm run reported 2 cached, while a fresh or bypass run generated a different compute key and reused the passthrough key.
  • After an emitter change, the new head reported the old build's entry warm while its own fresh build generated a different key.

All 11 fresh-process checks exited successfully. This shows preparation can skip the newly selected cache key; it does not establish a rendered-pixel error or GTA VI loading impact.

AI use: OpenAI Codex with GPT-6.2 Astra ran the checks; Codex coordinated and prepared this follow-up.

@POMXARK

POMXARK commented Nov 27, 2026

Copy link
Copy Markdown
Author

@andrewDoing Thank you for catching that! That cross-build and option-scoping bug is fixed in fe16099 (fe16099b195d971ab89a96def5a0ad214fb13401):

  1. Translator source identity in index scope (shaders.rs)

    • precompile_index_scope now hashes env!("XCL_TRANSLATOR_SOURCE_ID") alongside CARGO_PKG_VERSION, dxc::toolchain_identity(), window count, split flags, direct_gfx, and effective safe_barrier, so emitter changes automatically isolate old precompile-index-v1-*.bin files and trigger fresh DXIL generation.
  2. Runtime option scoping & diagnostic bypass (pso.rs, shaders.rs)

    • Included effective safe_barrier (XCL_SAFE_BARRIER: raw.map_or(true, |v| v.trim() != "0")) in the index scope so "0"/" 0 " and unset/"1"/" 1 " use distinct scopes without false misses on whitespace/explicit defaults.
    • Exposed pso::precompile_index_allowed() reusing pso::cache_allowed(Stage::Pixel, ...) so when any diagnostic override is present (XCL_RECOMP_NO_STRUCTURED, XCL_RECOMP_WHY, XCL_X64_ALL_MASKS, XCL_WAVE64_SPIN, XCL_DXIL_NO_OPT, XCL_DEBUG_*), use_record_index is forced to false.
  3. Tests added & verified on Windows 11 / RTX 5080 (rustc 1.91.1)

    • Extended shaders::tests::precompile_index_round_trips_and_ignores_corrupt_tail (translator source ID flip, safe_barrier_enabled padding/toggle, all 9 cache_allowed env vars).
    • Added E2E test shaders_precompile_scopes_safe_barrier_and_bypasses_diagnostic_overrides in crates/xcl/tests/shaders_e2e.rs.
    • cargo test -p xcl --lib dxc -- --test-threads=4: 45 passed, 0 failed, 3 ignored
    • cargo test -p xcl --lib preparation -- --test-threads=4: 5 passed, 0 failed
    • cargo test -p xcl --lib shaders -- --test-threads=4: 17 passed, 0 failed
    • cargo test -p xcl --test shaders_e2e -- --test-threads=4: 10 passed, 0 failed

@andrewDoing

Copy link
Copy Markdown

A separate synthetic concurrency check on current head 6c1a9ffb31137dcbd2b09b6f5ae4e0da3cbd635b: two CLI processes preparing different shaders into the same initially empty cache both returned success, but the resulting shared record index contained only one of the two entries. This happened in 3/3 concurrent pairs. Sequential preparation retained both entries.

Both DXIL files from every concurrent pair were byte-identical to the sequential controls. A serial revisit restored the two index entries with one cached shader and zero compiles per run. The demonstrated effect is a lost index shortcut, not lost DXIL or demonstrated extra DXC compilation. Retail timing remains unmeasured.

The experiment used two repository-derived synthetic pixel shaders and separate temporary caches per trial. In the current implementation each process loads an index snapshot and later saves its full updated snapshot to the shared path.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the synthetic checks; Codex coordinated and prepared this follow-up.

@andrewDoing

Copy link
Copy Markdown

Retested the valid-file cases from my earlier reports on updated head fe16099b195d971ab89a96def5a0ad214fb13401. The new scope and eligibility changes passed 19 synthetic CLI invocations: five across emitter builds, six for the branching pixel shader with the structured-flow override, and eight for the mesh safe-barrier settings. Warm runs produced the independently established cold keys, and equivalent barrier settings shared the expected index. No title or gameplay performance was measured.

The concurrent-writer case in my other comment is separate. On this head 3/3 concurrent pairs still retained only one of two index records; both DXIL files remained intact, and serial recovery recompiled neither shader.

AI use: OpenAI Codex with GPT-6.2 Astra ran the checks; Codex coordinated and prepared this follow-up.

@andrewDoing

Copy link
Copy Markdown

Follow-up to my concurrent-index report: I tested a local Windows candidate on fe16099b that acquires a persistent sidecar file lock, reloads the latest index under the lock, merges only newly learned records, and replaces the index from a completed same-directory temporary file. Compilation remains outside the lock.

All three concurrent two-process trials retained both index entries and produced DXIL byte-identical to the sequential control. A focused Rust serialization/merge regression passed. In separate controls, a held lock, a handle denying replacement, and an inaccessible sidecar each left the previous index intact; preparation still completed and a later run recovered both entries. The lock attempt is best-effort and timed out after about 250 ms in the held-lock test. A separate process-death test confirmed the OS releases the lock when its owner exits.

This addresses the index shortcut only. It does not establish a retail timing benefit, and power-loss durability was not tested.

AI use: OpenAI Codex with GPT-6.2 Astra implemented and independently reviewed the candidate and ran the synthetic tests; Codex coordinated and prepared this follow-up.

@POMXARK

POMXARK commented Nov 27, 2026

Copy link
Copy Markdown
Author

@andrewDoing Thank you again for isolating the concurrent-writer race and testing that approach! Pushed 9b91851 (9b918518e4ca7f6a02e4ffae13a9e5a3c77a2645) closing this race:

  1. Bounded cross-process lock + reload-merge + non-destructive replace (dxc.rs, shaders.rs)

    • Exposed recomp::dxc::lock_cache_file_timeout(path, 250) reusing the per-path Windows named mutex (best-effort 250 ms wait so a stuck lock cannot stall preparation; OS automatically releases mutex ownership if an owner exits).
    • save_precompile_index now only tracks and merges newly learned entries from the current run (index_state starts empty), reloads the latest on-disk precompile-index-v1-<scope>.bin under the lock, writes a unique same-directory temp file, and replaces atomically via MoveFileExW(..., MOVEFILE_REPLACE_EXISTING) without deleting the prior file first (if replacement fails, the previous index stays intact). Compilation and DXIL writes stay outside the index lock.
  2. Tests verified on Windows 11 / RTX 5080 (rustc 1.91.1)

    • Extended shaders::tests::precompile_index_round_trips_and_ignores_corrupt_tail to verify disjoint additions merge cleanly onto an existing index.
    • Added shaders_precompile_concurrent_processes_preserve_both_index_entries in crates/xcl/tests/shaders_e2e.rs (3 trials of two concurrent xcl shaders --precompile processes into an empty cache, asserting both succeed and subsequent warm runs of both hit compiled 0, already cached 1).
    • Targeted test suites after the last edit: dxc 45/45 (3 ignored), preparation 5/5, shaders 17/17, shaders_e2e 11/11 (78/78 PASS).

@andrewDoing

Copy link
Copy Markdown

Retested the concurrent-writer case from my earlier comment on updated head 9b918518e4ca7f6a02e4ffae13a9e5a3c77a2645. In 3/3 synthetic two-process pairs, the index contained both expected record hashes immediately after the children exited and before any serial revisit. Both DXIL files matched the sequential control byte-for-byte. The earlier heads retained only one record in the same check.

One test-oracle suggestion: checking only that subsequent serial runs report 0 compiled, 1 cached does not by itself prove both initial records survived. In the earlier failing trials, intact DXIL files allowed those serial runs to hit and repair the missing index entry. Inspecting both record hashes before either revisit distinguishes the cases.

In a separate Windows sharing-violation control, the prior index remained intact and both DXIL outputs were retained. This is synthetic cache evidence, not a retail timing measurement.

AI use: OpenAI Codex with GPT-6.2 Astra ran the controls and independently reviewed the test oracle; Codex coordinated and prepared this follow-up.

@ettehn

ettehn commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

One Windows path regression in 9b91851’s new index replacement. I built the exact load_precompile_index, save_precompile_index, and mutex helpers in a small standalone Rust 1.97.1 harness on Windows 11. Two saves with disjoint entries worked at 110 characters. At 326 characters, a normal absolute path created the directory and temporary file via Rust std, but both MoveFileExW calls returned Windows error 3 and no index was saved. The same directory worked through its canonical extended-length path.

Replacing only the raw Win32 call with std::fs::rename(&tmp, path) and retaining temp cleanup on error saved both entries through the ordinary 326-character path. With a destination handle denying replacement, the call still failed and the previous index stayed byte-identical. Retrying after closing the handle merged the third entry. No pre-delete is needed. This was an isolated index I/O test, without the full CLI or a retail title. Windows path limits · Rust rename

Concurrent long-path check. I tested ten fresh pairs of independent writer processes per variant. One process used an ordinary absolute path (314–315 characters) and the other used the canonical extended-length spelling of the same index. Both spellings contended on the same mutex. On 9b91851, the ordinary-path writer failed with Windows error 3 in 10/10 pairs and only the extended-path writer’s record survived. With the std::fs::rename change, both saves succeeded and both exact records were present immediately after exit in 10/10 pairs, before any serial revisit. This used the same standalone index harness.

OpenAI Codex (6.2sol) assisted with verification.

Originally posted by @ettehn in #2928 (comment)

@andrewDoing

Copy link
Copy Markdown

I reproduced @ettehn’s Windows long-path finding through xcl shaders <input> --precompile on 9b918518. I used one synthetic pixel shader and set XCL_DXIL_CACHE separately for each process.

Cache directory CLI result Files after exit
Fresh, ordinary 92-character path 1 compiled DXIL and precompile index
Fresh, ordinary 304-character path 1 compiled DXIL only
Same long directory reopened with the \\?\ prefix 1 already cached DXIL and precompile index

All three processes exited successfully and reported zero shader errors. The long-path run preserved the compiled shader but silently missed the index write. The extended-path revisit shows why the CLI summary alone cannot verify index persistence. This test covered the existing PR head. @ettehn’s proposed replacement was tested separately in the index harness.

AI use: OpenAI Codex with GPT-6.2 Astra ran the CLI checks; Codex coordinated and prepared this follow-up.

@POMXARK

POMXARK commented Nov 27, 2026

Copy link
Copy Markdown
Author

@ettehn @andrewDoing Thank you both for the follow-up verification and catching these! Pushed 65091a5a7af87d5d0b5bf11f35a34bd17e948661 addressing both points:

1. Long-path index replacement (crates/xcl/src/shaders.rs) — @ettehn

  • Replaced raw MoveFileExW in save_precompile_index with std::fs::rename(&tmp, path) (cleaning &tmp and leaving any existing index intact if replacement fails, without pre-deleting).
  • Added unit test shaders::tests::precompile_index_saves_and_merges_over_long_path exercising round-trip save and disjoint merge on a path exceeding 260 characters.

2. Direct on-disk index oracle in parallel E2E (crates/xcl/tests/shaders_e2e.rs) — @andrewDoing

  • Updated shaders_precompile_concurrent_processes_preserve_both_index_entries to parse the XCLPIDX1 index file from disk right after both concurrent child writers exit and assert both distinct entry hashes are present (entries.len() == 2) before any sequential warm revisit runs.

Tests verified on Windows 11 / RTX 5080 (rustc 1.91.1)

  • cargo test -p xcl --lib dxc -- --test-threads=4: 45 passed, 0 failed, 3 ignored
  • cargo test -p xcl --lib preparation -- --test-threads=4: 5 passed, 0 failed
  • cargo test -p xcl --lib shaders -- --test-threads=4: 18 passed, 0 failed (includes precompile_index_saves_and_merges_over_long_path)
  • cargo test -p xcl --test shaders_e2e -- --test-threads=4: 11 passed, 0 failed (includes updated shaders_precompile_concurrent_processes_preserve_both_index_entries)
  • Total: 79/79 active PASS + cargo build --release --locked -p xcl PASS

@andrewDoing

Copy link
Copy Markdown

On head 65091a5a7af87d5d0b5bf11f35a34bd17e948661, I reproduced a Windows case where an existing record index could be replaced even though the writer could not read it for the merge.

I used the unmodified xcl shaders --precompile CLI, two distinct synthetic shaders, and a fresh temporary DXIL cache per trial. After preparing shader A, I opened its index with read, write, and delete sharing and took an exclusive Windows byte-range lock on its first byte. I confirmed an independent read failed with the lock held. I then prepared shader B into the same cache, released the lock, and read the index before revisiting either shader.

In 3/3 trials, B's preparation succeeded with zero reported errors. A's record hash 50400740f07017b0 disappeared. The complete 28-byte index matched a separate cold B-only reference byte-for-byte, containing only f6376fb69eb5f730. Both DXIL files remained present. A separate trial denying read sharing while permitting delete sharing also lost A's record. This demonstrates loss of a cache-index entry under a forced read failure. I did not observe shader corruption or measure title performance.

The save path currently treats every index read error as an empty map. For publication, treating only NotFound as empty and skipping the save on other I/O errors would preserve the existing index until it can be read again. The ordinary startup lookup can still treat unreadable data as a cache miss.

AI use: OpenAI Codex with GPT-6.2 Astra adapted and ran the Windows probes, checked the independent reference, and drafted this report.

@andrewDoing

Copy link
Copy Markdown

Retested the long-path and concurrent-writer cases on updated head 65091a5a7af87d5d0b5bf11f35a34bd17e948661. The full CLI wrote one index record on a 304-character ordinary Windows path, then merged a second. Both record hashes and DXIL keys matched the short-path control before a warm revisit; the revisit hit. Three concurrent pairs also preserved both expected records before revisits, with DXIL files byte-identical to the sequential control.

This confirms the tested long-path and normal concurrent-writer fixes; the forced read-failure case in my separate follow-up remains distinct. No retail title or timing was tested.

AI use: OpenAI Codex with GPT-6.2 Astra ran the synthetic CLI checks; Codex coordinated and prepared this follow-up.

@andrewDoing

Copy link
Copy Markdown

Follow-up to the forced index-read failure on 65091a5a7af87d5d0b5bf11f35a34bd17e948661: I tested a local candidate where the writer treats NotFound as an empty index but skips publication for other read errors. Normal lookup remains best-effort.

In 3/3 Windows byte-range-denial trials, shader B still compiled while the existing A index remained byte-identical. After the lock was released, a B revisit produced both exact reference records. In 3/3 normal concurrent-writer pairs, both records were present immediately and both DXIL files matched the sequential controls. This is synthetic cache testing; retail performance and power-loss durability remain unmeasured.

AI use: OpenAI Codex with GPT-6.2 Astra implemented and tested the local candidate; Codex coordinated and prepared this follow-up.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

I measured one remaining warm-index cost on 65091a5a. --precompile --limit 1 still reads the DXIL for every key referenced by the whole index before selecting the input record. With one synthetic pixel shader selected, I populated both build-scoped indexes with the same selected record and 4,096 additional synthetic index entries, each pointing to an existing valid 2,828-byte DXIL file. Windows 11 26200, MSVC Rust 1.97.1 debug, SDK DXC 1.8.2502.11. Six interleaved fresh processes per variant with a warm OS file cache.

Variant Process read operations Process read bytes Median wall time
65091a5a, eager validation 8,198 11,668,624 419.4 ms
Local on-demand validation 6 85,136 29.9 ms

All runs reported compiled 0, already cached 1, errors 0. Read counts are Windows logical process I/O, including the index, input, and DXIL. The local patch memoizes validation in a per-key OnceLock<bool> and calls the same existing container validator only when a selected record needs the key. The 11 shaders_e2e tests passed, including same-length corruption recovery and concurrent index writers. This measures a small precompile slice against a larger warm index. Full-pack throughput and retail impact remain unmeasured.

OpenAI Codex assisted with the local patch and verification.

@LocranKing

Copy link
Copy Markdown

Tested PR #2928 (65091a5a) against main (8b957bec) on shaders.rpf (9cc6a39e) using an RTX 3060 Ti (8 GiB, driver 621.40) / Ryzen 5 5500 (6C/12T) with XCL_DEVICE_TYPE=lockhart, XCL_HEAP_WINDOWS=1, XCL_DIRECT_DXIL=all, XCL_DIRECT_SPLIT_INSTRUCTIONS=0, and DXC 1.9.2602.17 (21d28f72).

Precompile A/B (xcl shaders shaders.rpf --precompile --limit N)

Each arm used an isolated XCL_DXIL_CACHE directory in order (cold --limit 300 -> warm --limit 300 -> cold --limit 1500 -> warm --limit 1500):

Run main (8b957bec) wall / reported main counts (compiled / cached / err / skip) PR #2928 (65091a5a) wall / reported PR #2928 counts (compiled / cached / err / skip)
Cold --limit 300 17.070 s / 16.7 s 337 / 138 / 0 / 7 13.792 s / 12.0 s 337 / 138 / 0 / 7
Warm --limit 300 0.801 s / 0.7 s 0 / 475 / 0 / 7 0.664 s / 0.6 s 125 / 350 / 0 / 7
Cold --limit 1500 215.502 s / 215.4 s 1264 / 1447 / 0 / 31 202.950 s / 202.9 s 1389 / 1322 / 0 / 31
Warm --limit 1500 2.082 s / 2.0 s 0 / 2711 / 0 / 31 0.763 s / 0.7 s 289 / 2422 / 0 / 31

Notes

  • Faster across all four passes: Cold --limit 300 improved 17.07 s -> 13.79 s, cold --limit 1500 improved 215.50 s -> 202.95 s, and warm --limit 1500 dropped 2.082 s -> 0.763 s (~2.7x faster).
  • Warm-cache counter & index quirk under XCL_DIRECT_DXIL=all: In crates/xcl/src/shaders.rs, when direct_gfx (XCL_DIRECT_DXIL=all) is active, Vertex/Pixel entries take the console_to_dxil_in(...) branch which sets record_indexable = false and increments compiled unconditionally on Ok(_) instead of distinguishing cache hits from fresh compiles. That explains why the warm --limit 300 and --limit 1500 runs still reported compiled 125 and compiled 289 (350 + 125 = 475 and 2422 + 289 = 2711) despite finishing in 0.6 s / 0.7 s.
  • Not tested: Full 26,503-shader --precompile --pipelines, startup-screen cancellation (launch prep timer), or live in-game frames on PR perf(shaders): accelerate precompilation, warm cache & fix preparation timer #2928.

- Propagate non-NotFound I/O errors from try_load_precompile_index inside save_precompile_index so unreadable or locked index files are never overwritten as empty
- Replace eager upfront validated_cache_keys scan across the entire precompile index with lazy per-key OnceLock validation (recomp::dxc::validate_cached_key) evaluated only when a selected record matches the index
- Propagate cache-hit status through console_to_dxil_in_status so warm direct-DXIL and cached compute runs count against already cached instead of compiled

Tests (Windows release): precompile_index 3/3, validated_cache_keys 1/1, shader_cache_tests 23/23, shaders_e2e 12/12 PASS
@POMXARK

POMXARK commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thanks for the reviews @andrewDoing @ettehn @LocranKing — pushed updates addressing all three items:

  1. Preserve index on non-NotFound read errors (@andrewDoing):
    Split out try_load_precompile_index in crates/xcl/src/shaders.rs. save_precompile_index now propagates non-NotFound std::fs::read errors instead of falling back to an empty map, preventing an unreadable or Windows-locked existing index from being overwritten/truncated. load_precompile_index keeps best-effort behavior at startup. Added save_precompile_index_preserves_existing_file_on_read_error (including a Windows share_mode(0) lock case).

  2. Lazy per-key DXIL validation (@ettehn):
    Removed the eager validated_cache_keys scan across the whole initial_index before --limit selection. Added recomp::dxc::validate_cached_key and memoized per-key OnceLock<bool> cells inside precompile() so .dxil containers are validated on demand only for records actually visited during the run (still catching same-length corruption).

  3. Accurate compiled vs already cached counts under XCL_DIRECT_DXIL=all (@LocranKing):
    Added pso::console_to_dxil_in_status (propagating RAM ShaderCache, on-disk translated/v2, and DXC .dxil cache-hit status) and wired it into both the compute and direct graphics arms in shaders::precompile(), plus e2e coverage in shaders_precompile_direct_dxil_warm_reports_cached_and_limit_skips_unselected_keys.

Verified on Windows (cargo check -p xcl --all-targets, precompile_index 3/3, validated_cache_keys 1/1, shader_cache_tests 23/23, shaders_e2e 12/12 — all passing).

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.

4 participants