From 7b8a553d23754b86f8b2c49abdd94b9e04f10f81 Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:21:42 +0100 Subject: [PATCH 1/5] feat(computegraph): mark compute_with_context methods as deprecated --- crates/computegraph/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index 4023f50..afa8282 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -1661,6 +1661,7 @@ impl ComputeGraph { /// - An input port of the node or a dependency of the node are not connected, and /// no value is provided via the context /// - A cycle is detected in the graph. + #[deprecated] pub fn compute_untyped_with_context( &self, output: OutputPortUntyped, @@ -1725,6 +1726,7 @@ impl ComputeGraph { /// - An input port of the node or a dependency of the node are not connected, and /// no value is provided via the context /// - A cycle is detected in the graph. + #[deprecated] pub fn compute_with_context( &self, output: OutputPort, From 4393464a7c9183c271f1b655880e8eab2f80d933 Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:22:25 +0100 Subject: [PATCH 2/5] docs(computegraph): add links to _with variants to compute methods --- crates/computegraph/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index afa8282..b5d9ec7 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -1173,6 +1173,8 @@ impl ComputeGraph { /// /// This function is the untyped version of [`ComputeGraph::compute`]. /// + /// Use [`ComputeGraph::compute_untyped_with`] when caching or a context are needed. + /// /// # Arguments /// /// * `output` - The output port to compute. @@ -1678,6 +1680,8 @@ impl ComputeGraph { /// Computes the result for a given output port. /// + /// Use [`ComputeGraph::compute_with`] when caching or a context are needed. + /// /// # Arguments /// /// * `output` - The output port to compute. From ca22341b86826c80dad00f5a04c1e88aca4f0c68 Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:37:05 +0100 Subject: [PATCH 3/5] docs(computegraph): change links to _compute_with --- crates/computegraph/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index b5d9ec7..44a7d45 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -499,13 +499,13 @@ enum Fallback { Generator(FallbackGenerator), } -/// Set predefined values for [`ComputeGraph::compute_with_context`]. +/// Set predefined values for [`ComputeGraph::compute_with`]. /// /// Use this container to: /// - Override values passed to [`InputPort`]s /// - Set fallback values for unconnected [`InputPort`]s /// -/// To be used with [`ComputeGraph::compute_with_context`] and [`ComputeGraph::compute_untyped_with_context`]. +/// To be used with [`ComputeGraph::compute_with`] and [`ComputeGraph::compute_untyped_with`]. #[derive(Debug, Default)] pub struct ComputationContext { overrides: Vec, From 45a72fbf76f6c435f97e071b0bfc0010970fe75a Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:29:18 +0100 Subject: [PATCH 4/5] refactor: use ComputeGraph::compute_with instead of deprecated variants --- crates/computegraph/src/lib.rs | 11 ++++- crates/computegraph/tests/context.rs | 71 ++++++++++++++++++++++++---- crates/viewport/src/pipeline.rs | 30 +++++++++--- 3 files changed, 94 insertions(+), 18 deletions(-) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index 44a7d45..fb5ed49 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -57,7 +57,8 @@ //! context.set_override(multiply_node.input_b(), 2); //! //! // Compute the result -//! let result = graph.compute_with_context(multiply_node.output(), &context).unwrap(); +//! let options = ComputationOptions {context: Some(&context) }; +//! let result = graph.compute_with(multiply_node.output(), &options, None).unwrap(); //! assert_eq!(result, (3 + 4) * 2); //! ``` //! @@ -1736,7 +1737,13 @@ impl ComputeGraph { output: OutputPort, context: &ComputationContext, ) -> Result { - let res = self.compute_untyped_with_context(output.port.clone(), context)?; + let res = self.compute_untyped_with( + output.port.clone(), + &ComputationOptions { + context: Some(context), + }, + None, + )?; let res = res .into_any() .downcast::() diff --git a/crates/computegraph/tests/context.rs b/crates/computegraph/tests/context.rs index 8a20e78..472fa98 100644 --- a/crates/computegraph/tests/context.rs +++ b/crates/computegraph/tests/context.rs @@ -18,13 +18,25 @@ fn test_context_override() -> Result<()> { ctx.set_override(addition.input_a(), 5); assert_eq!( - graph.compute_with_context(addition.output(), &ctx)?, + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, 8, "ctx should use the latest given value" ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -55,7 +67,13 @@ fn test_context_override_skip_dependencies() -> Result<()> { assert_eq!( graph - .compute_with_context(addition.output(), &ctx) + .compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + ) .expect("This should skip 'invalid_dep' entirely"), 15 ); @@ -85,10 +103,25 @@ fn test_context_fallback() -> Result<()> { ctx.set_fallback(5_usize); ctx.set_fallback(10_usize); - assert_eq!(graph.compute_with_context(addition.output(), &ctx)?, 20); + assert_eq!( + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, + 20 + ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -112,10 +145,25 @@ fn test_context_fallback_generator() -> Result<()> { ctx.set_fallback(5_usize); ctx.set_fallback_generator(|_name| 10_usize); - assert_eq!(graph.compute_with_context(addition.output(), &ctx)?, 20); + assert_eq!( + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, + 20 + ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -127,7 +175,6 @@ fn test_context_fallback_generator() -> Result<()> { Ok(()) } - #[test] fn test_context_priority() -> Result<()> { let mut graph = ComputeGraph::new(); @@ -144,7 +191,13 @@ fn test_context_priority() -> Result<()> { ctx.set_fallback(10_usize); assert_eq!( - graph.compute_with_context(addition.output(), &ctx)?, + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, 1, "priority should be override > connected > fallback" ); diff --git a/crates/viewport/src/pipeline.rs b/crates/viewport/src/pipeline.rs index 69db9fd..d4778b7 100644 --- a/crates/viewport/src/pipeline.rs +++ b/crates/viewport/src/pipeline.rs @@ -1,7 +1,7 @@ use crate::ViewportEvent; use computegraph::{ - ComputationContext, ComputeGraph, DynamicNode, InputPort, InputPortUntyped, NodeFactory, - NodeHandle, OutputPort, OutputPortUntyped, + ComputationContext, ComputationOptions, ComputeGraph, DynamicNode, InputPort, InputPortUntyped, + NodeFactory, NodeHandle, OutputPort, OutputPortUntyped, }; use project::ProjectView; use std::any::TypeId; @@ -472,9 +472,13 @@ impl ViewportPipeline { let last_node = self.nodes.last().ok_or(ExecuteError::EmptyPipeline)?; let mut ctx = ComputationContext::default(); ctx.set_fallback(project_view); - let scene = self - .graph - .compute_with_context(last_node.scene_output.clone(), &ctx)?; + let scene = self.graph.compute_with( + last_node.scene_output.clone(), + &ComputationOptions { + context: Some(&ctx), + }, + None, + )?; Ok(scene) } @@ -500,7 +504,13 @@ impl ViewportPipeline { let result = scene .graph - .compute_untyped_with_context(scene.update_state_out, &ctx) + .compute_untyped_with( + scene.update_state_out, + &ComputationOptions { + context: Some(&ctx), + }, + None, + ) .map_err(ExecuteError::ComputeError)?; state.state = Some(result); Ok(()) @@ -525,7 +535,13 @@ impl ViewportPipeline { let result = scene .graph - .compute_with_context(scene.render_primitive_out, &ctx) + .compute_with( + scene.render_primitive_out, + &ComputationOptions { + context: Some(&ctx), + }, + None, + ) .map_err(ExecuteError::ComputeError); let a = ctx.remove_override_untyped(&scene.render_state_in); debug_assert!(a.is_some()); From 78504d4afc294c66a6246fcc0d223e747df593a9 Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:35:14 +0100 Subject: [PATCH 5/5] feat(computegraph): remove deprecated compute variants --- crates/computegraph/src/lib.rs | 83 ---------------------------------- 1 file changed, 83 deletions(-) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index fb5ed49..5bb3a1a 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -1640,45 +1640,6 @@ impl ComputeGraph { } } - /// Computes the result for a given output port using the provided context, returning a boxed value. - /// - /// This function is the untyped version of [`ComputeGraph::compute_with_context`]. - /// - /// This function behaves similarly to [`ComputeGraph::compute_untyped`], but uses - /// the values given in the context as described in [`ComputationContext`]. - /// - /// # Arguments - /// - /// * `output` - The output port to compute. - /// * `context` - Collection of values used to perform the computation. - /// - /// # Returns - /// - /// A result containing the computed boxed value or an error. - /// - /// # Errors - /// - /// An error is returned if: - /// - The node is not found. - /// - The node has the incorrect output type - /// - An input port of the node or a dependency of the node are not connected, and - /// no value is provided via the context - /// - A cycle is detected in the graph. - #[deprecated] - pub fn compute_untyped_with_context( - &self, - output: OutputPortUntyped, - context: &ComputationContext, - ) -> Result, ComputeError> { - self.compute_untyped_with( - output, - &ComputationOptions { - context: Some(context), - }, - None, - ) - } - /// Computes the result for a given output port. /// /// Use [`ComputeGraph::compute_with`] when caching or a context are needed. @@ -1709,50 +1670,6 @@ impl ComputeGraph { Ok(*res) } - /// Computes the result for a given output port using the provided context - /// - /// This function behaves similarly to [`ComputeGraph::compute`], but uses - /// the values given in the context as described in [`ComputationContext`]. - /// - /// # Arguments - /// - /// * `output` - The output port to compute. - /// * `context` - Collection of values used to perform the computation, - /// - /// # Returns - /// - /// A result containing the computed boxed value or an error. - /// - /// # Errors - /// - /// An error is returned if: - /// - The node is not found. - /// - The node has the incorrect output type - /// - An input port of the node or a dependency of the node are not connected, and - /// no value is provided via the context - /// - A cycle is detected in the graph. - #[deprecated] - pub fn compute_with_context( - &self, - output: OutputPort, - context: &ComputationContext, - ) -> Result { - let res = self.compute_untyped_with( - output.port.clone(), - &ComputationOptions { - context: Some(context), - }, - None, - )?; - let res = res - .into_any() - .downcast::() - .map_err(|_| ComputeError::OutputTypeMismatch { - node: output.port.node, - })?; - Ok(*res) - } - /// Computes the result for a given output port using the provided options. /// /// This function is the primary way to execute computations in the [`ComputeGraph`].