From 53012a5667304cac33e93436b45576d6d0edfda0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Feb 2021 14:28:24 +0100 Subject: [PATCH] Bug fixes, added TransferBuffer tests --- tests/ComputeSharp.Tests/BufferTests.cs | 8 +- .../ComputeSharp.Tests.csproj | 1 + .../Extensions/GraphicsDeviceExtensions.cs | 63 +++++++++ .../ComputeSharp.Tests/TransferBufferTests.cs | 129 ++++++++++++++++++ 4 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests/TransferBufferTests.cs diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 5d91a25b..ad1ad729 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -26,10 +26,10 @@ namespace ComputeSharp.Tests [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)] + [DataRow(typeof(ReadOnlyBuffer<>), -247824)] + [DataRow(typeof(ReadOnlyBuffer<>), -1)] + [DataRow(typeof(ReadWriteBuffer<>), -247824)] + [DataRow(typeof(ReadWriteBuffer<>), -1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Allocate_Uninitialized_Fail(Type bufferType, int length) { diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index dcb51fa4..11ddfefb 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -4,6 +4,7 @@ net5.0 false 9.0 + true diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index e7e704e4..9cfc05d1 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -123,6 +123,69 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the buffer. + /// The target instance to allocate the buffer for. + /// The type of buffer to allocate. + /// The length of the buffer to create. + /// A instance of the requested size. + [Pure] + public static TransferBuffer AllocateTransferBuffer(this GraphicsDevice device, Type type, int length) + where T : unmanaged + { + return type switch + { + _ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer(length), + _ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer(length), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the buffer. + /// The target instance to allocate the buffer for. + /// The type of buffer 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 TransferTexture2D AllocateTransferTexture2D(this GraphicsDevice device, Type type, int width, int height) + where T : unmanaged + { + return type switch + { + _ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D(width, height), + _ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D(width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the buffer. + /// The target instance to allocate the buffer for. + /// The type of buffer to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// The depth of the texture to create. + /// A instance of the requested size. + [Pure] + public static TransferTexture3D AllocateTransferTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + where T : unmanaged + { + return type switch + { + _ when type == typeof(UploadTexture3D<>) => device.AllocateUploadTexture3D(width, height, depth), + _ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D(width, height, depth), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs new file mode 100644 index 00000000..2367fc40 --- /dev/null +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -0,0 +1,129 @@ +using System; +using System.Linq; +using System.Runtime.CompilerServices; +using ComputeSharp.Resources; +using ComputeSharp.Tests.Extensions; +using Microsoft.Toolkit.HighPerformance.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("TransferBuffer")] + public partial class TransferBufferTests + { + [TestMethod] + [DataRow(typeof(UploadBuffer<>))] + [DataRow(typeof(ReadBackBuffer<>))] + public unsafe void Allocate_Uninitialized_Ok(Type bufferType) + { + using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, 128); + + Assert.IsNotNull(buffer); + Assert.AreEqual(buffer.Length, 128); + Assert.AreEqual(buffer.Memory.Length, 128); + Assert.AreEqual(buffer.Span.Length, 128); + Assert.IsTrue(Unsafe.AreSame(ref buffer.Memory.Span[0], ref buffer.Span[0])); + Assert.IsTrue(Unsafe.AreSame(ref *(float*)buffer.Memory.Pin().Pointer, ref buffer.Span[0])); + Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + } + + [TestMethod] + [DataRow(typeof(UploadBuffer<>), -247824)] + [DataRow(typeof(UploadBuffer<>), -1)] + [DataRow(typeof(ReadBackBuffer<>), -247824)] + [DataRow(typeof(ReadBackBuffer<>), -1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type bufferType, int length) + { + using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, length); + } + + [TestMethod] + [DataRow(typeof(UploadBuffer<>))] + [DataRow(typeof(ReadBackBuffer<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Type bufferType) + { + using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, 128); + + buffer.Dispose(); + + _ = buffer.Span; + } + + [TestMethod] + public void Allocate_UploadBuffer_Copy_Full() + { + using UploadBuffer uploadBuffer = Gpu.Default.AllocateUploadBuffer(4096); + + new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(uploadBuffer.Length); + + uploadBuffer.CopyTo(readOnlyBuffer); + + int[] result = readOnlyBuffer.ToArray(); + + Assert.AreEqual(uploadBuffer.Length, result.Length); + Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result)); + } + + [TestMethod] + [DataRow(0, 0, 4096)] + [DataRow(128, 0, 2048)] + [DataRow(0, 128, 2048)] + [DataRow(97, 33, 512)] + public void Allocate_UploadBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count) + { + using UploadBuffer uploadBuffer = Gpu.Default.AllocateUploadBuffer(4096); + + new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(uploadBuffer.Length); + + uploadBuffer.CopyTo(readOnlyBuffer, destinationOffset, bufferOffset, count); + + int[] result = readOnlyBuffer.ToArray(destinationOffset, count); + + Assert.AreEqual(result.Length, count); + Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result)); + } + + [TestMethod] + public void Allocate_ReadBackBuffer_Copy_Full() + { + int[] source = new int[4096]; + + new Random(42).NextBytes(source.AsSpan().AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = Gpu.Default.AllocateReadBackBuffer(readOnlyBuffer.Length); + + readOnlyBuffer.CopyTo(readBackBuffer); + + Assert.AreEqual(source.Length, readBackBuffer.Length); + Assert.IsTrue(source.AsSpan().SequenceEqual(readBackBuffer.Span)); + } + + [TestMethod] + [DataRow(0, 0, 4096)] + [DataRow(128, 0, 2048)] + [DataRow(0, 128, 2048)] + [DataRow(97, 33, 512)] + public void Allocate_ReadBackBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count) + { + int[] source = new int[4096]; + + new Random(42).NextBytes(source.AsSpan().AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = Gpu.Default.AllocateReadBackBuffer(readOnlyBuffer.Length); + + readOnlyBuffer.CopyTo(readBackBuffer, destinationOffset, bufferOffset, count); + + Assert.AreEqual(source.Length, readBackBuffer.Length); + Assert.IsTrue(source.AsSpan(bufferOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count))); + } + } +}