Added TerraFX buffer types
このコミットが含まれているのは:
1 親
c491d8c57c
コミット
b5f3bf07e3
6ファイル変更
+366
-1
検索条件に一致するファイルはありません
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ConstantBuffer2<T> : HlslBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBuffer2{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>
|
||||
internal ConstantBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, size * GetPaddedSize(), BufferType.Constant)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the right padded size for <typeparamref name="T"/> elements to store in the current instance
|
||||
/// </summary>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int GetPaddedSize()
|
||||
{
|
||||
int size = Unsafe.SizeOf<T>();
|
||||
|
||||
return (size + 15) & ~15;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current constant buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer<T>)}<T>[int]");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(span);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>((void*)resource.Pointer);
|
||||
|
||||
// Move to the initial ref
|
||||
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
|
||||
|
||||
Unsafe.Add(ref spanRef, i) = Unsafe.As<byte, T>(ref targetRef);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(span);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>((void*)resource.Pointer);
|
||||
|
||||
// Move to the initial offset
|
||||
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
|
||||
|
||||
Unsafe.As<byte, T>(ref targetRef) = Unsafe.Add(ref spanRef, i);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(span, resource.Pointer, offset, count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(HlslBuffer2<T> buffer)
|
||||
{
|
||||
if (buffer is ConstantBuffer2<T>)
|
||||
{
|
||||
// Directly copy the input buffer, if possible
|
||||
using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed readonly buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadOnlyBuffer2<T> : HlslStructuredBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadOnlyBuffer2{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ReadOnlyBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, BufferType.ReadOnly)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer<T>)}<T>[int]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadWriteBuffer2<T> : HlslStructuredBuffer2<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadWriteBuffer2{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>
|
||||
internal ReadWriteBuffer2(GraphicsDevice2 device, int size)
|
||||
: base(device, size, BufferType.ReadWrite)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get or set</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public ref T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer<T>)}<T>[int]");
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする