diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/ZonePlate.png b/tests/ComputeSharp.D2D1.Tests/Assets/ZonePlate.png new file mode 100644 index 00000000..3971ea5f Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/ZonePlate.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index d2d22ced..5816c386 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -35,6 +35,9 @@ PreserveNewest + + PreserveNewest + diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs new file mode 100644 index 00000000..bff94d85 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs @@ -0,0 +1,34 @@ +using System; + +namespace ComputeSharp.D2D1.Tests.Effects; + +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +public readonly partial struct ZonePlateEffect : ID2D1PixelShader +{ + private readonly int width; + private readonly int height; + private readonly float diameter; + + /// + public float4 Execute() + { + float2 scenePos = D2D.GetScenePosition().XY; + + float xo = scenePos.X - (this.width >> 1); + float yo = scenePos.Y - (this.height >> 1); + + float rm = 0.5f * this.diameter; + float km = 0.7f / this.diameter * MathF.PI; + float w = rm / 10.0f; + + float yd = yo * yo; + float xd = xo * xo; + float d = xd + yd; + float v = 1.0f + (1.0f + Hlsl.Tanh((rm - Hlsl.Sqrt(d)) / w)) * Hlsl.Sin(km * d) * 0.5f; + + return new Float4(v, v, v, 1.0f); + } +} diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index 24c50f5c..851d4e07 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -29,12 +29,18 @@ public class EndToEndTests "Landscape_Pixelate.png"); } + [TestMethod] + public unsafe void ZonePlate() + { + RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png"); + } + /// /// Executes a pixel shader and compares the expected results. /// /// The type of pixel shader to run. - /// The name of the source image. /// A custom factory 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. /// The shader to run. @@ -65,4 +71,43 @@ public class EndToEndTests // Compare the results TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); } + + /// + /// 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. + /// The name of the destination image to save results to. + /// The shader to run. + private static void RunAndCompareShader( + in T shader, + Func>? transformMapperFactory, + int width, + int height, + string expectedFileName, + [CallerMemberName] string destinationFileName = "") + where T : unmanaged, ID2D1PixelShader + { + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + string temporaryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "temp"); + + _ = Directory.CreateDirectory(temporaryPath); + + string expectedPath = Path.Combine(assetsPath, expectedFileName); + string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); + + // Run the shader + D2D1TestRunner.ExecutePixelShaderAndCompareResults( + in shader, + transformMapperFactory, + width, + height, + destinationPath); + + // Compare the results + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index 6357a08a..373fc4f6 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -50,4 +50,45 @@ internal static class D2D1TestRunner // Save the image WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits); } + + /// + /// Executes a pixel shader to produce an image. + /// + /// The shader type to execute. + /// The shader to run. + /// The resulting width. + /// The resulting height. + /// A custom factory for the effect. + /// The destination path for the result. + public static unsafe void ExecutePixelShaderAndCompareResults( + in T shader, + Func>? transformMapperFactory, + int width, + int height, + string destinationPath) + where T : unmanaged, ID2D1PixelShader + { + using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); + using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); + using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); + + D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + + using ComPtr d2D1Effect = default; + + D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext( + d2D1DeviceContext.Get(), + (void**)d2D1Effect.GetAddressOf()); + + D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + + using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); + + // Save the image + WICHelper.SaveBitmapToFile(destinationPath, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits); + } }