using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using ComputeSharp.SwapChain.Shaders; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.Tests { [TestClass] [TestCategory("Shaders")] public class ShadersTests { [CombinatorialTestMethod] [AllDevices] public void ColorfulInfinity(Device device) { RunAndCompareShader(device, static texture => new ColorfulInfinity(texture, 0), 0.0000193f); } [CombinatorialTestMethod] [AllDevices] public void FractalTiling(Device device) { RunAndCompareShader(device, static texture => new FractalTiling(texture, 0), 0.0000282f); } [CombinatorialTestMethod] [AllDevices] public void MengerJourney(Device device) { RunAndCompareShader(device, static texture => new MengerJourney(texture, 0), 0.0000864f); } [CombinatorialTestMethod] [AllDevices] public void TwoTiledTruchet(Device device) { RunAndCompareShader(device, static texture => new TwoTiledTruchet(texture, 0), 0.00033f); } [CombinatorialTestMethod] [AllDevices] public void Octagrams(Device device) { RunAndCompareShader(device, static texture => new Octagrams(texture, 0), 0.00000586f); } [CombinatorialTestMethod] [AllDevices] public void ProteanClouds(Device device) { RunAndCompareShader(device, static texture => new ProteanClouds(texture, 0), 0.00000107f); } /// /// Executes a given test for a specified shader. /// /// The type of shader to test. /// The device to use. /// A producer to create an instance of the shader to run. /// The comparison delta. private static void RunAndCompareShader(Device device, Func, T> factory, float delta) where T : struct, IComputeShader { _ = device.Get(); string expectedPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", $"{typeof(T).Name}.jpg"); IImageInfo imageInfo = Image.Identify(expectedPath); using Image image = new(imageInfo.Width, imageInfo.Height); using (ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(imageInfo.Width, imageInfo.Height)) { device.Get().For(texture.Width, texture.Height, factory(texture)); _ = image.TryGetSinglePixelSpan(out Span span); texture.CopyTo(MemoryMarshal.Cast(span)); } string actualPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", $"{typeof(T).Name}.jpg"); _ = Directory.CreateDirectory(Path.GetDirectoryName(actualPath)!); image.Save(actualPath); ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta); } } }