using System; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Commands.Abstract; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Commands { /// /// A that represents a list of commands to issue to a GPU /// internal sealed class CommandList : CommandController { /// public CommandList(GraphicsDevice device, CommandListType commandListType) : base(device, commandListType) { CommandAllocator = CommandListType switch { CommandListType.Bundle => GraphicsDevice.BundleAllocatorPool.GetCommandAllocator(), CommandListType.Compute => GraphicsDevice.ComputeAllocatorPool.GetCommandAllocator(), CommandListType.Copy => GraphicsDevice.CopyAllocatorPool.GetCommandAllocator(), CommandListType.Direct => GraphicsDevice.DirectAllocatorPool.GetCommandAllocator(), _ => throw new NotSupportedException("This command list type is not supported.") }; NativeCommandList = GraphicsDevice.NativeDevice.CreateCommandList(CommandListType, CommandAllocator, null); // Set the heap descriptor if the command list is for copy operations if (CommandListType != CommandListType.Copy) { NativeCommandList.SetDescriptorHeaps(GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap); } } /// /// Gets the object in use by the current instance /// public CommandAllocator CommandAllocator { get; } /// /// Gets the object in use by the current instance /// public GraphicsCommandList NativeCommandList { get; } /// /// Copies a memory region from one resource to another /// /// The source to read from /// The starting offset to read the source resource from /// The destination to write to /// The starting offset to write the destination resource from /// The total number of bytes to copy from one resource to another public void CopyBufferRegion(GraphicsResource source, long sourceOffset, GraphicsResource destination, long destinationOffset, long numBytes) { NativeCommandList.CopyBufferRegion(destination.NativeResource, destinationOffset, source.NativeResource, sourceOffset, numBytes); } /// /// Binds an input object to a specified root parameter /// /// The root parameter index to bind to the input resource /// The input instance to bind public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); NativeCommandList.SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } /// /// Sets a given object ready to be executed /// /// The input to setup public void SetPipelineState(PipelineState pipelineState) { NativeCommandList.SetComputeRootSignature(pipelineState.RootSignature); NativeCommandList.PipelineState = pipelineState.NativePipelineState; } /// /// Executes the pending command list using the specified thread group values /// /// The number of thread groups to schedule for the X axis /// The number of thread groups to schedule for the Y axis /// The number of thread groups to schedule for the Z axis public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ) { NativeCommandList.Dispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ); } /// /// Executes the pending operations and optionally waits for them to be completed /// /// Indicates whether or not to wait for the completion of the scheduled operations public void Flush(bool wait = false) { Close(); GraphicsDevice.ExecuteCommandLists(wait, this); } /// /// Closes the current native command list /// public void Close() => NativeCommandList.Close(); /// public override void Dispose() { switch (CommandListType) { case CommandListType.Bundle: GraphicsDevice.BundleAllocatorPool.Enqueue(CommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); break; case CommandListType.Direct: GraphicsDevice.DirectAllocatorPool.Enqueue(CommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); break; case CommandListType.Compute: GraphicsDevice.ComputeAllocatorPool.Enqueue(CommandAllocator, GraphicsDevice.NextComputeFenceValue - 1); break; case CommandListType.Copy: GraphicsDevice.CopyAllocatorPool.Enqueue(CommandAllocator, GraphicsDevice.NextCopyFenceValue - 1); break; default: throw new NotSupportedException("This command list type is not supported."); } NativeCommandList.Dispose(); } } }