mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-28 09:19:51 +01:00
* Extract the material graph runtime from TextureCraft Preserve its evaluator, compiler, existing verification, and format reference from TextureCraft #777 at 22a37c29. Original TextureCraft importer author: xilefenko (TextureCraft #521). * Adapt graph helpers to the shared workspace lints Preserve node evaluation and version tracking while splitting long dispatch branches. Update the EXR doctest and rule table references for the shared crate. * Read the format reference with LF endings in its test * Test that the format reference example reads the same with CRLF line endings * Make the kind versions follow module attributes, arm order, raw strings, includes and the epoch * Fix graph cache keys, publish, import and subgraph extraction; harden the graph tests * Keep the cubic sampler from panicking on NaN texels; say what RGBE drops; check NaN halves by their bits * Lower vector inputs of f_to_bool and f_vec to valid WGSL; tighten the FX-map error and pattern coverage tests; document clamping and the splatter data cutoff * Say which composite glyph placements the font reader lacks; fail the y-up text test on a blank render * Leave publish as it was; unit-test the WGSL vector lowering; document EPOCH and keep_outputs results * Satisfy clippy in the version scanner and the WGSL lowering; compare cache hits by relation
112 lines
5.4 KiB
Rust
112 lines
5.4 KiB
Rust
//! The node kind versions (`src/versions.rs`, written by `build.rs`) over the evaluator's real source: that the
|
|
//! splitting works on the real code, not only on small examples. `TC_VERSIONS_DUMP=1` prints what each kind's version
|
|
//! covers. The build script's own modules are compiled here as well, so their unit tests run.
|
|
|
|
// The build script's `digest` module is declared at the test crate's root (`main.rs`), where `source` looks for it.
|
|
#[allow(dead_code)]
|
|
#[path = "../../build/source/mod.rs"]
|
|
mod source;
|
|
|
|
use source::items::Dispatcher;
|
|
use source::modules::reachable;
|
|
use source::{SourceFile, SourceTree};
|
|
use std::path::Path;
|
|
|
|
const DISPATCHERS: &[Dispatcher] = &[
|
|
Dispatcher { file: "src/graph.rs", function: "compute" },
|
|
Dispatcher { file: "src/graph.rs", function: "finish_node" },
|
|
Dispatcher { file: "src/nodes2.rs", function: "compute" },
|
|
Dispatcher { file: "src/atomic.rs", function: "compute" },
|
|
Dispatcher { file: "src/compiled_ops.rs", function: "compute" },
|
|
];
|
|
const SHARED: &[&str] = &["eval_node", "evaluate_with_inputs", "parse"];
|
|
|
|
fn rust_files(dir: &Path, root: &Path, out: &mut Vec<SourceFile>) {
|
|
for entry in std::fs::read_dir(dir).unwrap().flatten() {
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
rust_files(&path, root, out);
|
|
} else if path.extension().is_some_and(|e| e == "rs") {
|
|
let name = path.strip_prefix(root).unwrap().display().to_string().replace('\\', "/");
|
|
let text = std::fs::read_to_string(&path).unwrap();
|
|
let included = source::included_files(&text, path.parent().unwrap());
|
|
out.push(SourceFile { name, text, included });
|
|
}
|
|
}
|
|
}
|
|
|
|
/// This crate's files by their names in it (`src/graph.rs`).
|
|
fn engine() -> Vec<SourceFile> {
|
|
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
let mut files = Vec::new();
|
|
rust_files(&root.join("src"), root, &mut files);
|
|
files.sort_by(|a, b| a.name.cmp(&b.name));
|
|
files
|
|
}
|
|
|
|
#[test]
|
|
fn a_fix_in_one_operation_leaves_most_kinds_alone() {
|
|
let all = engine();
|
|
let files: Vec<SourceFile> = reachable(&all, "src", &["src/graph.rs"]).into_iter().cloned().collect();
|
|
if std::env::var_os("TC_VERSIONS_DUMP").is_some() {
|
|
println!("{} of {} files reachable", files.len(), all.len());
|
|
files.iter().for_each(|f| println!(" {}", f.name));
|
|
}
|
|
assert!(files.len() > 20, "{} of {} files reachable", files.len(), all.len());
|
|
let tree = SourceTree::scan(&files, DISPATCHERS);
|
|
let kinds: Vec<String> = tree.kinds().map(str::to_string).collect();
|
|
assert!(kinds.len() > 100, "the kinds were found: {}", kinds.len());
|
|
let versions = |t: &SourceTree| kinds.iter().map(|k| t.kind_version(k, SHARED).unwrap()).collect::<Vec<_>>();
|
|
let before = versions(&tree);
|
|
let distinct: std::collections::BTreeSet<_> = before.iter().collect();
|
|
if std::env::var_os("TC_VERSIONS_DUMP").is_some() {
|
|
for k in &kinds {
|
|
let c = tree.kind_covers(k, SHARED).unwrap();
|
|
println!("{k}\t{} functions\t{} files", c.functions, c.files.len());
|
|
}
|
|
println!("{} kinds, {} distinct versions", kinds.len(), distinct.len());
|
|
for (file, names) in tree.functions_covered(None, SHARED) {
|
|
let shown: Vec<&str> = names.iter().map(String::as_str).collect();
|
|
println!("shared {file} {}: {}", names.len(), shown.join(" "));
|
|
}
|
|
}
|
|
assert!(distinct.len() > kinds.len() / 2, "{} distinct versions of {} kinds", distinct.len(), kinds.len());
|
|
|
|
let changed_by = |file: &str, from: &str, to: &str| {
|
|
let edited: Vec<SourceFile> = files
|
|
.iter()
|
|
.map(|f| {
|
|
let text = if f.name == file { f.text.replacen(from, to, 1) } else { f.text.clone() };
|
|
assert!(f.name != file || text != f.text, "{from} is in {file}");
|
|
SourceFile { text, ..f.clone() }
|
|
})
|
|
.collect();
|
|
let after = versions(&SourceTree::scan(&edited, DISPATCHERS));
|
|
kinds.iter().zip(before.iter().zip(&after)).filter(|(_, (a, b))| a != b).map(|(k, _)| k.clone()).collect::<Vec<String>>()
|
|
};
|
|
|
|
let added = changed_by("src/compiled_ops.rs", "pub fn compute(kind: &str", "fn __added_helper() -> u8 { 1 }\npub fn compute(kind: &str");
|
|
assert_eq!(added, Vec::<String>::new(), "an added function nothing uses changes nothing");
|
|
|
|
let comment = changed_by("src/atomic.rs", "\"levels_mid\" => {", "\"levels_mid\" => { // a comment\n");
|
|
assert_eq!(comment, Vec::<String>::new(), "a comment changes nothing");
|
|
|
|
// a fix inside one node's arm
|
|
let arm = changed_by("src/atomic.rs", "\"levels_mid\" => {", "\"levels_mid\" => { let _fixed = 1;");
|
|
assert_eq!(arm, ["levels_mid"], "only that kind");
|
|
|
|
// a fix in a helper only some kinds reach: those kinds and few others
|
|
let helper = changed_by(
|
|
"src/atomic.rs",
|
|
"fn hsl_shift(p: [f32; 4], h: f32, s: f32, l: f32) -> [f32; 4] {",
|
|
"fn hsl_shift(p: [f32; 4], h: f32, s: f32, l: f32) -> [f32; 4] { let _fixed = 1;",
|
|
);
|
|
assert!(helper.contains(&"hsl_centered".to_string()), "{helper:?}");
|
|
assert!(helper.len() < 5, "{helper:?}");
|
|
|
|
// what every kind goes through changes every kind
|
|
// (the cancellation check every node evaluation makes, outside any kind's arm)
|
|
let everyone = changed_by("src/graph.rs", "if ctx.ec.is_cancelled() {", "if ctx.ec.is_cancelled() { let _changed = 1;");
|
|
assert_eq!(everyone.len(), kinds.len());
|
|
}
|