diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs
index f4fc22e9..ce6cba9f 100644
--- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs
@@ -99,6 +99,33 @@ public static class BufferExtensions
buffer.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, offset);
}
+ ///
+ /// Reads the contents of the specified instance and copies them to the target instance.
+ ///
+ /// The type of items stored on the buffers.
+ /// The input instance to read data from.
+ /// The target instance to write data to.
+ public static void CopyTo(this Buffer buffer, Buffer destination)
+ where T : unmanaged
+ {
+ buffer.CopyTo(destination, 0, 0, buffer.Length);
+ }
+
+ ///
+ /// Reads the contents of the specified range from a instance and copies them to the target instance.
+ ///
+ /// The type of items stored on the buffers.
+ /// The input instance to read data from.
+ /// The target instance to write data to.
+ /// The offset to start reading data from.
+ /// The starting offset within to write data to.
+ /// The number of items to read.
+ public static void CopyTo(this Buffer buffer, Buffer destination, int sourceOffset, int destinationOffset, int count)
+ where T : unmanaged
+ {
+ buffer.CopyTo(destination, sourceOffset, destinationOffset, count);
+ }
+
///
/// Writes the contents of a given array to a instance.
///
@@ -152,4 +179,31 @@ public static class BufferExtensions
{
buffer.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, offset);
}
+
+ ///
+ /// Writes the contents of the specified instance to the curreent instance.
+ ///
+ /// The type of items stored on the buffers.
+ /// The input instance to write data data to.
+ /// The source instance to read data from.
+ public static void CopyFrom(this Buffer buffer, Buffer source)
+ where T : unmanaged
+ {
+ source.CopyTo(buffer, 0, 0, source.Length);
+ }
+
+ ///
+ /// Writes the contents of the specified range from a instance and copies them to the current instance.
+ ///
+ /// The type of items stored on the buffers.
+ /// The input instance to write data to.
+ /// The source 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 Buffer buffer, Buffer source, int sourceOffset, int destinationOffset, int count)
+ where T : unmanaged
+ {
+ source.CopyTo(buffer, sourceOffset, destinationOffset, count);
+ }
}
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
index 03f967c7..8a245512 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
@@ -131,34 +131,32 @@ public unsafe abstract class Buffer : NativeObject
internal abstract void CopyTo(ref T destination, int length, int offset);
///
- /// Writes the contents of a given memory area to a specified area of the current instance.
+ /// Writes the contents of a given range from the current instance and writes them into a target instance.
///
- /// The input memory area to read data from.
- /// The length of the input memory area to read data from.
- /// The offset to start writing data to.
- internal abstract void CopyFrom(ref T source, int length, int offset);
+ /// The target instance to write data to.
+ /// The offset to start reading data from.
+ /// The starting offset within to write data to.
+ /// The number of items to read.
+ internal abstract void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length);
///
- /// Writes the contents of a given to the current instance.
+ /// Writes the contents of a given range from the current instance and writes them into a target instance, using a temporary CPU buffer.
///
- /// The input to read data from.
- public abstract void CopyFrom(Buffer source);
-
- ///
- /// Writes the contents of a given to the current instance, using a temporary CPU buffer.
- ///
- /// The input to read data from.
- protected void CopyFromWithCpuBuffer(Buffer source)
+ /// The target instance to write data to.
+ /// The offset to start reading data from.
+ /// The starting offset within to write data to.
+ /// The number of items to read.
+ protected void CopyToWithCpuBuffer(Buffer destination, int sourceOffset, int destinationOffset, int length)
{
- T[] array = ArrayPool.Shared.Rent(source.Length);
+ T[] array = ArrayPool.Shared.Rent(length);
try
{
ref T r0 = ref MemoryMarshal.GetArrayDataReference(array);
- source.CopyTo(ref r0, source.Length, 0);
+ CopyTo(ref r0, length, sourceOffset);
- CopyFrom(ref r0, source.Length, 0);
+ destination.CopyFrom(ref r0, length, destinationOffset);
}
finally
{
@@ -166,6 +164,14 @@ public unsafe abstract class Buffer : NativeObject
}
}
+ ///
+ /// Writes the contents of a given memory area to a specified area of the current instance.
+ ///
+ /// The input memory area to read data from.
+ /// The length of the input memory area to read data from.
+ /// The offset to start writing data to.
+ internal abstract void CopyFrom(ref T source, int length, int offset);
+
///
protected override void OnDispose()
{
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index 99bb48e4..30a01823 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -36,8 +36,9 @@ public abstract class StructuredBuffer : Buffer
ThrowIfDisposed();
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
Guard.IsInRange(offset, 0, Length, nameof(offset));
- Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
if (GraphicsDevice.IsCacheCoherentUMA)
{
@@ -84,6 +85,44 @@ public abstract class StructuredBuffer : Buffer
}
}
+ ///
+ internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length)
+ {
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+ destination.ThrowIfDisposed();
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
+ Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length));
+ Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset));
+ Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset));
+ Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
+
+ if (!destination.IsPaddingPresent)
+ {
+ nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
+ nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
+ nint byteLength = length * sizeof(T);
+
+ // Directly copy the input buffer
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
+ destination.D3D12Resource,
+ (ulong)destinationByteOffset,
+ D3D12Resource,
+ (ulong)sourceByteOffset,
+ (ulong)byteLength);
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+ else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
+ }
+
///
/// Reads the contents of the specified range from the current instance and writes them into a target instance.
///
@@ -138,8 +177,9 @@ public abstract class StructuredBuffer : Buffer
ThrowIfDisposed();
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
Guard.IsInRange(offset, 0, Length, nameof(offset));
- Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
if (GraphicsDevice.IsCacheCoherentUMA)
{
@@ -230,27 +270,4 @@ public abstract class StructuredBuffer : Buffer
copyCommandList.ExecuteAndWaitForCompletion();
}
}
-
- ///
- public override unsafe void CopyFrom(Buffer source)
- {
- GraphicsDevice.ThrowIfDisposed();
-
- ThrowIfDisposed();
-
- source.ThrowIfDeviceMismatch(GraphicsDevice);
- source.ThrowIfDisposed();
-
- Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
-
- if (!source.IsPaddingPresent)
- {
- // Directly copy the input buffer
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
-
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
- copyCommandList.ExecuteAndWaitForCompletion();
- }
- else CopyFromWithCpuBuffer(source);
- }
}
diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
index 77fd7d81..87a9e671 100644
--- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
@@ -57,14 +57,15 @@ public sealed class ConstantBuffer : Buffer
}
///
- internal override unsafe void CopyTo(ref T destination, int size, int offset)
+ internal override unsafe void CopyTo(ref T destination, int length, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
Guard.IsInRange(offset, 0, Length, nameof(offset));
- Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
using ID3D12ResourceMap resource = D3D12Resource->Map();
@@ -77,19 +78,54 @@ public sealed class ConstantBuffer : Buffer
destinationElementOffset: 0,
sourceElementPitchInBytes: (uint)GetPaddedSize(),
destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)size);
+ count: (uint)length);
}
}
///
- internal override unsafe void CopyFrom(ref T source, int size, int offset)
+ internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+ destination.ThrowIfDisposed();
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
+ Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length));
+ Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset));
+ Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset));
+ Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
+
+ if (destination is ConstantBuffer buffer)
+ {
+ using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
+ using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
+
+ MemoryHelper.Copy(
+ source: sourceMap.Pointer,
+ destination: destinationMap.Pointer,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: (uint)destinationOffset,
+ sourceElementPitchInBytes: (uint)GetPaddedSize(),
+ destinationElementPitchInBytes: (uint)GetPaddedSize(),
+ count: (uint)length);
+ }
+ else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
+ }
+
+ ///
+ internal override unsafe void CopyFrom(ref T source, int length, int offset)
+ {
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
Guard.IsInRange(offset, 0, Length, nameof(offset));
- Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
using ID3D12ResourceMap resource = D3D12Resource->Map();
@@ -102,39 +138,10 @@ public sealed class ConstantBuffer : Buffer
destinationElementOffset: (uint)offset,
sourceElementPitchInBytes: (uint)sizeof(T),
destinationElementPitchInBytes: (uint)GetPaddedSize(),
- count: (uint)size);
+ count: (uint)length);
}
}
- ///
- public override unsafe void CopyFrom(Buffer source)
- {
- GraphicsDevice.ThrowIfDisposed();
-
- ThrowIfDisposed();
-
- source.ThrowIfDeviceMismatch(GraphicsDevice);
- source.ThrowIfDisposed();
-
- Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
-
- if (source is ConstantBuffer buffer)
- {
- using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
- using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
-
- MemoryHelper.Copy(
- source: sourceMap.Pointer,
- destination: destinationMap.Pointer,
- sourceElementOffset: 0,
- destinationElementOffset: 0,
- sourceElementPitchInBytes: (uint)GetPaddedSize(),
- destinationElementPitchInBytes: (uint)GetPaddedSize(),
- count: (uint)source.Length);
- }
- else CopyFromWithCpuBuffer(source);
- }
-
///
public override string ToString()
{
diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs
index 69895f84..2eb9a06e 100644
--- a/tests/ComputeSharp.Tests/BufferTests.cs
+++ b/tests/ComputeSharp.Tests/BufferTests.cs
@@ -224,6 +224,58 @@ public partial class BufferTests
buffer.CopyTo(result, sourceOffset, destinationOffset, count);
}
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ConstantBuffer<>))]
+ [Resource(typeof(ReadOnlyBuffer<>))]
+ [Resource(typeof(ReadWriteBuffer<>))]
+ [AdditionalResource(typeof(ConstantBuffer<>))]
+ [AdditionalResource(typeof(ReadOnlyBuffer<>))]
+ [AdditionalResource(typeof(ReadWriteBuffer<>))]
+ [Data(0, 0, 4096)]
+ [Data(0, 512, 2048)]
+ [Data(512, 0, 2048)]
+ [Data(1024, 127, 587)]
+ [Data(65, 127, 587)]
+ public void CopyTo_BufferToVoid_Ok(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count)
+ {
+ float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
+
+ using Buffer source = device.Get().AllocateBuffer(sourceType, array);
+ using Buffer destination = device.Get().AllocateBuffer(destinationType, 4096);
+
+ source.CopyTo(destination, sourceOffset, destinationOffset, count);
+
+ float[] result = destination.ToArray();
+
+ Assert.IsTrue(array.AsSpan(sourceOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count)));
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ConstantBuffer<>))]
+ [Resource(typeof(ReadOnlyBuffer<>))]
+ [Resource(typeof(ReadWriteBuffer<>))]
+ [AdditionalResource(typeof(ConstantBuffer<>))]
+ [AdditionalResource(typeof(ReadOnlyBuffer<>))]
+ [AdditionalResource(typeof(ReadWriteBuffer<>))]
+ [Data(0, 0, 8196)]
+ [Data(0, -12, 1024)]
+ [Data(-56, 0, 1024)]
+ [Data(0, 512, 4096)]
+ [Data(1024, 12, 3600)]
+ [Data(1024, 12, -2096)]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyTo_BufferToVoid_Fail(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count)
+ {
+ float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
+
+ using Buffer source = device.Get().AllocateBuffer(sourceType, array);
+ using Buffer destination = device.Get().AllocateBuffer(destinationType, 4096);
+
+ source.CopyTo(destination, sourceOffset, destinationOffset, count);
+ }
+
[CombinatorialTestMethod]
[AllDevices]
public void Dispatch_ConstantBuffer(Device device)