using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.Tests.Helpers;
using Win32;
using Win32.Graphics.Direct2D;
using D2D1_MAPPED_RECT = Win32.Graphics.Direct2D.MappedRect;
namespace ComputeSharp.D2D1.Tests.Helpers;
///
/// A test helper for D2D1 pixel shaders.
///
internal static class D2D1TestRunner
{
///
/// Executes a pixel shader and compares the expected results.
///
/// The type of pixel shader to run.
/// A custom instance 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.
/// The allowed difference threshold for the normalized delta.
public static void RunAndCompareShader(
in T shader,
D2D1TransformMapper? transformMapper,
string originalFileName,
string expectedFileName,
[CallerMemberName] string destinationFileName = "",
float threshold = 0.00001f)
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
ExecutePixelShaderAndSaveToFile(
in shader,
transformMapper,
originalPath,
destinationPath);
// Compare the results
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold);
}
///
/// Executes a pixel shader and compares the expected results.
///
/// The type of pixel shader to run.
/// 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.
/// The allowed difference threshold for the normalized delta.
/// The additional resource textures to use to run the shader.
public static void RunAndCompareShader(
in T shader,
int width,
int height,
string expectedFileName,
[CallerMemberName] string destinationFileName = "",
float threshold = 0.00001f,
params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures)
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
ExecutePixelShaderAndSaveToFile(
in shader,
null,
width,
height,
destinationPath,
resourceTextures);
// Compare the results
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold);
}
///
/// Executes a pixel shader to produce an image.
///
/// The shader type to execute.
/// The shader to run.
/// A custom instance for the effect.
/// The source path for the image to run the shader on.
/// The destination path for the result.
private static unsafe void ExecutePixelShaderAndSaveToFile(
in T shader,
D2D1TransformMapper? transformMapper,
string sourcePath,
string destinationPath)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get());
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _);
using ComPtr d2D1Effect = default;
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
if (transformMapper is not null)
{
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(d2D1Effect.Get(), transformMapper);
}
ReadOnlyMemory pixels = ImageHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height);
using ComPtr d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), pixels, width, height, d2D1Effect.Get());
using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), width, height);
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
// Save the image
ImageHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
}
///
/// Executes a pixel shader to produce an image.
///
/// The shader type to execute.
/// The shader to run.
/// The resulting width.
/// The resulting height.
/// A custom instance for the effect.
/// The destination path for the result.
/// The additional resource textures to use to run the shader.
private static unsafe void ExecutePixelShaderAndSaveToFile(
in T shader,
D2D1TransformMapper? transformMapper,
int width,
int height,
string destinationPath,
params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get());
D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _);
using ComPtr d2D1Effect = default;
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
if (transformMapper is not null)
{
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(d2D1Effect.Get(), transformMapper);
}
foreach ((int index, D2D1ResourceTextureManager resourceTextureManager) in resourceTextures)
{
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, index);
}
using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
// Save the image
ImageHelper.SaveBitmapToFile(destinationPath, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
}
}