From 96232a95384fdfc158a2edfc7b5b412c5d47de71 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 21:15:33 +0200 Subject: [PATCH] Readonly buffer optimized when no padding is needed --- .../Buffers/ReadOnlyBuffer{T}.cs | 82 ++++++++++++------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 2ff26f04..04a7d5c9 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -38,46 +38,72 @@ namespace ComputeSharp.Graphics.Buffers /// public override void GetData(Span span) { - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(); - - Map(0); - MemoryHelper.Copy(MappedResource, temporarySpan); - Unmap(0); - - ref byte tin = ref temporarySpan.GetPinnableReference(); - ref T tout = ref span.GetPinnableReference(); - - for (int i = 0; i < Size; i++) + // Directly copy the data back if there is no padding + if (PaddedElementSizeInBytes == ElementSizeInBytes) { - ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes); - Unsafe.Add(ref tout, i) = Unsafe.As(ref rsource); + Map(0); + MemoryHelper.Copy(MappedResource, span); + Unmap(0); } + else + { + // Create the temporary array + byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(); - ArrayPool.Shared.Return(temporaryArray); + // Copy the padded data to the temporary array + Map(0); + MemoryHelper.Copy(MappedResource, temporarySpan); + Unmap(0); + + 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 < Size; i++) + { + ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes); + Unsafe.Add(ref tout, i) = Unsafe.As(ref rsource); + } + + ArrayPool.Shared.Return(temporaryArray); + } } /// public override void SetData(Span span) { - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer - ref T tin = ref span.GetPinnableReference(); - ref byte tout = ref temporarySpan.GetPinnableReference(); - - temporarySpan.Clear(); - - for (int i = 0; i < Size; i++) + // Directly copy the input span if there is no padding + if (PaddedElementSizeInBytes == ElementSizeInBytes) { - ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes); - Unsafe.As(ref rtarget) = Unsafe.Add(ref tin, i); + Map(0); + MemoryHelper.Copy(span, MappedResource); + Unmap(0); } + else + { + // Create the temporary array + byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer + ref T tin = ref span.GetPinnableReference(); + ref byte tout = ref temporarySpan.GetPinnableReference(); - Map(0); - MemoryHelper.Copy(temporarySpan, MappedResource); - Unmap(0); + temporarySpan.Clear(); - ArrayPool.Shared.Return(temporaryArray); + // Copy the input data to the temporary array and add the padding + for (int i = 0; i < Size; i++) + { + ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes); + Unsafe.As(ref rtarget) = Unsafe.Add(ref tin, i); + } + + // Copy the padded data to the GPU + Map(0); + MemoryHelper.Copy(temporarySpan, MappedResource); + Unmap(0); + + ArrayPool.Shared.Return(temporaryArray); + } } } }