diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 4265c32f..86b14b8c 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -335,6 +335,212 @@ public partial class ComputeContextTests TestHelper.Run(Test, cpuType, gpuType, device); } + /// + /// Gets a sample pixel color of the specified type. + /// + /// The type of pixel to get. + /// A sample pixel color of the specified type. + /// Thrown when isn't valid. + private static T GetColor() + { + if (typeof(T) == typeof(Bgra32)) + { + return (T)(object)new Bgra32(0x6B, 0x6D, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(Rgba32)) + { + return (T)(object)new Rgba32(0x6B, 0x6D, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(Rgba64)) + { + return (T)(object)new Rgba64(0x6B, 0x6D05, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(R8)) + { + return (T)(object)new R8(0x6B); + } + + if (typeof(T) == typeof(R16)) + { + return (T)(object)new R16(0x6D05); + } + + if (typeof(T) == typeof(Rg16)) + { + return (T)(object)new Rg16(0x6B, 0x6D); + } + + if (typeof(T) == typeof(Rg32)) + { + return (T)(object)new Rg32(0x6B, 0x6D05); + } + + throw new ArgumentException("Invalid pixel type"); + } + + [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 Fill_ReadWriteTexture2D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill(texture, color); + } + + T[,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + 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 Fill_ReadWriteTexture3D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill(texture, color); + } + + T[,,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + 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 Fill_ReadWriteTexture2D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill((IReadWriteTexture2D)texture, color.ToPixel()); + } + + T[,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + 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 Fill_ReadWriteTexture3D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill((IReadWriteTexture3D)texture, color.ToPixel()); + } + + T[,,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + [CombinatorialTestMethod] [AllDevices] public void For_Batched(Device device)