Add ZonePlate test shader (no inputs, default mapping)

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:32 +02:00
1 親 4aeb97113a
コミット 8558af237f
5ファイル変更
+124 -1

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

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

変更後

幅:  |  高さ:  |  サイズ: 414 KiB

+3
ファイルの表示
@@ -35,6 +35,9 @@
<None Update="Assets\Landscape_Pixelate.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\ZonePlate.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+34
ファイルの表示
@@ -0,0 +1,34 @@
using System;
namespace ComputeSharp.D2D1.Tests.Effects;
[D2DInputCount(0)]
[D2DRequiresScenePosition]
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
[AutoConstructor]
public readonly partial struct ZonePlateEffect : ID2D1PixelShader
{
private readonly int width;
private readonly int height;
private readonly float diameter;
/// <inheritdoc/>
public float4 Execute()
{
float2 scenePos = D2D.GetScenePosition().XY;
float xo = scenePos.X - (this.width >> 1);
float yo = scenePos.Y - (this.height >> 1);
float rm = 0.5f * this.diameter;
float km = 0.7f / this.diameter * MathF.PI;
float w = rm / 10.0f;
float yd = yo * yo;
float xd = xo * xo;
float d = xd + yd;
float v = 1.0f + (1.0f + Hlsl.Tanh((rm - Hlsl.Sqrt(d)) / w)) * Hlsl.Sin(km * d) * 0.5f;
return new Float4(v, v, v, 1.0f);
}
}
+46 -1
ファイルの表示
@@ -29,12 +29,18 @@ public class EndToEndTests
"Landscape_Pixelate.png");
}
[TestMethod]
public unsafe void ZonePlate()
{
RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.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="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <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>
@@ -65,4 +71,43 @@ public class EndToEndTests
// Compare the results
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
}
/// <summary>
/// Executes a pixel shader and compares the expected results.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="width">The resulting width.</param>
/// <param name="height">The resulting height.</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,
Func<ID2D1TransformMapper<T>>? 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);
}
}
+41
ファイルの表示
@@ -50,4 +50,45 @@ internal static class D2D1TestRunner
// Save the image
WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
}
/// <summary>
/// Executes a pixel shader to produce an image.
/// </summary>
/// <typeparam name="T">The shader type to execute.</typeparam>
/// <param name="shader">The shader to run.</param>
/// <param name="width">The resulting width.</param>
/// <param name="height">The resulting height.</param>
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="destinationPath">The destination path for the result.</param>
public static unsafe void ExecutePixelShaderAndCompareResults<T>(
in T shader,
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
int width,
int height,
string destinationPath)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
using ComPtr<ID2D1Device> d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
using ComPtr<ID2D1DeviceContext> d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get());
D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _);
using ComPtr<ID2D1Effect> d2D1Effect = default;
D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext<T>(
d2D1DeviceContext.Get(),
(void**)d2D1Effect.GetAddressOf());
D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
// Save the image
WICHelper.SaveBitmapToFile(destinationPath, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
}
}