diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape.png b/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape.png new file mode 100644 index 00000000..7a09dc6d Binary files /dev/null and b/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape.png differ diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape_Inverted.png b/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape_Inverted.png new file mode 100644 index 00000000..966ba3a4 Binary files /dev/null and b/tests/ComputeSharp.D2D1Interop.Tests/Assets/Landscape_Inverted.png differ diff --git a/tests/ComputeSharp.D2D1Interop.Tests/ComputeSharp.D2D1Interop.Tests.csproj b/tests/ComputeSharp.D2D1Interop.Tests/ComputeSharp.D2D1Interop.Tests.csproj index 2a52626f..c695fcc1 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/ComputeSharp.D2D1Interop.Tests.csproj +++ b/tests/ComputeSharp.D2D1Interop.Tests/ComputeSharp.D2D1Interop.Tests.csproj @@ -6,11 +6,16 @@ $(NoWarn);CS0419;CA1416 + + + + + @@ -20,4 +25,13 @@ + + + PreserveNewest + + + PreserveNewest + + + diff --git a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs index 7b4eebbe..9cb0d289 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs @@ -1,4 +1,8 @@ -using ComputeSharp.D2D1Interop.Tests.Helpers; +using System.IO; +using System.Reflection; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1Interop.Tests.Helpers; +using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1Interop.Tests; @@ -8,8 +12,43 @@ namespace ComputeSharp.D2D1Interop.Tests; public class EndToEndTests { [TestMethod] - public unsafe void Test() + public unsafe void InvertEffectWithCustomThreshold() { + RunAndCompareShader(new InvertShader(1), "Landscape.png", "Landscape_Inverted.png"); + } + + /// + /// Executes a pixel shader and compares the expected results. + /// + /// The type of pixel shader to run. + /// 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. + private static void RunAndCompareShader( + in T shader, + string originalFileName, + 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 originalPath = Path.Combine(assetsPath, originalFileName); + string expectedPath = Path.Combine(assetsPath, expectedFileName); + string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); + + // Run the shader + D2D1ShaderTestHelper.ExecutePixelShaderAndCompareResults( + originalPath, + destinationPath, + in shader); + + // Compare the results + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); } } diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderRunner.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs similarity index 98% rename from tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderRunner.cs rename to tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs index f16d7df9..7e67b4d0 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderRunner.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs @@ -12,17 +12,15 @@ namespace ComputeSharp.D2D1Interop.Tests.Helpers; internal static class D2D1ShaderTestHelper { /// - /// Executes a pixel shader and compares the results. + /// Executes a pixel shader to produce an image. /// /// The shader type to execute. /// The source path for the image to run the shader on. /// The destination path for the result. - /// The path of the image with the expected output. /// The shader to run. public static unsafe void ExecutePixelShaderAndCompareResults( string sourcePath, string destinationPath, - string expectedPath, in T shader) where T : unmanaged, ID2D1PixelShader { @@ -224,6 +222,7 @@ internal static class D2D1ShaderTestHelper options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ, mappedRect: &d2D1MappedRect).Assert(); + // Save the image WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits); } } diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 23a1f5bc..c7cf2ebf 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -912,7 +912,7 @@ public partial class ComputeContextTests using var processed = destination.ToImage(); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000174f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.00000174f); } [CombinatorialTestMethod] @@ -938,7 +938,7 @@ public partial class ComputeContextTests using var processed = destination.ToImage(); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } [CombinatorialTestMethod] @@ -989,7 +989,7 @@ public partial class ComputeContextTests { using var processed = destination.ToImage(depth: z); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } } @@ -1033,7 +1033,7 @@ public partial class ComputeContextTests { using var processed = destination.ToImage(depth: z); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } } @@ -1062,7 +1062,7 @@ public partial class ComputeContextTests using var processed = destination.ToImage(); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); Rgba32[,] clear = source.ToArray(); @@ -1114,7 +1114,7 @@ public partial class ComputeContextTests { using var processed = destination.ToImage(depth: z); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } Rgba32[,,] clear = source.ToArray(); diff --git a/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs new file mode 100644 index 00000000..1c7c16ef --- /dev/null +++ b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.Text; +using SixLabors.ImageSharp; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Advanced; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Tests.Helpers; + +/// +/// An tolerant image comparer type, from ImageSharp. +/// +internal static class TolerantImageComparer +{ + /// + /// Asserts that two images are equal. + /// + /// The path to the reference image. + /// The path to the expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(string expectedPath, string actualPath, float threshold) + { + using var expected = Image.Load(expectedPath); + using var actual = Image.Load(actualPath); + + AssertEqual(expected, actual, threshold); + } + + /// + /// Asserts that two images are equal. + /// + /// The type of image pixels to analyze. + /// The reference image. + /// The expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(Image expected, Image actual, float threshold) + where TPixel : unmanaged, IPixel + { + if (expected.Size() != actual.Size()) + { + Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}"); + } + + if (expected.Frames.Count != actual.Frames.Count || + expected.Frames.Count != 1 || + actual.Frames.Count != 1) + { + Assert.Fail("The two input images must have 1 frame each"); + } + + int width = actual.Width; + + float totalDifference = 0F; + + var differences = new List(20); + Span aBuffer = new ImageSharpRgba32[actual.Width]; + Span bBuffer = new ImageSharpRgba32[actual.Width]; + + for (int y = 0; y < actual.Height; y++) + { + Span aSpan = expected.GetPixelRowSpan(y); + Span bSpan = actual.GetPixelRowSpan(y); + + PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), aSpan, aBuffer); + PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), bSpan, bBuffer); + + for (int x = 0; x < width; x++) + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b) + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int Diff(ushort a, ushort b) => Math.Abs(a - b); + + return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); + } + + int d = GetManhattanDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); + + if (d > 0) + { + if (differences.Count < 20) + { + var diff = new PixelDifference(new Point(x, y), aBuffer[x], bBuffer[x]); + differences.Add(diff); + } + + totalDifference += d; + } + } + } + + float normalizedDifference = totalDifference / (actual.Width * (float)actual.Height); + normalizedDifference /= 4F * 65535F; + + if (normalizedDifference > threshold) + { + StringBuilder builder = new(); + builder.AppendLine($"The input images don't match. Normalized delta: {normalizedDifference}%"); + builder.AppendLine($"First {differences.Count} pixel deltas:"); + + foreach (var delta in differences) + { + builder.AppendLine(delta.ToString()); + } + + Assert.Fail(builder.ToString()); + } + } + + /// + /// A simple model repressenting info about a difference between two pixels. + /// + private readonly struct PixelDifference + { + public PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual) + { + this.Position = position; + this.RedDifference = actual.R - expected.R; + this.GreenDifference = actual.G - expected.G; + this.BlueDifference = actual.B - expected.B; + this.AlphaDifference = actual.A - expected.A; + } + + public readonly Point Position; + + public readonly int RedDifference; + + public readonly int GreenDifference; + + public readonly int BlueDifference; + + public readonly int AlphaDifference; + + /// + public override string ToString() + { + return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]"; + } + } +} diff --git a/tests/ComputeSharp.Tests/ImageProcessingTests.cs b/tests/ComputeSharp.Tests/ImageProcessingTests.cs index 3399defc..99c6e5b1 100644 --- a/tests/ComputeSharp.Tests/ImageProcessingTests.cs +++ b/tests/ComputeSharp.Tests/ImageProcessingTests.cs @@ -3,6 +3,7 @@ using System.Reflection; using ComputeSharp.BokehBlur.Processors; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; @@ -37,7 +38,7 @@ public class ImageProcessingTests cpu.Save(expectedPath); gpu.Save(actualPath); - ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f); + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f); } [CombinatorialTestMethod] @@ -61,6 +62,6 @@ public class ImageProcessingTests cpu.Save(expectedPath); gpu.Save(actualPath); - ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.0000046f); + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.0000046f); } } diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index e301aa2d..75849c9a 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -1,21 +1,17 @@ using System; using System.Buffers; -using System.Collections.Generic; using System.IO; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Text; using CommunityToolkit.HighPerformance.Buffers; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.PixelFormats; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32; using ImageSharpL8 = SixLabors.ImageSharp.PixelFormats.L8; +using ComputeSharp.Tests.Helpers; namespace ComputeSharp.Tests; @@ -530,137 +526,4 @@ public class ImagingTests texture.Save(typeof(string), null!); } - - /// - /// An tolerant image comparer type, from ImageSharp. - /// - internal static class TolerantImageComparer - { - /// - /// Asserts that two images are equal. - /// - /// The path to the reference image. - /// The path to the expected image. - /// The allowed difference threshold for the normalized delta. - public static void AssertEqual(string expectedPath, string actualPath, float threshold) - { - using var expected = Image.Load(expectedPath); - using var actual = Image.Load(actualPath); - - AssertEqual(expected, actual, threshold); - } - - /// - /// Asserts that two images are equal. - /// - /// The type of image pixels to analyze. - /// The reference image. - /// The expected image. - /// The allowed difference threshold for the normalized delta. - public static void AssertEqual(Image expected, Image actual, float threshold) - where TPixel : unmanaged, IPixel - { - if (expected.Size() != actual.Size()) - { - Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}"); - } - - if (expected.Frames.Count != actual.Frames.Count || - expected.Frames.Count != 1 || - actual.Frames.Count != 1) - { - Assert.Fail("The two input images must have 1 frame each"); - } - - int width = actual.Width; - - float totalDifference = 0F; - - var differences = new List(20); - Span aBuffer = new ImageSharpRgba32[actual.Width]; - Span bBuffer = new ImageSharpRgba32[actual.Width]; - - for (int y = 0; y < actual.Height; y++) - { - Span aSpan = expected.GetPixelRowSpan(y); - Span bSpan = actual.GetPixelRowSpan(y); - - PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), aSpan, aBuffer); - PixelOperations.Instance.ToRgba32(actual.GetConfiguration(), bSpan, bBuffer); - - for (int x = 0; x < width; x++) - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b) - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static int Diff(ushort a, ushort b) => Math.Abs(a - b); - - return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); - } - - int d = GetManhattanDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); - - if (d > 0) - { - if (differences.Count < 20) - { - var diff = new PixelDifference(new Point(x, y), aBuffer[x], bBuffer[x]); - differences.Add(diff); - } - - totalDifference += d; - } - } - } - - float normalizedDifference = totalDifference / (actual.Width * (float)actual.Height); - normalizedDifference /= 4F * 65535F; - - if (normalizedDifference > threshold) - { - StringBuilder builder = new(); - builder.AppendLine($"The input images don't match. Normalized delta: {normalizedDifference}%"); - builder.AppendLine($"First {differences.Count} pixel deltas:"); - - foreach (var delta in differences) - { - builder.AppendLine(delta.ToString()); - } - - Assert.Fail(builder.ToString()); - } - } - - /// - /// A simple model repressenting info about a difference between two pixels. - /// - private readonly struct PixelDifference - { - public PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual) - { - this.Position = position; - this.RedDifference = actual.R - expected.R; - this.GreenDifference = actual.G - expected.G; - this.BlueDifference = actual.B - expected.B; - this.AlphaDifference = actual.A - expected.A; - } - - public readonly Point Position; - - public readonly int RedDifference; - - public readonly int GreenDifference; - - public readonly int BlueDifference; - - public readonly int AlphaDifference; - - /// - public override string ToString() - { - return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]"; - } - } - } } diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 31a9b1e0..bc04c844 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -6,6 +6,7 @@ using ComputeSharp.Interop; using ComputeSharp.SwapChain.Shaders; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; @@ -196,7 +197,7 @@ public class ShadersTests image.SaveAsPng(actualPath, new PngEncoder() { CompressionLevel = PngCompressionLevel.BestCompression, ColorType = PngColorType.Rgb }); - ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta); + TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta); } /// @@ -256,6 +257,6 @@ public class ShadersTests image.SaveAsPng(actualPath, new PngEncoder() { CompressionLevel = PngCompressionLevel.BestCompression, ColorType = PngColorType.Rgb }); - ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta); + TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta); } } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index 30ef0a3c..f357dfc0 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -68,7 +68,7 @@ public partial class Texture2DTests using var processed = destination.ToImage(); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } [AutoConstructor] @@ -101,7 +101,7 @@ public partial class Texture2DTests using var processed = destination.ToImage(); - ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } [AutoConstructor]