Add new ReadBackBufferExtensions type

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-12 02:03:03 +01:00
1 親 c657730249
コミット 29944625e0
2ファイル変更
+126 -12

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

+36
ファイルの表示
@@ -0,0 +1,36 @@
using ComputeSharp.Resources;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="ReadBackBuffer{T}"/> type.
/// </summary>
public static class ReadBackBufferExtensions
{
/// <summary>
/// Reads the contents of a <see cref="StructuredBuffer{T}"/> instance and writes them into a target <see cref="ReadBackBuffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="destination">The target <see cref="StructuredBuffer{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="UploadBuffer{T}"/> instance to read data from.</param>
public static void CopyFrom<T>(this ReadBackBuffer<T> destination, StructuredBuffer<T> source)
where T : unmanaged
{
source.CopyTo(destination, 0, 0, source.Length);
}
/// <summary>
/// Reads the contents of a <see cref="StructuredBuffer{T}"/> instance and writes them into a target <see cref="ReadBackBuffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="destination">The target <see cref="StructuredBuffer{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="UploadBuffer{T}"/> instance to read data from.</param>
/// <param name="sourceOffset">The offset to start reading data from.</param>
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
/// <param name="count">The number of items to read.</param>
public static void CopyFrom<T>(this ReadBackBuffer<T> destination, StructuredBuffer<T> source, int sourceOffset, int destinationOffset, int count)
where T : unmanaged
{
source.CopyTo(destination, sourceOffset, destinationOffset, count);
}
}
+90 -12
ファイルの表示
@@ -94,7 +94,7 @@ public partial class TransferBufferTests
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_UploadBuffer_Copy_Full(Device device)
public void Allocate_UploadBuffer_CopyTo_Full(Device device)
{
using UploadBuffer<int> uploadBuffer = device.Get().AllocateUploadBuffer<int>(4096);
@@ -113,10 +113,10 @@ public partial class TransferBufferTests
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 0, 4096)]
[Data(128, 0, 2048)]
[Data(0, 128, 2048)]
[Data(97, 33, 512)]
public void Allocate_UploadBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count)
[Data(128, 0, 2048)]
[Data(33, 97, 512)]
public void Allocate_UploadBuffer_CopyTo_Range(Device device, int sourceOffset, int destinationOffset, int count)
{
using UploadBuffer<int> uploadBuffer = device.Get().AllocateUploadBuffer<int>(4096);
@@ -124,17 +124,17 @@ public partial class TransferBufferTests
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer<int>(uploadBuffer.Length);
uploadBuffer.CopyTo(readOnlyBuffer, bufferOffset, destinationOffset, count);
uploadBuffer.CopyTo(readOnlyBuffer, sourceOffset, destinationOffset, count);
int[] result = readOnlyBuffer.ToArray(destinationOffset, count);
Assert.AreEqual(result.Length, count);
Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result));
Assert.IsTrue(uploadBuffer.Span.Slice(sourceOffset, count).SequenceEqual(result));
}
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_ReadBackBuffer_Copy_Full(Device device)
public void Allocate_ReadBackBuffer_CopyTo_Full(Device device)
{
int[] source = new int[4096];
@@ -152,10 +152,10 @@ public partial class TransferBufferTests
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 0, 4096)]
[Data(128, 0, 2048)]
[Data(0, 128, 2048)]
[Data(97, 33, 512)]
public void Allocate_ReadBackBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count)
[Data(128, 0, 2048)]
[Data(33, 97, 512)]
public void Allocate_ReadBackBuffer_CopyTo_Range(Device device, int sourceOffset, int destinationOffset, int count)
{
int[] source = new int[4096];
@@ -164,9 +164,87 @@ public partial class TransferBufferTests
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
using ReadBackBuffer<int> readBackBuffer = device.Get().AllocateReadBackBuffer<int>(readOnlyBuffer.Length);
readOnlyBuffer.CopyTo(readBackBuffer, bufferOffset, destinationOffset, count);
readOnlyBuffer.CopyTo(readBackBuffer, sourceOffset, destinationOffset, count);
Assert.AreEqual(source.Length, readBackBuffer.Length);
Assert.IsTrue(source.AsSpan(bufferOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count)));
Assert.IsTrue(source.AsSpan(sourceOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count)));
}
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_UploadBuffer_CopyFrom_Full(Device device)
{
using UploadBuffer<int> uploadBuffer = device.Get().AllocateUploadBuffer<int>(4096);
new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer<int>(uploadBuffer.Length);
readOnlyBuffer.CopyFrom(uploadBuffer);
int[] result = readOnlyBuffer.ToArray();
Assert.AreEqual(uploadBuffer.Length, result.Length);
Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result));
}
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 0, 4096)]
[Data(0, 128, 2048)]
[Data(128, 0, 2048)]
[Data(33, 97, 512)]
public void Allocate_UploadBuffer_CopyFrom_Range(Device device, int sourceOffset, int destinationOffset, int count)
{
using UploadBuffer<int> uploadBuffer = device.Get().AllocateUploadBuffer<int>(4096);
new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer<int>(uploadBuffer.Length);
readOnlyBuffer.CopyFrom(uploadBuffer, sourceOffset, destinationOffset, count);
int[] result = readOnlyBuffer.ToArray(destinationOffset, count);
Assert.AreEqual(result.Length, count);
Assert.IsTrue(uploadBuffer.Span.Slice(sourceOffset, count).SequenceEqual(result));
}
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_ReadBackBuffer_CopyFrom_Full(Device device)
{
int[] source = new int[4096];
new Random(42).NextBytes(source.AsSpan().AsBytes());
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
using ReadBackBuffer<int> readBackBuffer = device.Get().AllocateReadBackBuffer<int>(readOnlyBuffer.Length);
readBackBuffer.CopyFrom(readOnlyBuffer);
Assert.AreEqual(source.Length, readBackBuffer.Length);
Assert.IsTrue(source.AsSpan().SequenceEqual(readBackBuffer.Span));
}
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 0, 4096)]
[Data(0, 128, 2048)]
[Data(128, 0, 2048)]
[Data(33, 97, 512)]
public void Allocate_ReadBackBuffer_CopyFrom_Range(Device device, int sourceOffset, int destinationOffset, int count)
{
int[] source = new int[4096];
new Random(42).NextBytes(source.AsSpan().AsBytes());
using ReadOnlyBuffer<int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
using ReadBackBuffer<int> readBackBuffer = device.Get().AllocateReadBackBuffer<int>(readOnlyBuffer.Length);
readBackBuffer.CopyFrom(readOnlyBuffer, sourceOffset, destinationOffset, count);
Assert.AreEqual(source.Length, readBackBuffer.Length);
Assert.IsTrue(source.AsSpan(sourceOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count)));
}
}