Add EffectNode<T> interfaces for better ergonomics

このコミットが含まれているのは:
Sergio Pedri committed 2023-05-01 19:05:10 +02:00
1 親 a58e5e8c40
コミット 635780b203
2ファイル変更
+77 -10

検索条件に一致するファイルはありません

+64 -10
ファイルの表示
@@ -33,14 +33,12 @@ partial class CanvasEffect
/// <summary>
/// Gets a previously registered <see cref="ICanvasImage"/> object associated with a given effect graph node.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to retrieve.</typeparam>
/// <param name="effectNode">The <see cref="EffectNode{T}"/> instance to use to lookup the <see cref="ICanvasImage"/> object to retrieve.</param>
/// <param name="effectNode">The <see cref="IEffectNode"/> instance to use to lookup the <see cref="ICanvasImage"/> object to retrieve.</param>
/// <returns>The <see cref="ICanvasImage"/> object associated with <paramref name="effectNode"/> in the effect graph.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not currently registered in the effect graph.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="EffectGraph"/> instance is not valid.</exception>
public T GetNode<T>(EffectNode<T> 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;
}
/// <summary>
/// Gets a previously registered <typeparamref name="T"/> object (an <see cref="ICanvasImage"/> instance) associated with a given effect graph node.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to retrieve.</typeparam>
/// <param name="effectNode">The <see cref="IEffectNode{T}"/> instance to use to lookup the <see cref="ICanvasImage"/> object to retrieve.</param>
/// <returns>The <typeparamref name="T"/> object associated with <paramref name="effectNode"/> in the effect graph.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not currently registered in the effect graph.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="EffectGraph"/> instance is not valid.</exception>
public T GetNode<T>(IEffectNode<T> 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<T> being registered can only associate T instances with them.
return Unsafe.As<T>(canvasImage);
@@ -171,10 +193,9 @@ partial class CanvasEffect
}
/// <summary>
/// Sets a previously registered <see cref="EffectNode{T}"/> instance as the output node for the effect graph.
/// Sets a previously registered <see cref="IEffectNode"/> instance as the output node for the effect graph.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object that will be used as output image.</typeparam>
/// <param name="effectNode">The <see cref="EffectNode{T}"/> instance to set as output node for the effect graph.</param>
/// <param name="effectNode">The <see cref="IEffectNode"/> instance to set as output node for the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="EffectGraph"/> instance is not valid.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not registered in the effect graph.</exception>
@@ -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.
/// </remarks>
public void SetOutputNode<T>(EffectNode<T> 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
}
}
/// <summary>
/// A marker interface for an effect node that was registered from an <see cref="EffectGraph"/> value.
/// </summary>
/// <remarks>
/// <para>
/// This interface allows using <see cref="EffectGraph.GetNode"/> and <see cref="EffectGraph.SetOutputNode"/> 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.
/// </para>
/// <para>
/// This interface only implemented by <see cref="EffectNode{T}"/> and it's not meant to be implemented by external types.
/// </para>
/// </remarks>
protected interface IEffectNode
{
}
/// <summary>
/// A marker interface for a generic effect node that was registered from an <see cref="EffectGraph"/> value.
/// </summary>
/// <typeparam name="T">The covariant type of <see cref="ICanvasImage"/> associated with the current effect node.</typeparam>
/// <remarks>
/// <para>
/// This interface allows using <see cref="EffectGraph.GetNode"/> and <see cref="EffectGraph.SetOutputNode"/> in more scenarios,
/// including with ternary expressions returning multiple concrete <see cref="EffectNode{T}"/> instances with a common image type.
/// </para>
/// <para>
/// This interface only implemented by <see cref="EffectNode{T}"/> and it's not meant to be implemented by external types.
/// </para>
/// </remarks>
protected interface IEffectNode<out T> : IEffectNode
where T : ICanvasImage
{
}
/// <summary>
/// A marker type for an effect node that can be registered and retrieved from an <see cref="EffectGraph"/> value.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> associated with the current effect node.</typeparam>
protected sealed class EffectNode<T>
protected sealed class EffectNode<T> : IEffectNode<T>
where T : class, ICanvasImage
{
}
+13
ファイルの表示
@@ -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<ShaderWithNoInputs>);
}
protected override void Dispose(bool disposing)
@@ -596,6 +601,8 @@ public partial class CanvasEffectTests
private static readonly EffectNode<PixelShaderEffect<HelloWorld>> HelloWorldNode = new();
private static readonly EffectNode<PixelShaderEffect<ColorfulInfinity>> ColorfulInfinityNode = new();
private static readonly EffectNode<PixelShaderEffect<FractalTiling>> FractalTilingNode = new();
private static readonly EffectNode<PixelShaderEffect<Octagrams>> OctagramsNode = new();
private static readonly EffectNode<PixelShaderEffect<ProteanClouds>> ProteanCloudsNode = new();
private int step;
@@ -604,6 +611,8 @@ public partial class CanvasEffectTests
effectGraph.RegisterNode(HelloWorldNode, new PixelShaderEffect<HelloWorld> { ConstantBuffer = new HelloWorld(0, new int2(1280, 720)) });
effectGraph.RegisterNode(ColorfulInfinityNode, new PixelShaderEffect<ColorfulInfinity> { ConstantBuffer = new ColorfulInfinity(0, new int2(1280, 720)) });
effectGraph.RegisterNode(FractalTilingNode, new PixelShaderEffect<FractalTiling> { ConstantBuffer = new FractalTiling(0, new int2(1280, 720)) });
effectGraph.RegisterNode(OctagramsNode, new PixelShaderEffect<Octagrams> { ConstantBuffer = new Octagrams(0, new int2(1280, 720)) });
effectGraph.RegisterNode(ProteanCloudsNode, new PixelShaderEffect<ProteanClouds> { 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;