using System; using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Extensions; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; namespace ComputeSharp.Graphics.Buffers.Abstract { /// /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. public unsafe abstract class Buffer : NativeObject where T : unmanaged { /// /// The instance currently mapped. /// private ComPtr d3D12Resource; /// /// The instance for the current resource. /// /// This field is dynamically accessed when loading shader dispatch data. internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; /// /// The size in bytes of the current buffer (this value is never negative). /// protected readonly nint SizeInBytes; /// /// 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 size in bytes of each buffer item (including padding, if any). /// The resource type for the current buffer. private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType) { device.ThrowIfDisposed(); Guard.IsGreaterThanOrEqualTo(length, 0, nameof(length)); SizeInBytes = checked((nint)(length * elementSizeInBytes)); GraphicsDevice = device; Length = length; this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes); device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.Constant: device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, d3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle); break; } } /// /// Gets the associated with the current instance. /// public GraphicsDevice GraphicsDevice { get; } /// /// Gets the length of the current buffer. /// public int Length { get; } /// /// Gets the size in bytes of each value contained in the buffer. /// protected int ElementSizeInBytes { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => Unsafe.SizeOf(); } /// /// Gets whether or not there is some padding between elements in the current buffer. /// internal bool IsPaddingPresent { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => SizeInBytes > Length * Unsafe.SizeOf(); } /// /// Gets the instance currently mapped. /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; /// /// Reads the contents of the current instance and returns an array. /// /// A array with the contents of the current buffer. [Pure] public T[] GetData() { return GetData(0, Length); } /// /// Reads the contents of the current instance in a given range and returns an array. /// /// The offset to start reading data from. /// The number of items to read. /// A array with the contents of the specified range from the current buffer. [Pure] public T[] GetData(int offset, int count) { Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); T[] data = GC.AllocateUninitializedArray(count); GetData(data.AsSpan(), offset); return data; } /// /// Reads the contents of the current instance and writes them into a target array. /// /// The input array to write data to. public void GetData(T[] destination) { GetData(destination.AsSpan(), 0); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The input array to write data to. /// The starting offset within to write data to. /// The offset to start reading data from. /// The number of items to read. public void GetData(T[] destination, int destinationOffset, int bufferOffset, int count) { Span span = destination.AsSpan(destinationOffset, count); GetData(span, bufferOffset); } /// /// Reads the contents of the current instance and writes them into a target . /// The input data will be read from the start of the buffer. /// /// The input to write data to. public void GetData(Span destination) { GetData(destination, 0); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The input to write data to. /// The offset to start reading data from. public abstract void GetData(Span destination, int offset); /// /// Writes the contents of a given array to the current instance. /// /// The input array to read data from. public void SetData(T[] source) { SetData(source.AsSpan()); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The input array to read data from. /// The starting offset within to read data from. /// The offset to start writing data to. /// The number of items to write. public void SetData(T[] source, int sourceOffset, int bufferOffset, int count) { ReadOnlySpan span = source.AsSpan(sourceOffset, count); SetData(span, bufferOffset); } /// /// Writes the contents of a given to the current instance. /// The input data will be written to the start of the buffer, and all input items will be copied. /// /// The input to read data from. public void SetData(ReadOnlySpan source) { SetData(source, 0); } /// /// Writes the contents of a given to a specified area of the current instance. /// The input data will be written into the buffer starting at the specified offset, and all input items will be copied. /// /// The input to read data from. /// The offset to start writing data to. /// The number of items to write. public abstract void SetData(ReadOnlySpan source, int offset); /// /// Writes the contents of a given to the current instance. /// /// The input to read data from. public abstract void SetData(Buffer source); /// /// Writes the contents of a given to the current instance, using a temporary CPU buffer. /// /// The input to read data from. protected void SetDataWithCpuBuffer(Buffer source) { T[] array = ArrayPool.Shared.Rent(source.Length); try { Span span = array.AsSpan(0, source.Length); source.GetData(span); SetData(span); } finally { ArrayPool.Shared.Return(array); } } /// protected override void OnDispose() { this.d3D12Resource.Dispose(); } /// /// 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); } } } }