diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index 21186ea6..2e8f1928 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.Drawing; +using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Tests.Helpers; @@ -14,13 +16,17 @@ public class EndToEndTests [TestMethod] public unsafe void InvertWithCustomThreshold() { - RunAndCompareShader(new InvertShader(1), "Landscape.png", "Landscape_Inverted.png"); + RunAndCompareShader(new InvertShader(1), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] public unsafe void Pixelate() { - RunAndCompareShader(new PixelateShader(new PixelateShader.Constants(1280, 720, 16)), "Landscape.png", "Landscape_Pixelate.png"); + RunAndCompareShader( + new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 720, 16)), + static () => new PixelateEffect(), + "Landscape.png", + "Landscape_Pixelate.png"); } /// @@ -28,11 +34,13 @@ public class EndToEndTests /// /// The type of pixel shader to run. /// The name of the source image. + /// A custom factory for the effect. /// The name of the expected result image. /// The name of the destination image to save results to. /// The shader to run. private static void RunAndCompareShader( in T shader, + Func>? transformMapperFactory, string originalFileName, string expectedFileName, [CallerMemberName] string destinationFileName = "") @@ -49,9 +57,10 @@ public class EndToEndTests // Run the shader D2D1ShaderTestHelper.ExecutePixelShaderAndCompareResults( + in shader, + transformMapperFactory, originalPath, - destinationPath, - in shader); + destinationPath); // Compare the results TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); @@ -75,45 +84,67 @@ public partial struct InvertShader : ID2D1PixelShader } } -[D2DInputCount(1)] -[D2DInputComplex(0)] -[D2DRequiresScenePosition] -[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40)] -[AutoConstructor] -public partial struct PixelateShader : ID2D1PixelShader +public sealed partial class PixelateEffect : ID2D1TransformMapper { - [AutoConstructor] - public partial struct Constants + /// + void ID2D1TransformMapper.MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { - public readonly int inputWidth; - public readonly int inputHeight; - public readonly int cellSize; + output = inputs[0]; + opaqueOutput = Rectangle.Empty; } - private readonly Constants constants; - - public float4 Execute() + /// + void ID2D1TransformMapper.MapInvalidOutput(in Shader shader, int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) { - float2 scenePos = D2D.GetScenePosition().XY; - uint x = (uint)Hlsl.Floor(scenePos.X); - uint y = (uint)Hlsl.Floor(scenePos.Y); + invalidOutput = invalidInput; + } - int cellX = (int)Hlsl.Floor(x / this.constants.cellSize); - int cellY = (int)Hlsl.Floor(y / this.constants.cellSize); + /// + void ID2D1TransformMapper.MapOutputToInputs(in Shader shader, in Rectangle output, Span inputs) + { + inputs.Fill(output); + } - int x0 = cellX * this.constants.cellSize; - int y0 = cellY * this.constants.cellSize; + [D2DInputCount(1)] + [D2DInputComplex(0)] + [D2DRequiresScenePosition] + [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40)] + [AutoConstructor] + public partial struct Shader : ID2D1PixelShader + { + [AutoConstructor] + public partial struct Constants + { + public readonly int inputWidth; + public readonly int inputHeight; + public readonly int cellSize; + } - int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1; - int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1; + private readonly Constants constants; - float4 sample0 = D2D.SampleInputAtPosition(0, new int2(x0, y0)); - float4 sample1 = D2D.SampleInputAtPosition(0, new int2(x1, y0)); - float4 sample2 = D2D.SampleInputAtPosition(0, new int2(x0, y1)); - float4 sample3 = D2D.SampleInputAtPosition(0, new int2(x1, y1)); + public float4 Execute() + { + float2 scenePos = D2D.GetScenePosition().XY; + uint x = (uint)Hlsl.Floor(scenePos.X); + uint y = (uint)Hlsl.Floor(scenePos.Y); - float4 color = (sample0 + sample1 + sample2 + sample3) / 4; + int cellX = (int)Hlsl.Floor(x / this.constants.cellSize); + int cellY = (int)Hlsl.Floor(y / this.constants.cellSize); - return color; + int x0 = cellX * this.constants.cellSize; + int y0 = cellY * this.constants.cellSize; + + int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1; + int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1; + + float4 sample0 = D2D.SampleInputAtPosition(0, new int2(x0, y0)); + float4 sample1 = D2D.SampleInputAtPosition(0, new int2(x1, y0)); + float4 sample2 = D2D.SampleInputAtPosition(0, new int2(x0, y1)); + float4 sample3 = D2D.SampleInputAtPosition(0, new int2(x1, y1)); + + float4 color = (sample0 + sample1 + sample2 + sample3) / 4; + + return color; + } } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs index c04d9fea..cfa766ce 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1ShaderTestHelper.cs @@ -1,4 +1,5 @@ -using ComputeSharp.D2D1.Interop; +using System; +using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -14,13 +15,15 @@ internal static class D2D1ShaderTestHelper /// Executes a pixel shader to produce an image. /// /// The shader type to execute. + /// The shader to run. + /// A custom factory for the effect. /// The source path for the image to run the shader on. /// The destination path for the result. - /// The shader to run. public static unsafe void ExecutePixelShaderAndCompareResults( + in T shader, + Func>? transformMapperFactory, string sourcePath, - string destinationPath, - in T shader) + string destinationPath) where T : unmanaged, ID2D1PixelShader { using ComPtr d2D1Factory2 = default; @@ -113,7 +116,7 @@ internal static class D2D1ShaderTestHelper wicBitmapSource: (IWICBitmapSource*)wicBitmap.Get(), bitmap: d2D1BitmapSource.GetAddressOf()).Assert(); - D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), out _); + D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); using ComPtr d2D1Effect = default;