diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png new file mode 100644 index 00000000..0430d2c7 Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png differ diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 05ed6ec2..6839ee84 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -40,6 +40,9 @@ + + PreserveNewest + PreserveNewest diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index f9e8a0a7..b9736eb0 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -8,7 +8,7 @@ using ComputeSharp.Resources; namespace ComputeSharp.Tests.Extensions { /// - /// A helper class for testing APIs. + /// A helper class for testing resource types. /// public static class GraphicsDeviceExtensions { diff --git a/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs new file mode 100644 index 00000000..6994ab4c --- /dev/null +++ b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; + +namespace ComputeSharp.Tests.Extensions +{ + /// + /// A helper class for loading images into resources. + /// + public static class ImagingExtensions + { + /// + /// Allocates a new instance with the specified texture data. + /// + /// The target instance to allocate the texture for. + /// The path of the texture to load. + /// A texture with the data from the image at the specified path. + [Pure] + public static ReadOnlyTexture2D LoadTexture(this GraphicsDevice device, string path) + { + using Image image = Image.Load(path); + + Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); + + Span pixels = MemoryMarshal.Cast(span); + + return device.AllocateReadOnlyTexture2D(pixels, image.Width, image.Height); + } + + /// + /// Creates a new instance with the specified texture data. + /// + /// The source instance to read data from. + /// An image with the data from the input texture. + [Pure] + public static Image ToImage(this ReadWriteTexture2D texture) + { + Image image = new(texture.Width, texture.Height); + + Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); + + Span pixels = MemoryMarshal.Cast(span); + + texture.CopyTo(pixels); + + return image; + } + } +} diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index ef68773d..97ffe400 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -75,11 +75,22 @@ namespace ComputeSharp.Tests /// The path to the reference image. /// The path to the expected image. /// The allowed difference threshold for the normalized delta. - public static void AssertEqual(string expectedPath, string actualPath, float threshold) + public static void AssertEqual(string expectedPath, string actualPath, float threshold) { using var expected = Image.Load(expectedPath); using var actual = Image.Load(actualPath); + AssertEqual(expected, actual, threshold); + } + + /// + /// Asserts that two images are equal. + /// + /// The reference image. + /// The expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(Image expected, Image actual, float threshold) + { if (expected.Size() != actual.Size()) { Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}"); diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index 5eaf8df4..294dd521 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Numerics; using System.Reflection; using ComputeSharp.__Internals; @@ -6,6 +7,8 @@ using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; #pragma warning disable CS0618 @@ -61,5 +64,70 @@ namespace ComputeSharp.Tests throw e.InnerException; } } + + [CombinatorialTestMethod] + [AllDevices] + public void SampleFromSourceTexture_ComputeShader(Device device) + { + _ = device.Get(); + + string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + + using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); + + using ReadOnlyTexture2D source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); + + device.Get().For(sampled.Width, sampled.Height, new SamplingComputeShader(source, destination)); + + using var processed = destination.ToImage(); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f); + } + + [AutoConstructor] + public readonly partial struct SamplingComputeShader : IComputeShader + { + public readonly IReadOnlyTexture2D source; + public readonly IReadWriteTexture2D destination; + + public void Execute() + { + destination[ThreadIds.XY] = source[ThreadIds.Normalized.XY]; + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void SampleFromSourceTexture_PixelShader(Device device) + { + _ = device.Get(); + + string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + + using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); + + using ReadOnlyTexture2D source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); + + device.Get().ForEach(destination, new SamplingPixelShader(source)); + + using var processed = destination.ToImage(); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f); + } + + [AutoConstructor] + public readonly partial struct SamplingPixelShader : IPixelShader + { + public readonly IReadOnlyTexture2D texture; + + public Float4 Execute() + { + return texture[ThreadIds.Normalized.XY]; + } + } } }