From fdf35114892079b4a02da326e0ec6300076f996e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 25 Apr 2023 03:59:27 +0200 Subject: [PATCH] Switch image comparer in UWP tests to Win2D --- .../ComputeSharp.D2D1.UI.Tests.projitems | 2 +- .../Helpers/TolerantImageComparer.cs | 117 ++++++++++++++++++ .../ShadersTests.cs | 21 ++-- .../ComputeSharp.D2D1.Uwp.Tests.csproj | 3 - .../Helpers/TolerantImageComparer.cs | 3 + 5 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems index fa0581dd..64c69933 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems +++ b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems @@ -9,8 +9,8 @@ ComputeSharp.D2D1.UI.Tests + - \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs new file mode 100644 index 00000000..fd008045 --- /dev/null +++ b/tests/ComputeSharp.D2D1.UI.Tests/Helpers/TolerantImageComparer.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.CompilerServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Graphics.Canvas; +using System.Drawing; +using Color = Windows.UI.Color; + +namespace ComputeSharp.D2D1.UI.Tests.Helpers; + +/// +/// An tolerant image comparer type, inspired from ImageSharp. +/// +internal static class TolerantImageComparer +{ + /// + /// Asserts that two images are equal. + /// + /// The reference image. + /// The expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(CanvasBitmap expected, CanvasBitmap actual, float threshold) + { + if (expected.SizeInPixels.Width != actual.SizeInPixels.Width || + expected.SizeInPixels.Height != actual.SizeInPixels.Height) + { + Assert.Fail($"The input images have different sizes: {expected.SizeInPixels} and {actual.SizeInPixels}"); + } + + Color[] expectedColors = expected.GetPixelColors(); + Color[] actualColors = actual.GetPixelColors(); + + float totalDifference = 0F; + + List differences = new(20); + + for (int i = 0; i < expectedColors.Length; i++) + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int GetManhattanDistanceInRgbaSpace(ref Color a, ref Color 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 expectedColors[i], ref actualColors[i]); + + if (d > 0) + { + if (differences.Count < 20) + { + int offsetY = (int)(i / expected.SizeInPixels.Width); + int offsetX = (int)(i % expected.SizeInPixels.Width); + + PixelDifference diff = new(new Point(offsetX, offsetY), expectedColors[i], actualColors[i]); + + differences.Add(diff); + } + + totalDifference += d; + } + } + + float normalizedDifference = totalDifference / (actual.SizeInPixels.Width * (float)actual.SizeInPixels.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 (PixelDifference 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, Color expected, Color 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({this.RedDifference},{this.GreenDifference},{this.BlueDifference},{this.AlphaDifference}) @ ({this.Position.X},{this.Position.Y})]"; + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs index aa4378c5..e7cd49a2 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs @@ -1,14 +1,13 @@ using System; using System.IO; using System.Threading.Tasks; +using ComputeSharp.D2D1.UI.Tests.Helpers; using ComputeSharp.D2D1.Uwp; using ComputeSharp.SwapChain.Shaders.D2D1; -using ComputeSharp.Tests.Helpers; using Microsoft.Graphics.Canvas; using Microsoft.VisualStudio.TestTools.UnitTesting; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using Windows.Storage; +using Windows.UI; namespace ComputeSharp.D2D1.UI.Tests; @@ -64,9 +63,12 @@ public class ShadersTests T shader = (T)Activator.CreateInstance(typeof(T), 0f, new int2(1280, 720))!; PixelShaderEffect effect = new() { ConstantBuffer = shader }; - byte[] pixelBytes; + Color[] pixelColors; - using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 1280, 720, 96.0f)) + using CanvasDevice canvasDevice = new(); + + // Compute the resulting image + using (CanvasRenderTarget renderTarget = new(canvasDevice, 1280, 720, 96.0f)) { // Draw the shader on the render target using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession()) @@ -75,15 +77,16 @@ public class ShadersTests } // Get the BGRA32 pixel data from the render target - pixelBytes = renderTarget.GetPixelBytes(); + pixelColors = renderTarget.GetPixelColors(); } StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/Shaders/{typeof(T).Name}.png")); + // Retrieve the expected image using Stream stream = await imageFile.OpenStreamForReadAsync(); - using Image expected = await Image.LoadAsync(stream); - using Image result = Image.WrapMemory(pixelBytes, 1280, 720); + using CanvasBitmap expected = await CanvasBitmap.LoadAsync(canvasDevice, stream.AsRandomAccessStream()); + using CanvasBitmap actual = CanvasBitmap.CreateFromColors(canvasDevice, pixelColors, 1280, 720); - TolerantImageComparer.AssertEqual(expected, result, threshold); + TolerantImageComparer.AssertEqual(expected, actual, threshold); } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj index 9e3a82af..797c5c22 100644 --- a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj @@ -137,9 +137,6 @@ 2.2.7 - - 2.1.4 - diff --git a/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs index 97ed11aa..078c4777 100644 --- a/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs +++ b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs @@ -85,6 +85,7 @@ internal static class TolerantImageComparer if (differences.Count < 20) { PixelDifference diff = new(new Point(x, y), aBuffer[x], bBuffer[x]); + differences.Add(diff); } @@ -94,11 +95,13 @@ internal static class TolerantImageComparer } 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:");