Merge pull request #796 from Sergio0694/dev/rename-canvas-effect-apis

Rename 'CanvasEffect' APIs
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2024-05-23 21:46:56 +02:00
コミット 94fe99a0a8
13ファイル変更
+413 -400

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

+3 -3
ファイルの表示
@@ -63,16 +63,16 @@ internal abstract class PixelShaderEffect : CanvasEffect
/// <summary>
/// The <see cref="PixelShaderEffect{T}"/> node in use.
/// </summary>
private static readonly EffectNode<PixelShaderEffect<T>> Effect = new();
private static readonly CanvasEffectNode<PixelShaderEffect<T>> Effect = new();
/// <inheritdoc/>
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect<T>());
}
/// <inheritdoc/>
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.GetNode(Effect).ConstantBuffer = factory(ElapsedTime, ScreenWidth, ScreenHeight);
}
+3 -3
ファイルの表示
@@ -63,16 +63,16 @@ public abstract class PixelShaderEffect : CanvasEffect
/// <summary>
/// The <see cref="PixelShaderEffect{T}"/> node in use.
/// </summary>
private static readonly EffectNode<PixelShaderEffect<T>> Effect = new();
private static readonly CanvasEffectNode<PixelShaderEffect<T>> Effect = new();
/// <inheritdoc/>
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect<T>());
}
/// <inheritdoc/>
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.GetNode(Effect).ConstantBuffer = factory(ElapsedTime, ScreenWidth, ScreenHeight);
}
+3 -3
ファイルの表示
@@ -16,10 +16,10 @@ public sealed class D2D1ContouredLayersEffect : PixelShaderEffect
/// <summary>
/// The reusable <see cref="PixelShaderEffect{T}"/> node to use to render frames.
/// </summary>
private static readonly EffectNode<PixelShaderEffect<ContouredLayers>> PixelShaderEffect = new();
private static readonly CanvasEffectNode<PixelShaderEffect<ContouredLayers>> PixelShaderEffect = new();
/// <inheritdoc/>
protected override unsafe void BuildEffectGraph(EffectGraph effectGraph)
protected override unsafe void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png");
@@ -56,7 +56,7 @@ public sealed class D2D1ContouredLayersEffect : PixelShaderEffect
}
/// <inheritdoc/>
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.GetNode(PixelShaderEffect).ConstantBuffer = new ContouredLayers((float)ElapsedTime.TotalSeconds, new int2(ScreenWidth, ScreenHeight));
}
-255
ファイルの表示
@@ -1,255 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
/// <inheritdoc/>
partial class CanvasEffect
{
/// <summary>
/// An object representing an effect graph being built or configured.
/// </summary>
protected readonly ref struct EffectGraph
{
/// <summary>
/// The owning <see cref="CanvasEffect"/> instance.
/// </summary>
private readonly CanvasEffect owner;
/// <summary>
/// Creates a new <see cref="EffectGraph"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="CanvasEffect"/> instance.</param>
internal EffectGraph(CanvasEffect owner)
{
this.owner = owner;
}
/// <summary>
/// Gets a previously registered <see cref="ICanvasImage"/> object associated with a given effect graph node.
/// </summary>
/// <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 ICanvasImage GetNode(IEffectNode effectNode)
{
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.
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 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);
}
/// <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 current <see cref="EffectGraph"/> instance is not valid or 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 is null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// 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>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to register.</typeparam>
/// <param name="effectNode">The <see cref="EffectNode{T}"/> instance to use to register <paramref name="canvasImage"/>.</param>
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> or <paramref name="canvasImage"/> are <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the current <see cref="EffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
/// </exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is already registered in the effect graph.</exception>
/// <remarks>
/// This method can only be called from an <see cref="EffectGraph"/> value being passed to <see cref="BuildEffectGraph"/>. If this
/// method is called from within <see cref="ConfigureEffectGraph"/>, it will fail, as the effect graph cannot be mutated from there.
/// </remarks>
public void RegisterNode<T>(EffectNode<T> effectNode, T canvasImage)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// Try to add the new canvas image associated with the input effect node marker.
// This must not have been previously added from another RegisterNode call.
if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
}
}
/// <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="RegisterOutputNode{T}(EffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
/// <inheritdoc cref="RegisterNode(ICanvasImage)"/>
public void RegisterOutputNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// 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.
/// </summary>
/// <inheritdoc cref="RegisterNode"/>
public void RegisterOutputNode<T>(EffectNode<T> effectNode, T canvasImage)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// Try to add the output node as in the method above (but with an extra check before doing so)
if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
}
// Store the output node for later use
this.owner.canvasImage = canvasImage;
}
/// <summary>
/// Sets a previously registered <see cref="IEffectNode"/> instance as the output node for the effect graph.
/// </summary>
/// <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>
/// <remarks>
/// This method can be called from both <see cref="BuildEffectGraph"/> and <see cref="ConfigureEffectGraph"/>. It can be used for
/// 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(IEffectNode effectNode)
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
// Get the canvas image for the registered effect node (which has to exist at this point)
if (!this.owner.transformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified output node is not registered in the effect graph.");
}
// Store the new output node
this.owner.canvasImage = canvasImage;
}
}
/// <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> : IEffectNode<T>
where T : class, ICanvasImage;
}
+13 -13
ファイルの表示
@@ -57,40 +57,40 @@ partial class CanvasEffect
/// </summary>
/// <returns>The current <see cref="ICanvasImage"/> instance.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the current instance is disposed.</exception>
[MemberNotNull(nameof(canvasImage))]
[MemberNotNull(nameof(CanvasImage))]
private ICanvasImage GetCanvasImage()
{
lock (this.transformNodes)
lock (this.TransformNodes)
{
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
// Build the effect graph (the output node might not have been set yet)
if (this.invalidationType == InvalidationType.Creation)
if (this.InvalidationType == CanvasEffectInvalidationType.Creation)
{
DisposeEffectGraph();
BuildEffectGraph(new EffectGraph(this));
BuildEffectGraph(new CanvasEffectGraph(this));
// We successfully got past the effect graph creation, so update the current
// invalidation state. This ensures that even if the configuration failed, the
// next time the effect is drawn again the graph won't be created again too.
this.invalidationType = InvalidationType.Update;
this.InvalidationType = CanvasEffectInvalidationType.Update;
}
// Configure the effect graph, if the effect is invalidated
if (this.invalidationType == InvalidationType.Update)
if (this.InvalidationType == CanvasEffectInvalidationType.Update)
{
ConfigureEffectGraph(new EffectGraph(this));
ConfigureEffectGraph(new CanvasEffectGraph(this));
// The effect graph is now ready to go: no further work will be done before drawing
// unless it is explicitly invalidated again, using either creation or update mode.
this.invalidationType = default;
this.InvalidationType = default;
}
// At this point, there must be an output canvas image being set.
// If not, it means a derived type has forgot to set an output node.
default(InvalidOperationException).ThrowIf(this.canvasImage is null, "No output node is set in the effect graph.");
default(InvalidOperationException).ThrowIf(this.CanvasImage is null, "No output node is set in the effect graph.");
return this.canvasImage;
return this.CanvasImage;
}
}
@@ -100,7 +100,7 @@ partial class CanvasEffect
private void DisposeEffectGraph()
{
// Dispose all registered canvas images
foreach (ICanvasImage canvasImage in this.transformNodes.Values)
foreach (ICanvasImage canvasImage in this.TransformNodes.Values)
{
canvasImage.Dispose();
}
@@ -108,7 +108,7 @@ partial class CanvasEffect
// Also clear the current effect graph. Note that the canvas image used as output
// node for the effect graph does not need to be explicitly disposed here, as that
// object is guaranteed to have been part of the dictionary of transform nodes.
this.transformNodes.Clear();
this.canvasImage = null;
this.TransformNodes.Clear();
this.CanvasImage = null;
}
}
+23 -47
ファイルの表示
@@ -15,19 +15,25 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// The mapping of registered transform nodes for the current effect graph.
/// </summary>
/// <remarks>
/// When not empty (ie. when an effect graph has been built), this will always also include <see cref="canvasImage"/>.
/// <para>
/// When not empty (ie. when an effect graph has been built), this will always also include <see cref="CanvasImage"/>.
/// </para>
/// <para>
/// This field and the two below are <see langword="internal"/> due to lack of <see langword="friend"/> modifier in C#.
/// The <see cref="CanvasEffectGraph"/> type is the only one that needs access to these fields to configure the effect.
/// </para>
/// </remarks>
private readonly Dictionary<object, ICanvasImage> transformNodes = [];
internal readonly Dictionary<object, ICanvasImage> TransformNodes = [];
/// <summary>
/// The current cached result for <see cref="GetCanvasImage"/>, if available.
/// </summary>
private ICanvasImage? canvasImage;
internal ICanvasImage? CanvasImage;
/// <summary>
/// Indicates the current invalidation state for the effect graph (which controls the logic in <see cref="GetCanvasImage"/>).
/// </summary>
private InvalidationType invalidationType = InvalidationType.Creation;
internal CanvasEffectInvalidationType InvalidationType = CanvasEffectInvalidationType.Creation;
/// <summary>
/// Indicates whether the effect is disposed.
@@ -38,14 +44,14 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// Builds the effect graph for the current <see cref="CanvasEffect"/> instance, and configures all effect nodes, as well as the output
/// node for the graph. That <see cref="ICanvasImage"/> instance will then be passed to Win2D to perform the actual drawing, when needed.
/// </summary>
/// <param name="effectGraph">The input <see cref="EffectGraph"/> value to use to build the effect graph.</param>
/// <param name="effectGraph">The input <see cref="CanvasEffectGraph"/> value to use to build the effect graph.</param>
/// <remarks>
/// <para>
/// This method is called once before the current effect is drawn, and the resulting image is automatically cached and reused.
/// It will remain in use until <see cref="InvalidateEffectGraph"/> is called with <see cref="InvalidationType.Creation"/>.
/// It will remain in use until <see cref="InvalidateEffectGraph"/> is called with <see cref="CanvasEffectInvalidationType.Creation"/>.
/// </para>
/// <para>
/// If the effect is invalidated with <see cref="InvalidationType.Update"/>, only <see cref="ConfigureEffectGraph"/> will be
/// If the effect is invalidated with <see cref="CanvasEffectInvalidationType.Update"/>, only <see cref="ConfigureEffectGraph"/> will be
/// called. As such, derived types should save any effect graph nodes that might need updates into instance fields, for later use.
/// </para>
/// <para>
@@ -73,19 +79,19 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// </list>
/// </para>
/// <para>
/// For convenience, it is recommended to store all necessary <see cref="EffectNode{T}"/> objects in <see langword="static"/>
/// For convenience, it is recommended to store all necessary <see cref="CanvasEffectNode{T}"/> objects in <see langword="static"/>
/// <see langword="readonly"/> fields, so they can easily be accessed from <see cref="BuildEffectGraph"/> and <see cref="ConfigureEffectGraph"/>.
/// </para>
/// <para>
/// This method should never be called directly. It is automatically invoked when an effect graph is needed.
/// </para>
/// </remarks>
protected abstract void BuildEffectGraph(EffectGraph effectGraph);
protected abstract void BuildEffectGraph(CanvasEffectGraph effectGraph);
/// <summary>
/// Configures the current effect graph whenever it is invalidated.
/// </summary>
/// <param name="effectGraph">The input <see cref="EffectGraph"/> value to use to configure the effect graph.</param>
/// <param name="effectGraph">The input <see cref="CanvasEffectGraph"/> value to use to configure the effect graph.</param>
/// <remarks>
/// <para>
/// This method is guaranteed to be called after <see cref="BuildEffectGraph"/> has been invoked already. As such, any instance
@@ -95,7 +101,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// This method should never be called directly. It is used internally to configure the current effect graph.
/// </para>
/// </remarks>
protected abstract void ConfigureEffectGraph(EffectGraph effectGraph);
protected abstract void ConfigureEffectGraph(CanvasEffectGraph effectGraph);
/// <summary>
/// Invalidates the last returned result from <see cref="GetCanvasImage"/>.
@@ -108,14 +114,14 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// or whether it simply needs to refresh its internal state (by calling <see cref="ConfigureEffectGraph"/>).
/// </para>
/// <para>
/// If <see cref="InvalidateEffectGraph"/> is called with <see cref="InvalidationType.Update"/>, the effect
/// If <see cref="InvalidateEffectGraph"/> is called with <see cref="CanvasEffectInvalidationType.Update"/>, the effect
/// graph will only be refreshed the next time the image is actually requested. That is, repeated requests
/// for updates do not result in unnecessarily calls to <see cref="ConfigureEffectGraph"/>.
/// </para>
/// </remarks>
protected void InvalidateEffectGraph(InvalidationType invalidationType = InvalidationType.Update)
protected void InvalidateEffectGraph(CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update)
{
lock (this.transformNodes)
lock (this.TransformNodes)
{
// This method only updates the invalidation type. The next time the effect is drawn,
// the current graph (if existing) will be disposed automatically before building a
@@ -123,7 +129,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
// The new invalidation type is always set to the maximum between the current one and
// the requested one. This means that eg. if the current invalidation type is creation,
// and a property only needing an update is set, the creation request will persist.
this.invalidationType = (InvalidationType)Math.Max((byte)this.invalidationType, (byte)invalidationType);
this.InvalidationType = (CanvasEffectInvalidationType)Math.Max((byte)this.InvalidationType, (byte)invalidationType);
}
}
@@ -135,7 +141,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// <param name="storage">The storage for the effect property value.</param>
/// <param name="value">The new effect property value to set.</param>
/// <param name="invalidationType">The invalidation type to request.</param>
protected void SetAndInvalidateEffectGraph<T>([NotNullIfNotNull(nameof(value))] ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update)
protected void SetAndInvalidateEffectGraph<T>([NotNullIfNotNull(nameof(value))] ref T storage, T value, CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
@@ -185,7 +191,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
// the lock itself might have already been collected by the time this code runs, which would lead the lock
// statement to throw a NullReferenceException. So if a finalizer is running, just let objects be collected
// on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers.
lock (this.transformNodes)
lock (this.TransformNodes)
{
DisposeEffectGraph();
}
@@ -201,34 +207,4 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
GC.SuppressFinalize(this);
}
/// <summary>
/// Indicates the invalidation type to request when invoking <see cref="InvalidateEffectGraph"/> and related methods.
/// </summary>
protected enum InvalidationType : byte
{
/// <summary>
/// Invalidates the state of the effect graph, causing it to be configured again the next time it is used.
/// </summary>
/// <remarks>
/// <para>
/// This will preserve the last returned <see cref="ICanvasImage"/> instance, if available, and it will only
/// mark the internal state as being out of date, resulting in <see cref="ConfigureEffectGraph"/> to be called
/// the next time the effect is used for drawing.
/// </para>
/// <para>
/// This is much less expensive than creating the effect graph again, and should be preferred if possible.
/// </para>
/// </remarks>
Update,
/// <summary>
/// Fully invalidates the effect graph, causing it to be created again the next time it is needed.
/// </summary>
/// <remarks>
/// This will cause the last returned <see cref="ICanvasImage"/> instance to be disposed and discarded,
/// and <see cref="BuildEffectGraph"/> to be called again the next time the effect is used for drawing.
/// </remarks>
Creation
}
}
+214
ファイルの表示
@@ -0,0 +1,214 @@
using System;
using System.Runtime.CompilerServices;
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
/// <summary>
/// An object representing an effect graph being built or configured.
/// </summary>
public readonly ref struct CanvasEffectGraph
{
/// <summary>
/// The owning <see cref="CanvasEffect"/> instance.
/// </summary>
private readonly CanvasEffect owner;
/// <summary>
/// Creates a new <see cref="CanvasEffectGraph"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="CanvasEffect"/> instance.</param>
internal CanvasEffectGraph(CanvasEffect owner)
{
this.owner = owner;
}
/// <summary>
/// Gets a previously registered <see cref="ICanvasImage"/> object associated with a given effect graph node.
/// </summary>
/// <param name="effectNode">The <see cref="ICanvasEffectNode"/> 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="CanvasEffectGraph"/> instance is not valid.</exception>
public ICanvasImage GetNode(ICanvasEffectNode effectNode)
{
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.
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 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="ICanvasEffectNode{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="CanvasEffectGraph"/> instance is not valid.</exception>
public T GetNode<T>(ICanvasEffectNode<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);
}
/// <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 current <see cref="CanvasEffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
/// </exception>
/// <remarks>
/// <para><inheritdoc cref="RegisterNode{T}(CanvasEffectNode{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="CanvasEffect.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}(CanvasEffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
public void RegisterNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// 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="CanvasEffectNode{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to register.</typeparam>
/// <param name="effectNode">The <see cref="CanvasEffectNode{T}"/> instance to use to register <paramref name="canvasImage"/>.</param>
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> or <paramref name="canvasImage"/> are <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
/// </exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is already registered in the effect graph.</exception>
/// <remarks>
/// This method can only be called from an <see cref="CanvasEffectGraph"/> value being passed to <see cref="CanvasEffect.BuildEffectGraph"/>. If this
/// method is called from within <see cref="CanvasEffect.ConfigureEffectGraph"/>, it will fail, as the effect graph cannot be mutated from there.
/// </remarks>
public void RegisterNode<T>(CanvasEffectNode<T> effectNode, T canvasImage)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// Try to add the new canvas image associated with the input effect node marker.
// This must not have been previously added from another RegisterNode call.
if (!this.owner.TransformNodes.TryAdd(effectNode, canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
}
}
/// <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}(CanvasEffectNode{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="CanvasEffect.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="RegisterOutputNode{T}(CanvasEffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
/// <inheritdoc cref="RegisterNode(ICanvasImage)"/>
public void RegisterOutputNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// 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="CanvasEffectNode{T}"/> instance.
/// Additionally, it also marks the input <see cref="ICanvasImage"/> object as the output node for the effect graph being built.
/// </summary>
/// <inheritdoc cref="RegisterNode"/>
public void RegisterOutputNode<T>(CanvasEffectNode<T> effectNode, T canvasImage)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
// Try to add the output node as in the method above (but with an extra check before doing so)
if (!this.owner.TransformNodes.TryAdd(effectNode, canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
}
// Store the output node for later use
this.owner.CanvasImage = canvasImage;
}
/// <summary>
/// Sets a previously registered <see cref="ICanvasEffectNode"/> instance as the output node for the effect graph.
/// </summary>
/// <param name="effectNode">The <see cref="ICanvasEffectNode"/> 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="CanvasEffectGraph"/> instance is not valid.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not registered in the effect graph.</exception>
/// <remarks>
/// This method can be called from both <see cref="CanvasEffect.BuildEffectGraph"/> and <see cref="CanvasEffect.ConfigureEffectGraph"/>.
/// It can be used for 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(ICanvasEffectNode effectNode)
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
// Get the canvas image for the registered effect node (which has to exist at this point)
if (!this.owner.TransformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified output node is not registered in the effect graph.");
}
// Store the new output node
this.owner.CanvasImage = canvasImage;
}
}
+34
ファイルの表示
@@ -0,0 +1,34 @@
using ABI.Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
/// <summary>
/// Indicates the invalidation type to request when invoking <see cref="CanvasEffect.InvalidateEffectGraph"/> and related methods.
/// </summary>
public enum CanvasEffectInvalidationType : byte
{
/// <summary>
/// Invalidates the state of the effect graph, causing it to be configured again the next time it is used.
/// </summary>
/// <remarks>
/// <para>
/// This will preserve the last returned <see cref="ICanvasImage"/> instance, if available, and it will only
/// mark the internal state as being out of date, resulting in <see cref="CanvasEffect.ConfigureEffectGraph"/> to be called
/// the next time the effect is used for drawing.
/// </para>
/// <para>
/// This is much less expensive than creating the effect graph again, and should be preferred if possible.
/// </para>
/// </remarks>
Update,
/// <summary>
/// Fully invalidates the effect graph, causing it to be created again the next time it is needed.
/// </summary>
/// <remarks>
/// This will cause the last returned <see cref="ICanvasImage"/> instance to be disposed and discarded,
/// and <see cref="CanvasEffect.BuildEffectGraph"/> to be called again the next time the effect is used for drawing.
/// </remarks>
Creation
}
+10
ファイルの表示
@@ -0,0 +1,10 @@
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
/// <summary>
/// A marker type for an effect node that can be registered and retrieved from an <see cref="CanvasEffectGraph"/> value.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> associated with the current effect node.</typeparam>
public sealed class CanvasEffectNode<T> : ICanvasEffectNode<T>
where T : class, ICanvasImage;
+15
ファイルの表示
@@ -0,0 +1,15 @@
namespace ComputeSharp.D2D1.WinUI;
/// <summary>
/// A marker interface for an effect node that was registered from an <see cref="CanvasEffectGraph"/> value.
/// </summary>
/// <remarks>
/// <para>
/// This interface allows using <see cref="CanvasEffectGraph.GetNode"/> and <see cref="CanvasEffectGraph.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="CanvasEffectNode{T}"/> and it's not meant to be implemented by external types.
/// </para>
/// </remarks>
public interface ICanvasEffectNode;
+19
ファイルの表示
@@ -0,0 +1,19 @@
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
/// <summary>
/// A marker interface for a generic effect node that was registered from an <see cref="CanvasEffectGraph"/> 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="CanvasEffectGraph.GetNode"/> and <see cref="CanvasEffectGraph.SetOutputNode"/> in more scenarios,
/// including with ternary expressions returning multiple concrete <see cref="CanvasEffectNode{T}"/> instances with a common image type.
/// </para>
/// <para>
/// This interface only implemented by <see cref="CanvasEffectNode{T}"/> and it's not meant to be implemented by external types.
/// </para>
/// </remarks>
public interface ICanvasEffectNode<out T> : ICanvasEffectNode
where T : ICanvasImage;
+73 -73
ファイルの表示
@@ -383,7 +383,7 @@ public partial class CanvasEffectTests
private sealed class EffectWithNoInputs : CanvasEffect
{
private static readonly EffectNode<PixelShaderEffect<ShaderWithNoInputs>> Effect = new();
private static readonly CanvasEffectNode<PixelShaderEffect<ShaderWithNoInputs>> Effect = new();
private int value;
@@ -396,7 +396,7 @@ public partial class CanvasEffectTests
public int ValueWithReload
{
get => this.value;
set => SetAndInvalidateEffectGraph(ref this.value, value, InvalidationType.Creation);
set => SetAndInvalidateEffectGraph(ref this.value, value, CanvasEffectInvalidationType.Creation);
}
public int NumberOfBuildEffectGraphCalls { get; private set; }
@@ -405,21 +405,21 @@ public partial class CanvasEffectTests
public int NumberOfDisposeCalls { get; private set; }
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
NumberOfBuildEffectGraphCalls++;
effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect<ShaderWithNoInputs>());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
NumberOfConfigureEffectGraphCalls++;
effectGraph.GetNode(Effect).ConstantBuffer = new ShaderWithNoInputs(this.value);
// Also test the non-generic overload
Assert.IsTrue(effectGraph.GetNode((IEffectNode)Effect) is PixelShaderEffect<ShaderWithNoInputs>);
Assert.IsTrue(effectGraph.GetNode((ICanvasEffectNode)Effect) is PixelShaderEffect<ShaderWithNoInputs>);
}
protected override void Dispose(bool disposing)
@@ -434,17 +434,17 @@ public partial class CanvasEffectTests
{
public bool WasConfigureEffectGraphCalled { get; private set; }
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
WasConfigureEffectGraphCalled = true;
effectGraph.SetOutputNode(new EffectNode<ColorSourceEffect>());
effectGraph.SetOutputNode(new CanvasEffectNode<ColorSourceEffect>());
}
}
@@ -452,14 +452,14 @@ public partial class CanvasEffectTests
{
public bool WasConfigureEffectGraphCalled { get; private set; }
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
WasConfigureEffectGraphCalled = true;
}
@@ -467,15 +467,15 @@ public partial class CanvasEffectTests
private sealed class EffectRegisteringNodesMultipleTimes1 : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
EffectNode<ColorSourceEffect> node = new();
CanvasEffectNode<ColorSourceEffect> node = new();
effectGraph.RegisterNode(node, new ColorSourceEffect());
effectGraph.RegisterOutputNode(node, new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
Assert.Fail();
}
@@ -483,16 +483,16 @@ public partial class CanvasEffectTests
private sealed class EffectRegisteringNodesMultipleTimes2 : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
EffectNode<ColorSourceEffect> node = new();
CanvasEffectNode<ColorSourceEffect> node = new();
effectGraph.RegisterNode(node, new ColorSourceEffect());
effectGraph.RegisterNode(node, new ColorSourceEffect());
effectGraph.RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
Assert.Fail();
}
@@ -500,63 +500,63 @@ public partial class CanvasEffectTests
private sealed class EffectRegisteringNullObjects : CanvasEffect
{
protected override unsafe void BuildEffectGraph(EffectGraph effectGraph)
protected override unsafe void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
// Verify that if the effect graph is invalid, arguments are validated first
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!, (ColorSourceEffect?)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterOutputNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterOutputNode(null!, (ColorSourceEffect)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterNode(null!, (ColorSourceEffect?)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(CanvasEffectGraph).RegisterOutputNode(null!, (ColorSourceEffect)null!));
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).RegisterOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterOutputNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterOutputNode(null!, (ColorSourceEffect)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterNode(null!, (ColorSourceEffect?)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).RegisterOutputNode(null!, (ColorSourceEffect)null!));
effectGraph.RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
}
}
private sealed class EffectConfiguringGraphIncorrectly : CanvasEffect
{
private static readonly EffectNode<ColorSourceEffect> EffectNode1 = new();
private static readonly EffectNode<ColorSourceEffect> EffectNode2 = new();
private static readonly EffectNode<ColorSourceEffect> EffectNode3 = new();
private static readonly CanvasEffectNode<ColorSourceEffect> EffectNode1 = new();
private static readonly CanvasEffectNode<ColorSourceEffect> EffectNode2 = new();
private static readonly CanvasEffectNode<ColorSourceEffect> EffectNode3 = new();
#pragma warning disable CA2213
private readonly ColorSourceEffect effect1 = new();
private readonly ColorSourceEffect effect2 = new();
private readonly ColorSourceEffect effect3 = new();
#pragma warning restore CA2213
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterNode(EffectNode1, this.effect1);
effectGraph.RegisterNode(EffectNode2, this.effect2);
effectGraph.RegisterOutputNode(EffectNode3, this.effect3);
}
protected override unsafe void ConfigureEffectGraph(EffectGraph effectGraph)
protected override unsafe void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
void* ptr = &effectGraph;
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).GetNode((EffectNode<ColorSourceEffect>)null!));
_ = Assert.ThrowsException<ArgumentException>(() => (*(EffectGraph*)ptr).GetNode(new EffectNode<ColorSourceEffect>()));
_ = Assert.ThrowsException<InvalidOperationException>(() => (*(EffectGraph*)ptr).RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => (*(EffectGraph*)ptr).RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(CanvasEffectGraph*)ptr).GetNode((CanvasEffectNode<ColorSourceEffect>)null!));
_ = Assert.ThrowsException<ArgumentException>(() => (*(CanvasEffectGraph*)ptr).GetNode(new CanvasEffectNode<ColorSourceEffect>()));
_ = Assert.ThrowsException<InvalidOperationException>(() => (*(CanvasEffectGraph*)ptr).RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => (*(CanvasEffectGraph*)ptr).RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
Assert.AreSame(effectGraph.GetNode(EffectNode1), this.effect1);
Assert.AreSame(effectGraph.GetNode(EffectNode2), this.effect2);
@@ -566,47 +566,47 @@ public partial class CanvasEffectTests
private sealed class EffectOnlyUsingAnonymousNodes : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterNode(new ColorSourceEffect());
effectGraph.RegisterOutputNode(new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
}
}
private sealed class EffectUsingEffectGraphInInvalidState : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterOutputNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(CanvasEffectGraph).RegisterNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(CanvasEffectGraph).RegisterNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(CanvasEffectGraph).RegisterOutputNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(CanvasEffectGraph).RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
effectGraph.RegisterOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
effectGraph.RegisterOutputNode(new CanvasEffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).GetNode(new EffectNode<ColorSourceEffect>()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(CanvasEffectGraph).GetNode(new CanvasEffectNode<ColorSourceEffect>()));
}
}
private sealed class PixelShaderSwitchEffect : CanvasEffect
{
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 static readonly CanvasEffectNode<PixelShaderEffect<HelloWorld>> HelloWorldNode = new();
private static readonly CanvasEffectNode<PixelShaderEffect<ColorfulInfinity>> ColorfulInfinityNode = new();
private static readonly CanvasEffectNode<PixelShaderEffect<FractalTiling>> FractalTilingNode = new();
private static readonly CanvasEffectNode<PixelShaderEffect<Octagrams>> OctagramsNode = new();
private static readonly CanvasEffectNode<PixelShaderEffect<ProteanClouds>> ProteanCloudsNode = new();
private int step;
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
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)) });
@@ -616,7 +616,7 @@ public partial class CanvasEffectTests
effectGraph.SetOutputNode(HelloWorldNode);
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
switch (this.step++)
{
@@ -640,24 +640,24 @@ public partial class CanvasEffectTests
private sealed class EffectTestingDisposal : CanvasEffect
{
private static readonly EffectNode<DummyCanvasImageTrackingDisposal> EffectNode1 = new();
private static readonly EffectNode<DummyCanvasImageTrackingDisposal> EffectNode2 = new();
private static readonly EffectNode<DummyCanvasImageTrackingDisposal> EffectNode3 = new();
private static readonly CanvasEffectNode<DummyCanvasImageTrackingDisposal> EffectNode1 = new();
private static readonly CanvasEffectNode<DummyCanvasImageTrackingDisposal> EffectNode2 = new();
private static readonly CanvasEffectNode<DummyCanvasImageTrackingDisposal> EffectNode3 = new();
public readonly List<DummyCanvasImageTrackingDisposal> Effects1 = [];
public readonly List<DummyCanvasImageTrackingDisposal> Effects2 = [];
public readonly List<DummyCanvasImageTrackingDisposal> Effects3 = [];
public void InvalidateCreation()
{
InvalidateEffectGraph(InvalidationType.Creation);
InvalidateEffectGraph(CanvasEffectInvalidationType.Creation);
}
public void InvalidateUpdate()
{
InvalidateEffectGraph(InvalidationType.Update);
InvalidateEffectGraph(CanvasEffectInvalidationType.Update);
}
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
this.Effects1.Add(new DummyCanvasImageTrackingDisposal());
this.Effects2.Add(new DummyCanvasImageTrackingDisposal());
@@ -668,7 +668,7 @@ public partial class CanvasEffectTests
effectGraph.RegisterOutputNode(EffectNode3, this.Effects3[^1]);
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
}
}
+3 -3
ファイルの表示
@@ -113,7 +113,7 @@ public class ShadersTests
private sealed class TestCanvasEffect<T> : CanvasEffect
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
private static readonly EffectNode<PixelShaderEffect<T>> Effect = new();
private static readonly CanvasEffectNode<PixelShaderEffect<T>> Effect = new();
private T constantBuffer;
@@ -123,12 +123,12 @@ public class ShadersTests
set => SetAndInvalidateEffectGraph(ref this.constantBuffer, value);
}
protected override void BuildEffectGraph(EffectGraph effectGraph)
protected override void BuildEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect<T>());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph)
{
effectGraph.GetNode(Effect).ConstantBuffer = this.constantBuffer;
}