Files
craft-libs/crates/material-graph/tests/it/determinism.rs
T
Valentin Klein c3155aad96 Share the material graph runtime and WGSL pixel compiler (#3109)
* 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
2026-11-23 08:55:49 -05:00

78 lines
3.5 KiB
Rust

//! Evaluation gives the same bytes however it is run: on one worker thread or many, and from the node cache or computed
//! afresh. The graphs are the engine's own demo graphs (one per node kind, written for this repository), so nothing
//! here depends on outside data.
use craft_material_graph::context::EvalContext;
use craft_material_graph::graph::{self, Graph, NodeCache};
use craft_material_graph::tex::Tex;
use std::collections::BTreeMap;
use std::sync::Arc;
/// Rendered at a size that is not the demo graphs' own (64), so sizes scale, and large enough that the parallel loops
/// split every image into several pieces.
const SIDE: usize = 96;
fn demo_graphs() -> Vec<(&'static str, Graph)> {
graph::SPECS
.iter()
.filter_map(|s| graph::demo_graph_for(s.kind).map(|t| (s.kind, t)))
.map(|(k, t)| (k, Graph::parse(&t).unwrap_or_else(|e| panic!("the demo graph of {k} does not parse: {e}"))))
.collect()
}
fn bits(t: &Tex) -> Bits {
(t.w, t.h, t.px.iter().flat_map(|p| p.map(f32::to_bits)).collect())
}
/// An image as (width, height, the bits of every component).
type Bits = (usize, usize, Vec<u32>);
/// Every output of `g`, or its error, as bits.
fn render(g: &Graph) -> Result<BTreeMap<String, Bits>, String> {
g.evaluate(Some(SIDE)).map(|o| o.iter().map(|(k, t)| (k.clone(), bits(t))).collect()).map_err(|e| e.to_string())
}
fn in_pool<R: Send>(threads: usize, f: impl FnOnce() -> R + Send) -> R {
rayon::ThreadPoolBuilder::new().num_threads(threads).build().expect("a thread pool").install(f)
}
#[test]
fn one_thread_and_eight_render_the_same_bytes() {
let graphs = demo_graphs();
assert!(graphs.len() > 100, "only {} demo graphs", graphs.len());
let one = in_pool(1, || graphs.iter().map(|(_, g)| render(g)).collect::<Vec<_>>());
let eight = in_pool(8, || graphs.iter().map(|(_, g)| render(g)).collect::<Vec<_>>());
let differ: Vec<&str> = graphs.iter().zip(one.iter().zip(&eight)).filter(|(_, (a, b))| a != b).map(|((k, _), _)| *k).collect();
assert!(differ.is_empty(), "these kinds render differently on 1 and 8 threads: {differ:?}");
assert!(one.iter().filter(|r| r.is_ok()).count() > 100, "most demo graphs must render");
}
#[test]
fn cached_and_uncached_evaluations_give_the_same_bytes() {
let cache = Arc::new(NodeCache::new(256));
let mut differ = Vec::new();
let mut not_reused = Vec::new();
for (kind, g) in demo_graphs() {
let keep: Vec<u32> = g.nodes.keys().copied().collect();
let fresh = g.evaluate_all(SIDE, &keep);
let first = g.evaluate_all_cached(&EvalContext::default(), SIDE, &keep, &cache, None, &mut |_| {});
// the second run takes every node from the cache
let hits = cache.stats().0;
let again = g.evaluate_all_cached(&EvalContext::default(), SIDE, &keep, &cache, None, &mut |_| {});
if again.errors.len() < keep.len() && cache.stats().0 == hits {
not_reused.push(kind);
}
for r in [&first, &again] {
let same = keep.iter().all(|id| fresh.kept.get(id).map(|t| bits(t)) == r.kept.get(id).map(|t| bits(t)))
&& fresh.errors == r.errors
&& fresh.thumbs == r.thumbs;
if !same {
differ.push(kind);
}
}
cache.clear();
}
assert!(differ.is_empty(), "the cache changes these kinds: {differ:?}");
assert!(not_reused.is_empty(), "the second run took nothing from the cache for these kinds: {not_reused:?}");
}