diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index 09d85ec9..2e8cd78c 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -140,6 +140,26 @@ 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 width of the texture to create. + /// The allocation mode to use for the new resource. + /// A instance of the requested size. + public static TransferTexture1D AllocateTransferTexture1D(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default) + where T : unmanaged + { + return type switch + { + _ when type == typeof(UploadTexture1D<>) => device.AllocateUploadTexture1D(width, allocationMode), + _ when type == typeof(ReadBackTexture1D<>) => device.AllocateReadBackTexture1D(width, allocationMode), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/TransferTexture1DTests.cs b/tests/ComputeSharp.Tests/TransferTexture1DTests.cs new file mode 100644 index 00000000..61529276 --- /dev/null +++ b/tests/ComputeSharp.Tests/TransferTexture1DTests.cs @@ -0,0 +1,175 @@ +using System; +using System.Linq; +using System.Runtime.CompilerServices; +using CommunityToolkit.HighPerformance; +using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("TransferTexture1D")] +public partial class TransferTexture1DTests +{ + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture1D<>))] + [Resource(typeof(ReadBackTexture1D<>))] + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode) + { + using TransferTexture1D texture = device.Get().AllocateTransferTexture1D(textureType, 256, allocationMode); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 256); + Assert.AreEqual(texture.Memory.Length, 256); + Assert.AreEqual(texture.Span.Length, 256); + Assert.AreSame(texture.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + foreach (float x in texture.Memory.Span) + { + Assert.AreEqual(x, 0f); + } + + foreach (float x in texture.Span) + { + Assert.AreEqual(x, 0f); + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture1D<>))] + [Resource(typeof(ReadBackTexture1D<>))] + [Data(-14253)] + [Data(-1)] + [Data(0)] + [Data(-14)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width) + { + using TransferTexture1D texture = device.Get().AllocateTransferTexture1D(textureType, width); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture1D<>))] + [Resource(typeof(ReadBackTexture1D<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Device device, Type textureType) + { + using TransferTexture1D texture = device.Get().AllocateTransferTexture1D(textureType, 32); + + texture.Dispose(); + + _ = texture.Span; + } + + [CombinatorialTestMethod] + [AllDevices] + public unsafe void Allocate_UploadTexture1D_Copy_Full(Device device) + { + using UploadTexture1D uploadTexture1D = device.Get().AllocateUploadTexture1D(256); + + new Random(42).NextBytes(uploadTexture1D.Span.AsBytes()); + + using ReadOnlyTexture1D readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(uploadTexture1D.Width); + + uploadTexture1D.CopyTo(readOnlyTexture1D); + + int[] result = readOnlyTexture1D.ToArray(); + + Assert.AreEqual(uploadTexture1D.Width, result.Length); + + fixed (int* _ = result) + { + Assert.IsTrue(uploadTexture1D.Span.SequenceEqual(new Span(Unsafe.AsPointer(ref result[0]), result.Length))); + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 2048)] + [Data(128, 1813)] + [Data(0, 2000)] + [Data(97, 512)] + public unsafe void Allocate_UploadTexture1D_Copy_Range(Device device, int x, int width) + { + using UploadTexture1D uploadTexture1D = device.Get().AllocateUploadTexture1D(2048); + + new Random(42).NextBytes(uploadTexture1D.Span.AsBytes()); + + using ReadOnlyTexture1D readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(uploadTexture1D.Width); + + uploadTexture1D.CopyTo(readOnlyTexture1D, x, width); + + int[] result = readOnlyTexture1D.ToArray(); + + fixed (void* _ = result) + { + Span sourceRow = uploadTexture1D.Span.Slice(x, width); + Span destinationRow = new Span(Unsafe.AsPointer(ref result[0]), result.Length).Slice(0, width); + + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } + } + + [CombinatorialTestMethod] + [AllDevices] + public unsafe void Allocate_ReadBackTexture1D_Copy_Full(Device device) + { + int[] source = new int[256]; + + fixed (int* p = source) + { + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); + } + + using ReadOnlyTexture1D readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source); + using ReadBackTexture1D readBackTexture1D = device.Get().AllocateReadBackTexture1D(256); + + readOnlyTexture1D.CopyTo(readBackTexture1D); + + Assert.AreEqual(readBackTexture1D.Width, 256); + + fixed (int* p = source) + { + Assert.IsTrue(readBackTexture1D.Span.SequenceEqual(new Span(Unsafe.AsPointer(ref source[0]), source.Length))); + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 2048)] + [Data(128, 1813)] + [Data(0, 2000)] + [Data(97, 512)] + public unsafe void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int width) + { + int[] source = new int[2048]; + + fixed (int* p = source) + { + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); + } + + using ReadOnlyTexture1D readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source); + using ReadBackTexture1D readBackTexture1D = device.Get().AllocateReadBackTexture1D(2048); + + readOnlyTexture1D.CopyTo(readBackTexture1D, x, width); + + fixed (int* p = source) + { + Span sourceRow = new Span(Unsafe.AsPointer(ref source[0]), source.Length).Slice(x, width); + Span destinationRow = readBackTexture1D.Span.Slice(0, width); + + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } + } +}