From 5c60e773b0d29283e2df1f4756e809a80a551ee2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Jun 2023 21:52:42 +0200 Subject: [PATCH] Add allocation COM API definitions --- .../Interop/Allocation/ID3D12Allocation.cs | 61 ++++++++++++++++ .../Allocation/ID3D12MemoryAllocator.cs | 71 +++++++++++++++++++ .../ID3D12MemoryAllocatorFactory.cs | 65 +++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs create mode 100644 src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs create mode 100644 src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs new file mode 100644 index 00000000..20081667 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs @@ -0,0 +1,61 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0649 + +namespace ComputeSharp.Interop.Allocation; + +/// +/// An allocation wrapper for a native object. +/// +[Guid("D42D5782-2DE7-4539-A817-482E3AA01E2E")] +internal unsafe struct ID3D12Allocation +{ + /// + /// Gets the for (D42D5782-2DE7-4539-A817-482E3AA01E2E). + /// + public static ref readonly Guid Guid + { + get + { + ReadOnlySpan data = new byte[] { + 0x82, 0x57, 0x2D, 0xD4, + 0xE7, 0x2D, + 0x39, 0x45, + 0xA8, 0x17, + 0x48, + 0x2E, + 0x3A, + 0xA0, + 0x1E, + 0x2E + }; + + return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); + } + } + + /// + /// The vtable for the current instance. + /// + private readonly void** lpVtbl; + + /// + /// Gets the underlying for the current instance. + /// + /// The resulting object. + /// An for the operation. + /// + /// The returned resource should not be used after the current allocation instance has been released. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public HRESULT GetD3D12Resource(ID3D12Resource** resource) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( + (ID3D12Allocation*)Unsafe.AsPointer(ref this), + resource); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs new file mode 100644 index 00000000..3841fbaa --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs @@ -0,0 +1,71 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0649 + +namespace ComputeSharp.Interop.Allocation; + +/// +/// An object that can allocate resources for a given instance. +/// +[Guid("2D5E55D2-9244-431F-868E-0D90AAB6E575")] +internal unsafe struct ID3D12MemoryAllocator +{ + /// + /// Gets the for (2D5E55D2-9244-431F-868E-0D90AAB6E575). + /// + public static ref readonly Guid Guid + { + get + { + ReadOnlySpan data = new byte[] { + 0xD2, 0x55, 0x5E, 0x2D, + 0x44, 0x92, + 0x1F, 0x43, + 0x86, 0x8E, + 0x0D, + 0x90, + 0xAA, + 0xB6, + 0xE5, + 0x75 + }; + + return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); + } + } + + /// + /// The vtable for the current instance. + /// + private readonly void** lpVtbl; + + /// + /// Creates a new resource allocation from the current allocator. + /// + /// 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. + /// Whether to clear the allocation after creating it. + /// The resulting allocation. + /// An for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public HRESULT AllocateResource( + D3D12_RESOURCE_DESC* resourceDescription, + D3D12_HEAP_TYPE heapType, + D3D12_RESOURCE_STATES resourceState, + BOOL clearAllocation, + ID3D12Allocation** allocation) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( + (ID3D12MemoryAllocator*)Unsafe.AsPointer(ref this), + resourceDescription, + heapType, + resourceState, + clearAllocation, + allocation); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs new file mode 100644 index 00000000..bc06b6b2 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs @@ -0,0 +1,65 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0649 + +namespace ComputeSharp.Interop.Allocation; + +/// +/// A factory type for objects. +/// +[Guid("CC1E74A7-786D-40F4-8AE2-F8B7A255587E")] +internal unsafe struct ID3D12MemoryAllocatorFactory +{ + /// + /// Gets the for (CC1E74A7-786D-40F4-8AE2-F8B7A255587E). + /// + public static ref readonly Guid Guid + { + get + { + ReadOnlySpan data = new byte[] { + 0xA7, 0x74, 0x1E, 0xCC, + 0x6D, 0x78, + 0xF4, 0x40, + 0x8A, 0xE2, + 0xF8, + 0xB7, + 0xA2, + 0x55, + 0x58, + 0x7E + }; + + return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); + } + } + + /// + /// The vtable for the current instance. + /// + private readonly void** lpVtbl; + + /// + /// Creates a new for a given device. + /// + /// The device to create the allocator for. + /// The for . + /// The resulting instance. + /// An for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public HRESULT CreateAllocator( + ID3D12Device* device, + IDXGIAdapter* adapter, + ID3D12MemoryAllocator** allocator) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( + (ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this), + device, + adapter, + allocator); + } +} \ No newline at end of file