diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs
index 41926a7a..78fad45f 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs
@@ -52,8 +52,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
-
- MemoryHelper.Copy(resource.Pointer, 0, destination);
+ fixed (void* destinationPointer = destination)
+ {
+ MemoryHelper.Copy(
+ resource.Pointer,
+ 0u,
+ (uint)destination.Length,
+ (uint)sizeof(T),
+ destinationPointer);
+ }
}
///
@@ -73,8 +80,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ fixed (void* sourcePointer = source)
{
- MemoryHelper.Copy(source, resource.Pointer, 0);
+ MemoryHelper.Copy(
+ sourcePointer,
+ 0u,
+ (uint)source.Length,
+ (uint)sizeof(T),
+ resource.Pointer);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
index a9e40779..5935c197 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
@@ -274,13 +274,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
- MemoryHelper.Copy(
- resource.Pointer,
- width,
- height,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprint.Footprint.RowPitch,
- destination);
+ fixed (void* destinationPointer = destination)
+ {
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)height,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprint.Footprint.RowPitch,
+ destinationPointer);
+ }
}
///
@@ -415,12 +417,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ fixed (void* sourcePointer = source)
{
MemoryHelper.Copy(
- source,
+ sourcePointer,
resource.Pointer,
- width,
- height,
+ (uint)height,
rowSizeInBytes,
d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
}
diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
index 58797a40..2ee65640 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
@@ -269,15 +269,17 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
- MemoryHelper.Copy(
- resource.Pointer,
- width,
- height,
- depth,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprint.Footprint.RowPitch,
- d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height,
- destination);
+ fixed (void* destinationPointer = destination)
+ {
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)height,
+ (uint)depth,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprint.Footprint.RowPitch,
+ d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height,
+ destinationPointer);
+ }
}
///
@@ -430,13 +432,13 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ fixed (void* sourcePointer = source)
{
MemoryHelper.Copy(
- source,
+ sourcePointer,
resource.Pointer,
- width,
- height,
- depth,
+ (uint)height,
+ (uint)depth,
rowSizeInBytes,
d3D12PlacedSubresourceFootprint.Footprint.RowPitch,
d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height);
diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
index 2789b764..16c005bb 100644
--- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
@@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
@@ -64,24 +63,27 @@ namespace ComputeSharp
Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination));
using ID3D12ResourceMap resource = D3D12Resource->Map();
-
- if (IsPaddingPresent)
+ fixed (void* destinationPointer = destination)
{
- int count = destination.Length;
- ref T spanRef = ref MemoryMarshal.GetReference(destination);
- ref byte resourceRef = ref Unsafe.AsRef(resource.Pointer);
-
- // Move to the initial ref
- resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
-
- for (var i = 0; i < count; i++)
+ if (IsPaddingPresent)
{
- ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
-
- Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef);
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)offset,
+ (uint)destination.Length,
+ (uint)GetPaddedSize(),
+ destinationPointer);
+ }
+ else
+ {
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)offset,
+ (uint)destination.Length,
+ (uint)sizeof(T),
+ destinationPointer);
}
}
- else MemoryHelper.Copy(resource.Pointer, offset, destination);
}
///
@@ -95,24 +97,27 @@ namespace ComputeSharp
Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source));
using ID3D12ResourceMap resource = D3D12Resource->Map();
-
- if (IsPaddingPresent)
+ fixed (void* sourcePointer = source)
{
- int count = source.Length;
- ref T spanRef = ref MemoryMarshal.GetReference(source);
- ref byte resourceRef = ref Unsafe.AsRef(resource.Pointer);
-
- // Move to the initial offset
- resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
-
- for (var i = 0; i < count; i++)
+ if (IsPaddingPresent)
{
- ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
-
- Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i);
+ MemoryHelper.Copy(
+ sourcePointer,
+ resource.Pointer,
+ (uint)offset,
+ (uint)source.Length,
+ (uint)GetPaddedSize());
+ }
+ else
+ {
+ MemoryHelper.Copy(
+ sourcePointer,
+ (uint)offset,
+ (uint)source.Length,
+ (uint)sizeof(T),
+ resource.Pointer);
}
}
- else MemoryHelper.Copy(source, resource.Pointer, offset);
}
///
@@ -128,7 +133,7 @@ namespace ComputeSharp
source.GraphicsDevice == GraphicsDevice)
{
// Directly copy the input buffer, if possible
- using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
index a9a4c4fb..4842f413 100644
--- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
+++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
@@ -9,192 +9,252 @@ namespace ComputeSharp.Graphics.Helpers
internal static class MemoryHelper
{
///
- /// Copies the content of a to the area pointed by an input pointer.
+ /// Copies the content of a source memory area to a target one.
///
- /// The type of values in the input .
- /// The source to read.
+ /// The source memory area to read from.
/// The pointer for the destination memory area.
- /// The destination offset to start writing data to.
+ /// The destination offset to start writing data to.
+ /// The number of items to copy.
+ /// The size in bytes of each item to copy.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(ReadOnlySpan source, void* destination, int destinationOffset)
- where T : unmanaged
+ public static unsafe void Copy(
+ void* source,
+ void* destination,
+ uint offset,
+ uint length,
+ uint elementSizeInBytes)
{
- source.CopyTo(new Span((T*)destination + destinationOffset, source.Length));
+ Buffer.MemoryCopy(
+ source,
+ (byte*)destination + offset * elementSizeInBytes,
+ ulong.MaxValue,
+ elementSizeInBytes * length);
}
///
- /// Copies a memory area pointed by a pointer to a target .
+ /// 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.
///
- /// The type of values to read.
- /// The pointer that indicates the memory area to read from.
- /// The source offset to start reading data from.
- /// The destination to write.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(void* source, int sourceOffset, Span destination)
+ /// 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.
+ public static unsafe void Copy(
+ void* source,
+ void* destination,
+ uint offset,
+ uint length,
+ uint elementPitchInBytes)
where T : unmanaged
{
- new Span((T*)source + sourceOffset, destination.Length).CopyTo(destination);
+ destination = (byte*)destination + offset * elementPitchInBytes;
+
+ for (int i = 0; i < length; i++)
+ {
+ *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ }
}
///
- /// Copies the content of a to the 2D area pointed by an input pointer.
+ /// Copies the content of a source memory area to a target one.
///
- /// The type of values to read.
- /// The destination to read from.
+ /// 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];
+ }
+ }
+
+ ///
+ /// Copies the content of a source memory area to the 2D area pointed by an input pointer.
+ /// The destination memory area has padding in each row, while the source does not.
+ ///
+ /// The source pointer to read from.
/// The pointer that indicates the 2D memory area to write to.
- /// The width of the 2D memory area to write to.
/// The height of the 2D memory area to write to.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
- ReadOnlySpan source,
+ public static unsafe void Copy(
+ void* source,
void* destination,
- int width,
- int height,
+ uint height,
ulong widthInBytes,
ulong pitchInBytes)
- where T : unmanaged
{
if (widthInBytes == pitchInBytes)
{
- source.CopyTo(new Span(destination, width * height));
+ Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height);
}
else
{
for (int i = 0; i < height; i++)
{
- ReadOnlySpan sourceRow = source.Slice(i * width, width);
- Span destinationRow = new((byte*)destination + (uint)i * pitchInBytes, width);
-
- sourceRow.CopyTo(destinationRow);
+ Buffer.MemoryCopy(
+ (byte*)source + (uint)i * widthInBytes,
+ (byte*)destination + (uint)i * pitchInBytes,
+ ulong.MaxValue,
+ widthInBytes);
}
}
}
///
- /// Copies a 2D memory area pointed by a pointer value to a target .
+ /// Copies a 2D memory area pointed by a pointer value to a target memory area.
+ /// The source memory area has padding in each row, while the target does not.
///
- /// The type of values to read.
/// The pointer that indicates the memory area to read from.
- /// The width of the 2D memory area to read.
/// The height of the 2D memory area to read.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
- /// The destination to write.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
+ /// The destination pointer to write to.
+ public static unsafe void Copy(
void* source,
- int width,
- int height,
+ uint height,
ulong widthInBytes,
ulong pitchInBytes,
- Span destination)
- where T : unmanaged
+ void* destination)
{
if (widthInBytes == pitchInBytes)
{
- new Span(source, width * height).CopyTo(destination);
+ Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height);
}
else
{
for (int i = 0; i < height; i++)
{
- ReadOnlySpan sourceRow = new((byte*)source + (uint)i * pitchInBytes, width);
- Span destinationRow = destination.Slice(i * width, width);
-
- sourceRow.CopyTo(destinationRow);
+ Buffer.MemoryCopy(
+ (byte*)source + (uint)i * pitchInBytes,
+ (byte*)destination + (uint)i * widthInBytes,
+ ulong.MaxValue,
+ widthInBytes);
}
}
}
///
- /// Copies the content of a to the 3D area pointed by an input pointer.
+ /// Copies the content of a source memory area to the 3D area pointed by an input pointer.
+ /// The destination memory area has padding in each row, while the source does not.
///
/// The type of values to read.
- /// The destination to read from.
+ /// The source memory area to read from.
/// The pointer that indicates the 3D memory area to write to.
- /// The width of the 3D memory area to write to.
/// The height of the 3D memory area to write to.
/// The depth of the 3D memory area to write to.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
/// The size in bytes of each 2D slice within the target 3D memory area.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
- ReadOnlySpan source,
+ public static unsafe void Copy(
+ void* source,
void* destination,
- int width,
- int height,
- int depth,
+ uint height,
+ uint depth,
ulong widthInBytes,
ulong pitchInBytes,
ulong sliceInBytes)
- where T : unmanaged
{
if (widthInBytes == pitchInBytes)
{
- source.CopyTo(new Span(destination, width * height * depth));
+ Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth);
}
else
{
for (int z = 0; z < depth; z++)
{
- int sourceZOffset = width * height * z;
- ulong destinationZOffset = sliceInBytes * (uint)z;
+ ulong
+ sourceZOffset = (uint)z * widthInBytes * height,
+ destinationZOffset = (uint)z * sliceInBytes;
for (int i = 0; i < height; i++)
{
- ReadOnlySpan sourceRow = source.Slice(sourceZOffset + i * width, width);
- Span destinationRow = new((byte*)destination + destinationZOffset + (uint)i * pitchInBytes, width);
-
- sourceRow.CopyTo(destinationRow);
+ Buffer.MemoryCopy(
+ (byte*)source + sourceZOffset + (uint)i * widthInBytes,
+ (byte*)destination + destinationZOffset + (uint)i * pitchInBytes,
+ ulong.MaxValue,
+ widthInBytes);
}
}
}
}
///
- /// Copies a 3D memory area pointed by a pointer value to a target .
+ /// Copies a 3D memory area pointed by a pointer value to a target memory area.
+ /// The source memory area has padding in each row, while the target does not.
///
/// The type of values to read.
/// The pointer that indicates the memory area to read from.
- /// The width of the 3D memory area to read.
/// The height of the 3D memory area to read.
/// The depth of the 3D memory area to read.
/// The width of the memory area in bytes.
/// The pitch (padded width) of the memory area in bytes.
/// The size in bytes of each 2D slice within the source 3D memory area.
- /// The destination to write.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
+ /// The destination pointer to write.
+ public static unsafe void Copy(
void* source,
- int width,
- int height,
- int depth,
+ uint height,
+ uint depth,
ulong widthInBytes,
ulong pitchInBytes,
ulong sliceInBytes,
- Span destination)
- where T : unmanaged
+ void* destination)
{
if (widthInBytes == pitchInBytes)
{
- new Span(source, width * height * depth).CopyTo(destination);
+ Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth);
}
else
{
for (int z = 0; z < depth; z++)
{
- ulong sourceZOffset = sliceInBytes * (uint)z;
- int destinationZOffset = width * height * z;
+ ulong
+ sourceZOffset = (uint)z * sliceInBytes,
+ destinationZOffset = (uint)z * widthInBytes * height;
for (int i = 0; i < height; i++)
{
- ReadOnlySpan sourceRow = new((byte*)source + sourceZOffset + (uint)i * pitchInBytes, width);
- Span destinationRow = destination.Slice(destinationZOffset + i * width, width);
-
- sourceRow.CopyTo(destinationRow);
+ Buffer.MemoryCopy(
+ (byte*)source + sourceZOffset + (uint)i * pitchInBytes,
+ (byte*)destination + destinationZOffset + (uint)i * widthInBytes,
+ ulong.MaxValue,
+ widthInBytes);
}
}
}