From 8fa29edf19775294cc638e1d9fbb10bfac10f409 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 18 Nov 2022 17:52:00 +0100 Subject: [PATCH] Update unit tests for new API shapes --- .../D2D1PixelShaderEffectTests.cs | 38 ++----------------- .../D2D1ResourceTextureManagerTests.cs | 22 +++++------ .../Effects/BokehBlurEffect.cs | 16 +++++--- .../Helpers/D2D1TestRunner.cs | 28 +++++++++----- 4 files changed, 43 insertions(+), 61 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 49e06915..ee06baf1 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -1,6 +1,5 @@ using System; using System.ComponentModel; -using System.Drawing; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; @@ -23,42 +22,11 @@ public partial class D2D1PixelShaderEffectTests D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); } - [TestMethod] - [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] - public unsafe void RegisterForD2D1Factory1_WithTransformMapper_NullD2D1Factory1() - { - D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); - } - - public sealed partial class DummyTransformMapper : ID2D1TransformMapper - { - public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) - { - output = default; - opaqueOutput = default; - } - - public void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) - { - invalidOutput = default; - } - - public void MapOutputToInputs(in Rectangle output, Span inputs) - { - } - - [D2DInputCount(0)] - public partial struct Shader : ID2D1PixelShader - { - public float4 Execute() => default; - } - } - [TestMethod] [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] public unsafe void RegisterForD2D1Factory1_WithTransformMapperFactory_NullD2D1Factory1() { - D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); } [TestMethod] @@ -125,7 +93,7 @@ public partial class D2D1PixelShaderEffectTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -156,7 +124,7 @@ public partial class D2D1PixelShaderEffectTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 9f304859..61dd5d58 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -108,7 +108,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr resourceTextureManager = default; @@ -152,7 +152,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); D2D1ResourceTextureManager resourceTextureManager = new( extents: stackalloc uint[] { 128, 128 }, @@ -192,7 +192,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -251,7 +251,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -315,7 +315,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -373,7 +373,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; string wallpaper0Path = Path.Combine(assemblyPath, "Assets", "Wallpaper0.png"); @@ -480,7 +480,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -567,7 +567,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -655,7 +655,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -758,7 +758,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -854,7 +854,7 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index c68de934..c3b1d893 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -317,8 +317,8 @@ public sealed partial class BokehBlurEffect d2D1DeviceContext.Get()->SetRenderingControls(&d2D1RenderingControls); // Register all necessary effects - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), VerticalConvolution.Transform, out _); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), HorizontalConvolutionAndAccumulatePartials.Transform, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); @@ -344,6 +344,10 @@ public sealed partial class BokehBlurEffect D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)verticalConvolutionEffectsForReals[i].GetAddressOf()); D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)verticalConvolutionEffectsForImaginaries[i].GetAddressOf()); D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)horizontalConvolutionEffects[i].GetAddressOf()); + + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(verticalConvolutionEffectsForReals[i].Get(), VerticalConvolution.Transform); + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(verticalConvolutionEffectsForImaginaries[i].Get(), VerticalConvolution.Transform); + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(horizontalConvolutionEffects[i].Get(), HorizontalConvolutionAndAccumulatePartials.Transform); } // Create the border effect to clamp the input image @@ -494,9 +498,9 @@ public sealed partial class BokehBlurEffect private sealed partial class VerticalConvolution { /// - /// The for the shader. + /// The for the shader. /// - public static ID2D1TransformMapperFactory Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (0, shader.kernelLength, 0, shader.kernelLength)); + public static D2D1TransformMapper Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (0, shader.kernelLength, 0, shader.kernelLength)); /// /// Kernel for the vertical convolution pass for real or imaginary components. @@ -541,9 +545,9 @@ public sealed partial class BokehBlurEffect private sealed partial class HorizontalConvolutionAndAccumulatePartials { /// - /// The for the shader. + /// The for the shader. /// - public static ID2D1TransformMapperFactory Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (shader.kernelLength, 0, shader.kernelLength, 0)); + public static D2D1TransformMapper Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (shader.kernelLength, 0, shader.kernelLength, 0)); /// /// Kernel for the horizontal convolution pass. diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index 35ce6288..df176f89 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -22,7 +22,7 @@ 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. + /// A custom instance for the effect. /// The name of the source image. /// The name of the expected result image. /// The name of the destination image to save results to. @@ -30,7 +30,7 @@ internal static class D2D1TestRunner /// The allowed difference threshold for the normalized delta. public static void RunAndCompareShader( in T shader, - ID2D1TransformMapperFactory? transformMapperFactory, + D2D1TransformMapper? transformMapper, string originalFileName, string expectedFileName, [CallerMemberName] string destinationFileName = "", @@ -49,7 +49,7 @@ internal static class D2D1TestRunner // Run the shader ExecutePixelShaderAndSaveToFile( in shader, - transformMapperFactory, + transformMapper, originalPath, destinationPath); @@ -104,12 +104,12 @@ internal static class D2D1TestRunner /// /// The shader type to execute. /// The shader to run. - /// A custom factory for the effect. + /// A custom instance for the effect. /// The source path for the image to run the shader on. /// The destination path for the result. private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - ID2D1TransformMapperFactory? transformMapperFactory, + D2D1TransformMapper? transformMapper, string sourcePath, string destinationPath) where T : unmanaged, ID2D1PixelShader @@ -118,7 +118,7 @@ internal static class D2D1TestRunner using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -126,6 +126,11 @@ internal static class D2D1TestRunner D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + if (transformMapper is not null) + { + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(d2D1Effect.Get(), transformMapper); + } + ReadOnlyMemory pixels = ImageHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height); using ComPtr d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), pixels, width, height, d2D1Effect.Get()); @@ -146,12 +151,12 @@ internal static class D2D1TestRunner /// The shader to run. /// The resulting width. /// The resulting height. - /// A custom factory for the effect. + /// A custom instance for the effect. /// The destination path for the result. /// The additional resource textures to use to run the shader. private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - ID2D1TransformMapperFactory? transformMapperFactory, + D2D1TransformMapper? transformMapper, int width, int height, string destinationPath, @@ -162,7 +167,7 @@ internal static class D2D1TestRunner using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; @@ -170,6 +175,11 @@ internal static class D2D1TestRunner D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + if (transformMapper is not null) + { + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(d2D1Effect.Get(), transformMapper); + } + foreach ((int index, D2D1ResourceTextureManager resourceTextureManager) in resourceTextures) { D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, index);