Add ID2D1TransformMapperFactory<T> interface

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-06 00:37:31 +02:00
1 親 77c7a767ba
コミット 2c9dad8c98
9ファイル変更
+88 -25

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

+15
ファイルの表示
@@ -0,0 +1,15 @@
namespace ComputeSharp.D2D1;
/// <summary>
/// An <see langword="interface"/> acting as a factory for <see cref="ID2D1TransformMapper{T}"/> instances.
/// </summary>
/// <typeparam name="T">The type of shader this factory creates transform mapper instances for.</typeparam>
public interface ID2D1TransformMapperFactory<T>
where T : unmanaged, ID2D1PixelShader
{
/// <summary>
/// Creates a new <see cref="ID2D1TransformMapper{T}"/> instance for a given shader.
/// </summary>
/// <returns>A new <see cref="ID2D1TransformMapper{T}"/> instance for the input shader.</returns>
ID2D1TransformMapper<T> Create();
}
+6 -6
ファイルの表示
@@ -59,7 +59,7 @@ public static unsafe class D2D1PixelShaderEffect
ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null.");
}
RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId);
RegisterForD2D1Factory1(d2D1Factory1, D2D1TransformMapper.FactoryOf<T, TMapper>.Get(), out effectId);
}
/// <summary>
@@ -72,7 +72,7 @@ public static unsafe class D2D1PixelShaderEffect
/// <exception cref="ArgumentNullException">Thrown if <paramref name="d2D1Factory1"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
public static void RegisterForD2D1Factory1<T>(void* d2D1Factory1, Func<ID2D1TransformMapper<T>>? mapperFactory, out Guid effectId)
public static void RegisterForD2D1Factory1<T>(void* d2D1Factory1, ID2D1TransformMapperFactory<T>? mapperFactory, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
if (d2D1Factory1 is null)
@@ -258,7 +258,7 @@ public static unsafe class D2D1PixelShaderEffect
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(Func{ID2D1TransformMapper{T}}?, out Guid)"/>.</remarks>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(ID2D1TransformMapperFactory{T}?, out Guid)"/>.</remarks>
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
@@ -273,12 +273,12 @@ public static unsafe class D2D1PixelShaderEffect
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(Func{ID2D1TransformMapper{T}}?, out Guid)"/>.</remarks>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(ID2D1TransformMapperFactory{T}?, out Guid)"/>.</remarks>
public static ReadOnlyMemory<byte> GetRegistrationBlob<T, TMapper>(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper<T>, new()
{
return GetRegistrationBlob(static () => new TMapper(), out effectId);
return GetRegistrationBlob(D2D1TransformMapper.FactoryOf<T, TMapper>.Get(), out effectId);
}
/// <summary>
@@ -321,7 +321,7 @@ public static unsafe class D2D1PixelShaderEffect
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.
/// </para>
/// </remarks>
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(Func<ID2D1TransformMapper<T>>? mapperFactory, out Guid effectId)
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(ID2D1TransformMapperFactory<T>? mapperFactory, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
effectId = default;
+32
ファイルの表示
@@ -93,4 +93,36 @@ internal abstract class D2D1TransformMapper
this.transformMapper.MapInvalidOutput(inputIndex, invalidInput, out invalidOutput);
}
}
/// <summary>
/// A generic <see cref="ID2D1TransformMapperFactory{T}"/> implementation for a mapper with a parameterless constructor.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
/// <typeparam name="TMapper">The type of <see cref="ID2D1TransformMapper{T}"/> implementation to register.</typeparam>
public sealed class FactoryOf<T, TMapper> : ID2D1TransformMapperFactory<T>
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper<T>, new()
{
/// <summary>
/// Creates a new <see cref="FactoryOf{T, TMapper}"/> instance.
/// </summary>
private FactoryOf()
{
}
/// <summary>
/// Creates a new <see cref="FactoryOf{T, TMapper}"/> instance for a given transform mapper.
/// </summary>
/// <returns>A new <see cref="FactoryOf{T, TMapper}"/> instance.</returns>
public static FactoryOf<T, TMapper> Get()
{
return new();
}
/// <inheritdoc/>
public ID2D1TransformMapper<T> Create()
{
return new TMapper();
}
}
}
+3 -3
ファイルの表示
@@ -107,14 +107,14 @@ internal unsafe partial struct PixelShaderEffect
/// <summary>
/// The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.
/// </summary>
private static Func<ID2D1TransformMapper<T>>? d2D1DrawTransformMapperFactory;
private static ID2D1TransformMapperFactory<T>? d2D1DrawTransformMapperFactory;
/// <summary>
/// Initializes the <see cref="For{T}"/> shared state.
/// </summary>
/// <param name="d2D1DrawTransformMapperFactory">The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.</param>
/// <exception cref="InvalidOperationException">Thrown if initialization is attempted with a mismatched transform factory.</exception>
public static void Initialize(Func<ID2D1TransformMapper<T>>? d2D1DrawTransformMapperFactory)
public static void Initialize(ID2D1TransformMapperFactory<T>? d2D1DrawTransformMapperFactory)
{
// This conceptually acts as a static constructor, and this type is
// internal, so in this very specific case locking on the type is fine.
@@ -253,7 +253,7 @@ internal unsafe partial struct PixelShaderEffect
// and managed exceptions should never cross the ABI boundary. If it throws, just return the HRESULT.
try
{
ID2D1TransformMapper<T>? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Invoke();
ID2D1TransformMapper<T>? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Create();
d2D1TransformMapper = D2D1TransformMapper.For<T>.Get(d2D1DrawTransformMapper);
}
+1 -1
ファイルの表示
@@ -31,7 +31,7 @@ public partial class D2D1PixelShaderEffectTests
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public unsafe void RegisterForD2D1Factory1_WithTransformMapperFactory_NullD2D1Factory1()
{
D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, () => new PixelateEffect(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1<PixelateEffect.Shader, PixelateEffect>(null, out _);
}
[TestMethod]
+16 -4
ファイルの表示
@@ -303,8 +303,8 @@ public sealed partial class BokehBlurEffect
d2D1DeviceContext.Get()->SetRenderingControls(&d2D1RenderingControls);
// Register all necessary effects
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), static () => new VerticalConvolution(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), static () => new HorizontalConvolutionAndAccumulatePartials(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), new VerticalConvolution(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), new HorizontalConvolutionAndAccumulatePartials(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1<GammaHighlight>(d2D1Factory2.Get(), out _);
D2D1PixelShaderEffect.RegisterForD2D1Factory1<InverseGammaHighlight>(d2D1Factory2.Get(), out _);
@@ -477,13 +477,19 @@ public sealed partial class BokehBlurEffect
/// <summary>
/// The vertical convolution shader and transform.
/// </summary>
private sealed partial class VerticalConvolution : ID2D1TransformMapper<VerticalConvolution.Shader>
private sealed partial class VerticalConvolution : ID2D1TransformMapperFactory<VerticalConvolution.Shader>, ID2D1TransformMapper<VerticalConvolution.Shader>
{
/// <summary>
/// The length of the convolution kernel.
/// </summary>
private int kernelLength;
/// <inheritdoc/>
public ID2D1TransformMapper<Shader> Create()
{
return new VerticalConvolution();
}
/// <inheritdoc/>
public void MapInputsToOutput(in Shader shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
{
@@ -547,13 +553,19 @@ public sealed partial class BokehBlurEffect
/// <summary>
/// The horizontal convolutin and partial accumulation effect and transform.
/// </summary>
private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapper<HorizontalConvolutionAndAccumulatePartials.Shader>
private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapperFactory<HorizontalConvolutionAndAccumulatePartials.Shader>, ID2D1TransformMapper<HorizontalConvolutionAndAccumulatePartials.Shader>
{
/// <summary>
/// The length of the convolution kernel.
/// </summary>
private int kernelLength;
/// <inheritdoc/>
public ID2D1TransformMapper<Shader> Create()
{
return new HorizontalConvolutionAndAccumulatePartials();
}
/// <inheritdoc/>
public void MapInputsToOutput(in Shader shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
{
+7 -1
ファイルの表示
@@ -3,8 +3,14 @@ using System.Drawing;
namespace ComputeSharp.D2D1.Tests.Effects;
public sealed partial class PixelateEffect : ID2D1TransformMapper<PixelateEffect.Shader>
public sealed partial class PixelateEffect : ID2D1TransformMapperFactory<PixelateEffect.Shader>, ID2D1TransformMapper<PixelateEffect.Shader>
{
/// <inheritdoc/>
public ID2D1TransformMapper<Shader> Create()
{
return new PixelateEffect();
}
/// <inheritdoc/>
void ID2D1TransformMapper<Shader>.MapInputsToOutput(in Shader shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
{
+2 -2
ファイルの表示
@@ -38,7 +38,7 @@ public class EndToEndTests
{
D2D1TestRunner.RunAndCompareShader(
new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 840, 16)),
static () => new PixelateEffect(),
new PixelateEffect(),
"Landscape.png",
"Landscape_Pixelate.png");
}
@@ -46,7 +46,7 @@ public class EndToEndTests
[TestMethod]
public unsafe void ZonePlate()
{
D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png");
D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), 1280, 720, "ZonePlate.png");
}
[TestMethod]
+6 -8
ファイルの表示
@@ -25,7 +25,7 @@ internal static class D2D1TestRunner
/// <param name="shader">The shader to run.</param>
public static void RunAndCompareShader<T>(
in T shader,
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
ID2D1TransformMapperFactory<T>? transformMapperFactory,
string originalFileName,
string expectedFileName,
[CallerMemberName] string destinationFileName = "")
@@ -55,7 +55,6 @@ internal static class D2D1TestRunner
/// Executes a pixel shader and compares the expected results.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="width">The resulting width.</param>
/// <param name="height">The resulting height.</param>
/// <param name="expectedFileName">The name of the expected result image.</param>
@@ -63,7 +62,6 @@ internal static class D2D1TestRunner
/// <param name="shader">The shader to run.</param>
public static void RunAndCompareShader<T>(
in T shader,
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
int width,
int height,
string expectedFileName,
@@ -81,7 +79,7 @@ internal static class D2D1TestRunner
// Run the shader
ExecutePixelShaderAndSaveToFile(
in shader,
transformMapperFactory,
null,
width,
height,
destinationPath);
@@ -98,9 +96,9 @@ internal static class D2D1TestRunner
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="sourcePath">The source path for the image to run the shader on.</param>
/// <param name="destinationPath">The destination path for the result.</param>
public static unsafe void ExecutePixelShaderAndSaveToFile<T>(
private static unsafe void ExecutePixelShaderAndSaveToFile<T>(
in T shader,
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
ID2D1TransformMapperFactory<T>? transformMapperFactory,
string sourcePath,
string destinationPath)
where T : unmanaged, ID2D1PixelShader
@@ -139,9 +137,9 @@ internal static class D2D1TestRunner
/// <param name="height">The resulting height.</param>
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="destinationPath">The destination path for the result.</param>
public static unsafe void ExecutePixelShaderAndSaveToFile<T>(
private static unsafe void ExecutePixelShaderAndSaveToFile<T>(
in T shader,
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
ID2D1TransformMapperFactory<T>? transformMapperFactory,
int width,
int height,
string destinationPath)