Added UploadBuffer<T> type

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-31 19:41:39 +01:00
1 親 42f6401655
コミット 1857407c3d
5ファイル変更
+240 -18

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

+12
ファイルの表示
@@ -80,5 +80,17 @@ namespace ComputeSharp.Exceptions
{
throw Create(texture, texture.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
/// <param name="buffer">The input <see cref="UploadBuffer{T}"/> that was used.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
internal static void Throw<T>(UploadBuffer<T> buffer, GraphicsDevice device)
where T : unmanaged
{
throw Create(buffer, buffer.GraphicsDevice, device);
}
}
}
+4 -4
ファイルの表示
@@ -110,17 +110,17 @@ namespace ComputeSharp.Resources
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target memory area.
/// </summary>
/// <param name="destination">The input memory area to write data to.</param>
/// <param name="size">The size of the memory area to write data to.</param>
/// <param name="length">The length of the memory area to write data to.</param>
/// <param name="offset">The offset to start reading data from.</param>
internal abstract void GetData(ref T destination, int size, int offset);
internal abstract void GetData(ref T destination, int length, int offset);
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Buffer{T}"/> instance.
/// </summary>
/// <param name="source">The input memory area to read data from.</param>
/// <param name="size">The size of the input memory area to read data from.</param>
/// <param name="length">The length of the input memory area to read data from.</param>
/// <param name="offset">The offset to start writing data to.</param>
internal abstract void SetData(ref T source, int size, int offset);
internal abstract void SetData(ref T source, int length, int offset);
/// <summary>
/// Writes the contents of a given <see cref="Buffer{T}"/> to the current <see cref="Buffer{T}"/> instance.
+40 -14
ファイルの表示
@@ -29,14 +29,14 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
internal override unsafe void GetData(ref T destination, int size, int offset)
internal override unsafe void GetData(ref T destination, int length, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
if (GraphicsDevice.IsCacheCoherentUMA)
{
@@ -47,7 +47,7 @@ namespace ComputeSharp.Resources
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)size,
(uint)length,
(uint)sizeof(T),
destinationPointer);
}
@@ -56,13 +56,13 @@ namespace ComputeSharp.Resources
{
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
byteLength = length * sizeof(T);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize, false);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength, false);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize);
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -73,7 +73,7 @@ namespace ComputeSharp.Resources
MemoryHelper.Copy(
resource.Pointer,
0u,
(uint)size,
(uint)length,
(uint)sizeof(T),
destinationPointer);
}
@@ -81,14 +81,14 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
internal override unsafe void SetData(ref T source, int size, int offset)
internal override unsafe void SetData(ref T source, int length, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
if (GraphicsDevice.IsCacheCoherentUMA)
{
@@ -99,7 +99,7 @@ namespace ComputeSharp.Resources
MemoryHelper.Copy(
sourcePointer,
(uint)offset,
(uint)size,
(uint)length,
(uint)sizeof(T),
resource.Pointer);
}
@@ -108,9 +108,9 @@ namespace ComputeSharp.Resources
{
nint
byteOffset = (nint)offset * sizeof(T),
byteSize = size * sizeof(T);
byteLength = length * sizeof(T);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize, false);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength, false);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
@@ -118,18 +118,44 @@ namespace ComputeSharp.Resources
MemoryHelper.Copy(
sourcePointer,
0u,
(uint)size,
(uint)length,
(uint)sizeof(T),
resource.Pointer);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
}
/// <inheritdoc/>
public unsafe void SetData(UploadBuffer<T> source, int sourceOffset, int length, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
Guard.IsInRange(sourceOffset, 0, source.Length, nameof(sourceOffset));
Guard.IsLessThanOrEqualTo(sourceOffset + length, source.Length, nameof(length));
ulong
byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T),
byteOffset = (uint)offset * (uint)sizeof(T),
byteLength = (uint)length * (uint)sizeof(T);
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
public override unsafe void SetData(Buffer<T> source)
{
+17
ファイルの表示
@@ -0,0 +1,17 @@
using System;
namespace ComputeSharp
{
/// <summary>
/// An interface representing a typed readonly buffer used to transfer data to the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
internal interface ITransferBuffer<T>
where T : unmanaged
{
/// <summary>
/// Gets a <see cref="Span{T}"/> with the data of the current buffer.
/// </summary>
Span<T> Span { get; }
}
}
+167
ファイルの表示
@@ -0,0 +1,167 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Interop;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> representing a typed buffer stored on CPU memory, that can be used to transfer data to the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
public sealed unsafe class UploadBuffer<T> : NativeObject, IMemoryOwner<T>, ITransferBuffer<T>
where T : unmanaged
{
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped, if any.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The size in bytes of the current buffer (this value is never negative).
/// </summary>
private readonly nint sizeInBytes;
/// <summary>
/// The pointer to the start of the mapped buffer data.
/// </summary>
private readonly T* mappedData;
/// <summary>
/// Creates a new <see cref="UploadBuffer{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
public UploadBuffer(GraphicsDevice device, int length, AllocationMode allocationMode)
{
device.ThrowIfDisposed();
// The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255, nameof(length));
GraphicsDevice = device;
Length = length;
this.sizeInBytes = checked((nint)(length * (uint)sizeof(T)));
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(ResourceType.Upload, allocationMode, (ulong)this.sizeInBytes, device.IsCacheCoherentUMA);
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
}
/// <summary>
/// Gets the <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the length of the current buffer.
/// </summary>
public int Length { get; }
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <inheritdoc/>
public Memory<T> Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ThrowIfDisposed();
return new MemoryManager(this).Memory;
}
}
/// <inheritdoc/>
public Span<T> Span
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ThrowIfDisposed();
return new(this.mappedData, Length);
}
}
/// <inheritdoc/>
protected override bool OnDispose()
{
this.d3D12Resource.Dispose();
return true;
}
/// <summary>
/// Throws a <see cref="GraphicsDeviceMismatchException"/> if the target device doesn't match the current one.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
{
if (GraphicsDevice != device)
{
GraphicsDeviceMismatchException.Throw(this, device);
}
}
/// <summary>
/// A <see cref="MemoryManager{T}"/> implementation wrapping an <see cref="UploadBuffer{T}"/> instance.
/// </summary>
private sealed class MemoryManager : MemoryManager<T>
{
/// <summary>
/// The <see cref="UploadBuffer{T}"/> in use.
/// </summary>
private readonly UploadBuffer<T> buffer;
/// <summary>
/// Creates a new <see cref="MemoryManager"/> instance for a given buffer.
/// </summary>
/// <param name="buffer">The <see cref="UploadBuffer{T}"/> in use.</param>
public MemoryManager(UploadBuffer<T> buffer)
{
this.buffer = buffer;
}
/// <inheritdoc/>
public override Memory<T> Memory
{
get => CreateMemory(this.buffer.Length);
}
/// <inheritdoc/>
public override Span<T> GetSpan()
{
return this.buffer.Span;
}
/// <inheritdoc/>
public override MemoryHandle Pin(int elementIndex = 0)
{
Guard.IsInRange(elementIndex, 0, this.buffer.Length, nameof(elementIndex));
this.buffer.ThrowIfDisposed();
return new(this.buffer.mappedData + elementIndex);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}
}