diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
index 14371062..75cd89cc 100644
--- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
@@ -1,5 +1,4 @@
using System;
-using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -24,16 +23,18 @@ namespace ComputeSharp
///
/// The associated with the current instance
/// The number of items to store in the current buffer
- internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * GetPaddedSize() * 16, BufferType.Constant) { }
+ internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * GetPaddedSize(), BufferType.Constant) { }
///
/// Gets the right padded size for elements to store in the current instance
///
[Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetPaddedSize()
{
int size = Unsafe.SizeOf();
- return size % 16 == 0 ? 1 : size / 16 + 1;
+
+ return (size + 15) & ~15;
}
///
@@ -44,74 +45,49 @@ namespace ComputeSharp
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer)}[int]");
///
- public override void GetData(Span span, int offset, int count)
+ public override unsafe void GetData(Span span, int offset, int count)
{
+ using MappedResource resource = MapResource();
+
if (IsPaddingPresent)
{
- // Create the temporary array
- byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes);
- Span temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes);
+ ref T spanRef = ref MemoryMarshal.GetReference(span);
+ ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer);
- // Copy the padded data to the temporary array
- using (MappedResource resource = MapResource())
+ // Move to the initial ref
+ resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
+
+ for (var i = 0; i < count; i++)
{
- MemoryHelper.Copy(resource.Pointer, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes);
+ ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
+
+ Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef);
}
-
- ref byte tin = ref temporarySpan.GetPinnableReference();
- ref T tout = ref span.GetPinnableReference();
-
- // Copy the padded data to the target span, removing the padding
- for (int i = 0; i < count; i++)
- {
- ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes);
- Unsafe.Add(ref tout, i) = Unsafe.As(ref rsource);
- }
-
- ArrayPool.Shared.Return(temporaryArray);
- }
- else
- {
- // Directly copy the data back if there is no padding
- using MappedResource resource = MapResource();
-
- MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
}
+ else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
}
///
- public override void SetData(ReadOnlySpan span, int offset, int count)
+ public override unsafe void SetData(ReadOnlySpan span, int offset, int count)
{
+ using MappedResource resource = MapResource();
+
if (IsPaddingPresent)
{
- // Create the temporary array
- byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes);
- ReadOnlySpan temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes); // Array pool arrays can be longer
- ref T tin = ref MemoryMarshal.GetReference(span);
- ref byte tout = ref MemoryMarshal.GetReference(temporarySpan);
+ ref T spanRef = ref MemoryMarshal.GetReference(span);
+ ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer);
- // Copy the input data to the temporary array and add the padding
- for (int i = 0; i < count; i++)
+ // Move to the initial offset
+ resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
+
+ for (var i = 0; i < count; i++)
{
- ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes);
- Unsafe.As(ref rtarget) = Unsafe.Add(ref tin, i);
+ ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
+
+ Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i);
}
-
- // Copy the padded data to the GPU
- using (MappedResource resource = MapResource())
- {
- MemoryHelper.Copy(temporarySpan, resource.Pointer, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes);
- }
-
- ArrayPool.Shared.Return(temporaryArray);
- }
- else
- {
- // Directly copy the input span if there is no padding
- using MappedResource resource = MapResource();
-
- MemoryHelper.Copy(span, resource.Pointer, offset, count);
}
+ else MemoryHelper.Copy(span, resource.Pointer, offset, count);
}
///