From 874dd5943a45b1cfb7e1cd4aa833c8debccdf930 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 10 Jun 2020 21:27:30 +0100 Subject: [PATCH 1/4] Fix constant buffer copying See #67 --- .../Buffers/ConstantBuffer{T}.cs | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 14371062..d609f74f 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -24,7 +24,7 @@ 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 @@ -33,7 +33,7 @@ namespace ComputeSharp private static int GetPaddedSize() { int size = Unsafe.SizeOf(); - return size % 16 == 0 ? 1 : size / 16 + 1; + return (size + 15) & ~15; } /// @@ -44,31 +44,20 @@ 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) { if (IsPaddingPresent) { - // Create the temporary array - byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes); - // Copy the padded data to the temporary array using (MappedResource resource = MapResource()) { - MemoryHelper.Copy(resource.Pointer, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes); + ref var dest = ref MemoryMarshal.GetReference(span); + byte* untypedSrc = (byte*)resource.Pointer; + for (var i = offset; i < count; i++) + { + Unsafe.Add(ref dest, i) = *(T*)&untypedSrc[i * GetPaddedSize()]; + } } - - 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 { @@ -80,36 +69,23 @@ namespace ComputeSharp } /// - 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); - - // Copy the input data to the temporary array and add the padding - for (int i = 0; i < count; i++) + // Copy element-by-element + ref var src = ref MemoryMarshal.GetReference(span); + byte* untypedDest = (byte*)resource.Pointer; + for (var i = offset; i < count; i++) { - ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes); - Unsafe.As(ref rtarget) = Unsafe.Add(ref tin, i); + *(T*)&untypedDest[i * GetPaddedSize()] = Unsafe.Add(ref src, 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); } } From c613d92413a8e45a044032e9759351ba5b305fa3 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 11 Jun 2020 15:07:21 +0100 Subject: [PATCH 2/4] Resolve offset bug, fix mapped resource location, remove all pointers where possible --- .../Buffers/ConstantBuffer{T}.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index d609f74f..0deb2358 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -30,6 +30,7 @@ namespace ComputeSharp /// 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(); @@ -46,29 +47,27 @@ namespace ComputeSharp /// public override unsafe void GetData(Span span, int offset, int count) { + using MappedResource resource = MapResource(); if (IsPaddingPresent) { // Copy the padded data to the temporary array - using (MappedResource resource = MapResource()) { ref var dest = ref MemoryMarshal.GetReference(span); - byte* untypedSrc = (byte*)resource.Pointer; - for (var i = offset; i < count; i++) + ref byte untypedSrc = ref Unsafe.AsRef((void*)resource.Pointer); + untypedSrc = ref Unsafe.Add(ref untypedSrc, offset * GetPaddedSize()); + + for (var i = 0; i < count; i++) { - Unsafe.Add(ref dest, i) = *(T*)&untypedSrc[i * GetPaddedSize()]; + Unsafe.Add(ref dest, i) = Unsafe.As(ref Unsafe.Add(ref untypedSrc, i * GetPaddedSize())); } } } else { - // Directly copy the data back if there is no padding - using MappedResource resource = MapResource(); - MemoryHelper.Copy(resource.Pointer, offset, span, 0, count); } } - /// /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { @@ -77,10 +76,12 @@ namespace ComputeSharp { // Copy element-by-element ref var src = ref MemoryMarshal.GetReference(span); - byte* untypedDest = (byte*)resource.Pointer; - for (var i = offset; i < count; i++) + ref byte untypedDest = ref Unsafe.AsRef((void*)resource.Pointer); + untypedDest = ref Unsafe.Add(ref untypedDest, offset * GetPaddedSize()); + + for (var i = 0; i < count; i++) { - *(T*)&untypedDest[i * GetPaddedSize()] = Unsafe.Add(ref src, i); + Unsafe.As(ref Unsafe.Add(ref untypedDest, i * GetPaddedSize())) = Unsafe.Add(ref src, i); } } else From 8aa83aae9150af20f1d599d6e1fffc016a3f97eb Mon Sep 17 00:00:00 2001 From: John Date: Thu, 11 Jun 2020 15:19:02 +0100 Subject: [PATCH 3/4] Inline else expression --- src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 0deb2358..4d50c4bd 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -84,11 +84,8 @@ namespace ComputeSharp Unsafe.As(ref Unsafe.Add(ref untypedDest, i * GetPaddedSize())) = Unsafe.Add(ref src, i); } } - else - { - // Directly copy the input span if there is no padding - MemoryHelper.Copy(span, resource.Pointer, offset, count); - } + // Directly copy as there is no padding + else MemoryHelper.Copy(span, resource.Pointer, offset, count); } /// From ec8e0b6b1e861f0794647a01c8f69328bae0ed76 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Jun 2020 16:26:47 +0200 Subject: [PATCH 4/4] Minor code style tweaks --- .../Buffers/ConstantBuffer{T}.cs | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 4d50c4bd..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; @@ -34,6 +33,7 @@ namespace ComputeSharp private static int GetPaddedSize() { int size = Unsafe.SizeOf(); + return (size + 15) & ~15; } @@ -48,43 +48,45 @@ namespace ComputeSharp public override unsafe void GetData(Span span, int offset, int count) { using MappedResource resource = MapResource(); + if (IsPaddingPresent) { - // Copy the padded data to the temporary array - { - ref var dest = ref MemoryMarshal.GetReference(span); - ref byte untypedSrc = ref Unsafe.AsRef((void*)resource.Pointer); - untypedSrc = ref Unsafe.Add(ref untypedSrc, offset * GetPaddedSize()); + ref T spanRef = ref MemoryMarshal.GetReference(span); + ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer); - for (var i = 0; i < count; i++) - { - Unsafe.Add(ref dest, i) = Unsafe.As(ref Unsafe.Add(ref untypedSrc, i * GetPaddedSize())); - } + // Move to the initial ref + resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); + + for (var i = 0; i < count; i++) + { + ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); + + Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef); } } - else - { - MemoryHelper.Copy(resource.Pointer, offset, span, 0, count); - } + else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count); } /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { using MappedResource resource = MapResource(); + if (IsPaddingPresent) { - // Copy element-by-element - ref var src = ref MemoryMarshal.GetReference(span); - ref byte untypedDest = ref Unsafe.AsRef((void*)resource.Pointer); - untypedDest = ref Unsafe.Add(ref untypedDest, offset * GetPaddedSize()); + ref T spanRef = ref MemoryMarshal.GetReference(span); + ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer); + + // Move to the initial offset + resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); for (var i = 0; i < count; i++) { - Unsafe.As(ref Unsafe.Add(ref untypedDest, i * GetPaddedSize())) = Unsafe.Add(ref src, i); + ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); + + Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i); } } - // Directly copy as there is no padding else MemoryHelper.Copy(span, resource.Pointer, offset, count); }