mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-20 06:21:05 +01:00
Fix function instance inputs and cache refresh
This commit is contained in:
1 parent
131c7b8853
commit
709cd7f86e
3 files changed
+116
-5
No files matched your search
@@ -250,6 +250,102 @@ fn unconnected_instance_inputs_use_the_source_defaults() {
|
||||
assert!(function::warnings(graph).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_instance_inputs_follow_current_source_values() {
|
||||
for linked in [false, true] {
|
||||
let mut session = Session::new(source());
|
||||
session.path = Some(std::env::temp_dir().join("materialcraft-function-input-source.sbs").to_string_lossy().into_owned());
|
||||
execute(&mut session, "graph.exposed.set", &json!({"id": "amount", "value": 0.25})).unwrap();
|
||||
if linked {
|
||||
let mut package = Package::new("Calls");
|
||||
let mut graph = Graph::new("Caller");
|
||||
graph.function = Some(FunctionGraph::default());
|
||||
package.graphs.push(graph);
|
||||
file_cmds::open_bytes(&mut session, &formats::sbs::write_sbs(&package).unwrap(), Some("function-input-target.sbs".into())).unwrap();
|
||||
} else {
|
||||
execute(&mut session, "file.new", &json!({"name": "Caller", "kind": "function"})).unwrap();
|
||||
}
|
||||
let document = session.document_id;
|
||||
let target = json!({"kind": "graph"});
|
||||
let instance = call(&mut session, 0, "Twice", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(0.5));
|
||||
|
||||
execute(&mut session, "file.new", &json!({"name": "Outer", "kind": "function"})).unwrap();
|
||||
let outer = call(&mut session, document, "Caller", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": outer}));
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Twice"})).unwrap();
|
||||
execute(&mut session, "graph.exposed.set", &json!({"id": "amount", "value": 0.75})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": document})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Outer"})).unwrap();
|
||||
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(1.5));
|
||||
assert!(function::warnings(session.graph().unwrap().function.as_ref().unwrap()).is_empty());
|
||||
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Caller"})).unwrap();
|
||||
let constant = edit(&mut session, &target, json!({"action": "add", "operation": "const_float1", "at": [0, 0]}))["id"].as_u64().unwrap();
|
||||
edit(&mut session, &target, json!({"action": "value", "id": constant, "value": Value::Float(0.125)}));
|
||||
edit(&mut session, &target, json!({"action": "connect", "from": constant, "to": instance, "input": 0}));
|
||||
execute(&mut session, "package.activate", &json!({"document": 0})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Twice"})).unwrap();
|
||||
execute(&mut session, "graph.exposed.set", &json!({"id": "amount", "value": 1.0})).unwrap();
|
||||
execute(&mut session, "package.activate", &json!({"document": document})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Outer"})).unwrap();
|
||||
|
||||
assert_eq!(evaluate(session.graph().unwrap().function.as_ref().unwrap()), Value::Float(0.25));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_instance_input_changes_recook_parameter_functions_and_processors() {
|
||||
let mut session = Session::new(source());
|
||||
execute(&mut session, "graph.exposed.set", &json!({"id": "amount", "value": 0.25})).unwrap();
|
||||
execute(&mut session, "file.new", &json!({"name": "Material", "template": "empty"})).unwrap();
|
||||
let blur = NodeId(execute(&mut session, "graph.node.add", &json!({"def": "blur"})).unwrap()["id"].as_u64().unwrap());
|
||||
execute(&mut session, "graph.node.function", &json!({"id": blur, "param": "intensity", "mode": "empty"})).unwrap();
|
||||
let processor = NodeId(execute(&mut session, "graph.node.add", &json!({"def": "value_processor"})).unwrap()["id"].as_u64().unwrap());
|
||||
for target in [json!({"kind": "dynamic", "node": blur, "parameter": "intensity"}), json!({"kind": "parameter", "node": processor, "parameter": "function"})]
|
||||
{
|
||||
let instance = call(&mut session, 0, "Twice", &target);
|
||||
edit(&mut session, &target, json!({"action": "output", "id": instance}));
|
||||
}
|
||||
session.preview_size = Some(16);
|
||||
let cooked = session.cook();
|
||||
|
||||
assert_eq!(cooked.nodes[&blur].error, None);
|
||||
assert_eq!(cooked.nodes[&blur].parameters["intensity"], Value::Float(0.5));
|
||||
assert_eq!(cooked.value(processor, 0), Some(&Value::Float(0.5)));
|
||||
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Twice"})).unwrap();
|
||||
execute(&mut session, "graph.exposed.set", &json!({"id": "amount", "value": 0.75})).unwrap();
|
||||
execute(&mut session, "graph.open", &json!({"graph": "Material"})).unwrap();
|
||||
let cooked = session.cook();
|
||||
|
||||
assert_eq!(cooked.nodes[&blur].error, None);
|
||||
assert_eq!(cooked.nodes[&blur].parameters["intensity"], Value::Float(1.5));
|
||||
assert_eq!(cooked.nodes[&processor].error, None);
|
||||
assert_eq!(cooked.value(processor, 0), Some(&Value::Float(1.5)));
|
||||
assert!(!cooked.nodes[&blur].cached);
|
||||
assert!(!cooked.nodes[&processor].cached);
|
||||
let cached = session.cook();
|
||||
assert!(cached.nodes[&blur].cached);
|
||||
assert!(cached.nodes[&processor].cached);
|
||||
assert_eq!(cached.value(processor, 0), Some(&Value::Float(1.5)));
|
||||
|
||||
let saved = formats::sbs::write_sbs(&session.pkg).unwrap();
|
||||
let mut reopened = Session::new(formats::sbs::read_sbs(&saved, "function-inputs.sbs", &|_| None).unwrap().pkg);
|
||||
execute(&mut reopened, "graph.open", &json!({"graph": "Material"})).unwrap();
|
||||
reopened.preview_size = Some(16);
|
||||
let blur = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "blur").unwrap().id;
|
||||
let processor = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "value_processor").unwrap().id;
|
||||
let cooked = reopened.cook();
|
||||
|
||||
assert_eq!(cooked.nodes[&blur].parameters["intensity"], Value::Float(1.5));
|
||||
assert_eq!(cooked.value(processor, 0), Some(&Value::Float(1.5)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connected_inputs_survive_source_interface_changes_and_missing_sources() {
|
||||
let mut session = Session::new(source());
|
||||
|
||||
@@ -94,14 +94,19 @@ pub(crate) fn variable_key(node: &Node, parameters: &BTreeMap<String, Value>, va
|
||||
let mut pending = vec![graph.as_ref()];
|
||||
while let Some(graph) = pending.pop() {
|
||||
serde_json::to_string(graph).unwrap_or_default().hash(&mut hasher);
|
||||
for function in &graph.nodes {
|
||||
match &function.source {
|
||||
for function_node in &graph.nodes {
|
||||
// Resolved call defaults are omitted from graph serialization
|
||||
if let Some(instance) = &function_node.instance {
|
||||
serde_json::to_string(&instance.defaults).unwrap_or_default().hash(&mut hasher);
|
||||
}
|
||||
|
||||
match &function_node.source {
|
||||
Some(Ok(source)) => pending.push(source.as_ref()),
|
||||
Some(Err(error)) => error.hash(&mut hasher),
|
||||
None => {}
|
||||
}
|
||||
if (function.op.starts_with("get_") || function.op == "isdefined")
|
||||
&& let Some(Value::Text(name)) = &function.value
|
||||
if (function_node.op.starts_with("get_") || function_node.op == "isdefined")
|
||||
&& let Some(Value::Text(name)) = &function_node.value
|
||||
{
|
||||
names.insert(name.as_str());
|
||||
}
|
||||
@@ -124,5 +129,6 @@ pub(crate) fn variable_key(node: &Node, parameters: &BTreeMap<String, Value>, va
|
||||
name.hash(&mut hasher);
|
||||
variables.get(name).map(|value| serde_json::to_string(value).unwrap_or_default()).hash(&mut hasher);
|
||||
}
|
||||
|
||||
hasher.finish()
|
||||
}
|
||||
@@ -24,16 +24,20 @@ fn resolve(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap<Str
|
||||
if stack.contains(&key) {
|
||||
return Err("Function Graph dependency cycle".into());
|
||||
}
|
||||
|
||||
if stack.len() >= 8 || body.nodes.len() > *remaining {
|
||||
return Err("Function instances exceed the 8-level or 4096-node limit".into());
|
||||
}
|
||||
|
||||
if graph.exposed.len() > 4096
|
||||
|| graph.exposed.iter().map(|parameter| ¶meter.id).collect::<std::collections::BTreeSet<_>>().len() != graph.exposed.len()
|
||||
{
|
||||
return Err("Function inputs must have unique identifiers and at most 4096 entries".into());
|
||||
}
|
||||
|
||||
let previous: BTreeMap<_, _> = instance.inputs.iter().zip(&node.inputs).map(|(port, value)| (port.identifier.clone(), *value)).collect();
|
||||
instance.defaults = graph.exposed.iter().map(|parameter| (parameter.id.clone(), parameter.default.clone())).collect();
|
||||
instance.defaults =
|
||||
graph.exposed.iter().map(|parameter| (parameter.id.clone(), graph.input_value(¶meter.id).unwrap_or(¶meter.default).clone())).collect();
|
||||
instance.inputs = graph
|
||||
.exposed
|
||||
.iter()
|
||||
@@ -47,15 +51,19 @@ fn resolve(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap<Str
|
||||
for (name, value) in &previous {
|
||||
if value.is_some() && !instance.inputs.iter().any(|port| port.identifier == *name) {
|
||||
instance.inputs.push(GraphPort { identifier: name.clone(), type_code: 0 });
|
||||
|
||||
// A legacy connector may repeat a declared wire without supplying another argument
|
||||
conflicting_input |= value.is_some_and(|source| !declared_sources.contains(&source));
|
||||
}
|
||||
}
|
||||
|
||||
node.inputs = instance.inputs.iter().map(|port| previous.get(&port.identifier).copied().flatten()).collect();
|
||||
|
||||
let mut body = body.clone();
|
||||
stack.push(key);
|
||||
resolve(&mut body, source, sources, stack, remaining);
|
||||
stack.pop();
|
||||
|
||||
let output = super::output_node(&body).ok_or_else(|| format!("{}: no output node defined", instance.graph))?;
|
||||
let types = super::node_types(&body)?;
|
||||
let type_code = types.get(&output.id).copied().unwrap_or(256);
|
||||
@@ -64,6 +72,7 @@ fn resolve(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap<Str
|
||||
if conflicting_input {
|
||||
return Err(format!("{}: a connected input no longer exists in the source", instance.graph));
|
||||
}
|
||||
|
||||
Ok(Arc::new(body))
|
||||
})();
|
||||
node.source = Some(resolved);
|
||||
|
||||
Reference in new issue
Block a user