Implement end to end D2D1 pixel shader effect tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:29 +02:00
1 親 e8ce3695f2
コミット c9083edef0
11ファイル変更
+216 -155

検索条件に一致するファイルはありません

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 1.7 MiB

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 2.0 MiB

+14
ファイルの表示
@@ -6,11 +6,16 @@
<NoWarn>$(NoWarn);CS0419;CA1416</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\ComputeSharp.Tests\Helpers\TolerantImageComparer.cs" Link="Helpers\ComputeSharp.Tests\TolerantImageComparer.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.20348" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
</ItemGroup>
<ItemGroup>
@@ -20,4 +25,13 @@
<ProjectReference Include="..\..\src\ComputeSharp.D2D1Interop.SourceGenerators\ComputeSharp.D2D1Interop.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
</ItemGroup>
<ItemGroup>
<None Update="Assets\Landscape.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\Landscape_Inverted.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+41 -2
ファイルの表示
@@ -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");
}
/// <summary>
/// Executes a pixel shader and compares the expected results.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <param name="originalFileName">The name of the source image.</param>
/// <param name="expectedFileName">The name of the expected result image.</param>
/// <param name="destinationFileName">The name of the destination image to save results to.</param>
/// <param name="shader">The shader to run.</param>
private static void RunAndCompareShader<T>(
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);
}
}
@@ -12,17 +12,15 @@ namespace ComputeSharp.D2D1Interop.Tests.Helpers;
internal static class D2D1ShaderTestHelper
{
/// <summary>
/// Executes a pixel shader and compares the results.
/// Executes a pixel shader to produce an image.
/// </summary>
/// <typeparam name="T">The shader type to execute.</typeparam>
/// <param name="sourcePath">The source path for the image to run the shader on.</param>
/// <param name="destinationPath">The destination path for the result.</param>
/// <param name="expectedPath">The path of the image with the expected output.</param>
/// <param name="shader">The shader to run.</param>
public static unsafe void ExecutePixelShaderAndCompareResults<T>(
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);
}
}
+6 -6
ファイルの表示
@@ -912,7 +912,7 @@ public partial class ComputeContextTests
using var processed = destination.ToImage<Rgba32, ImageSharpRgba32>();
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<Rgba32, ImageSharpRgba32>();
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<Rgba32, ImageSharpRgba32>(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<Rgba32, ImageSharpRgba32>(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<Rgba32, ImageSharpRgba32>();
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<Rgba32, ImageSharpRgba32>(depth: z);
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f);
TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f);
}
Rgba32[,,] clear = source.ToArray();
+144
ファイルの表示
@@ -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;
/// <summary>
/// An tolerant image comparer type, 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="expectedPath">The path to the reference image.</param>
/// <param name="actualPath">The path to the expected image.</param>
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
public static void AssertEqual(string expectedPath, string actualPath, float threshold)
{
using var expected = Image.Load<ImageSharpRgba32>(expectedPath);
using var actual = Image.Load<ImageSharpRgba32>(actualPath);
AssertEqual(expected, actual, threshold);
}
/// <summary>
/// Asserts that two images are equal.
/// </summary>
/// <typeparam name="TPixel">The type of image pixels to analyze.</typeparam>
/// <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<TPixel>(Image<TPixel> expected, Image<TPixel> actual, float threshold)
where TPixel : unmanaged, IPixel<TPixel>
{
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<PixelDifference>(20);
Span<ImageSharpRgba32> aBuffer = new ImageSharpRgba32[actual.Width];
Span<ImageSharpRgba32> bBuffer = new ImageSharpRgba32[actual.Width];
for (int y = 0; y < actual.Height; y++)
{
Span<TPixel> aSpan = expected.GetPixelRowSpan(y);
Span<TPixel> bSpan = actual.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.ToRgba32(actual.GetConfiguration(), aSpan, aBuffer);
PixelOperations<TPixel>.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());
}
}
/// <summary>
/// A simple model repressenting info about a difference between two pixels.
/// </summary>
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;
/// <inheritdoc/>
public override string ToString()
{
return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]";
}
}
}
+3 -2
ファイルの表示
@@ -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);
}
}
+1 -138
ファイルの表示
@@ -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!);
}
/// <summary>
/// An tolerant image comparer type, 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="expectedPath">The path to the reference image.</param>
/// <param name="actualPath">The path to the expected image.</param>
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
public static void AssertEqual(string expectedPath, string actualPath, float threshold)
{
using var expected = Image.Load<ImageSharpRgba32>(expectedPath);
using var actual = Image.Load<ImageSharpRgba32>(actualPath);
AssertEqual(expected, actual, threshold);
}
/// <summary>
/// Asserts that two images are equal.
/// </summary>
/// <typeparam name="TPixel">The type of image pixels to analyze.</typeparam>
/// <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<TPixel>(Image<TPixel> expected, Image<TPixel> actual, float threshold)
where TPixel : unmanaged, IPixel<TPixel>
{
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<PixelDifference>(20);
Span<ImageSharpRgba32> aBuffer = new ImageSharpRgba32[actual.Width];
Span<ImageSharpRgba32> bBuffer = new ImageSharpRgba32[actual.Width];
for (int y = 0; y < actual.Height; y++)
{
Span<TPixel> aSpan = expected.GetPixelRowSpan(y);
Span<TPixel> bSpan = actual.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.ToRgba32(actual.GetConfiguration(), aSpan, aBuffer);
PixelOperations<TPixel>.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());
}
}
/// <summary>
/// A simple model repressenting info about a difference between two pixels.
/// </summary>
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;
/// <inheritdoc/>
public override string ToString()
{
return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]";
}
}
}
}
+3 -2
ファイルの表示
@@ -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);
}
/// <summary>
@@ -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);
}
}
+2 -2
ファイルの表示
@@ -68,7 +68,7 @@ public partial class Texture2DTests
using var processed = destination.ToImage<Rgba32, ImageSharpRgba32>();
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<Rgba32, ImageSharpRgba32>();
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}
[AutoConstructor]