From 5fafd3d9acfc0f471a9476e9f6b724eb4c265f94 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 02:50:53 +0100 Subject: [PATCH] Added dispatch unit tests to resource types --- tests/ComputeSharp.Tests/BufferTests.cs | 83 +++++++++++++++++++++- tests/ComputeSharp.Tests/Texture2DTests.cs | 58 ++++++++++++++- tests/ComputeSharp.Tests/Texture3DTests.cs | 58 ++++++++++++++- 3 files changed, 196 insertions(+), 3 deletions(-) diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 280c5c24..6731b5fa 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Buffer")] - public class BufferTests + public partial class BufferTests { [TestMethod] [DataRow(typeof(ConstantBuffer<>))] @@ -201,5 +201,86 @@ namespace ComputeSharp.Tests buffer.GetData(result, destinationOffset, bufferOffset, count); } + + [TestMethod] + public void Dispatch_ConstantBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ConstantBuffer source = Gpu.Default.AllocateConstantBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ConstantBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ConstantBufferKernel : IComputeShader + { + public readonly ConstantBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } + + [TestMethod] + public void Dispatch_ReadOnlyBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ReadOnlyBuffer source = Gpu.Default.AllocateReadOnlyBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ReadOnlyBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyBufferKernel : IComputeShader + { + public readonly ReadOnlyBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ReadWriteBuffer source = Gpu.Default.AllocateReadWriteBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ReadWriteBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteBufferKernel : IComputeShader + { + public readonly ReadWriteBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } } } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index 08a79c1c..73e37112 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Texture2D")] - public class Texture2DTests + public partial class Texture2DTests { [TestMethod] [DataRow(typeof(ReadOnlyTexture2D<>))] @@ -137,5 +137,61 @@ namespace ComputeSharp.Tests texture.GetData(result, x, y, width, height); } + + [TestMethod] + public void Dispatch_ReadOnlyTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32).ToArray(); + + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(data, 32, 32); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Width, source.Height, new ReadOnlyTexture2DKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyTexture2DKernel : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y)]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32).ToArray(); + + using ReadWriteTexture2D source = Gpu.Default.AllocateReadWriteTexture2D(data, 32, 32); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(32, 32); + + Gpu.Default.For(source.Width, source.Height, new ReadWriteTexture2DKernel(source, destination)); + + int[] result = new int[data.Length]; + + destination.GetData(result); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteTexture2DKernel : IComputeShader + { + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[new((uint)ids.X, (uint)ids.Y)] = source[new((uint)ids.X, (uint)ids.Y)]; + } + } } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index a6764388..43145dd4 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Texture3D")] - public class Texture3DTests + public partial class Texture3DTests { [TestMethod] [DataRow(typeof(ReadOnlyTexture3D<>))] @@ -171,5 +171,61 @@ namespace ComputeSharp.Tests texture.GetData(result, x, y, z, width, height, depth); } + + [TestMethod] + public void Dispatch_ReadOnlyTexture3D() + { + int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); + + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(data, 32, 32, 3); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadOnlyTexture3DKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyTexture3DKernel : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); + + using ReadWriteTexture3D source = Gpu.Default.AllocateReadWriteTexture3D(data, 32, 32, 3); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(32, 32, 3); + + Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadWriteTexture3DKernel(source, destination)); + + int[] result = new int[data.Length]; + + destination.GetData(result); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteTexture3DKernel : IComputeShader + { + public readonly ReadWriteTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + } + } } }