diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index b1ffbb42..705b166f 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -16,13 +16,23 @@ namespace ComputeSharp.Tests [Resource(typeof(ConstantBuffer<>))] [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] - public void Allocate_Uninitialized_Ok(Device device, Type bufferType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) { - using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128, allocationMode); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Length, 128); Assert.AreSame(buffer.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + foreach (float x in buffer.ToArray()) + { + Assert.AreEqual(x, 0f); + } + } } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 12ff3527..364ecaa2 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -7,7 +7,7 @@ - + diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index 9cfc05d1..f9e8a0a7 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -19,16 +19,17 @@ namespace ComputeSharp.Tests.Extensions /// The target instance to allocate the buffer for. /// The type of buffer to allocate. /// The length of the buffer to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, int length) + public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default) 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), + _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(length, allocationMode), + _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(length, allocationMode), + _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(length, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -41,15 +42,16 @@ namespace ComputeSharp.Tests.Extensions /// The type of texture to allocate. /// The width of the texture to create. /// The height of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { - _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(width, height), - _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(width, height), + _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(width, height, allocationMode), + _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -63,16 +65,17 @@ namespace ComputeSharp.Tests.Extensions /// The type of texture to allocate. /// The width of the texture to create. /// The height of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { return type switch { - _ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D(width, height), - _ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D(width, height), + _ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D(width, height, allocationMode), + _ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -86,15 +89,16 @@ namespace ComputeSharp.Tests.Extensions /// The width of the texture to create. /// The height of the texture to create. /// The depth of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { - _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(width, height, depth), - _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(width, height, depth), + _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(width, height, depth, allocationMode), + _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -109,16 +113,17 @@ namespace ComputeSharp.Tests.Extensions /// The width of the texture to create. /// The height of the texture to create. /// The depth of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { return type switch { - _ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D(width, height, depth), - _ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D(width, height, depth), + _ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D(width, height, depth, allocationMode), + _ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -130,15 +135,16 @@ namespace ComputeSharp.Tests.Extensions /// The target instance to allocate the buffer for. /// The type of buffer to allocate. /// The length of the buffer to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static TransferBuffer AllocateTransferBuffer(this GraphicsDevice device, Type type, int length) + public static TransferBuffer AllocateTransferBuffer(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { - _ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer(length), - _ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer(length), + _ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer(length, allocationMode), + _ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer(length, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -151,15 +157,16 @@ namespace ComputeSharp.Tests.Extensions /// The type of buffer to allocate. /// The width of the texture to create. /// The height of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static TransferTexture2D AllocateTransferTexture2D(this GraphicsDevice device, Type type, int width, int height) + public static TransferTexture2D AllocateTransferTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { - _ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D(width, height), - _ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D(width, height), + _ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D(width, height, allocationMode), + _ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } @@ -173,15 +180,16 @@ namespace ComputeSharp.Tests.Extensions /// The width of the texture to create. /// The height of the texture to create. /// The depth of the texture to create. + /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] - public static TransferTexture3D AllocateTransferTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + public static TransferTexture3D AllocateTransferTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { - _ when type == typeof(UploadTexture3D<>) => device.AllocateUploadTexture3D(width, height, depth), - _ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D(width, height, depth), + _ when type == typeof(UploadTexture3D<>) => device.AllocateUploadTexture3D(width, height, depth, allocationMode), + _ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index a0192a65..ee23d188 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -3,7 +3,7 @@ using System.Linq; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; -using Microsoft.Toolkit.HighPerformance.Memory; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -16,14 +16,24 @@ namespace ComputeSharp.Tests [AllDevices] [Resource(typeof(ReadOnlyTexture2D<>))] [Resource(typeof(ReadWriteTexture2D<>))] - public void Allocate_Uninitialized_Ok(Device device, Type textureType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode) { - using Texture2D texture = device.Get().AllocateTexture2D(textureType, 128, 128); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, 128, 128, allocationMode); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); Assert.AreEqual(texture.Height, 128); Assert.AreSame(texture.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + foreach (float x in texture.ToArray()) + { + Assert.AreEqual(x, 0f); + } + } } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index d535bc97..34a384b7 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -3,8 +3,7 @@ using System.Linq; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; -using Microsoft.Toolkit.HighPerformance.Extensions; -using Microsoft.Toolkit.HighPerformance.Memory; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -17,15 +16,25 @@ namespace ComputeSharp.Tests [AllDevices] [Resource(typeof(ReadOnlyTexture3D<>))] [Resource(typeof(ReadWriteTexture3D<>))] - public void Allocate_Uninitialized_Ok(Device device, Type textureType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode) { - using Texture3D texture = device.Get().AllocateTexture3D(textureType, 64, 64, 12); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, 64, 64, 12, allocationMode); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 64); Assert.AreEqual(texture.Height, 64); Assert.AreEqual(texture.Depth, 12); Assert.AreSame(texture.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + foreach (float x in texture.ToArray()) + { + Assert.AreEqual(x, 0f); + } + } } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs index d1c478de..a16c9c3d 100644 --- a/tests/ComputeSharp.Tests/TransferBufferTests.cs +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -4,7 +4,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; -using Microsoft.Toolkit.HighPerformance.Extensions; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -17,9 +17,11 @@ namespace ComputeSharp.Tests [AllDevices] [Resource(typeof(UploadBuffer<>))] [Resource(typeof(ReadBackBuffer<>))] - public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) { - using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, 128); + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, 128, allocationMode); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Length, 128); @@ -28,6 +30,14 @@ namespace ComputeSharp.Tests 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, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + foreach (float x in buffer.Span) + { + Assert.AreEqual(x, 0f); + } + } } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs index 316962af..2e4a6e58 100644 --- a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs @@ -3,7 +3,7 @@ using System.Linq; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; -using Microsoft.Toolkit.HighPerformance.Extensions; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -16,16 +16,29 @@ namespace ComputeSharp.Tests [AllDevices] [Resource(typeof(UploadTexture2D<>))] [Resource(typeof(ReadBackTexture2D<>))] - public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) { - using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, 128, 256); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, 128, 256, allocationMode); - Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Width, 128); - Assert.AreEqual(buffer.Height, 256); - Assert.AreEqual(buffer.View.Width, 128); - Assert.AreEqual(buffer.View.Height, 256); - Assert.AreSame(buffer.GraphicsDevice, device.Get()); + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 256); + Assert.AreEqual(texture.View.Width, 128); + Assert.AreEqual(texture.View.Height, 256); + Assert.AreSame(texture.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + for (int y = 0; y < texture.Height; y++) + { + foreach (float x in texture.View.GetRowSpan(y)) + { + Assert.AreEqual(x, 0f); + } + } + } } [CombinatorialTestMethod] @@ -39,7 +52,7 @@ namespace ComputeSharp.Tests [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height) { - using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, width, height); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, width, height); } [CombinatorialTestMethod] @@ -49,11 +62,11 @@ namespace ComputeSharp.Tests [ExpectedException(typeof(ObjectDisposedException))] public void UsageAfterDispose(Device device, Type bufferType) { - using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, 32, 32); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, 32, 32); - buffer.Dispose(); + texture.Dispose(); - _ = buffer.View; + _ = texture.View; } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs index edebcc8e..ad06735c 100644 --- a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs @@ -3,7 +3,7 @@ using System.Linq; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; -using Microsoft.Toolkit.HighPerformance.Extensions; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -16,18 +16,34 @@ namespace ComputeSharp.Tests [AllDevices] [Resource(typeof(UploadTexture3D<>))] [Resource(typeof(ReadBackTexture3D<>))] - public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) + [Data(AllocationMode.Default)] + [Data(AllocationMode.Clear)] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) { - using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, 128, 256, 32); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, 128, 256, 32, allocationMode); - Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Width, 128); - Assert.AreEqual(buffer.Height, 256); - Assert.AreEqual(buffer.Depth, 32); - Assert.AreEqual(buffer.View.Width, 128); - Assert.AreEqual(buffer.View.Height, 256); - Assert.AreEqual(buffer.View.Depth, 32); - Assert.AreSame(buffer.GraphicsDevice, device.Get()); + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 256); + Assert.AreEqual(texture.Depth, 32); + Assert.AreEqual(texture.View.Width, 128); + Assert.AreEqual(texture.View.Height, 256); + Assert.AreEqual(texture.View.Depth, 32); + Assert.AreSame(texture.GraphicsDevice, device.Get()); + + if (allocationMode == AllocationMode.Clear) + { + for (int z = 0; z < texture.Depth; z++) + { + for (int y = 0; y < texture.Height; y++) + { + foreach (float x in texture.View.GetRowSpan(y, z)) + { + Assert.AreEqual(x, 0f); + } + } + } + } } [CombinatorialTestMethod] @@ -43,7 +59,7 @@ namespace ComputeSharp.Tests [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height, int depth) { - using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, width, height, depth); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, width, height, depth); } [CombinatorialTestMethod] @@ -53,11 +69,11 @@ namespace ComputeSharp.Tests [ExpectedException(typeof(ObjectDisposedException))] public void UsageAfterDispose(Device device, Type bufferType) { - using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, 32, 32, 16); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, 32, 32, 16); - buffer.Dispose(); + texture.Dispose(); - _ = buffer.View; + _ = texture.View; } [CombinatorialTestMethod]