From 21ae8a57bc4d3885bf9d564aa4369bba9dc60843 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Nov 2021 16:18:33 +0100 Subject: [PATCH] Add debug view resource tests --- .../Resources/Debug/BufferDebugView{T}.cs | 2 +- .../Resources/Debug/Texture2DDebugView{T}.cs | 2 +- .../Resources/Debug/Texture3DDebugView{T}.cs | 2 +- .../Debug/TransferBufferDebugView{T}.cs | 2 +- .../ComputeSharp.Tests.Internals.csproj | 11 + .../DebugViewTests.cs | 321 ++++++++++++++++++ 6 files changed, 336 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests.Internals/DebugViewTests.cs diff --git a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs index 44a88976..4f123a0f 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs @@ -18,7 +18,7 @@ internal sealed class BufferDebugView { if (buffer is not null) { - var items = GC.AllocateUninitializedArray(buffer.Length); + T[] items = GC.AllocateUninitializedArray(buffer.Length); buffer.CopyTo(ref items[0], 0, buffer.Length); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs index bcfcf0d5..88020a15 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs @@ -17,7 +17,7 @@ internal sealed class Texture2DDebugView { if (texture is not null) { - var items = new T[texture.Height, texture.Width]; + T[,] items = new T[texture.Height, texture.Width]; texture.CopyTo(ref items[0, 0], items.Length, 0, 0, texture.Width, texture.Height); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs index 7d1a3591..4da600b1 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs @@ -17,7 +17,7 @@ internal sealed class Texture3DDebugView { if (texture is not null) { - var items = new T[texture.Depth, texture.Height, texture.Width]; + T[,,] items = new T[texture.Depth, texture.Height, texture.Width]; texture.CopyTo(ref items[0, 0, 0], items.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs index e90b1027..a42c8df6 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs @@ -18,7 +18,7 @@ internal sealed class TransferBufferDebugView { if (buffer is not null) { - var items = GC.AllocateUninitializedArray(buffer.Length); + T[] items = GC.AllocateUninitializedArray(buffer.Length); buffer.Span.CopyTo(items); diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 670731c9..5828f8b3 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -19,4 +19,15 @@ PrivateAssets="contentfiles;build"/> + + + + + + + + + + + diff --git a/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs b/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs new file mode 100644 index 00000000..2831ede4 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs @@ -0,0 +1,321 @@ +using System; +using System.Linq; +using ComputeSharp.Resources; +using ComputeSharp.Resources.Debug; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Internals; + +[TestClass] +[TestCategory("Buffer")] +public partial class DebugViewTests +{ + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(32)] + [Data(256)] + [Data(1024)] + public void BufferDebugView_NotNull(Device device, Type bufferType, int size) + { + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); + + using Buffer buffer = device.Get().AllocateBuffer(bufferType, data); + + BufferDebugView view = new(buffer); + + Assert.IsNotNull(view.Items); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void BufferDebugView_Null() + { + using Buffer buffer = null!; + + BufferDebugView view = new(buffer); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(32, 32)] + [Data(1024, 1024)] + [Data(667, 480)] + public void Texture2DDebugView_NotNull(Device device, Type textureType, int width, int height) + { + float[] data = Enumerable.Range(0, width * height).Select(static i => (float)i).ToArray(); + + using Texture2D texture = device.Get().AllocateTexture2D(textureType, data, width, height); + + Texture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [Data(32, 32)] + [Data(512, 512)] + [Data(667, 480)] + public void Texture2DDebugView_WithTPixel_NotNull(Device device, Type textureType, int width, int height) + { + Random random = new(42); + Rgba32[,] data = new Rgba32[height, width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[i, j] = new Rgba32((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + } + } + + using Texture2D texture = device.Get().AllocateTexture2D(textureType, width, height); + + texture.CopyFrom(data); + + Texture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void Texture2DDebugView_Null() + { + using Texture2D texture = null!; + + Texture2DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void Texture3DDebugView_NotNull(Device device, Type textureType, int width, int height, int depth) + { + float[] data = Enumerable.Range(0, width * height * depth).Select(static i => (float)i).ToArray(); + + using Texture3D texture = device.Get().AllocateTexture3D(textureType, data, width, height, depth); + + Texture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<,>))] + [Resource(typeof(ReadWriteTexture3D<,>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void Texture3DDebugView_WithTPixel_NotNull(Device device, Type textureType, int width, int height, int depth) + { + Random random = new(42); + Rgba32[,,] data = new Rgba32[depth, height, width]; + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[k, i, j] = new Rgba32((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + } + } + } + + using Texture3D texture = device.Get().AllocateTexture3D(textureType, width, height, depth); + + texture.CopyFrom(data); + + Texture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void Texture3DDebugView_Null() + { + using Texture3D texture = null!; + + Texture3DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + [Data(32)] + [Data(1024)] + [Data(667)] + public void TransferBufferDebugView_NotNull(Device device, Type bufferType, int size) + { + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); + + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, size); + + data.AsSpan().CopyTo(buffer.Span); + + TransferBufferDebugView view = new(buffer); + + Assert.IsNotNull(view.Items); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferBufferDebugView_Null() + { + using TransferBuffer buffer = null!; + + TransferBufferDebugView view = new(buffer); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + [Data(32, 32)] + [Data(1024, 1024)] + [Data(667, 480)] + public void TransferTexture2DDebugView_NotNull(Device device, Type textureType, int width, int height) + { + Random random = new(42); + float[,] data = new float[height, width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[i, j] = random.NextSingle(); + } + } + + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(textureType, width, height); + + for (int i = 0; i < height; i++) + { + Span row = texture.View.GetRowSpan(i); + + for (int j = 0; j < width; j++) + { + row[j] = data[i, j]; + } + } + + TransferTexture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferTexture2DDebugView_Null() + { + using Texture2D texture = null!; + + Texture2DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void TransferTexture3DDebugView_NotNull(Device device, Type textureType, int width, int height, int depth) + { + Random random = new(42); + float[,,] data = new float[depth, height, width]; + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[k, i, j] = random.NextSingle(); + } + } + } + + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(textureType, width, height, depth); + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + Span row = texture.View.GetRowSpan(i, k); + + for (int j = 0; j < width; j++) + { + row[j] = data[k, i, j]; + } + } + } + + TransferTexture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferTexture3DDebugView_Null() + { + using Texture3D texture = null!; + + Texture3DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } +}