using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
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 unsafe ref struct CommandList
{
///
/// The instance associated with the current command list.
///
private readonly GraphicsDevice 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 ComPtr d3D12CommandAllocator;
///
/// The object in use by the current instance.
///
private ComPtr d3D12GraphicsCommandList;
///
/// Creates a new instance with the specified parameters.
///
/// The target instance to use.
/// The type of command list to create.
public CommandList(GraphicsDevice device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
this.device = device;
this.d3d12CommandListType = d3d12CommandListType;
this.d3D12CommandAllocator = device.GetCommandAllocator(d3d12CommandListType);
this.d3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3d12CommandListType, this.d3D12CommandAllocator);
// Set the heap descriptor if the command list is not for copy operations
if (d3d12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY)
{
device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
}
}
///
/// Gets the command list type being used by the current instance.
///
public readonly D3D12_COMMAND_LIST_TYPE D3d12CommandListType => this.d3d12CommandListType;
///
/// Detaches the object in use by the current instance.
///
/// The object in use, with ownership.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ComPtr DetachD3D12CommandAllocator()
{
return this.d3D12CommandAllocator.Move();
}
///
/// Gets a pointer to the object in use by the current instance.
///
/// A double pointer to the current object to execute.
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ID3D12CommandList** GetD3D12CommandListAddressOf()
{
return this.d3D12GraphicsCommandList.Upcast().GetAddressOf();
}
///
/// 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 readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ulong numBytes)
{
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes);
}
///
/// Binds an input value to a specified root parameter.
///
/// The root parameter index to bind to the input resource.
/// The input value to bind.
public readonly void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
}
///
/// Sets a given object ready to be executed.
///
/// The input to setup.
public readonly void SetPipelineData(PipelineData pipelineData)
{
this.d3D12GraphicsCommandList.Get()->SetComputeRootSignature(pipelineData.D3D12RootSignature);
this.d3D12GraphicsCommandList.Get()->SetPipelineState(pipelineData.D3D12PipelineState);
}
///
/// 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 readonly void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
{
this.d3D12GraphicsCommandList.Get()->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
}
///
/// Executes the commands in the current commands list, and waits for completion.
///
public void ExecuteAndWaitForCompletion()
{
this.d3D12GraphicsCommandList.Get()->Close();
this.device.ExecuteCommandList(ref this);
}
///
public void Dispose()
{
this.d3D12CommandAllocator.Dispose();
this.d3D12GraphicsCommandList.Dispose();
}
}
}