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); } }