Skip to content

Commit

Permalink
refactor: use ComputeGraph::compute_with instead of deprecated variants
Browse files Browse the repository at this point in the history
  • Loading branch information
maximmaxim345 committed Jan 7, 2025
1 parent ca22341 commit 45a72fb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
11 changes: 9 additions & 2 deletions crates/computegraph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//! ```
//!
Expand Down Expand Up @@ -1736,7 +1737,13 @@ impl ComputeGraph {
output: OutputPort<T>,
context: &ComputationContext,
) -> Result<T, ComputeError> {
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::<T>()
Expand Down
71 changes: 62 additions & 9 deletions crates/computegraph/tests/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>()
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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::<usize>()
Expand All @@ -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::<usize>()
Expand All @@ -127,7 +175,6 @@ fn test_context_fallback_generator() -> Result<()> {

Ok(())
}

#[test]
fn test_context_priority() -> Result<()> {
let mut graph = ComputeGraph::new();
Expand All @@ -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"
);
Expand Down
30 changes: 23 additions & 7 deletions crates/viewport/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(())
Expand All @@ -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());
Expand Down

0 comments on commit 45a72fb

Please sign in to comment.