using System;
using System.Threading;
using ComputeSharp.Graphics.Commands;
using Vortice.DirectX.Direct3D;
using Vortice.DirectX.Direct3D12;
using Vortice.DirectX.DXGI;
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
namespace ComputeSharp.Graphics
{
///
/// A that represents a DX12.1-compatible GPU device that can be used to run compute shaders
///
public sealed class GraphicsDevice : IDisposable
{
///
/// Gets the value that needs to be supported by GPUs mapped to a instance
///
public const FeatureLevel FeatureLevel = Vortice.DirectX.Direct3D.FeatureLevel.Level_12_1;
///
/// The instance used to wait for completion when executing commands
///
private readonly AutoResetEvent AutoResetEvent = new AutoResetEvent(false);
///
/// The instance to use for compute operations
///
private readonly ID3D12CommandQueue NativeComputeCommandQueue;
///
/// The instance to use for copy operations
///
private readonly ID3D12CommandQueue NativeCopyCommandQueue;
///
/// The instance to use for direct operations
///
private readonly ID3D12CommandQueue NativeDirectCommandQueue;
///
/// Creates a new instance for the input
///
/// The to use for the new instance
/// The available info for the new instance
public GraphicsDevice(ID3D12Device device, AdapterDescription description)
{
NativeDevice = device;
Description = description;
WavefrontSize = 64;
NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(CommandListType.Compute));
NativeCopyCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(CommandListType.Copy));
NativeDirectCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct));
ComputeAllocatorPool = new CommandAllocatorPool(this, CommandListType.Compute);
CopyAllocatorPool = new CommandAllocatorPool(this, CommandListType.Copy);
DirectAllocatorPool = new CommandAllocatorPool(this, CommandListType.Direct);
NativeComputeFence = NativeDevice.CreateFence(0);
NativeCopyFence = NativeDevice.CreateFence(0);
NativeDirectFence = NativeDevice.CreateFence(0);
ShaderResourceViewAllocator = new DescriptorAllocator(this);
}
///
/// Gets the available info for the current instance
///
public AdapterDescription Description { get; }
///
/// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width")
///
public int WavefrontSize { get; }
///
/// Gets the object wrapped by the current instance
///
internal ID3D12Device NativeDevice { get; }
///
/// Gets the object for the current instance, used when allocating new buffers
///
internal DescriptorAllocator ShaderResourceViewAllocator { get; set; }
///
/// Gets the instance for compute operations
///
internal CommandAllocatorPool ComputeAllocatorPool { get; }
///
/// Gets the instance for copy operations
///
internal CommandAllocatorPool CopyAllocatorPool { get; }
///
/// Gets the instance for direct operations
///
internal CommandAllocatorPool DirectAllocatorPool { get; }
///
/// Gets the instance used for compute operations
///
internal ID3D12Fence NativeComputeFence { get; }
///
/// Gets the instance used for copy operations
///
internal ID3D12Fence NativeCopyFence { get; }
///
/// Gets the instance used for direct operations
///
internal ID3D12Fence NativeDirectFence { get; }
///
/// Gets the next fence value for compute operations
///
internal long NextComputeFenceValue { get; private set; } = 1;
///
/// Gets the next fence value for copy operations
///
internal long NextCopyFenceValue { get; private set; } = 1;
///
/// Gets the next fence value for direct operations
///
internal long NextDirectFenceValue { get; private set; } = 1;
///
/// Creates a instance from a description
///
/// A instance with the info for the new object to create
/// A new instance to use in a compute shader
internal ID3D12RootSignature CreateRootSignature(VersionedRootSignatureDescription rootSignatureDescription)
{
return NativeDevice.CreateRootSignature(rootSignatureDescription);
}
///
/// Executes a and waits for the GPU to finish processing it
///
/// The input to execute
internal void ExecuteCommandList(CommandList commandList)
{
ID3D12Fence fence = commandList.CommandListType switch
{
CommandListType.Direct => NativeDirectFence,
CommandListType.Compute => NativeComputeFence,
CommandListType.Copy => NativeCopyFence,
_ => throw new NotSupportedException("This command list type is not supported.")
};
long fenceValue = GetFenceValueForCommandList(commandList);
if (fenceValue <= fence.CompletedValue) return;
lock (fence)
{
fence.SetEventOnCompletion(fenceValue, AutoResetEvent.SafeWaitHandle.DangerousGetHandle());
AutoResetEvent.WaitOne();
}
}
///
/// Executes a and, signals and retrieves the following fence value
///
/// The input to execute
/// The value of the new fence to wait for
private long GetFenceValueForCommandList(CommandList commandList)
{
CommandAllocatorPool commandAllocatorPool;
ID3D12CommandQueue commandQueue;
ID3D12Fence fence;
long fenceValue;
switch (commandList.CommandListType)
{
case CommandListType.Compute:
commandAllocatorPool = ComputeAllocatorPool;
commandQueue = NativeComputeCommandQueue;
fence = NativeComputeFence;
fenceValue = NextComputeFenceValue;
NextComputeFenceValue++;
break;
case CommandListType.Copy:
commandAllocatorPool = CopyAllocatorPool;
commandQueue = NativeCopyCommandQueue;
fence = NativeCopyFence;
fenceValue = NextCopyFenceValue;
NextCopyFenceValue++;
break;
case CommandListType.Direct:
commandAllocatorPool = DirectAllocatorPool;
commandQueue = NativeDirectCommandQueue;
fence = NativeDirectFence;
fenceValue = NextDirectFenceValue;
NextDirectFenceValue++;
break;
default: throw new NotSupportedException($"Unsupported command list of type {commandList.CommandListType}");
}
commandAllocatorPool.Enqueue(commandList.CommandAllocator, fenceValue);
commandQueue.ExecuteCommandLists(commandList.NativeCommandList);
commandQueue.Signal(fence, fenceValue);
return fenceValue;
}
///
public void Dispose()
{
NativeDirectCommandQueue.Signal(NativeDirectFence, NextDirectFenceValue);
NativeDirectCommandQueue.Wait(NativeDirectFence, NextDirectFenceValue);
ShaderResourceViewAllocator.Dispose();
ComputeAllocatorPool.Dispose();
CopyAllocatorPool.Dispose();
DirectAllocatorPool.Dispose();
NativeComputeCommandQueue.Dispose();
NativeCopyCommandQueue.Dispose();
NativeDirectCommandQueue.Dispose();
NativeComputeFence.Dispose();
NativeDirectFence.Dispose();
NativeDirectFence.Dispose();
NativeDevice.Dispose();
AutoResetEvent.Dispose();
}
}
}