Bug fixes, added TransferBuffer<T> tests
このコミットが含まれているのは:
1 親
7e9bd561ca
コミット
53012a5667
4ファイル変更
+197
-4
検索条件に一致するファイルはありません
@@ -26,10 +26,10 @@ namespace ComputeSharp.Tests
|
||||
[TestMethod]
|
||||
[DataRow(typeof(ConstantBuffer<>), -247824)]
|
||||
[DataRow(typeof(ConstantBuffer<>), -1)]
|
||||
[DataRow(typeof(ConstantBuffer<>), -247824)]
|
||||
[DataRow(typeof(ConstantBuffer<>), -1)]
|
||||
[DataRow(typeof(ConstantBuffer<>), -247824)]
|
||||
[DataRow(typeof(ConstantBuffer<>), -1)]
|
||||
[DataRow(typeof(ReadOnlyBuffer<>), -247824)]
|
||||
[DataRow(typeof(ReadOnlyBuffer<>), -1)]
|
||||
[DataRow(typeof(ReadWriteBuffer<>), -247824)]
|
||||
[DataRow(typeof(ReadWriteBuffer<>), -1)]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Allocate_Uninitialized_Fail(Type bufferType, int length)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<TargetFrameworks>net5.0</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -123,6 +123,69 @@ namespace ComputeSharp.Tests.Extensions
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="TransferBuffer{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="length">The length of the buffer to create.</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)
|
||||
where T : unmanaged
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer<T>(length),
|
||||
_ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer<T>(length),
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="TransferTexture2D{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="height">The height of the texture to create.</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)
|
||||
where T : unmanaged
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D<T>(width, height),
|
||||
_ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D<T>(width, height),
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="TransferTexture3D{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="height">The height of the texture to create.</param>
|
||||
/// <param name="depth">The depth of the texture to create.</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)
|
||||
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),
|
||||
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Resources;
|
||||
using ComputeSharp.Tests.Extensions;
|
||||
using Microsoft.Toolkit.HighPerformance.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("TransferBuffer")]
|
||||
public partial class TransferBufferTests
|
||||
{
|
||||
[TestMethod]
|
||||
[DataRow(typeof(UploadBuffer<>))]
|
||||
[DataRow(typeof(ReadBackBuffer<>))]
|
||||
public unsafe void Allocate_Uninitialized_Ok(Type bufferType)
|
||||
{
|
||||
using TransferBuffer<float> buffer = Gpu.Default.AllocateTransferBuffer<float>(bufferType, 128);
|
||||
|
||||
Assert.IsNotNull(buffer);
|
||||
Assert.AreEqual(buffer.Length, 128);
|
||||
Assert.AreEqual(buffer.Memory.Length, 128);
|
||||
Assert.AreEqual(buffer.Span.Length, 128);
|
||||
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, Gpu.Default);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(typeof(UploadBuffer<>), -247824)]
|
||||
[DataRow(typeof(UploadBuffer<>), -1)]
|
||||
[DataRow(typeof(ReadBackBuffer<>), -247824)]
|
||||
[DataRow(typeof(ReadBackBuffer<>), -1)]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Allocate_Uninitialized_Fail(Type bufferType, int length)
|
||||
{
|
||||
using TransferBuffer<float> buffer = Gpu.Default.AllocateTransferBuffer<float>(bufferType, length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(typeof(UploadBuffer<>))]
|
||||
[DataRow(typeof(ReadBackBuffer<>))]
|
||||
[ExpectedException(typeof(ObjectDisposedException))]
|
||||
public void UsageAfterDispose(Type bufferType)
|
||||
{
|
||||
using TransferBuffer<float> buffer = Gpu.Default.AllocateTransferBuffer<float>(bufferType, 128);
|
||||
|
||||
buffer.Dispose();
|
||||
|
||||
_ = buffer.Span;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Allocate_UploadBuffer_Copy_Full()
|
||||
{
|
||||
using UploadBuffer<int> uploadBuffer = Gpu.Default.AllocateUploadBuffer<int>(4096);
|
||||
|
||||
new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
|
||||
|
||||
using ReadOnlyBuffer<int> readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer<int>(uploadBuffer.Length);
|
||||
|
||||
uploadBuffer.CopyTo(readOnlyBuffer);
|
||||
|
||||
int[] result = readOnlyBuffer.ToArray();
|
||||
|
||||
Assert.AreEqual(uploadBuffer.Length, result.Length);
|
||||
Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(0, 0, 4096)]
|
||||
[DataRow(128, 0, 2048)]
|
||||
[DataRow(0, 128, 2048)]
|
||||
[DataRow(97, 33, 512)]
|
||||
public void Allocate_UploadBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count)
|
||||
{
|
||||
using UploadBuffer<int> uploadBuffer = Gpu.Default.AllocateUploadBuffer<int>(4096);
|
||||
|
||||
new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
|
||||
|
||||
using ReadOnlyBuffer<int> readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer<int>(uploadBuffer.Length);
|
||||
|
||||
uploadBuffer.CopyTo(readOnlyBuffer, destinationOffset, bufferOffset, count);
|
||||
|
||||
int[] result = readOnlyBuffer.ToArray(destinationOffset, count);
|
||||
|
||||
Assert.AreEqual(result.Length, count);
|
||||
Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Allocate_ReadBackBuffer_Copy_Full()
|
||||
{
|
||||
int[] source = new int[4096];
|
||||
|
||||
new Random(42).NextBytes(source.AsSpan().AsBytes());
|
||||
|
||||
using ReadOnlyBuffer<int> readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source);
|
||||
using ReadBackBuffer<int> readBackBuffer = Gpu.Default.AllocateReadBackBuffer<int>(readOnlyBuffer.Length);
|
||||
|
||||
readOnlyBuffer.CopyTo(readBackBuffer);
|
||||
|
||||
Assert.AreEqual(source.Length, readBackBuffer.Length);
|
||||
Assert.IsTrue(source.AsSpan().SequenceEqual(readBackBuffer.Span));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(0, 0, 4096)]
|
||||
[DataRow(128, 0, 2048)]
|
||||
[DataRow(0, 128, 2048)]
|
||||
[DataRow(97, 33, 512)]
|
||||
public void Allocate_ReadBackBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count)
|
||||
{
|
||||
int[] source = new int[4096];
|
||||
|
||||
new Random(42).NextBytes(source.AsSpan().AsBytes());
|
||||
|
||||
using ReadOnlyBuffer<int> readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source);
|
||||
using ReadBackBuffer<int> readBackBuffer = Gpu.Default.AllocateReadBackBuffer<int>(readOnlyBuffer.Length);
|
||||
|
||||
readOnlyBuffer.CopyTo(readBackBuffer, destinationOffset, bufferOffset, count);
|
||||
|
||||
Assert.AreEqual(source.Length, readBackBuffer.Length);
|
||||
Assert.IsTrue(source.AsSpan(bufferOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count)));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする