From 40d3cd0182ea76aae24bc56c027adf3619830607 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 17 Jan 2022 00:19:44 +0100 Subject: [PATCH] Add unit tests for ComputeContext.Transition API Also test ReadWriteTexture[2D|3D].AsReadOnly() --- .../ComputeSharp.Tests/ComputeContextTests.cs | 43 +++++++++++ .../Texture2DTests.Pixels.cs | 73 +++++++++++++++++-- .../Texture3DTests.Pixels.cs | 65 ++++++++++++++++- 3 files changed, 169 insertions(+), 12 deletions(-) diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 86b14b8c..28150834 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,11 +1,16 @@ using System; +using System.IO; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; 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; @@ -883,6 +888,32 @@ public partial class ComputeContextTests await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => Task.Run(() => TestAsync(device)))); } + [CombinatorialTestMethod] + [AllDevices] + public void Transition_ReadWriteTexture2D(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 ReadWriteTexture2D source = device.Get().LoadReadWriteTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(source, ResourceState.ReadOnly); + context.ForEach(destination, new LinearSamplingPixelShader(source.AsReadOnly())); + context.Transition(source, ResourceState.ReadWrite); + } + + using var processed = destination.ToImage(); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + } + [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] @@ -1037,4 +1068,16 @@ public partial class ComputeContextTests return color; } } + + [AutoConstructor] + internal readonly partial struct LinearSamplingPixelShader : IPixelShader + { + public readonly IReadOnlyTexture2D source; + + /// + public float4 Execute() + { + return source[ThreadIds.Normalized.XY]; + } + } } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index e7c7624c..b779e9db 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -4,6 +4,7 @@ using System.Reflection; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; @@ -46,14 +47,7 @@ public partial class Texture2DTests } } - try - { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); - } - catch (TargetInvocationException e) when (e.InnerException is not null) - { - throw e.InnerException; - } + TestHelper.Run(Test, t, tPixel, device, textureType); } [CombinatorialTestMethod] @@ -120,4 +114,67 @@ public partial class Texture2DTests return texture[ThreadIds.Normalized.XY]; } } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(float2))] + [Data(typeof(Rg32), typeof(float2))] + [Data(typeof(Rgba32), typeof(float4))] + [Data(typeof(Rgba64), typeof(float4))] + public void AsReadOnly_MultipleCalls_SameInstance(Device device, Type t, Type tPixel) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(64, 64); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(texture, ResourceState.ReadOnly); + + IReadOnlyTexture2D wrapper1 = texture.AsReadOnly(); + + Assert.IsNotNull(wrapper1); + + IReadOnlyTexture2D wrapper2 = texture.AsReadOnly(); + + Assert.IsNotNull(wrapper2); + Assert.AreSame(wrapper1, wrapper2); + + context.Transition(texture, ResourceState.ReadWrite); + } + } + + TestHelper.Run(Test, t, tPixel, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(float2))] + [Data(typeof(Rg32), typeof(float2))] + [Data(typeof(Rgba32), typeof(float4))] + [Data(typeof(Rgba64), typeof(float4))] + [ExpectedException(typeof(InvalidOperationException))] + public void AsReadOnly_InvalidState_ThrowInvalidOperationException(Device device, Type t, Type tPixel) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(64, 64); + + _ = texture.AsReadOnly(); + + Assert.Fail(); + } + + TestHelper.Run(Test, t, tPixel, device); + } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs index ec8f9a64..7a75234b 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -3,6 +3,7 @@ using System.Reflection; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests; @@ -44,13 +45,69 @@ public partial class Texture3DTests } } - try + TestHelper.Run(Test, t, tPixel, device, textureType); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(float2))] + [Data(typeof(Rg32), typeof(float2))] + [Data(typeof(Rgba32), typeof(float4))] + [Data(typeof(Rgba64), typeof(float4))] + public void AsReadOnly_MultipleCalls_SameInstance(Device device, Type t, Type tPixel) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(32, 32, 2); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(texture, ResourceState.ReadOnly); + + IReadOnlyTexture3D wrapper1 = texture.AsReadOnly(); + + Assert.IsNotNull(wrapper1); + + IReadOnlyTexture3D wrapper2 = texture.AsReadOnly(); + + Assert.IsNotNull(wrapper2); + Assert.AreSame(wrapper1, wrapper2); + + context.Transition(texture, ResourceState.ReadWrite); + } } - catch (TargetInvocationException e) when (e.InnerException is not null) + + TestHelper.Run(Test, t, tPixel, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(float2))] + [Data(typeof(Rg32), typeof(float2))] + [Data(typeof(Rgba32), typeof(float4))] + [Data(typeof(Rgba64), typeof(float4))] + [ExpectedException(typeof(InvalidOperationException))] + public void AsReadOnly_InvalidState_ThrowInvalidOperationException(Device device, Type t, Type tPixel) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged { - throw e.InnerException; + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(32, 32, 2); + + _ = texture.AsReadOnly(); + + Assert.Fail(); } + + TestHelper.Run(Test, t, tPixel, device); } }