using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Graphics.Resources.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Resources { /// /// 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 unsafe StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType) : base(device, length, (uint)sizeof(T), resourceType) { } /// internal override unsafe void GetData(ref T destination, int size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); if (GraphicsDevice.IsCacheCoherentUMA) { using ID3D12ResourceMap resource = D3D12Resource->Map(); fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, (uint)offset, (uint)size, (uint)sizeof(T), destinationPointer); } } else { nint byteOffset = (nint)offset * sizeof(T), byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize, false); 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(); fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, 0u, (uint)size, (uint)sizeof(T), destinationPointer); } } } /// internal override unsafe void SetData(ref T source, int size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); if (GraphicsDevice.IsCacheCoherentUMA) { using ID3D12ResourceMap resource = D3D12Resource->Map(); fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, (uint)offset, (uint)size, (uint)sizeof(T), resource.Pointer); } } else { nint byteOffset = (nint)offset * sizeof(T), byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize, false); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, 0u, (uint)size, (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.ExecuteAndWaitForCompletion(); } } /// public override unsafe void SetData(Buffer source) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDeviceMismatch(source.GraphicsDevice); 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); } } }