using System;
using System.Diagnostics.Contracts;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Resources.Enums;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS;
using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY;
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_FEATURE;
using static TerraFX.Interop.D3D12_FENCE_FLAGS;
using static TerraFX.Interop.D3D12_MEMORY_POOL;
using static TerraFX.Interop.D3D12_HEAP_FLAGS;
using static TerraFX.Interop.D3D12_HEAP_TYPE;
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
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 = (int)D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
d3D12CommandQueueDesc.NodeMask = 0;
d3D12Device.CreateCommandQueue(
&d3D12CommandQueueDesc,
FX.__uuidof(),
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;
d3D12Device.CreateFence(
0,
D3D12_FENCE_FLAG_NONE,
FX.__uuidof(),
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;
d3D12Device.CreateDescriptorHeap(
&d3D12DescriptorHeapDesc,
FX.__uuidof(),
d3D12DescriptorHeap.GetVoidAddressOf()).Assert();
return d3D12DescriptorHeap.Move();
}
///
/// Creates a committed resource for a given buffer type.
///
/// The instance in use.
/// The resource type currently in use.
/// The allocation mode to use for the new resource.
/// The size in bytes of the current buffer.
/// Indicates whether or not the current device has a cache coherent UMA architecture.
/// An reference for the current buffer.
public static ComPtr CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
AllocationMode allocationMode,
ulong sizeInBytes,
bool isCacheCoherentUMA)
{
D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE;
(D3D12_HEAP_TYPE d3D12HeapType,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch
{
ResourceType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
_ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = d3D12HeapType;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
d3D12HeapFlags,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
FX.__uuidof(),
d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
///
/// Creates a committed resource for a given 2D texture type.
///
/// The instance in use.
/// The resource type currently in use.
/// The allocation mode to use for the new resource.
/// The value to use.
/// The width of the texture resource.
/// The height of the texture resource.
/// Indicates whether or not the current device has a cache coherent UMA architecture.
/// The default value for the resource.
/// An reference for the current texture.
public static ComPtr CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
bool isCacheCoherentUMA,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
d3D12HeapFlags,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
FX.__uuidof(),
d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
///
/// Creates a committed resource for a given 3D texture type.
///
/// The instance in use.
/// The resource type currently in use.
/// The allocation mode to use for the new resource.
/// The value to use.
/// The width of the texture resource.
/// The height of the texture resource.
/// The depth of the texture resource.
/// Indicates whether or not the current device has a cache coherent UMA architecture.
/// The default value for the resource.
/// An reference for the current texture.
public static ComPtr CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
ushort depth,
bool isCacheCoherentUMA,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
d3D12HeapFlags,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
FX.__uuidof(),
d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
///
/// Creates a view for a constant buffer.
///
/// The target instance in use.
/// The to create a view for.
/// The size of the target resource.
/// The instance for the current resource.
public static void CreateConstantBufferView(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
nint bufferSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
uint constantBufferSize = checked((uint)((bufferSize + 255) & ~255));
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
d3D12ConstantBufferViewDescription.BufferLocation = d3D12Resource->GetGPUVirtualAddress();
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
d3D12Device.CreateConstantBufferView(&d3D12ConstantBufferViewDescription, d3D12CpuDescriptorHandle);
}
///
/// Creates a view for a readonly buffer.
///
/// The target instance in use.
/// The to create a view for.
/// The size of the target resource.
/// The size in byte of each item in the resource.
/// The instance for the current resource.
public static void CreateShaderResourceView(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
uint bufferSize,
uint elementSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
d3D12ShaderResourceViewDescription.Buffer.NumElements = bufferSize;
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = elementSize;
d3D12Device.CreateShaderResourceView(d3D12Resource, &d3D12ShaderResourceViewDescription, d3D12CpuDescriptorHandle);
}
///
/// Creates a view for a readonly texture.
///
/// The target instance in use.
/// The to create a view for.
/// The value to use.
/// The value for the view to create.
/// The instance for the current resource.
public static void CreateShaderResourceView(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
DXGI_FORMAT dxgiFormat,
D3D12_SRV_DIMENSION d3D12SrvDimension,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
d3D12ShaderResourceViewDescription.ViewDimension = d3D12SrvDimension;
d3D12ShaderResourceViewDescription.Format = dxgiFormat;
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
d3D12ShaderResourceViewDescription.Texture2D.MipLevels = uint.MaxValue;
d3D12Device.CreateShaderResourceView(d3D12Resource, &d3D12ShaderResourceViewDescription, d3D12CpuDescriptorHandle);
}
///
/// Creates a view for a buffer that can be both read and written to.
///
/// The target instance in use.
/// The to create a view for.
/// The size of the target resource.
/// The size in byte of each item in the resource.
/// The instance for the current resource.
public static void CreateUnorderedAccessView(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
uint bufferSize,
uint elementSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
d3D12UnorderedAccessViewDescription.Buffer.NumElements = bufferSize;
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = elementSize;
d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
///
/// Creates a view for a texture that can be both read and written to.
///
/// The target instance in use.
/// The to create a view for.
/// The value to use.
/// The value for the view to create.
/// The instance for the current resource.
public static void CreateUnorderedAccessView(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
DXGI_FORMAT dxgiFormat,
D3D12_UAV_DIMENSION d3D12UavDimension,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = d3D12UavDimension;
d3D12UnorderedAccessViewDescription.Format = dxgiFormat;
d3D12UnorderedAccessViewDescription.Texture3D.WSize = uint.MaxValue;
d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
///
/// 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;
d3D12Device.CreateCommandAllocator(
d3D12CommandListType,
FX.__uuidof(),
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.
/// The command allocator to use to create the command list.
/// 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;
d3D12Device.CreateCommandList(
0,
d3D12CommandListType,
d3D12CommandAllocator,
null,
FX.__uuidof(),
d3D12GraphicsCommandList.GetVoidAddressOf()).Assert();
return d3D12GraphicsCommandList.Move();
}
///
/// Gets the layout data for a target resource.
///
/// The target to use to get the layout info.
/// The value for the target resource.
/// The resulting layout info for the resource.
/// The size in bytes of each row in the resource.
/// The total number of bytes for the resource.
public static void GetCopyableFootprint(
this ref ID3D12Device d3D12Device,
D3D12_RESOURCE_DESC* d3D12ResourceDesc,
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint,
out ulong rowSizeInBytes,
out ulong totalSizeInBytes)
{
ulong a, b;
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* p = &d3D12PlacedSubresourceFootprint)
{
uint _;
d3D12Device.GetCopyableFootprints(d3D12ResourceDesc, 0, 1, 0, p, &_, &a, &b);
}
rowSizeInBytes = a;
totalSizeInBytes = b;
}
///
/// 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;
}
///
/// Checks whether or not a given DXGI format is supported for the specified resource type.
///
/// The target to use to check features for.
/// The type to check support for.
/// The resource type to check support for.
/// Whether or not the input device supports the requested format for the specified resource type.
[Pure]
public static unsafe bool IsDxgiFormatSupported(this ref ID3D12Device d3D12Device, DXGI_FORMAT dxgiFormat, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport1)
{
D3D12_FEATURE_DATA_FORMAT_SUPPORT d3D12FeatureDataFormatSupport = default;
d3D12FeatureDataFormatSupport.Format = dxgiFormat;
d3D12Device.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &d3D12FeatureDataFormatSupport, (uint)sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)).Assert();
return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) == d3D12FormatSupport1;
}
}
}