diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
index 4a30f2b8..c3f56001 100644
--- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
+++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
@@ -8,29 +8,6 @@ namespace ComputeSharp.Graphics.Helpers
///
internal static class MemoryHelper
{
- ///
- /// Copies the content of a source memory area to a target one.
- ///
- /// 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 size in bytes of each item to copy.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
- void* source,
- void* destination,
- uint offset,
- uint length,
- uint elementSizeInBytes)
- {
- Buffer.MemoryCopy(
- source,
- (byte*)destination + offset * elementSizeInBytes,
- ulong.MaxValue,
- elementSizeInBytes * length);
- }
-
///
/// 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.
@@ -40,19 +17,31 @@ namespace ComputeSharp.Graphics.Helpers
/// 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.
public static unsafe void Copy(
void* source,
void* destination,
uint offset,
uint length,
- uint elementPitchInBytes)
+ uint elementPitchInBytes,
+ bool isPaddingInSourceToo)
where T : unmanaged
{
destination = (byte*)destination + offset * elementPitchInBytes;
- for (int i = 0; i < length; i++)
+ if (isPaddingInSourceToo)
{
- *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ for (int i = 0; i < length; i++)
+ {
+ *(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes];
+ }
+ }
+ else
+ {
+ for (int i = 0; i < length; i++)
+ {
+ *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ }
}
}
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index 14395371..cd4c3629 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -199,12 +199,14 @@ namespace ComputeSharp.Resources
{
GraphicsDevice.ThrowIfDisposed();
- ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
+
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
- if (!source.IsPaddingPresent &&
- source.GraphicsDevice == GraphicsDevice)
+ 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);
diff --git a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
index 5274b1f3..96d673af 100644
--- a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
@@ -3,7 +3,6 @@ using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Exceptions;
-using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Graphics.Resources.Enums;
@@ -11,7 +10,6 @@ using ComputeSharp.Graphics.Resources.Interop;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using Microsoft.Toolkit.Diagnostics;
-using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp
@@ -114,7 +112,8 @@ namespace ComputeSharp
resource.Pointer,
(uint)offset,
(uint)size,
- (uint)GetPaddedSize());
+ (uint)GetPaddedSize(),
+ isPaddingInSourceToo: false);
}
else
{
@@ -133,18 +132,37 @@ namespace ComputeSharp
{
GraphicsDevice.ThrowIfDisposed();
- ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
+
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
- if (source is ConstantBuffer &&
- source.GraphicsDevice == GraphicsDevice)
- {
- // Directly copy the input buffer, if possible
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
- copyCommandList.ExecuteAndWaitForCompletion();
+ if (source is 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);
+ }
}
else CopyFromWithCpuBuffer(source);
}