Skip to content

Shaders: cache HLSL mesh compute passes in ShaderCache and translated/v2 - #2040

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
RandomGithub12309:shaders/cache-hlsl-mesh-cs
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
RandomGithub12309:shaders/cache-hlsl-mesh-cs

Conversation

@RandomGithub12309

Copy link
Copy Markdown
Contributor

Part of #1269. Route the default HLSL path (and XCL_DIRECT_DXIL=1 HLSL fallback) of console_mesh through ShaderCache, shader_flight compile coalescing, and the translated/v2 disk cache under Stage::Mesh.

After #1836, console_mesh cached its compute pass only inside the XCL_DIRECT_DXIL=1 direct-translation branch. On the default HLSL path and on any direct-DXIL fallback, every (MS, PS) pipeline pairing sharing a mesh shader still re-ran translate_mesh and dxc::compile_cached without touching ShaderCache or translated/v2. Unifying both paths through compile_console_mesh + console_mesh_with matches compile_console_shader's caching behaviour (with Backend::current() keeping HLSL and direct-DXIL disk entries in separate translated/v2 namespaces).

What is in it

  • crates/xcl/src/gpu/recomp/pso.rs:
    • Extract finish_two_pass_compiled (shared by finish_two_pass and console_mesh_with) and compile_console_mesh (handling XCL_DIRECT_DXIL=1 with dxc::defer() and HLSL fallback, plus the default HLSL path).
    • Route console_mesh through console_mesh_with so both direct-DXIL and HLSL mesh compute passes share ShaderCache, shader_flight coalescing, and translated/v2 (Stage::Mesh) across (MS, PS) pairings.
    • Add console_mesh_caches_hlsl_compute_pass_in_memory_and_on_disk using a synthetic console MS container (PSV0 kind 13 + OSG1 SV_Position + NGG FSSM hw 7) to verify in-memory cache reuse, cold-RAM translated/v2 disk reuse, and dxc::cached_only hit on the public console_mesh entry point.

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 workspace build/tests, release for smoke.ps1).
  • cargo build --workspace: ok.
  • cargo test -p xcl --lib gpu::recomp::pso -- --test-threads=1 at 4b0af47: 16 passed, 0 failed, 1 ignored (0 skipped).
  • cargo test --workspace -- --test-threads=4 --skip fuzz_validate_and_gpu at 4b0af47: 1091 passed, 0 failed, 46 ignored across all workspace targets (xcl lib: 1042 passed, 0 failed, 26 ignored; integration + e2e targets: 49 passed, 0 failed, 20 ignored, including e2e: 3/0/0).
    • SKIPPED on this adapter (wave32-only, Developer Mode off, XCL_HEAP_WINDOWS unset): wave64-only GPU tests (gpu::recomp::lower, gpu::recomp::lower_dxil, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware), per-lane wave32 f16/f32 conversion refusals, shaders::tests::pso_measurement_compiles_an_exact_synthetic_record, and heap_window_out_of_memory_falls_back_to_the_switch_form.
    • Excluded gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_{0..3} due to the pre-existing RTX 50 0x8007000E CreateComputePipelineState failure tracked by DXIL GPU fuzz tests reproducibly fail with E_OUTOFMEMORY on RTX 5060 #1905.
  • Game checked: 60 s scripts/smoke.ps1 release build at 4b0af47 vs main (bf9eebd).

Regression run

xcl-smoke v1
commit: bf9eebd53e3ebf9a0311326d3d2a92c0e240ee5c
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1610 (PresentX 1610)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14534 MB
xcl-smoke v1
commit: 4b0af4783135891fa08b8406735083404be6af6b
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14617 MB

Not tested

  • AMD / Intel / native wave64 GPUs, XCL_DIRECT_DXIL=1 fallback caching under a retail shader pack, Wine / Linux, and gameplay beyond the 60 s smoke window (the 60 s startup run does not isolate mesh-pipeline compile time).

AI use

  • Antigravity (Gemini 4 Neon) wrote the code change, synthetic MS regression test, and PR description; all builds, workspace tests, and scripts/smoke.ps1 regression runs were executed on the hardware listed above.

Route both the direct-DXIL path (including its HLSL fallback) and the default HLSL path of console_mesh through ShaderCache, shader_flight coalescing, and translated/v2 under Stage::Mesh. Previously only successful XCL_DIRECT_DXIL=1 mesh translations were cached, so default HLSL builds and direct-DXIL fallbacks re-translated and re-compiled the mesh compute pass on every (MS, PS) pairing.

Part of xcl-dev#1269

Assisted-by: Antigravity (Gemini 4 Neon)
@RandomGithub12309
RandomGithub12309 marked this pull request as ready for review November 25, 2026 01:37
@raylitebull

Copy link
Copy Markdown
Member

Diff audit passed on 4b0af47 (compile_console_mesh, console_mesh_with, finish_two_pass_compiled, and console_mesh_caches_hlsl_compute_pass_in_memory_and_on_disk routing both HLSL and Direct DXIL mesh compute passes through ShaderCache, shader_flight, and translated/v2 while preserving dxc::cached_only parity). Build and test (Windows, no GPU) and PR description are both green, and RTX 5050 passed (1091 workspace + 16/16 gpu::recomp::pso).

Remaining before merge: one independent AMD verification pass on 4b0af47.

@WebIdentity1

WebIdentity1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Independent AMD check of exact 4b0af4783135891fa08b8406735083404be6af6b, with a local test-only overlay below; runtime unchanged.

Tested

Windows 10 Home 19045, Ryzen 7 7800X3D, AMD Radeon(TM) Graphics iGPU driver 32.0.21030.2001, native wave64. Rust/Cargo 1.98.1 x64 MSVC, DXC/DXIL 1.8.2502.11, debug test profile. The probe prints and verifies successful initialization of the selected AMD adapter. All 317 source hashes matched before/after and match the local snapshot; XCL recompiled.

The committed cache test never dispatches a shader. The added probe wraps a public synthetic NGG program in the same container helper, then calls console_mesh_with through first compile, RAM reuse, and fresh-RAM disk reuse. Each returned compute shader runs on AMD and checks the allocation header (3 vertices, 1 primitive) and all 12 vertex-position components against CPU values. Whole mesh-record bytes and compute DXIL match across all three cache states. The translation closure executes once, not three times; both hits avoid translation. No runtime patch was needed.

  • XCL_ADAPTER=AMD, XCL_DIRECT_DXIL=0 and 1 in separate processes:
    cargo test --locked -p xcl --lib -- gpu_cached_mesh_compute_records_match --test-threads=1 --show-output
    1 passed / 0 failed / 0 ignored / 0 skipped each. Three compute dispatches per process. Compute DXIL sizes were 4,408 bytes with HLSL selected and 2,836 with Direct DXIL selected.
  • With XCL_DIRECT_DXIL=0:
    cargo test --locked -p xcl --lib -- gpu::recomp::pso --test-threads=1 --show-output
    17 passed / 0 failed / 1 ignored / 0 skipped, including the new probe and the committed RAM/disk/public cached_only regression. The existing ignored test is compile_time_bench::repeated_pso_shader_compile. This adds three more successful compute dispatches.

Not tested

Full workspace suite, parent baseline, retail smoke/gameplay, rasterization of the returned mesh shader, concurrent compile coalescing under load, forced direct-to-HLSL fallback, discrete AMD or NVIDIA in this pass. These are compute-record checks, not pixel-output checks. No cold-driver PSO, peak-memory, or general speedup claim: the diagnostic wall timers include cache lookup and pass-through compilation, and the DXC/driver caches were not cleared. The explicit temporary translated-cache directory was fresh per probe; the wrapper's XCL_CACHE_DIR variable is not a recognized DXC override, so DXC used its existing default cache. The local probe intentionally fails without its GPU prerequisites; it is a reproduction diagnostic, not proposed unchanged for GPU-less CI. Existing required full-suite/retail gates remain separate.

AI use — Codex (GPT-6) inspected the code, wrote this test-only probe using the project's existing synthetic NGG fixture pattern, ran native Windows checks, verified hashes/output, and drafted the report.

Local test-only reproduction patch (one file)
diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs
index 9b5a305..254271d 100644
--- a/crates/xcl/src/gpu/recomp/pso.rs
+++ b/crates/xcl/src/gpu/recomp/pso.rs
@@ -1392,13 +1392,17 @@ mod shader_cache_tests {
     }
 
     fn synthetic_mesh_container() -> Vec<u8> {
-        use crate::gpu::xbox::tests::{container, dxil_part, fssm, CODE};
+        synthetic_mesh_code_container(&crate::gpu::xbox::tests::CODE, 32)
+    }
+
+    fn synthetic_mesh_code_container(code: &[u8], threads: u32) -> Vec<u8> {
+        use crate::gpu::xbox::tests::{container, dxil_part, fssm};
         let mut psv_r = vec![0u8; 48];
         psv_r[12..14].copy_from_slice(&3u16.to_le_bytes()); // max_vertices
         psv_r[14..16].copy_from_slice(&1u16.to_le_bytes()); // max_primitives
         psv_r[24] = 13; // mesh
         psv_r[27] = 2; // triangles
-        psv_r[36..40].copy_from_slice(&32u32.to_le_bytes());
+        psv_r[36..40].copy_from_slice(&threads.to_le_bytes());
         psv_r[40..44].copy_from_slice(&1u32.to_le_bytes());
         psv_r[44..48].copy_from_slice(&1u32.to_le_bytes());
         let mut psv0 = 48u32.to_le_bytes().to_vec();
@@ -1416,7 +1420,7 @@ mod shader_cache_tests {
         }
         osg1.extend_from_slice(name);
 
-        let f = fssm(&CODE, &[(7, 0)]);
+        let f = fssm(code, &[(7, 0)]);
         container(&[
             (b"SFI0", &[0; 8]),
             (b"HASH", &[0; 20]),
@@ -1426,6 +1430,61 @@ mod shader_cache_tests {
         ])
     }
 
+    #[test]
+    fn gpu_cached_mesh_compute_records_match() {
+        use super::super::{runner, MESH_HEADER};
+        use crate::gpu::rdna2::encode;
+        use std::sync::atomic::{AtomicUsize, Ordering};
+        // Local diagnostic: fail rather than silently skip the requested GPU verification.
+        println!("MESH_CACHE adapter={} native64={:?} mesh={:?}", runner::adapter().unwrap(), runner::wave64_supported(), runner::mesh_shaders_supported());
+        let split = runner::split_wave64();
+        let mut code = Vec::new();
+        let mut op = |n, e, f: &[(&str, u64)], l| code.extend(encode(n, e, f, l).unwrap());
+        op("V_MBCNT_LO_U32_B32", "ENC_VOP3", &[("VDST", 0), ("SRC0", 193), ("SRC1", 128)], None);
+        op("V_MBCNT_HI_U32_B32", "ENC_VOP3", &[("VDST", 0), ("SRC0", 193), ("SRC1", 256)], None);
+        op("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 255)], Some(1 << 12 | 3));
+        op("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 9)], None);
+        op("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 131), ("VSRC1", 0)], None);
+        op("BUFFER_LOAD_DWORDX4", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("IDXEN", 1)], None);
+        op("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 15), ("VSRC0", 10), ("VSRC1", 11), ("VSRC2", 12), ("VSRC3", 13), ("DONE", 1)], None);
+        op("S_ENDPGM", "ENC_SOPP", &[], None);
+        let blob = synthetic_mesh_code_container(&code, 64);
+        let dir = std::env::temp_dir().join(format!("xcl-mesh-gpu-cache-{}", std::process::id()));
+        let _ = std::fs::remove_dir_all(&dir);
+        let cache = Mutex::new(ShaderCache::new(64 << 20));
+        let cold = Mutex::new(ShaderCache::new(64 << 20));
+        let compiles = AtomicUsize::new(0);
+        let positions = [[-1.0f32, -1.0, 0.0, 1.0], [-1.0, 1.0, 0.0, 1.0], [0.0, -1.0, 0.0, 1.0]];
+        let mut mem = vec![0u32; 256];
+        for (v, pos) in positions.iter().enumerate() {
+            for (c, value) in pos.iter().enumerate() { mem[0x40 + 4 * v + c] = value.to_bits(); }
+        }
+        let mut state = runner::KernelState::default();
+        state.sgpr[16..20].copy_from_slice(&[0x100, 16 << 16, 3, 0]);
+        let mut first = None;
+        for (label, c) in [("first", &cache), ("ram", &cache), ("disk", &cold)] {
+            let start = std::time::Instant::now();
+            let pass = console_mesh_with(&blob, None, split, 1, &dir, c, |code, ms, wave64| {
+                assert!(wave64);
+                compiles.fetch_add(1, Ordering::SeqCst);
+                compile_console_mesh(&blob, code, ms, wave64, 1, split)
+            }).unwrap();
+            println!("MESH_CACHE {label} build_ms={:.3} compiles={} cs_bytes={}", start.elapsed().as_secs_f64()*1000.0, compiles.load(Ordering::SeqCst), pass.cs.len());
+            assert_eq!(compiles.load(Ordering::SeqCst), 1);
+            let out = runner::run(&pass.cs, &state, &mem).unwrap();
+            assert_eq!(&out.raw[..2], &[3, 1]);
+            for (v, pos) in positions.iter().enumerate() {
+                for (c, value) in pos.iter().enumerate() {
+                    assert_eq!(out.raw[MESH_HEADER + v * pass.record.vert_dwords() + c], value.to_bits(), "{label} vertex {v} component {c}");
+                }
+            }
+            let record = out.raw[..pass.record.record_dwords()].to_vec();
+            if let Some((ref dxil, ref raw)) = first { assert_eq!(&pass.cs, dxil); assert_eq!(&record, raw); }
+            else { first = Some((pass.cs, record)); }
+        }
+        let _ = std::fs::remove_dir_all(&dir);
+    }
+
     #[test]
     fn console_mesh_caches_hlsl_compute_pass_in_memory_and_on_disk() {
         use std::sync::atomic::{AtomicUsize, Ordering};

@raylitebull
raylitebull merged commit 32d6923 into xcl-dev:main Nov 25, 2026
3 checks passed
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.

3 participants