diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
index 1be88a83..9af0f2a7 100644
--- a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
+++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs
@@ -33,14 +33,12 @@ partial class CanvasEffect
///
/// 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 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.
/// Thrown if the current instance is not valid.
- public T GetNode(EffectNode effectNode)
- where T : class, ICanvasImage
+ public ICanvasImage GetNode(IEffectNode effectNode)
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
@@ -52,6 +50,30 @@ partial class CanvasEffect
default(ArgumentException).Throw(nameof(effectNode), "The specified node is not registered in the effect graph.");
}
+ return canvasImage;
+ }
+
+ ///
+ /// Gets a previously registered object (an instance) 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.
+ /// Thrown if the current instance is not valid.
+ public T GetNode(IEffectNode effectNode)
+ where T : class, ICanvasImage
+ {
+ default(ArgumentNullException).ThrowIfNull(effectNode);
+ default(InvalidOperationException).ThrowIf(this.owner is null);
+
+ // Retrieve the registered canvas image, same as above
+ 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);
@@ -171,10 +193,9 @@ partial class CanvasEffect
}
///
- /// Sets a previously registered instance as the output node for the effect graph.
+ /// 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.
+ /// 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.
@@ -183,8 +204,7 @@ partial class CanvasEffect
/// 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
+ public void SetOutputNode(IEffectNode effectNode)
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
@@ -200,11 +220,45 @@ partial class CanvasEffect
}
}
+ ///
+ /// A marker interface for an effect node that was registered from an value.
+ ///
+ ///
+ ///
+ /// This interface allows using and in more scenarios,
+ /// including where the concrete type of the image being used (eg. to set the output node) is not known (or needed) by the caller.
+ ///
+ ///
+ /// This interface only implemented by and it's not meant to be implemented by external types.
+ ///
+ ///
+ protected interface IEffectNode
+ {
+ }
+
+ ///
+ /// A marker interface for a generic effect node that was registered from an value.
+ ///
+ /// The covariant type of associated with the current effect node.
+ ///
+ ///
+ /// This interface allows using and in more scenarios,
+ /// including with ternary expressions returning multiple concrete instances with a common image type.
+ ///
+ ///
+ /// This interface only implemented by and it's not meant to be implemented by external types.
+ ///
+ ///
+ protected interface IEffectNode : IEffectNode
+ where T : ICanvasImage
+ {
+ }
+
///
/// 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
+ protected sealed class EffectNode : IEffectNode
where T : class, ICanvasImage
{
}
diff --git a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
index 52d60df4..6bdccd44 100644
--- a/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
+++ b/tests/ComputeSharp.D2D1.UI.Tests/CanvasEffectTests.cs
@@ -220,6 +220,8 @@ public partial class CanvasEffectTests
await TestAsync(nameof(HelloWorld));
await TestAsync(nameof(ColorfulInfinity));
await TestAsync(nameof(FractalTiling));
+ await TestAsync(nameof(Octagrams));
+ await TestAsync(nameof(ProteanClouds));
}
[TestMethod]
@@ -415,6 +417,9 @@ public partial class CanvasEffectTests
NumberOfConfigureEffectGraphCalls++;
effectGraph.GetNode(Effect).ConstantBuffer = new ShaderWithNoInputs(this.value);
+
+ // Also test the non-generic overload
+ Assert.IsTrue(effectGraph.GetNode((IEffectNode)Effect) is PixelShaderEffect);
}
protected override void Dispose(bool disposing)
@@ -596,6 +601,8 @@ public partial class CanvasEffectTests
private static readonly EffectNode> HelloWorldNode = new();
private static readonly EffectNode> ColorfulInfinityNode = new();
private static readonly EffectNode> FractalTilingNode = new();
+ private static readonly EffectNode> OctagramsNode = new();
+ private static readonly EffectNode> ProteanCloudsNode = new();
private int step;
@@ -604,6 +611,8 @@ public partial class CanvasEffectTests
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.RegisterNode(OctagramsNode, new PixelShaderEffect { ConstantBuffer = new Octagrams(0, new int2(1280, 720)) });
+ effectGraph.RegisterNode(ProteanCloudsNode, new PixelShaderEffect { ConstantBuffer = new ProteanClouds(0, new int2(1280, 720)) });
effectGraph.SetOutputNode(HelloWorldNode);
}
@@ -618,6 +627,10 @@ public partial class CanvasEffectTests
case 2:
effectGraph.SetOutputNode(FractalTilingNode);
break;
+ case 3 or 4:
+ // Test the overload which is non-generic, allowing ternary expressions too
+ effectGraph.SetOutputNode(this.step == 4 ? OctagramsNode : ProteanCloudsNode);
+ break;
default:
Assert.Fail();
break;