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/from the GPU.
///
/// The type of items stored on the buffer.
public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner
where T : unmanaged
{
///
/// The instance currently mapped.
///
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 resource type for the current buffer.
/// The allocation mode to use for the new resource.
private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, 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, 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 a instance.
///
private sealed class MemoryManager : MemoryManager
{
///
/// The in use.
///
private readonly TransferBuffer buffer;
///
/// Creates a new instance for a given buffer.
///
/// The in use.
public MemoryManager(TransferBuffer 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)
{
}
}
}
}