Add CanvasEffect.EffectGraph APIs
このコミットが含まれているのは:
1 親
d58430e73f
コミット
f23762f6af
6ファイル変更
+184
-15
検索条件に一致するファイルはありません
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
#else
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
|
||||
/// <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>
|
||||
/// <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>
|
||||
/// <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>
|
||||
public T GetNode<T>(EffectNode<T> effectNode)
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
|
||||
// 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 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, 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 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.isBuildingEffectGraph);
|
||||
|
||||
// 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, 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 RegisterAndSetOutputNode<T>(EffectNode<T> effectNode, T canvasImage)
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(ArgumentNullException).ThrowIfNull(canvasImage);
|
||||
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
|
||||
default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null);
|
||||
|
||||
// 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>
|
||||
/// 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>
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -73,11 +73,23 @@ unsafe partial class CanvasEffect
|
||||
{
|
||||
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
|
||||
|
||||
this.canvasImage ??= BuildEffectGraph();
|
||||
// Build the effect graph and get the output node, if missing
|
||||
if (this.canvasImage is null)
|
||||
{
|
||||
this.isBuildingEffectGraph = true;
|
||||
|
||||
DisposeEffectGraph();
|
||||
BuildEffectGraph(new EffectGraph(this));
|
||||
|
||||
this.isBuildingEffectGraph = false;
|
||||
|
||||
default(InvalidOperationException).ThrowIf(this.canvasImage is null);
|
||||
}
|
||||
|
||||
// Configure the effect graph, if the effect is invalidated
|
||||
if (this.isInvalidated)
|
||||
{
|
||||
ConfigureEffectGraph();
|
||||
ConfigureEffectGraph(new EffectGraph(this));
|
||||
|
||||
this.isInvalidated = false;
|
||||
}
|
||||
@@ -85,4 +97,22 @@ unsafe partial class CanvasEffect
|
||||
return this.canvasImage;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes and clears the current effect graph, if any.
|
||||
/// </summary>
|
||||
private void DisposeEffectGraph()
|
||||
{
|
||||
// Dispose all registered canvas images
|
||||
foreach (ICanvasImage canvasImage in this.transformNodes.Values)
|
||||
{
|
||||
canvasImage.Dispose();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,28 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
|
||||
/// </summary>
|
||||
private readonly object lockObject = new();
|
||||
|
||||
/// <summary>
|
||||
/// 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"/>.
|
||||
/// </remarks>
|
||||
private readonly Dictionary<object, ICanvasImage> transformNodes = new();
|
||||
|
||||
/// <summary>
|
||||
/// The current cached result for <see cref="GetCanvasImage"/>, if available.
|
||||
/// </summary>
|
||||
private ICanvasImage? canvasImage;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the current instance is within a call to <see cref="BuildEffectGraph"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is used to validate method calls from within <see cref="EffectGraph"/>. That is, the
|
||||
/// <see cref="EffectGraph.RegisterNode"/> APIs will use this to detect whether they can be called.
|
||||
/// </remarks>
|
||||
private bool isBuildingEffectGraph;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the current state has been invalidated (requiring <see cref="ConfigureEffectGraph"/> to be called).
|
||||
/// </summary>
|
||||
@@ -40,10 +57,10 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
|
||||
private bool isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the effect graph for the current <see cref="CanvasEffect"/> instance, and returns an <see cref="ICanvasImage"/> object
|
||||
/// representing the output node of the graph. This image will then be passed to Win2D to perform the actual drawing, when needed.
|
||||
/// 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>
|
||||
/// <returns>The resulting <see cref="ICanvasImage"/> object representing the output node of the effect graph.</returns>
|
||||
/// <param name="effectGraph">The input <see cref="EffectGraph"/> 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.
|
||||
@@ -74,29 +91,33 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
|
||||
/// <list type="bullet">
|
||||
/// <item>Create an instance of all necessary nodes in the effect graph.</item>
|
||||
/// <item>Connect the effect nodes as needed to build the connected graph.</item>
|
||||
/// <item>Save all effects that can be configured as instance fields in the effect type.</item>
|
||||
/// <item>Return the <see cref="ICanvasImage"/> instance for the output node of the graph.</item>
|
||||
/// <item>Register all effects as effect nodes in the graph, including the output node.</item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. It is automatically invoked when an <see cref="ICanvasImage"/> is needed.
|
||||
/// For convenience, it is recommended to store all necessary <see cref="EffectNode{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 ICanvasImage BuildEffectGraph();
|
||||
protected abstract void BuildEffectGraph(EffectGraph 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>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method is guaranteed to be called after <see cref="BuildEffectGraph"/> has been invoked already. As such, any instance
|
||||
/// fields that are set by <see cref="BuildEffectGraph"/> can be assumed to never be <see langword="null"/> when this method runs.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. It is used internally to configure the current <see cref="ICanvasImage"/> instance.
|
||||
/// This method should never be called directly. It is used internally to configure the current effect graph.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected abstract void ConfigureEffectGraph();
|
||||
protected abstract void ConfigureEffectGraph(EffectGraph effectGraph);
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates the last returned result from <see cref="GetCanvasImage"/>.
|
||||
@@ -193,8 +214,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
|
||||
// on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers.
|
||||
lock (this.lockObject)
|
||||
{
|
||||
this.canvasImage?.Dispose();
|
||||
this.canvasImage = null;
|
||||
DisposeEffectGraph();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Buffers\GraphicsEffectSourceBuffer.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Buffers\ResourceTextureManagerBuffer.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Buffers\SourceReference.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)CanvasEffect.EffectGraph.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)CanvasEffect.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)CanvasEffect.Interop.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Collections\FixedCountList{T}.cs" />
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ partial class PixelShaderEffect<T>
|
||||
internal static ImmutableArray<int> Indices { get; } = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// Gets the owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ partial class PixelShaderEffect<T>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// Gets the owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする