Migrate pass through and value node to identity implementation

This commit is contained in:
Adam
2025-07-06 13:23:24 -07:00
parent 007812077b
commit 7ecd1d0054
11 changed files with 83 additions and 68 deletions

View File

@@ -1,13 +1,6 @@
use crate::Node;
use std::marker::PhantomData;
// TODO: Rename to "Passthrough"
/// Passes-through the input value without changing it. This is useful for rerouting wires for organization purposes.
#[node_macro::node(skip_impl)]
fn identity<'i, T: 'i + Send>(value: T) -> T {
value
}
// Type
// TODO: Document this
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]

View File

@@ -13,28 +13,28 @@ impl<'i, const N: u32, I> Node<'i, I> for IntNode<N> {
}
}
// #[derive(Default, Debug, Clone, Copy)]
// pub struct ValueNode<T>(pub T);
#[derive(Default, Debug, Clone, Copy)]
pub struct ValueNode<T>(pub T);
// impl<'i, T: 'i, I> Node<'i, I> for ValueNode<T> {
// type Output = &'i T;
// #[inline(always)]
// fn eval(&'i self, _input: I) -> Self::Output {
// &self.0
// }
// }
impl<'i, T: 'i, I> Node<'i, I> for ValueNode<T> {
type Output = &'i T;
#[inline(always)]
fn eval(&'i self, _input: I) -> Self::Output {
&self.0
}
}
// impl<T> ValueNode<T> {
// pub const fn new(value: T) -> ValueNode<T> {
// ValueNode(value)
// }
// }
impl<T> ValueNode<T> {
pub const fn new(value: T) -> ValueNode<T> {
ValueNode(value)
}
}
// impl<T> From<T> for ValueNode<T> {
// fn from(value: T) -> Self {
// ValueNode::new(value)
// }
// }
impl<T> From<T> for ValueNode<T> {
fn from(value: T) -> Self {
ValueNode::new(value)
}
}
#[derive(Default, Debug, Clone, Copy)]
pub struct AsRefNode<T: AsRef<U>, U>(pub T, PhantomData<U>);

View File

@@ -460,7 +460,7 @@ pub enum DocumentNodeImplementation {
impl Default for DocumentNodeImplementation {
fn default() -> Self {
Self::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"))
Self::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"))
}
}
@@ -916,7 +916,7 @@ impl NodeNetwork {
return;
};
// If the node is hidden, replace it with an identity node
let identity_node = DocumentNodeImplementation::ProtoNode("graphene_core::ops::IdentityNode".into());
let identity_node = DocumentNodeImplementation::ProtoNode("graphene_std::any::IdentityNode".into());
if !node.visible && node.implementation != identity_node {
node.implementation = identity_node;
@@ -1092,7 +1092,7 @@ impl NodeNetwork {
fn remove_id_node(&mut self, id: NodeId) -> Result<(), String> {
let node = self.nodes.get(&id).ok_or_else(|| format!("Node with id {id} does not exist"))?.clone();
if let DocumentNodeImplementation::ProtoNode(ident) = &node.implementation {
if ident.name == "graphene_core::ops::IdentityNode" {
if ident.name == "graphene_std::any::IdentityNode" {
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;
@@ -1139,13 +1139,13 @@ impl NodeNetwork {
Ok(())
}
/// Strips out any [`graphene_core::ops::IdentityNode`]s that are unnecessary.
/// Strips out any [`graphene_std::any::IdentityNode`]s that are unnecessary.
pub fn remove_redundant_id_nodes(&mut self) {
let id_nodes = self
.nodes
.iter()
.filter(|(_, node)| {
matches!(&node.implementation, DocumentNodeImplementation::ProtoNode(ident) if ident == &ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"))
matches!(&node.implementation, DocumentNodeImplementation::ProtoNode(ident) if ident == &ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"))
&& node.inputs.len() == 1
&& matches!(node.inputs[0], NodeInput::Node { .. })
})
@@ -1333,7 +1333,7 @@ mod test {
fn extract_node() {
let id_node = DocumentNode {
inputs: vec![],
implementation: DocumentNodeImplementation::ProtoNode("graphene_core::ops::IdentityNode".into()),
implementation: DocumentNodeImplementation::ProtoNode("graphene_std::any::IdentityNode".into()),
..Default::default()
};
// TODO: Extend test cases to test nested network
@@ -1535,7 +1535,13 @@ mod test {
NodeId(1),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 0)],
<<<<<<< HEAD
implementation: DocumentNodeImplementation::ProtoNode(graphene_core::ops::identity::IDENTIFIER),
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
=======
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
..Default::default()
},
),
@@ -1543,7 +1549,13 @@ mod test {
NodeId(2),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 1)],
<<<<<<< HEAD
implementation: DocumentNodeImplementation::ProtoNode(graphene_core::ops::identity::IDENTIFIER),
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
=======
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
..Default::default()
},
),
@@ -1570,7 +1582,13 @@ mod test {
NodeId(2),
DocumentNode {
inputs: vec![result_node_input],
<<<<<<< HEAD
implementation: DocumentNodeImplementation::ProtoNode(graphene_core::ops::identity::IDENTIFIER),
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
=======
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
..Default::default()
},
),

View File

@@ -143,7 +143,7 @@ pub struct ProtoNode {
impl Default for ProtoNode {
fn default() -> Self {
Self {
identifier: ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode"),
identifier: ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"),
construction_args: ConstructionArgs::Value(value::TaggedValue::U32(0).into()),
input: ProtoNodeInput::None,
original_location: OriginalLocation::default(),

View File

@@ -47,20 +47,19 @@ pub fn downcast_node<I: StaticType, O: StaticType>(n: SharedNodeContainer) -> Do
DowncastBothNode::new(n)
}
// Same idea as the identity node, but with a hidden primary input in order to have an auto generated properties widget for the value
pub struct Value {
pub struct IdentityNode {
value: SharedNodeContainer,
}
impl<'i> Node<'i, Any<'i>> for Value {
impl<'i> Node<'i, Any<'i>> for IdentityNode {
type Output = DynFuture<'i, Any<'i>>;
fn eval(&'i self, input: Any<'i>) -> Self::Output {
Box::pin(async move { self.value.eval(input).await })
}
}
impl Value {
impl IdentityNode {
pub const fn new(value: SharedNodeContainer) -> Self {
Value { value }
IdentityNode { value }
}
}

View File

@@ -20,7 +20,13 @@ mod tests {
NodeId(0),
DocumentNode {
inputs: vec![NodeInput::network(concrete!(u32), 0)],
<<<<<<< HEAD
implementation: DocumentNodeImplementation::ProtoNode(ops::identity::IDENTIFIER),
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode")),
=======
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::any::IdentityNode")),
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
..Default::default()
},
),

View File

@@ -14,7 +14,7 @@ use graphene_std::Context;
use graphene_std::GraphicElement;
#[cfg(feature = "gpu")]
use graphene_std::any::DowncastBothNode;
use graphene_std::any::{ComposeTypeErased, DynAnyNode, IntoTypeErasedNode, Value};
use graphene_std::any::{ComposeTypeErased, DynAnyNode, IdentityNode, IntoTypeErasedNode};
use graphene_std::application_io::{ImageTexture, SurfaceFrame};
#[cfg(feature = "gpu")]
use graphene_std::wasm_application_io::{WasmEditorApi, WasmSurfaceHandle};
@@ -115,10 +115,10 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
),
),
(
ProtoNodeIdentifier::new("graphene_core::any::ValueNode"),
ProtoNodeIdentifier::new("graphene_std::any::IdentityNode"),
|args| {
Box::pin(async move {
let node = Value::new(args[0].clone());
let node = IdentityNode::new(args[0].clone());
node.into_type_erased()
})
},

View File

@@ -68,7 +68,13 @@ pub fn wrap_network_in_scope(mut network: NodeNetwork, editor_api: Arc<WasmEdito
inner_network,
render_node,
DocumentNode {
<<<<<<< HEAD
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::ops::identity::IDENTIFIER),
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
implementation: DocumentNodeImplementation::proto("graphene_core::ops::IdentityNode"),
=======
implementation: DocumentNodeImplementation::proto("graphene_std::any::IdentityNode"),
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
inputs: vec![NodeInput::value(TaggedValue::EditorApi(editor_api), false)],
..Default::default()
},

View File

@@ -49,7 +49,13 @@ pub fn generate_node_substitutions() -> HashMap<ProtoNodeIdentifier, DocumentNod
let input_count = inputs.len();
let network_inputs = (0..input_count).map(|i| NodeInput::node(NodeId(i as u64), 0)).collect();
<<<<<<< HEAD
let identity_node = ops::identity::IDENTIFIER;
||||||| parent of 8e045313 (Migrate pass through and value node to identity implementation)
let identity_node = ProtoNodeIdentifier::new("graphene_core::ops::IdentityNode");
=======
let identity_node = ProtoNodeIdentifier::new("graphene_std::any::IdentityNode");
>>>>>>> 8e045313 (Migrate pass through and value node to identity implementation)
let into_node_registry = &interpreted_executor::node_registry::NODE_REGISTRY;