diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 9e747b73..a6ba37df 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,11 +1,14 @@ using System; using System.Linq; +using ComputeSharp.__Internals; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using ComputeSharp.Tests.Helpers; using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests; [TestClass] @@ -118,6 +121,174 @@ public partial class ComputeContextTests TestHelper.Run(Test, type, device); } + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture2D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear(texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture3D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear(texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture2D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear((IReadWriteTexture2D)texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture3D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear((IReadWriteTexture3D)texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + [CombinatorialTestMethod] [AllDevices] public void For_Batched(Device device) diff --git a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs index 3601bd20..4d2a447c 100644 --- a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs +++ b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs @@ -31,4 +31,28 @@ public static class TestHelper throw e.InnerException; } } + + /// + /// Runs a generic test methods with the specified arguments. + /// + /// A instance wrapping the target method. + /// The first type argument to use to invoke . + /// The second type argument to use to invoke . + /// The arguments to use to invoke . + public static void Run(Delegate @delegate, Type type1, Type type2, params object[] arguments) + { + if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true }) + { + Assert.Fail("The input delegate must point to a generic static method"); + } + + try + { + @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type1, type2).Invoke(null, arguments); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } }