diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/PixelShaderEffect.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/PixelShaderEffect.cs index 8170940c..125c6e68 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/PixelShaderEffect.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/PixelShaderEffect.cs @@ -86,7 +86,7 @@ public abstract class PixelShaderEffect : CanvasEffect /// protected override void BuildEffectGraph(EffectGraph effectGraph) { - effectGraph.RegisterAndSetOutputNode(Effect, new PixelShaderEffect()); + effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect()); } /// diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs index fc297c83..863c2c2e 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs @@ -58,7 +58,7 @@ public sealed class D2D1ContouredLayersEffect : PixelShaderEffect PixelShaderEffect pixelShaderEffect = new() { ResourceTextureManagers = { [0] = resourceTextureManager } }; // Register the pixel shader effect as the output node in the effect graph - effectGraph.RegisterAndSetOutputNode(PixelShaderEffect, pixelShaderEffect); + effectGraph.RegisterOutputNode(PixelShaderEffect, pixelShaderEffect); } /// diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs index dbf741e3..65fd73ff 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs @@ -81,7 +81,7 @@ internal abstract class PixelShaderEffect : CanvasEffect /// protected override void BuildEffectGraph(EffectGraph effectGraph) { - effectGraph.RegisterAndSetOutputNode(Effect, new PixelShaderEffect()); + effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect()); } /// diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs index ce032a96..b3beefbf 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs @@ -80,7 +80,7 @@ partial class CanvasEffect { default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); // 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, @@ -109,7 +109,7 @@ partial class CanvasEffect default(ArgumentNullException).ThrowIfNull(effectNode); default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); // 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. @@ -130,16 +130,15 @@ partial class CanvasEffect /// 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. + /// If performing lookups on is required, use the overload. /// /// /// - public void RegisterAndSetOutputNode(ICanvasImage canvasImage) + public void RegisterOutputNode(ICanvasImage canvasImage) { default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); - default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); // Register the anonymous effect node with a dummy object, same as above _ = this.owner.transformNodes.TryAdd(new object(), canvasImage); @@ -153,14 +152,13 @@ partial class CanvasEffect /// Additionally, it also marks the input object as the output node for the effect graph being built. /// /// - public void RegisterAndSetOutputNode(EffectNode effectNode, T canvasImage) + public void RegisterOutputNode(EffectNode effectNode, T canvasImage) where T : class, ICanvasImage { default(ArgumentNullException).ThrowIfNull(effectNode); default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph); - default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); // 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)) @@ -171,6 +169,35 @@ partial class CanvasEffect // Store the output node for later use this.owner.canvasImage = canvasImage; } + + /// + /// Sets a previously registered instance as the output node for the effect graph. + /// + /// The type of object that will be used as output image. + /// The instance to set as output node for the effect graph. + /// Thrown if is . + /// Thrown if the current instance is not valid. + /// Thrown if is not registered in the effect graph. + /// + /// This method can be called from both and . It can be used for + /// efficiently changing the output node of a graph, without having to rebuild it. For instance, this can be used for effects that + /// have multiple paths that can be taken for their inputs, depending on the value of some property describing the effect behavior. + /// + public void SetOutputNode(EffectNode effectNode) + where T : class, ICanvasImage + { + default(ArgumentNullException).ThrowIfNull(effectNode); + default(InvalidOperationException).ThrowIf(this.owner is null); + + // Get the canvas image for the registered effect node (which has to exist at this point) + if (!this.owner.transformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage)) + { + default(ArgumentException).Throw(nameof(effectNode), "The specified output node is not registered in the effect graph."); + } + + // Store the new output node + this.owner.canvasImage = canvasImage; + } } /// diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs index 6628c398..88ef69c6 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs @@ -73,27 +73,32 @@ unsafe partial class CanvasEffect { default(ObjectDisposedException).ThrowIf(this.isDisposed, this); - // Build the effect graph and get the output node, if missing - if (this.canvasImage is null) + // Build the effect graph (the output node might not have been set yet) + if (this.invalidationType == InvalidationType.Creation) { - this.isBuildingEffectGraph = true; - DisposeEffectGraph(); BuildEffectGraph(new EffectGraph(this)); - this.isBuildingEffectGraph = false; - - default(InvalidOperationException).ThrowIf(this.canvasImage is null); + // We successfully got past the effect graph creation, so update the current + // invalidation state. This ensures that even if the configuration failed, the + // next time the effect is drawn again the graph won't be created again too. + this.invalidationType = InvalidationType.Update; } // Configure the effect graph, if the effect is invalidated - if (this.isInvalidated) + if (this.invalidationType == InvalidationType.Update) { ConfigureEffectGraph(new EffectGraph(this)); - this.isInvalidated = false; + // The effect graph is now ready to go: no further work will be done before drawing + // unless it is explicitly invalidated again, using either creation or update mode. + this.invalidationType = default; } + // At this point, there must be an output canvas image being set. + // If not, it means a derived type has forgot to set an output node. + default(InvalidOperationException).ThrowIf(this.canvasImage is null); + return this.canvasImage; } } diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.cs index 276bd8aa..c9cdbf63 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.cs @@ -29,22 +29,9 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I private ICanvasImage? canvasImage; /// - /// Indicates whether the current instance is within a call to . + /// Indicates the current invalidation state for the effect graph (which controls the logic in ). /// - /// - /// 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). - /// - /// - /// This is initially so that will always be called when - /// first creating the image, even if the effect has not bee invalidated. This ensures default parameters are set. - /// - private bool isInvalidated = true; + private InvalidationType invalidationType = InvalidationType.Creation; /// /// Indicates whether the effect is disposed. @@ -134,18 +121,13 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I { lock (this.transformNodes) { - // If the effect graph should be created again, dispose the image and throw it away - if (invalidationType == InvalidationType.Creation) - { - this.canvasImage?.Dispose(); - - this.canvasImage = null; - } - - // For both invalidation types, always mark the internal state as not being valid anymore. - // The next time the image is requested, the effect graph will be configured if needed. - // This matches the default value for the field (which is needed for the first realization). - this.isInvalidated = true; + // This method only updates the invalidation type. The next time the effect is drawn, + // the current graph (if existing) will be disposed automatically before building a + // new one. If the invalidation mode just needs an update, no disposal will happen. + // The new invalidation type is always set to the maximum between the current one and + // the requested one. This means that eg. if the current invalidation type is creation, + // and a property only needing an update is set, the creation request will persist. + this.invalidationType = (InvalidationType)Math.Max((byte)this.invalidationType, (byte)invalidationType); } } @@ -227,17 +209,8 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// /// Indicates the invalidation type to request when invoking and related methods. /// - protected enum InvalidationType + protected enum InvalidationType : byte { - /// - /// Fully invalidates the effect graph, causing it to be created again the next time it is needed. - /// - /// - /// This will cause the last returned instance to be disposed and discarded, - /// and to be called again the next time the effect is used for drawing. - /// - Creation, - /// /// Invalidates the state of the effect graph, causing it to be configured again the next time it is used. /// @@ -251,6 +224,15 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// This is much less expensive than creating the effect graph again, and should be preferred if possible. /// /// - Update + Update, + + /// + /// Fully invalidates the effect graph, causing it to be created again the next time it is needed. + /// + /// + /// This will cause the last returned instance to be disposed and discarded, + /// and to be called again the next time the effect is used for drawing. + /// + Creation } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs index 48490f43..52d60df4 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs +++ b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs @@ -1,12 +1,22 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Numerics; +using System.Threading.Tasks; +using ComputeSharp.D2D1.UI.Tests.Helpers; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp; #else using ComputeSharp.D2D1.WinUI; #endif +using ComputeSharp.SwapChain.Shaders.D2D1; using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.Effects; using Microsoft.VisualStudio.TestTools.UnitTesting; +using TerraFX.Interop.Windows; +using Windows.Foundation; +using Windows.Storage; +using Windows.UI; #nullable enable @@ -17,7 +27,7 @@ namespace ComputeSharp.D2D1.UI.Tests; public partial class CanvasEffectTests { [TestMethod] - public unsafe void CanvasEffect_IsRealizedAndInvalidatedCorrectly() + public void CanvasEffect_IsRealizedAndInvalidatedCorrectly() { EffectWithNoInputs effect = new(); @@ -66,32 +76,50 @@ public partial class CanvasEffectTests } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] - public unsafe void CanvasEffect_EffectRegisteringMultipleOutputNodes() + [ExpectedException(typeof(ArgumentException))] + public void CanvasEffect_EffectSettingNotRegisteredOutputNode() { - EffectRegisteringMultipleOutputNodes effect = new(); + EffectSettingNotRegisteredOutputNode effect = new(); - using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); - using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); + try + { + using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); + using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); - drawingSession.DrawImage(effect); + drawingSession.DrawImage(effect); + } + catch + { + Assert.IsTrue(effect.WasConfigureEffectGraphCalled); + + throw; + } } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] - public unsafe void CanvasEffect_EffectNotRegisteringAnOutputNode() + public void CanvasEffect_EffectNotRegisteringAnOutputNode() { EffectNotRegisteringAnOutputNode effect = new(); - using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); - using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); + try + { + using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); + using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); - drawingSession.DrawImage(effect); + drawingSession.DrawImage(effect); + } + catch + { + Assert.IsTrue(effect.WasConfigureEffectGraphCalled); + + throw; + } } [TestMethod] [ExpectedException(typeof(ArgumentException))] - public unsafe void CanvasEffect_EffectRegisteringNodesMultipleTimes1() + public void CanvasEffect_EffectRegisteringNodesMultipleTimes1() { EffectRegisteringNodesMultipleTimes1 effect = new(); @@ -103,7 +131,7 @@ public partial class CanvasEffectTests [TestMethod] [ExpectedException(typeof(ArgumentException))] - public unsafe void CanvasEffect_EffectRegisteringNodesMultipleTimes2() + public void CanvasEffect_EffectRegisteringNodesMultipleTimes2() { EffectRegisteringNodesMultipleTimes2 effect = new(); @@ -114,7 +142,7 @@ public partial class CanvasEffectTests } [TestMethod] - public unsafe void CanvasEffect_EffectRegisteringNullObjects() + public void CanvasEffect_EffectRegisteringNullObjects() { EffectRegisteringNullObjects effect = new(); @@ -125,7 +153,7 @@ public partial class CanvasEffectTests } [TestMethod] - public unsafe void CanvasEffect_EffectConfiguringGraphIncorrectly() + public void CanvasEffect_EffectConfiguringGraphIncorrectly() { EffectConfiguringGraphIncorrectly effect = new(); @@ -136,7 +164,7 @@ public partial class CanvasEffectTests } [TestMethod] - public unsafe void CanvasEffect_EffectOnlyUsingAnonymousNodes() + public void CanvasEffect_EffectOnlyUsingAnonymousNodes() { EffectOnlyUsingAnonymousNodes effect = new(); @@ -147,7 +175,7 @@ public partial class CanvasEffectTests } [TestMethod] - public unsafe void CanvasEffect_EffectUsingEffectGraphInInvalidState() + public void CanvasEffect_EffectUsingEffectGraphInInvalidState() { EffectUsingEffectGraphInInvalidState effect = new(); @@ -157,6 +185,200 @@ public partial class CanvasEffectTests drawingSession.DrawImage(effect); } + [TestMethod] + public async Task CanvasEffect_PixelShaderSwitchEffect() + { + PixelShaderSwitchEffect effect = new(); + + async Task TestAsync(string shaderName) + { + Color[] pixelColors; + + using CanvasDevice canvasDevice = new(); + + // Same logic as in the shader tests + using (CanvasRenderTarget renderTarget = new(canvasDevice, 1280, 720, 96.0f)) + { + using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession()) + { + drawingSession.DrawImage(effect); + } + + pixelColors = renderTarget.GetPixelColors(); + } + + StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/Shaders/{shaderName}.png")); + + using Stream stream = await imageFile.OpenStreamForReadAsync(); + using CanvasBitmap expected = await CanvasBitmap.LoadAsync(canvasDevice, stream.AsRandomAccessStream()); + using CanvasBitmap actual = CanvasBitmap.CreateFromColors(canvasDevice, pixelColors, 1280, 720); + + TolerantImageComparer.AssertEqual(expected, actual, threshold: 0.00001f); + } + + // Every time the effect is re-drawn, a different shader is set as the output node + await TestAsync(nameof(HelloWorld)); + await TestAsync(nameof(ColorfulInfinity)); + await TestAsync(nameof(FractalTiling)); + } + + [TestMethod] + public void CanvasEffect_EffectTestingDisposal() + { + EffectTestingDisposal effect = new(); + + using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f); + using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession(); + + // Force creating the graph (the drawing fails because the effects are dummies) + try + { + drawingSession.DrawImage(effect); + } + catch (Exception e) when (e.HResult == E.E_FAIL) + { + } + + Assert.AreEqual(1, effect.Effects1.Count); + Assert.AreEqual(1, effect.Effects2.Count); + Assert.AreEqual(1, effect.Effects3.Count); + Assert.AreEqual(0, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[0].NumberOfDisposeCalls); + + effect.InvalidateCreation(); + + // Effects aren't disposed yet until we redraw + Assert.AreEqual(1, effect.Effects1.Count); + Assert.AreEqual(1, effect.Effects2.Count); + Assert.AreEqual(1, effect.Effects3.Count); + Assert.AreEqual(0, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[0].NumberOfDisposeCalls); + + // Redraw, which builds a new effect graph and disposes the old one + try + { + drawingSession.DrawImage(effect); + } + catch (Exception e) when (e.HResult == E.E_FAIL) + { + } + + Assert.AreEqual(2, effect.Effects1.Count); + Assert.AreEqual(2, effect.Effects2.Count); + Assert.AreEqual(2, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[1].NumberOfDisposeCalls); + + // Redrawing now doesn't change anything + try + { + drawingSession.DrawImage(effect); + } + catch (Exception e) when (e.HResult == E.E_FAIL) + { + } + + Assert.AreEqual(2, effect.Effects1.Count); + Assert.AreEqual(2, effect.Effects2.Count); + Assert.AreEqual(2, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[1].NumberOfDisposeCalls); + + effect.InvalidateUpdate(); + + // Invalidating with update mode also doesn't do anything here + Assert.AreEqual(2, effect.Effects1.Count); + Assert.AreEqual(2, effect.Effects2.Count); + Assert.AreEqual(2, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[1].NumberOfDisposeCalls); + + // Redraw, and also verify the disposal state wasn't changed + try + { + drawingSession.DrawImage(effect); + } + catch (Exception e) when (e.HResult == E.E_FAIL) + { + } + + Assert.AreEqual(2, effect.Effects1.Count); + Assert.AreEqual(2, effect.Effects2.Count); + Assert.AreEqual(2, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[1].NumberOfDisposeCalls); + + // Verify that invalidating with update after creation is ignored + effect.InvalidateCreation(); + effect.InvalidateUpdate(); + + Assert.AreEqual(2, effect.Effects1.Count); + Assert.AreEqual(2, effect.Effects2.Count); + Assert.AreEqual(2, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[1].NumberOfDisposeCalls); + + // Redraw, disposing the old graph once again + try + { + drawingSession.DrawImage(effect); + } + catch (Exception e) when (e.HResult == E.E_FAIL) + { + } + + Assert.AreEqual(3, effect.Effects1.Count); + Assert.AreEqual(3, effect.Effects2.Count); + Assert.AreEqual(3, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[1].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects1[2].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects2[2].NumberOfDisposeCalls); + Assert.AreEqual(0, effect.Effects3[2].NumberOfDisposeCalls); + + // Finally dispose the effect + effect.Dispose(); + + Assert.AreEqual(3, effect.Effects1.Count); + Assert.AreEqual(3, effect.Effects2.Count); + Assert.AreEqual(3, effect.Effects3.Count); + Assert.AreEqual(1, effect.Effects1[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[0].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects1[1].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[1].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[1].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects1[2].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects2[2].NumberOfDisposeCalls); + Assert.AreEqual(1, effect.Effects3[2].NumberOfDisposeCalls); + } + private sealed class EffectWithNoInputs : CanvasEffect { private static readonly EffectNode> Effect = new(); @@ -185,7 +407,7 @@ public partial class CanvasEffectTests { NumberOfBuildEffectGraphCalls++; - effectGraph.RegisterAndSetOutputNode(Effect, new PixelShaderEffect()); + effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -203,22 +425,28 @@ public partial class CanvasEffectTests } } - private sealed class EffectRegisteringMultipleOutputNodes : CanvasEffect + private sealed class EffectSettingNotRegisteredOutputNode : CanvasEffect { + public bool WasConfigureEffectGraphCalled { get; private set; } + protected override void BuildEffectGraph(EffectGraph effectGraph) { - effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect()); - effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new EffectNode(), new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new EffectNode(), new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) { - Assert.Fail(); + WasConfigureEffectGraphCalled = true; + + effectGraph.SetOutputNode(new EffectNode()); } } private sealed class EffectNotRegisteringAnOutputNode : CanvasEffect { + public bool WasConfigureEffectGraphCalled { get; private set; } + protected override void BuildEffectGraph(EffectGraph effectGraph) { effectGraph.RegisterNode(new ColorSourceEffect()); @@ -228,7 +456,7 @@ public partial class CanvasEffectTests protected override void ConfigureEffectGraph(EffectGraph effectGraph) { - Assert.Fail(); + WasConfigureEffectGraphCalled = true; } } @@ -239,7 +467,7 @@ public partial class CanvasEffectTests EffectNode node = new(); effectGraph.RegisterNode(node, new ColorSourceEffect()); - effectGraph.RegisterAndSetOutputNode(node, new ColorSourceEffect()); + effectGraph.RegisterOutputNode(node, new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -256,7 +484,7 @@ public partial class CanvasEffectTests effectGraph.RegisterNode(node, new ColorSourceEffect()); effectGraph.RegisterNode(node, new ColorSourceEffect()); - effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new EffectNode(), new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -274,10 +502,10 @@ public partial class CanvasEffectTests _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(null!, new ColorSourceEffect())); _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(new EffectNode(), null!)); _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(null!, (ColorSourceEffect?)null!)); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(null!)); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(null!, new ColorSourceEffect())); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(new EffectNode(), null!)); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(null!, (ColorSourceEffect)null!)); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(null!)); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(null!, new ColorSourceEffect())); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(new EffectNode(), null!)); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(null!, (ColorSourceEffect)null!)); void* ptr = &effectGraph; @@ -285,12 +513,12 @@ public partial class CanvasEffectTests _ = 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!)); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterOutputNode(null!)); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterOutputNode(null!, new ColorSourceEffect())); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterOutputNode(new EffectNode(), null!)); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterOutputNode(null!, (ColorSourceEffect)null!)); - effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new EffectNode(), new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -313,7 +541,7 @@ public partial class CanvasEffectTests { effectGraph.RegisterNode(EffectNode1, this.effect1); effectGraph.RegisterNode(EffectNode2, this.effect2); - effectGraph.RegisterAndSetOutputNode(EffectNode3, this.effect3); + effectGraph.RegisterOutputNode(EffectNode3, this.effect3); } protected override unsafe void ConfigureEffectGraph(EffectGraph effectGraph) @@ -323,7 +551,7 @@ public partial class CanvasEffectTests _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).GetNode((EffectNode)null!)); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).GetNode(new EffectNode())); _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(new EffectNode(), new ColorSourceEffect())); - _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect())); + _ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterOutputNode(new EffectNode(), new ColorSourceEffect())); Assert.AreSame(effectGraph.GetNode(EffectNode1), this.effect1); Assert.AreSame(effectGraph.GetNode(EffectNode2), this.effect2); @@ -337,7 +565,7 @@ public partial class CanvasEffectTests { effectGraph.RegisterNode(new ColorSourceEffect()); effectGraph.RegisterNode(new ColorSourceEffect()); - effectGraph.RegisterAndSetOutputNode(new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -351,10 +579,10 @@ public partial class CanvasEffectTests { _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(new ColorSourceEffect())); _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(new EffectNode(), new ColorSourceEffect())); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(new ColorSourceEffect())); - _ = Assert.ThrowsException(() => default(EffectGraph).RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect())); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(new ColorSourceEffect())); + _ = Assert.ThrowsException(() => default(EffectGraph).RegisterOutputNode(new EffectNode(), new ColorSourceEffect())); - effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect()); + effectGraph.RegisterOutputNode(new EffectNode(), new ColorSourceEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph) @@ -363,6 +591,99 @@ public partial class CanvasEffectTests } } + private sealed class PixelShaderSwitchEffect : CanvasEffect + { + private static readonly EffectNode> HelloWorldNode = new(); + private static readonly EffectNode> ColorfulInfinityNode = new(); + private static readonly EffectNode> FractalTilingNode = new(); + + private int step; + + protected override void BuildEffectGraph(EffectGraph effectGraph) + { + effectGraph.RegisterNode(HelloWorldNode, new PixelShaderEffect { ConstantBuffer = new HelloWorld(0, new int2(1280, 720)) }); + effectGraph.RegisterNode(ColorfulInfinityNode, new PixelShaderEffect { ConstantBuffer = new ColorfulInfinity(0, new int2(1280, 720)) }); + effectGraph.RegisterNode(FractalTilingNode, new PixelShaderEffect { ConstantBuffer = new FractalTiling(0, new int2(1280, 720)) }); + effectGraph.SetOutputNode(HelloWorldNode); + } + + protected override void ConfigureEffectGraph(EffectGraph effectGraph) + { + switch (this.step++) + { + case 0: break; + case 1: + effectGraph.SetOutputNode(ColorfulInfinityNode); + break; + case 2: + effectGraph.SetOutputNode(FractalTilingNode); + break; + default: + Assert.Fail(); + break; + } + } + } + + private sealed class EffectTestingDisposal : CanvasEffect + { + private static readonly EffectNode EffectNode1 = new(); + private static readonly EffectNode EffectNode2 = new(); + private static readonly EffectNode EffectNode3 = new(); + public readonly List Effects1 = new(); + public readonly List Effects2 = new(); + public readonly List Effects3 = new(); + + public void InvalidateCreation() + { + InvalidateEffectGraph(InvalidationType.Creation); + } + + public void InvalidateUpdate() + { + InvalidateEffectGraph(InvalidationType.Update); + } + + protected override void BuildEffectGraph(EffectGraph effectGraph) + { + this.Effects1.Add(new DummyCanvasImageTrackingDisposal()); + this.Effects2.Add(new DummyCanvasImageTrackingDisposal()); + this.Effects3.Add(new DummyCanvasImageTrackingDisposal()); + + effectGraph.RegisterNode(EffectNode1, this.Effects1[^1]); + effectGraph.RegisterNode(EffectNode2, this.Effects2[^1]); + effectGraph.RegisterOutputNode(EffectNode3, this.Effects3[^1]); + } + + protected override void ConfigureEffectGraph(EffectGraph effectGraph) + { + } + } + + private sealed class DummyCanvasImageTrackingDisposal : ICanvasImage + { + public int NumberOfDisposeCalls { get; private set; } + + public Rect GetBounds(ICanvasResourceCreator resourceCreator) + { + Assert.Fail(); + + return default; + } + + public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) + { + Assert.Fail(); + + return default; + } + + public void Dispose() + { + NumberOfDisposeCalls++; + } + } + [D2DInputCount(0)] [AutoConstructor] private partial struct ShaderWithNoInputs : ID2D1PixelShader diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs index ebc9b3f3..9a4a8b86 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs @@ -132,7 +132,7 @@ public class ShadersTests protected override void BuildEffectGraph(EffectGraph effectGraph) { - effectGraph.RegisterAndSetOutputNode(Effect, new PixelShaderEffect()); + effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect()); } protected override void ConfigureEffectGraph(EffectGraph effectGraph)