From eef8e418997e8c148f74c5960006ddb2343a3cb7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 15:49:14 +0100 Subject: [PATCH] Added Texture2D unit tests --- tests/ComputeSharp.Tests/BufferTests.cs | 21 ++- .../ComputeSharp.Tests.csproj | 1 + .../Extensions/GraphicsDeviceExtensions.cs | 43 ++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 141 ++++++++++++++++++ 4 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Texture2DTests.cs diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 7810777e..280c5c24 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -7,14 +7,14 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests { [TestClass] - [TestCategory("Buffers")] + [TestCategory("Buffer")] public class BufferTests { [TestMethod] [DataRow(typeof(ConstantBuffer<>))] [DataRow(typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_Uninitialized(Type bufferType) + public void Allocate_Uninitialized_Ok(Type bufferType) { using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); @@ -23,11 +23,24 @@ namespace ComputeSharp.Tests Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); } + [TestMethod] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type bufferType, int length) + { + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, length); + } + [TestMethod] [DataRow(typeof(ConstantBuffer<>))] [DataRow(typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_FromArray(Type bufferType) + public void Allocate_FromArray(Type bufferType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); @@ -54,7 +67,7 @@ namespace ComputeSharp.Tests [DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_FromBuffer(Type sourceType, Type destinationType) + public void Allocate_FromBuffer(Type sourceType, Type destinationType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 385ffb57..9e45c3d9 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index 48aa0554..9ffb2d25 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -31,6 +31,27 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// A instance of the requested size. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(width, height), + _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// @@ -52,6 +73,28 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of buffer to allocate. + /// The data to load on the texture. + /// The width of the texture to create. + /// The height of the texture to create. + /// A instance of the requested type. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, T[] data, int width, int height) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(data, width, height), + _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(data, width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs new file mode 100644 index 00000000..08a79c1c --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -0,0 +1,141 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.Toolkit.HighPerformance.Memory; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("Texture2D")] + public class Texture2DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + public void Allocate_Uninitialized_Ok(Type textureType) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 128, -14253)] + [DataRow(typeof(ReadOnlyTexture2D<>), 128, -1)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, -4314)] + [DataRow(typeof(ReadOnlyTexture2D<>), -14, -53)] + [DataRow(typeof(ReadWriteTexture2D<>), 128, -14253)] + [DataRow(typeof(ReadWriteTexture2D<>), 128, -1)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, -4314)] + [DataRow(typeof(ReadWriteTexture2D<>), -14, -53)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type textureType, int width, int height) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, width, height); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + public void Allocate_FromArray(Type textureType) + { + float[] data = Enumerable.Range(0, 128 * 128).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, data, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + float[,] result = texture.GetData(); + + Assert.IsNotNull(result); + Assert.AreEqual(data.Length, result.Length); + Assert.IsTrue(data.SequenceEqual(result.Cast())); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Type textureType) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 10, 10); + + texture.Dispose(); + + _ = texture.GetData(); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 0, 64, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 14, 64, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 14, 0, 50, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 10, 10, 54, 54)] + [DataRow(typeof(ReadOnlyTexture2D<>), 20, 20, 32, 27)] + [DataRow(typeof(ReadOnlyTexture2D<>), 60, 0, 4, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 60, 64, 4)] + [DataRow(typeof(ReadOnlyTexture2D<>), 63, 2, 1, 60)] + [DataRow(typeof(ReadOnlyTexture2D<>), 2, 63, 60, 1)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 0, 64, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 14, 64, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 14, 0, 50, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 10, 10, 54, 54)] + [DataRow(typeof(ReadWriteTexture2D<>), 20, 20, 32, 27)] + [DataRow(typeof(ReadWriteTexture2D<>), 60, 0, 4, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 60, 64, 4)] + [DataRow(typeof(ReadWriteTexture2D<>), 63, 2, 1, 60)] + [DataRow(typeof(ReadWriteTexture2D<>), 2, 63, 60, 1)] + public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int width, int height) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + + float[] result = new float[width * height]; + + texture.GetData(result, x, y, width, height); + + Span2D + expected = new Span2D(array, 64, 64).Slice(y, x, height, width), + data = new(result, height, width); + + CollectionAssert.AreEqual(expected.ToArray(), data.ToArray()); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, -1, 50, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), -1, 0, 50, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, -1, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, 20, -1)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 20, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 60, 20)] + [DataRow(typeof(ReadOnlyTexture2D<>), 80, 20, 40, 20)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 80, 40, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, -1, 50, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), -1, 0, 50, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, -1, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, 20, -1)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 20, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 60, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 80, 20, 40, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 80, 40, 20)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int width, int height) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + + float[] result = new float[4096]; + + texture.GetData(result, x, y, width, height); + } + } +}