Minor code refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2020-04-14 12:30:10 +02:00
1 親 e2ccda0144
コミット 7fcf755e64
4ファイル変更
+23 -22

検索条件に一致するファイルはありません

+6 -6
ファイルの表示
@@ -71,18 +71,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count);
/// <summary>
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
public void SetData(Span<T> span) => SetData(span, 0, Size);
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> to read data from</param>
public void SetData(ReadOnlySpan<T> span) => SetData(span, 0, Size);
/// <summary>
/// Writes the contents of a given <see cref="Span{T}"/> to a specified area of the current <see cref="HlslBuffer{T}"/> instance
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="HlslBuffer{T}"/> instance
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> to read data from</param>
/// <param name="offset">The offset to start writing data to</param>
/// <param name="count">The number of items to write</param>
public abstract void SetData(Span<T> span, int offset, int count);
public abstract void SetData(ReadOnlySpan<T> span, int offset, int count);
/// <summary>
/// Writes the contents of a given <see cref="HlslBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
+2 -2
ファイルの表示
@@ -36,12 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
/// <inheritdoc/>
public override void SetData(Span<T> span, int offset, int count)
public override void SetData(ReadOnlySpan<T> span, int offset, int count)
{
using Buffer<T> transferBuffer = new Buffer<T>(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);
+7 -6
ファイルの表示
@@ -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
}
/// <inheritdoc/>
public override void SetData(Span<T> span, int offset, int count)
public override void SetData(ReadOnlySpan<T> span, int offset, int count)
{
if (IsPaddingPresent)
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(count * PaddedElementSizeInBytes);
Span<byte> 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<byte> 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<byte>.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();
}
}
+8 -8
ファイルの表示
@@ -9,19 +9,19 @@ namespace ComputeSharp.Graphics.Helpers
internal static class MemoryHelper
{
/// <summary>
/// Copies the content of a <see cref="Span{T}"/> to the area pointed by an input <see cref="IntPtr"/> value
/// Copies the content of a <see cref="ReadOnlySpan{T}"/> to the area pointed by an input <see cref="IntPtr"/> value
/// </summary>
/// <typeparam name="T">The type of values in the input <see cref="Span{T}"/></typeparam>
/// <param name="source">The source <see cref="Span{T}"/> to read</param>
/// <param name="sourceOffset">The source offset to start reading data from</param>
/// <typeparam name="T">The type of values in the input <see cref="ReadOnlySpan{T}"/></typeparam>
/// <param name="source">The source <see cref="ReadOnlySpan{T}"/> to read</param>
/// <param name="destination">The <see cref="IntPtr"/> for the destination memory area</param>
/// <param name="destinationOffset">The destination offset to start writing data to</param>
/// <param name="count">The total number of items to copy</param>
public static unsafe void Copy<T>(Span<T> source, int sourceOffset, IntPtr destination, int destinationOffset, int count) where T : unmanaged
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(ReadOnlySpan<T> source, IntPtr destination, int destinationOffset, int count) where T : unmanaged
{
ref T rout = ref Unsafe.AsRef<T>(destination.ToPointer());
void* target = Unsafe.AsPointer(ref Unsafe.Add(ref rout, destinationOffset));
source.Slice(sourceOffset, count).CopyTo(new Span<T>(target, count));
T* p = (T*)destination.ToPointer() + destinationOffset;
source.CopyTo(new Span<T>(p, count));
}
/// <summary>