Added TerraFX buffer types

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-17 13:36:23 +01:00
1 親 c491d8c57c
コミット b5f3bf07e3
6ファイル変更
+366 -1

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

+115
ファイルの表示
@@ -0,0 +1,115 @@
using System;
using System.Buffers;
using System.Diagnostics.Contracts;
using ComputeSharp.Graphics.Buffers.Enums;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
/// <summary>
/// A <see langword="class"/> representing a typed buffer stored on GPU memory, that can be used from an HLSL shader
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
public abstract class HlslBuffer2<T> : Buffer2<T> where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="HlslBuffer2{T}"/> instance with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice2"/> associated with the current instance</param>
/// <param name="size">The number of items to store in the current buffer</param>
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
/// <param name="bufferType">The buffer type for the current buffer</param>
private protected HlslBuffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType)
: base(device, size, sizeInBytes, bufferType)
{
}
/// <summary>
/// Reads the contents of the current <see cref="HlslBuffer2{T}"/> instance and returns an array
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
[Pure]
public T[] GetData() => GetData(0, Size);
/// <summary>
/// Reads the contents of the current <see cref="HlslBuffer2{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[count];
GetData(data, offset, count);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="HlslBuffer2{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 void GetData(Span<T> span) => GetData(span, 0, Size);
/// <summary>
/// Reads the contents of the specified range from the current <see cref="HlslBuffer2{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="HlslBuffer2{T}"/> instance
/// </summary>
/// <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="HlslBuffer2{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="ReadOnlySpan{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance
/// </summary>
/// <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="ReadOnlySpan{T}"/> to a specified area of the current <see cref="HlslBuffer2{T}"/> instance
/// </summary>
/// <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(ReadOnlySpan<T> span, int offset, int count);
/// <summary>
/// Writes the contents of a given <see cref="HlslBuffer2{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance
/// </summary>
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> to read data from</param>
public abstract void SetData(HlslBuffer2<T> buffer);
/// <summary>
/// Writes the contents of a given <see cref="HlslBuffer2{T}"/> to the current <see cref="HlslBuffer2{T}"/> instance, using a temporary CPU buffer
/// </summary>
/// <param name="buffer">The input <see cref="HlslBuffer2{T}"/> to read data from</param>
protected void SetDataWithCpuBuffer(HlslBuffer2<T> buffer)
{
// Create a temporary array
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
Span<T> span = array.AsSpan(0, 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);
}
}
}
+74
ファイルの表示
@@ -0,0 +1,74 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Helpers;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
/// <summary>
/// A <see langword="class"/> representing a typed structured buffer stored on GPU memory
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
public abstract class HlslStructuredBuffer2<T> : HlslBuffer2<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="HlslStructuredBuffer2{T}"/> instance with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice2"/> associated with the current instance</param>
/// <param name="size">The number of items to store in the current buffer</param>
/// <param name="bufferType">The buffer type for the current buffer</param>
internal HlslStructuredBuffer2(GraphicsDevice2 device, int size, BufferType bufferType)
: base(device, size, size * Unsafe.SizeOf<T>(), bufferType)
{
}
/// <inheritdoc/>
public override unsafe void GetData(Span<T> span, int offset, int count)
{
using Buffer2<T> transferBuffer = new Buffer2<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.ReadBack);
using (CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.CopyBufferRegion(D3D12Resource, offset * ElementSizeInBytes, transferBuffer.D3D12Resource, 0, count * ElementSizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
using MappedResource resource = transferBuffer.MapResource();
MemoryHelper.Copy(resource.Pointer, 0, span, 0, count);
}
/// <inheritdoc/>
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
{
using Buffer2<T> transferBuffer = new Buffer2<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
using (MappedResource resource = transferBuffer.MapResource())
{
MemoryHelper.Copy(span, resource.Pointer, 0, count);
}
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(transferBuffer.D3D12Resource, 0, D3D12Resource, offset * ElementSizeInBytes, count * ElementSizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
public override unsafe void SetData(HlslBuffer2<T> buffer)
{
if (!buffer.IsPaddingPresent)
{
// Directly copy the input buffer
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(buffer);
}
}
}
+2 -1
ファイルの表示
@@ -11,7 +11,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// A <see langword="class"/> representing a typed structured buffer stored on GPU memory
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
public abstract class HlslStructuredBuffer<T> : HlslBuffer<T> where T : unmanaged
public abstract class HlslStructuredBuffer<T> : HlslBuffer<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="HlslStructuredBuffer{T}"/> instance with the specified parameters