70 行
2.4 KiB
C#
70 行
2.4 KiB
C#
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;
|
|
|
|
[TestClass]
|
|
[TestCategory("EndToEnd")]
|
|
public class EndToEndTests
|
|
{
|
|
[TestMethod]
|
|
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);
|
|
}
|
|
}
|
|
|
|
[D2DInputCount(1)]
|
|
[D2DInputSimple(0)]
|
|
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
|
|
[AutoConstructor]
|
|
public partial struct InvertShader : ID2D1PixelShader
|
|
{
|
|
public float number;
|
|
|
|
public float4 Execute()
|
|
{
|
|
float4 color = D2D1.GetInput(0);
|
|
float3 rgb = Hlsl.Saturate(this.number - color.RGB);
|
|
|
|
return new(rgb, 1);
|
|
}
|
|
} |