diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs index 4e4d0f47..2177a0ce 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs @@ -55,6 +55,34 @@ partial class CanvasEffect return Unsafe.As(canvasImage); } + /// + /// Registers an object in the effect graph. + /// + /// The object to register in the effect graph. + /// Thrown if is . + /// Thrown if the effect graph does not support modifications at this time. + /// + /// + /// + /// This method can be used when doesn't need to be retrieved from , ie. + /// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to + /// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed. + /// + /// + /// If performing lookups on is required, use the overload. + /// + /// + public void RegisterNode(ICanvasImage canvasImage) + { + default(ArgumentNullException).ThrowIfNull(canvasImage); + default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); + + // Use a new dummy object as key to register the anonymous effect node. This is cheaper than having to maintain a different + // data structure (eg. a HashSet instance) just to hold the anonymous effects that may be registered. This way, + // the same dictionary can be reused for all kinds of effect nodes instead. Anonymous effects are generally a nicher scenario. + _ = this.owner.transformNodes.TryAdd(new object(), canvasImage); + } + /// /// Registers an object in the effect graph, associated with a given instance. /// @@ -83,6 +111,34 @@ partial class CanvasEffect } } + /// + /// Registers an object in the effect graph, and marks it as the output node for the effect graph being built. + /// + /// + /// + /// + /// This method can be used when doesn't need to be retrieved from , ie. + /// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to + /// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed. + /// + /// + /// If performing lookups on is required, use the overload. + /// + /// + /// + public void RegisterAndSetOutputNode(ICanvasImage canvasImage) + { + default(ArgumentNullException).ThrowIfNull(canvasImage); + default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); + default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null); + + // Register the anonymous effect node with a dummy object, same as above + _ = this.owner.transformNodes.TryAdd(new object(), canvasImage); + + // Store the anonymous output node for later use + this.owner.canvasImage = canvasImage; + } + /// /// 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. diff --git a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs index b2787802..a1dbca88 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs +++ b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs @@ -135,6 +135,17 @@ public partial class CanvasEffectTests drawingSession.DrawImage(effect); } + [TestMethod] + public unsafe void CanvasEffect_EffectOnlyUsingAnonymousNodes() + { + EffectOnlyUsingAnonymousNodes effect = new(); + + using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); + using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); + + drawingSession.DrawImage(effect); + } + private sealed class EffectWithNoInputs : CanvasEffect { private static readonly EffectNode> Effect = new(); @@ -199,6 +210,7 @@ public partial class CanvasEffectTests { protected override void BuildEffectGraph(EffectGraph effectGraph) { + effectGraph.RegisterNode(new ColorSourceEffect()); effectGraph.RegisterNode(new EffectNode(), new ColorSourceEffect()); effectGraph.RegisterNode(new EffectNode(), new ColorSourceEffect()); } @@ -248,9 +260,11 @@ public partial class CanvasEffectTests { void* ptr = &effectGraph; + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(null!)); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(null!, new ColorSourceEffect())); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(new EffectNode(), null!)); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(null!, (ColorSourceEffect?)null!)); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!)); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!, new ColorSourceEffect())); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(new EffectNode(), null!)); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!, (ColorSourceEffect)null!)); @@ -274,7 +288,7 @@ public partial class CanvasEffectTests private readonly ColorSourceEffect effect3 = new(); #pragma warning restore CA2213 - protected unsafe override void BuildEffectGraph(EffectGraph effectGraph) + protected override void BuildEffectGraph(EffectGraph effectGraph) { effectGraph.RegisterNode(EffectNode1, this.effect1); effectGraph.RegisterNode(EffectNode2, this.effect2); @@ -296,6 +310,20 @@ public partial class CanvasEffectTests } } + private sealed class EffectOnlyUsingAnonymousNodes : CanvasEffect + { + protected override void BuildEffectGraph(EffectGraph effectGraph) + { + effectGraph.RegisterNode(new ColorSourceEffect()); + effectGraph.RegisterNode(new ColorSourceEffect()); + effectGraph.RegisterAndSetOutputNode(new ColorSourceEffect()); + } + + protected override void ConfigureEffectGraph(EffectGraph effectGraph) + { + } + } + [D2DInputCount(0)] [AutoConstructor] private partial struct ShaderWithNoInputs : ID2D1PixelShader