using ComputeSharp.Core.Interop;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_FEATURE;
namespace ComputeSharp.Graphics
{
///
/// A that represents a DX12.1-compatible GPU device that can be used to run compute shaders.
///
public sealed unsafe class GraphicsDevice2 : NativeObject
{
///
/// The underlying wrapped by the current instance.
///
private ComPtr d3D12Device;
///
/// The instance to use for compute operations.
///
private ComPtr d3D12ComputeCommandQueue;
///
/// The instance to use for copy operations.
///
private ComPtr d3D12CopyCommandQueue;
///
/// The instance used for compute operations.
///
private ComPtr d3D12ComputeFence;
///
/// The instance used for copy operations.
///
private ComPtr d3D12CopyFence;
///
/// The instance for compute operations.
///
private readonly CommandAllocatorPool2 computeCommandAllocatorPool;
///
/// Gets the instance for copy operations.
///
private readonly CommandAllocatorPool2 copyCommandAllocatorPool;
///
/// The instance to use when allocating new buffers.
///
private DescriptorAllocator2 shaderResourceViewDescriptorAllocator;
///
/// The next fence value for compute operations using .
///
private ulong nextD3D12ComputeFenceValue = 1;
///
/// The next fence value for copy operations using .
///
private ulong nextD3D12CopyFenceValue = 1;
///
/// Creates a new instance for the input .
///
/// The to use for the new instance.
/// The available info for the new instance.
internal GraphicsDevice2(ComPtr d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
this.d3D12Device = d3d12device;
this.d3D12ComputeCommandQueue = d3d12device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.d3D12CopyCommandQueue = d3d12device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
this.d3D12ComputeFence = d3d12device.Get()->CreateFence();
this.d3D12CopyFence = d3d12device.Get()->CreateFence();
this.computeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.copyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COPY);
this.shaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device);
Name = new string((char*)dxgiDescription1->Description);
MemorySize = dxgiDescription1->DedicatedVideoMemory;
var d3D12Options1Data = d3d12device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1);
ComputeUnits = d3D12Options1Data.TotalLaneCount;
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
}
///
/// Gets the name of the current instance.
///
public string Name { get; }
///
/// Gets the size of the dedicated video memory for the current instance.
///
public nuint MemorySize { get; }
///
/// Gets the number of total lanes on the current device (eg. CUDA cores on an nVidia GPU).
///
public uint ComputeUnits { get; }
///
/// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width").
///
public uint WavefrontSize { get; }
///
/// Gets the underlying wrapped by the current instance.
///
internal ID3D12Device* D3D12Device => this.d3D12Device;
///
internal void AllocateShaderResourceViewDescriptorHandles(
out D3D12_CPU_DESCRIPTOR_HANDLE d3d12CpuDescriptorHandle,
out D3D12_GPU_DESCRIPTOR_HANDLE d3d12GpuDescriptorHandle)
{
this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle);
}
///
/// The instance to return to the pool.
/// The type of command allocator to return to the pool.
internal void EnqueueCommandAllocator(ComPtr d3D12CommandAllocator, D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
switch (d3d12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
this.computeCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12ComputeFenceValue);
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
this.copyCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12CopyFenceValue);
break;
};
}
///
/// The type of command allocator to rent.
internal ComPtr GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
return d3d12CommandListType switch
{
D3D12_COMMAND_LIST_TYPE_COMPUTE => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12ComputeFence),
D3D12_COMMAND_LIST_TYPE_COPY => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12CopyFence),
_ => throw null!
};
}
///
/// Sets the descriptor heap for a given instance.
///
/// The input instance to use.
internal void SetDescriptorHeapForCommandList(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
{
ID3D12DescriptorHeap* d3D12DescriptorHeap = this.shaderResourceViewDescriptorAllocator.D3D12DescriptorHeap;
d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
}
///
protected override void Dispose(bool disposing)
{
this.d3D12Device.Dispose();
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();
this.d3D12CopyFence.Dispose();
this.computeCommandAllocatorPool.Dispose();
this.copyCommandAllocatorPool.Dispose();
this.shaderResourceViewDescriptorAllocator.Dispose();
}
}
}