From 3b6f2e117edb83860f40135a9abf79fbc509251b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Nov 2020 13:19:45 +0100 Subject: [PATCH] Added initial version of TerraFX CommandList type --- .../Commands/CommandList2.cs | 131 ++++++++++++++++++ .../Extensions/ID3D12DeviceExtensions.cs | 30 +++- src/ComputeSharp.Graphics/GraphicsDevice2.cs | 4 +- 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Commands/CommandList2.cs diff --git a/src/ComputeSharp.Graphics/Commands/CommandList2.cs b/src/ComputeSharp.Graphics/Commands/CommandList2.cs new file mode 100644 index 00000000..2bdbed00 --- /dev/null +++ b/src/ComputeSharp.Graphics/Commands/CommandList2.cs @@ -0,0 +1,131 @@ +using System; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Extensions; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; + +namespace ComputeSharp.Graphics.Commands +{ + /// + /// A command list to set and execute operaations on the GPU. + /// + internal readonly unsafe ref struct CommandList2 + { + /// + /// The instance associated with the current command list. + /// + private readonly GraphicsDevice2 device; + + /// + /// The command list type being used by the current instance. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3d12CommandListType; + + /// + /// The object in use by the current instance. + /// + private readonly ID3D12CommandAllocator* d3D12CommandAllocator; + + /// + /// The object in use by the current instance. + /// + public readonly ID3D12GraphicsCommandList* D3D12GraphicsCommandList; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The target instance to use. + /// The type of command list to create. + public CommandList2(GraphicsDevice2 device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType) + { + this.device = device; + this.d3d12CommandListType = d3d12CommandListType; + this.d3D12CommandAllocator = d3d12CommandListType switch + { + D3D12_COMMAND_LIST_TYPE_COMPUTE => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12ComputeFence), + D3D12_COMMAND_LIST_TYPE_COPY => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12CopyFence), + _ => throw null! + }; + + D3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3d12CommandListType, d3D12CommandAllocator); + + // Set the heap descriptor if the command list is not for copy operations + if (d3d12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY) + { + ID3D12DescriptorHeap* d3D12DescriptorHeap = device.ShaderResourceViewDescriptorAllocator.D3D12DescriptorHeap; + + D3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap); + } + } + + /// + /// 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(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes) + { + D3D12GraphicsCommandList->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)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, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + { + D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle); + } + + /// + /// Sets a given object ready to be executed. + /// + /// The input to setup. + public void SetPipelineState(PipelineState pipelineState) + { + // TODO: D3D12GraphicsCommandList->SetComputeRootSignature() + // TODO: D3D12GraphicsCommandList->SetPipelineState() + } + + /// + /// Dispatches the pending shader 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) + { + D3D12GraphicsCommandList->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ); + } + + /// + /// Executes the commands in the current commands list, and waits for completion. + /// + public void ExecuteAndWaitForCompletion() + { + D3D12GraphicsCommandList->Close(); + + // TODO: GraphicsDevice.ExecuteCommandList(this); + } + + /// + public void Dispose() + { + switch (this.d3d12CommandListType) + { + case D3D12_COMMAND_LIST_TYPE_COMPUTE: + device.ComputeCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12ComputeFenceValue); + break; + case D3D12_COMMAND_LIST_TYPE_COPY: + device.CopyCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12CopyFenceValue); + break; + }; + + D3D12GraphicsCommandList->Release(); + } + } +} diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index a36ce53c..a16d0a56 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -136,7 +136,7 @@ namespace ComputeSharp.Graphics.Extensions /// Creates a new for a given device. /// /// The target to use to create the command allocator. - /// The type of command list to create. + /// The type of command list allocator to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command allocator fails. public static ID3D12CommandAllocator* CreateCommandAllocator( @@ -156,6 +156,34 @@ namespace ComputeSharp.Graphics.Extensions return d3D12CommandAllocator; } + /// + /// Creates a new for a given device. + /// + /// The target to use to create the command list. + /// The type of command list to create. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command list fails. + public static ID3D12GraphicsCommandList* CreateCommandList( + this ref ID3D12Device d3d12device, + D3D12_COMMAND_LIST_TYPE d3d12CommandListType, + ID3D12CommandAllocator* d3D12CommandAllocator) + { + ID3D12GraphicsCommandList* d3d12GraphicsCommandList; + Guid d3D12GraphicsCommandListGuid = FX.IID_ID3D12GraphicsCommandList; + + int result = d3d12device.CreateCommandList( + 0, + d3d12CommandListType, + d3D12CommandAllocator, + null, + &d3D12GraphicsCommandListGuid, + (void**)&d3d12GraphicsCommandList); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3d12GraphicsCommandList; + } + /// /// Checks the feature support of a given type for a given device. /// diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs index ca3010f2..0e0b42b2 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -53,12 +53,12 @@ namespace ComputeSharp.Graphics /// /// The next fence value for compute operations using . /// - internal long NextD3D12ComputeFenceValue = 1; + internal ulong NextD3D12ComputeFenceValue = 1; /// /// The next fence value for copy operations using . /// - internal long NextD3D12CopyFenceValue = 1; + internal ulong NextD3D12CopyFenceValue = 1; /// /// Creates a new instance for the input .