using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_FEATURE;
using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1;
using static TerraFX.Interop.DXGI_ADAPTER_FLAG;
namespace ComputeSharp
{
///
/// A that represents a DX12-compatible GPU device that can be used to run compute shaders.
///
[DebuggerDisplay("{ToString(),raw}")]
public sealed unsafe class GraphicsDevice : 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 ID3D12CommandAllocatorPool computeCommandAllocatorPool;
///
/// Gets the instance for copy operations.
///
private readonly ID3D12CommandAllocatorPool copyCommandAllocatorPool;
///
/// The instance to use when allocating new buffers.
///
private ID3D12DescriptorHandleAllocator 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 GraphicsDevice(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 ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY);
this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3D12Device);
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
Name = new string((char*)dxgiDescription1->Description);
DedicatedMemorySize = dxgiDescription1->DedicatedVideoMemory;
SharedMemorySize = dxgiDescription1->SharedSystemMemory;
IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0;
var d3D12Options1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1);
ComputeUnits = d3D12Options1Data.TotalLaneCount;
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE1);
IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0;
}
///
/// Gets the locally unique identifier for the current device.
///
public Luid Luid { get; }
///
/// Gets the name of the current instance.
///
public string Name { get; }
///
/// Gets the size of the dedicated memory for the current device.
///
public nuint DedicatedMemorySize { get; }
///
/// Gets the size of the shared system memory for the current device.
///
public nuint SharedMemorySize { get; }
///
/// Gets whether or not the current device is hardware accelerated.
/// This value is for software fallback devices.
///
public bool IsHardwareAccelerated { 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;
///
/// Gets whether or not the current device has a cache coherent UMA architecture.
///
internal bool IsCacheCoherentUMA { get; }
///
/// Checks whether the current device supports the creation of
/// resources for a specified type .
///
/// The type of values to check support for.
/// Whether instances can be created by the current device.
[Pure]
public bool IsReadOnlyTexture2DSupportedForType()
where T : unmanaged
{
ThrowIfDisposed();
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D);
}
///
/// Checks whether the current device supports the creation of
/// resources for a specified type .
///
/// The type of values to check support for.
/// Whether instances can be created by the current device.
[Pure]
public bool IsReadWriteTexture2DSupportedForType()
where T : unmanaged
{
ThrowIfDisposed();
return this.d3D12Device.Get()->IsDxgiFormatSupported(
DXGIFormatHelper.GetForType(),
D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
}
///
/// Checks whether the current device supports the creation of
/// resources for a specified type .
///
/// The type of values to check support for.
/// Whether instances can be created by the current device.
[Pure]
public bool IsReadOnlyTexture3DSupportedForType()
where T : unmanaged
{
ThrowIfDisposed();
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D);
}
///
/// Checks whether the current device supports the creation of
/// resources for a specified type .
///
/// The type of values to check support for.
/// Whether instances can be created by the current device.
[Pure]
public bool IsReadWriteTexture3DSupportedForType()
where T : unmanaged
{
ThrowIfDisposed();
return this.d3D12Device.Get()->IsDxgiFormatSupported(
DXGIFormatHelper.GetForType(),
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
}
///
internal void RentShaderResourceViewDescriptorHandles(
out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle);
}
///
internal void ReturnShaderResourceViewDescriptorHandles(
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle);
}
///
/// 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.copyCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12CopyFence),
_ => ThrowHelper.ThrowArgumentException>()
};
}
///
/// 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);
}
///
/// Executes a given command list and waits for the operation to be completed.
///
/// The input to execute.
internal void ExecuteCommandList(ref CommandList commandList)
{
ref readonly ID3D12CommandAllocatorPool commandAllocatorPool = ref Unsafe.NullRef();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ulong d3D12FenceValue;
// Get the target command queue, fence and pool for the list type
switch (commandList.D3D12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
commandAllocatorPool = ref this.computeCommandAllocatorPool;
d3D12CommandQueue = this.d3D12ComputeCommandQueue;
d3D12Fence = this.d3D12ComputeFence;
d3D12FenceValue = this.nextD3D12ComputeFenceValue++;
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
commandAllocatorPool = ref this.copyCommandAllocatorPool;
d3D12CommandQueue = this.d3D12CopyCommandQueue;
d3D12Fence = this.d3D12CopyFence;
d3D12FenceValue = this.nextD3D12CopyFenceValue++;
break;
default: ThrowHelper.ThrowArgumentException(); return;
}
// Execute the command list and signal to the target fence
d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue).Assert();
// If the fence value hasn't been reached, wait until the operation completes
if (d3D12FenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default).Assert();
}
// Enqueue the command allocator pool so that it can be reused later
commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator());
}
///
protected override bool OnDispose()
{
if (DeviceHelper.GetDefaultDeviceLuid() == Luid)
{
return false;
}
DeviceHelper.NotifyDisposedDevice(this);
this.d3D12Device.Dispose();
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();
this.d3D12CopyFence.Dispose();
this.computeCommandAllocatorPool.Dispose();
this.copyCommandAllocatorPool.Dispose();
this.shaderResourceViewDescriptorAllocator.Dispose();
return true;
}
///
public override string ToString()
{
return $"[{Luid}] {Name}";
}
}
}