diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs
index 2706fc13..efe0b5f4 100644
--- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs
+++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs
@@ -80,5 +80,17 @@ namespace ComputeSharp.Exceptions
{
throw Create(texture, texture.GraphicsDevice, device);
}
+
+ ///
+ /// Throws a new instance from the specified parameters.
+ ///
+ /// The type of values in the input buffer.
+ /// The input that was used.
+ /// The target instance that was used.
+ internal static void Throw(UploadBuffer buffer, GraphicsDevice device)
+ where T : unmanaged
+ {
+ throw Create(buffer, buffer.GraphicsDevice, device);
+ }
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
index 744e26ed..9e4a617d 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
@@ -110,17 +110,17 @@ namespace ComputeSharp.Resources
/// Reads the contents of the specified range from the current instance and writes them into a target memory area.
///
/// The input memory area to write data to.
- /// The size of the memory area to write data to.
+ /// The length of the memory area to write data to.
/// The offset to start reading data from.
- internal abstract void GetData(ref T destination, int size, int offset);
+ internal abstract void GetData(ref T destination, int length, int offset);
///
/// Writes the contents of a given memory area to a specified area of the current instance.
///
/// The input memory area to read data from.
- /// The size of the input memory area to read data from.
+ /// The length of the input memory area to read data from.
/// The offset to start writing data to.
- internal abstract void SetData(ref T source, int size, int offset);
+ internal abstract void SetData(ref T source, int length, int offset);
///
/// Writes the contents of a given to the current instance.
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index e47ea518..1e479da4 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -29,14 +29,14 @@ namespace ComputeSharp.Resources
}
///
- 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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize, false);
+ using ComPtr 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
}
///
- 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 d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize, false);
+ using ComPtr 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();
}
}
+ ///
+ public unsafe void SetData(UploadBuffer 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();
+ }
+
///
public override unsafe void SetData(Buffer source)
{
diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/ITransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/ITransferBuffer{T}.cs
new file mode 100644
index 00000000..749a63ff
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Resources/Interfaces/ITransferBuffer{T}.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace ComputeSharp
+{
+ ///
+ /// An interface representing a typed readonly buffer used to transfer data to the GPU.
+ ///
+ /// The type of items stored on the buffer.
+ internal interface ITransferBuffer
+ where T : unmanaged
+ {
+ ///
+ /// Gets a with the data of the current buffer.
+ ///
+ Span Span { get; }
+ }
+}
diff --git a/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs
new file mode 100644
index 00000000..66e13751
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs
@@ -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
+{
+ ///
+ /// A representing a typed buffer stored on CPU memory, that can be used to transfer data to the GPU.
+ ///
+ /// The type of items stored on the buffer.
+ public sealed unsafe class UploadBuffer : NativeObject, IMemoryOwner, ITransferBuffer
+ where T : unmanaged
+ {
+ ///
+ /// The instance currently mapped, if any.
+ ///
+ private ComPtr d3D12Resource;
+
+ ///
+ /// The size in bytes of the current buffer (this value is never negative).
+ ///
+ private readonly nint sizeInBytes;
+
+ ///
+ /// The pointer to the start of the mapped buffer data.
+ ///
+ private readonly T* mappedData;
+
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The associated with the current instance.
+ /// The number of items to store in the current buffer.
+ /// The allocation mode to use for the new resource.
+ 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;
+ }
+
+ ///
+ /// Gets the associated with the current instance.
+ ///
+ public GraphicsDevice GraphicsDevice { get; }
+
+ ///
+ /// Gets the length of the current buffer.
+ ///
+ public int Length { get; }
+
+ ///
+ /// Gets the instance currently mapped.
+ ///
+ internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
+
+ ///
+ public Memory Memory
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get
+ {
+ ThrowIfDisposed();
+
+ return new MemoryManager(this).Memory;
+ }
+ }
+
+ ///
+ public Span Span
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get
+ {
+ ThrowIfDisposed();
+
+ return new(this.mappedData, Length);
+ }
+ }
+
+ ///
+ protected override bool OnDispose()
+ {
+ this.d3D12Resource.Dispose();
+
+ return true;
+ }
+
+ ///
+ /// Throws a if the target device doesn't match the current one.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal void ThrowIfDeviceMismatch(GraphicsDevice device)
+ {
+ if (GraphicsDevice != device)
+ {
+ GraphicsDeviceMismatchException.Throw(this, device);
+ }
+ }
+
+ ///
+ /// A implementation wrapping an instance.
+ ///
+ private sealed class MemoryManager : MemoryManager
+ {
+ ///
+ /// The in use.
+ ///
+ private readonly UploadBuffer buffer;
+
+ ///
+ /// Creates a new instance for a given buffer.
+ ///
+ /// The in use.
+ public MemoryManager(UploadBuffer buffer)
+ {
+ this.buffer = buffer;
+ }
+
+ ///
+ public override Memory Memory
+ {
+ get => CreateMemory(this.buffer.Length);
+ }
+
+ ///
+ public override Span GetSpan()
+ {
+ return this.buffer.Span;
+ }
+
+ ///
+ 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);
+ }
+
+ ///
+ public override void Unpin()
+ {
+ }
+
+ ///
+ protected override void Dispose(bool disposing)
+ {
+ }
+ }
+ }
+}