diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
index 2177a0ce..ce032a96 100644
--- a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
+++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
@@ -38,10 +38,12 @@ partial class CanvasEffect
/// The object associated with in the effect graph.
/// Thrown if is .
/// Thrown if is not currently registered in the effect graph.
+ /// Thrown if the current instance is not valid.
public T GetNode(EffectNode effectNode)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
+ default(InvalidOperationException).ThrowIf(this.owner is null);
// Try to get the canvas image associated with the input effect node marker.
// This must have been previously registered in a call to BuildEffectGraph.
@@ -60,7 +62,9 @@ partial class CanvasEffect
///
/// The object to register in the effect graph.
/// Thrown if is .
- /// Thrown if the effect graph does not support modifications at this time.
+ ///
+ /// Thrown if the current instance is not valid or if the effect graph does not support modifications at this time.
+ ///
///
///
///
@@ -75,6 +79,7 @@ partial class CanvasEffect
public void RegisterNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
+ default(InvalidOperationException).ThrowIf(this.owner is null);
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
@@ -90,7 +95,9 @@ partial class CanvasEffect
/// 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 the current instance is not valid or 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
@@ -101,6 +108,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);
// Try to add the new canvas image associated with the input effect node marker.
@@ -129,6 +137,7 @@ partial class CanvasEffect
public void RegisterAndSetOutputNode(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);
@@ -149,6 +158,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.canvasImage is not null);
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
index a1dbca88..48490f43 100644
--- a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
+++ b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
@@ -146,6 +146,17 @@ public partial class CanvasEffectTests
drawingSession.DrawImage(effect);
}
+ [TestMethod]
+ public unsafe void CanvasEffect_EffectUsingEffectGraphInInvalidState()
+ {
+ EffectUsingEffectGraphInInvalidState 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();
@@ -258,6 +269,16 @@ public partial class CanvasEffectTests
{
protected override unsafe void BuildEffectGraph(EffectGraph effectGraph)
{
+ // Verify that if the effect graph is invalid, arguments are validated first
+ _ = Assert.ThrowsException(() => default(EffectGraph).RegisterNode(null!));
+ _ = 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!));
+
void* ptr = &effectGraph;
_ = Assert.ThrowsException(() => (*(EffectGraph*)ptr).RegisterNode(null!));
@@ -324,6 +345,24 @@ public partial class CanvasEffectTests
}
}
+ private sealed class EffectUsingEffectGraphInInvalidState : CanvasEffect
+ {
+ protected override void BuildEffectGraph(EffectGraph effectGraph)
+ {
+ _ = 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()));
+
+ effectGraph.RegisterAndSetOutputNode(new EffectNode(), new ColorSourceEffect());
+ }
+
+ protected override void ConfigureEffectGraph(EffectGraph effectGraph)
+ {
+ _ = Assert.ThrowsException(() => default(EffectGraph).GetNode(new EffectNode()));
+ }
+ }
+
[D2DInputCount(0)]
[AutoConstructor]
private partial struct ShaderWithNoInputs : ID2D1PixelShader