Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions d3d12/crates/leonida-shader/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ fn files(dir: &Path, out: &mut Vec<PathBuf>) {
}
}

/// 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]) {
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions d3d12/crates/leonida-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
93 changes: 93 additions & 0 deletions d3d12/crates/leonida-shader/src/source_hash_tests.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>) {
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");
}