diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
new file mode 100644
index 00000000..4e4d0f47
--- /dev/null
+++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
@@ -0,0 +1,118 @@
+using System;
+using System.Runtime.CompilerServices;
+using Microsoft.Graphics.Canvas;
+
+#if WINDOWS_UWP
+namespace ComputeSharp.D2D1.Uwp;
+#else
+namespace ComputeSharp.D2D1.WinUI;
+#endif
+
+///
+partial class CanvasEffect
+{
+ ///
+ /// An object representing an effect graph being built or configured.
+ ///
+ protected readonly ref struct EffectGraph
+ {
+ ///
+ ///The owning instance.
+ ///
+ private readonly CanvasEffect owner;
+
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The owning instance.
+ internal EffectGraph(CanvasEffect owner)
+ {
+ this.owner = owner;
+ }
+
+ ///
+ /// Gets a previously registered object associated with a given effect graph node.
+ ///
+ /// The type of object to retrieve.
+ /// The instance to use to lookup the object to retrieve.
+ /// The object associated with in the effect graph.
+ /// Thrown if is .
+ /// Thrown if is not currently registered in the effect graph.
+ public T GetNode(EffectNode effectNode)
+ where T : class, ICanvasImage
+ {
+ default(ArgumentNullException).ThrowIfNull(effectNode);
+
+ // Try to get the canvas image associated with the input effect node marker.
+ // This must have been previously registered in a call to BuildEffectGraph.
+ if (!this.owner.transformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
+ {
+ default(ArgumentException).Throw(nameof(effectNode), "The specified node is not registered in the effect graph.");
+ }
+
+ // Return the T node (we can skip the expensive cast since this is guaranteed to be valid).
+ // This is because EffectNode being registered can only associate T instances with them.
+ return Unsafe.As(canvasImage);
+ }
+
+ ///
+ /// Registers an object in the effect graph, associated with a given instance.
+ ///
+ /// The type of object to register.
+ /// The instance to use to register .
+ /// The object to register in the effect graph.
+ /// Thrown if or are .
+ /// Thrown if the effect graph does not support modifications at this time.
+ /// Thrown if is already registered in the effect graph.
+ ///
+ /// This method can only be called from an value being passed to . If this
+ /// method is called from within , it will fail, as the effect graph cannot be mutated from there.
+ ///
+ public void RegisterNode(EffectNode effectNode, T canvasImage)
+ where T : class, ICanvasImage
+ {
+ default(ArgumentNullException).ThrowIfNull(effectNode);
+ default(ArgumentNullException).ThrowIfNull(canvasImage);
+ default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
+
+ // Try to add the new canvas image associated with the input effect node marker.
+ // This must not have been previously added from another RegisterNode call.
+ if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage))
+ {
+ default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
+ }
+ }
+
+ ///
+ /// Registers an object in the effect graph, associated with a given instance.
+ /// Additionally, it also marks the input object as the output node for the effect graph being built.
+ ///
+ ///
+ public void RegisterAndSetOutputNode(EffectNode effectNode, T canvasImage)
+ where T : class, ICanvasImage
+ {
+ default(ArgumentNullException).ThrowIfNull(effectNode);
+ default(ArgumentNullException).ThrowIfNull(canvasImage);
+ default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
+ default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null);
+
+ // Try to add the output node as in the method above (but with an extra check before doing so)
+ if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage))
+ {
+ default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
+ }
+
+ // Store the output node for later use
+ this.owner.canvasImage = canvasImage;
+ }
+ }
+
+ ///
+ /// A marker type for an effect node that can be registered and retrieved from an value.
+ ///
+ /// The type of associated with the current effect node.
+ protected sealed class EffectNode
+ where T : class, ICanvasImage
+ {
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs
index e3a6ea74..e184095a 100644
--- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs
+++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs
@@ -73,11 +73,23 @@ unsafe partial class CanvasEffect
{
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
- this.canvasImage ??= BuildEffectGraph();
+ // Build the effect graph and get the output node, if missing
+ if (this.canvasImage is null)
+ {
+ this.isBuildingEffectGraph = true;
+ DisposeEffectGraph();
+ BuildEffectGraph(new EffectGraph(this));
+
+ this.isBuildingEffectGraph = false;
+
+ default(InvalidOperationException).ThrowIf(this.canvasImage is null);
+ }
+
+ // Configure the effect graph, if the effect is invalidated
if (this.isInvalidated)
{
- ConfigureEffectGraph();
+ ConfigureEffectGraph(new EffectGraph(this));
this.isInvalidated = false;
}
@@ -85,4 +97,22 @@ unsafe partial class CanvasEffect
return this.canvasImage;
}
}
+
+ ///
+ /// Disposes and clears the current effect graph, if any.
+ ///
+ private void DisposeEffectGraph()
+ {
+ // Dispose all registered canvas images
+ foreach (ICanvasImage canvasImage in this.transformNodes.Values)
+ {
+ canvasImage.Dispose();
+ }
+
+ // Also clear the current effect graph. Note that the canvas image used as output
+ // node for the effect graph does not need to be explicitly disposed here, as that
+ // object is guaranteed to have been part of the dictionary of transform nodes.
+ this.transformNodes.Clear();
+ this.canvasImage = null;
+ }
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.cs
index 898aa459..c7496205 100644
--- a/src/ComputeSharp.D2D1.UI/CanvasEffect.cs
+++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.cs
@@ -20,11 +20,28 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
///
private readonly object lockObject = new();
+ ///
+ /// The mapping of registered transform nodes for the current effect graph.
+ ///
+ ///
+ /// When not empty (ie. when an effect graph has been built), this will always also include .
+ ///
+ private readonly Dictionary