From 2c9dad8c984b049ff4fe47d328896150c1c11576 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 3 Sep 2022 23:07:50 +0200 Subject: [PATCH] Add ID2D1TransformMapperFactory interface --- .../ID2D1TransformMapperFactory{T}.cs | 15 +++++++++ .../Shaders/Interop/D2D1PixelShaderEffect.cs | 12 +++---- .../Interop/Effects/D2D1TransformMapper.cs | 32 +++++++++++++++++++ .../Effects/PixelShaderEffect.For{T}.cs | 6 ++-- .../D2D1PixelShaderEffectTests.cs | 2 +- .../Effects/BokehBlurEffect.cs | 20 +++++++++--- .../Effects/PixelateEffect.cs | 8 ++++- .../ComputeSharp.D2D1.Tests/EndToEndTests.cs | 4 +-- .../Helpers/D2D1TestRunner.cs | 14 ++++---- 9 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs diff --git a/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs new file mode 100644 index 00000000..16b10fbf --- /dev/null +++ b/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs @@ -0,0 +1,15 @@ +namespace ComputeSharp.D2D1; + +/// +/// An acting as a factory for instances. +/// +/// The type of shader this factory creates transform mapper instances for. +public interface ID2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader +{ + /// + /// Creates a new instance for a given shader. + /// + /// A new instance for the input shader. + ID2D1TransformMapper Create(); +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index cd73c442..10f8f3bf 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -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.Get(), out effectId); } /// @@ -72,7 +72,7 @@ public static unsafe class D2D1PixelShaderEffect /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . - public static void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) + public static void RegisterForD2D1Factory1(void* d2D1Factory1, ID2D1TransformMapperFactory? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { if (d2D1Factory1 is null) @@ -258,7 +258,7 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. - /// For more info and for details on the binary format, see . + /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { @@ -273,12 +273,12 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. - /// For more info and for details on the binary format, see . + /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { - return GetRegistrationBlob(static () => new TMapper(), out effectId); + return GetRegistrationBlob(D2D1TransformMapper.FactoryOf.Get(), out effectId); } /// @@ -321,7 +321,7 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . /// /// - public static ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) + public static ReadOnlyMemory GetRegistrationBlob(ID2D1TransformMapperFactory? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs index 787411ef..0f70e521 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs @@ -93,4 +93,36 @@ internal abstract class D2D1TransformMapper this.transformMapper.MapInvalidOutput(inputIndex, invalidInput, out invalidOutput); } } + + /// + /// A generic implementation for a mapper with a parameterless constructor. + /// + /// The type of D2D1 pixel shader to register. + /// The type of implementation to register. + public sealed class FactoryOf : ID2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader + where TMapper : class, ID2D1TransformMapper, new() + { + /// + /// Creates a new instance. + /// + private FactoryOf() + { + } + + /// + /// Creates a new instance for a given transform mapper. + /// + /// A new instance. + public static FactoryOf Get() + { + return new(); + } + + /// + public ID2D1TransformMapper Create() + { + return new TMapper(); + } + } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index 7a694f5d..d8639943 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -107,14 +107,14 @@ internal unsafe partial struct PixelShaderEffect /// /// The factory of instances to use for each created effect. /// - private static Func>? d2D1DrawTransformMapperFactory; + private static ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory; /// /// Initializes the shared state. /// /// The factory of instances to use for each created effect. /// Thrown if initialization is attempted with a mismatched transform factory. - public static void Initialize(Func>? d2D1DrawTransformMapperFactory) + public static void Initialize(ID2D1TransformMapperFactory? 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? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Invoke(); + ID2D1TransformMapper? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Create(); d2D1TransformMapper = D2D1TransformMapper.For.Get(d2D1DrawTransformMapper); } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 9238932b..bc82b5b6 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -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(null, out _); } [TestMethod] diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index bcecb144..adbb3816 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -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(d2D1Factory2.Get(), out _); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); @@ -477,13 +477,19 @@ public sealed partial class BokehBlurEffect /// /// The vertical convolution shader and transform. /// - private sealed partial class VerticalConvolution : ID2D1TransformMapper + private sealed partial class VerticalConvolution : ID2D1TransformMapperFactory, ID2D1TransformMapper { /// /// The length of the convolution kernel. /// private int kernelLength; + /// + public ID2D1TransformMapper Create() + { + return new VerticalConvolution(); + } + /// public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { @@ -547,13 +553,19 @@ public sealed partial class BokehBlurEffect /// /// The horizontal convolutin and partial accumulation effect and transform. /// - private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapper + private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapperFactory, ID2D1TransformMapper { /// /// The length of the convolution kernel. /// private int kernelLength; + /// + public ID2D1TransformMapper Create() + { + return new HorizontalConvolutionAndAccumulatePartials(); + } + /// public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs index 84aa3544..02f60a88 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs @@ -3,8 +3,14 @@ using System.Drawing; namespace ComputeSharp.D2D1.Tests.Effects; -public sealed partial class PixelateEffect : ID2D1TransformMapper +public sealed partial class PixelateEffect : ID2D1TransformMapperFactory, ID2D1TransformMapper { + /// + public ID2D1TransformMapper Create() + { + return new PixelateEffect(); + } + /// void ID2D1TransformMapper.MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index b6bcd911..4696b592 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -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] diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index dfe1d5e9..f18b683a 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -25,7 +25,7 @@ internal static class D2D1TestRunner /// The shader to run. public static void RunAndCompareShader( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? 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. /// /// The type of pixel shader to run. - /// A custom factory for the effect. /// The resulting width. /// The resulting height. /// The name of the expected result image. @@ -63,7 +62,6 @@ internal static class D2D1TestRunner /// The shader to run. public static void RunAndCompareShader( in T shader, - Func>? 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 /// A custom factory for the effect. /// The source path for the image to run the shader on. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndSaveToFile( + private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? transformMapperFactory, string sourcePath, string destinationPath) where T : unmanaged, ID2D1PixelShader @@ -139,9 +137,9 @@ internal static class D2D1TestRunner /// The resulting height. /// A custom factory for the effect. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndSaveToFile( + private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? transformMapperFactory, int width, int height, string destinationPath)