diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.cs index 30546024..a36c1dc6 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.cs @@ -1,3 +1,4 @@ +using System; using ComputeSharp.D2D1.Interop; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp.Extensions; @@ -92,6 +93,9 @@ unsafe partial class PixelShaderEffect } else { + // If the effect is not realized, manually ensure the transform isn't null + default(ArgumentNullException).ThrowIfNull(value); + this.transformMapper = value; } } diff --git a/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs index 8ab42cf7..47e06960 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using ComputeSharp.D2D1.Uwp; using Microsoft.Graphics.Canvas; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -72,6 +73,97 @@ public partial class PixelShaderEffectTests Assert.IsTrue(actual.SequenceEqual(expected)); } + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void SourcesOutOfRange_ThrowsArgumentOutOfRangeException() + { + PixelShaderEffect effect0 = new(); + + _ = Assert.ThrowsException(() => effect0.Sources[0] = null); + _ = Assert.ThrowsException(() => effect0.Sources[1] = null); + + PixelShaderEffect effect1 = new(); + + effect1.Sources[0] = null; + + _ = Assert.ThrowsException(() => effect1.Sources[1] = null); + _ = Assert.ThrowsException(() => effect1.Sources[2] = null); + + PixelShaderEffect effect2 = new(); + + effect2.Sources[0] = null; + effect2.Sources[1] = null; + + _ = Assert.ThrowsException(() => effect2.Sources[2] = null); + _ = Assert.ThrowsException(() => effect2.Sources[3] = null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void NullTransformMapper_UnrealizedEffect_ThrowsArgumentNullException() + { + PixelShaderEffect effect = new(); + + effect.TransformMapper = null!; + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void NullTransformMapper_RealizedEffect_ThrowsArgumentNullException() + { + PixelShaderEffect effect = new(); + + using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f)) + using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession()) + { + drawingSession.DrawImage(effect); + } + + effect.TransformMapper = null!; + } + + [TestMethod] + [ExpectedException(typeof(Exception), AllowDerivedTypes = true)] + public unsafe void GraphCycle_IsDetectedCorrectly() + { + PixelShaderEffect effect1 = new(); + PixelShaderEffect effect2 = new(); + PixelShaderEffect effect3 = new(); + PixelShaderEffect effect4 = new(); + PixelShaderEffect effect5 = new(); + PixelShaderEffect effect6 = new(); + PixelShaderEffect effect7 = new(); + + // Build a graph with a cycle in it: + // + // ┌───┐ ┌───┐ ┌───┐ + // │ 1 ├──►│ 2 ├──►│ 3 ├──────────┐ + // └───┘ └───┘ └───┘ ▼ + // ┌───┐ ┌───┐ ▲ │ ┌───┐ ┌───┐ + // │ 7 ├──►│ 6 ├─┘ └─►│ 4 ├──►│ 5 │ + // └───┘ └───┘ └───┘ └───┘ + // ▲ │ + // └─────────────────┘ + // + // The cycle is 4 -> 6 -> 2 -> 3 -> 4. + effect2.Sources[0] = effect1; + effect2.Sources[1] = effect6; + effect3.Sources[0] = effect2; + effect4.Sources[0] = effect3; + effect5.Sources[0] = effect3; + effect5.Sources[1] = effect4; + effect6.Sources[0] = effect4; + effect6.Sources[1] = effect7; + + using ComPtr d2D1Image = default; + + // Realize the effect for it to detect a graph cycle. We specifically don't want to draw + // the effect, because in that case we'd still get an exception from D2D, even if the + // effect from ComputeSharp failed to detect a graph cycle. We want to make sure the + // detection and subsequent error is coming from our wrapper, not from D2D's logic. + Win2DHelper.GetD2DImage(effect5, new CanvasDevice(), d2D1Image.GetAddressOf()); + } + [D2DInputCount(0)] [AutoConstructor] private partial struct ShaderWithSomeProperties : ID2D1PixelShader @@ -87,4 +179,34 @@ public partial class PixelShaderEffectTests return default; } } + + [D2DInputCount(0)] + private partial struct ShaderWith0Inputs : ID2D1PixelShader + { + public float4 Execute() + { + return default; + } + } + + [D2DInputCount(1)] + [D2DInputSimple(0)] + private partial struct ShaderWith1Input : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0); + } + } + + [D2DInputCount(2)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + private partial struct ShaderWith2Inputs : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0) + D2D.GetInput(1); + } + } }