Updated buffer types to implement the new architecture

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-13 16:21:50 +02:00
1 親 a68ad24929
コミット 12198f9943
3ファイル変更
+62 -88

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

+19 -84
ファイルの表示
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics.Contracts;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Helpers;
using SharpDX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers
@@ -18,12 +16,13 @@ namespace ComputeSharp.Graphics.Buffers
/// </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>
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
/// <param name="heapType">The heap type for the current buffer</param>
protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device)
protected internal Buffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device)
{
Size = size;
ElementSizeInBytes = Unsafe.SizeOf<T>();
SizeInBytes = Size * ElementSizeInBytes;
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
HeapType = heapType;
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
@@ -45,6 +44,21 @@ namespace ComputeSharp.Graphics.Buffers
};
}
/// <summary>
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
/// </summary>
public int Size { get; }
/// <summary>
/// Gets the size in bytes of the current buffer
/// </summary>
protected int SizeInBytes { get; }
/// <summary>
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
/// </summary>
protected int ElementSizeInBytes { get; }
/// <summary>
/// Creates the descriptors for a constant buffer
/// </summary>
@@ -88,88 +102,9 @@ namespace ComputeSharp.Graphics.Buffers
return (cpuHandle, gpuHandle);
}
/// <summary>
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
/// </summary>
public int Size { get; }
/// <summary>
/// Gets the size in bytes of the current buffer
/// </summary>
public int SizeInBytes { get; }
/// <summary>
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
/// </summary>
public int ElementSizeInBytes { get; }
/// <summary>
/// Gets the heap type being targeted by the current buffer
/// </summary>
public HeapType HeapType { get; }
/// <summary>
/// Reads the contents of the current <see cref="Buffer{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()
{
T[] data = new T[Size];
GetData(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Buffer{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)
{
// Create a temporary readback buffer if the current buffer type is read write (can't be read from directly)
if (HeapType == HeapType.Default)
{
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Readback);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
copyCommandList.Flush(true);
readbackBaffer.GetData(span);
}
else
{
// Map and read the memory back directly
Map(0);
MemoryHelper.Copy(MappedResource, span);
Unmap(0);
}
}
/// <summary>
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="Buffer{T}"/> instance
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
public void SetData(Span<T> span)
{
// Create a temporary upload buffer if the current buffer type is read write (can't be written to directly)
if (HeapType == HeapType.Default)
{
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Upload);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
uploadBuffer.SetData(span);
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
copyCommandList.Flush(true);
}
else
{
// Map and copy the memory directly
Map(0);
MemoryHelper.Copy(span, MappedResource);
Unmap(0);
}
}
}
}
+24 -2
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using SharpDX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers
@@ -7,14 +9,22 @@ namespace ComputeSharp.Graphics.Buffers
/// 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 ReadOnlyBuffer<T> : Buffer<T> where T : unmanaged
public sealed class ReadOnlyBuffer<T> : HlslBuffer<T> where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyBuffer{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 ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { }
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1), HeapType.Upload)
{
PaddedElementSizeInBytes = SizeInBytes / Size;
}
/// <summary>
/// Gets the size in bytes of the current buffer
/// </summary>
private int PaddedElementSizeInBytes { get; }
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
@@ -22,5 +32,17 @@ namespace ComputeSharp.Graphics.Buffers
/// <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[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
/// <inheritdoc/>
public override void GetData(Span<T> span)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
{
throw new NotImplementedException();
}
}
}
+19 -2
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using SharpDX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers
@@ -7,14 +9,17 @@ namespace ComputeSharp.Graphics.Buffers
/// 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 ReadWriteBuffer<T> : Buffer<T> where T : unmanaged
public sealed class ReadWriteBuffer<T> : HlslBuffer<T> where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteBuffer{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 ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { }
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf<T>(), HeapType.Default)
{
}
/// <summary>
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
@@ -26,5 +31,17 @@ namespace ComputeSharp.Graphics.Buffers
get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
}
/// <inheritdoc/>
public override void GetData(Span<T> span)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
{
throw new NotImplementedException();
}
}
}