diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12MemoryAllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12MemoryAllocatorExtensions.cs new file mode 100644 index 00000000..58e6f2e9 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12MemoryAllocatorExtensions.cs @@ -0,0 +1,180 @@ +using System; +using ComputeSharp.Core.Extensions; +using ComputeSharp.Interop.Allocation; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using static TerraFX.Interop.DirectX.D3D12_HEAP_TYPE; +using static TerraFX.Interop.DirectX.D3D12_RESOURCE_FLAGS; +using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES; +using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; + +namespace ComputeSharp.Graphics.Extensions; + +/// +/// A with extensions for the type. +/// +internal static unsafe class ID3D12MemoryAllocatorExtensions +{ + /// + /// Allocates a 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. + /// An reference for the current object. + public static ComPtr AllocateResource( + this ref ID3D12MemoryAllocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + ulong sizeInBytes) + { + (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), + _ => default(ArgumentException).Throw<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) + }; + + using ComPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); + + allocator.AllocateResource( + resourceDescription: &d3D12ResourceDescription, + heapType: d3D12HeapType, + resourceStates: d3D12ResourceStates, + clearAllocation: allocationMode == AllocationMode.Clear, + allocation: allocation.GetAddressOf()).Assert(); + + return allocation.Move(); + } + + /// + /// Allocates a resource for a given 1D 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 default value for the resource. + /// An reference for the current object. + public static ComPtr AllocateResource( + this ref ID3D12MemoryAllocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + (D3D12_RESOURCE_FLAGS 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), + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) + }; + + using ComPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags); + + allocator.AllocateResource( + resourceDescription: &d3D12ResourceDescription, + heapType: D3D12_HEAP_TYPE_DEFAULT, + resourceStates: d3D12ResourceStates, + clearAllocation: allocationMode == AllocationMode.Clear, + allocation: allocation.GetAddressOf()).Assert(); + + return allocation.Move(); + } + + /// + /// Allocates a 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. + /// The default value for the resource. + /// An reference for the current object. + public static ComPtr AllocateResource( + this ref ID3D12MemoryAllocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + (D3D12_RESOURCE_FLAGS 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), + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) + }; + + using ComPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags); + + allocator.AllocateResource( + resourceDescription: &d3D12ResourceDescription, + heapType: D3D12_HEAP_TYPE_DEFAULT, + resourceStates: d3D12ResourceStates, + clearAllocation: allocationMode == AllocationMode.Clear, + allocation: allocation.GetAddressOf()).Assert(); + + return allocation.Move(); + } + + /// + /// Allocates a 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. + /// The default value for the resource. + /// An reference for the current object. + public static ComPtr AllocateResource( + this ref ID3D12MemoryAllocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + ushort depth, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + (D3D12_RESOURCE_FLAGS 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), + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) + }; + + using ComPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags); + + allocator.AllocateResource( + resourceDescription: &d3D12ResourceDescription, + heapType: D3D12_HEAP_TYPE_DEFAULT, + resourceStates: d3D12ResourceStates, + clearAllocation: allocationMode == AllocationMode.Clear, + allocation: allocation.GetAddressOf()).Assert(); + + return allocation.Move(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs index 2b1e54f9..0582b127 100644 --- a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs @@ -77,7 +77,7 @@ internal unsafe struct ID3D12MemoryAllocator : IUnknown.Interface /// /// The description of the resource to allocate. /// The type of heap to use for the allocation. - /// The resource state to use initially for the resource. + /// The resource states to use initially for the resource. /// Whether to clear the allocation after creating it. /// The resulting allocation. /// An for the operation. @@ -85,7 +85,7 @@ internal unsafe struct ID3D12MemoryAllocator : IUnknown.Interface public HRESULT AllocateResource( D3D12_RESOURCE_DESC* resourceDescription, D3D12_HEAP_TYPE heapType, - D3D12_RESOURCE_STATES resourceState, + D3D12_RESOURCE_STATES resourceStates, BOOL clearAllocation, ID3D12Allocation** allocation) { @@ -93,7 +93,7 @@ internal unsafe struct ID3D12MemoryAllocator : IUnknown.Interface (ID3D12MemoryAllocator*)Unsafe.AsPointer(ref this), resourceDescription, heapType, - resourceState, + resourceStates, clearAllocation, allocation); }