Add EffectGraph APIs to register anonymous nodes

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-30 16:02:31 +02:00
1 親 596187aeaa
コミット 4db990d03b
2ファイル変更
+85 -1

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

+56
ファイルの表示
@@ -55,6 +55,34 @@ partial class CanvasEffect
return Unsafe.As<T>(canvasImage);
}
/// <summary>
/// Registers an <see cref="ICanvasImage"/> object in the effect graph.
/// </summary>
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="canvasImage"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the effect graph does not support modifications at this time.</exception>
/// <remarks>
/// <para><inheritdoc cref="RegisterNode{T}(EffectNode{T}, T)" path="/remarks/node()"/></para>
/// <para>
/// This method can be used when <paramref name="canvasImage"/> doesn't need to be retrieved from <see cref="ConfigureEffectGraph"/>, ie.
/// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to
/// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed.
/// </para>
/// <para>
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterNode{T}(EffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
public void RegisterNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
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
// data structure (eg. a HashSet<ICanvasImage> instance) just to hold the anonymous effects that may be registered. This way,
// the same dictionary can be reused for all kinds of effect nodes instead. Anonymous effects are generally a nicher scenario.
_ = this.owner.transformNodes.TryAdd(new object(), canvasImage);
}
/// <summary>
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, associated with a given <see cref="EffectNode{T}"/> instance.
/// </summary>
@@ -83,6 +111,34 @@ partial class CanvasEffect
}
}
/// <summary>
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, and marks it as the output node for the effect graph being built.
/// </summary>
/// <remarks>
/// <para><inheritdoc cref="RegisterNode{T}(EffectNode{T}, T)" path="/remarks/node()"/></para>
/// <para>
/// This method can be used when <paramref name="canvasImage"/> doesn't need to be retrieved from <see cref="ConfigureEffectGraph"/>, ie.
/// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to
/// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed.
/// </para>
/// <para>
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterAndSetOutputNode{T}(EffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
/// <inheritdoc cref="RegisterNode(ICanvasImage)"/>
public void RegisterAndSetOutputNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null);
// Register the anonymous effect node with a dummy object, same as above
_ = this.owner.transformNodes.TryAdd(new object(), canvasImage);
// Store the anonymous output node for later use
this.owner.canvasImage = canvasImage;
}
/// <summary>
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, associated with a given <see cref="EffectNode{T}"/> instance.
/// Additionally, it also marks the input <see cref="ICanvasImage"/> object as the output node for the effect graph being built.
+29 -1
ファイルの表示
@@ -135,6 +135,17 @@ public partial class CanvasEffectTests
drawingSession.DrawImage(effect);
}
[TestMethod]
public unsafe void CanvasEffect_EffectOnlyUsingAnonymousNodes()
{
EffectOnlyUsingAnonymousNodes 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<PixelShaderEffect<ShaderWithNoInputs>> Effect = new();
@@ -199,6 +210,7 @@ public partial class CanvasEffectTests
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
{
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
@@ -248,9 +260,11 @@ public partial class CanvasEffectTests
{
void* ptr = &effectGraph;
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterNode(null!, (ColorSourceEffect?)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterAndSetOutputNode(null!, (ColorSourceEffect)null!));
@@ -274,7 +288,7 @@ public partial class CanvasEffectTests
private readonly ColorSourceEffect effect3 = new();
#pragma warning restore CA2213
protected unsafe override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(EffectGraph effectGraph)
{
effectGraph.RegisterNode(EffectNode1, this.effect1);
effectGraph.RegisterNode(EffectNode2, this.effect2);
@@ -296,6 +310,20 @@ public partial class CanvasEffectTests
}
}
private sealed class EffectOnlyUsingAnonymousNodes : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
{
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterAndSetOutputNode(new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
{
}
}
[D2DInputCount(0)]
[AutoConstructor]
private partial struct ShaderWithNoInputs : ID2D1PixelShader