Merge pull request #32 from Sergio0694/feature_range-buffer-copy

Range buffer copy APIs
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-20 01:29:50 +02:00
コミット 97bf95484f
7ファイル変更
+129 -34

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

+38 -5
ファイルの表示
@@ -25,10 +25,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
[Pure]
public T[] GetData()
public T[] GetData() => GetData(0, Size);
/// <summary>
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance in a given range and returns an array
/// </summary>
/// <param name="offset">The offset to start reading data from</param>
/// <param name="count">The number of items to read</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the specified range from the current buffer</returns>
[Pure]
public T[] GetData(int offset, int count)
{
T[] data = new T[Size];
GetData(data);
T[] data = new T[count];
GetData(data, offset, count);
return data;
}
@@ -37,7 +46,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
public abstract void GetData(Span<T> span);
public void GetData(Span<T> span) => GetData(span, 0, Size);
/// <summary>
/// Reads the contents of the specified range from the current <see cref="HlslBuffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
/// <param name="offset">The offset to start reading data from</param>
/// <param name="count">The number of items to read</param>
public abstract void GetData(Span<T> span, int offset, int count);
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="HlslBuffer{T}"/> instance
@@ -45,11 +62,27 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="array">The input <typeparamref name="T"/> array to read data from</param>
public void SetData(T[] array) => SetData(array.AsSpan());
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="HlslBuffer{T}"/> instance
/// </summary>
/// <param name="array">The input <typeparamref name="T"/> array 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 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
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
public abstract void SetData(Span<T> span);
public void SetData(Span<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
/// </summary>
/// <param name="span">The input <see cref="Span{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);
/// <summary>
/// Writes the contents of a given <see cref="HlslBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
+8 -8
ファイルの表示
@@ -22,31 +22,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf<T>(), bufferType) { }
/// <inheritdoc/>
public override void GetData(Span<T> span)
public override void GetData(Span<T> span, int offset, int count)
{
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(this, 0, transferBuffer, 0, SizeInBytes);
copyCommandList.CopyBufferRegion(this, offset * ElementSizeInBytes, transferBuffer, 0, count * ElementSizeInBytes);
copyCommandList.Flush();
transferBuffer.Map(0);
MemoryHelper.Copy(transferBuffer.MappedResource, span);
MemoryHelper.Copy(transferBuffer.MappedResource, 0, span, 0, count);
transferBuffer.Unmap(0);
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
public override void SetData(Span<T> span, int offset, int count)
{
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
transferBuffer.Map(0);
MemoryHelper.Copy(span, transferBuffer.MappedResource);
MemoryHelper.Copy(span, 0, transferBuffer.MappedResource, 0, count);
transferBuffer.Unmap(0);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(transferBuffer, 0, this, 0, SizeInBytes);
copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset * ElementSizeInBytes, count * ElementSizeInBytes);
copyCommandList.Flush();
}
+12 -12
ファイルの表示
@@ -41,24 +41,24 @@ namespace ComputeSharp.Graphics.Buffers
public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
/// <inheritdoc/>
public override void GetData(Span<T> span)
public override void GetData(Span<T> span, int offset, int count)
{
if (IsPaddingPresent)
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan();
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(count * PaddedElementSizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes);
// Copy the padded data to the temporary array
Map(0);
MemoryHelper.Copy(MappedResource, temporarySpan);
MemoryHelper.Copy(MappedResource, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes);
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++)
for (int i = 0; i < count; i++)
{
ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes);
Unsafe.Add(ref tout, i) = Unsafe.As<byte, T>(ref rsource);
@@ -70,24 +70,24 @@ namespace ComputeSharp.Graphics.Buffers
{
// Directly copy the data back if there is no padding
Map(0);
MemoryHelper.Copy(MappedResource, span);
MemoryHelper.Copy(MappedResource, offset, span, 0, count);
Unmap(0);
}
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
public override void SetData(Span<T> span, int offset, int count)
{
if (IsPaddingPresent)
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer
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();
// Copy the input data to the temporary array and add the padding
for (int i = 0; i < Size; i++)
for (int i = 0; i < count; i++)
{
ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes);
Unsafe.As<byte, T>(ref rtarget) = Unsafe.Add(ref tin, i);
@@ -95,7 +95,7 @@ namespace ComputeSharp.Graphics.Buffers
// Copy the padded data to the GPU
Map(0);
MemoryHelper.Copy(temporarySpan, MappedResource);
MemoryHelper.Copy(temporarySpan, 0, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes);
Unmap(0);
ArrayPool<byte>.Shared.Return(temporaryArray);
@@ -104,7 +104,7 @@ namespace ComputeSharp.Graphics.Buffers
{
// Directly copy the input span if there is no padding
Map(0);
MemoryHelper.Copy(span, MappedResource);
MemoryHelper.Copy(span, 0, MappedResource, offset, count);
Unmap(0);
}
}
+14 -5
ファイルの表示
@@ -13,11 +13,15 @@ namespace ComputeSharp.Graphics.Helpers
/// </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>
/// <param name="destination">The <see cref="IntPtr"/> for the destination memory area</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(Span<T> source, IntPtr destination) where T : unmanaged
/// <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
{
source.CopyTo(new Span<T>(destination.ToPointer(), source.Length));
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));
}
/// <summary>
@@ -25,11 +29,16 @@ namespace ComputeSharp.Graphics.Helpers
/// </summary>
/// <typeparam name="T">The type of values to read</typeparam>
/// <param name="source">The <see cref="IntPtr"/> that indicates the memory area to read from</param>
/// <param name="sourceOffset">The source offset to start reading data from</param>
/// <param name="destination">The destination <see cref="Span{T}"/> to write</param>
/// <param name="destinationOffset">The destination offset to start writing data to</param>
/// <param name="count">The total number of items to copy</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(IntPtr source, Span<T> destination) where T : unmanaged
public static unsafe void Copy<T>(IntPtr source, int sourceOffset, Span<T> destination, int destinationOffset, int count) where T : unmanaged
{
new Span<T>(source.ToPointer(), destination.Length).CopyTo(destination);
ref T rin = ref Unsafe.AsRef<T>(source.ToPointer());
void* target = Unsafe.AsPointer(ref Unsafe.Add(ref rin, sourceOffset));
new Span<T>(target, count).CopyTo(destination.Slice(destinationOffset, count));
}
}
}
+3 -2
ファイルの表示
@@ -112,19 +112,20 @@ namespace ComputeSharp.Shaders.Extensions
// Constant replacement if the value is a readonly scalar value
if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberInfo.MemberType))
{
return memberInfo.GetValue(null) switch
LiteralExpressionSyntax expression = memberInfo.GetValue(null) switch
{
true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)),
false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)),
IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))),
_ => throw new InvalidOperationException($"Invalid field of type {memberInfo.MemberType}")
};
return expression.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}
// Captured member, treat it like any other captured variable in the closure
string name = $"{containingMemberSymbolInfo.Symbol.Name}_{memberInfo.Name}";
variable = (name, memberInfo);
return SyntaxFactory.IdentifierName(name);
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}
return node;
-2
ファイルの表示
@@ -132,8 +132,6 @@ namespace ComputeSharp.Shaders.Mappings
return $"{containingMemberSymbol.Name}{mapped}";
}
if (memberSymbol.IsStatic) return $"{containingMemberSymbol.Name}::{memberSymbol.Name}";
return null;
}
}
+54
ファイルの表示
@@ -91,5 +91,59 @@ namespace ComputeSharp.Tests
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
}
[TestMethod]
public void ConstantBufferGetRangeData()
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
float[] sourceResult = sourceBuffer.GetData(128, 100);
Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult));
}
[TestMethod]
public void ConstantBufferSetRangeData()
{
float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
array.AsSpan(56, 100).Clear();
sourceBuffer.SetData(new float[100], 56, 100);
float[] sourceResult = sourceBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
}
[TestMethod]
public void ReadWriteBufferGetRangeData()
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
float[] sourceResult = sourceBuffer.GetData(128, 100);
Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult));
}
[TestMethod]
public void ReadWriteBufferSetRangeData()
{
float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
array.AsSpan(56, 100).Clear();
sourceBuffer.SetData(new float[100], 56, 100);
float[] sourceResult = sourceBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
}
}
}