Added APIs to set buffers from existing buffers

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-13 21:33:52 +02:00
1 親 244b3535d4
コミット 984d1113f8
3ファイル変更
+106 -18

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

+12
ファイルの表示
@@ -43,5 +43,17 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
public abstract void SetData(Span<T> span);
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlyBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
/// </summary>
/// <param name="buffer">The input <see cref="ReadOnlyBuffer{T}"/> to read data from</param>
public abstract void SetData(ReadOnlyBuffer<T> buffer);
/// <summary>
/// Writes the contents of a given <see cref="ReadWriteBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
/// </summary>
/// <param name="buffer">The input <see cref="ReadWriteBuffer{T}"/> to read data from</param>
public abstract void SetData(ReadWriteBuffer<T> buffer);
}
}
+48 -17
ファイルの表示
@@ -21,12 +21,18 @@ namespace ComputeSharp.Graphics.Buffers
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, HeapType.Upload)
{
PaddedElementSizeInBytes = SizeInBytes / Size;
IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes;
}
/// <summary>
/// Gets the size in bytes of the current buffer
/// </summary>
private int PaddedElementSizeInBytes { get; }
internal int PaddedElementSizeInBytes { get; }
/// <summary>
/// Gets whether or not there is some padding between elements in the current buffer
/// </summary>
internal bool IsPaddingPresent { get; }
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
@@ -38,14 +44,7 @@ namespace ComputeSharp.Graphics.Buffers
/// <inheritdoc/>
public override void GetData(Span<T> span)
{
// Directly copy the data back if there is no padding
if (PaddedElementSizeInBytes == ElementSizeInBytes)
{
Map(0);
MemoryHelper.Copy(MappedResource, span);
Unmap(0);
}
else
if (IsPaddingPresent)
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
@@ -68,19 +67,19 @@ namespace ComputeSharp.Graphics.Buffers
ArrayPool<byte>.Shared.Return(temporaryArray);
}
else
{
// Directly copy the data back if there is no padding
Map(0);
MemoryHelper.Copy(MappedResource, span);
Unmap(0);
}
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
{
// Directly copy the input span if there is no padding
if (PaddedElementSizeInBytes == ElementSizeInBytes)
{
Map(0);
MemoryHelper.Copy(span, MappedResource);
Unmap(0);
}
else
if (IsPaddingPresent)
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
@@ -102,6 +101,38 @@ namespace ComputeSharp.Graphics.Buffers
ArrayPool<byte>.Shared.Return(temporaryArray);
}
else
{
// Directly copy the input span if there is no padding
Map(0);
MemoryHelper.Copy(span, MappedResource);
Unmap(0);
}
}
/// <inheritdoc/>
public override void SetData(ReadOnlyBuffer<T> buffer)
{
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
copyCommandList.Flush(true);
}
/// <inheritdoc/>
public override void SetData(ReadWriteBuffer<T> buffer)
{
// Create a temporary array
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
Span<T> span = array.AsSpan(buffer.Size);
// Get the unpadded data from the read write buffer
buffer.GetData(span);
// Set the data, adding the padding if needed
SetData(span);
ArrayPool<T>.Shared.Return(array);
}
}
}
+46 -1
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Helpers;
@@ -48,14 +49,58 @@ namespace ComputeSharp.Graphics.Buffers
public override void SetData(Span<T> span)
{
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, HeapType.Upload);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
uploadBuffer.Map(0);
MemoryHelper.Copy(span, uploadBuffer.MappedResource);
uploadBuffer.Unmap(0);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
copyCommandList.Flush(true);
}
/// <inheritdoc/>
public override void SetData(ReadOnlyBuffer<T> buffer)
{
if (buffer.IsPaddingPresent)
{
// Create a temporary array
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
Span<T> span = array.AsSpan(buffer.Size);
// Get the data from the readonly array, removing the padding
buffer.GetData(span);
// Set the data to the current buffer
SetData(span);
ArrayPool<T>.Shared.Return(array);
}
else
{
// Directly copy the input buffer
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
copyCommandList.Flush(true);
}
}
/// <inheritdoc/>
public override void SetData(ReadWriteBuffer<T> buffer)
{
// Create a temporary array
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
Span<T> span = array.AsSpan(buffer.Size);
// Get the unpadded data from the read write buffer
buffer.GetData(span);
// Set the data to the current buffer
SetData(span);
ArrayPool<T>.Shared.Return(array);
}
}
}