From f57f44985ca4f91347c4fc1bdebcf08c8a9a02f6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Nov 2021 01:07:14 +0100 Subject: [PATCH] Move D3D12MA usage behind USE_D3D12MA flag --- src/ComputeSharp/ComputeSharp.csproj | 2 +- .../Extensions/Interop/AllocatorExtensions.cs | 6 +- .../Interop/ID3D12DeviceExtensions.cs | 211 ++++++++++++++++++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 12 + .../Graphics/Helpers/D3D12FeatureHelper.cs | 65 ------ .../Graphics/Resources/Abstract/Buffer{T}.cs | 8 + .../Resources/Abstract/StructuredBuffer{T}.cs | 32 +++ .../Resources/Abstract/Texture2D{T}.cs | 31 +++ .../Resources/Abstract/Texture3D{T}.cs | 32 +++ .../Resources/Abstract/TransferBuffer{T}.cs | 8 + .../Abstract/TransferTexture2D{T}.cs | 8 + .../Abstract/TransferTexture3D{T}.cs | 8 + 12 files changed, 356 insertions(+), 67 deletions(-) delete mode 100644 src/ComputeSharp/Graphics/Helpers/D3D12FeatureHelper.cs diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index d077610d..9043d479 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 4502b004..21a03af6 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -1,4 +1,6 @@ -using ComputeSharp.Core.Extensions; +#if USE_D3D12MA + +using ComputeSharp.Core.Extensions; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -196,3 +198,5 @@ internal static unsafe class AllocatorExtensions return allocation.Move(); } } + +#endif diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index a5d074d6..033e4f98 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,16 +1,34 @@ using System; using System.Diagnostics.Contracts; +#if USE_D3D12MA using System.Runtime.CompilerServices; +#endif using ComputeSharp.Core.Extensions; +#if !USE_D3D12MA +using ComputeSharp.Graphics.Resources.Enums; +using Microsoft.Toolkit.Diagnostics; +#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 !USE_D3D12MA +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 +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 +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; @@ -21,6 +39,7 @@ namespace ComputeSharp.Graphics.Extensions; /// internal static unsafe class ID3D12DeviceExtensions { +#if USE_D3D12MA /// /// Creates a instance for a given device. /// @@ -39,6 +58,198 @@ internal static unsafe class ID3D12DeviceExtensions return allocator.Move(); } +#else + /// + /// 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_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, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + Windows.__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_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, mipLevels: 1, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + Windows.__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_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, mipLevels: 1, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + Windows.__uuidof(), + d3D12Resource.GetVoidAddressOf()).Assert(); + + return d3D12Resource.Move(); + } +#endif /// /// Creates a new for a given device. diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 9f3947fb..1a554f32 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -74,6 +74,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// private ulong nextD3D12CopyFenceValue; +#if USE_D3D12MA /// /// The in use associated to the current device. /// @@ -83,6 +84,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// The instance in use, if is . /// private ComPtr pool; +#endif /// /// Creates a new instance for the input . @@ -118,12 +120,14 @@ public sealed unsafe class GraphicsDevice : NativeObject IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; +#if USE_D3D12MA this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); if (IsCacheCoherentUMA) { this.pool = this.allocator.Get()->CreatePoolForCacheCoherentUMA(); } +#endif } /// @@ -167,6 +171,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal ID3D12Device* D3D12Device => this.d3D12Device; +#if USE_D3D12MA /// /// Gets the underlying wrapped by the current instance. /// @@ -176,6 +181,7 @@ public sealed unsafe class GraphicsDevice : NativeObject /// Gets the underlying wrapped by the current instance, if any. /// internal D3D12MA_Pool* Pool => this.pool; +#endif /// /// Gets whether or not the current device has a cache coherent UMA architecture. @@ -251,8 +257,10 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal void RegisterAllocatedResource() { +#if USE_D3D12MA this.pool.AddRef(); this.allocator.AddRef(); +#endif } /// @@ -260,8 +268,10 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal void UnregisterAllocatedResource() { +#if USE_D3D12MA this.pool.Release(); this.allocator.Release(); +#endif } /// @@ -394,8 +404,10 @@ public sealed unsafe class GraphicsDevice : NativeObject this.computeCommandListPool.Dispose(); this.copyCommandListPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); +#if USE_D3D12MA this.pool.Dispose(); this.allocator.Dispose(); +#endif } /// diff --git a/src/ComputeSharp/Graphics/Helpers/D3D12FeatureHelper.cs b/src/ComputeSharp/Graphics/Helpers/D3D12FeatureHelper.cs deleted file mode 100644 index 1b9520c8..00000000 --- a/src/ComputeSharp/Graphics/Helpers/D3D12FeatureHelper.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; -using static TerraFX.Interop.DirectX.D3D12_FEATURE; - -namespace ComputeSharp.Graphics.Helpers; - -/// -/// A helper exposing some runtime or OS dependent D3D12 features. -/// -internal static class D3D12FeatureHelper -{ - /// - /// The currently supported flag (or fallback). - /// - private static readonly D3D12_HEAP_FLAGS D3D12_HEAP_FLAG_CREATE_NOT_ZEROED = GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED(); - - /// - /// Gets the value for a given setting. - /// - /// The allocation mode to use for creating new resources. - /// The matching that is currently supported - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static D3D12_HEAP_FLAGS GetD3D12HeapFlags(AllocationMode allocationMode) - { - if (allocationMode == AllocationMode.Default) - { - return D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; - } - - return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE; - } - - /// - /// Initializes the field. - /// - /// The currently supported flag. - private static unsafe D3D12_HEAP_FLAGS GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED() - { - lock (DeviceHelper.DevicesCache) - { - foreach (KeyValuePair pair in DeviceHelper.DevicesCache) - { - D3D12_FEATURE_DATA_D3D12_OPTIONS7 d3D12Options7; - - int result = pair.Value.D3D12Device->CheckFeatureSupport( - D3D12_FEATURE_D3D12_OPTIONS7, - &d3D12Options7, - (uint)sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS7)); - - if (result == S.S_OK) - { - return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; - } - - break; - } - - return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE; - } - } -} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index dca6efdd..0160258a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -21,10 +21,12 @@ namespace ComputeSharp.Resources; public unsafe abstract class Buffer : NativeObject where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -81,8 +83,12 @@ public unsafe abstract class Buffer : NativeObject GraphicsDevice = device; Length = length; +#if USE_D3D12MA 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, allocationMode, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA); +#endif device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -181,7 +187,9 @@ public unsafe abstract class Buffer : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index e510e336..9b9041f2 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -61,15 +61,31 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)sourceOffset * sizeof(T); nint byteLength = count * sizeof(T); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + AllocationMode.Default, + (ulong)byteLength, + GraphicsDevice.IsCacheCoherentUMA); +#endif using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { +#if USE_D3D12MA 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 USE_D3D12MA using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map(); +#else + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); +#endif fixed (void* destinationPointer = &destination) { @@ -204,9 +220,21 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)offset * sizeof(T); nint byteLength = length * sizeof(T); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.Upload, + AllocationMode.Default, + (ulong)byteLength, + GraphicsDevice.IsCacheCoherentUMA); +#endif +#if USE_D3D12MA using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) +#else + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) +#endif fixed (void* sourcePointer = &source) { MemoryHelper.Copy( @@ -221,7 +249,11 @@ public abstract class StructuredBuffer : Buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); +#if USE_D3D12MA 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/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index db46dfae..74a0ad37 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -26,10 +26,12 @@ namespace ComputeSharp.Resources; public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -84,6 +86,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper GraphicsDevice = device; +#if USE_D3D12MA this.allocation = device.Allocator->CreateResource( device.Pool, resourceType, @@ -94,6 +97,16 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out this.d3D12ResourceState); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); +#else + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + allocationMode, + 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 @@ -174,6 +187,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, @@ -181,6 +195,13 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper totalSizeInBytes); using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + AllocationMode.Default, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -387,6 +408,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, @@ -394,6 +416,13 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper totalSizeInBytes); using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.Upload, + AllocationMode.Default, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) @@ -503,7 +532,9 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 492be47d..221711da 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -26,10 +26,12 @@ namespace ComputeSharp.Resources; public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -86,6 +88,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper GraphicsDevice = device; +#if USE_D3D12MA this.allocation = device.Allocator->CreateResource( device.Pool, resourceType, @@ -97,6 +100,17 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out this.d3D12ResourceState); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); +#else + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + allocationMode, + 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 @@ -189,6 +203,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, @@ -196,6 +211,13 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper totalSizeInBytes); using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + AllocationMode.Default, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -428,6 +450,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); +#if USE_D3D12MA using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, @@ -435,6 +458,13 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper totalSizeInBytes); using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.Upload, + AllocationMode.Default, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) @@ -555,7 +585,9 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 00ff3ae7..95b2eb29 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -18,10 +18,12 @@ namespace ComputeSharp.Resources; public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -52,8 +54,12 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner ulong sizeInBytes = (uint)length * (uint)sizeof(T); +#if USE_D3D12MA 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, allocationMode, sizeInBytes, device.IsCacheCoherentUMA); +#endif device.RegisterAllocatedResource(); @@ -110,7 +116,9 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 90ce33fd..9b046430 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -18,10 +18,12 @@ namespace ComputeSharp.Resources; public unsafe abstract class TransferTexture2D : NativeObject where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -68,8 +70,12 @@ public unsafe abstract class TransferTexture2D : NativeObject out _, out ulong totalSizeInBytes); +#if USE_D3D12MA 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, allocationMode, totalSizeInBytes, device.IsCacheCoherentUMA); +#endif device.RegisterAllocatedResource(); @@ -119,7 +125,9 @@ public unsafe abstract class TransferTexture2D : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index f7765687..c7b42f14 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -18,10 +18,12 @@ namespace ComputeSharp.Resources; public unsafe abstract class TransferTexture3D : NativeObject where T : unmanaged { +#if USE_D3D12MA /// /// The instance used to retrieve . /// private ComPtr allocation; +#endif /// /// The instance currently mapped. @@ -71,8 +73,12 @@ public unsafe abstract class TransferTexture3D : NativeObject out _, out ulong totalSizeInBytes); +#if USE_D3D12MA 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, allocationMode, totalSizeInBytes, device.IsCacheCoherentUMA); +#endif device.RegisterAllocatedResource(); @@ -127,7 +133,9 @@ public unsafe abstract class TransferTexture3D : NativeObject protected override void OnDispose() { this.d3D12Resource.Dispose(); +#if USE_D3D12MA this.allocation.Dispose(); +#endif if (GraphicsDevice is GraphicsDevice device) {