using System; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using CommunityToolkit.HighPerformance; 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; #pragma warning disable IDE0047 namespace ComputeSharp.Tests; [TestClass] [TestCategory("ComputeContext")] public partial class ComputeContextTests { [CombinatorialTestMethod] [AllDevices] [Data(typeof(int))] [Data(typeof(Int2))] [Data(typeof(Int3))] [Data(typeof(Int4))] [Data(typeof(uint))] [Data(typeof(UInt2))] [Data(typeof(UInt3))] [Data(typeof(UInt4))] [Data(typeof(float))] [Data(typeof(Float2))] [Data(typeof(Float3))] [Data(typeof(Float4))] [Data(typeof(Bgra32))] [Data(typeof(Rgba32))] [Data(typeof(Rgba64))] [Data(typeof(R8))] [Data(typeof(R16))] [Data(typeof(Rg16))] [Data(typeof(Rg32))] public void Clear_ReadWriteBuffer(Device device, Type type) { static void Test(Device device) where T : unmanaged { T[] array = new T[128]; new Random().NextBytes(array.AsSpan().AsBytes()); using ReadWriteBuffer texture = device.Get().AllocateReadWriteBuffer(array); 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, type, device); } [CombinatorialTestMethod] [AllDevices] [Data(typeof(int))] [Data(typeof(Int2))] [Data(typeof(Int3))] [Data(typeof(Int4))] [Data(typeof(uint))] [Data(typeof(UInt2))] [Data(typeof(UInt3))] [Data(typeof(UInt4))] [Data(typeof(float))] [Data(typeof(Float2))] [Data(typeof(Float3))] [Data(typeof(Float4))] [Data(typeof(Bgra32))] [Data(typeof(Rgba32))] [Data(typeof(Rgba64))] [Data(typeof(R8))] [Data(typeof(R16))] [Data(typeof(Rg16))] [Data(typeof(Rg32))] public void Clear_ReadWriteTexture2D(Device device, Type type) { static void Test(Device device) where T : 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, type, device); } [CombinatorialTestMethod] [AllDevices] [Data(typeof(int))] [Data(typeof(Int2))] [Data(typeof(Int3))] [Data(typeof(Int4))] [Data(typeof(uint))] [Data(typeof(UInt2))] [Data(typeof(UInt3))] [Data(typeof(UInt4))] [Data(typeof(float))] [Data(typeof(Float2))] [Data(typeof(Float3))] [Data(typeof(Float4))] [Data(typeof(Bgra32))] [Data(typeof(Rgba32))] [Data(typeof(Rgba64))] [Data(typeof(R8))] [Data(typeof(R16))] [Data(typeof(Rg16))] [Data(typeof(Rg32))] public void Clear_ReadWriteTexture3D(Device device, Type type) { static void Test(Device device) where T : unmanaged { if (!device.Get().IsReadWriteTexture3DSupportedForType()) { 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, 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, IPixel 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, IPixel 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, IPixel 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((IReadWriteNormalizedTexture2D)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, IPixel 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((IReadWriteNormalizedTexture3D)texture); } texture.CopyTo(array); foreach (byte value in array.AsSpan().AsBytes()) { Assert.AreEqual(value, 0); } } 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((IReadWriteNormalizedTexture2D)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((IReadWriteNormalizedTexture3D)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) { int[] array = Enumerable.Range(0, 1024).ToArray(); using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); using (ComputeContext context = device.Get().CreateComputeContext()) { context.For(512, new OffsetComputeShader(buffer, 0)); context.For(64, new OffsetComputeShader(buffer, 512)); context.For(299, new OffsetComputeShader(buffer, 576)); context.For(149, new OffsetComputeShader(buffer, 875)); } int[] result = buffer.ToArray(); foreach (ref int x in array.AsSpan()) { x *= 2; } CollectionAssert.AreEqual(array, result); } [CombinatorialTestMethod] [AllDevices] public void ForEach_Batched(Device device) { using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); using (ComputeContext context = device.Get().CreateComputeContext()) { context.ForEach(texture, default(ClearPixelShader)); context.Barrier(texture); context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); } Rgba32[,] result = texture.ToArray(); foreach (Rgba32 pixel in result) { Assert.AreEqual(pixel.R, 56); Assert.AreEqual(pixel.G, 112); Assert.AreEqual(pixel.B, 168); Assert.AreEqual(pixel.A, 224); } } [CombinatorialTestMethod] [AllDevices] public void ForAndForEach_Batched(Device device) { int[] array = Enumerable.Range(0, 1024).ToArray(); using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); using (ComputeContext context = device.Get().CreateComputeContext()) { context.For(512, new OffsetComputeShader(buffer, 0)); context.For(64, new OffsetComputeShader(buffer, 512)); context.ForEach(texture, default(ClearPixelShader)); context.For(299, new OffsetComputeShader(buffer, 576)); context.Barrier(texture); context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); context.For(149, new OffsetComputeShader(buffer, 875)); } // Buffer test { int[] result = buffer.ToArray(); foreach (ref int x in array.AsSpan()) { x *= 2; } CollectionAssert.AreEqual(array, result); } // Texture test { Rgba32[,] result = texture.ToArray(); foreach (Rgba32 pixel in result) { Assert.AreEqual(pixel.R, 56); Assert.AreEqual(pixel.G, 112); Assert.AreEqual(pixel.B, 168); Assert.AreEqual(pixel.A, 224); } } } [CombinatorialTestMethod] [AllDevices] public void ForAndForEach_Batched_Parallel_WithParallelFor(Device device) { void Test(Device device) { for (int i = 0; i < 20; i++) { ForAndForEach_Batched(device); } } _ = device.Get(); _ = Parallel.For(0, 64, _ => Test(device)); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Batched_Parallel_WithTaskRun(Device device) { void Test(Device device) { for (int i = 0; i < 20; i++) { ForAndForEach_Batched(device); } } _ = device.Get(); await Task.Run(() => Test(device)); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched(Device device) { int[] array = Enumerable.Range(0, 1024).ToArray(); using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); await using (ComputeContext context = device.Get().CreateComputeContext()) { context.For(512, new OffsetComputeShader(buffer, 0)); context.For(64, new OffsetComputeShader(buffer, 512)); context.ForEach(texture, default(ClearPixelShader)); context.For(299, new OffsetComputeShader(buffer, 576)); context.Barrier(texture); context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); context.For(149, new OffsetComputeShader(buffer, 875)); } void Validate() { // Buffer test { int[] result = buffer.ToArray(); foreach (ref int x in array.AsSpan()) { x *= 2; } CollectionAssert.AreEqual(array, result); } // Texture test { Rgba32[,] result = texture.ToArray(); foreach (Rgba32 pixel in result) { Assert.AreEqual(pixel.R, 56); Assert.AreEqual(pixel.G, 112); Assert.AreEqual(pixel.B, 168); Assert.AreEqual(pixel.A, 224); } } } Validate(); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_WithTaskRun(Device device) { _ = device.Get(); await Task.Run(() => ForAndForEach_Async_Batched(device)); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 20; i++) { await ForAndForEach_Async_Batched(device); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_WithConfigureAwait(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 20; i++) { await ForAndForEach_Async_Batched(device).ConfigureAwait(false); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_WithTaskRun(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 20; i++) { await ForAndForEach_Async_Batched(device); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => Task.Run(() => TestAsync(device)))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_WithConfigureAwaitAndTaskRun(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 20; i++) { await ForAndForEach_Async_Batched(device).ConfigureAwait(false); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => Task.Run(() => TestAsync(device)))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_Multiple(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 10; i++) { Task task1 = ForAndForEach_Async_Batched(device); Task task2 = ForAndForEach_Async_Batched(device); Task task3 = ForAndForEach_Async_Batched(device); await Task.WhenAll(task1, task2, task3); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_Multiple_WithConfigureAwait(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 10; i++) { Task task1 = ForAndForEach_Async_Batched(device); Task task2 = ForAndForEach_Async_Batched(device); Task task3 = ForAndForEach_Async_Batched(device); await Task.WhenAll(task1, task2, task3).ConfigureAwait(false); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_Multiple_WithTaskRun(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 10; i++) { Task task1 = ForAndForEach_Async_Batched(device); Task task2 = ForAndForEach_Async_Batched(device); Task task3 = ForAndForEach_Async_Batched(device); await Task.WhenAll(task1, task2, task3); } } _ = device.Get(); await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => Task.Run(() => TestAsync(device)))); } [CombinatorialTestMethod] [AllDevices] public async Task ForAndForEach_Async_Batched_Parallel_Multiple_WithConfigureAwaitAndTaskRun(Device device) { async Task TestAsync(Device device) { for (int i = 0; i < 10; i++) { Task task1 = ForAndForEach_Async_Batched(device); Task task2 = ForAndForEach_Async_Batched(device); Task task3 = ForAndForEach_Async_Batched(device); await Task.WhenAll(task1, task2, task3).ConfigureAwait(false); } } _ = device.Get(); 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 Image sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); using ReadOnlyTexture2D source = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); using ReadWriteTexture2D sourceAsFloat4 = device.Get().AllocateReadWriteTexture2D(source.Width, source.Height); using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); using (ComputeContext context = device.Get().CreateComputeContext()) { context.For(source.Width, source.Height, new ConvertToNonNormalized2DShader(source, sourceAsFloat4)); context.Transition(sourceAsFloat4, ResourceState.ReadOnly); context.ForEach(destination, new LinearSampling2DPixelShader(sourceAsFloat4.AsReadOnly())); context.Transition(sourceAsFloat4, ResourceState.ReadWrite); } using Image processed = destination.ToImage(); TolerantImageComparer.AssertEqual(sampled, processed, 0.00000174f); } [CombinatorialTestMethod] [AllDevices] public void Transition_ReadWriteTexture2D_Normalized(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 Image 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 (ComputeContext context = device.Get().CreateComputeContext()) { context.Transition(source, ResourceState.ReadOnly); context.ForEach(destination, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); context.Transition(source, ResourceState.ReadWrite); } using Image processed = destination.ToImage(); TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } [CombinatorialTestMethod] [AllDevices] public void Transition_ReadWriteTexture2D_Normalized_WithWriteToo(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 Image sampled1 = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); using Image sampled2 = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024SamplingInverted.png")); using ReadOnlyTexture2D image = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); using ReadWriteTexture2D source = device.Get().AllocateReadWriteTexture2D(image.Width, image.Height); using ReadWriteTexture2D destination1 = device.Get().AllocateReadWriteTexture2D(sampled1.Width, sampled1.Height); using ReadWriteTexture2D destination2 = device.Get().AllocateReadWriteTexture2D(sampled1.Width, sampled1.Height); using (ComputeContext context = device.Get().CreateComputeContext()) { // Dispatch before any transitions, and UAV barrier test context.ForEach(source); context.Barrier(source); // Dispatch following a UAV barrier context.ForEach(source, new CopyToOutputPixelShader(image)); // Transition to readonly and first use as readonly resource context.Transition(source, ResourceState.ReadOnly); context.ForEach(destination1, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); // Transition back and write to it context.Transition(source, ResourceState.ReadWrite); context.ForEach(source, new InvertPixelShader(source)); // Transition once again to readonly and final use as readonly resource context.Transition(source, ResourceState.ReadOnly); context.ForEach(destination2, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); } using Image processed1 = destination1.ToImage(); using Image processed2 = destination2.ToImage(); TolerantImageComparer.AssertEqual(sampled1, processed1, 0.0000017f); TolerantImageComparer.AssertEqual(sampled2, processed2, 0.0000028f); } [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, "CityAfter1024x1024SamplingFrom1920x1080.png"); IImageInfo imageInfo = Image.Identify(originalPath); using Image sampled = Image.Load(Path.Combine(assetsPath, sampledPath)); using ReadWriteTexture3D sourceAsFloat4 = 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++) { TextureView2D layer = upload3D.View.GetDepthView(z); for (int y = 0; y < layer.Height; y++) { for (int x = 0; x < layer.Width; x++) { layer[x, y] = upload2D.View[x, y].ToPixel(); } } } upload3D.CopyTo(sourceAsFloat4); } using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(sampled.Width, sampled.Height, 2); using (ComputeContext context = device.Get().CreateComputeContext()) { context.Transition(sourceAsFloat4, ResourceState.ReadOnly); context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(sourceAsFloat4.AsReadOnly(), destination)); context.Transition(sourceAsFloat4, ResourceState.ReadWrite); } for (int z = 0; z < destination.Depth; z++) { using Image processed = destination.ToImage(depth: z); TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } } [CombinatorialTestMethod] [AllDevices] public void Transition_ReadWriteTexture3D_Normalized(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, "CityAfter1024x1024SamplingFrom1920x1080.png"); IImageInfo imageInfo = Image.Identify(originalPath); using Image 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 (ComputeContext context = device.Get().CreateComputeContext()) { context.Transition(source, ResourceState.ReadOnly); context.For(destination.Width, destination.Height, destination.Depth, new LinearSamplingFromNormalized3DComputeShader(source.AsReadOnly(), destination)); context.Transition(source, ResourceState.ReadWrite); } for (int z = 0; z < destination.Depth; z++) { using Image processed = destination.ToImage(depth: z); TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } } [CombinatorialTestMethod] [AllDevices] public void TransitionAndWrites_ReadWriteTexture2D_Normalized(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 Image sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024SamplingAndDashing.png")); using ReadWriteTexture2D source = device.Get().LoadReadWriteTexture2D(Path.Combine(imagingPath, "city.jpg")); using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); using (ComputeContext context = device.Get().CreateComputeContext()) { context.For(source.Width, source.Height, new Dotted2DPixelShader(source)); context.Transition(source, ResourceState.ReadOnly); context.ForEach(destination, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); context.Transition(source, ResourceState.ReadWrite); context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } using Image processed = destination.ToImage(); TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); Rgba32[,] clear = source.ToArray(); foreach (Rgba32 value in clear) { Assert.AreEqual(value, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } } [CombinatorialTestMethod] [AllDevices] public void TransitionAndWrites_ReadWriteTexture3D_Normalized(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, "CityAfter1024x1024SamplingFrom1920x1080AndDashing.png"); IImageInfo imageInfo = Image.Identify(originalPath); using Image 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 (ComputeContext context = device.Get().CreateComputeContext()) { context.For(source.Width, source.Height, source.Depth, new Dotted3DPixelShader(source)); context.Transition(source, ResourceState.ReadOnly); context.For(destination.Width, destination.Height, destination.Depth, new LinearSamplingFromNormalized3DComputeShader(source.AsReadOnly(), destination)); context.Transition(source, ResourceState.ReadWrite); context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } for (int z = 0; z < destination.Depth; z++) { using Image processed = destination.ToImage(depth: z); TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); } Rgba32[,,] clear = source.ToArray(); foreach (Rgba32 value in clear) { Assert.AreEqual(value, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void Barrier_WithoutPreviousDispatches_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); using (ComputeContext context = device.Get().CreateComputeContext()) { context.Barrier(buffer); } Assert.Fail(); } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void For_AfterDispose_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(128); ComputeContext context = device.Get().CreateComputeContext(); context.Dispose(); context.For(512, new OffsetComputeShader(buffer, 0)); Assert.Fail(); } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public async Task For_AfterDisposeAsync_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(128); ComputeContext context = device.Get().CreateComputeContext(); await context.DisposeAsync(); context.For(512, new OffsetComputeShader(buffer, 0)); Assert.Fail(); } [TestMethod] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_GetGraphicsDevice_ThrowsException() { _ = default(ComputeContext).GraphicsDevice; } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Barrier_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); using ComputeContext context = default; context.Barrier(buffer); Assert.Fail(); } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Clear_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); using ComputeContext context = default; context.Clear(buffer); Assert.Fail(); } [TestMethod] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_For_ThrowsException() { using ComputeContext context = default; context.For(32, 1, 1, default(OffsetComputeShader)); Assert.Fail(); } [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_ForEach_ThrowsException(Device device) { using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); using ComputeContext context = default; context.ForEach(texture, default(ColorPixelShader)); Assert.Fail(); } [TestMethod] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Dispose_ThrowsException() { using ComputeContext context = default; } [TestMethod] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public async Task DefaultContext_DisposeAsync_ThrowsException() { await using ComputeContext context = default; } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.X)] internal readonly partial struct OffsetComputeShader : IComputeShader { public readonly ReadWriteBuffer buffer; public readonly int offset; /// public void Execute() { this.buffer[this.offset + ThreadIds.X] *= 2; } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct ClearPixelShader : IPixelShader { /// public float4 Execute() { return default; } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct ColorPixelShader : IPixelShader { public readonly float4 color; /// public float4 Execute() { return this.color; } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct LinearSampling2DPixelShader : IPixelShader { public readonly IReadOnlyTexture2D source; /// public float4 Execute() { return this.source.Sample(ThreadIds.Normalized.XY); } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XYZ)] internal readonly partial struct LinearSampling3DComputeShader : IComputeShader { public readonly IReadOnlyTexture3D source; public readonly IReadWriteNormalizedTexture3D destination; /// public void Execute() { float3 xyz = ThreadIds.Normalized.XYZ; // The source has a depth of 3, but the destination has a depth of 2 xyz.Z = Hlsl.Lerp(0, 0.5f, xyz.Z); this.destination[ThreadIds.XYZ] = this.source.Sample(xyz); } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct LinearSamplingFromNormalized2DPixelShader : IPixelShader { public readonly IReadOnlyNormalizedTexture2D source; /// public float4 Execute() { return this.source.Sample(ThreadIds.Normalized.XY); } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct CopyToOutputPixelShader : IPixelShader { public readonly IReadOnlyNormalizedTexture2D source; /// public float4 Execute() { return this.source[ThreadIds.XY]; } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct InvertPixelShader : IPixelShader { public readonly IReadWriteNormalizedTexture2D source; /// public float4 Execute() { return new(1.0f - this.source[ThreadIds.XY].XYZ, this.source[ThreadIds.XY].W); } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XYZ)] internal readonly partial struct LinearSamplingFromNormalized3DComputeShader : IComputeShader { public readonly IReadOnlyNormalizedTexture3D source; public readonly IReadWriteNormalizedTexture3D destination; /// public void Execute() { float3 xyz = ThreadIds.Normalized.XYZ; // See comment in LinearSampling3DComputeShader xyz.Z = Hlsl.Lerp(0, 0.5f, xyz.Z); this.destination[ThreadIds.XYZ] = this.source.Sample(xyz); } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct Dotted2DPixelShader : IComputeShader { public readonly IReadWriteNormalizedTexture2D source; /// public void Execute() { bool isHorizontal192CellEven = (ThreadIds.X / 192) % 2 == 0; bool isVertical128CellEven = (ThreadIds.Y / 128) % 2 == 0; if (isHorizontal192CellEven != isVertical128CellEven) { if ((ThreadIds.Normalized.X <= 0.5f && ThreadIds.Normalized.Y <= 0.5) || (ThreadIds.Normalized.X > 0.5f && ThreadIds.Normalized.Y > 0.5)) { this.source[ThreadIds.XY] = float4.UnitW; } else { this.source[ThreadIds.XY] = float4.Zero; } } } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XYZ)] internal readonly partial struct Dotted3DPixelShader : IComputeShader { public readonly IReadWriteNormalizedTexture3D source; /// public void Execute() { bool isHorizontal192CellEven = (ThreadIds.X / 192) % 2 == 0; bool isVertical128CellEven = (ThreadIds.Y / 128) % 2 == 0; if (isHorizontal192CellEven != isVertical128CellEven) { if ((ThreadIds.Normalized.X <= 0.5f && ThreadIds.Normalized.Y <= 0.5) || (ThreadIds.Normalized.X > 0.5f && ThreadIds.Normalized.Y > 0.5)) { this.source[ThreadIds.XYZ] = float4.UnitW; } else { this.source[ThreadIds.XYZ] = float4.Zero; } } } } [AutoConstructor] [EmbeddedBytecode(DispatchAxis.XY)] internal readonly partial struct ConvertToNonNormalized2DShader : IComputeShader { public readonly IReadOnlyNormalizedTexture2D source; public readonly ReadWriteTexture2D destination; /// public void Execute() { this.destination[ThreadIds.XY] = this.source[ThreadIds.XY]; } } }