Refactor document node type lookup function to fix performance degradation over time (#1878)

* Refactor document_node_types function

* Fix node introspection

* Implement diff based type updates

* Fix missing monitor nodes

* Improve docs and fix warings

* Fix wrongful removal of node paths

* Remove code examples for non pub methodsü

* Code review

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Dennis Kobert 2024-08-09 02:37:28 +02:00 committed by GitHub
parent 06a409f1c5
commit 0dfddd529b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 460 additions and 282 deletions

View file

@ -118,8 +118,8 @@ impl core::ops::IndexMut<usize> for Rect {
}
}
impl Into<Quad> for Rect {
fn into(self) -> Quad {
Quad::from_box(self.0)
impl From<Rect> for Quad {
fn from(val: Rect) -> Self {
Quad::from_box(val.0)
}
}

View file

@ -9,8 +9,8 @@ default = ["dealloc_nodes"]
serde = ["dep:serde", "graphene-core/serde", "glam/serde", "bezier-rs/serde"]
dealloc_nodes = []
wgpu = []
ci = []
tokio = ["dep:tokio"]
wayland = []
[dependencies]
# Local dependencies

View file

@ -227,8 +227,7 @@ pub struct OriginalLocation {
pub path: Option<Vec<NodeId>>,
/// Each document input source maps to one proto node input (however one proto node input may come from several sources)
pub inputs_source: HashMap<Source, usize>,
/// A list of document sources for the node's output
pub outputs_source: HashMap<Source, usize>,
/// A list of flags indicating whether the input is exposed in the UI
pub inputs_exposed: Vec<bool>,
/// Skipping inputs is useful for the manual composition thing - whereby a hidden `Footprint` input is added as the first input.
pub skip_inputs: usize,
@ -251,7 +250,6 @@ impl Hash for OriginalLocation {
fn hash<H: Hasher>(&self, state: &mut H) {
self.path.hash(state);
self.inputs_source.iter().for_each(|val| val.hash(state));
self.outputs_source.iter().for_each(|val| val.hash(state));
self.inputs_exposed.hash(state);
self.skip_inputs.hash(state);
}
@ -266,14 +264,6 @@ impl OriginalLocation {
.flatten()
.chain(self.inputs_source.iter().filter(move |x| *x.1 == index).map(|(source, _)| source.clone()))
}
pub fn outputs(&self, index: usize) -> impl Iterator<Item = Source> + '_ {
[Source {
node: self.path.clone().unwrap_or_default(),
index,
}]
.into_iter()
.chain(self.outputs_source.iter().filter(move |x| *x.1 == index).map(|(source, _)| source.clone()))
}
}
impl DocumentNode {
/// Locate the input that is a [`NodeInput::Network`] at index `offset` and replace it with a [`NodeInput::Node`].
@ -1161,11 +1151,6 @@ impl NodeNetwork {
if let NodeInput::Node { node_id, output_index, .. } = &export {
self.replace_node_inputs(node_input(id, i, false), node_input(*node_id, *output_index, false));
self.replace_node_inputs(node_input(id, i, true), node_input(*node_id, *output_index, true));
if let Some(new_output_node) = self.nodes.get_mut(node_id) {
for source in node.original_location.outputs(i) {
new_output_node.original_location.outputs_source.insert(source, *output_index);
}
}
}
self.replace_network_outputs(NodeInput::node(id, i), export);
@ -1197,11 +1182,6 @@ impl NodeNetwork {
assert_eq!(node.inputs.len(), 1, "Id node has more than one input");
if let NodeInput::Node { node_id, output_index, .. } = node.inputs[0] {
let node_input_output_index = output_index;
if let Some(input_node) = self.nodes.get_mut(&node_id) {
for source in node.original_location.outputs(0) {
input_node.original_location.outputs_source.insert(source, node_input_output_index);
}
}
let input_node_id = node_id;
for output in self.nodes.values_mut() {
@ -1507,7 +1487,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(0)]),
inputs_source: [(Source { node: vec![NodeId(1)], index: 1 }, 1)].into(),
outputs_source: HashMap::new(),
inputs_exposed: vec![true, true],
skip_inputs: 0,
},
@ -1524,7 +1503,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(1)]),
inputs_source: HashMap::new(),
outputs_source: [(Source { node: vec![NodeId(1)], index: 0 }, 0)].into(),
inputs_exposed: vec![true],
skip_inputs: 0,
},
@ -1540,7 +1518,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(4)]),
inputs_source: HashMap::new(),
outputs_source: HashMap::new(),
inputs_exposed: vec![true, false],
skip_inputs: 0,
},
@ -1571,7 +1548,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(0)]),
inputs_source: [(Source { node: vec![NodeId(1)], index: 1 }, 1)].into(),
outputs_source: HashMap::new(),
inputs_exposed: vec![true, true],
skip_inputs: 0,
},
@ -1586,7 +1562,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(4)]),
inputs_source: HashMap::new(),
outputs_source: HashMap::new(),
inputs_exposed: vec![true, false],
skip_inputs: 0,
},
@ -1601,7 +1576,6 @@ mod test {
original_location: OriginalLocation {
path: Some(vec![NodeId(1), NodeId(1)]),
inputs_source: HashMap::new(),
outputs_source: [(Source { node: vec![NodeId(1)], index: 0 }, 0)].into(),
inputs_exposed: vec![true],
skip_inputs: 0,
},

View file

@ -25,7 +25,7 @@ impl std::cmp::PartialEq for ImaginateCache {
impl core::hash::Hash for ImaginateCache {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.try_lock().map(|g| g.hash(state));
let _ = self.0.try_lock().map(|g| g.hash(state)).map_err(|_| "error".hash(state));
}
}

View file

@ -349,6 +349,18 @@ 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>> {
let mut edges: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
@ -675,20 +687,18 @@ impl TypingContext {
/// 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> {
let mut deleted_nodes = self.inferred.keys().copied().collect::<HashSet<_>>();
for (id, node) in network.nodes.iter() {
self.infer(*id, node)?;
deleted_nodes.remove(id);
}
for node in deleted_nodes {
self.inferred.remove(&node);
}
Ok(())
}
pub fn remove_inference(&mut self, node_id: NodeId) -> Option<NodeIOTypes> {
self.constructor.remove(&node_id);
self.inferred.remove(&node_id)
}
/// Returns the node constructor for a given node id.
pub fn constructor(&self, node_id: NodeId) -> Option<NodeConstructor> {
self.constructor.get(&node_id).copied()

View file

@ -103,13 +103,13 @@ impl WasmApplicationIo {
ids: AtomicU64::new(0),
#[cfg(feature = "wgpu")]
gpu_executor: executor,
windows: Vec::new().into(),
windows: Vec::new(),
resources: HashMap::new(),
};
#[cfg(not(feature = "ci"))]
let window = io.create_window();
#[cfg(not(feature = "ci"))]
io.windows.push(WindowWrapper { window });
if cfg!(target_arch = "wasm32") {
let window = io.create_window();
io.windows.push(WindowWrapper { window });
}
io.resources.insert("null".to_string(), Arc::from(include_bytes!("null.png").to_vec()));
io
@ -267,7 +267,7 @@ impl ApplicationIo for WasmApplicationIo {
}
fn window(&self) -> Option<SurfaceHandle<Self::Surface>> {
self.windows.iter().next().map(|wrapper| wrapper.window.clone())
self.windows.first().map(|wrapper| wrapper.window.clone())
}
}

View file

@ -21,7 +21,7 @@ imaginate = ["image/png", "base64", "js-sys", "web-sys", "wasm-bindgen-futures"]
image-compare = ["dep:image-compare"]
vello = ["dep:vello", "resvg", "gpu"]
resvg = ["dep:resvg"]
wayland = []
wayland = ["graph-craft/wayland"]
[dependencies]
# Local dependencies

View file

@ -2,12 +2,13 @@ use crate::node_registry;
use dyn_any::StaticType;
use graph_craft::document::value::{TaggedValue, UpcastAsRefNode, UpcastNode};
use graph_craft::document::{NodeId, Source};
use graph_craft::document::NodeId;
use graph_craft::graphene_compiler::Executor;
use graph_craft::proto::{ConstructionArgs, GraphError, LocalFuture, NodeContainer, ProtoNetwork, ProtoNode, SharedNodeContainer, TypeErasedBox, TypingContext};
use graph_craft::proto::{GraphErrorType, GraphErrors};
use graph_craft::Type;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::panic::UnwindSafe;
@ -21,7 +22,7 @@ pub struct DynamicExecutor {
/// Stores the types of the proto nodes.
typing_context: TypingContext,
// This allows us to keep the nodes around for one more frame which is used for introspection
orphaned_nodes: Vec<NodeId>,
orphaned_nodes: HashSet<NodeId>,
}
impl Default for DynamicExecutor {
@ -30,16 +31,31 @@ impl Default for DynamicExecutor {
output: Default::default(),
tree: Default::default(),
typing_context: TypingContext::new(&node_registry::NODE_REGISTRY),
orphaned_nodes: Vec::new(),
orphaned_nodes: HashSet::new(),
}
}
}
#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeTypes {
pub inputs: Vec<Type>,
pub output: Type,
}
#[derive(PartialEq, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ResolvedDocumentNodeTypes {
pub inputs: HashMap<Source, Type>,
pub outputs: HashMap<Source, Type>,
pub types: HashMap<Vec<NodeId>, NodeTypes>,
}
type Path = Box<[NodeId]>;
#[derive(PartialEq, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ResolvedDocumentNodeTypesDelta {
pub add: Vec<(Path, NodeTypes)>,
pub remove: Vec<Path>,
}
impl DynamicExecutor {
@ -53,26 +69,33 @@ impl DynamicExecutor {
tree,
output,
typing_context,
orphaned_nodes: Vec::new(),
orphaned_nodes: HashSet::new(),
})
}
/// Updates the existing [`BorrowTree`] to reflect the new [`ProtoNetwork`], reusing nodes where possible.
pub async fn update(&mut self, proto_network: ProtoNetwork) -> Result<(), GraphErrors> {
#[cfg_attr(debug_assertions, inline(never))]
pub async fn update(&mut self, proto_network: ProtoNetwork) -> Result<ResolvedDocumentNodeTypesDelta, GraphErrors> {
self.output = proto_network.output;
self.typing_context.update(&proto_network)?;
let mut orphans = self.tree.update(proto_network, &self.typing_context).await?;
core::mem::swap(&mut self.orphaned_nodes, &mut orphans);
for node_id in orphans {
let (add, orphaned) = self.tree.update(proto_network, &self.typing_context).await?;
let old_to_remove = core::mem::replace(&mut self.orphaned_nodes, orphaned);
let mut remove = Vec::with_capacity(old_to_remove.len() - self.orphaned_nodes.len().min(old_to_remove.len()));
for node_id in old_to_remove {
if self.orphaned_nodes.contains(&node_id) {
self.tree.free_node(node_id)
let path = self.tree.free_node(node_id);
self.typing_context.remove_inference(node_id);
if let Some(path) = path {
remove.push(path);
}
}
}
Ok(())
let add = self.document_node_types(add.into_iter()).collect();
Ok(ResolvedDocumentNodeTypesDelta { add, remove })
}
/// Calls the `Node::serialize` for that specific node, returning for example the cached value for a monitor node. The node path must match the document node path.
pub fn introspect(&self, node_path: &[NodeId]) -> Option<Option<Arc<dyn std::any::Any>>> {
pub fn introspect(&self, node_path: &[NodeId]) -> Result<Arc<dyn std::any::Any>, IntrospectError> {
self.tree.introspect(node_path)
}
@ -84,22 +107,10 @@ impl DynamicExecutor {
self.typing_context.type_of(self.output).map(|node_io| node_io.output.clone())
}
pub fn document_node_types(&self) -> ResolvedDocumentNodeTypes {
let mut resolved_document_node_types = ResolvedDocumentNodeTypes::default();
pub fn document_node_types<'a>(&'a self, nodes: impl Iterator<Item = Path> + 'a) -> impl Iterator<Item = (Path, NodeTypes)> + 'a {
nodes.flat_map(|id| self.tree.source_map().get(&id).map(|(_, b)| (id, b.clone())))
// TODO: https://github.com/GraphiteEditor/Graphite/issues/1767
// TODO: Non exposed inputs are not added to the inputs_source_map, so they are not included in the resolved_document_node_types. The type is still available in the typing_context. This only affects the UI-only "Import" node.
for (source, &(protonode_id, protonode_index)) in self.tree.inputs_source_map() {
let Some(node_io) = self.typing_context.type_of(protonode_id) else { continue };
let Some(ty) = [&node_io.input].into_iter().chain(&node_io.parameters).nth(protonode_index) else {
continue;
};
resolved_document_node_types.inputs.insert(source.clone(), ty.clone());
}
for (source, &protonode_id) in self.tree.outputs_source_map() {
let Some(node_io) = self.typing_context.type_of(protonode_id) else { continue };
resolved_document_node_types.outputs.insert(source.clone(), node_io.output.clone());
}
resolved_document_node_types
}
}
@ -121,18 +132,51 @@ impl<'a, I: StaticType + 'static + Send + Sync + std::panic::UnwindSafe> Executo
})
}
}
pub struct InputMapping {}
#[derive(Default)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum IntrospectError {
PathNotFound(Vec<NodeId>),
ProtoNodeNotFound(NodeId),
NoData,
RuntimeNotReady,
}
impl std::fmt::Display for IntrospectError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IntrospectError::PathNotFound(path) => write!(f, "Path not found: {:?}", path),
IntrospectError::ProtoNodeNotFound(id) => write!(f, "ProtoNode not found: {:?}", id),
IntrospectError::NoData => write!(f, "No data found for this node"),
IntrospectError::RuntimeNotReady => write!(f, "Node runtime is not ready"),
}
}
}
/// A store of dynamically typed nodes and their associated source map.
///
/// [`BorrowTree`] maintains two main data structures:
/// 1. A map of [`NodeId`]s to their corresponding nodes and paths.
/// 2. A source map that links document paths to node IDs and their types.
///
/// This structure is central to managing the graph of nodes in the interpreter,
/// allowing for efficient access and manipulation of nodes based on their IDs or paths.
///
/// # Fields
///
/// * `nodes`: A [`HashMap`] of [`NodeId`]s to tuples of [`SharedNodeContainer`] and [`Path`].
/// This stores the actual node instances and their associated paths.
///
/// * `source_map`: A [`HashMap`] from [`Path`] to tuples of [`NodeId`] and [`NodeTypes`].
/// This maps document paths to node IDs and their associated type information.
///
/// A store of the dynamically typed nodes and also the source map.
#[derive(Default)]
pub struct BorrowTree {
/// A hashmap of node IDs and dynamically typed nodes.
nodes: HashMap<NodeId, SharedNodeContainer>,
nodes: HashMap<NodeId, (SharedNodeContainer, Path)>,
/// A hashmap from the document path to the proto node ID.
source_map: HashMap<Vec<NodeId>, NodeId>,
/// Each document input source maps to one proto node input (however one proto node input may come from several sources)
inputs_source_map: HashMap<Source, (NodeId, usize)>,
/// A mapping of document input sources to the (single) proto node output
outputs_source_map: HashMap<Source, NodeId>,
source_map: HashMap<Path, (NodeId, NodeTypes)>,
}
impl BorrowTree {
@ -145,79 +189,181 @@ impl BorrowTree {
}
/// Pushes new nodes into the tree and return orphaned nodes
pub async fn update(&mut self, proto_network: ProtoNetwork, typing_context: &TypingContext) -> Result<Vec<NodeId>, GraphErrors> {
pub async fn update(&mut self, proto_network: ProtoNetwork, typing_context: &TypingContext) -> Result<(Vec<Path>, HashSet<NodeId>), GraphErrors> {
let mut old_nodes: HashSet<_> = self.nodes.keys().copied().collect();
let mut new_nodes: Vec<_> = Vec::new();
// TODO: Problem: When an identity node is connected directly to an export the first input to identity node is not added to the proto network, while the second input is. This means the primary input does not have a type.
for (id, node) in proto_network.nodes {
if !self.nodes.contains_key(&id) {
new_nodes.push(node.original_location.path.clone().unwrap_or_default().into());
self.push_node(id, node, typing_context).await?;
} else {
self.update_source_map(id, &node);
} else if self.update_source_map(id, typing_context, &node) {
new_nodes.push(node.original_location.path.clone().unwrap_or_default().into());
}
old_nodes.remove(&id);
}
self.source_map.retain(|_, nid| !old_nodes.contains(nid));
self.inputs_source_map.retain(|_, (nid, _)| !old_nodes.contains(nid));
self.outputs_source_map.retain(|_, nid| !old_nodes.contains(nid));
self.nodes.retain(|nid, _| !old_nodes.contains(nid));
Ok(old_nodes.into_iter().collect())
Ok((new_nodes, old_nodes))
}
fn node_deps(&self, nodes: &[NodeId]) -> Vec<SharedNodeContainer> {
nodes.iter().map(|node| self.nodes.get(node).unwrap().clone()).collect()
nodes.iter().map(|node| self.nodes.get(node).unwrap().0.clone()).collect()
}
fn store_node(&mut self, node: SharedNodeContainer, id: NodeId) {
self.nodes.insert(id, node);
fn store_node(&mut self, node: SharedNodeContainer, id: NodeId, path: Path) {
self.nodes.insert(id, (node, path));
}
/// Calls the `Node::serialize` for that specific node, returning for example the cached value for a monitor node. The node path must match the document node path.
pub fn introspect(&self, node_path: &[NodeId]) -> Option<Option<Arc<dyn std::any::Any>>> {
let id = self.source_map.get(node_path)?;
let node = self.nodes.get(id)?;
Some(node.serialize())
pub fn introspect(&self, node_path: &[NodeId]) -> Result<Arc<dyn std::any::Any>, IntrospectError> {
let (id, _) = self.source_map.get(node_path).ok_or_else(|| IntrospectError::PathNotFound(node_path.to_vec()))?;
let (node, _path) = self.nodes.get(id).ok_or(IntrospectError::ProtoNodeNotFound(*id))?;
node.serialize().ok_or(IntrospectError::NoData)
}
pub fn get(&self, id: NodeId) -> Option<SharedNodeContainer> {
self.nodes.get(&id).cloned()
self.nodes.get(&id).map(|(node, _)| node.clone())
}
/// Evaluate the output node of the [`BorrowTree`].
pub async fn eval<'i, I: StaticType + 'i + Send + Sync, O: StaticType + 'i>(&'i self, id: NodeId, input: I) -> Option<O> {
let node = self.nodes.get(&id).cloned()?;
let (node, _path) = self.nodes.get(&id).cloned()?;
let output = node.eval(Box::new(input));
dyn_any::downcast::<O>(output.await).ok().map(|o| *o)
}
/// Evaluate the output node of the [`BorrowTree`] and cast it to a tagged value.
/// This ensures that no borrowed data can escape the node graph.
pub async fn eval_tagged_value<I: StaticType + 'static + Send + Sync + UnwindSafe>(&self, id: NodeId, input: I) -> Result<TaggedValue, String> {
let node = self.nodes.get(&id).cloned().ok_or("Output node not found in executor")?;
let (node, _path) = self.nodes.get(&id).cloned().ok_or("Output node not found in executor")?;
let output = node.eval(Box::new(input));
TaggedValue::try_from_any(output.await)
}
pub fn free_node(&mut self, id: NodeId) {
self.nodes.remove(&id);
}
pub fn update_source_map(&mut self, id: NodeId, proto_node: &ProtoNode) {
self.source_map.insert(proto_node.original_location.path.clone().unwrap_or_default(), id);
let params = match &proto_node.construction_args {
ConstructionArgs::Nodes(nodes) => nodes.len() + 1,
_ => 2,
};
self.inputs_source_map
.extend((0..params).flat_map(|i| proto_node.original_location.inputs(i).map(move |source| (source, (id, i)))));
self.outputs_source_map.extend(proto_node.original_location.outputs(0).map(|source| (source, id)));
for x in proto_node.original_location.outputs_source.values() {
assert_eq!(*x, 0, "Proto nodes should refer to output index 0");
/// Removes a node from the [`BorrowTree`] and returns its associated path.
///
/// This method removes the specified node from both the `nodes` HashMap and,
/// if applicable, the `source_map` HashMap.
///
/// # Arguments
///
/// * `self` - Mutable reference to the [`BorrowTree`].
/// * `id` - The `NodeId` of the node to be removed.
///
/// # Returns
///
/// [`Option<Path>`] - The path associated with the removed node, or `None` if the node wasn't found.
///
/// # Example
///
/// ```rust
/// use std::collections::HashMap;
/// use graph_craft::{proto::*, document::*};
/// use interpreted_executor::{node_registry, dynamic_executor::BorrowTree};
///
///
/// async fn example() -> Result<(), GraphErrors> {
/// let (proto_network, node_id, proto_node) = ProtoNetwork::example();
/// let typing_context = TypingContext::new(&node_registry::NODE_REGISTRY);
/// let mut borrow_tree = BorrowTree::new(proto_network, &typing_context).await?;
///
/// // Assert that the node exists in the BorrowTree
/// assert!(borrow_tree.get(node_id).is_some(), "Node should exist before removal");
///
/// // Remove the node
/// let removed_path = borrow_tree.free_node(node_id);
///
/// // Assert that the node was successfully removed
/// assert!(removed_path.is_some(), "Node removal should return a path");
/// assert!(borrow_tree.get(node_id).is_none(), "Node should not exist after removal");
///
/// // Try to remove the same node again
/// let second_removal = borrow_tree.free_node(node_id);
///
/// assert_eq!(second_removal, None, "Second removal should return None");
///
/// println!("All assertions passed. free_node function works as expected.");
///
/// Ok(())
/// }
/// ```
///
/// # Notes
///
/// - Removes the node from `nodes` HashMap.
/// - If the node is the primary node for its path in the `source_map`, it's also removed from there.
/// - Returns `None` if the node is not found in the `nodes` HashMap.
pub fn free_node(&mut self, id: NodeId) -> Option<Path> {
let (_, path) = self.nodes.remove(&id)?;
if self.source_map.get(&path)?.0 == id {
self.source_map.remove(&path);
return Some(path);
}
None
}
/// Insert a new node into the borrow tree, calling the constructor function from `node_registry.rs`.
pub async fn push_node(&mut self, id: NodeId, proto_node: ProtoNode, typing_context: &TypingContext) -> Result<(), GraphErrors> {
self.update_source_map(id, &proto_node);
/// Updates the source map for a given node in the [`BorrowTree`].
///
/// This method updates or inserts an entry in the `source_map` HashMap for the specified node,
/// using type information from the provided [`TypingContext`] and [`ProtoNode`].
///
/// # Arguments
///
/// * `self` - Mutable reference to the [`BorrowTree`].
/// * `id` - The `NodeId` of the node to update in the source map.
/// * `typing_context` - A reference to the [`TypingContext`] containing type information.
/// * `proto_node` - A reference to the [`ProtoNode`] containing original location information.
///
/// # Returns
///
/// `bool` - `true` if a new entry was inserted, `false` if an existing entry was updated.
///
/// # Notes
///
/// - Updates or inserts an entry in the `source_map` HashMap.
/// - Uses the `ProtoNode`'s original location path as the key for the source map.
/// - Collects input types from both the main input and parameters.
/// - Returns `false` and logs a warning if the node's type information is not found in the typing context.
fn update_source_map(&mut self, id: NodeId, typing_context: &TypingContext, proto_node: &ProtoNode) -> bool {
let Some(node_io) = typing_context.type_of(id) else {
log::warn!("did not find type");
return false;
};
let inputs = [&node_io.input].into_iter().chain(&node_io.parameters).cloned().collect();
let node_path = &proto_node.original_location.path.as_ref().unwrap_or(const { &vec![] });
let entry = self.source_map.entry(node_path.to_vec().into());
let newly_inserted = matches!(entry, Entry::Vacant(_));
let entry = entry.or_insert((
id,
NodeTypes {
inputs,
output: node_io.output.clone(),
},
));
entry.0 = id;
entry.1.output = node_io.output.clone();
newly_inserted
}
/// Inserts a new node into the [`BorrowTree`], calling the constructor function from `node_registry.rs`.
///
/// This method creates a new node contianer based on the provided `ProtoNode`, updates the source map,
/// and stores the node container in the `BorrowTree`.
///
///
/// # Notes
///
/// - Updates the source map using [`update_source_map`](BorrowTree::update_source_map) before inserting the node.
/// - Handles different types of construction arguments:
/// - `Value`: Creates a node from a `TaggedValue`, with special handling for `EditorApi` values.
/// - `Inline`: Currently unimplemented. Only used for `rust-gpu` support.
/// - `Nodes`: Constructs a node using other nodes as dependencies.
/// - Uses the constructor function from the `typing_context` for `Nodes` construction arguments.
/// - Returns an error if no constructor is found for the given node ID.
async fn push_node(&mut self, id: NodeId, proto_node: ProtoNode, typing_context: &TypingContext) -> Result<(), GraphErrors> {
self.update_source_map(id, typing_context, &proto_node);
let path = proto_node.original_location.path.clone().unwrap_or_default();
match &proto_node.construction_args {
ConstructionArgs::Value(value) => {
@ -230,7 +376,7 @@ impl BorrowTree {
let node = Box::new(upcasted) as TypeErasedBox<'_>;
NodeContainer::new(node)
};
self.store_node(node, id);
self.store_node(node, id, path.into());
}
ConstructionArgs::Inline(_) => unimplemented!("Inline nodes are not supported yet"),
ConstructionArgs::Nodes(ids) => {
@ -239,18 +385,15 @@ impl BorrowTree {
let constructor = typing_context.constructor(id).ok_or_else(|| vec![GraphError::new(&proto_node, GraphErrorType::NoConstructor)])?;
let node = constructor(construction_nodes).await;
let node = NodeContainer::new(node);
self.store_node(node, id);
self.store_node(node, id, path.into());
}
};
Ok(())
}
pub fn inputs_source_map(&self) -> impl Iterator<Item = (&Source, &(NodeId, usize))> {
self.inputs_source_map.iter()
}
pub fn outputs_source_map(&self) -> impl Iterator<Item = (&Source, &NodeId)> {
self.outputs_source_map.iter()
/// Returns the source map of the borrow tree
pub fn source_map(&self) -> &HashMap<Path, (NodeId, NodeTypes)> {
&self.source_map
}
}

View file

@ -28,7 +28,7 @@ use web_sys::HtmlCanvasElement;
pub struct WgpuExecutor {
pub context: Context,
render_configuration: RenderConfiguration,
vello_renderer: std::sync::Mutex<vello::Renderer>,
vello_renderer: futures::lock::Mutex<vello::Renderer>,
}
impl std::fmt::Debug for WgpuExecutor {
@ -160,11 +160,12 @@ impl WgpuExecutor {
width,
height,
antialiasing_method: AaConfig::Msaa8,
debug: DebugLayers::all(),
// This setting can be used to visualize things like bounding boxes used for rendering
debug: DebugLayers::none(),
};
{
let mut renderer = self.vello_renderer.lock().unwrap();
let mut renderer = self.vello_renderer.lock().await;
for (id, texture) in context.ressource_overrides.iter() {
let texture_view = wgpu::ImageCopyTextureBase {
texture: texture.clone(),
@ -536,14 +537,12 @@ impl WgpuExecutor {
#[cfg(not(target_arch = "wasm32"))]
pub fn create_surface(&self, window: SurfaceHandle<Window>) -> Result<SurfaceHandle<Surface>> {
let size = window.surface.inner_size();
let resolution = UVec2::new(size.width, size.height);
let surface = self.context.instance.create_surface(wgpu::SurfaceTarget::Window(Box::new(window.surface)))?;
Ok(SurfaceHandle {
window_id: window.window_id,
surface: Surface {
inner: surface,
resolution: UVec2::ZERO,
},
surface: Surface { inner: surface, resolution },
})
}
}
@ -967,7 +966,7 @@ pub struct UploadTextureNode<Executor> {
#[node_macro::node_fn(UploadTextureNode)]
async fn upload_texture<'a: 'input>(input: ImageFrame<Color>, executor: &'a WgpuExecutor) -> TextureFrame {
// let new_data: Vec<RGBA16F> = input.image.data.into_iter().map(|c| c.into()).collect();
let new_data = input.image.data.into_iter().map(|c| SRGBA8::from(c)).collect();
let new_data = input.image.data.into_iter().map(SRGBA8::from).collect();
let new_image = Image {
width: input.image.width,
height: input.image.height,