Add CanvasEffect integration tests
このコミットが含まれているのは:
1 親
fdf3511489
コミット
c561c18b5e
4ファイル変更
+182
-17
検索条件に一致するファイルはありません
@@ -0,0 +1,98 @@
|
||||
using ComputeSharp.D2D1.Uwp;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.D2D1.UI.Tests;
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("CanvasEffect")]
|
||||
public partial class CanvasEffectTests
|
||||
{
|
||||
[TestMethod]
|
||||
public unsafe void CanvasEffect_IsRealizedAndInvalidatedCorrectly()
|
||||
{
|
||||
EffectWithNoInputs effect = new();
|
||||
|
||||
using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f))
|
||||
using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession())
|
||||
{
|
||||
drawingSession.DrawImage(effect);
|
||||
}
|
||||
|
||||
Assert.AreEqual(effect.NumberOfBuildEffectGraphCalls, 1);
|
||||
Assert.AreEqual(effect.NumberOfConfigureEffectGraphCalls, 1);
|
||||
|
||||
effect.Value = 42;
|
||||
|
||||
using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f))
|
||||
using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession())
|
||||
{
|
||||
drawingSession.DrawImage(effect);
|
||||
}
|
||||
|
||||
Assert.AreEqual(effect.NumberOfBuildEffectGraphCalls, 1);
|
||||
Assert.AreEqual(effect.NumberOfConfigureEffectGraphCalls, 2);
|
||||
|
||||
effect.ValueWithReload = 123;
|
||||
|
||||
using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f))
|
||||
using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession())
|
||||
{
|
||||
drawingSession.DrawImage(effect);
|
||||
}
|
||||
|
||||
Assert.AreEqual(effect.NumberOfBuildEffectGraphCalls, 2);
|
||||
Assert.AreEqual(effect.NumberOfConfigureEffectGraphCalls, 3);
|
||||
}
|
||||
|
||||
private sealed class EffectWithNoInputs : CanvasEffect
|
||||
{
|
||||
private PixelShaderEffect<ShaderWithNoInputs>? effect;
|
||||
|
||||
private int value;
|
||||
|
||||
public int Value
|
||||
{
|
||||
get => this.value;
|
||||
set => SetAndInvalidateEffectGraph(ref this.value, value);
|
||||
}
|
||||
|
||||
public int ValueWithReload
|
||||
{
|
||||
get => this.value;
|
||||
set => SetAndInvalidateEffectGraph(ref this.value, value, InvalidationType.Creation);
|
||||
}
|
||||
|
||||
public int NumberOfBuildEffectGraphCalls { get; private set; }
|
||||
|
||||
public int NumberOfConfigureEffectGraphCalls { get; private set; }
|
||||
|
||||
protected override ICanvasImage BuildEffectGraph()
|
||||
{
|
||||
NumberOfBuildEffectGraphCalls++;
|
||||
|
||||
return this.effect = new PixelShaderEffect<ShaderWithNoInputs>();
|
||||
}
|
||||
|
||||
protected override void ConfigureEffectGraph()
|
||||
{
|
||||
NumberOfConfigureEffectGraphCalls++;
|
||||
|
||||
this.effect!.ConstantBuffer = new ShaderWithNoInputs(this.value);
|
||||
}
|
||||
}
|
||||
|
||||
[D2DInputCount(0)]
|
||||
[AutoConstructor]
|
||||
private partial struct ShaderWithNoInputs : ID2D1PixelShader
|
||||
{
|
||||
public int Value;
|
||||
|
||||
public float4 Execute()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\TolerantImageComparer.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)CanvasEffectTests.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffectTests.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ShadersTests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -56,6 +56,20 @@ public partial class PixelShaderEffectTests
|
||||
|
||||
// The shader also matches when retrieving it through the realized effect
|
||||
Assert.IsTrue(actual.SequenceEqual(expected));
|
||||
|
||||
// Draw the effect on a different device (this forces it to unrealize and re-realize).
|
||||
// This also acts as a unit test for the device changed logic, since that's used here.
|
||||
using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f))
|
||||
using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession())
|
||||
{
|
||||
drawingSession.DrawImage(effect);
|
||||
}
|
||||
|
||||
roundTripShader = effect.ConstantBuffer;
|
||||
actual = new(&roundTripShader, sizeof(ShaderWithSomeProperties));
|
||||
|
||||
// The shader also matches when retrieving it through the realized effect
|
||||
Assert.IsTrue(actual.SequenceEqual(expected));
|
||||
}
|
||||
|
||||
[D2DInputCount(0)]
|
||||
|
||||
@@ -9,6 +9,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Windows.Storage;
|
||||
using Windows.UI;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.D2D1.UI.Tests;
|
||||
|
||||
[TestClass]
|
||||
@@ -16,52 +18,72 @@ namespace ComputeSharp.D2D1.UI.Tests;
|
||||
public class ShadersTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task HelloWorld()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task HelloWorld(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<HelloWorld>();
|
||||
await RunTestAsync<HelloWorld>(wrapperType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ColorfulInfinity()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task ColorfulInfinity(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<ColorfulInfinity>();
|
||||
await RunTestAsync<ColorfulInfinity>(wrapperType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task FractalTiling()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task FractalTiling(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<FractalTiling>();
|
||||
await RunTestAsync<FractalTiling>(wrapperType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MengerJourney()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task MengerJourney(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<MengerJourney>(0.000011f);
|
||||
await RunTestAsync<MengerJourney>(wrapperType, 0.000011f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Octagrams()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task Octagrams(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<Octagrams>();
|
||||
await RunTestAsync<Octagrams>(wrapperType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ProteanClouds()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task ProteanClouds(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<ProteanClouds>();
|
||||
await RunTestAsync<ProteanClouds>(wrapperType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TerracedHills()
|
||||
[DataRow(WrapperType.PixelShaderEffect)]
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task TerracedHills(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<TerracedHills>(0.000026f);
|
||||
await RunTestAsync<TerracedHills>(wrapperType, 0.000026f);
|
||||
}
|
||||
|
||||
private static async Task RunTestAsync<T>(float threshold = 0.00001f)
|
||||
private static async Task RunTestAsync<T>(WrapperType wrapperType, float threshold = 0.00001f)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
T shader = (T)Activator.CreateInstance(typeof(T), 0f, new int2(1280, 720))!;
|
||||
PixelShaderEffect<T> effect = new() { ConstantBuffer = shader };
|
||||
|
||||
// Either create the effect directly, or a canvas effect
|
||||
ICanvasImage canvasImage = wrapperType switch
|
||||
{
|
||||
WrapperType.PixelShaderEffect => new PixelShaderEffect<T>() { ConstantBuffer = shader },
|
||||
_ => new TestCanvasEffect<T> { ConstantBuffer = shader }
|
||||
};
|
||||
|
||||
Color[] pixelColors;
|
||||
|
||||
@@ -73,7 +95,7 @@ public class ShadersTests
|
||||
// Draw the shader on the render target
|
||||
using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession())
|
||||
{
|
||||
drawingSession.DrawImage(effect);
|
||||
drawingSession.DrawImage(canvasImage);
|
||||
}
|
||||
|
||||
// Get the BGRA32 pixel data from the render target
|
||||
@@ -89,4 +111,34 @@ public class ShadersTests
|
||||
|
||||
TolerantImageComparer.AssertEqual(expected, actual, threshold);
|
||||
}
|
||||
|
||||
private sealed class TestCanvasEffect<T> : CanvasEffect
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
private PixelShaderEffect<T>? effect;
|
||||
|
||||
private T constantBuffer;
|
||||
|
||||
public T ConstantBuffer
|
||||
{
|
||||
get => this.constantBuffer;
|
||||
set => SetAndInvalidateEffectGraph(ref this.constantBuffer, value);
|
||||
}
|
||||
|
||||
protected override ICanvasImage BuildEffectGraph()
|
||||
{
|
||||
return this.effect = new PixelShaderEffect<T>();
|
||||
}
|
||||
|
||||
protected override void ConfigureEffectGraph()
|
||||
{
|
||||
this.effect!.ConstantBuffer = this.constantBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
public enum WrapperType
|
||||
{
|
||||
PixelShaderEffect,
|
||||
CanvasEffect
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする