From 7fcf755e6492b577cac096075671b3e3a17fd0f9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 14 Apr 2020 12:30:10 +0200 Subject: [PATCH] Minor code refactoring --- .../Buffers/Abstract/HlslBuffer{T}.cs | 12 ++++++------ .../Buffers/Abstract/HlslStructuredBuffer{T}.cs | 4 ++-- .../Buffers/ConstantBuffer{T}.cs | 13 +++++++------ .../Helpers/MemoryHelper.cs | 16 ++++++++-------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 76b0e267..4b938cca 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -71,18 +71,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count); /// - /// Writes the contents of a given to the current instance + /// Writes the contents of a given to the current instance /// - /// The input to read data from - public void SetData(Span span) => SetData(span, 0, Size); + /// The input to read data from + public void SetData(ReadOnlySpan span) => SetData(span, 0, Size); /// - /// Writes the contents of a given to a specified area of the current instance + /// Writes the contents of a given to a specified area of the current instance /// - /// The input to read data from + /// The input to read data from /// The offset to start writing data to /// The number of items to write - public abstract void SetData(Span span, int offset, int count); + public abstract void SetData(ReadOnlySpan span, int offset, int count); /// /// Writes the contents of a given to the current instance diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 92ad8911..657e1f71 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -36,12 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - public override void SetData(Span span, int offset, int count) + public override void SetData(ReadOnlySpan span, int offset, int count) { using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); transferBuffer.Map(); - MemoryHelper.Copy(span, 0, transferBuffer.MappedResource, 0, count); + MemoryHelper.Copy(span, transferBuffer.MappedResource, 0, count); transferBuffer.Unmap(); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 591d6dda..8ff900c1 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; @@ -78,15 +79,15 @@ namespace ComputeSharp } /// - public override void SetData(Span span, int offset, int count) + public override void SetData(ReadOnlySpan 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); // Array pool arrays can be longer - ref T tin = ref span.GetPinnableReference(); - ref byte tout = ref temporarySpan.GetPinnableReference(); + 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++) @@ -97,7 +98,7 @@ namespace ComputeSharp // Copy the padded data to the GPU Map(); - MemoryHelper.Copy(temporarySpan, 0, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes); + MemoryHelper.Copy(temporarySpan, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes); Unmap(); ArrayPool.Shared.Return(temporaryArray); @@ -106,7 +107,7 @@ namespace ComputeSharp { // Directly copy the input span if there is no padding Map(); - MemoryHelper.Copy(span, 0, MappedResource, offset, count); + MemoryHelper.Copy(span, MappedResource, offset, count); Unmap(); } } diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 560db122..b9911acb 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -9,19 +9,19 @@ namespace ComputeSharp.Graphics.Helpers internal static class MemoryHelper { /// - /// Copies the content of a to the area pointed by an input value + /// Copies the content of a to the area pointed by an input value /// - /// The type of values in the input - /// The source to read - /// The source offset to start reading data from + /// The type of values in the input + /// The source to read /// The for the destination memory area /// The destination offset to start writing data to /// The total number of items to copy - public static unsafe void Copy(Span source, int sourceOffset, IntPtr destination, int destinationOffset, int count) where T : unmanaged + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy(ReadOnlySpan source, IntPtr destination, int destinationOffset, int count) where T : unmanaged { - ref T rout = ref Unsafe.AsRef(destination.ToPointer()); - void* target = Unsafe.AsPointer(ref Unsafe.Add(ref rout, destinationOffset)); - source.Slice(sourceOffset, count).CopyTo(new Span(target, count)); + T* p = (T*)destination.ToPointer() + destinationOffset; + + source.CopyTo(new Span(p, count)); } ///