diff --git a/tests/ComputeSharp.Tests/Assets/CityWith1920x1280Resizing.png b/tests/ComputeSharp.Tests/Assets/CityWith1920x1280Resizing.png new file mode 100644 index 00000000..aa317531 Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityWith1920x1280Resizing.png differ diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 28150834..048893d4 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -9,7 +9,6 @@ using ComputeSharp.Tests.Helpers; using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Processing; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.Tests; @@ -905,7 +904,7 @@ public partial class ComputeContextTests using (var context = device.Get().CreateComputeContext()) { context.Transition(source, ResourceState.ReadOnly); - context.ForEach(destination, new LinearSamplingPixelShader(source.AsReadOnly())); + context.ForEach(destination, new LinearSampling2DPixelShader(source.AsReadOnly())); context.Transition(source, ResourceState.ReadWrite); } @@ -914,6 +913,50 @@ public partial class ComputeContextTests ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } + [CombinatorialTestMethod] + [AllDevices] + public void Transition_ReadWriteTexture3D(Device device) + { + _ = device.Get(); + + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + string originalPath = Path.Combine(assetsPath, "CityWith1920x1280Resizing.png"); + string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"); + + var imageInfo = Image.Identify(originalPath); + + using var sampled = Image.Load(Path.Combine(assetsPath, sampledPath)); + + using ReadWriteTexture3D source = device.Get().AllocateReadWriteTexture3D(imageInfo.Width, imageInfo.Height, 3); + + using (UploadTexture2D upload2D = device.Get().LoadUploadTexture2D(originalPath)) + using (UploadTexture3D upload3D = device.Get().AllocateUploadTexture3D(upload2D.Width, upload2D.Height, 2)) + { + for (int z = 0; z < upload3D.Depth; z++) + { + upload2D.View.CopyTo(upload3D.View.GetDepthView(z)); + } + + upload3D.CopyTo(source); + } + + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(sampled.Width, sampled.Height, 2); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(source, ResourceState.ReadOnly); + context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(source.AsReadOnly(), destination)); + context.Transition(source, ResourceState.ReadWrite); + } + + for (int z = 0; z < destination.Depth; z++) + { + using var processed = destination.ToImage(depth: z); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + } + } + [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] @@ -1070,7 +1113,7 @@ public partial class ComputeContextTests } [AutoConstructor] - internal readonly partial struct LinearSamplingPixelShader : IPixelShader + internal readonly partial struct LinearSampling2DPixelShader : IPixelShader { public readonly IReadOnlyTexture2D source; @@ -1080,4 +1123,17 @@ public partial class ComputeContextTests return source[ThreadIds.Normalized.XY]; } } + + [AutoConstructor] + internal readonly partial struct LinearSampling3DComputeShader : IComputeShader + { + public readonly IReadOnlyTexture3D source; + public readonly IReadWriteTexture3D destination; + + /// + public void Execute() + { + destination[ThreadIds.XYZ] = source[ThreadIds.Normalized.XYZ]; + } + } } diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index e8d85f91..c73f109d 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -48,7 +48,10 @@ - + + PreserveNewest + + PreserveNewest diff --git a/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs index f05cc40f..785bc763 100644 --- a/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/ImagingExtensions.cs @@ -36,4 +36,29 @@ public static class ImagingExtensions return image; } + + /// + /// Creates a new instance with the specified texture data. + /// + /// The input pixel format used in the texture. + /// The target pixel format for the returned image. + /// The source instance to read data from. + /// The depth layer to read the image from. + /// An image with the data from the input texture at a specified depth layer. + public static unsafe Image ToImage(this Texture3D texture, int depth) + where TFrom : unmanaged + where TTo : unmanaged, IPixel + { + Guard.IsEqualTo(sizeof(TTo), sizeof(TFrom), nameof(TTo)); + + Image image = new(texture.Width, texture.Height); + + Assert.IsTrue(image.TryGetSinglePixelSpan(out Span span)); + + Span pixels = MemoryMarshal.Cast(span); + + texture.CopyTo(pixels, 0, 0, depth, texture.Width, texture.Height, 1); + + return image; + } }