using ComputeSharp.Graphics.Helpers; using System; using System.Diagnostics.Contracts; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; using static TerraFX.Interop.D3D12_CPU_PAGE_PROPERTY; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Extensions { /// /// A with extensions for the type. /// internal static unsafe class ID3D12DeviceExtensions { /// /// 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 ComPtr CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type) { using ComPtr d3D12CommandQueue = default; D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc; d3d12CommandQueueDesc.Type = type; d3d12CommandQueueDesc.Priority = 0; d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; d3d12CommandQueueDesc.NodeMask = 0; Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue; d3d12device.CreateCommandQueue( &d3d12CommandQueueDesc, &d3d12CommandQueueDescGuid, d3D12CommandQueue.GetVoidAddressOf()).Assert(); return d3D12CommandQueue.Move(); } /// /// 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 ComPtr CreateFence(this ref ID3D12Device d3d12device) { using ComPtr d3d12Fence = default; Guid d3D12FenceGuid = FX.IID_ID3D12Fence; d3d12device.CreateFence( 0, D3D12_FENCE_FLAG_NONE, &d3D12FenceGuid, d3d12Fence.GetVoidAddressOf()).Assert(); return d3d12Fence.Move(); } /// /// 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 ComPtr CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount) { using ComPtr d3d12DescriptorHeap = default; D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; d3d12DescriptorHeapDesc.NumDescriptors = descriptorsCount; d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; d3d12DescriptorHeapDesc.NodeMask = 0; Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; d3d12device.CreateDescriptorHeap( &d3d12DescriptorHeapDesc, &d3d12DescriptorHeapDescGuid, d3d12DescriptorHeap.GetVoidAddressOf()).Assert(); return d3d12DescriptorHeap.Move(); } /// /// 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 ComPtr CreateCommittedResource( this ref ID3D12Device d3d12device, D3D12_HEAP_TYPE d3D12HeapType, ulong width, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) { using ComPtr d3D12Resource = default; D3D12_HEAP_PROPERTIES d3D12HeapProperties; d3D12HeapProperties.Type = d3D12HeapType; d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; d3D12HeapProperties.CreationNodeMask = 1; d3D12HeapProperties.VisibleNodeMask = 1; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(width, d3D12ResourceFlags); Guid d3D12ResourceGuid = FX.IID_ID3D12Resource; d3d12device.CreateCommittedResource( &d3D12HeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &d3D12ResourceDescription, d3D12ResourceStates, null, &d3D12ResourceGuid, d3D12Resource.GetVoidAddressOf()).Assert(); return d3D12Resource.Move(); } /// /// Creates a new for a given device. /// /// The target to use to create the command allocator. /// The type of command list allocator to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command allocator fails. public static ComPtr CreateCommandAllocator( this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType) { using ComPtr d3D12CommandAllocator = default; Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator; d3d12device.CreateCommandAllocator( d3d12CommandListType, &d3D12CommandAllocatorGuid, d3D12CommandAllocator.GetVoidAddressOf()).Assert(); return d3D12CommandAllocator.Move(); } /// /// Creates a new for a given device. /// /// The target to use to create the command list. /// The type of command list to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command list fails. public static ComPtr CreateCommandList( this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType, ID3D12CommandAllocator* d3D12CommandAllocator) { using ComPtr d3d12GraphicsCommandList = default; Guid d3D12GraphicsCommandListGuid = FX.IID_ID3D12GraphicsCommandList; d3d12device.CreateCommandList( 0, d3d12CommandListType, d3D12CommandAllocator, null, &d3D12GraphicsCommandListGuid, d3d12GraphicsCommandList.GetVoidAddressOf()).Assert(); return d3d12GraphicsCommandList.Move(); } /// /// Checks the feature support of a given type for a given device. /// /// The type of feature support data to retrieve. /// The target to use to check features for. /// The type of features to check. /// A value with the features data. [Pure] public static unsafe TFeature CheckFeatureSupport(this ref ID3D12Device d3d12device, D3D12_FEATURE d3d12feature) where TFeature : unmanaged { TFeature feature; d3d12device.CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)).Assert(); return feature; } } }