Skip to main content

lumen_engine/node/processing/
memo.rs

1use crate::node::{NodeId, NodeParamEvalContext, NodeParams, PortRef};
2
3use crate::gpu::{BoundFrame, CompiledOutput, FrameBindContext, GpuCompileNode, GpuCompiledNode};
4
5/// Aliases a raster input through a stable cache boundary.
6#[derive(Debug, Clone, Default, lumen_macros::Delegate)]
7pub struct MemoParams {
8    /// Stable cache identifier for this memo boundary.
9    #[meta(name = "Cache key", role = "cache_id")]
10    pub cache_id: String,
11    /// Allows property expressions to be evaluated across this memo boundary.
12    #[meta()]
13    pub allow_expressions: bool,
14}
15
16/// Aliases a raster input through a stable cache boundary.
17#[derive(Debug, Clone, lumen_macros::Node)]
18#[node(kind = "memo", name = "Memo", category = "processing")]
19pub struct Memo {
20    pub id: NodeId,
21    #[params]
22    pub params: MemoParamsDelegate,
23
24    #[input()]
25    pub source: PortRef,
26}
27
28impl Default for Memo {
29    fn default() -> Self {
30        Self {
31            id: NodeId::new(0),
32            params: MemoParamsDelegate::default(),
33            source: PortRef::empty(),
34        }
35    }
36}
37
38impl GpuCompileNode for Memo {
39    fn compile_gpu(
40        &self,
41        ctx: &mut crate::gpu::CompileContext<'_>,
42        port: &PortRef,
43    ) -> crate::Result<CompiledOutput> {
44        if port.port != "output" {
45            return Err(ctx.missing_output(self.id, &port.port));
46        }
47        let source = ctx.compile_port(&self.source)?;
48        ctx.register_compiled_node(CompiledMemo {
49            node_id: self.id,
50            params: self.params.clone(),
51        });
52        Ok(source)
53    }
54}
55
56#[derive(Debug, Clone)]
57struct CompiledMemo {
58    node_id: NodeId,
59    params: MemoParamsDelegate,
60}
61
62impl GpuCompiledNode for CompiledMemo {
63    fn node_id(&self) -> NodeId {
64        self.node_id
65    }
66
67    fn bind(&self, ctx: &FrameBindContext<'_>, _bound: &mut BoundFrame) -> crate::Result<()> {
68        let _ = self.params.eval(&NodeParamEvalContext {
69            node_id: self.node_id,
70            expr: &ctx.expr_context(self.node_id, "params"),
71        })?;
72        Ok(())
73    }
74}