diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 1f3e2fa9..987e7491 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -483,6 +483,8 @@ Global samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{209c95a3-fa53-431b-b688-3299ca6c29d2}*SharedItemsImports = 5 src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{314eb353-e7e3-4146-8111-685cc83ec1de}*SharedItemsImports = 5 tests\ComputeSharp.D2D1.UI.Tests\ComputeSharp.D2D1.UI.Tests.projitems*{346aeb10-a4ea-427e-8578-f60915db3053}*SharedItemsImports = 13 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4 + tests\ComputeSharp.D2D1.UI.Tests\ComputeSharp.D2D1.UI.Tests.projitems*{4d4bb2f6-5653-4db5-a8dd-90d58d8fe4d3}*SharedItemsImports = 4 src\TerraFX.Interop.Windows.D2D1\TerraFX.Interop.Windows.D2D1.projitems*{4d7d9ba8-a821-4b1c-9ee2-cbcf5849906e}*SharedItemsImports = 5 src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{514ff00e-bbf0-4d55-a47c-eeeb6208adce}*SharedItemsImports = 13 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{51d5a422-d3a5-4c4d-8a6d-a048940bf279}*SharedItemsImports = 13 diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems index b7d37ed3..fa0581dd 100644 --- a/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems +++ b/tests/ComputeSharp.D2D1.UI.Tests/ComputeSharp.D2D1.UI.Tests.projitems @@ -9,5 +9,8 @@ ComputeSharp.D2D1.UI.Tests + + + - + \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs new file mode 100644 index 00000000..674bd754 --- /dev/null +++ b/tests/ComputeSharp.D2D1.UI.Tests/PixelShaderEffectTests.cs @@ -0,0 +1,76 @@ +using System; +using ComputeSharp.D2D1.Uwp; +using Microsoft.Graphics.Canvas; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.UI.Tests; + +[TestClass] +[TestCategory("PixelShaderEffect")] +public partial class PixelShaderEffectTests +{ + [TestMethod] + public unsafe void ConstantBuffer_IsInitiallySetToDefault() + { + PixelShaderEffect effect = new(); + + ShaderWithSomeProperties shader = effect.ConstantBuffer; + ShaderWithSomeProperties defaultShader = default; + + ReadOnlySpan actual = new(&shader, sizeof(ShaderWithSomeProperties)); + ReadOnlySpan expected = new(&defaultShader, sizeof(ShaderWithSomeProperties)); + + // The default constant buffer is set to 0, and it can be retrieved correctly + Assert.IsTrue(actual.SequenceEqual(expected)); + } + + [TestMethod] + public unsafe void ConstantBuffer_RoundTripsCorrectly() + { + ShaderWithSomeProperties shader = new( + A: 3.14f, + B: 123456, + C: new int2(111, -222), + D: new uint2x2(8888, 123456, 98765, 192837465), + E: new float2(-3.14f, 6.28f)); + + PixelShaderEffect effect = new() { ConstantBuffer = shader }; + + ShaderWithSomeProperties roundTripShader = effect.ConstantBuffer; + + ReadOnlySpan expected = new(&shader, sizeof(ShaderWithSomeProperties)); + ReadOnlySpan actual = new(&roundTripShader, sizeof(ShaderWithSomeProperties)); + + // The resulting constant buffer matches the source, when the effect is not realized + Assert.IsTrue(actual.SequenceEqual(expected)); + + // Draw the effect, to force realization + 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)] + [AutoConstructor] + private partial struct ShaderWithSomeProperties : ID2D1PixelShader + { + public float A; + public int B; + public int2 C; + public uint2x2 D; + public float2 E; + + public float4 Execute() + { + return default; + } + } +} diff --git a/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs new file mode 100644 index 00000000..aa4378c5 --- /dev/null +++ b/tests/ComputeSharp.D2D1.UI.Tests/ShadersTests.cs @@ -0,0 +1,89 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using ComputeSharp.D2D1.Uwp; +using ComputeSharp.SwapChain.Shaders.D2D1; +using ComputeSharp.Tests.Helpers; +using Microsoft.Graphics.Canvas; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using Windows.Storage; + +namespace ComputeSharp.D2D1.UI.Tests; + +[TestClass] +[TestCategory("Shaders")] +public class ShadersTests +{ + [TestMethod] + public async Task HelloWorld() + { + await RunTestAsync(); + } + + [TestMethod] + public async Task ColorfulInfinity() + { + await RunTestAsync(); + } + + [TestMethod] + public async Task FractalTiling() + { + await RunTestAsync(); + } + + [TestMethod] + public async Task MengerJourney() + { + await RunTestAsync(0.000011f); + } + + [TestMethod] + public async Task Octagrams() + { + await RunTestAsync(); + } + + [TestMethod] + public async Task ProteanClouds() + { + await RunTestAsync(); + } + + [TestMethod] + public async Task TerracedHills() + { + await RunTestAsync(0.000026f); + } + + private static async Task RunTestAsync(float threshold = 0.00001f) + where T : unmanaged, ID2D1PixelShader + { + T shader = (T)Activator.CreateInstance(typeof(T), 0f, new int2(1280, 720))!; + PixelShaderEffect effect = new() { ConstantBuffer = shader }; + + byte[] pixelBytes; + + using (CanvasRenderTarget renderTarget = new(new CanvasDevice(), 1280, 720, 96.0f)) + { + // Draw the shader on the render target + using (CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession()) + { + drawingSession.DrawImage(effect); + } + + // Get the BGRA32 pixel data from the render target + pixelBytes = renderTarget.GetPixelBytes(); + } + + StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/Shaders/{typeof(T).Name}.png")); + + using Stream stream = await imageFile.OpenStreamForReadAsync(); + using Image expected = await Image.LoadAsync(stream); + using Image result = Image.WrapMemory(pixelBytes, 1280, 720); + + TolerantImageComparer.AssertEqual(expected, result, threshold); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj index daae7b31..9e3a82af 100644 --- a/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Uwp.Tests/ComputeSharp.D2D1.Uwp.Tests.csproj @@ -90,6 +90,34 @@ + + Assets\Shaders\ColorfulInfinity.png + PreserveNewest + + + Assets\Shaders\FractalTiling.png + PreserveNewest + + + Assets\Shaders\HelloWorld.png + PreserveNewest + + + Assets\Shaders\MengerJourney.png + PreserveNewest + + + Assets\Shaders\Octagrams.png + PreserveNewest + + + Assets\Shaders\ProteanClouds.png + PreserveNewest + + + Assets\Shaders\TerracedHills.png + PreserveNewest + @@ -109,7 +137,19 @@ 2.2.7 + + 2.1.4 + + + + + + + + + + 14.0