Hash bound input-image resources into graph instance cache keys (#110)

* Hash bound input-image resources into graph instance cache keys

A graph instance can show a stale preview image after its bound bitmap resource is replaced, because the instance cache key never changes. Hashing those resources makes the instance re-cook when the bitmap changes.

Include the content hashes of resources referenced by graph.input_images (and their linear flags) in graph_tree_hash, and add a regression test that cooks an instance, replaces the bound resource bytes, and asserts the new image is cooked instead of the cached one.

Test: bound_input_image_resource_change_recooks_instance failed before the change and passes after.

* Address review for pull request 110

---------

Co-authored-by: eliets2 <287273517+eliets2@users.noreply.github.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-06 10:10:21 +01:00
1 parent 1a468e7ee0
commit 0ce7a6aafb
3 files changed
+72 -2

No files matched your search

+5
View File
@@ -52,6 +52,11 @@ pub(crate) fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 {
index.hash(&mut h);
serde_json::to_string(graph).unwrap_or_default().hash(&mut h);
for (id, res) in &graph.input_images {
pkg.resource(res).map(|r| r.bytes.content_hash()).hash(&mut h);
id.hash(&mut h);
graph.input_images_linear.contains(id).hash(&mut h);
}
for n in &graph.nodes {
if let Some(Value::Text(name)) = n.params.get("resource") {
pkg.resource(name).map(|r| r.bytes.content_hash()).hash(&mut h);
+2 -2
View File
@@ -12,9 +12,9 @@ pub fn cook(package: &Package, graph_index: usize, options: &Options, cache: &Mu
let mut cooked = Cooked::default();
let Some(graph) = package.graphs.get(graph_index) else { return cooked };
// The top-level graph's input nodes show their preview images; the caller's images win
// Input nodes show their bound preview images; the caller's images win
let bound;
let options = if options.depth == 0 && !graph.input_images.is_empty() {
let options = if !graph.input_images.is_empty() {
let mut inputs: BTreeMap<String, Image> = graph
.input_images
.iter()
+65
View File
@@ -364,3 +364,68 @@ fn linear_input_images_and_bitmap_nodes_request_data_reads() {
p.graphs[0].node_mut(b).unwrap().params.insert("colour_space".into(), Value::Enum(1));
assert_eq!(first(&p), (0.5, 0.5));
}
#[test]
fn bound_input_image_resource_change_recooks_instance() {
use materialcraft_core::{Resource, ResourceBytes};
use std::sync::atomic::AtomicU8;
let mut sub = Graph::new("sub");
sub.size = [4, 4];
let i = sub.add_node("input_grayscale", [0.0, 0.0]);
sub.node_mut(i).unwrap().params.insert("identifier".into(), Value::Text("in".into()));
let o = sub.add_node("output", [100.0, 0.0]);
sub.connect(i, "output", o, "input").unwrap();
sub.input_images.insert("in".into(), "curv".into());
let (top, inst) = instancing("top", "sub");
let mut p = Package::new("p");
p.graphs = vec![top, sub];
p.resources.push(Resource::new("curv", "curv.bin", ResourceBytes::new(vec![1; 100])));
// Stands in for decoding: the first byte picks the shade.
let shade = AtomicU8::new(1);
let res = |_: &str| Some(Image::filled(4, 4, Kind::Gray, [f32::from(shade.load(Ordering::Relaxed)) / 10.0; 4]));
let cache = Mutex::new(Cache::default());
let opts = Options::new(&res);
let first = cook(&p, 0, &opts, &cache);
assert!(first.nodes[&inst].error.is_none(), "{:?}", first.nodes[&inst].error);
assert!(!first.nodes[&inst].cached);
let unchanged = cook(&p, 0, &opts, &cache);
assert!(unchanged.nodes[&inst].cached, "an unchanged instance should be served from the cache");
p.resources[0].bytes = ResourceBytes::new(vec![2; 100]);
shade.store(2, Ordering::Relaxed);
let second = cook(&p, 0, &opts, &cache);
assert!(!second.nodes[&inst].cached, "changing the bound input image resource must recook the instance");
}
#[test]
fn bound_input_image_linear_flag_change_recooks_instance() {
use materialcraft_core::{Resource, ResourceBytes};
let mut sub = Graph::new("sub");
sub.size = [4, 4];
let i = sub.add_node("input_grayscale", [0.0, 0.0]);
sub.node_mut(i).unwrap().params.insert("identifier".into(), Value::Text("in".into()));
let o = sub.add_node("output", [100.0, 0.0]);
sub.connect(i, "output", o, "input").unwrap();
sub.input_images.insert("in".into(), "curv".into());
let (top, inst) = instancing("top", "sub");
let mut p = Package::new("p");
p.graphs = vec![top, sub];
p.resources.push(Resource::new("curv", "curv.bin", ResourceBytes::new(vec![1; 100])));
let res = |k: &str| {
let (name, linear) = materialcraft_image::split_resource_key(k);
(name == "curv").then(|| Image::filled(4, 4, Kind::Gray, [if linear { 0.5 } else { 0.2 }; 4]))
};
let cache = Mutex::new(Cache::default());
let opts = Options::new(&res);
let first = cook(&p, 0, &opts, &cache);
assert!(first.nodes[&inst].error.is_none(), "{:?}", first.nodes[&inst].error);
assert!(!first.nodes[&inst].cached);
let first_value = first.image(inst, 0).unwrap().data[0];
assert!((first_value - 0.2).abs() < 1e-4, "sRGB bound image should reach the instance, got {first_value}");
let second = cook(&p, 0, &opts, &cache);
assert!(second.nodes[&inst].cached, "an unchanged instance should be served from the cache");
p.graphs[1].input_images_linear.insert("in".into());
let third = cook(&p, 0, &opts, &cache);
assert!(!third.nodes[&inst].cached, "toggling input_images_linear must recook the instance");
let third_value = third.image(inst, 0).unwrap().data[0];
assert!((third_value - 0.5).abs() < 1e-4, "linear bound image should reach the instance, got {third_value}");
}