From 9eac016ae9f2f7d13734856a703682b9e1347656 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 30 Jun 2023 01:06:52 +0200 Subject: [PATCH] Switch GraphicsDevice to use new allocator APIs --- .../Interop/ID3D12DeviceExtensions.cs | 32 ---- .../Graphics/GraphicsDevice.Resources.cs | 179 ++++++++++++++++++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 78 +------- .../Graphics/Helpers/DeviceHelper.Factory.cs | 2 +- .../Graphics/Resources/Abstract/Buffer{T}.cs | 23 +-- .../Resources/Abstract/StructuredBuffer{T}.cs | 45 ++--- .../Resources/Abstract/Texture1D{T}.cs | 61 ++---- .../Resources/Abstract/Texture2D{T}.cs | 62 ++---- .../Resources/Abstract/Texture3D{T}.cs | 63 ++---- .../Resources/Abstract/TransferBuffer{T}.cs | 28 +-- .../Abstract/TransferTexture1D{T}.cs | 28 +-- .../Abstract/TransferTexture2D{T}.cs | 28 +-- .../Abstract/TransferTexture3D{T}.cs | 28 +-- 13 files changed, 304 insertions(+), 353 deletions(-) create mode 100644 src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index a6707113..c77fd2a2 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,32 +1,21 @@ using System; -#if NET6_0_OR_GREATER -using System.Runtime.CompilerServices; -#endif using ComputeSharp.Core.Extensions; -#if !NET6_0_OR_GREATER using ComputeSharp.Graphics.Resources.Enums; -#endif using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_QUEUE_FLAGS; using static TerraFX.Interop.DirectX.D3D12_COMMAND_QUEUE_PRIORITY; -#if !NET6_0_OR_GREATER using static TerraFX.Interop.DirectX.D3D12_CPU_PAGE_PROPERTY; -#endif using static TerraFX.Interop.DirectX.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.DirectX.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.DirectX.D3D12_FEATURE; using static TerraFX.Interop.DirectX.D3D12_FENCE_FLAGS; -#if !NET6_0_OR_GREATER using static TerraFX.Interop.DirectX.D3D12_HEAP_FLAGS; using static TerraFX.Interop.DirectX.D3D12_MEMORY_POOL; -#endif using static TerraFX.Interop.DirectX.D3D12_MESSAGE_ID; -#if !NET6_0_OR_GREATER using static TerraFX.Interop.DirectX.D3D12_HEAP_TYPE; using static TerraFX.Interop.DirectX.D3D12_RESOURCE_FLAGS; using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES; -#endif using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION; @@ -37,26 +26,6 @@ namespace ComputeSharp.Graphics.Extensions; /// internal static unsafe class ID3D12DeviceExtensions { -#if NET6_0_OR_GREATER - /// - /// Creates a instance for a given device. - /// - /// The target to use to create the allocator. - /// The that was created from. - /// A instance to create resources on the device in use. - public static ComPtr CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter) - { - using ComPtr allocator = default; - - D3D12MA_ALLOCATOR_DESC allocatorDesc = default; - allocatorDesc.pDevice = (ID3D12Device*)Unsafe.AsPointer(ref d3D12Device); - allocatorDesc.pAdapter = dxgiAdapter; - - D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator.GetAddressOf()).Assert(); - - return allocator.Move(); - } -#else /// /// Creates a committed resource for a given buffer type. /// @@ -301,7 +270,6 @@ internal static unsafe class ID3D12DeviceExtensions return d3D12Resource.Move(); } -#endif /// /// Creates a new for a given device. diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs new file mode 100644 index 00000000..24cfa1a5 --- /dev/null +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs @@ -0,0 +1,179 @@ +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Interop.Allocation; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; + +namespace ComputeSharp; + +/// +unsafe partial class GraphicsDevice +{ + /// + /// Creates or allocates a resource for a given buffer type. + /// + /// The resource type currently in use. + /// The allocation mode to use for the new resource. + /// The size in bytes of the current buffer. + /// The resulting object, if one is used. + /// The resulting object for the buffer. + internal void CreateOrAllocateResource( + ResourceType resourceType, + AllocationMode allocationMode, + ulong sizeInBytes, + out ComPtr allocation, + out ComPtr d3D12Resource) + { + if (this.allocator.Get() is not null) + { + allocation = this.allocator.Get()->AllocateResource(resourceType, allocationMode, sizeInBytes); + d3D12Resource = allocation.Get()->GetD3D12Resource(); + } + else + { + allocation = null; + d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource(resourceType, sizeInBytes, IsCacheCoherentUMA); + } + } + + /// + /// Creates or allocates a resource for a given 1D texture type. + /// + /// 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. + /// The resulting object, if one is used. + /// The resulting object for the 1D texture. + internal void CreateOrAllocateResource( + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + out ComPtr allocation, + out ComPtr d3D12Resource, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + if (this.allocator.Get() is not null) + { + allocation = this.allocator.Get()->AllocateResource( + resourceType, + allocationMode, + dxgiFormat, + width, + out d3D12ResourceStates); + + d3D12Resource = allocation.Get()->GetD3D12Resource(); + } + else + { + allocation = null; + + d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( + resourceType, + dxgiFormat, + width, + IsCacheCoherentUMA, + out d3D12ResourceStates); + } + } + + /// + /// Creates or allocates a resource for a given 2D texture type. + /// + /// 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. + /// The resulting object, if one is used. + /// The resulting object for the 2D texture. + internal void CreateOrAllocateResource( + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + out ComPtr allocation, + out ComPtr d3D12Resource, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + if (this.allocator.Get() is not null) + { + allocation = this.allocator.Get()->AllocateResource( + resourceType, + allocationMode, + dxgiFormat, + width, + height, + out d3D12ResourceStates); + + d3D12Resource = allocation.Get()->GetD3D12Resource(); + } + else + { + allocation = null; + + d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( + resourceType, + dxgiFormat, + width, + height, + IsCacheCoherentUMA, + out d3D12ResourceStates); + } + } + + /// + /// Creates or allocates a resource for a given 3D texture type. + /// + /// 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. + /// The resulting object, if one is used. + /// The resulting object for the 3D texture. + internal void CreateOrAllocateResource( + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + ushort depth, + out ComPtr allocation, + out ComPtr d3D12Resource, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + if (this.allocator.Get() is not null) + { + allocation = this.allocator.Get()->AllocateResource( + resourceType, + allocationMode, + dxgiFormat, + width, + height, + depth, + out d3D12ResourceStates); + + d3D12Resource = allocation.Get()->GetD3D12Resource(); + } + else + { + allocation = null; + + d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( + resourceType, + dxgiFormat, + width, + height, + depth, + IsCacheCoherentUMA, + out d3D12ResourceStates); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 16b1e412..0470f306 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -4,12 +4,10 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Graphics.Commands.Interop; -#if NET6_0_OR_GREATER -using ComputeSharp.Core.Extensions; -#endif using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using ComputeSharp.Shaders.Models; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -81,17 +79,10 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject /// private ulong nextD3D12CopyFenceValue; -#if NET6_0_OR_GREATER /// - /// The in use associated to the current device. + /// The in use associated to the current device, if available. /// - private ComPtr allocator; - - /// - /// The instance in use, if is . - /// - private ComPtr pool; -#endif + private ComPtr allocator; /// /// A weak to the current instance (used to support the device lost callback). @@ -149,20 +140,20 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject /// The to use for the new instance. /// The that was created from. /// The available info for the new instance. - internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) + /// The optional instance to use. + internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1, ID3D12MemoryAllocator* allocator) { using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker); this.d3D12Device = new ComPtr(d3D12Device); - this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE); this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY); this.d3D12ComputeFence = d3D12Device->CreateFence(); this.d3D12CopyFence = d3D12Device->CreateFence(); - this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3D12Device); this.computeCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COMPUTE); this.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY); + this.allocator = new ComPtr(allocator); Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid); Name = new string((char*)dxgiDescription1->Description); @@ -179,15 +170,6 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; -#if NET6_0_OR_GREATER - this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); - - if (IsCacheCoherentUMA) - { - this.pool = this.allocator.Get()->CreatePoolForCacheCoherentUMA(); - } -#endif - this.deviceRemovedReason = S.S_OK; this.cachedPipelineData = new List(); @@ -233,19 +215,7 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject /// /// Gets the underlying wrapped by the current instance. /// - internal ID3D12Device* D3D12Device => this.d3D12Device; - -#if NET6_0_OR_GREATER - /// - /// Gets the underlying wrapped by the current instance. - /// - internal D3D12MA_Allocator* Allocator => this.allocator; - - /// - /// Gets the underlying wrapped by the current instance, if any. - /// - internal D3D12MA_Pool* Pool => this.pool; -#endif + internal ID3D12Device* D3D12Device => this.d3D12Device.Get(); /// /// Gets whether or not the current device has a cache coherent UMA architecture. @@ -369,28 +339,6 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); } - /// - /// Registers that a new resource has been allocated on the current device. - /// - internal void RegisterAllocatedResource() - { -#if NET6_0_OR_GREATER - this.pool.AddRef(); - this.allocator.AddRef(); -#endif - } - - /// - /// Unregisters a generic resource that was allocated on the current device. - /// - internal void UnregisterAllocatedResource() - { -#if NET6_0_OR_GREATER - this.pool.Release(); - this.allocator.Release(); -#endif - } - /// /// Registers a object for the current device to enable early disposal. /// @@ -484,17 +432,7 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject this.computeCommandListPool.Dispose(); this.copyCommandListPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); - - // On .NET 6, D3D12MA is used. In this case, the pool and allocator must be kept alive - // until all associated resources are returned and destroyed. Because of this, when the - // device is disposed (since there might be outstanding resources that are still alive), - // the pool and allocator are only released, but not disposed. This allows reosurces to - // also release them when disposed (since each resource keeps a reference back to the - // parent device). When the last one is disposed, the pool and allocator will be deleted. -#if NET6_0_OR_GREATER - this.pool.Release(); - this.allocator.Release(); -#endif + this.allocator.Dispose(); UnregisterDeviceLostCallback(this); diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index 2c985a08..4cef9487 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -128,7 +128,7 @@ unsafe partial class DeviceHelper _ = TryGetMemoryAllocator(d3D12Device, dxgiAdapter, memoryAllocator.GetAddressOf()); // Create the device passing the underlying device, adapter, and optional allocator - device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1); + device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1, memoryAllocator.Get()); DevicesCache.Add(luid, device); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 34e8bfa8..481e58a5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -13,6 +13,7 @@ using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FEATURE; using static TerraFX.Interop.DirectX.DXGI_FORMAT; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; +using ComputeSharp.Interop.Allocation; #if NET6_0_OR_GREATER using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal; #else @@ -33,12 +34,10 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -100,14 +99,13 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph GraphicsDevice = device; Length = length; -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA); -#endif + device.CreateOrAllocateResource( + resourceType, + allocationMode, + (ulong)effectiveSizeInBytes, + out this.allocation, + out this.d3D12Resource); - device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); @@ -214,13 +212,10 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph void IReferenceTrackedObject.DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif if (GraphicsDevice is GraphicsDevice device) { - device.UnregisterAllocatedResource(); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 9416d22a..a76d6154 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -65,30 +66,23 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)sourceOffset * sizeof(T); nint byteLength = count * sizeof(T); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(GraphicsDevice.Pool, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.ReadBack, + AllocationMode.Default, (ulong)byteLength, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { -#if NET6_0_OR_GREATER - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength); -#else copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength); -#endif copyCommandList.ExecuteAndWaitForCompletion(); } -#if NET6_0_OR_GREATER - using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map(); -#else using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); -#endif fixed (void* destinationPointer = &destination) { @@ -229,20 +223,18 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)offset * sizeof(T); nint byteLength = length * sizeof(T); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(GraphicsDevice.Pool, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.Upload, + AllocationMode.Default, (ulong)byteLength, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); -#if NET6_0_OR_GREATER - using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) -#else using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) -#endif + { fixed (void* sourcePointer = &source) { MemoryHelper.Copy( @@ -254,14 +246,11 @@ public abstract class StructuredBuffer : Buffer destinationElementPitchInBytes: (uint)sizeof(T), count: (uint)length); } + } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); -#if NET6_0_OR_GREATER - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength); -#else copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength); -#endif copyCommandList.ExecuteAndWaitForCompletion(); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index d04db4c8..a527c406 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -33,12 +34,10 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -90,25 +89,15 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr GraphicsDevice = device; -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource( - device.Pool, + device.CreateOrAllocateResource( resourceType, allocationMode, DXGIFormatHelper.GetForType(), (uint)width, + out this.allocation, + out this.d3D12Resource, out this.d3D12ResourceState); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - DXGIFormatHelper.GetForType(), - (uint)width, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); -#endif - this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; @@ -120,7 +109,6 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr out _, out _); - device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) @@ -181,20 +169,15 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.ReadBack, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -379,20 +362,15 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.Upload, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { @@ -495,13 +473,10 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr protected virtual partial void DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif if (GraphicsDevice is GraphicsDevice device) { - device.UnregisterAllocatedResource(); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 5759bcf9..6d27d4c3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -33,12 +34,10 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -92,27 +91,16 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr GraphicsDevice = device; -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource( - device.Pool, + device.CreateOrAllocateResource( resourceType, allocationMode, DXGIFormatHelper.GetForType(), (uint)width, (uint)height, + out this.allocation, + out this.d3D12Resource, out this.d3D12ResourceState); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); -#endif - this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; @@ -125,7 +113,6 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr out _, out _); - device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) @@ -197,20 +184,15 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.ReadBack, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -420,20 +402,15 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.Upload, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { @@ -546,13 +523,10 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr protected virtual partial void DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif if (GraphicsDevice is GraphicsDevice device) { - device.UnregisterAllocatedResource(); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index b3acb736..62e819ca 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; @@ -33,12 +34,10 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -94,29 +93,17 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr GraphicsDevice = device; -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource( - device.Pool, + device.CreateOrAllocateResource( resourceType, allocationMode, DXGIFormatHelper.GetForType(), (uint)width, (uint)height, (ushort)depth, + out this.allocation, + out this.d3D12Resource, out this.d3D12ResourceState); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); -#endif - this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; @@ -130,7 +117,6 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr out _, out _); - device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) @@ -213,20 +199,15 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.ReadBack, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -462,20 +443,15 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, + using ComPtr allocation = default; + using ComPtr d3D12Resource = default; + + GraphicsDevice.CreateOrAllocateResource( ResourceType.Upload, AllocationMode.Default, - totalSizeInBytes); - - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); -#else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); -#endif + out *&allocation, + out *&d3D12Resource); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { @@ -599,13 +575,10 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr protected virtual partial void DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif if (GraphicsDevice is GraphicsDevice device) { - device.UnregisterAllocatedResource(); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index e72a5462..8fcbe06f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -6,6 +6,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FEATURE; @@ -27,12 +28,10 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -74,14 +73,12 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject ulong sizeInBytes = (uint)length * (uint)sizeof(T); -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, sizeInBytes, device.IsCacheCoherentUMA); -#endif - - device.RegisterAllocatedResource(); + device.CreateOrAllocateResource( + resourceType, + allocationMode, + sizeInBytes, + out this.allocation, + out this.d3D12Resource); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; @@ -134,14 +131,7 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject void IReferenceTrackedObject.DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif - - if (GraphicsDevice is GraphicsDevice device) - { - device.UnregisterAllocatedResource(); - } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs index f33dc3e4..b1ba8b0b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; @@ -26,12 +27,10 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -79,14 +78,12 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj out _, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA); -#endif - - device.RegisterAllocatedResource(); + device.CreateOrAllocateResource( + resourceType, + allocationMode, + totalSizeInBytes, + out this.allocation, + out this.d3D12Resource); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; @@ -139,14 +136,7 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj void IReferenceTrackedObject.DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif - - if (GraphicsDevice is GraphicsDevice device) - { - device.UnregisterAllocatedResource(); - } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 61da3915..164ac349 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; @@ -25,12 +26,10 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -81,14 +80,12 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj out _, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA); -#endif - - device.RegisterAllocatedResource(); + device.CreateOrAllocateResource( + resourceType, + allocationMode, + totalSizeInBytes, + out this.allocation, + out this.d3D12Resource); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; @@ -134,14 +131,7 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj void IReferenceTrackedObject.DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif - - if (GraphicsDevice is GraphicsDevice device) - { - device.UnregisterAllocatedResource(); - } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index a2d79929..62db3795 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; +using ComputeSharp.Interop.Allocation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; @@ -25,12 +26,10 @@ public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObj /// private ReferenceTracker referenceTracker; -#if NET6_0_OR_GREATER /// - /// The instance used to retrieve . + /// The instance used to retrieve , if available. /// - private ComPtr allocation; -#endif + private ComPtr allocation; /// /// The instance currently mapped. @@ -84,14 +83,12 @@ public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObj out _, out ulong totalSizeInBytes); -#if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); -#else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA); -#endif - - device.RegisterAllocatedResource(); + device.CreateOrAllocateResource( + resourceType, + allocationMode, + totalSizeInBytes, + out this.allocation, + out this.d3D12Resource); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; @@ -142,14 +139,7 @@ public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObj void IReferenceTrackedObject.DangerousOnDispose() { this.d3D12Resource.Dispose(); -#if NET6_0_OR_GREATER this.allocation.Dispose(); -#endif - - if (GraphicsDevice is GraphicsDevice device) - { - device.UnregisterAllocatedResource(); - } } ///