using System; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Buffers.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Graphics.Buffers.Abstract { /// /// A representing a typed structured buffer stored on GPU memory. /// /// The type of items stored on the buffer. public abstract class StructuredBuffer : Buffer where T : unmanaged { /// /// 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 buffer type for the current buffer. private protected StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType) : base(device, length, (uint)Unsafe.SizeOf(), resourceType) { } /// public override unsafe void GetData(Span destination, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); nint byteOffset = (nint)offset * ElementSizeInBytes, byteSize = (nint)destination.Length * ElementSizeInBytes; using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset,(ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); MemoryHelper.Copy(resource.Pointer, 0, destination); } /// public override unsafe void SetData(ReadOnlySpan source, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); nint byteOffset = (nint)offset * ElementSizeInBytes, byteSize = (nint)source.Length * ElementSizeInBytes; using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { MemoryHelper.Copy(source, resource.Pointer, 0); } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0,(ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } /// public override unsafe void SetData(Buffer source) { GraphicsDevice.ThrowIfDisposed(); source.GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); source.ThrowIfDisposed(); if (!source.IsPaddingPresent && source.GraphicsDevice == GraphicsDevice) { // Directly copy the input buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); copyCommandList.ExecuteAndWaitForCompletion(); } else SetDataWithCpuBuffer(source); } } }