diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 9149926c..7e9d13cf 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -114,6 +114,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract [Pure] public T[] GetData(int offset, int count) { + Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); + T[] data = GC.AllocateUninitializedArray(count); GetData(data.AsSpan(), offset); diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 435d2de8..5b6f8177 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,139 +11,182 @@ namespace ComputeSharp.Tests public class BufferTests { [TestMethod] - public void ReadWriteBufferGetSetDataFromArray() + [DataRow(typeof(ConstantBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>))] + public void AllocateBuffer_Uninitialized(Type bufferType) { - float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); + + Assert.IsNotNull(buffer); + Assert.AreEqual(buffer.Length, 128); + Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + } + + [TestMethod] + [DataRow(typeof(ConstantBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>))] + public void AllocateBuffer_FromArray(Type bufferType) + { + float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, data); + + Assert.IsNotNull(buffer); + Assert.AreEqual(buffer.Length, 128); + Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(array); float[] result = buffer.GetData(); - Assert.IsTrue(array.AsSpan().ContentEquals(result)); + Assert.IsNotNull(result); + Assert.AreEqual(data.Length, result.Length); + Assert.IsTrue(data.SequenceEqual(result)); } [TestMethod] - public void ReadOnlyBufferGetSetDataFromArray() + [DataRow(typeof(ConstantBuffer<>), typeof(ConstantBuffer<>))] + [DataRow(typeof(ConstantBuffer<>), typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ConstantBuffer<>), typeof(ReadWriteBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>), typeof(ConstantBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadWriteBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))] + public void AllocateBuffer_FromBuffer(Type sourceType, Type destinationType) + { + float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + + using Buffer source = Gpu.Default.AllocateBuffer(sourceType, data); + using Buffer destination = Gpu.Default.AllocateBuffer(destinationType, source); + + Assert.IsNotNull(destination); + Assert.AreEqual(destination.Length, 128); + Assert.AreSame(destination.GraphicsDevice, Gpu.Default); + + float[] result = destination.GetData(); + + Assert.IsNotNull(result); + Assert.AreEqual(data.Length, result.Length); + Assert.IsTrue(data.SequenceEqual(result)); + } + + [TestMethod] + [DataRow(typeof(ConstantBuffer<>))] + [DataRow(typeof(ReadOnlyBuffer<>))] + [DataRow(typeof(ReadWriteBuffer<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Type bufferType) + { + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); + + buffer.Dispose(); + + _ = buffer.GetData(); + } + + [TestMethod] + [DataRow(typeof(ConstantBuffer<>), 0, 4096)] + [DataRow(typeof(ConstantBuffer<>), 128, 512)] + [DataRow(typeof(ConstantBuffer<>), 2048, 2048)] + [DataRow(typeof(ReadOnlyBuffer<>), 0, 4096)] + [DataRow(typeof(ReadOnlyBuffer<>), 128, 512)] + [DataRow(typeof(ReadOnlyBuffer<>), 2048, 2048)] + [DataRow(typeof(ReadWriteBuffer<>), 0, 4096)] + [DataRow(typeof(ReadWriteBuffer<>), 128, 512)] + [DataRow(typeof(ReadWriteBuffer<>), 2048, 2048)] + public void GetData_RangeToArray_Ok(Type bufferType, int offset, int count) { float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - using ConstantBuffer buffer = Gpu.Default.AllocateConstantBuffer(array); - float[] result = buffer.GetData(); + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); - Assert.IsTrue(array.AsSpan().ContentEquals(result)); + float[] result = buffer.GetData(offset, count); + + Assert.IsNotNull(result); + Assert.AreEqual(result.Length, count); + Assert.IsTrue(array.AsSpan(offset, count).SequenceEqual(result)); } [TestMethod] - public void ReadWriteBufferGetSetDataFromReadWriteBuffer() - { - float[] array = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); - - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); - using ReadWriteBuffer destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer); - - float[] sourceResult = sourceBuffer.GetData(); - float[] destinationResult = destinationBuffer.GetData(); - - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); - Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult)); - } - - [TestMethod] - public void ReadWriteBufferGetSetDataFromReadOnlyBuffer() + [DataRow(typeof(ConstantBuffer<>), -1, 128)] + [DataRow(typeof(ConstantBuffer<>), 8192, 128)] + [DataRow(typeof(ConstantBuffer<>), 512, 4096)] + [DataRow(typeof(ConstantBuffer<>), 512, -128)] + [DataRow(typeof(ReadOnlyBuffer<>), -1, 128)] + [DataRow(typeof(ReadOnlyBuffer<>), 8192, 128)] + [DataRow(typeof(ReadOnlyBuffer<>), 512, 4096)] + [DataRow(typeof(ReadOnlyBuffer<>), 512, -128)] + [DataRow(typeof(ReadWriteBuffer<>), -1, 128)] + [DataRow(typeof(ReadWriteBuffer<>), 8192, 128)] + [DataRow(typeof(ReadWriteBuffer<>), 512, 4096)] + [DataRow(typeof(ReadWriteBuffer<>), 512, -128)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToArray_Fail(Type bufferType, int offset, int count) { float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); - using ReadWriteBuffer destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer); + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); - float[] sourceResult = sourceBuffer.GetData(); - float[] destinationResult = destinationBuffer.GetData(); - - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); - Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult)); + _ = buffer.GetData(offset, count); } [TestMethod] - public void ReadOnlyBufferGetSetDataFromReadWriteBuffer() + [DataRow(typeof(ConstantBuffer<>), 0, 0, 4096)] + [DataRow(typeof(ConstantBuffer<>), 512, 0, 2048)] + [DataRow(typeof(ConstantBuffer<>), 0, 512, 2048)] + [DataRow(typeof(ConstantBuffer<>), 127, 1024, 587)] + [DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 4096)] + [DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 2048)] + [DataRow(typeof(ReadOnlyBuffer<>), 0, 512, 2048)] + [DataRow(typeof(ReadOnlyBuffer<>), 127, 1024, 587)] + [DataRow(typeof(ReadWriteBuffer<>), 0, 0, 4096)] + [DataRow(typeof(ReadWriteBuffer<>), 512, 0, 2048)] + [DataRow(typeof(ReadWriteBuffer<>), 0, 512, 2048)] + [DataRow(typeof(ReadWriteBuffer<>), 127, 1024, 587)] + public void GetData_RangeToVoid_Ok(Type bufferType, int destinationOffset, int bufferOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); - using ConstantBuffer destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer); + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); - float[] sourceResult = sourceBuffer.GetData(); - float[] destinationResult = destinationBuffer.GetData(); + float[] result = new float[4096]; - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); - Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult)); + buffer.GetData(result, destinationOffset, bufferOffset, count); + + Assert.IsTrue(array.AsSpan(bufferOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count))); } [TestMethod] - public void ReadOnlyBufferGetSetDataFromReadOnlyBuffer() + [DataRow(typeof(ConstantBuffer<>), 0, 0, 8196)] + [DataRow(typeof(ConstantBuffer<>), -12, 0, 1024)] + [DataRow(typeof(ConstantBuffer<>), 0, -56, 1024)] + [DataRow(typeof(ConstantBuffer<>), 512, 0, 4096)] + [DataRow(typeof(ConstantBuffer<>), 12, 1024, 3600)] + [DataRow(typeof(ConstantBuffer<>), 12, 1024, -2096)] + [DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 8196)] + [DataRow(typeof(ReadOnlyBuffer<>), -12, 0, 1024)] + [DataRow(typeof(ReadOnlyBuffer<>), 0, -56, 1024)] + [DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 4096)] + [DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, 3600)] + [DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, -2096)] + [DataRow(typeof(ReadWriteBuffer<>), 0, 0, 8196)] + [DataRow(typeof(ReadWriteBuffer<>), -12, 0, 1024)] + [DataRow(typeof(ReadWriteBuffer<>), 0, -56, 1024)] + [DataRow(typeof(ReadWriteBuffer<>), 512, 0, 4096)] + [DataRow(typeof(ReadWriteBuffer<>), 12, 1024, 3600)] + [DataRow(typeof(ReadWriteBuffer<>), 12, 1024, -2096)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToVoid_Fail(Type bufferType, int destinationOffset, int bufferOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); - using ConstantBuffer destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer); + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); - float[] sourceResult = sourceBuffer.GetData(); - float[] destinationResult = destinationBuffer.GetData(); + float[] result = new float[4096]; - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); - Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult)); - } - - [TestMethod] - public void ConstantBufferGetRangeData() - { - float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - - using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); - - float[] sourceResult = sourceBuffer.GetData(128, 100); - - Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult)); - } - - [TestMethod] - public void ConstantBufferSetRangeData() - { - float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray(); - - using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); - - array.AsSpan(56, 100).Clear(); - sourceBuffer.SetData(new float[100], 56); - - float[] sourceResult = sourceBuffer.GetData(); - - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); - } - - [TestMethod] - public void ReadWriteBufferGetRangeData() - { - float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); - - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); - - float[] sourceResult = sourceBuffer.GetData(128, 100); - - Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult)); - } - - [TestMethod] - public void ReadWriteBufferSetRangeData() - { - float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray(); - - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); - - array.AsSpan(56, 100).Clear(); - sourceBuffer.SetData(new float[100], 56); - - float[] sourceResult = sourceBuffer.GetData(); - - Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); + buffer.GetData(result, destinationOffset, bufferOffset, count); } } } diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs new file mode 100644 index 00000000..48aa0554 --- /dev/null +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -0,0 +1,76 @@ +using System; +using System.Diagnostics.Contracts; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp.Tests.Extensions +{ + /// + /// A helper class for testing APIs. + /// + public static class GraphicsDeviceExtensions + { + /// + /// 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 Buffer AllocateBuffer(this GraphicsDevice device, Type type, int length) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(length), + _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(length), + _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(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 data to load on the buffer. + /// A instance of the requested type. + [Pure] + public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, T[] data) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data), + _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data), + _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data), + _ => 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 data to load on the buffer. + /// A instance of the requested type. + [Pure] + public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, Buffer data) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data), + _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data), + _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + } +}