From aec6998a9d5c9fac4565c83aa87b18c909939cfd Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:20:58 +0100 Subject: [PATCH] d3d12: the translator's test modules are no part of the shader cache key (D3D12-SHADER-KEY-NO-TESTS) leonida-shader's build script hashed every .rs file of the crate, its *_tests.rs modules included, into LEONIDA_SHADER_SOURCE_HASH, and since 0baa9278c that hash is part of the shader cache key (translation_fingerprint). A pull request that only adds a translator test made every player and tester translate all shaders again at the next start and make their early pipelines again, which 69837e53a had ruled out for leonida-gpu's own fingerprint. The switch-name scan in the same script also put the switches only tests read (LEONIDA_DIFF_ONLY, LEONIDA_EMIT_DUMP, LEONIDA_CONSOLE_DXC, LEONIDA_PROBE) into the key. The build script now leaves *_tests.rs out of both. Each of those modules is declared under a cfg with test, so none changes what the translator emits. Three portable tests: the hash recomputed without the test modules equals the built one; test-only switches are out of the key and the translator's own are in; every `mod *_tests;` is test-only. --- d3d12/crates/leonida-shader/build.rs | 10 ++ d3d12/crates/leonida-shader/src/lib.rs | 2 + .../leonida-shader/src/source_hash_tests.rs | 93 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 d3d12/crates/leonida-shader/src/source_hash_tests.rs diff --git a/d3d12/crates/leonida-shader/build.rs b/d3d12/crates/leonida-shader/build.rs index cf99afc2ee..19eb8c5f97 100644 --- a/d3d12/crates/leonida-shader/build.rs +++ b/d3d12/crates/leonida-shader/build.rs @@ -20,6 +20,11 @@ fn files(dir: &Path, out: &mut Vec) { } } +/// Whether `p` is a test module, which the fingerprint leaves out. +fn is_tests(p: &Path) -> bool { + p.file_name().is_some_and(|n| n.to_string_lossy().ends_with("_tests.rs")) +} + /// FNV-1a, 64 bits, two lanes with different offsets: a mismatch only costs /// a cache miss, so this need not be cryptographic. fn fnv(state: &mut [u64; 2], bytes: &[u8]) { @@ -37,6 +42,11 @@ fn main() { println!("cargo:rerun-if-changed=Cargo.toml"); let mut list = Vec::new(); files(&src, &mut list); + // Not the test modules (`*_tests.rs`, each declared under `#[cfg(test)]`): they never change what the + // translator emits, and a new test changed the key, so every shader was translated again at the next start + // and its pipelines made again (as `leonida-gpu`'s build script already says for its own fingerprint). The + // switches only they read (`LEONIDA_PROBE`, ...) are no part of the key either. + list.retain(|p| !is_tests(p)); list.sort(); let mut state = [0xcbf2_9ce4_8422_2325u64, 0x8422_2325_cbf2_9ce4u64]; let mut switches = std::collections::BTreeSet::new(); diff --git a/d3d12/crates/leonida-shader/src/lib.rs b/d3d12/crates/leonida-shader/src/lib.rs index ac47f7514b..6ec69c5868 100644 --- a/d3d12/crates/leonida-shader/src/lib.rs +++ b/d3d12/crates/leonida-shader/src/lib.rs @@ -18,6 +18,8 @@ pub mod native; pub mod resource; pub mod spill; pub mod structure; +#[cfg(test)] +mod source_hash_tests; pub use dxbc::{Container, FourCC, Part}; pub use dxil::{DxilProgram, ShaderKind, XboxExtras}; diff --git a/d3d12/crates/leonida-shader/src/source_hash_tests.rs b/d3d12/crates/leonida-shader/src/source_hash_tests.rs new file mode 100644 index 0000000000..a64ee3e986 --- /dev/null +++ b/d3d12/crates/leonida-shader/src/source_hash_tests.rs @@ -0,0 +1,93 @@ +//! The translator's fingerprint (`build.rs`) leaves its test modules out: a test changes neither +//! the shader cache key nor the switches whose values join it. + +use std::path::{Path, PathBuf}; + +fn files(dir: &Path, out: &mut Vec) { + for e in std::fs::read_dir(dir).unwrap().flatten() { + let p = e.path(); + if p.is_dir() { + files(&p, out); + } else if p.extension().is_some_and(|x| x == "rs") { + out.push(p); + } + } +} + +fn src() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("src") +} + +fn is_tests(p: &Path) -> bool { + p.file_name().is_some_and(|n| n.to_string_lossy().ends_with("_tests.rs")) +} + +/// The same FNV-1a, two lanes, as `build.rs`. +fn fnv(state: &mut [u64; 2], bytes: &[u8]) { + for &b in bytes { + for (i, s) in state.iter_mut().enumerate() { + *s ^= u64::from(b) ^ (i as u64 * 0x9e); + *s = s.wrapping_mul(0x0000_0100_0000_01b3); + } + } +} + +#[test] +fn the_source_hash_leaves_the_test_modules_out() { + let src = src(); + let mut list = Vec::new(); + files(&src, &mut list); + list.retain(|p| !is_tests(p)); + list.sort(); + let mut state = [0xcbf2_9ce4_8422_2325u64, 0x8422_2325_cbf2_9ce4u64]; + for p in &list { + let rel = p.strip_prefix(&src).unwrap().to_string_lossy().replace('\\', "/"); + let text = std::fs::read(p).unwrap(); + fnv(&mut state, rel.as_bytes()); + fnv(&mut state, &(text.len() as u64).to_le_bytes()); + fnv(&mut state, &text); + } + assert_eq!( + format!("{:016x}{:016x}", state[0], state[1]), + env!("LEONIDA_SHADER_SOURCE_HASH"), + "the hash of the translator's sources without its test modules" + ); +} + +#[test] +fn switches_only_tests_read_are_no_part_of_the_key() { + let switches: Vec<&str> = env!("LEONIDA_SHADER_SWITCHES").split(',').collect(); + // Read only in `emit/diff_tests.rs` and `emit/probe_tests.rs`. + for name in ["LEONIDA_DIFF_ONLY", "LEONIDA_EMIT_DUMP", "LEONIDA_CONSOLE_DXC"] { + assert!(!switches.contains(&name), "{name} is in the key's switches"); + } + // Read by the translator itself (`emit/lower.rs`, `emit/mesh_counts.rs`). + for name in ["LEONIDA_GROUP_PACK", "LEONIDA_MESH_COUNTS"] { + assert!(switches.contains(&name), "{name} is missing from the key's switches"); + } +} + +#[test] +fn every_test_module_is_built_for_tests_only() { + let mut list = Vec::new(); + files(&src(), &mut list); + let mut seen = 0; + for p in &list { + let text = std::fs::read_to_string(p).unwrap(); + let lines: Vec<&str> = text.lines().map(str::trim).collect(); + for (i, line) in lines.iter().enumerate() { + let declared = line.trim_start_matches("pub(crate) ").trim_start_matches("pub "); + if declared.starts_with("mod ") && declared.ends_with("_tests;") { + seen += 1; + let attribute = if i > 0 { lines[i - 1] } else { "" }; + assert!( + attribute.starts_with("#[cfg(") && attribute.contains("test"), + "{}: `{line}` is not under a `cfg` with `test`, so it can change the translator \ + while its file is no part of the key", + p.display() + ); + } + } + } + assert!(seen >= 13, "found only {seen} test modules"); +}