Switch image comparer in UWP tests to Win2D
このコミットが含まれているのは:
1 親
5adce3d048
コミット
fdf3511489
5ファイル変更
+133
-13
検索条件に一致するファイルはありません
@@ -9,8 +9,8 @@
|
||||
<Import_RootNamespace>ComputeSharp.D2D1.UI.Tests</Import_RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\TolerantImageComparer.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffectTests.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Tests\Helpers\TolerantImageComparer.cs" Link="Helpers\TolerantImageComparer.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ShadersTests.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// An tolerant image comparer type, inspired from <see href="https://github.com/SixLabors/ImageSharp">ImageSharp</see>.
|
||||
/// </summary>
|
||||
internal static class TolerantImageComparer
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that two images are equal.
|
||||
/// </summary>
|
||||
/// <param name="expected">The reference image.</param>
|
||||
/// <param name="actual">The expected image.</param>
|
||||
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
|
||||
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<PixelDifference> 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());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A simple model repressenting info about a difference between two pixels.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[delta({this.RedDifference},{this.GreenDifference},{this.BlueDifference},{this.AlphaDifference}) @ ({this.Position.X},{this.Position.Y})]";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<T> 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<Bgra32> expected = await Image.LoadAsync<Bgra32>(stream);
|
||||
using Image<Bgra32> result = Image.WrapMemory<Bgra32>(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);
|
||||
}
|
||||
}
|
||||
@@ -137,9 +137,6 @@
|
||||
<PackageReference Include="MSTest.TestFramework">
|
||||
<Version>2.2.7</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="SixLabors.ImageSharp">
|
||||
<Version>2.1.4</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
|
||||
@@ -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:");
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする