From 9f07d7bb0c5729060c926dad7b9a6c710e54fe5f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Nov 2021 10:57:03 +0100 Subject: [PATCH] Restore D3D12MA support on .NET 6 --- .../Extensions/Interop/AllocatorExtensions.cs | 2 +- .../Extensions/Interop/ID3D12DeviceExtensions.cs | 12 ++++++------ src/ComputeSharp/Graphics/GraphicsDevice.cs | 12 ++++++------ .../Graphics/Resources/Abstract/Buffer{T}.cs | 6 +++--- .../Resources/Abstract/StructuredBuffer{T}.cs | 12 ++++++------ .../Graphics/Resources/Abstract/Texture2D{T}.cs | 10 +++++----- .../Graphics/Resources/Abstract/Texture3D{T}.cs | 10 +++++----- .../Graphics/Resources/Abstract/TransferBuffer{T}.cs | 6 +++--- .../Resources/Abstract/TransferTexture2D{T}.cs | 6 +++--- .../Resources/Abstract/TransferTexture3D{T}.cs | 6 +++--- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 21a03af6..ff57809c 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -1,4 +1,4 @@ -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComputeSharp.Core.Extensions; using Microsoft.Toolkit.Diagnostics; diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 033e4f98..79bca8ea 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,10 +1,10 @@ using System; using System.Diagnostics.Contracts; -#if USE_D3D12MA +#if NET6_0_OR_GREATER using System.Runtime.CompilerServices; #endif using ComputeSharp.Core.Extensions; -#if !USE_D3D12MA +#if !NET6_0_OR_GREATER using ComputeSharp.Graphics.Resources.Enums; using Microsoft.Toolkit.Diagnostics; #endif @@ -12,19 +12,19 @@ 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 !USE_D3D12MA +#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 !USE_D3D12MA +#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 !USE_D3D12MA +#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; @@ -39,7 +39,7 @@ namespace ComputeSharp.Graphics.Extensions; /// internal static unsafe class ID3D12DeviceExtensions { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// Creates a instance for a given device. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 483f7370..fb303d28 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -74,7 +74,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// private ulong nextD3D12CopyFenceValue; -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The in use associated to the current device. /// @@ -120,7 +120,7 @@ public sealed unsafe class GraphicsDevice : NativeObject IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); if (IsCacheCoherentUMA) @@ -171,7 +171,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal ID3D12Device* D3D12Device => this.d3D12Device; -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// Gets the underlying wrapped by the current instance. /// @@ -257,7 +257,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal void RegisterAllocatedResource() { -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.pool.AddRef(); this.allocator.AddRef(); #endif @@ -268,7 +268,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal void UnregisterAllocatedResource() { -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.pool.Release(); this.allocator.Release(); #endif @@ -408,7 +408,7 @@ public sealed unsafe class GraphicsDevice : NativeObject this.computeCommandListPool.Dispose(); this.copyCommandListPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.pool.Dispose(); this.allocator.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 37096b8f..b61cf390 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -25,7 +25,7 @@ namespace ComputeSharp.Resources; public unsafe abstract class Buffer : NativeObject where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -87,7 +87,7 @@ public unsafe abstract class Buffer : NativeObject GraphicsDevice = device; Length = length; -#if USE_D3D12MA +#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 @@ -191,7 +191,7 @@ public unsafe abstract class Buffer : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 9b9041f2..f4ebdea0 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -61,7 +61,7 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)sourceOffset * sizeof(T); nint byteLength = count * sizeof(T); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); #else using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( @@ -73,7 +73,7 @@ public abstract class StructuredBuffer : Buffer using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { -#if USE_D3D12MA +#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); @@ -81,7 +81,7 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map(); #else using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); @@ -220,7 +220,7 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)offset * sizeof(T); nint byteLength = length * sizeof(T); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); #else using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( @@ -230,7 +230,7 @@ public abstract class StructuredBuffer : Buffer GraphicsDevice.IsCacheCoherentUMA); #endif -#if USE_D3D12MA +#if NET6_0_OR_GREATER using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) #else using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) @@ -249,7 +249,7 @@ public abstract class StructuredBuffer : Buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); -#if USE_D3D12MA +#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); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 74a0ad37..2619cc2b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -26,7 +26,7 @@ namespace ComputeSharp.Resources; public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -86,7 +86,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper GraphicsDevice = device; -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation = device.Allocator->CreateResource( device.Pool, resourceType, @@ -187,7 +187,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, @@ -408,7 +408,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, @@ -532,7 +532,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 221711da..d49a14b3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -26,7 +26,7 @@ namespace ComputeSharp.Resources; public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -88,7 +88,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper GraphicsDevice = device; -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation = device.Allocator->CreateResource( device.Pool, resourceType, @@ -203,7 +203,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, @@ -450,7 +450,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, @@ -585,7 +585,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 95b2eb29..771a7a66 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -18,7 +18,7 @@ namespace ComputeSharp.Resources; public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -54,7 +54,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner ulong sizeInBytes = (uint)length * (uint)sizeof(T); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); #else @@ -116,7 +116,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 9b046430..ab815e0f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -18,7 +18,7 @@ namespace ComputeSharp.Resources; public unsafe abstract class TransferTexture2D : NativeObject where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -70,7 +70,7 @@ public unsafe abstract class TransferTexture2D : NativeObject out _, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); #else @@ -125,7 +125,7 @@ public unsafe abstract class TransferTexture2D : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index c7b42f14..e9bd0498 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -18,7 +18,7 @@ namespace ComputeSharp.Resources; public unsafe abstract class TransferTexture3D : NativeObject where T : unmanaged { -#if USE_D3D12MA +#if NET6_0_OR_GREATER /// /// The instance used to retrieve . /// @@ -73,7 +73,7 @@ public unsafe abstract class TransferTexture3D : NativeObject out _, out ulong totalSizeInBytes); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); #else @@ -133,7 +133,7 @@ public unsafe abstract class TransferTexture3D : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); -#if USE_D3D12MA +#if NET6_0_OR_GREATER this.allocation.Dispose(); #endif