diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs index cb33f8e4..ecd83f64 100644 --- a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs @@ -1,7 +1,6 @@ -using System; -using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Helpers; +using System; using TerraFX.Interop; -using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics { @@ -51,28 +50,13 @@ namespace ComputeSharp.Graphics /// The instance to use public DescriptorAllocator2(ID3D12Device* device) { - D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; - d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - d3d12DescriptorHeapDesc.NumDescriptors = DescriptorsPerHeap; - d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - d3d12DescriptorHeapDesc.NodeMask = 0; - Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; - ID3D12DescriptorHeap* d3d12DescriptorHeap; - - int result = device->CreateDescriptorHeap( - &d3d12DescriptorHeapDesc, - &d3d12DescriptorHeapDescGuid, - (void**)&d3d12DescriptorHeap); - - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); - - D3D12DescriptorHeap = d3d12DescriptorHeap; + D3D12DescriptorHeap = D3D12Helper.CreateDescriptorHeap(device, DescriptorsPerHeap); this.allocationLock = new object(); this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - this.d3d12CpuDescriptorHandle = d3d12DescriptorHeap->GetCPUDescriptorHandleForHeapStart(); - this.d3d12GpuDescriptorHandle = d3d12DescriptorHeap->GetGPUDescriptorHandleForHeapStart(); - this.remainingHandles = DescriptorsPerHeap; + this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart(); + this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart(); + this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors; } /// @@ -86,9 +70,9 @@ namespace ComputeSharp.Graphics { if (this.remainingHandles == 0) { - this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors; this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart(); this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart(); + this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors; } d3d12CpuDescriptorHandle = this.d3d12CpuDescriptorHandle; diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs new file mode 100644 index 00000000..9f82ccd2 --- /dev/null +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -0,0 +1,121 @@ +using ComputeSharp.Core.Interop; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Helpers; +using TerraFX.Interop; + +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. + /// + internal readonly ID3D12Device* D3D12Device; + + /// + /// The instance to use for compute operations. + /// + internal readonly ID3D12CommandQueue* D3D12ComputeCommandQueue; + + /// + /// The instance to use for copy operations. + /// + internal readonly ID3D12CommandQueue* D3D12CopyCommandQueue; + + /// + /// The instance used for compute operations. + /// + internal readonly ID3D12Fence* D3D12ComputeFence; + + /// + /// The instance used for copy operations. + /// + internal readonly ID3D12Fence* D3D12CopyFence; + + /// + /// The instance for compute operations. + /// + internal readonly CommandAllocatorPool2 ComputeCommandAllocatorPool; + + /// + /// Gets the instance for copy operations. + /// + internal readonly CommandAllocatorPool2 CopyCommandAllocatorPool; + + /// + /// The instance to use when allocating new buffers. + /// + internal readonly DescriptorAllocator2 ShaderResourceViewDescriptorAllocator; + + /// + /// The next fence value for compute operations using . + /// + internal long NextD3D12ComputeFenceValue = 1; + + /// + /// The next fence value for copy operations using . + /// + internal long 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(ID3D12Device* d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1) + { + D3D12Device = d3d12device; + D3D12ComputeCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE); + D3D12CopyCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY); + D3D12ComputeFence = D3D12Helper.CreateFence(d3d12device); + D3D12CopyFence = D3D12Helper.CreateFence(d3d12device); + ComputeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE); + CopyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY); + ShaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device); + + Name = new string((char*)dxgiDescription1->Description); + MemorySize = dxgiDescription1->DedicatedVideoMemory; + + var d3D12Options1Data = D3D12Helper.CheckFeatureSupport(d3d12device, D3D12_FEATURE.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; } + + /// + protected override void Dispose(bool disposing) + { + D3D12Device->Release(); + D3D12ComputeCommandQueue->Release(); + D3D12CopyCommandQueue->Release(); + D3D12ComputeFence->Release(); + D3D12CopyFence->Release(); + ComputeCommandAllocatorPool.Dispose(); + CopyCommandAllocatorPool.Dispose(); + ShaderResourceViewDescriptorAllocator.Dispose(); + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs b/src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs new file mode 100644 index 00000000..0e384c7f --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs @@ -0,0 +1,103 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A with helper methods to perform some D3D12 operations. + /// + internal static unsafe class D3D12Helper + { + /// + /// Creates a new of the specified type, for a given device. + /// + /// The target to use to create the command queue. + /// The type of command queue to create. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command queue fails. + public static ID3D12CommandQueue* CreateCommandQueue(ID3D12Device* d3d12device, D3D12_COMMAND_LIST_TYPE type) + { + D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc; + d3d12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE; + d3d12CommandQueueDesc.Priority = 0; + d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE; + d3d12CommandQueueDesc.NodeMask = 0; + Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue; + ID3D12CommandQueue* commandQueue; + + int result = d3d12device->CreateCommandQueue( + &d3d12CommandQueueDesc, + &d3d12CommandQueueDescGuid, + (void**)&commandQueue); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return commandQueue; + } + + /// + /// Creates a new for a given device. + /// + /// The target to use to create the fence. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command queue fails. + public static ID3D12Fence* CreateFence(ID3D12Device* d3d12device) + { + Guid d3D12FenceGuid = FX.IID_ID3D12Fence; + ID3D12Fence* d3d12Fence; + + int result = d3d12device->CreateFence( + 0, + D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE, + &d3D12FenceGuid, + (void**)&d3d12Fence); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3d12Fence; + } + + /// + /// Creates a new for a given device. + /// + /// The target to use to create the descriptor heap. + /// The number of descriptors to allocate. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command queue fails. + public static ID3D12DescriptorHeap* CreateDescriptorHeap(ID3D12Device* d3d12device, uint descriptorsCount) + { + D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; + d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + d3d12DescriptorHeapDesc.NumDescriptors = descriptorsCount; + d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + d3d12DescriptorHeapDesc.NodeMask = 0; + Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; + ID3D12DescriptorHeap* d3d12DescriptorHeap; + + int result = d3d12device->CreateDescriptorHeap( + &d3d12DescriptorHeapDesc, + &d3d12DescriptorHeapDescGuid, + (void**)&d3d12DescriptorHeap); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3d12DescriptorHeap; + } + + [Pure] + public static unsafe TFeature CheckFeatureSupport(ID3D12Device* d3d12device, D3D12_FEATURE d3d12feature) + where TFeature : unmanaged + { + TFeature feature; + + int result = d3d12device->CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return feature; + } + } +}