diff --git a/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs
index 71ba72da..8b9597c3 100644
--- a/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs
+++ b/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs
@@ -9,87 +9,109 @@ namespace ComputeSharp.Graphics.Helpers;
internal static class MemoryHelper
{
///
- /// Copies the content of a source memory area to a target one, accounting for padding.
- /// The destination memory area has padding for each element, while the source does not.
+ /// Copies the content of a source memory area to a target one, accounting for padding if present.
///
+ /// The type of elements being copied.
/// The source memory area to read from.
/// The pointer for the destination memory area.
- /// The destination offset to start writing data to.
- /// The number of items to copy.
- /// The padded size of each element.
- /// Indicates whether or not the source is padded too.
+ /// The source offset to start reading data from.
+ /// The destination offset to start writing data to.
+ /// The padded size of each element in .
+ /// The padded size of each element in .
+ /// The number of items to copy from to .
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy(
void* source,
void* destination,
- uint offset,
- uint length,
- uint elementPitchInBytes,
- bool isPaddingInSourceToo)
+ uint sourceElementOffset,
+ uint destinationElementOffset,
+ uint sourceElementPitchInBytes,
+ uint destinationElementPitchInBytes,
+ uint count)
where T : unmanaged
{
- destination = (byte*)destination + offset * elementPitchInBytes;
+ // Offset adjustment
+ source = (byte*)source + sourceElementOffset * sourceElementPitchInBytes;
+ destination = (byte*)destination + destinationElementOffset * destinationElementPitchInBytes;
- if (isPaddingInSourceToo)
+ if (sourceElementPitchInBytes == (uint)sizeof(T))
{
- for (int i = 0; i < length; i++)
+ if (destinationElementPitchInBytes == (uint)sizeof(T))
{
- *(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes];
+ // Neither source nor destination have padding
+ Buffer.MemoryCopy(
+ source: source,
+ destination: destination,
+ destinationSizeInBytes: ulong.MaxValue,
+ sourceBytesToCopy: count * (uint)sizeof(T));
}
+ else
+ {
+ // Source has no padding, destination does
+ static void Copy(
+ void* source,
+ void* destination,
+ uint destinationElementPitchInBytes,
+ uint count)
+ {
+ void* sourceEnd = (byte*)source + count * sizeof(T);
+
+ while (source < sourceEnd)
+ {
+ *(T*)destination = *(T*)source;
+
+ source = (byte*)source + sizeof(T);
+ destination = (byte*)destination + destinationElementPitchInBytes;
+ }
+ }
+
+ Copy(source, destination, destinationElementPitchInBytes, count);
+ }
+ }
+ else if (destinationElementPitchInBytes == (uint)sizeof(T))
+ {
+ // Source has padding, destination does not
+ static void Copy(
+ void* source,
+ void* destination,
+ uint sourceElementPitchInBytes,
+ uint count)
+ {
+ void* sourceEnd = (byte*)source + count * sourceElementPitchInBytes;
+
+ while (source < sourceEnd)
+ {
+ *(T*)destination = *(T*)source;
+
+ source = (byte*)source + sourceElementPitchInBytes;
+ destination = (byte*)destination + sizeof(T);
+ }
+ }
+
+ Copy(source, destination, sourceElementPitchInBytes, count);
}
else
{
- for (int i = 0; i < length; i++)
+ // Both source and destination have padding
+ static void Copy(
+ void* source,
+ void* destination,
+ uint sourceElementPitchInBytes,
+ uint destinationElementPitchInBytes,
+ uint count)
{
- *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ void* sourceEnd = (byte*)source + count * sourceElementPitchInBytes;
+
+ while (source < sourceEnd)
+ {
+ *(T*)destination = *(T*)source;
+
+ source = (byte*)source + sourceElementPitchInBytes;
+ destination = (byte*)destination + destinationElementPitchInBytes;
+ }
}
- }
- }
- ///
- /// Copies the content of a source memory area to a target one.
- ///
- /// The source memory area to read from.
- /// The source offset to start reading data from.
- /// The number of items to copy.
- /// The size in bytes of each item to copy.
- /// The pointer for the destination memory area.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
- void* source,
- uint offset,
- uint length,
- uint elementSizeInBytes,
- void* destination)
- {
- Buffer.MemoryCopy(
- (byte*)source + offset * elementSizeInBytes,
- destination,
- ulong.MaxValue,
- elementSizeInBytes * length);
- }
-
- ///
- /// Copies the content of a source memory area to a target one, accounting for padding.
- /// The source memory area has padding for each element, while the destination does not.
- ///
- /// The source memory area to read from.
- /// The source offset to start reading data from.
- /// The number of items to copy.
- /// The padded size of each element.
- /// The pointer for the destination memory area.
- public static unsafe void Copy(
- void* source,
- uint offset,
- uint length,
- uint elementPitchInBytes,
- void* destination)
- where T : unmanaged
- {
- source = (byte*)source + offset * elementPitchInBytes;
-
- for (int i = 0; i < length; i++)
- {
- ((T*)destination)[i] = *(T*)&((byte*)source)[i * elementPitchInBytes];
+ Copy(source, destination, sourceElementPitchInBytes, destinationElementPitchInBytes, count);
}
}
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index 902ed07f..99bb48e4 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -45,12 +45,14 @@ public abstract class StructuredBuffer : Buffer
fixed (void* destinationPointer = &destination)
{
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)offset,
- (uint)length,
- (uint)sizeof(T),
- destinationPointer);
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: (uint)offset,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
}
else
@@ -70,12 +72,14 @@ public abstract class StructuredBuffer : Buffer
fixed (void* destinationPointer = &destination)
{
- MemoryHelper.Copy(
- resource.Pointer,
- 0u,
- (uint)length,
- (uint)sizeof(T),
- destinationPointer);
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
}
}
@@ -105,12 +109,14 @@ public abstract class StructuredBuffer : Buffer
{
using ID3D12ResourceMap resource = D3D12Resource->Map();
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)offset,
- (uint)length,
- (uint)sizeof(T),
- destination.MappedData + destinationOffset);
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destination.MappedData,
+ sourceElementOffset: (uint)offset,
+ destinationElementOffset: (uint)destinationOffset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
else
{
@@ -141,12 +147,14 @@ public abstract class StructuredBuffer : Buffer
fixed (void* sourcePointer = &source)
{
- MemoryHelper.Copy(
- sourcePointer,
- (uint)offset,
- (uint)length,
- (uint)sizeof(T),
- resource.Pointer);
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: (uint)offset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
}
else
@@ -159,12 +167,14 @@ public abstract class StructuredBuffer : Buffer
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
fixed (void* sourcePointer = &source)
{
- MemoryHelper.Copy(
- sourcePointer,
- 0u,
- (uint)length,
- (uint)sizeof(T),
- resource.Pointer);
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
@@ -199,12 +209,14 @@ public abstract class StructuredBuffer : Buffer
{
using ID3D12ResourceMap resource = D3D12Resource->Map();
- MemoryHelper.Copy(
- source.MappedData,
- (uint)sourceOffset,
- (uint)length,
- (uint)sizeof(T),
- (T*)resource.Pointer + offset);
+ MemoryHelper.Copy(
+ source: source.MappedData,
+ destination: resource.Pointer,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: (uint)offset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
}
else
{
diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
index 7432bf43..77fd7d81 100644
--- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
@@ -70,24 +70,14 @@ public sealed class ConstantBuffer : Buffer
fixed (void* destinationPointer = &destination)
{
- if (IsPaddingPresent)
- {
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)offset,
- (uint)size,
- (uint)GetPaddedSize(),
- destinationPointer);
- }
- else
- {
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)offset,
- (uint)size,
- (uint)sizeof(T),
- destinationPointer);
- }
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: (uint)offset,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)GetPaddedSize(),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)size);
}
}
@@ -105,25 +95,14 @@ public sealed class ConstantBuffer : Buffer
fixed (void* sourcePointer = &source)
{
- if (IsPaddingPresent)
- {
- MemoryHelper.Copy(
- sourcePointer,
- resource.Pointer,
- (uint)offset,
- (uint)size,
- (uint)GetPaddedSize(),
- isPaddingInSourceToo: false);
- }
- else
- {
- MemoryHelper.Copy(
- sourcePointer,
- (uint)offset,
- (uint)size,
- (uint)sizeof(T),
- resource.Pointer);
- }
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: (uint)offset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)GetPaddedSize(),
+ count: (uint)size);
}
}
@@ -144,25 +123,14 @@ public sealed class ConstantBuffer : Buffer
using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
- if (IsPaddingPresent)
- {
- MemoryHelper.Copy(
- sourceMap.Pointer,
- destinationMap.Pointer,
- 0,
- (uint)source.Length,
- (uint)GetPaddedSize(),
- isPaddingInSourceToo: true);
- }
- else
- {
- MemoryHelper.Copy(
- sourceMap.Pointer,
- 0,
- (uint)source.Length,
- (uint)sizeof(T),
- destinationMap.Pointer);
- }
+ 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);
}