Add TransferTexture1DTests unit tests
このコミットが含まれているのは:
1 親
d3476684fc
コミット
f46f924f08
2ファイル変更
+195
検索条件に一致するファイルはありません
@@ -140,6 +140,26 @@ public static class GraphicsDeviceExtensions
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="TransferTexture1D{T}"/> instance of the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items in the buffer.</typeparam>
|
||||
/// <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="width">The width of the texture to create.</param>
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
/// <returns>A <see cref="TransferTexture1D{T}"/> instance of the requested size.</returns>
|
||||
public static TransferTexture1D<T> AllocateTransferTexture1D<T>(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
|
||||
where T : unmanaged
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(UploadTexture1D<>) => device.AllocateUploadTexture1D<T>(width, allocationMode),
|
||||
_ when type == typeof(ReadBackTexture1D<>) => device.AllocateReadBackTexture1D<T>(width, allocationMode),
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="TransferTexture2D{T}"/> instance of the specified type.
|
||||
/// </summary>
|
||||
|
||||
@@ -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<float> texture = device.Get().AllocateTransferTexture1D<float>(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<float> texture = device.Get().AllocateTransferTexture1D<float>(textureType, width);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(UploadTexture1D<>))]
|
||||
[Resource(typeof(ReadBackTexture1D<>))]
|
||||
[ExpectedException(typeof(ObjectDisposedException))]
|
||||
public void UsageAfterDispose(Device device, Type textureType)
|
||||
{
|
||||
using TransferTexture1D<float> texture = device.Get().AllocateTransferTexture1D<float>(textureType, 32);
|
||||
|
||||
texture.Dispose();
|
||||
|
||||
_ = texture.Span;
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public unsafe void Allocate_UploadTexture1D_Copy_Full(Device device)
|
||||
{
|
||||
using UploadTexture1D<int> uploadTexture1D = device.Get().AllocateUploadTexture1D<int>(256);
|
||||
|
||||
new Random(42).NextBytes(uploadTexture1D.Span.AsBytes());
|
||||
|
||||
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D<int>(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<int>(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<int> uploadTexture1D = device.Get().AllocateUploadTexture1D<int>(2048);
|
||||
|
||||
new Random(42).NextBytes(uploadTexture1D.Span.AsBytes());
|
||||
|
||||
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D<int>(uploadTexture1D.Width);
|
||||
|
||||
uploadTexture1D.CopyTo(readOnlyTexture1D, x, width);
|
||||
|
||||
int[] result = readOnlyTexture1D.ToArray();
|
||||
|
||||
fixed (void* _ = result)
|
||||
{
|
||||
Span<int> sourceRow = uploadTexture1D.Span.Slice(x, width);
|
||||
Span<int> destinationRow = new Span<int>(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<int>(p, source.Length).AsBytes());
|
||||
}
|
||||
|
||||
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source);
|
||||
using ReadBackTexture1D<int> readBackTexture1D = device.Get().AllocateReadBackTexture1D<int>(256);
|
||||
|
||||
readOnlyTexture1D.CopyTo(readBackTexture1D);
|
||||
|
||||
Assert.AreEqual(readBackTexture1D.Width, 256);
|
||||
|
||||
fixed (int* p = source)
|
||||
{
|
||||
Assert.IsTrue(readBackTexture1D.Span.SequenceEqual(new Span<int>(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<int>(p, source.Length).AsBytes());
|
||||
}
|
||||
|
||||
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source);
|
||||
using ReadBackTexture1D<int> readBackTexture1D = device.Get().AllocateReadBackTexture1D<int>(2048);
|
||||
|
||||
readOnlyTexture1D.CopyTo(readBackTexture1D, x, width);
|
||||
|
||||
fixed (int* p = source)
|
||||
{
|
||||
Span<int> sourceRow = new Span<int>(Unsafe.AsPointer(ref source[0]), source.Length).Slice(x, width);
|
||||
Span<int> destinationRow = readBackTexture1D.Span.Slice(0, width);
|
||||
|
||||
Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする