diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs
new file mode 100644
index 00000000..ba0f8009
--- /dev/null
+++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs
@@ -0,0 +1,36 @@
+using ComputeSharp.Resources;
+
+namespace ComputeSharp;
+
+///
+/// A that contains extension methods for the type.
+///
+public static class ReadBackBufferExtensions
+{
+ ///
+ /// Reads the contents of a instance and writes them into a target instance.
+ ///
+ /// The type of items stored on the buffer.
+ /// The target instance to write data to.
+ /// The input instance to read data from.
+ public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source)
+ where T : unmanaged
+ {
+ source.CopyTo(destination, 0, 0, source.Length);
+ }
+
+ ///
+ /// Reads the contents of a instance and writes them into a target instance.
+ ///
+ /// The type of items stored on the buffer.
+ /// The target instance to write data to.
+ /// The input instance to read data from.
+ /// The offset to start reading data from.
+ /// The starting offset within to write data to.
+ /// The number of items to read.
+ public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count)
+ where T : unmanaged
+ {
+ source.CopyTo(destination, sourceOffset, destinationOffset, count);
+ }
+}
diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs
index 47cc9a66..1a933637 100644
--- a/tests/ComputeSharp.Tests/TransferBufferTests.cs
+++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs
@@ -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 uploadBuffer = device.Get().AllocateUploadBuffer(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 uploadBuffer = device.Get().AllocateUploadBuffer(4096);
@@ -124,17 +124,17 @@ public partial class TransferBufferTests
using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(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 readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(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 uploadBuffer = device.Get().AllocateUploadBuffer(4096);
+
+ new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
+
+ using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(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 uploadBuffer = device.Get().AllocateUploadBuffer(4096);
+
+ new Random(42).NextBytes(uploadBuffer.Span.AsBytes());
+
+ using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(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 readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
+ using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(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 readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source);
+ using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(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)));
}
}