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 transformNodes = new(); + /// /// The current cached result for , if available. /// private ICanvasImage? canvasImage; + /// + /// Indicates whether the current instance is within a call to . + /// + /// + /// This is used to validate method calls from within . That is, the + /// APIs will use this to detect whether they can be called. + /// + private bool isBuildingEffectGraph; + /// /// Indicates whether the current state has been invalidated (requiring to be called). /// @@ -40,10 +57,10 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I private bool isDisposed; /// - /// Builds the effect graph for the current instance, and returns an object - /// representing the output node of the graph. This image will then be passed to Win2D to perform the actual drawing, when needed. + /// Builds the effect graph for the current instance, and configures all effect nodes, as well as the output + /// node for the graph. That instance will then be passed to Win2D to perform the actual drawing, when needed. /// - /// The resulting object representing the output node of the effect graph. + /// The input value to use to build the effect graph. /// /// /// This method is called once before the current effect is drawn, and the resulting image is automatically cached and reused. @@ -74,29 +91,33 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// /// Create an instance of all necessary nodes in the effect graph. /// Connect the effect nodes as needed to build the connected graph. - /// Save all effects that can be configured as instance fields in the effect type. - /// Return the instance for the output node of the graph. + /// Register all effects as effect nodes in the graph, including the output node. /// /// /// - /// This method should never be called directly. It is automatically invoked when an is needed. + /// For convenience, it is recommended to store all necessary objects in + /// fields, so they can easily be accessed from and . + /// + /// + /// This method should never be called directly. It is automatically invoked when an effect graph is needed. /// /// - protected abstract ICanvasImage BuildEffectGraph(); + protected abstract void BuildEffectGraph(EffectGraph effectGraph); /// /// Configures the current effect graph whenever it is invalidated. /// + /// The input value to use to configure the effect graph. /// /// /// This method is guaranteed to be called after has been invoked already. As such, any instance /// fields that are set by can be assumed to never be when this method runs. /// /// - /// This method should never be called directly. It is used internally to configure the current instance. + /// This method should never be called directly. It is used internally to configure the current effect graph. /// /// - protected abstract void ConfigureEffectGraph(); + protected abstract void ConfigureEffectGraph(EffectGraph effectGraph); /// /// Invalidates the last returned result from . @@ -193,8 +214,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I // on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers. lock (this.lockObject) { - this.canvasImage?.Dispose(); - this.canvasImage = null; + DisposeEffectGraph(); } } diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems index 2bdd3aaf..16149cbc 100644 --- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems +++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems @@ -26,6 +26,7 @@ + diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index 94dbdfad..1a1630ad 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs @@ -92,7 +92,7 @@ partial class PixelShaderEffect internal static ImmutableArray Indices { get; } = GetIndices(); /// - /// The owning instance. + /// Gets the owning instance. /// internal PixelShaderEffect Owner { get; } diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.SourceCollection.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.SourceCollection.cs index 8fe5889b..69c3369d 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.SourceCollection.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.SourceCollection.cs @@ -78,7 +78,7 @@ partial class PixelShaderEffect } /// - /// The owning instance. + /// Gets the owning instance. /// internal PixelShaderEffect Owner { get; }