From 7ae74ea94d4d9d5dd19b6a2cc809cb14edc12efb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Feb 2021 23:39:52 +0100 Subject: [PATCH] Added runtime checks for D3D12_HEAP_FLAGS support --- .../Extensions/ID3D12DeviceExtensions.cs | 11 ++-- .../Helpers/D3D12FeatureHelper.cs | 66 +++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index b101e51a..a29a900b 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; @@ -11,7 +12,6 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.D3D12_FEATURE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; -using static TerraFX.Interop.D3D12_HEAP_FLAGS; using static TerraFX.Interop.D3D12_HEAP_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; using static TerraFX.Interop.D3D12_RESOURCE_STATES; @@ -112,7 +112,6 @@ namespace ComputeSharp.Graphics.Extensions ulong sizeInBytes, bool isCacheCoherentUMA) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; (D3D12_HEAP_TYPE d3D12HeapType, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch @@ -148,7 +147,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -180,7 +179,6 @@ namespace ComputeSharp.Graphics.Extensions bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -213,7 +211,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -247,7 +245,6 @@ namespace ComputeSharp.Graphics.Extensions bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -280,7 +277,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, diff --git a/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs new file mode 100644 index 00000000..8c3e003b --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_FEATURE; +using FX = TerraFX.Interop.Windows; + +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 == FX.S_OK) + { + return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; + } + + break; + } + + return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE; + } + } + } +}