Added unit tests for custom allocation modes

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-13 14:24:55 +01:00
1 親 c65a36c87d
コミット 5622a8b60a
8ファイル変更
+142 -66

検索条件に一致するファイルはありません

+12 -2
ファイルの表示
@@ -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<float> buffer = device.Get().AllocateBuffer<float>(bufferType, 128);
using Buffer<float> buffer = device.Get().AllocateBuffer<float>(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]
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.0.0-preview4" />
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
+33 -25
ファイルの表示
@@ -19,16 +19,17 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="length">The length of the buffer to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Buffer{T}"/> instance of the requested size.</returns>
[Pure]
public static Buffer<T> AllocateBuffer<T>(this GraphicsDevice device, Type type, int length)
public static Buffer<T> AllocateBuffer<T>(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer<T>(length),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer<T>(length),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer<T>(length),
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer<T>(length, allocationMode),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer<T>(length, allocationMode),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer<T>(length, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -41,15 +42,16 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance of the requested size.</returns>
[Pure]
public static Texture2D<T> AllocateTexture2D<T>(this GraphicsDevice device, Type type, int width, int height)
public static Texture2D<T> AllocateTexture2D<T>(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<T>(width, height),
_ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D<T>(width, height),
_ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D<T>(width, height, allocationMode),
_ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D<T>(width, height, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -63,16 +65,17 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance of the requested size.</returns>
[Pure]
public static Texture2D<T> AllocateTexture2D<T, TPixel>(this GraphicsDevice device, Type type, int width, int height)
public static Texture2D<T> AllocateTexture2D<T, TPixel>(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D<T, TPixel>(width, height),
_ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D<T, TPixel>(width, height),
_ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D<T, TPixel>(width, height, allocationMode),
_ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D<T, TPixel>(width, height, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -86,15 +89,16 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="depth">The depth of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance of the requested size.</returns>
[Pure]
public static Texture3D<T> AllocateTexture3D<T>(this GraphicsDevice device, Type type, int width, int height, int depth)
public static Texture3D<T> AllocateTexture3D<T>(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<T>(width, height, depth),
_ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D<T>(width, height, depth),
_ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D<T>(width, height, depth, allocationMode),
_ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D<T>(width, height, depth, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -109,16 +113,17 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="depth">The depth of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture3D{T}"/> instance of the requested size.</returns>
[Pure]
public static Texture3D<T> AllocateTexture3D<T, TPixel>(this GraphicsDevice device, Type type, int width, int height, int depth)
public static Texture3D<T> AllocateTexture3D<T, TPixel>(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D<T, TPixel>(width, height, depth),
_ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D<T, TPixel>(width, height, depth),
_ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D<T, TPixel>(width, height, depth, allocationMode),
_ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D<T, TPixel>(width, height, depth, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -130,15 +135,16 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="length">The length of the buffer to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="TransferBuffer{T}"/> instance of the requested size.</returns>
[Pure]
public static TransferBuffer<T> AllocateTransferBuffer<T>(this GraphicsDevice device, Type type, int length)
public static TransferBuffer<T> AllocateTransferBuffer<T>(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer<T>(length),
_ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer<T>(length),
_ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer<T>(length, allocationMode),
_ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer<T>(length, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -151,15 +157,16 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="TransferTexture2D{T}"/> instance of the requested size.</returns>
[Pure]
public static TransferTexture2D<T> AllocateTransferTexture2D<T>(this GraphicsDevice device, Type type, int width, int height)
public static TransferTexture2D<T> AllocateTransferTexture2D<T>(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<T>(width, height),
_ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D<T>(width, height),
_ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D<T>(width, height, allocationMode),
_ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D<T>(width, height, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
@@ -173,15 +180,16 @@ namespace ComputeSharp.Tests.Extensions
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="depth">The depth of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="TransferTexture3D{T}"/> instance of the requested size.</returns>
[Pure]
public static TransferTexture3D<T> AllocateTransferTexture3D<T>(this GraphicsDevice device, Type type, int width, int height, int depth)
public static TransferTexture3D<T> AllocateTransferTexture3D<T>(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<T>(width, height, depth),
_ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D<T>(width, height, depth),
_ when type == typeof(UploadTexture3D<>) => device.AllocateUploadTexture3D<T>(width, height, depth, allocationMode),
_ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D<T>(width, height, depth, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
+13 -3
ファイルの表示
@@ -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<float> texture = device.Get().AllocateTexture2D<float>(textureType, 128, 128);
using Texture2D<float> texture = device.Get().AllocateTexture2D<float>(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]
+13 -4
ファイルの表示
@@ -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<float> texture = device.Get().AllocateTexture3D<float>(textureType, 64, 64, 12);
using Texture3D<float> texture = device.Get().AllocateTexture3D<float>(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]
+13 -3
ファイルの表示
@@ -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<float> buffer = device.Get().AllocateTransferBuffer<float>(bufferType, 128);
using TransferBuffer<float> buffer = device.Get().AllocateTransferBuffer<float>(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]
+26 -13
ファイルの表示
@@ -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<float> buffer = device.Get().AllocateTransferTexture2D<float>(bufferType, 128, 256);
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(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<float> buffer = device.Get().AllocateTransferTexture2D<float>(bufferType, width, height);
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(bufferType, width, height);
}
[CombinatorialTestMethod]
@@ -49,11 +62,11 @@ namespace ComputeSharp.Tests
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Device device, Type bufferType)
{
using TransferTexture2D<float> buffer = device.Get().AllocateTransferTexture2D<float>(bufferType, 32, 32);
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(bufferType, 32, 32);
buffer.Dispose();
texture.Dispose();
_ = buffer.View;
_ = texture.View;
}
[CombinatorialTestMethod]
+31 -15
ファイルの表示
@@ -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<float> buffer = device.Get().AllocateTransferTexture3D<float>(bufferType, 128, 256, 32);
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(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<float> buffer = device.Get().AllocateTransferTexture3D<float>(bufferType, width, height, depth);
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(bufferType, width, height, depth);
}
[CombinatorialTestMethod]
@@ -53,11 +69,11 @@ namespace ComputeSharp.Tests
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Device device, Type bufferType)
{
using TransferTexture3D<float> buffer = device.Get().AllocateTransferTexture3D<float>(bufferType, 32, 32, 16);
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(bufferType, 32, 32, 16);
buffer.Dispose();
texture.Dispose();
_ = buffer.View;
_ = texture.View;
}
[CombinatorialTestMethod]