Skip to content

Commit 41288d7

Browse files
authored
Fix the Into nodes, which were broken but unused except in GPU nodes (#2480)
* Prototype document network level into node insertion * Fix generic type resolution * Cleanup * Remove network nesting
1 parent 9213291 commit 41288d7

File tree

8 files changed

+100
-47
lines changed

8 files changed

+100
-47
lines changed

editor/src/messages/portfolio/document/graph_operation/utility_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a> ModifyInputsContext<'a> {
292292
// If inserting a path node, insert a flatten vector elements if the type is a graphic group.
293293
// TODO: Allow the path node to operate on Graphic Group data by utilizing the reference for each vector data in a group.
294294
if node_definition.identifier == "Path" {
295-
let layer_input_type = self.network_interface.input_type(&InputConnector::node(output_layer.to_node(), 1), &[]).0.nested_type();
295+
let layer_input_type = self.network_interface.input_type(&InputConnector::node(output_layer.to_node(), 1), &[]).0.nested_type().clone();
296296
if layer_input_type == concrete!(GraphicGroupTable) {
297297
let Some(flatten_vector_elements_definition) = resolve_document_node_type("Flatten Vector Elements") else {
298298
log::error!("Flatten Vector Elements does not exist in ModifyInputsContext::existing_node_id");

editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::node_properties;
22
use super::utility_types::FrontendNodeType;
33
use crate::messages::layout::utility_types::widget_prelude::*;
44
use crate::messages::portfolio::document::utility_types::network_interface::{
5-
DocumentNodeMetadata, DocumentNodePersistentMetadata, NodeNetworkInterface, NodeNetworkMetadata, NodeNetworkPersistentMetadata, NodeTemplate, NodeTypePersistentMetadata, NumberInputSettings,
6-
PropertiesRow, Vec2InputSettings, WidgetOverride,
5+
DocumentNodeMetadata, DocumentNodePersistentMetadata, NodeNetworkInterface, NodeNetworkMetadata, NodeNetworkPersistentMetadata, NodePersistentMetadata, NodePosition, NodeTemplate,
6+
NodeTypePersistentMetadata, NumberInputSettings, PropertiesRow, Vec2InputSettings, WidgetOverride,
77
};
88
use crate::messages::portfolio::utility_types::PersistentData;
99
use crate::messages::prelude::Message;
@@ -2663,6 +2663,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
26632663
let node_registry = graphene_core::registry::NODE_REGISTRY.lock().unwrap();
26642664
'outer: for (id, metadata) in graphene_core::registry::NODE_METADATA.lock().unwrap().iter() {
26652665
use graphene_core::registry::*;
2666+
let id = id.clone();
26662667

26672668
for node in custom.iter() {
26682669
let DocumentNodeDefinition {
@@ -2673,7 +2674,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
26732674
..
26742675
} = node;
26752676
match implementation {
2676-
DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier { name }) if name == id => continue 'outer,
2677+
DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier { name }) if name == &id => continue 'outer,
26772678
_ => (),
26782679
}
26792680
}
@@ -2685,12 +2686,12 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
26852686
description,
26862687
properties,
26872688
} = metadata;
2688-
let Some(implementations) = &node_registry.get(id) else { continue };
2689+
let Some(implementations) = &node_registry.get(&id) else { continue };
26892690
let valid_inputs: HashSet<_> = implementations.iter().map(|(_, node_io)| node_io.call_argument.clone()).collect();
26902691
let first_node_io = implementations.first().map(|(_, node_io)| node_io).unwrap_or(const { &NodeIOTypes::empty() });
26912692
let mut input_type = &first_node_io.call_argument;
26922693
if valid_inputs.len() > 1 {
2693-
input_type = &const { generic!(T) };
2694+
input_type = &const { generic!(D) };
26942695
}
26952696
let output_type = &first_node_io.return_value;
26962697

@@ -2740,6 +2741,7 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
27402741
output_names: vec![output_type.to_string()],
27412742
has_primary_output: true,
27422743
locked: false,
2744+
27432745
..Default::default()
27442746
},
27452747
},

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl NodeNetworkInterface {
654654
let input_type = self.input_type(&InputConnector::node(*node_id, iterator_index), network_path).0;
655655
// Value inputs are stored as concrete, so they are compared to the nested type. Node inputs are stored as fn, so they are compared to the entire type.
656656
// For example a node input of (Footprint) -> VectorData would not be compatible with () -> VectorData
657-
node_io.inputs[iterator_index].clone().nested_type() == input_type || node_io.inputs[iterator_index] == input_type
657+
node_io.inputs[iterator_index].clone().nested_type() == &input_type || node_io.inputs[iterator_index] == input_type
658658
});
659659
if valid_implementation { node_io.inputs.get(*input_index).cloned() } else { None }
660660
})

editor/src/messages/tool/common_functionality/graph_modification_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl<'a> NodeGraphLayer<'a> {
420420

421421
/// Check if a layer is a raster layer
422422
pub fn is_raster_layer(layer: LayerNodeIdentifier, network_interface: &mut NodeNetworkInterface) -> bool {
423-
let layer_input_type = network_interface.input_type(&InputConnector::node(layer.to_node(), 1), &[]).0.nested_type();
423+
let layer_input_type = network_interface.input_type(&InputConnector::node(layer.to_node(), 1), &[]).0.nested_type().clone();
424424
if layer_input_type == concrete!(graphene_core::raster::image::ImageFrameTable<graphene_core::Color>)
425425
|| layer_input_type == concrete!(graphene_core::application_io::TextureFrameTable)
426426
|| layer_input_type == concrete!(graphene_std::RasterFrame)

node-graph/gcore/src/types.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,26 @@ impl Type {
293293
}
294294
}
295295

296-
pub fn nested_type(self) -> Type {
296+
pub fn nested_type(&self) -> &Type {
297297
match self {
298298
Self::Generic(_) => self,
299299
Self::Concrete(_) => self,
300300
Self::Fn(_, output) => output.nested_type(),
301301
Self::Future(output) => output.nested_type(),
302302
}
303303
}
304+
305+
pub fn replace_nested(&mut self, f: impl Fn(&Type) -> Option<Type>) -> Option<Type> {
306+
if let Some(replacement) = f(self) {
307+
return Some(std::mem::replace(self, replacement));
308+
}
309+
match self {
310+
Self::Generic(_) => None,
311+
Self::Concrete(_) => None,
312+
Self::Fn(_, output) => output.replace_nested(f),
313+
Self::Future(output) => output.replace_nested(f),
314+
}
315+
}
304316
}
305317

306318
fn format_type(ty: &str) -> String {

node-graph/graph-craft/src/document.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,16 @@ impl DocumentNode {
281281
self.inputs[index] = NodeInput::Node { node_id, output_index, lambda };
282282
let input_source = &mut self.original_location.inputs_source;
283283
for source in source {
284-
input_source.insert(source, index + self.original_location.skip_inputs - skip);
284+
input_source.insert(source, (index + self.original_location.skip_inputs).saturating_sub(skip));
285285
}
286286
}
287287

288288
fn resolve_proto_node(mut self) -> ProtoNode {
289289
assert!(!self.inputs.is_empty() || self.manual_composition.is_some(), "Resolving document node {self:#?} with no inputs");
290-
let DocumentNodeImplementation::ProtoNode(fqn) = self.implementation else {
290+
let DocumentNodeImplementation::ProtoNode(identifier) = self.implementation else {
291291
unreachable!("tried to resolve not flattened node on resolved node {self:?}");
292292
};
293293

294-
// TODO replace with proper generics removal
295-
let identifier = match fqn.name.clone().split_once('<') {
296-
Some((path, _generics)) => ProtoNodeIdentifier { name: Cow::Owned(path.to_string()) },
297-
_ => ProtoNodeIdentifier { name: fqn.name },
298-
};
299294
let (input, mut args) = if let Some(ty) = self.manual_composition {
300295
(ProtoNodeInput::ManualComposition(ty), ConstructionArgs::Nodes(vec![]))
301296
} else {

node-graph/graph-craft/src/proto.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl TypingContext {
696696
// Direct comparison of two concrete types.
697697
(Type::Concrete(type1), Type::Concrete(type2)) => type1 == type2,
698698
// Check inner type for futures
699-
(Type::Future(type1), Type::Future(type2)) => type1 == type2,
699+
(Type::Future(type1), Type::Future(type2)) => valid_type(type1, type2),
700700
// Direct comparison of two function types.
701701
// Note: in the presence of subtyping, functions are considered on a "greater than or equal to" basis of its function type's generality.
702702
// That means we compare their types with a contravariant relationship, which means that a more general type signature may be substituted for a more specific type signature.
@@ -728,16 +728,17 @@ impl TypingContext {
728728
let substitution_results = valid_output_types
729729
.iter()
730730
.map(|node_io| {
731-
collect_generics(node_io)
731+
let generics_lookup: Result<HashMap<_, _>, _> = collect_generics(node_io)
732732
.iter()
733-
.try_for_each(|generic| check_generic(node_io, &primary_input_or_call_argument, &inputs, generic).map(|_| ()))
734-
.map(|_| {
735-
if let Type::Generic(out) = &node_io.return_value {
736-
((*node_io).clone(), check_generic(node_io, &primary_input_or_call_argument, &inputs, out).unwrap())
737-
} else {
738-
((*node_io).clone(), node_io.return_value.clone())
739-
}
740-
})
733+
.map(|generic| check_generic(node_io, &primary_input_or_call_argument, &inputs, generic).map(|x| (generic.to_string(), x)))
734+
.collect();
735+
736+
generics_lookup.map(|generics_lookup| {
737+
let orig_node_io = (*node_io).clone();
738+
let mut new_node_io = orig_node_io.clone();
739+
replace_generics(&mut new_node_io, &generics_lookup);
740+
(new_node_io, orig_node_io)
741+
})
741742
})
742743
.collect::<Vec<_>>();
743744

@@ -783,8 +784,8 @@ impl TypingContext {
783784
.join("\n");
784785
Err(vec![GraphError::new(node, GraphErrorType::InvalidImplementations { inputs, error_inputs })])
785786
}
786-
[(org_nio, _)] => {
787-
let node_io = org_nio.clone();
787+
[(node_io, org_nio)] => {
788+
let node_io = node_io.clone();
788789

789790
// Save the inferred type
790791
self.inferred.insert(node_id, node_io.clone());
@@ -794,15 +795,15 @@ impl TypingContext {
794795
// If two types are available and one of them accepts () an input, always choose that one
795796
[first, second] => {
796797
if first.0.call_argument != second.0.call_argument {
797-
for (org_nio, _) in [first, second] {
798-
if org_nio.call_argument != concrete!(()) {
798+
for (node_io, orig_nio) in [first, second] {
799+
if node_io.call_argument != concrete!(()) {
799800
continue;
800801
}
801802

802803
// Save the inferred type
803-
self.inferred.insert(node_id, org_nio.clone());
804-
self.constructor.insert(node_id, impls[org_nio]);
805-
return Ok(org_nio.clone());
804+
self.inferred.insert(node_id, node_io.clone());
805+
self.constructor.insert(node_id, impls[orig_nio]);
806+
return Ok(node_io.clone());
806807
}
807808
}
808809
let inputs = [&primary_input_or_call_argument].into_iter().chain(&inputs).map(|t| t.to_string()).collect::<Vec<_>>().join(", ");
@@ -821,7 +822,7 @@ impl TypingContext {
821822

822823
/// Returns a list of all generic types used in the node
823824
fn collect_generics(types: &NodeIOTypes) -> Vec<Cow<'static, str>> {
824-
let inputs = [&types.call_argument].into_iter().chain(types.inputs.iter().flat_map(|x| x.fn_output()));
825+
let inputs = [&types.call_argument].into_iter().chain(types.inputs.iter().map(|x| x.nested_type()));
825826
let mut generics = inputs
826827
.filter_map(|t| match t {
827828
Type::Generic(out) => Some(out.clone()),
@@ -839,6 +840,7 @@ fn collect_generics(types: &NodeIOTypes) -> Vec<Cow<'static, str>> {
839840
fn check_generic(types: &NodeIOTypes, input: &Type, parameters: &[Type], generic: &str) -> Result<Type, String> {
840841
let inputs = [(Some(&types.call_argument), Some(input))]
841842
.into_iter()
843+
.chain(types.inputs.iter().map(|x| x.fn_input()).zip(parameters.iter().map(|x| x.fn_input())))
842844
.chain(types.inputs.iter().map(|x| x.fn_output()).zip(parameters.iter().map(|x| x.fn_output())));
843845
let concrete_inputs = inputs.filter(|(ni, _)| matches!(ni, Some(Type::Generic(input)) if generic == input));
844846
let mut outputs = concrete_inputs.flat_map(|(_, out)| out);
@@ -851,6 +853,21 @@ fn check_generic(types: &NodeIOTypes, input: &Type, parameters: &[Type], generic
851853
Ok(out_ty.clone())
852854
}
853855

856+
/// Returns a list of all generic types used in the node
857+
fn replace_generics(types: &mut NodeIOTypes, lookup: &HashMap<String, Type>) {
858+
let replace = |ty: &Type| {
859+
let Type::Generic(ident) = ty else {
860+
return None;
861+
};
862+
lookup.get(ident.as_ref()).cloned()
863+
};
864+
types.call_argument.replace_nested(replace);
865+
types.return_value.replace_nested(replace);
866+
for input in &mut types.inputs {
867+
input.replace_nested(replace);
868+
}
869+
}
870+
854871
#[cfg(test)]
855872
mod test {
856873
use super::*;

node-graph/interpreted-executor/src/node_registry.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ macro_rules! async_node {
4747
let node = <$path>::new($(
4848
graphene_std::any::PanicNode::<$arg, core::pin::Pin<Box<dyn core::future::Future<Output = $type> + Send>>>::new()
4949
),*);
50-
// TODO: Propagate the future type through the node graph
51-
// let params = vec![$(Type::Fn(Box::new(concrete!(())), Box::new(Type::Future(Box::new(concrete!($type)))))),*];
5250
let params = vec![$(fn_type_fut!($arg, $type)),*];
5351
let mut node_io = NodeIO::<'_, $input>::to_async_node_io(&node, params);
5452
node_io.call_argument = concrete!(<$input as StaticType>::Static);
@@ -58,6 +56,28 @@ macro_rules! async_node {
5856
};
5957
}
6058

59+
macro_rules! into_node {
60+
(from: $from:ty, to: $to:ty) => {
61+
(
62+
ProtoNodeIdentifier::new(concat!["graphene_core::ops::IntoNode<", stringify!($to), ">"]),
63+
|mut args| {
64+
Box::pin(async move {
65+
args.reverse();
66+
let node = graphene_core::ops::IntoNode::<$to>::new();
67+
let any: DynAnyNode<$from, _, _> = graphene_std::any::DynAnyNode::new(node);
68+
Box::new(any) as TypeErasedBox
69+
})
70+
},
71+
{
72+
let node = graphene_core::ops::IntoNode::<$to>::new();
73+
let mut node_io = NodeIO::<'_, $from>::to_async_node_io(&node, vec![]);
74+
node_io.call_argument = future!(<$from as StaticType>::Static);
75+
node_io
76+
},
77+
)
78+
};
79+
}
80+
6181
// TODO: turn into hashmap
6282
fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeConstructor>> {
6383
let node_types: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
@@ -68,15 +88,20 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
6888
// ),
6989
// async_node!(graphene_core::ops::IntoNode<ImageFrameTable<SRGBA8>>, input: ImageFrameTable<Color>, params: []),
7090
// async_node!(graphene_core::ops::IntoNode<ImageFrameTable<Color>>, input: ImageFrameTable<SRGBA8>, params: []),
71-
async_node!(graphene_core::ops::IntoNode<GraphicGroupTable>, input: ImageFrameTable<Color>, params: []),
72-
async_node!(graphene_core::ops::IntoNode<GraphicGroupTable>, input: VectorDataTable, params: []),
91+
into_node!(from: f64, to: f64),
92+
into_node!(from: ImageFrameTable<Color>, to: GraphicGroupTable),
93+
into_node!(from: f64,to: f64),
94+
into_node!(from: u32,to: f64),
95+
into_node!(from: u8,to: u32),
96+
into_node!(from: ImageFrameTable<Color>,to: GraphicGroupTable),
97+
into_node!(from: VectorDataTable,to: GraphicGroupTable),
7398
#[cfg(feature = "gpu")]
74-
async_node!(graphene_core::ops::IntoNode<&WgpuExecutor>, input: &WasmEditorApi, params: []),
75-
async_node!(graphene_core::ops::IntoNode<GraphicElement>, input: VectorDataTable, params: []),
76-
async_node!(graphene_core::ops::IntoNode<GraphicElement>, input: ImageFrameTable<Color>, params: []),
77-
async_node!(graphene_core::ops::IntoNode<GraphicElement>, input: GraphicGroupTable, params: []),
78-
async_node!(graphene_core::ops::IntoNode<GraphicGroupTable>, input: VectorDataTable, params: []),
79-
async_node!(graphene_core::ops::IntoNode<GraphicGroupTable>, input: ImageFrameTable<Color>, params: []),
99+
into_node!(from: &WasmEditorApi,to: &WgpuExecutor),
100+
into_node!(from: VectorDataTable,to: GraphicElement),
101+
into_node!(from: ImageFrameTable<Color>,to: GraphicElement),
102+
into_node!(from: GraphicGroupTable,to: GraphicElement),
103+
into_node!(from: VectorDataTable,to: GraphicGroupTable),
104+
into_node!(from: ImageFrameTable<Color>,to: GraphicGroupTable),
80105
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => ImageFrameTable<Color>]),
81106
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => ImageTexture]),
82107
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => VectorDataTable]),
@@ -304,9 +329,11 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
304329
// This occurs for the ChannelMixerNode presumably because of the long name.
305330
// This might be caused by the stringify! macro
306331
let mut new_name = id.name.replace('\n', " ");
307-
// Remove struct generics
308-
if let Some((path, _generics)) = new_name.split_once("<") {
309-
new_name = path.to_string();
332+
// Remove struct generics for all nodes except for the IntoNode
333+
if !new_name.contains("IntoNode") {
334+
if let Some((path, _generics)) = new_name.split_once("<") {
335+
new_name = path.to_string();
336+
}
310337
}
311338
let nid = ProtoNodeIdentifier { name: Cow::Owned(new_name) };
312339
map.entry(nid).or_default().insert(types.clone(), c);

0 commit comments

Comments
 (0)