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

@ -89,7 +89,7 @@ jobs:
- name: 🧪 Run Rust tests
run: |
mold -run cargo nextest run --all-features
mold -run cargo test --all-features
# miri:
# runs-on: self-hosted

View file

@ -66,7 +66,7 @@ web-sys = "=0.3.69"
winit = "0.29"
url = "2.5"
tokio = { version = "1.29", features = ["fs", "io-std"] }
vello = { git = "https://github.com/linebender/vello" }
vello = { git = "https://github.com/linebender/vello", features = ["debug_layers"] }
resvg = "0.42"
usvg = "0.42"
rand = { version = "0.8", default-features = false }

View file

@ -6,7 +6,7 @@ use crate::messages::prelude::*;
use graph_craft::document::value::TaggedValue;
use graph_craft::document::{NodeId, NodeInput};
use graph_craft::proto::GraphErrors;
use interpreted_executor::dynamic_executor::ResolvedDocumentNodeTypes;
use interpreted_executor::dynamic_executor::ResolvedDocumentNodeTypesDelta;
#[impl_message(Message, DocumentMessage, NodeGraph)]
#[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
@ -151,7 +151,7 @@ pub enum NodeGraphMessage {
UpdateNewNodeGraph,
UpdateTypes {
#[serde(skip)]
resolved_types: ResolvedDocumentNodeTypes,
resolved_types: ResolvedDocumentNodeTypesDelta,
#[serde(skip)]
node_graph_errors: GraphErrors,
},

View file

@ -11,7 +11,7 @@ use crate::messages::portfolio::document::utility_types::nodes::{CollapsedLayers
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::auto_panning::AutoPanning;
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeId, NodeInput, Source};
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeId, NodeInput};
use graph_craft::proto::GraphErrors;
use graphene_core::*;
use interpreted_executor::dynamic_executor::ResolvedDocumentNodeTypes;
@ -74,7 +74,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
match message {
// TODO: automatically remove broadcast messages.
NodeGraphMessage::AddNodes { nodes, new_ids } => {
let Some(new_layer_id) = new_ids.get(&NodeId(0)).cloned().or_else(|| nodes.get(0).map(|(node_id, _)| *node_id)) else {
let Some(new_layer_id) = new_ids.get(&NodeId(0)).cloned().or_else(|| nodes.first().map(|(node_id, _)| *node_id)) else {
log::error!("No nodes to add in AddNodes");
return;
};
@ -723,7 +723,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
return;
};
// TODO: Cache all wire locations if this is a performance issue
let mut overlapping_wires = Self::collect_wires(network_interface, selection_network_path)
let overlapping_wires = Self::collect_wires(network_interface, selection_network_path)
.into_iter()
.filter(|frontend_wire| {
// Prevent inserting on a link that is connected upstream to the selected node
@ -798,7 +798,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
let overlapping_wire = if network_interface.is_layer(&selected_node_id, selection_network_path) {
if stack_wires.len() == 1 {
stack_wires.first()
} else if stack_wires.len() == 0 && node_wires.len() == 1 {
} else if stack_wires.is_empty() && node_wires.len() == 1 {
node_wires.first()
} else {
None
@ -1240,7 +1240,12 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
responses.add(FrontendMessage::UpdateNodeTypes { node_types });
}
NodeGraphMessage::UpdateTypes { resolved_types, node_graph_errors } => {
network_interface.resolved_types = resolved_types;
for (path, node_type) in resolved_types.add {
network_interface.resolved_types.types.insert(path.to_vec(), node_type);
}
for path in resolved_types.remove {
network_interface.resolved_types.types.remove(&path.to_vec());
}
self.node_graph_errors = node_graph_errors;
}
NodeGraphMessage::UpdateActionButtons => {
@ -1573,11 +1578,11 @@ impl NodeGraphMessageHandler {
let frontend_graph_inputs = node.inputs.iter().enumerate().map(|(index, _)| {
// Convert the index in all inputs to the index in only the exposed inputs
// TODO: Only display input type if potential inputs in node_registry are all the same type
let input_type = network_interface.resolved_types.inputs.get(&Source { node: node_id_path.clone(), index }).cloned();
let node_types = network_interface.resolved_types.types.get(node_id_path.as_slice());
// TODO: Should display the color of the "most commonly relevant" (we'd need some sort of precedence) data type it allows given the current generic form that's constrained by the other present connections.
let frontend_data_type = if let Some(ref input_type) = input_type {
FrontendGraphDataType::with_type(input_type)
let frontend_data_type = if let Some(node_types) = node_types {
FrontendGraphDataType::with_type(&node_types.inputs[index])
} else {
FrontendGraphDataType::General
};
@ -1592,7 +1597,7 @@ impl NodeGraphMessageHandler {
FrontendGraphInput {
data_type: frontend_data_type,
name: input_name,
resolved_type: input_type.map(|input| format!("{input:?}")),
resolved_type: node_types.map(|types| format!("{:?}", types.inputs[index])),
connected_to: None,
}
});
@ -1801,21 +1806,45 @@ impl NodeGraphMessageHandler {
}
}
/// Retrieves the output types for a given document node and its exports.
///
/// This function traverses the node and its nested network structure (if applicable) to determine
/// the types of all outputs, including the primary output and any additional exports.
///
/// # Arguments
///
/// * `node` - A reference to the `DocumentNode` for which to determine output types.
/// * `resolved_types` - A reference to `ResolvedDocumentNodeTypes` containing pre-resolved type information.
/// * `node_id_path` - A slice of `NodeId`s representing the path to the current node in the document graph.
///
/// # Returns
///
/// A `Vec<Option<Type>>` where:
/// - The first element is the primary output type of the node.
/// - Subsequent elements are types of additional exports (if the node is a network).
/// - `None` values indicate that a type couldn't be resolved for a particular output.
///
/// # Behavior
///
/// 1. Retrieves the primary output type from `resolved_types`.
/// 2. If the node is a network:
/// - Iterates through its exports (skipping the first/primary export).
/// - For each export, traverses the network until reaching a protonode or terminal condition.
/// - Determines the output type based on the final node/value encountered.
/// 3. Collects and returns all resolved types.
///
/// # Note
///
/// This function assumes that export indices and node IDs always exist within their respective
/// collections. It will panic if these assumptions are violated.
pub fn get_output_types(node: &DocumentNode, resolved_types: &ResolvedDocumentNodeTypes, node_id_path: &[NodeId]) -> Vec<Option<Type>> {
let mut output_types = Vec::new();
let primary_output_type = resolved_types
.outputs
.get(&Source {
node: node_id_path.to_owned(),
index: 0,
})
.cloned();
output_types.push(primary_output_type);
let primary_output_type = resolved_types.types.get(node_id_path).map(|ty| ty.output.clone());
// If the node is not a protonode, get types by traversing across exports until a proto node is reached.
if let graph_craft::document::DocumentNodeImplementation::Network(internal_network) = &node.implementation {
for export in internal_network.exports.iter().skip(1) {
for export in internal_network.exports.iter() {
let mut current_export = export;
let mut current_network = internal_network;
let mut current_path = node_id_path.to_owned();
@ -1833,25 +1862,23 @@ impl NodeGraphMessageHandler {
}
}
let output_type: Option<Type> = if let NodeInput::Node { output_index, .. } = current_export {
// Current export is pointing to a proto node where type can be derived
assert_eq!(*output_index, 0, "Output index for a proto node should always be 0");
resolved_types.outputs.get(&Source { node: current_path.clone(), index: 0 }).cloned()
} else if let NodeInput::Value { tagged_value, .. } = current_export {
Some(tagged_value.ty())
} else if let NodeInput::Network { import_index, .. } = current_export {
resolved_types
.outputs
.get(&Source {
node: node_id_path.to_owned(),
index: *import_index,
})
.cloned()
} else {
None
let output_type: Option<Type> = match current_export {
NodeInput::Node { output_index, .. } => {
// Current export is pointing to a proto node where type can be derived
assert_eq!(*output_index, 0, "Output index for a proto node should always be 0");
resolved_types.types.get(&current_path).map(|ty| ty.output.clone())
}
NodeInput::Value { tagged_value, .. } => Some(tagged_value.ty()),
NodeInput::Network { import_type, .. } => Some(import_type.clone()),
_ => None,
};
output_types.push(output_type);
}
} else {
if primary_output_type.is_none() {
log::warn!("no output type found for {:?} {:?}", node_id_path, &node.implementation);
}
output_types.push(primary_output_type);
}
output_types
}

View file

@ -6,7 +6,7 @@ use crate::messages::portfolio::document::node_graph::utility_types::{FrontendCl
use crate::messages::prelude::NodeGraphMessageHandler;
use bezier_rs::Subpath;
use graph_craft::document::{value::TaggedValue, DocumentNode, DocumentNodeImplementation, NodeId, NodeInput, NodeNetwork, OldDocumentNodeImplementation, OldNodeNetwork, Source};
use graph_craft::document::{value::TaggedValue, DocumentNode, DocumentNodeImplementation, NodeId, NodeInput, NodeNetwork, OldDocumentNodeImplementation, OldNodeNetwork};
use graph_craft::{concrete, Type};
use graphene_std::renderer::{ClickTarget, Quad};
use graphene_std::vector::{PointId, VectorModificationType};
@ -386,11 +386,10 @@ impl NodeNetworkInterface {
// TODO: Store types for all document nodes, not just the compiled proto nodes, which currently skips isolated nodes
let node_type_from_compiled_network = if let Some(node_id) = input_connector.node_id() {
let node_id_path = [network_path, &[node_id]].concat().clone();
let input_type = self.resolved_types.inputs.get(&graph_craft::document::Source {
node: node_id_path,
index: input_connector.input_index(),
});
input_type.cloned()
self.resolved_types
.types
.get(node_id_path.as_slice())
.map(|node_types| node_types.inputs[input_connector.input_index()].clone())
} else if let Some(encapsulating_node) = self.encapsulating_node(network_path) {
let output_types = NodeGraphMessageHandler::get_output_types(encapsulating_node, &self.resolved_types, network_path);
output_types.get(input_connector.input_index()).map_or_else(
@ -502,14 +501,7 @@ impl NodeNetworkInterface {
if !network_path.is_empty() {
// TODO: https://github.com/GraphiteEditor/Graphite/issues/1767
// TODO: Non exposed inputs are not added to the inputs_source_map, fix `pub fn document_node_types(&self) -> ResolvedDocumentNodeTypes`
let input_type = self
.resolved_types
.inputs
.get(&Source {
node: network_path.to_vec(),
index: *import_index,
})
.cloned();
let input_type = self.resolved_types.types.get(network_path).map(|nt| nt.inputs[*import_index].clone());
let frontend_data_type = if let Some(input_type) = input_type.clone() {
FrontendGraphDataType::with_type(&input_type)
@ -2823,8 +2815,8 @@ impl NodeNetworkInterface {
pub fn set_to_node_or_layer(&mut self, node_id: &NodeId, network_path: &[NodeId], is_layer: bool) {
// If a layer is set to a node, set upstream nodes to absolute position, and upstream siblings to absolute position
let child_id = { self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::HorizontalFlow).skip(1).next() };
let upstream_sibling_id = { self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::PrimaryFlow).skip(1).next() };
let child_id = { self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::HorizontalFlow).nth(1) };
let upstream_sibling_id = { self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::PrimaryFlow).nth(1) };
match (self.is_layer(node_id, network_path), is_layer) {
(true, false) => {
if let Some(child_id) = child_id {
@ -2865,7 +2857,7 @@ impl NodeNetworkInterface {
.and_then(|outward_wires| {
outward_wires
.get(&OutputConnector::node(*node_id, 0))
.and_then(|outward_wires| outward_wires.get(0))
.and_then(|outward_wires| outward_wires.first())
.and_then(|downstream_connector| if downstream_connector.input_index() == 0 { downstream_connector.node_id() } else { None })
})
.is_some_and(|downstream_node_id| self.is_layer(&downstream_node_id, network_path));

View file

@ -16,6 +16,7 @@ use graph_craft::document::value::TaggedValue;
use graph_craft::document::{NodeId, NodeInput};
use graphene_core::text::Font;
use graphene_std::vector::style::{Fill, FillType, Gradient};
use interpreted_executor::dynamic_executor::IntrospectError;
use std::sync::Arc;
use std::vec;
@ -749,7 +750,7 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageData<'_>> for PortfolioMes
}
impl PortfolioMessageHandler {
pub async fn introspect_node(&self, node_path: &[NodeId]) -> Option<Arc<dyn std::any::Any>> {
pub async fn introspect_node(&self, node_path: &[NodeId]) -> Result<Arc<dyn std::any::Any>, IntrospectError> {
self.executor.introspect_node(node_path).await
}

View file

@ -108,6 +108,7 @@ pub fn get_blend_mode(layer: LayerNodeIdentifier, network_interface: &NodeNetwor
/// - Set by an Opacity node with an exposed parameter value driven by another node
/// - Already factored into the pixel alpha channel of an image
/// - The default value of 100% if no Opacity node is present, but this function returns None in that case
///
/// With those limitations in mind, the intention of this function is to show just the value already present in an upstream Opacity node so that value can be directly edited.
pub fn get_opacity(layer: LayerNodeIdentifier, network_interface: &NodeNetworkInterface) -> Option<f64> {
let inputs = NodeGraphLayer::new(layer, network_interface).find_node_inputs("Opacity")?;

View file

@ -406,9 +406,11 @@ fn assert_boxes_in_order(rectangles: &VecDeque<Rect>, index: usize) {
#[test]
fn dist_snap_point_right() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.right = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
dist_snapper.left = [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
left: [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -422,9 +424,11 @@ fn dist_snap_point_right() {
#[test]
fn dist_snap_point_right_left() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.right = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
dist_snapper.left = [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -439,8 +443,10 @@ fn dist_snap_point_right_left() {
#[test]
fn dist_snap_point_left() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.left = [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -454,9 +460,11 @@ fn dist_snap_point_left() {
#[test]
fn dist_snap_point_left_right() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.left = [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
dist_snapper.right = [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
right: [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -470,9 +478,11 @@ fn dist_snap_point_left_right() {
#[test]
fn dist_snap_point_center_x() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.left = [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
dist_snapper.right = [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
left: [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
right: [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -488,9 +498,11 @@ fn dist_snap_point_center_x() {
#[test]
fn dist_snap_point_down() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.down = [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.up = [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
up: [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -504,9 +516,11 @@ fn dist_snap_point_down() {
#[test]
fn dist_snap_point_down_up() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.down = [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.up = [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -521,8 +535,10 @@ fn dist_snap_point_down_up() {
#[test]
fn dist_snap_point_up() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.up = [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -536,9 +552,11 @@ fn dist_snap_point_up() {
#[test]
fn dist_snap_point_up_down() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.up = [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.down = [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -552,9 +570,11 @@ fn dist_snap_point_up_down() {
#[test]
fn dist_snap_point_center_y() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.up = [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.down = [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default()
};
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
@ -568,11 +588,12 @@ fn dist_snap_point_center_y() {
#[test]
fn dist_snap_point_center_xy() {
let mut dist_snapper = DistributionSnapper::default();
dist_snapper.up = [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.down = [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec();
dist_snapper.left = [-12., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
dist_snapper.right = [12., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec();
let dist_snapper = DistributionSnapper {
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
left: [-12., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
right: [12., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
};
let source = Rect::from_square(DVec2::new(0.3, 0.4), 2.);
let snap_results = &mut SnapResults::default();
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);

View file

@ -22,7 +22,7 @@ use graphene_core::vector::VectorData;
use graphene_core::{Color, GraphicElement, SurfaceFrame};
use graphene_std::renderer::format_transform_matrix;
use graphene_std::wasm_application_io::{WasmApplicationIo, WasmEditorApi};
use interpreted_executor::dynamic_executor::{DynamicExecutor, ResolvedDocumentNodeTypes};
use interpreted_executor::dynamic_executor::{DynamicExecutor, IntrospectError, ResolvedDocumentNodeTypesDelta};
use glam::{DAffine2, DVec2, UVec2};
use once_cell::sync::Lazy;
@ -43,7 +43,6 @@ pub struct NodeRuntime {
editor_api: Arc<WasmEditorApi>,
node_graph_errors: GraphErrors,
resolved_types: ResolvedDocumentNodeTypes,
monitor_nodes: Vec<Vec<NodeId>>,
// TODO: Remove, it doesn't need to be persisted anymore
@ -91,8 +90,7 @@ pub struct ExecutionResponse {
}
pub struct CompilationResponse {
result: Result<(), String>,
resolved_types: ResolvedDocumentNodeTypes,
result: Result<ResolvedDocumentNodeTypesDelta, String>,
node_graph_errors: GraphErrors,
}
@ -143,7 +141,6 @@ impl NodeRuntime {
.into(),
node_graph_errors: Vec::new(),
resolved_types: ResolvedDocumentNodeTypes::default(),
monitor_nodes: Vec::new(),
thumbnail_renders: Default::default(),
@ -214,7 +211,6 @@ impl NodeRuntime {
self.update_thumbnails = true;
self.sender.send_generation_response(CompilationResponse {
result,
resolved_types: self.resolved_types.clone(),
node_graph_errors: self.node_graph_errors.clone(),
});
}
@ -240,13 +236,8 @@ impl NodeRuntime {
}
}
async fn update_network(&mut self, graph: NodeNetwork) -> Result<(), String> {
async fn update_network(&mut self, graph: NodeNetwork) -> Result<ResolvedDocumentNodeTypesDelta, String> {
let scoped_network = wrap_network_in_scope(graph, self.editor_api.clone());
self.monitor_nodes = scoped_network
.recursive_nodes()
.filter(|(_, node)| node.implementation == DocumentNodeImplementation::proto("graphene_core::memo::MonitorNode<_, _, _>"))
.map(|(_, node)| node.original_location.path.clone().unwrap_or_default())
.collect::<Vec<_>>();
// We assume only one output
assert_eq!(scoped_network.exports.len(), 1, "Graph with multiple outputs not yet handled");
@ -255,14 +246,18 @@ impl NodeRuntime {
Ok(network) => network,
Err(e) => return Err(e),
};
self.monitor_nodes = proto_network
.nodes
.iter()
.filter(|(_, node)| node.identifier == "graphene_core::memo::MonitorNode<_, _, _>".into())
.map(|(_, node)| node.original_location.path.clone().unwrap_or_default())
.collect::<Vec<_>>();
assert_ne!(proto_network.nodes.len(), 0, "No proto nodes exist?");
if let Err(e) = self.executor.update(proto_network).await {
self.node_graph_errors = e;
}
self.resolved_types = self.executor.document_node_types();
Ok(())
self.executor.update(proto_network).await.map_err(|e| {
self.node_graph_errors = e.clone();
format!("{e:?}")
})
}
async fn execute_network(&mut self, render_config: RenderConfig) -> Result<TaggedValue, String> {
@ -296,10 +291,10 @@ impl NodeRuntime {
};
// Extract the monitor node's stored `GraphicElement` data.
let Some(introspected_data) = self.executor.introspect(monitor_node_path).flatten() else {
let Ok(introspected_data) = self.executor.introspect(monitor_node_path) else {
// TODO: Fix the root of the issue causing the spam of this warning (this at least temporarily disables it in release builds)
#[cfg(debug_assertions)]
warn!("Failed to introspect monitor node {:?}", self.executor.introspect(monitor_node_path));
warn!("Failed to introspect monitor node {}", self.executor.introspect(monitor_node_path).unwrap_err());
continue;
};
@ -377,12 +372,12 @@ impl NodeRuntime {
}
}
pub async fn introspect_node(path: &[NodeId]) -> Option<Arc<dyn std::any::Any>> {
pub async fn introspect_node(path: &[NodeId]) -> Result<Arc<dyn std::any::Any>, IntrospectError> {
let runtime = NODE_RUNTIME.lock();
if let Some(ref mut runtime) = runtime.as_ref() {
return runtime.executor.introspect(path).flatten();
return runtime.executor.introspect(path);
}
None
Err(IntrospectError::RuntimeNotReady)
}
pub async fn run_node_graph() -> bool {
@ -436,7 +431,7 @@ impl NodeGraphExecutor {
execution_id
}
pub async fn introspect_node(&self, path: &[NodeId]) -> Option<Arc<dyn std::any::Any>> {
pub async fn introspect_node(&self, path: &[NodeId]) -> Result<Arc<dyn std::any::Any>, IntrospectError> {
introspect_node(path).await
}
@ -462,7 +457,7 @@ impl NodeGraphExecutor {
return None;
};
let introspection_node = find_node(wrapped_network)?;
let introspection = futures::executor::block_on(self.introspect_node(&[node_path, &[introspection_node]].concat()))?;
let introspection = futures::executor::block_on(self.introspect_node(&[node_path, &[introspection_node]].concat())).ok()?;
let Some(downcasted): Option<&T> = <dyn std::any::Any>::downcast_ref(introspection.as_ref()) else {
log::warn!("Failed to downcast type for introspection");
return None;
@ -609,20 +604,22 @@ impl NodeGraphExecutor {
}
}
NodeGraphUpdate::CompilationResponse(execution_response) => {
let CompilationResponse {
resolved_types,
node_graph_errors,
result,
} = execution_response;
if let Err(e) = result {
// Clear the click targets while the graph is in an un-renderable state
document.network_interface.document_metadata_mut().update_from_monitor(HashMap::new(), HashMap::new());
log::trace!("{e}");
let CompilationResponse { node_graph_errors, result } = execution_response;
let type_delta = match result {
Err(e) => {
// Clear the click targets while the graph is in an un-renderable state
document.network_interface.document_metadata_mut().update_from_monitor(HashMap::new(), HashMap::new());
log::trace!("{e}");
return Err("Node graph evaluation failed".to_string());
return Err("Node graph evaluation failed".to_string());
}
Ok(result) => result,
};
responses.add(NodeGraphMessage::UpdateTypes { resolved_types, node_graph_errors });
responses.add(NodeGraphMessage::UpdateTypes {
resolved_types: type_delta,
node_graph_errors,
});
responses.add(NodeGraphMessage::SendGraph);
}
NodeGraphUpdate::NodeGraphUpdateMessage(NodeGraphUpdateMessage::ImaginateStatusUpdate) => {

View file

@ -786,7 +786,7 @@ impl EditorHandle {
let Some(transform_node_id) = modify_inputs.existing_node_id("Transform") else {
return;
};
if !updated_nodes.insert(transform_node_id.clone()) {
if !updated_nodes.insert(transform_node_id) {
return;
}
let Some(inputs) = modify_inputs.network_interface.network(&[]).unwrap().nodes.get(&transform_node_id).map(|node| &node.inputs) else {

View file

@ -67,6 +67,7 @@ impl Bezier {
/// Create a quadratic bezier curve that goes through 3 points, where the middle point will be at the corresponding position `t` on the curve.
/// - `t` - A representation of how far along the curve the provided point should occur at. The default value is 0.5.
///
/// Note that when `t = 0` or `t = 1`, the expectation is that the `point_on_curve` should be equal to `start` and `end` respectively.
/// In these cases, if the provided values are not equal, this function will use the `point_on_curve` as the `start`/`end` instead.
/// <iframe frameBorder="0" width="100%" height="375px" src="https://graphite.rs/libraries/bezier-rs#bezier/bezier-through-points/solo" title="Through Points Demo"></iframe>
@ -84,6 +85,7 @@ impl Bezier {
/// Create a cubic bezier curve that goes through 3 points, where the middle point will be at the corresponding position `t` on the curve.
/// - `t` - A representation of how far along the curve the provided point should occur at. The default value is 0.5.
///
/// Note that when `t = 0` or `t = 1`, the expectation is that the `point_on_curve` should be equal to `start` and `end` respectively.
/// In these cases, if the provided values are not equal, this function will use the `point_on_curve` as the `start`/`end` instead.
/// - `midpoint_separation` - A representation of how wide the resulting curve will be around `t` on the curve. This parameter designates the distance between the `e1` and `e2` defined in [the projection identity section](https://pomax.github.io/bezierinfo/#abc) of Pomax's bezier curve primer. It is an optional parameter and the default value is the distance between the points `B` and `C` defined in the primer.

View file

@ -133,6 +133,7 @@ impl Bezier {
/// Determine if it is possible to scale the given curve, using the following conditions:
/// 1. All the handles are located on a single side of the curve.
/// 2. The on-curve point for `t = 0.5` must occur roughly in the center of the polygon defined by the curve's endpoint normals.
///
/// See [the offset section](https://pomax.github.io/bezierinfo/#offsetting) of Pomax's bezier curve primer for more details.
fn is_scalable(&self) -> bool {
if self.handles == BezierHandles::Linear {
@ -345,7 +346,7 @@ impl Bezier {
/// A proof for why this is true can be found in the [Curve offsetting section](https://pomax.github.io/bezierinfo/#offsetting) of Pomax's bezier curve primer.
/// Offset takes the following parameter:
/// - `distance` - The offset's distance from the curve. Positive values will offset the curve in the same direction as the endpoint normals,
/// while negative values will offset in the opposite direction.
/// while negative values will offset in the opposite direction.
/// <iframe frameBorder="0" width="100%" height="325px" src="https://graphite.rs/libraries/bezier-rs#bezier/offset/solo" title="Offset Demo"></iframe>
pub fn offset<PointId: crate::Identifier>(&self, distance: f64) -> Subpath<PointId> {
if self.is_point() {

View file

@ -65,6 +65,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// Because the calculation of area for self-intersecting path requires finding the intersections, the following parameters are used:
/// - `error` - For intersections with non-linear beziers, `error` defines the threshold for bounding boxes to be considered an intersection point.
/// - `minimum_separation` - the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two
///
/// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out.
@ -109,6 +110,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// Because the calculation of area and centroid for self-intersecting path requires finding the intersections, the following parameters are used:
/// - `error` - For intersections with non-linear beziers, `error` defines the threshold for bounding boxes to be considered an intersection point.
/// - `minimum_separation` - the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two.
///
/// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out.
@ -167,6 +169,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// - `error` - For intersections with non-linear beziers, `error` defines the threshold for bounding boxes to be considered an intersection point.
/// - `minimum_separation` - the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
/// - `tolerance` - Tolerance used to approximate the curve if it falls back to length centroid.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two
///
/// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out.

View file

@ -82,6 +82,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// - `IgnoreStart`: drops the bezier's start point in favor of the subpath's last anchor
/// - `SmoothJoin(f64)`: joins the subpath's endpoint with the bezier's start with a another Bezier segment that is continuous up to the second derivative
/// if the difference between the subpath's end point and Bezier's start point exceeds the wrapped integer value.
///
/// This function assumes that the position of the [Bezier]'s starting point is equal to that of the Subpath's last manipulator group.
pub fn append_bezier(&mut self, bezier: &Bezier, append_type: AppendType) {
if self.manipulator_groups.is_empty() {

View file

@ -22,6 +22,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// - `other`: a [Bezier] curve to check intersections against
/// - `error`: an optional f64 value to provide an error bound
/// - `minimum_separation`: the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two.
/// <iframe frameBorder="0" width="100%" height="375px" src="https://graphite.rs/libraries/bezier-rs#subpath/intersect-linear/solo" title="Intersection Demo"></iframe>
///
@ -107,6 +108,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// Returns a list of `t` values that correspond to the self intersection points of the subpath. For each intersection point, the returned `t` value is the smaller of the two that correspond to the point.
/// - `error` - For intersections with non-linear beziers, `error` defines the threshold for bounding boxes to be considered an intersection point.
/// - `minimum_separation`: the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two
///
/// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out.
@ -134,6 +136,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// The points will be sorted based on their index and `t` repsectively.
/// - `error` - For intersections with non-linear beziers, `error` defines the threshold for bounding boxes to be considered an intersection point.
/// - `minimum_separation`: the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two
///
/// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out.
@ -168,6 +171,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// - `corner2`: the corner opposite to `corner1`
/// - `error`: an optional f64 value to provide an error bound
/// - `minimum_separation`: the minimum difference two adjacent `t`-values must have when comparing adjacent `t`-values in sorted order.
///
/// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two.
/// <iframe frameBorder="0" width="100%" height="375px" src="https://graphite.rs/libraries/bezier-rs#subpath/intersect-rectangle/solo" title="Intersection Demo"></iframe>
pub fn rectangle_intersections(&self, corner1: DVec2, corner2: DVec2, error: Option<f64>, minimum_separation: Option<f64>) -> Vec<(usize, f64)> {
@ -340,6 +344,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// Conceptually, this works by "throwing a dart" at the subpath's bounding box and keeping the dart only if:
/// - It's inside the shape
/// - It's not closer than `separation_disk_diameter` to any other point from a previous accepted dart throw
///
/// This repeats until accepted darts fill all possible areas between one another.
///
/// While the conceptual process described above asymptotically slows down and is never guaranteed to produce a maximal set in finite time,
@ -372,6 +377,7 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
/// Returns the manipulator point that is needed for a miter join if it is possible.
/// - `miter_limit`: Defines a limit for the ratio between the miter length and the stroke width.
///
/// Alternatively, this can be interpreted as limiting the angle that the miter can form.
/// When the limit is exceeded, no manipulator group will be returned.
/// This value should be at least 1. If not, the default of 4 will be used.

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,

View file

@ -38,35 +38,35 @@ let
in
# Make a shell with the dependencies we need
pkgs.mkShell {
packages = [
packages = with pkgs; [
rustc-wasm
pkgs.nodejs
pkgs.cargo
pkgs.cargo-watch
pkgs.cargo-nextest
pkgs.cargo-expand
pkgs.wasm-pack
pkgs.binaryen
pkgs.wasm-bindgen-cli
pkgs.vulkan-loader
pkgs.libxkbcommon
pkgs.llvm
pkgs.gcc-unwrapped.lib
pkgs.llvmPackages.libcxxStdenv
pkgs.pkg-config
nodejs
cargo
cargo-watch
cargo-nextest
cargo-expand
wasm-pack
binaryen
wasm-bindgen-cli
vulkan-loader
libxkbcommon
llvm
gcc-unwrapped.lib
llvmPackages.libcxxStdenv
pkg-config
# For Tauri
pkgs.openssl
pkgs.glib
pkgs.gtk3
pkgs.libsoup
pkgs.webkitgtk
openssl
glib
gtk3
libsoup
webkitgtk
# For Raw-rs tests
pkgs.libraw
libraw
# Use Mold as a linker
pkgs.mold
mold
];
# Hacky way to run Cargo through Mold