using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Tests.Effects;
using ComputeSharp.D2D1.Tests.Helpers;
using ComputeSharp.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
namespace ComputeSharp.D2D1.Tests;
[TestClass]
[TestCategory("EndToEnd")]
public class EndToEndTests
{
[AssemblyInitialize]
public static void ConfigureImageSharp()
{
Configuration.Default.PreferContiguousImageBuffers = true;
}
[TestMethod]
public unsafe void Invert()
{
RunAndCompareShader(new InvertEffect(), null, "Landscape.png", "Landscape_Inverted.png");
}
[TestMethod]
public unsafe void InvertWithThreshold()
{
RunAndCompareShader(new InvertWithThresholdEffect(1), null, "Landscape.png", "Landscape_Inverted.png");
}
[TestMethod]
public unsafe void Pixelate()
{
RunAndCompareShader(
new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 840, 16)),
static () => new PixelateEffect(),
"Landscape.png",
"Landscape_Pixelate.png");
}
[TestMethod]
public unsafe void ZonePlate()
{
RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png");
}
[TestMethod]
public unsafe void CheckerboardClip()
{
RunAndCompareShader(new CheckerboardClipEffect(1280, 840, 32), null, "Landscape.png", "Landscape_CheckerboardClip.png");
}
///
/// Executes a pixel shader and compares the expected results.
///
/// The type of pixel shader to run.
/// 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.
private static void RunAndCompareShader(
in T shader,
Func>? transformMapperFactory,
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
D2D1TestRunner.ExecutePixelShaderAndCompareResults(
in shader,
transformMapperFactory,
originalPath,
destinationPath);
// 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);
}
}