mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-02 00:01:59 -10:00
* Retire the rust-gpu twins of the adjustment nodes, leaving rust-gpu dormant * Remove the dormant rust-gpu shader crate and everything that supported it
1483 lines
56 KiB
Rust
1483 lines
56 KiB
Rust
use crate::document::value;
|
|
use crate::document::value::TaggedValue;
|
|
use crate::document::{NodeId, OriginalLocation};
|
|
pub use core_types::registry::*;
|
|
use core_types::*;
|
|
use rustc_hash::FxHashMap;
|
|
use std::borrow::Cow;
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::fmt::Debug;
|
|
use std::hash::Hash;
|
|
|
|
#[derive(Debug, Default, PartialEq, Clone, Eq, serde::Serialize, serde::Deserialize)]
|
|
/// A list of [`ProtoNode`]s, which is an intermediate step between the [`crate::document::NodeNetwork`] and the `BorrowTree` containing a single flattened network.
|
|
pub struct ProtoNetwork {
|
|
// TODO: remove this since it seems to be unused?
|
|
// Should a proto Network even allow inputs? Don't think so
|
|
pub inputs: Vec<NodeId>,
|
|
/// The node ID that provides the output. This node is then responsible for calling the rest of the graph.
|
|
pub output: NodeId,
|
|
/// A list of nodes stored in a Vec to allow for sorting.
|
|
pub nodes: Vec<(NodeId, ProtoNode)>,
|
|
}
|
|
|
|
impl core::fmt::Display for ProtoNetwork {
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
f.write_str("Proto Network with nodes: ")?;
|
|
fn write_node(f: &mut core::fmt::Formatter<'_>, network: &ProtoNetwork, id: NodeId, indent: usize) -> core::fmt::Result {
|
|
f.write_str(&"\t".repeat(indent))?;
|
|
let Some((_, node)) = network.nodes.iter().find(|(node_id, _)| *node_id == id) else {
|
|
return f.write_str("{{Unknown Node}}");
|
|
};
|
|
f.write_str("Node: ")?;
|
|
f.write_str(node.identifier.as_str())?;
|
|
|
|
f.write_str("\n")?;
|
|
f.write_str(&"\t".repeat(indent))?;
|
|
f.write_str("{\n")?;
|
|
|
|
f.write_str(&"\t".repeat(indent + 1))?;
|
|
f.write_str("Input: ")?;
|
|
f.write_fmt(format_args!("Call Argument (type = {:?})", node.call_argument))?;
|
|
f.write_str("\n")?;
|
|
|
|
match &node.construction_args {
|
|
ConstructionArgs::Value(value) => {
|
|
f.write_str(&"\t".repeat(indent + 1))?;
|
|
f.write_fmt(format_args!("Value construction argument: {value:?}"))?
|
|
}
|
|
ConstructionArgs::Nodes(nodes) => {
|
|
for id in nodes {
|
|
write_node(f, network, *id, indent + 1)?;
|
|
}
|
|
}
|
|
}
|
|
f.write_str(&"\t".repeat(indent))?;
|
|
f.write_str("}\n")?;
|
|
Ok(())
|
|
}
|
|
|
|
let id = self.output;
|
|
write_node(f, self, id, 0)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
|
/// Defines the arguments used to construct the boxed node struct. This is used to call the constructor function in the `node_registry.rs` file - which is hidden behind a wall of macros.
|
|
pub enum ConstructionArgs {
|
|
/// A value of a type that is known, allowing serialization (serde::Deserialize is not object safe)
|
|
Value(MemoHash<value::TaggedValue>),
|
|
/// A list of nodes used as inputs to the constructor function in `node_registry.rs`.
|
|
/// The bool indicates whether to treat the node as lambda node.
|
|
// TODO: use a struct for clearer naming.
|
|
Nodes(Vec<NodeId>),
|
|
}
|
|
|
|
impl Eq for ConstructionArgs {}
|
|
|
|
impl PartialEq for ConstructionArgs {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
match (&self, &other) {
|
|
(Self::Nodes(n1), Self::Nodes(n2)) => n1 == n2,
|
|
(Self::Value(v1), Self::Value(v2)) => v1 == v2,
|
|
_ => {
|
|
use std::hash::Hasher;
|
|
let hash = |input: &Self| {
|
|
let mut hasher = rustc_hash::FxHasher::default();
|
|
input.cache_hash(&mut hasher);
|
|
hasher.finish()
|
|
};
|
|
hash(self) == hash(other)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl CacheHash for ConstructionArgs {
|
|
fn cache_hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
core::mem::discriminant(self).hash(state);
|
|
match self {
|
|
Self::Nodes(nodes) => {
|
|
for node in nodes {
|
|
node.hash(state);
|
|
}
|
|
}
|
|
Self::Value(value) => value.cache_hash(state),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ConstructionArgs {
|
|
pub fn new_function_args(&self) -> Vec<String> {
|
|
match self {
|
|
ConstructionArgs::Nodes(nodes) => nodes.iter().map(|n| format!("n{:0x}", n.0)).collect(),
|
|
ConstructionArgs::Value(value) => vec![value.to_primitive_string()],
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
/// A proto node is an intermediate step between the `DocumentNode` and the boxed struct that actually runs the node (found in the [`BorrowTree`]).
|
|
/// At different stages in the compilation process, this struct will be transformed into a reduced (more restricted) form acting as a subset of its original form, but that restricted form is still valid in the earlier stage in the compilation process before it was transformed.
|
|
pub struct ProtoNode {
|
|
pub construction_args: ConstructionArgs,
|
|
pub call_argument: Type,
|
|
pub identifier: ProtoNodeIdentifier,
|
|
pub original_location: OriginalLocation,
|
|
pub skip_deduplication: bool,
|
|
pub(crate) compile_metadata: NodeCompileMetadata,
|
|
/// The context features read by this node and everything upstream of it.
|
|
pub upstream_context_features: ContextFeatures,
|
|
}
|
|
|
|
impl Default for ProtoNode {
|
|
fn default() -> Self {
|
|
Self {
|
|
identifier: graphene_core::ops::passthrough::IDENTIFIER,
|
|
construction_args: ConstructionArgs::Value(value::TaggedValue::Integer(0).into()),
|
|
call_argument: concrete!(()),
|
|
original_location: OriginalLocation::default(),
|
|
skip_deduplication: false,
|
|
compile_metadata: Default::default(),
|
|
upstream_context_features: ContextFeatures::empty(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ProtoNode {
|
|
/// A stable node ID is a hash of a node that should stay constant. This is used in order to remove duplicates from the graph.
|
|
/// In the case of `skip_deduplication`, the `document_node_path` is also hashed in order to avoid duplicate monitor nodes from being removed (which would make it impossible to load thumbnails).
|
|
pub fn stable_node_id(&self) -> Option<NodeId> {
|
|
use std::hash::Hasher;
|
|
let mut hasher = rustc_hash::FxHasher::default();
|
|
|
|
self.identifier.as_str().hash(&mut hasher);
|
|
self.construction_args.cache_hash(&mut hasher);
|
|
if self.skip_deduplication {
|
|
self.original_location.path.hash(&mut hasher);
|
|
}
|
|
|
|
std::mem::discriminant(&self.call_argument).hash(&mut hasher);
|
|
self.call_argument.hash(&mut hasher);
|
|
|
|
Some(NodeId(hasher.finish()))
|
|
}
|
|
|
|
/// Construct a new [`ProtoNode`] with the specified construction args and a `ClonedNode` implementation.
|
|
pub fn value(value: ConstructionArgs, path: Vec<NodeId>) -> Self {
|
|
Self {
|
|
identifier: ProtoNodeIdentifier::new("core_types::value::ClonedNode"),
|
|
construction_args: value,
|
|
call_argument: concrete!(Context),
|
|
original_location: OriginalLocation {
|
|
path: Some(path),
|
|
..Default::default()
|
|
},
|
|
skip_deduplication: false,
|
|
compile_metadata: Default::default(),
|
|
upstream_context_features: ContextFeatures::empty(),
|
|
}
|
|
}
|
|
|
|
/// Converts all references to other node IDs into new IDs by running the specified function on them.
|
|
/// This can be used when changing the IDs of the nodes, for example in the case of generating stable IDs.
|
|
pub fn map_ids(&mut self, f: impl Fn(NodeId) -> NodeId) {
|
|
if let ConstructionArgs::Nodes(ids) = &mut self.construction_args {
|
|
ids.iter_mut().for_each(|id| *id = f(*id));
|
|
}
|
|
}
|
|
|
|
pub fn unwrap_construction_nodes(&self) -> Vec<NodeId> {
|
|
match &self.construction_args {
|
|
ConstructionArgs::Nodes(nodes) => nodes.clone(),
|
|
_ => panic!("tried to unwrap nodes from non node construction args \n node: {self:#?}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
enum NodeState {
|
|
Unvisited,
|
|
Visiting,
|
|
Visited,
|
|
}
|
|
|
|
impl ProtoNetwork {
|
|
#[cfg(debug_assertions)]
|
|
pub fn example() -> (Self, NodeId, ProtoNode) {
|
|
let node_id = NodeId(1);
|
|
let proto_node = ProtoNode::default();
|
|
let proto_network = ProtoNetwork {
|
|
inputs: vec![node_id],
|
|
output: node_id,
|
|
nodes: vec![(node_id, proto_node.clone())],
|
|
};
|
|
(proto_network, node_id, proto_node)
|
|
}
|
|
|
|
/// Construct a hashmap containing a list of the nodes that depend on this proto network.
|
|
pub fn collect_outwards_edges(&self) -> HashMap<NodeId, Vec<NodeId>> {
|
|
// Collecting the nodes beforehand avoids expensive rehashing (although it will create some unnecessary empty entries)
|
|
let mut edges: HashMap<NodeId, Vec<NodeId>> = self.nodes.iter().map(|(id, _)| (*id, Vec::new())).collect();
|
|
for (id, node) in &self.nodes {
|
|
if let ConstructionArgs::Nodes(ref_nodes) = &node.construction_args {
|
|
for ref_id in ref_nodes {
|
|
debug_assert!(
|
|
edges.contains_key(ref_id),
|
|
"Node with ID {id} has a reference which uses the node with ID {ref_id} which doesn't exist in network {self:#?}"
|
|
);
|
|
edges.entry(*ref_id).or_default().push(*id)
|
|
}
|
|
}
|
|
}
|
|
edges
|
|
}
|
|
|
|
/// Convert all node IDs to be stable (based on the hash generated by [`ProtoNode::stable_node_id`]).
|
|
/// This function requires that the graph be topologically sorted.
|
|
pub fn generate_stable_node_ids(&mut self) {
|
|
debug_assert!(self.is_topologically_sorted());
|
|
let outwards_edges = self.collect_outwards_edges();
|
|
|
|
for index in 0..self.nodes.len() {
|
|
let Some(sni) = self.nodes[index].1.stable_node_id() else {
|
|
panic!("failed to generate stable node id for node {:#?}", self.nodes[index].1);
|
|
};
|
|
self.replace_node_id(&outwards_edges, NodeId(index as u64), sni);
|
|
self.nodes[index].0 = sni;
|
|
}
|
|
}
|
|
|
|
fn collect_inwards_edges_with_mapping(&self) -> (Vec<Vec<usize>>, FxHashMap<NodeId, usize>) {
|
|
let id_map: FxHashMap<_, _> = self.nodes.iter().enumerate().map(|(idx, (id, _))| (*id, idx)).collect();
|
|
|
|
// Collect inwards edges using dense indices
|
|
let mut inwards_edges = vec![Vec::new(); self.nodes.len()];
|
|
for (node_id, node) in &self.nodes {
|
|
let node_index = id_map[node_id];
|
|
|
|
if let ConstructionArgs::Nodes(ref_nodes) = &node.construction_args {
|
|
for ref_id in ref_nodes {
|
|
debug_assert!(
|
|
id_map.get(ref_id).and_then(|&index| self.nodes.get(index)).is_some_and(|(id, _)| id == ref_id),
|
|
"Node with ID {node_id} has a reference which uses the node with ID {ref_id} which doesn't exist in network {self:#?}"
|
|
);
|
|
inwards_edges[node_index].push(id_map[ref_id]);
|
|
}
|
|
}
|
|
}
|
|
|
|
(inwards_edges, id_map)
|
|
}
|
|
|
|
/// Inserts context nullification nodes to optimize caching.
|
|
/// This analysis is performed after topological sorting to ensure proper dependency tracking.
|
|
pub fn insert_context_nullification_nodes(&mut self) -> Result<(), String> {
|
|
// Perform topological sort once
|
|
self.reorder_ids()?;
|
|
|
|
let (output_dependencies, _) = self.find_context_dependencies(self.output, &mut FxHashMap::default());
|
|
self.nodes[self.output.0 as usize].1.upstream_context_features = output_dependencies.features;
|
|
|
|
// Perform topological sort a second time to integrate the new nodes
|
|
self.reorder_ids()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn footprint_invariance_violations(&self) -> GraphErrors {
|
|
self.nodes
|
|
.iter()
|
|
.filter(|(_, node)| node.compile_metadata.footprint_invariant && node.upstream_context_features.contains(ContextFeatures::FOOTPRINT))
|
|
.map(|(_, node)| GraphError::new(node, GraphErrorType::FootprintDependentInput))
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn non_constant_inputs(&self) -> GraphErrors {
|
|
self.nodes
|
|
.iter()
|
|
.filter(|(_, node)| node.compile_metadata.constant_inputs != 0)
|
|
.flat_map(|(_, node)| {
|
|
let inputs = match &node.construction_args {
|
|
ConstructionArgs::Nodes(inputs) => inputs.as_slice(),
|
|
_ => &[],
|
|
};
|
|
inputs
|
|
.iter()
|
|
.enumerate()
|
|
.filter(|&(index, &input)| node.compile_metadata.requires_constant_input(index) && !self.resolves_to_value(input))
|
|
.map(|(index, _)| GraphError::new(node, GraphErrorType::NonConstantInput { index }))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn resolves_to_value(&self, mut id: NodeId) -> bool {
|
|
loop {
|
|
let Some((node_id, node)) = self.nodes.get(id.0 as usize) else { return false };
|
|
debug_assert_eq!(*node_id, id, "Node IDs must be dense indices");
|
|
|
|
match &node.construction_args {
|
|
ConstructionArgs::Value(_) => return true,
|
|
ConstructionArgs::Nodes(inputs) if node.compile_metadata.forwards_input => match inputs.first() {
|
|
Some(&input) => id = input,
|
|
None => return false,
|
|
},
|
|
_ => return false,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn insert_context_nullification_node(&mut self, node_id: NodeId, context_deps: ContextModification) -> NodeId {
|
|
let (_, node) = &self.nodes[node_id.0 as usize];
|
|
let mut path = node.original_location.path.clone();
|
|
|
|
// Add a path extension with a placeholder value which should not conflict with existing paths
|
|
if let Some(p) = path.as_mut() {
|
|
p.push(NodeId(10))
|
|
}
|
|
|
|
let features = context_deps.features;
|
|
let forwarding = NodeCompileMetadata {
|
|
forwards_input: true,
|
|
..Default::default()
|
|
};
|
|
let memoize_node_id = NodeId(self.nodes.len() as u64);
|
|
|
|
self.nodes.push((
|
|
memoize_node_id,
|
|
ProtoNode {
|
|
construction_args: ConstructionArgs::Nodes(vec![node_id]),
|
|
call_argument: concrete!(Context),
|
|
identifier: graphene_core::memo::memoize::IDENTIFIER,
|
|
original_location: OriginalLocation {
|
|
path: path.clone(),
|
|
..Default::default()
|
|
},
|
|
compile_metadata: forwarding,
|
|
upstream_context_features: features,
|
|
..Default::default()
|
|
},
|
|
));
|
|
|
|
let nullification_value_node_id = NodeId(self.nodes.len() as u64);
|
|
|
|
self.nodes.push((
|
|
nullification_value_node_id,
|
|
ProtoNode {
|
|
construction_args: ConstructionArgs::Value(MemoHash::new(TaggedValue::ContextModification(context_deps))),
|
|
call_argument: concrete!(Context),
|
|
identifier: ProtoNodeIdentifier::new("core_types::value::ClonedNode"),
|
|
original_location: OriginalLocation {
|
|
path: path.clone(),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
},
|
|
));
|
|
let nullification_node_id = NodeId(self.nodes.len() as u64);
|
|
self.nodes.push((
|
|
nullification_node_id,
|
|
ProtoNode {
|
|
construction_args: ConstructionArgs::Nodes(vec![memoize_node_id, nullification_value_node_id]),
|
|
call_argument: concrete!(Context),
|
|
identifier: graphene_core::context_modification::context_modification::IDENTIFIER,
|
|
original_location: OriginalLocation {
|
|
path: path.clone(),
|
|
..Default::default()
|
|
},
|
|
compile_metadata: forwarding,
|
|
upstream_context_features: features,
|
|
..Default::default()
|
|
},
|
|
));
|
|
nullification_node_id
|
|
}
|
|
|
|
pub fn source_ids(&self) -> impl Iterator<Item = &SourceId> {
|
|
self.nodes.iter().filter_map(|(_, node)| match &node.construction_args {
|
|
ConstructionArgs::Value(value) => match &**value {
|
|
TaggedValue::SourceId(source) => Some(source),
|
|
_ => None,
|
|
},
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
fn find_context_dependencies(&mut self, id: NodeId, analyzed: &mut FxHashMap<NodeId, (ContextModification, Option<NodeId>)>) -> (ContextModification, Option<NodeId>) {
|
|
if let Some(result) = analyzed.get(&id) {
|
|
return result.clone();
|
|
}
|
|
|
|
let result = self.analyze_context_dependencies(id, analyzed);
|
|
analyzed.insert(id, result.clone());
|
|
result
|
|
}
|
|
|
|
fn analyze_context_dependencies(&mut self, id: NodeId, analyzed: &mut FxHashMap<NodeId, (ContextModification, Option<NodeId>)>) -> (ContextModification, Option<NodeId>) {
|
|
let mut branch_dependencies = Vec::new();
|
|
let mut combined_deps = ContextModification::default();
|
|
let node_index = id.0 as usize;
|
|
|
|
let dependencies = self.nodes[node_index].1.compile_metadata;
|
|
|
|
let mut inputs = match &self.nodes[node_index].1.construction_args {
|
|
ConstructionArgs::Nodes(items) => items.clone(),
|
|
// We pretend like we have already placed context modification nodes after ourselves because value nodes don't need to be cached
|
|
ConstructionArgs::Value(value) if let TaggedValue::SourceId(source) = **value => {
|
|
return (
|
|
ContextModification {
|
|
features: dependencies.extract,
|
|
sources: [source].into(),
|
|
},
|
|
Some(id),
|
|
);
|
|
}
|
|
ConstructionArgs::Value(_) => return (dependencies.extract.into(), Some(id)),
|
|
};
|
|
|
|
// Compute the dependencies for each branch and combine all of them
|
|
for &node in &inputs {
|
|
let branch = self.find_context_dependencies(node, analyzed);
|
|
self.nodes[node.0 as usize].1.upstream_context_features = branch.0.features;
|
|
|
|
combined_deps.features |= branch.0.features;
|
|
combined_deps.sources.extend(&branch.0.sources);
|
|
branch_dependencies.push(branch);
|
|
}
|
|
// Remove requirements which this node provides and add requirements we have
|
|
let features = (combined_deps.features & !dependencies.inject) | dependencies.extract;
|
|
|
|
// If we either introduce new dependencies, we can cache all children which don't yet need that dependency
|
|
let we_introduces_new_deps = !combined_deps.features.contains(features);
|
|
|
|
// For diverging branches, we can add a cache node for all branches which don't reqire all dependencies
|
|
for (child_node, (deps, new_id)) in inputs.iter_mut().zip(branch_dependencies) {
|
|
if let Some(new_id) = new_id {
|
|
*child_node = new_id;
|
|
} else if we_introduces_new_deps || deps != combined_deps {
|
|
*child_node = self.insert_context_nullification_node(*child_node, deps);
|
|
}
|
|
}
|
|
self.nodes[node_index].1.construction_args = ConstructionArgs::Nodes(inputs);
|
|
|
|
// Which dependencies do we supply (and don't need ourselves)?
|
|
let net_injections = dependencies.inject.difference(dependencies.extract);
|
|
|
|
// Which dependencies still need to be met after this node?
|
|
let remaining_deps_from_children = combined_deps.features.difference(net_injections);
|
|
|
|
// Do we satisfy any existing dependencies?
|
|
let we_supply_existing_deps = !combined_deps.features.difference(remaining_deps_from_children).is_empty();
|
|
|
|
let new_deps = ContextModification {
|
|
features,
|
|
sources: combined_deps.sources,
|
|
};
|
|
|
|
let mut new_id = None;
|
|
if we_supply_existing_deps {
|
|
// Our set of context dependencies has shrunk so we can add a cache node after the current node
|
|
new_id = Some(self.insert_context_nullification_node(id, new_deps.clone()));
|
|
}
|
|
|
|
(new_deps, new_id)
|
|
}
|
|
|
|
/// Update all of the references to a node ID in the graph with a new ID named `compose_node_id`.
|
|
fn replace_node_id(&mut self, outwards_edges: &HashMap<NodeId, Vec<NodeId>>, node_id: NodeId, replacement_node_id: NodeId) {
|
|
// Update references in other nodes to use the new node
|
|
if let Some(referring_nodes) = outwards_edges.get(&node_id) {
|
|
for &referring_node_id in referring_nodes {
|
|
let (_, referring_node) = &mut self.nodes[referring_node_id.0 as usize];
|
|
referring_node.map_ids(|id| if id == node_id { replacement_node_id } else { id })
|
|
}
|
|
}
|
|
|
|
if self.output == node_id {
|
|
self.output = replacement_node_id;
|
|
}
|
|
|
|
self.inputs.iter_mut().for_each(|id| {
|
|
if *id == node_id {
|
|
*id = replacement_node_id;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Based on https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
|
|
// This approach excludes nodes that are not connected
|
|
pub fn topological_sort(&self) -> Result<(Vec<NodeId>, FxHashMap<NodeId, usize>), String> {
|
|
let (inwards_edges, id_map) = self.collect_inwards_edges_with_mapping();
|
|
let mut sorted = Vec::with_capacity(self.nodes.len());
|
|
let mut stack = vec![id_map[&self.output]];
|
|
let mut state = vec![NodeState::Unvisited; self.nodes.len()];
|
|
|
|
while let Some(&node_index) = stack.last() {
|
|
match state[node_index] {
|
|
NodeState::Unvisited => {
|
|
state[node_index] = NodeState::Visiting;
|
|
for &dep_index in inwards_edges[node_index].iter().rev() {
|
|
match state[dep_index] {
|
|
NodeState::Visiting => {
|
|
return Err(format!("Cycle detected involving node {}", self.nodes[dep_index].0));
|
|
}
|
|
NodeState::Unvisited => {
|
|
stack.push(dep_index);
|
|
}
|
|
NodeState::Visited => {}
|
|
}
|
|
}
|
|
}
|
|
NodeState::Visiting => {
|
|
stack.pop();
|
|
state[node_index] = NodeState::Visited;
|
|
sorted.push(NodeId(node_index as u64));
|
|
}
|
|
NodeState::Visited => {
|
|
stack.pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok((sorted, id_map))
|
|
}
|
|
|
|
fn is_topologically_sorted(&self) -> bool {
|
|
let mut visited = HashSet::new();
|
|
|
|
for (id, node) in &self.nodes {
|
|
if let ConstructionArgs::Nodes(ref_nodes) = &node.construction_args {
|
|
for &dependency in ref_nodes {
|
|
if !visited.contains(&dependency) {
|
|
dbg!(id, dependency);
|
|
dbg!(&visited);
|
|
dbg!(&self.nodes);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
visited.insert(*id);
|
|
}
|
|
true
|
|
}
|
|
|
|
/// Sort the nodes vec so it is in a topological order. This ensures that no node takes an input from a node that is found later in the list.
|
|
fn reorder_ids(&mut self) -> Result<(), String> {
|
|
let (order, _id_map) = self.topological_sort()?;
|
|
|
|
// // Map of node ids to their current index in the nodes vector
|
|
// let current_positions: FxHashMap<_, _> = self.nodes.iter().enumerate().map(|(pos, (id, _))| (*id, pos)).collect();
|
|
|
|
// // Map of node ids to their new index based on topological order
|
|
let new_positions: FxHashMap<_, _> = order.iter().enumerate().map(|(pos, id)| (self.nodes[id.0 as usize].0, pos)).collect();
|
|
// assert_eq!(id_map, current_positions);
|
|
|
|
// Create a new nodes vector based on the topological order
|
|
|
|
let mut new_nodes = Vec::with_capacity(order.len());
|
|
for (index, &id) in order.iter().enumerate() {
|
|
let mut node = std::mem::take(&mut self.nodes[id.0 as usize].1);
|
|
// Update node references to reflect the new order
|
|
node.map_ids(|id| NodeId(*new_positions.get(&id).expect("node not found in lookup table") as u64));
|
|
new_nodes.push((NodeId(index as u64), node));
|
|
}
|
|
|
|
// Update node references to reflect the new order
|
|
// new_nodes.iter_mut().for_each(|(_, node)| {
|
|
// node.map_ids(|id| *new_positions.get(&id).expect("node not found in lookup table"), false);
|
|
// });
|
|
|
|
// Update the nodes vector and other references
|
|
self.nodes = new_nodes;
|
|
self.inputs = self.inputs.iter().filter_map(|id| new_positions.get(id).map(|x| NodeId(*x as u64))).collect();
|
|
self.output = NodeId(*new_positions.get(&self.output).unwrap() as u64);
|
|
|
|
assert_eq!(order.len(), self.nodes.len());
|
|
Ok(())
|
|
}
|
|
}
|
|
#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub enum GraphErrorType {
|
|
NodeNotFound(NodeId),
|
|
UnexpectedGenerics {
|
|
index: usize,
|
|
inputs: Vec<Type>,
|
|
},
|
|
NoImplementations,
|
|
NoConstructor,
|
|
/// The `inputs` represents a formatted list of input indices corresponding to their types.
|
|
/// Each element in `error_inputs` represents a valid `NodeIOTypes` implementation.
|
|
/// The inner Vec stores the inputs which need to be changed and what type each needs to be changed to.
|
|
InvalidImplementations {
|
|
inputs: String,
|
|
error_inputs: Vec<Vec<(usize, (Type, Type))>>,
|
|
},
|
|
MultipleImplementations {
|
|
inputs: String,
|
|
valid: Vec<NodeIOTypes>,
|
|
},
|
|
FootprintDependentInput,
|
|
NonConstantInput {
|
|
index: usize,
|
|
},
|
|
}
|
|
impl Debug for GraphErrorType {
|
|
// TODO: format with the document graph context so the input index is the same as in the graph UI.
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
GraphErrorType::NodeNotFound(id) => write!(f, "Input node {id} is not present in the typing context"),
|
|
GraphErrorType::UnexpectedGenerics { index, inputs } => write!(f, "Generic inputs should not exist but found at {index}: {inputs:?}"),
|
|
GraphErrorType::NoImplementations => write!(f, "No implementations found"),
|
|
GraphErrorType::NoConstructor => write!(f, "No construct found for node"),
|
|
GraphErrorType::InvalidImplementations { inputs, error_inputs } => {
|
|
let format_error = |(index, (found, expected)): &(usize, (Type, Type))| {
|
|
let index = index + 1;
|
|
format!(
|
|
"\
|
|
• Input {index}:\n\
|
|
…found: {found}\n\
|
|
…expected: {expected}\
|
|
"
|
|
)
|
|
};
|
|
let format_error_list = |errors: &Vec<(usize, (Type, Type))>| errors.iter().map(format_error).collect::<Vec<_>>().join("\n");
|
|
let mut errors = error_inputs.iter().map(format_error_list).collect::<Vec<_>>();
|
|
errors.sort();
|
|
let errors = errors.join("\n");
|
|
let incompatibility = if errors.chars().filter(|&c| c == '•').count() == 1 {
|
|
"This input type is incompatible:"
|
|
} else {
|
|
"These input types are incompatible:"
|
|
};
|
|
|
|
write!(
|
|
f,
|
|
"\
|
|
{incompatibility}\n\
|
|
{errors}\n\
|
|
\n\
|
|
The node is currently receiving all of the following input types:\n\
|
|
{inputs}\n\
|
|
This is not a supported arrangement of types for the node.\
|
|
"
|
|
)
|
|
}
|
|
GraphErrorType::MultipleImplementations { inputs, valid } => write!(f, "Multiple implementations found ({inputs}):\n{valid:#?}"),
|
|
GraphErrorType::FootprintDependentInput => write!(
|
|
f,
|
|
"Warning: an upstream node adapts to the visible canvas area, so this node's output changes when the view is panned or zoomed"
|
|
),
|
|
GraphErrorType::NonConstantInput { index } => write!(f, "Input {} must be a fixed value rather than computed by other nodes", index + 1),
|
|
}
|
|
}
|
|
}
|
|
#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct GraphError {
|
|
pub node_path: Vec<NodeId>,
|
|
pub identifier: Cow<'static, str>,
|
|
pub error: GraphErrorType,
|
|
}
|
|
impl GraphError {
|
|
pub fn new(node: &ProtoNode, text: impl Into<GraphErrorType>) -> Self {
|
|
Self {
|
|
node_path: node.original_location.path.clone().unwrap_or_default(),
|
|
identifier: Cow::Owned(node.identifier.as_str().to_string()),
|
|
error: text.into(),
|
|
}
|
|
}
|
|
}
|
|
impl Debug for GraphError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("GraphError")
|
|
.field("node_path", &self.node_path.iter().map(|id| id.0).collect::<Vec<_>>())
|
|
.field("identifier", &self.identifier.to_string())
|
|
.field("error", &self.error)
|
|
.finish()
|
|
}
|
|
}
|
|
pub type GraphErrors = Vec<GraphError>;
|
|
|
|
/// The `TypingContext` is used to store the types of the nodes indexed by their stable node id.
|
|
#[derive(Default, Clone, dyn_any::DynAny)]
|
|
pub struct TypingContext {
|
|
lookup: Cow<'static, HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeConstructor>>>,
|
|
inferred: HashMap<NodeId, NodeIOTypes>,
|
|
constructor: HashMap<NodeId, NodeConstructor>,
|
|
promotions: HashMap<NodeId, Vec<(usize, Promotion)>>,
|
|
}
|
|
|
|
/// Checks if a proposed input to a particular (primary or secondary) input connector is valid for its type signature.
|
|
/// `from` indicates the value given to a input, `to` indicates the input's allowed type as specified by its type signature.
|
|
fn valid_type(from: &Type, to: &Type) -> bool {
|
|
match (from, to) {
|
|
// Direct comparison of two concrete types.
|
|
(Type::Concrete(type1), Type::Concrete(type2)) => type1 == type2,
|
|
// Direct comparison of two function types.
|
|
// Note: in the presence of subtyping, functions are considered on a "greater than or equal to" basis of its function type's generality.
|
|
// That means we compare their types with a contravariant relationship, which means that a more general type signature may be substituted for a more specific type signature.
|
|
// For example, we allow `T -> V` to be substituted with `T' -> V` or `() -> V` where T' and () are more specific than T.
|
|
// This allows us to supply anything to a function that is satisfied with `()`.
|
|
// In other words, we are implementing these two relations, where the >= operator means that the left side is more general than the right side:
|
|
// - `T >= T' ⇒ (T' -> V) >= (T -> V)` (functions are contravariant in their input types)
|
|
// - `V >= V' ⇒ (T -> V) >= (T -> V')` (functions are covariant in their output types)
|
|
// While these two relations aren't a truth about the universe, they are a design decision that we are employing in our language design that is also common in other languages.
|
|
// For example, Rust implements these same relations as it describes here: <https://doc.rust-lang.org/nomicon/subtyping.html>
|
|
// Graphite doesn't have subtyping currently, but it used to have it, and may do so again, so we make sure to compare types in this way to make things easier.
|
|
// More details explained here: <https://github.com/GraphiteEditor/Graphite/issues/1741>
|
|
(Type::Fn(in1, out1), Type::Fn(in2, out2)) => valid_type(out2, out1) && valid_type(in1, in2),
|
|
// Ranked wrappers of the same rank are compared element-wise
|
|
(Type::Item(element1), Type::Item(element2)) => valid_type(element1, element2),
|
|
(Type::List(element1), Type::List(element2)) => valid_type(element1, element2),
|
|
// If either the proposed input or the allowed input are generic, we allow the substitution (meaning this is a valid subtype).
|
|
// TODO: Add proper generic counting which is not based on the name
|
|
(Type::Generic(_), _) | (_, Type::Generic(_)) => true,
|
|
// Reject unknown type relationships.
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// A rank adapter which type resolution marks for insertion between a wire and a connector whose ranks differ,
|
|
/// carrying the element type the adapter is registered under.
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum Promotion {
|
|
/// Raises an `Item<X>` wire onto a `List<X>` connector as a one-element list.
|
|
ItemToList(Type),
|
|
/// Bundles a whole `List<X>` wire into one opaque `Item<Bundle<X>>` cell.
|
|
Bundle(Type),
|
|
/// Unbundles an `Item<Bundle<X>>` wire back into the whole `List<X>`.
|
|
Unbundle(Type),
|
|
}
|
|
|
|
/// Builds a [`Promotion`] variant from its element type.
|
|
type PromotionVariant = fn(Type) -> Promotion;
|
|
|
|
impl Promotion {
|
|
/// The promotion that lets a wire of the `from` value type feed a connector of the `to` value type of a different rank, if there is one.
|
|
pub fn between(from: &Type, to: &Type) -> Option<Self> {
|
|
Self::variant_between(from, to).map(|(variant, element)| variant(element.clone()))
|
|
}
|
|
|
|
/// Whether a promotion lets a wire of the `from` value type feed a connector of the `to` value type, without building it.
|
|
pub fn exists_between(from: &Type, to: &Type) -> bool {
|
|
Self::variant_between(from, to).is_some()
|
|
}
|
|
|
|
/// The variant and element type of the promotion between two value types. Every rank relation is matched structurally,
|
|
/// and only the `Bundle` layer inside an `Item` cell remains name-encoded.
|
|
fn variant_between<'a>(from: &'a Type, to: &'a Type) -> Option<(PromotionVariant, &'a Type)> {
|
|
fn concrete_name(ty: &Type) -> Option<&str> {
|
|
match ty {
|
|
Type::Concrete(descriptor) => Some(&descriptor.name),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
match (from, to) {
|
|
// An `Item<X>` wire may feed a `List<X>` connector via a singleton raise
|
|
(Type::Item(from_element), Type::List(element)) if valid_type(from_element, element) => Some((Self::ItemToList, element)),
|
|
|
|
// A `List<X>` wire may feed an `Item<Bundle<X>>` connector by bundling the whole list into one opaque cell
|
|
(Type::List(element), Type::Item(_)) if to.bundle_element_name().is_some_and(|name| Some(name) == concrete_name(element)) => Some((Self::Bundle, element)),
|
|
|
|
// An `Item<Bundle<X>>` wire may feed a `List<X>` connector by unbundling it back into the whole list
|
|
(Type::Item(_), Type::List(element)) if from.bundle_element_name().is_some_and(|name| Some(name) == concrete_name(element)) => Some((Self::Unbundle, element)),
|
|
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The registry identifier of the adapter node monomorphized for this promotion's element type.
|
|
pub fn adapter_identifier(&self) -> ProtoNodeIdentifier {
|
|
let (adapter_name, element) = match self {
|
|
Self::ItemToList(element) => ("graphene_core::ops::ItemToListNode", element),
|
|
Self::Bundle(element) => ("graphene_core::ops::BundleNode", element),
|
|
Self::Unbundle(element) => ("graphene_core::ops::UnbundleNode", element),
|
|
};
|
|
ProtoNodeIdentifier::with_owned_string(format!("{adapter_name}<{}>", element.identifier_name()))
|
|
}
|
|
}
|
|
|
|
impl TypingContext {
|
|
/// Creates a new `TypingContext` with the given lookup table.
|
|
pub fn new(lookup: &'static HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeConstructor>>) -> Self {
|
|
Self {
|
|
lookup: Cow::Borrowed(lookup),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Updates the `TypingContext` with a given proto network. This will infer the types of the nodes
|
|
/// and store them in the `inferred` field. The proto network has to be topologically sorted
|
|
/// and contain fully resolved stable node ids.
|
|
pub fn update(&mut self, network: &ProtoNetwork) -> Result<(), GraphErrors> {
|
|
for (id, node) in network.nodes.iter() {
|
|
self.infer(*id, node)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn remove_inference(&mut self, node_id: NodeId) -> Option<NodeIOTypes> {
|
|
self.constructor.remove(&node_id);
|
|
self.promotions.remove(&node_id);
|
|
self.inferred.remove(&node_id)
|
|
}
|
|
|
|
/// Returns the input positions of a node which type resolution marked for rank promotion, with each position's adapter.
|
|
pub fn promotions(&self, node_id: NodeId) -> Option<&Vec<(usize, Promotion)>> {
|
|
self.promotions.get(&node_id)
|
|
}
|
|
|
|
/// Looks up the sole constructor registered under an adapter identifier, such as an Item -> List promotion adapter.
|
|
pub fn adapter_constructor(&self, identifier: &ProtoNodeIdentifier) -> Option<NodeConstructor> {
|
|
self.lookup.get(identifier).and_then(|implementations| implementations.values().next().copied())
|
|
}
|
|
|
|
/// Returns the node constructor for a given node id.
|
|
pub fn constructor(&self, node_id: NodeId) -> Option<NodeConstructor> {
|
|
self.constructor.get(&node_id).copied()
|
|
}
|
|
|
|
/// Returns the type of a given node id if it exists
|
|
pub fn type_of(&self, node_id: NodeId) -> Option<&NodeIOTypes> {
|
|
self.inferred.get(&node_id)
|
|
}
|
|
|
|
/// Returns the inferred types for a given node id.
|
|
pub fn infer(&mut self, node_id: NodeId, node: &ProtoNode) -> Result<NodeIOTypes, GraphErrors> {
|
|
// Return the inferred type if it is already known
|
|
if let Some(inferred) = self.inferred.get(&node_id) {
|
|
return Ok(inferred.clone());
|
|
}
|
|
|
|
let inputs = match node.construction_args {
|
|
// If the node has a value input we can infer the return type from it
|
|
ConstructionArgs::Value(ref v) => {
|
|
// TODO: This should return a reference to the value
|
|
let types = NodeIOTypes::new(concrete!(Context), v.ty(), vec![]);
|
|
self.inferred.insert(node_id, types.clone());
|
|
return Ok(types);
|
|
}
|
|
// If the node has nodes as inputs we can infer the types from the node outputs
|
|
ConstructionArgs::Nodes(ref nodes) => nodes
|
|
.iter()
|
|
.map(|id| {
|
|
self.inferred
|
|
.get(id)
|
|
.ok_or_else(|| vec![GraphError::new(node, GraphErrorType::NodeNotFound(*id))])
|
|
.map(|node| node.ty())
|
|
})
|
|
.collect::<Result<Vec<Type>, GraphErrors>>()?,
|
|
};
|
|
|
|
// Get the node input type from the proto node declaration
|
|
let call_argument = &node.call_argument;
|
|
let impls = self.lookup.get(&node.identifier).ok_or_else(|| vec![GraphError::new(node, GraphErrorType::NoImplementations)])?;
|
|
|
|
if let Some(index) = inputs.iter().position(|p| {
|
|
matches!(p,
|
|
Type::Fn(_, b) if matches!(b.as_ref(), Type::Generic(_)))
|
|
}) {
|
|
return Err(vec![GraphError::new(node, GraphErrorType::UnexpectedGenerics { index, inputs })]);
|
|
}
|
|
|
|
// List of all implementations that match the input types
|
|
let valid_output_types = impls
|
|
.keys()
|
|
.filter(|node_io| valid_type(&node_io.call_argument, call_argument) && inputs.iter().zip(node_io.inputs.iter()).all(|(p1, p2)| valid_type(p1, p2)))
|
|
.collect::<Vec<_>>();
|
|
|
|
// Attempt to substitute generic types with concrete types and save the list of results
|
|
let substitution_results = valid_output_types
|
|
.iter()
|
|
.map(|node_io| {
|
|
let generics_lookup: Result<HashMap<_, _>, _> = collect_generics(node_io)
|
|
.iter()
|
|
.map(|generic| check_generic(node_io, call_argument, &inputs, generic).map(|x| (generic.to_string(), x)))
|
|
.collect();
|
|
|
|
generics_lookup.map(|generics_lookup| {
|
|
let orig_node_io = (*node_io).clone();
|
|
let mut new_node_io = orig_node_io.clone();
|
|
replace_generics(&mut new_node_io, &generics_lookup);
|
|
(new_node_io, orig_node_io)
|
|
})
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
// Collect all substitutions that are valid
|
|
let valid_impls = substitution_results.iter().filter_map(|result| result.as_ref().ok()).collect::<Vec<_>>();
|
|
|
|
match valid_impls.as_slice() {
|
|
[] => {
|
|
// Retry allowing a wire whose rank differs from its connector, satisfied at construction time by an inserted promotion adapter
|
|
fn promotable_adapter(from: &Type, to: &Type) -> Option<Promotion> {
|
|
let (Type::Fn(from_input, from_output), Type::Fn(to_input, to_output)) = (from, to) else {
|
|
return None;
|
|
};
|
|
if !valid_type(from_input, to_input) {
|
|
return None;
|
|
}
|
|
|
|
Promotion::between(from_output.nested_type(), to_output.nested_type())
|
|
}
|
|
|
|
let mut promotable_matches = impls
|
|
.keys()
|
|
.filter(|node_io| collect_generics(node_io).is_empty() && valid_type(&node_io.call_argument, call_argument) && inputs.len() == node_io.inputs.len())
|
|
.filter_map(|node_io| {
|
|
let mut required_promotions = Vec::new();
|
|
for (index, (provided, expected)) in inputs.iter().zip(node_io.inputs.iter()).enumerate() {
|
|
if valid_type(provided, expected) {
|
|
continue;
|
|
}
|
|
required_promotions.push((index, promotable_adapter(provided, expected)?));
|
|
}
|
|
Some((node_io, required_promotions))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
// Prefer the variant needing the fewest promotions, so an Item wire resolves the all-Item variant instead of raising every ranked connector to reach the mapped variant; a tie stays ambiguous.
|
|
promotable_matches.sort_by_key(|(_, required_promotions)| required_promotions.len());
|
|
let minimum_promotions = promotable_matches.first().map(|(_, required_promotions)| required_promotions.len());
|
|
let tied_at_minimum = promotable_matches
|
|
.iter()
|
|
.take_while(|(_, required_promotions)| Some(required_promotions.len()) == minimum_promotions)
|
|
.count();
|
|
|
|
if tied_at_minimum == 1 {
|
|
let (node_io, required_promotions) = promotable_matches.remove(0);
|
|
let node_io = node_io.clone();
|
|
|
|
self.inferred.insert(node_id, node_io.clone());
|
|
self.constructor.insert(node_id, impls[&node_io]);
|
|
self.promotions.insert(node_id, required_promotions);
|
|
return Ok(node_io);
|
|
}
|
|
|
|
let convert_node_index_offset = node.original_location.auto_convert_index.unwrap_or(0);
|
|
let mut best_errors = usize::MAX;
|
|
let mut error_inputs = Vec::new();
|
|
for node_io in impls.keys() {
|
|
// For errors on Convert nodes, offset the input index so it correctly corresponds to the node it is connected to.
|
|
let current_errors = [call_argument]
|
|
.into_iter()
|
|
.chain(&inputs)
|
|
.cloned()
|
|
.zip([&node_io.call_argument].into_iter().chain(&node_io.inputs).cloned())
|
|
.enumerate()
|
|
.filter(|(_, (p1, p2))| !valid_type(p1, p2))
|
|
.map(|(index, expected)| (index - 1 + convert_node_index_offset, expected))
|
|
.collect::<Vec<_>>();
|
|
if current_errors.len() < best_errors {
|
|
best_errors = current_errors.len();
|
|
error_inputs.clear();
|
|
}
|
|
if current_errors.len() <= best_errors {
|
|
error_inputs.push(current_errors);
|
|
}
|
|
}
|
|
let inputs = [call_argument]
|
|
.into_iter()
|
|
.chain(&inputs)
|
|
.enumerate()
|
|
.filter_map(|(i, t)| {
|
|
if i == 0 {
|
|
None
|
|
} else {
|
|
let number = i + convert_node_index_offset;
|
|
Some(format!("• Input {number}: {t}"))
|
|
}
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
Err(vec![GraphError::new(node, GraphErrorType::InvalidImplementations { inputs, error_inputs })])
|
|
}
|
|
[(node_io, org_nio)] => {
|
|
let node_io = node_io.clone();
|
|
|
|
// Save the inferred type
|
|
self.inferred.insert(node_id, node_io.clone());
|
|
self.constructor.insert(node_id, impls[org_nio]);
|
|
Ok(node_io)
|
|
}
|
|
// If two types are available and one of them accepts () an input, always choose that one
|
|
[first, second] => {
|
|
if first.0.call_argument != second.0.call_argument {
|
|
for (node_io, orig_nio) in [first, second] {
|
|
if node_io.call_argument != concrete!(()) {
|
|
continue;
|
|
}
|
|
|
|
// Save the inferred type
|
|
self.inferred.insert(node_id, node_io.clone());
|
|
self.constructor.insert(node_id, impls[orig_nio]);
|
|
return Ok(node_io.clone());
|
|
}
|
|
}
|
|
let inputs = [call_argument].into_iter().chain(&inputs).map(ToString::to_string).collect::<Vec<_>>().join(", ");
|
|
let valid = valid_output_types.into_iter().cloned().collect();
|
|
Err(vec![GraphError::new(node, GraphErrorType::MultipleImplementations { inputs, valid })])
|
|
}
|
|
|
|
_ => {
|
|
let inputs = [call_argument].into_iter().chain(&inputs).map(ToString::to_string).collect::<Vec<_>>().join(", ");
|
|
let valid = valid_output_types.into_iter().cloned().collect();
|
|
Err(vec![GraphError::new(node, GraphErrorType::MultipleImplementations { inputs, valid })])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Returns a list of all generic types used in the node
|
|
fn collect_generics(types: &NodeIOTypes) -> Vec<Cow<'static, str>> {
|
|
let inputs = [&types.call_argument].into_iter().chain(types.inputs.iter().map(|x| x.nested_type()));
|
|
let mut generics = inputs
|
|
.filter_map(|t| match t {
|
|
Type::Generic(out) => Some(out.clone()),
|
|
_ => None,
|
|
})
|
|
.collect::<Vec<_>>();
|
|
if let Type::Generic(out) = &types.return_value {
|
|
generics.push(out.clone());
|
|
}
|
|
generics.dedup();
|
|
generics
|
|
}
|
|
|
|
/// Checks if a generic type can be substituted with a concrete type and returns the concrete type
|
|
fn check_generic(types: &NodeIOTypes, input: &Type, parameters: &[Type], generic: &str) -> Result<Type, String> {
|
|
let inputs = [(Some(&types.call_argument), Some(input))]
|
|
.into_iter()
|
|
.chain(types.inputs.iter().map(|x| x.fn_input()).zip(parameters.iter().map(|x| x.fn_input())))
|
|
.chain(types.inputs.iter().map(|x| x.fn_output()).zip(parameters.iter().map(|x| x.fn_output())));
|
|
let concrete_inputs = inputs.filter(|(ni, _)| matches!(ni, Some(Type::Generic(input)) if generic == input));
|
|
let mut outputs = concrete_inputs.flat_map(|(_, out)| out);
|
|
let out_ty = outputs
|
|
.next()
|
|
.ok_or_else(|| format!("Generic output type {generic} is not dependent on input {input:?} or parameters {parameters:?}",))?;
|
|
if outputs.any(|ty| ty != out_ty) {
|
|
return Err(format!("Generic output type {generic} is dependent on multiple inputs or parameters",));
|
|
}
|
|
Ok(out_ty.clone())
|
|
}
|
|
|
|
/// Returns a list of all generic types used in the node
|
|
fn replace_generics(types: &mut NodeIOTypes, lookup: &HashMap<String, Type>) {
|
|
let replace = |ty: &Type| {
|
|
let Type::Generic(ident) = ty else { return None };
|
|
lookup.get(ident.as_ref()).cloned()
|
|
};
|
|
types.call_argument.replace_nested(replace);
|
|
types.return_value.replace_nested(replace);
|
|
for input in &mut types.inputs {
|
|
input.replace_nested(replace);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use crate::proto::{ConstructionArgs, ProtoNetwork, ProtoNode};
|
|
|
|
#[test]
|
|
fn topological_sort() {
|
|
let construction_network = test_network();
|
|
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
|
|
let sorted: Vec<_> = sorted.iter().map(|x| construction_network.nodes[x.0 as usize].0).collect();
|
|
println!("{sorted:#?}");
|
|
assert_eq!(sorted, vec![NodeId(14), NodeId(10), NodeId(11), NodeId(1)]);
|
|
}
|
|
|
|
#[test]
|
|
fn topological_sort_with_cycles() {
|
|
let construction_network = test_network_with_cycles();
|
|
let sorted = construction_network.topological_sort();
|
|
|
|
assert!(sorted.is_err())
|
|
}
|
|
|
|
#[test]
|
|
fn id_reordering() {
|
|
let mut construction_network = test_network();
|
|
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
|
|
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
|
|
let sorted: Vec<_> = sorted.iter().map(|x| construction_network.nodes[x.0 as usize].0).collect();
|
|
println!("nodes: {:#?}", construction_network.nodes);
|
|
assert_eq!(sorted, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
|
|
let ids: Vec<_> = construction_network.nodes.iter().map(|(id, _)| *id).collect();
|
|
println!("{ids:#?}");
|
|
println!("nodes: {:#?}", construction_network.nodes);
|
|
assert_eq!(construction_network.nodes[0].1.identifier.as_str(), "value");
|
|
assert_eq!(ids, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
|
|
}
|
|
|
|
#[test]
|
|
fn id_reordering_idempotent() {
|
|
let mut construction_network = test_network();
|
|
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
|
|
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
|
|
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
|
|
assert_eq!(sorted, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
|
|
let ids: Vec<_> = construction_network.nodes.iter().map(|(id, _)| *id).collect();
|
|
println!("{ids:#?}");
|
|
assert_eq!(construction_network.nodes[0].1.identifier.as_str(), "value");
|
|
assert_eq!(ids, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
|
|
}
|
|
|
|
#[test]
|
|
fn stable_node_id_generation() {
|
|
let mut construction_network = test_network();
|
|
construction_network
|
|
.insert_context_nullification_nodes()
|
|
.expect("Error when calling 'insert_context_nullification_nodes' on 'construction_network.");
|
|
construction_network.generate_stable_node_ids();
|
|
assert_eq!(construction_network.nodes[0].1.identifier.as_str(), "value");
|
|
let ids: Vec<_> = construction_network.nodes.iter().map(|(id, _)| *id).collect();
|
|
|
|
// If this assert fails: These NodeIds seem to be changing when you modify TaggedValue, just update them.
|
|
assert_eq!(
|
|
ids,
|
|
vec![NodeId(7074930193456627328), NodeId(651950451264872860), NodeId(15983493604836528424), NodeId(2458326405488235061)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn retain_filter_placement_on_source_free_branch() {
|
|
let mut network = source_branch_network(&[SourceId(1)], &[]);
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
|
|
let filters = nullification_filters(&network);
|
|
assert_eq!(filters.len(), 1);
|
|
let (filter_id, wrapped, retained) = &filters[0];
|
|
assert_eq!(wrapped, "source_b");
|
|
assert!(retained.is_empty());
|
|
|
|
let (source_a_id, _) = find_node(&network, "source_a");
|
|
let (_, join) = find_node(&network, "join");
|
|
let ConstructionArgs::Nodes(join_args) = &join.construction_args else { unreachable!() };
|
|
assert_eq!(join_args, &vec![source_a_id, *filter_id]);
|
|
}
|
|
|
|
#[test]
|
|
fn diverging_source_sets_filter_each_branch() {
|
|
let mut network = source_branch_network(&[SourceId(1)], &[SourceId(2)]);
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
|
|
let mut filters = nullification_filters(&network);
|
|
filters.sort_by(|(_, a, _), (_, b, _)| a.cmp(b));
|
|
let summary: Vec<_> = filters.iter().map(|(_, wrapped, retained)| (wrapped.as_str(), retained.as_slice())).collect();
|
|
assert_eq!(summary, vec![("source_a", &[SourceId(1)][..]), ("source_b", &[SourceId(2)][..])]);
|
|
}
|
|
|
|
#[test]
|
|
fn matching_source_sets_insert_no_filter() {
|
|
let mut network = source_branch_network(&[SourceId(1)], &[SourceId(1)]);
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
|
|
assert!(nullification_filters(&network).is_empty());
|
|
assert_eq!(network.source_ids().copied().collect::<Vec<_>>(), vec![SourceId(1), SourceId(1)]);
|
|
}
|
|
|
|
fn find_node<'a>(network: &'a ProtoNetwork, name: &str) -> (NodeId, &'a ProtoNode) {
|
|
network.nodes.iter().find(|(_, node)| node.identifier.as_str() == name).map(|(id, node)| (*id, node)).unwrap()
|
|
}
|
|
|
|
fn nullification_filters(network: &ProtoNetwork) -> Vec<(NodeId, String, Vec<SourceId>)> {
|
|
let node = |id: NodeId| &network.nodes[id.0 as usize].1;
|
|
network
|
|
.nodes
|
|
.iter()
|
|
.filter(|(_, candidate)| candidate.identifier == graphene_core::context_modification::context_modification::IDENTIFIER)
|
|
.map(|(id, candidate)| {
|
|
let ConstructionArgs::Nodes(args) = &candidate.construction_args else { unreachable!() };
|
|
let ConstructionArgs::Nodes(memoized) = &node(args[0]).construction_args else { unreachable!() };
|
|
let ConstructionArgs::Value(value) = &node(args[1]).construction_args else { unreachable!() };
|
|
let TaggedValue::ContextModification(modification) = &**value else { unreachable!() };
|
|
(*id, node(memoized[0]).identifier.as_str().to_string(), modification.sources.iter().copied().collect())
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn shared_nodes_are_wrapped_in_one_nullification() {
|
|
let node = |name: &'static str, inputs: Vec<NodeId>, extract: ContextFeatures| ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new(name),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(inputs),
|
|
compile_metadata: NodeCompileMetadata { extract, ..Default::default() },
|
|
..Default::default()
|
|
};
|
|
let none = ContextFeatures::empty();
|
|
|
|
let mut network = ProtoNetwork {
|
|
inputs: vec![],
|
|
output: NodeId(6),
|
|
nodes: vec![
|
|
(NodeId(0), ProtoNode::value(ConstructionArgs::Value(TaggedValue::Integer(2).into()), vec![])),
|
|
(NodeId(1), node("reader", vec![NodeId(0)], ContextFeatures::ANIMATION_TIME)),
|
|
(NodeId(2), node("plain", vec![NodeId(0)], none)),
|
|
(NodeId(3), node("shared", vec![NodeId(1), NodeId(2)], none)),
|
|
(NodeId(4), node("first_consumer", vec![NodeId(3)], none)),
|
|
(NodeId(5), node("second_consumer", vec![NodeId(3)], none)),
|
|
(NodeId(6), node("output", vec![NodeId(4), NodeId(5)], none)),
|
|
],
|
|
};
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
|
|
let nullification = graphene_core::context_modification::context_modification::IDENTIFIER;
|
|
let nullifications: Vec<_> = network.nodes.iter().filter(|(_, node)| node.identifier == nullification).collect();
|
|
assert_eq!(nullifications.len(), 1, "The plain branch of the shared node should be nullified once");
|
|
|
|
let ConstructionArgs::Nodes(inputs) = &nullifications[0].1.construction_args else {
|
|
panic!("A nullification node should have node inputs")
|
|
};
|
|
let ConstructionArgs::Nodes(memoized) = &network.nodes[inputs[0].0 as usize].1.construction_args else {
|
|
panic!("A memoize node should have node inputs")
|
|
};
|
|
assert_eq!(
|
|
network.nodes[memoized[0].0 as usize].1.identifier.as_str(),
|
|
"plain",
|
|
"The nullification should wrap the plain node, not another nullification"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn nodes_record_the_context_features_their_output_depends_on() {
|
|
let node = |name: &'static str, construction_args: ConstructionArgs, extract: ContextFeatures, inject: ContextFeatures| ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new(name),
|
|
call_argument: concrete!(()),
|
|
construction_args,
|
|
compile_metadata: NodeCompileMetadata {
|
|
extract,
|
|
inject,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
let none = ContextFeatures::empty();
|
|
|
|
let mut network = ProtoNetwork {
|
|
inputs: vec![],
|
|
output: NodeId(3),
|
|
nodes: vec![
|
|
(NodeId(0), node("value", ConstructionArgs::Value(TaggedValue::Integer(2).into()), none, none)),
|
|
(
|
|
NodeId(1),
|
|
node("reader", ConstructionArgs::Nodes(vec![NodeId(0)]), ContextFeatures::ANIMATION_TIME | ContextFeatures::INDEX, none),
|
|
),
|
|
(NodeId(2), node("repeat", ConstructionArgs::Nodes(vec![NodeId(1)]), none, ContextFeatures::INDEX)),
|
|
(NodeId(3), node("wrap", ConstructionArgs::Nodes(vec![NodeId(2)]), none, none)),
|
|
],
|
|
};
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
|
|
let features_of = |name: &str| network.nodes.iter().find(|(_, node)| node.identifier.as_str() == name).unwrap().1.upstream_context_features;
|
|
assert_eq!(features_of("value"), none);
|
|
assert_eq!(features_of("reader"), ContextFeatures::ANIMATION_TIME | ContextFeatures::INDEX);
|
|
assert_eq!(features_of("repeat"), ContextFeatures::ANIMATION_TIME);
|
|
assert_eq!(features_of("wrap"), ContextFeatures::ANIMATION_TIME);
|
|
assert_eq!(features_of(graphene_core::memo::memoize::IDENTIFIER.as_str()), ContextFeatures::ANIMATION_TIME);
|
|
assert_eq!(
|
|
features_of(graphene_core::context_modification::context_modification::IDENTIFIER.as_str()),
|
|
ContextFeatures::ANIMATION_TIME
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn footprint_invariant_nodes_warn_only_when_their_inputs_read_the_footprint() {
|
|
let node = |name: &'static str, inputs: Vec<NodeId>, compile_metadata: NodeCompileMetadata| ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new(name),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(inputs),
|
|
compile_metadata,
|
|
..Default::default()
|
|
};
|
|
let reader = NodeCompileMetadata {
|
|
extract: ContextFeatures::FOOTPRINT,
|
|
..Default::default()
|
|
};
|
|
let footprint_injector = NodeCompileMetadata {
|
|
inject: ContextFeatures::FOOTPRINT,
|
|
..Default::default()
|
|
};
|
|
let invariant = NodeCompileMetadata {
|
|
footprint_invariant: true,
|
|
..Default::default()
|
|
};
|
|
let warnings = |with_injector: bool| {
|
|
let mut nodes = vec![
|
|
(NodeId(0), ProtoNode::value(ConstructionArgs::Value(TaggedValue::Integer(2).into()), vec![])),
|
|
(NodeId(1), node("reader", vec![NodeId(0)], reader)),
|
|
];
|
|
if with_injector {
|
|
nodes.push((NodeId(2), node("injector", vec![NodeId(1)], footprint_injector)));
|
|
}
|
|
let output = NodeId(nodes.len() as u64);
|
|
nodes.push((output, node("invariant", vec![NodeId(output.0 - 1)], invariant)));
|
|
|
|
let mut network = ProtoNetwork { inputs: vec![], output, nodes };
|
|
network.insert_context_nullification_nodes().unwrap();
|
|
network.footprint_invariance_violations()
|
|
};
|
|
|
|
let violations = warnings(false);
|
|
assert_eq!(violations.len(), 1);
|
|
assert_eq!(violations[0].identifier, "invariant");
|
|
assert!(violations[0].error == GraphErrorType::FootprintDependentInput);
|
|
|
|
assert!(warnings(true).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn constant_inputs_must_resolve_to_values_through_forwarding_nodes() {
|
|
let value = || ProtoNode::value(ConstructionArgs::Value(TaggedValue::String("name".into()).into()), vec![]);
|
|
let node = |inputs: Vec<NodeId>, compile_metadata: NodeCompileMetadata| ProtoNode {
|
|
construction_args: ConstructionArgs::Nodes(inputs),
|
|
compile_metadata,
|
|
..Default::default()
|
|
};
|
|
let forwarding = NodeCompileMetadata {
|
|
forwards_input: true,
|
|
..Default::default()
|
|
};
|
|
let consumer = NodeCompileMetadata {
|
|
constant_inputs: 1 << 1,
|
|
..Default::default()
|
|
};
|
|
let errors = |name_wrapper: NodeCompileMetadata| {
|
|
let network = ProtoNetwork {
|
|
inputs: vec![],
|
|
output: NodeId(5),
|
|
nodes: vec![
|
|
(NodeId(0), value()),
|
|
(NodeId(1), value()),
|
|
(NodeId(2), node(vec![NodeId(1)], name_wrapper)),
|
|
(NodeId(3), node(vec![NodeId(2)], forwarding)),
|
|
(NodeId(4), value()),
|
|
(NodeId(5), node(vec![NodeId(0), NodeId(3), NodeId(4)], consumer)),
|
|
],
|
|
};
|
|
network.non_constant_inputs()
|
|
};
|
|
|
|
assert!(errors(forwarding).is_empty());
|
|
|
|
let computed = errors(NodeCompileMetadata::default());
|
|
assert_eq!(computed.len(), 1);
|
|
assert!(computed[0].error == GraphErrorType::NonConstantInput { index: 1 });
|
|
}
|
|
|
|
fn source_branch_network(branch_a_sources: &[SourceId], branch_b_sources: &[SourceId]) -> ProtoNetwork {
|
|
let node = |name: &'static str, construction_args: ConstructionArgs| ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new(name),
|
|
call_argument: concrete!(()),
|
|
construction_args,
|
|
..Default::default()
|
|
};
|
|
let mut nodes = vec![(NodeId(0), node("value", ConstructionArgs::Value(TaggedValue::Integer(2).into())))];
|
|
let mut branch = |name: &'static str, sources: &[SourceId]| {
|
|
let mut inputs = vec![NodeId(0)];
|
|
for &source in sources {
|
|
inputs.push(NodeId(nodes.len() as u64));
|
|
nodes.push((NodeId(nodes.len() as u64), node("source_id", ConstructionArgs::Value(TaggedValue::SourceId(source).into()))));
|
|
}
|
|
let id = NodeId(nodes.len() as u64);
|
|
nodes.push((id, node(name, ConstructionArgs::Nodes(inputs))));
|
|
id
|
|
};
|
|
let branch_a = branch("source_a", branch_a_sources);
|
|
let branch_b = branch("source_b", branch_b_sources);
|
|
|
|
let output = NodeId(nodes.len() as u64);
|
|
nodes.push((output, node("join", ConstructionArgs::Nodes(vec![branch_a, branch_b]))));
|
|
ProtoNetwork { inputs: vec![], output, nodes }
|
|
}
|
|
|
|
fn test_network() -> ProtoNetwork {
|
|
ProtoNetwork {
|
|
inputs: vec![NodeId(10)],
|
|
output: NodeId(1),
|
|
nodes: [
|
|
(
|
|
NodeId(7),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("id"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(11)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
(
|
|
NodeId(1),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("id"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(11)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
(
|
|
NodeId(10),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("cons"),
|
|
call_argument: concrete!(u32),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(14)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
(
|
|
NodeId(11),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("add"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(10)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
(
|
|
NodeId(14),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("value"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Value(value::TaggedValue::Integer(2).into()),
|
|
..Default::default()
|
|
},
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect(),
|
|
}
|
|
}
|
|
|
|
fn test_network_with_cycles() -> ProtoNetwork {
|
|
ProtoNetwork {
|
|
inputs: vec![NodeId(1)],
|
|
output: NodeId(1),
|
|
nodes: [
|
|
(
|
|
NodeId(1),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("id"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(2)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
(
|
|
NodeId(2),
|
|
ProtoNode {
|
|
identifier: ProtoNodeIdentifier::new("id"),
|
|
call_argument: concrete!(()),
|
|
construction_args: ConstructionArgs::Nodes(vec![NodeId(1)]),
|
|
..Default::default()
|
|
},
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect(),
|
|
}
|
|
}
|
|
}
|