From 16ccc67bd957e85177e7102455d508f55c304537 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 17 Jun 2022 13:46:46 +0200 Subject: [PATCH] Add configuration switch to enable DRED Using "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA" --- .../Helpers/DeviceHelper.Enumerator.cs | 2 + .../Graphics/Helpers/DeviceHelper.Factory.cs | 52 ++++++++++++++++ .../Graphics/Helpers/DeviceHelper.cs | 4 ++ src/ComputeSharp/Properties/Configuration.cs | 10 +++ .../DirectX/um/d3d12/D3D12_DRED_ENABLEMENT.cs | 14 +++++ ...ID3D12DeviceRemovedExtendedDataSettings.cs | 62 +++++++++++++++++++ .../TerraFX.Interop.Windows.projitems | 2 + 7 files changed, 146 insertions(+) create mode 100644 src/TerraFX.Interop.Windows/DirectX/um/d3d12/D3D12_DRED_ENABLEMENT.cs create mode 100644 src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12DeviceRemovedExtendedDataSettings.cs diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index 795cc5d2..d28e9b75 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -105,6 +105,8 @@ internal static partial class DeviceHelper { this.isInitialized = true; + EnsureDeviceRemovedExtendedDataConfiguration(); + fixed (IDXGIFactory6** dxgiFactory6 = this.dxgiFactory6) { CreateDXGIFactory6(dxgiFactory6); diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index 95b02175..dacfc5ff 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -1,8 +1,12 @@ using System.Collections.Generic; +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +#pragma warning disable CA1416 + namespace ComputeSharp.Graphics.Helpers; /// @@ -18,6 +22,54 @@ internal static partial class DeviceHelper /// private static readonly Dictionary> D3D12InfoQueueMap = new(); + /// + /// Indicates whether or not DRED is enabled (see ). + /// + private static volatile bool IsDeviceRemovedExtendedDataConfigurationEnabled; + + /// + /// Ensures the DRED settings are enabled, if not already. If DRED is not configured, this method does nothing. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe void EnsureDeviceRemovedExtendedDataConfiguration() + { + // Enables DRED, if not enabled already + [MethodImpl(MethodImplOptions.NoInlining)] + static void EnableDeviceRemovedExtendedDataConfigurationIfNeeded() + { + if (IsDeviceRemovedExtendedDataConfigurationEnabled) + { + return; + } + + lock (DevicesCache) + { + if (IsDeviceRemovedExtendedDataConfigurationEnabled) + { + return; + } + + using ComPtr d3D12DeviceRemovedExtendedDataSettings = default; + + DirectX.D3D12GetDebugInterface( + riid: Windows.__uuidof(), + ppvDebug: d3D12DeviceRemovedExtendedDataSettings.GetVoidAddressOf()).Assert(); + + // Enable the auto-breadcrumbs and page faults reporting + d3D12DeviceRemovedExtendedDataSettings.Get()->SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT.D3D12_DRED_ENABLEMENT_FORCED_ON); + d3D12DeviceRemovedExtendedDataSettings.Get()->SetPageFaultEnablement(D3D12_DRED_ENABLEMENT.D3D12_DRED_ENABLEMENT_FORCED_ON); + + IsDeviceRemovedExtendedDataConfigurationEnabled = true; + } + } + + // If the app context switch is set, ensure the DRED setting is enabled + if (Configuration.IsDeviceRemovedExtendedDataEnabled) + { + EnableDeviceRemovedExtendedDataConfigurationIfNeeded(); + } + } + /// /// Retrieves a instance for an object. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index d31e3065..bc5d79b3 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -81,6 +81,8 @@ internal static partial class DeviceHelper /// Whether a default device was found with the requested feature level. private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { + EnsureDeviceRemovedExtendedDataConfiguration(); + using ComPtr dxgiFactory6 = default; CreateDXGIFactory6(dxgiFactory6.GetAddressOf()); @@ -173,6 +175,8 @@ internal static partial class DeviceHelper /// Whether a warp device was created successfully. private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { + EnsureDeviceRemovedExtendedDataConfiguration(); + using ComPtr dxgiFactory6 = default; CreateDXGIFactory6(dxgiFactory6.GetAddressOf()); diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs index ab805bcb..5907cfff 100644 --- a/src/ComputeSharp/Properties/Configuration.cs +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -13,11 +13,21 @@ internal static class Configuration /// private const string EnableDebugOutput = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; + /// + /// The configuration property name for . + /// + private const string EnableDeviceRemovedExtendedDataInfo = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"; + /// /// Indicates whether or not the debug output is enabled. /// public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput); + /// + /// Indicates whether or not the debug output is enabled. + /// + public static readonly bool IsDeviceRemovedExtendedDataEnabled = GetConfigurationValue(EnableDeviceRemovedExtendedDataInfo); + /// /// Gets a configuration value for a specified property. /// diff --git a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/D3D12_DRED_ENABLEMENT.cs b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/D3D12_DRED_ENABLEMENT.cs new file mode 100644 index 00000000..d3fb1b22 --- /dev/null +++ b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/D3D12_DRED_ENABLEMENT.cs @@ -0,0 +1,14 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from d3d12.h in Microsoft.Direct3D.D3D12 v1.600.10 +// Original source is Copyright © Microsoft. Licensed under the MIT license + +namespace TerraFX.Interop.DirectX +{ + internal enum D3D12_DRED_ENABLEMENT + { + D3D12_DRED_ENABLEMENT_SYSTEM_CONTROLLED = 0, + D3D12_DRED_ENABLEMENT_FORCED_OFF = 1, + D3D12_DRED_ENABLEMENT_FORCED_ON = 2, + } +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12DeviceRemovedExtendedDataSettings.cs b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12DeviceRemovedExtendedDataSettings.cs new file mode 100644 index 00000000..f8378e6e --- /dev/null +++ b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12DeviceRemovedExtendedDataSettings.cs @@ -0,0 +1,62 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from d3d12.h in Microsoft.Direct3D.D3D12 v1.600.10 +// Original source is Copyright © Microsoft. Licensed under the MIT license + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop.Windows; + +namespace TerraFX.Interop.DirectX +{ + [Guid("82BC481C-6B9B-4030-AEDB-7EE3D1DF1E63")] + [NativeTypeName("struct ID3D12DeviceRemovedExtendedDataSettings : IUnknown")] + [NativeInheritance("IUnknown")] + internal unsafe partial struct ID3D12DeviceRemovedExtendedDataSettings + { + public void** lpVtbl; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(0)] + public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject) + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[0]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this), riid, ppvObject); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(1)] + public uint AddRef() + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[1]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(2)] + public uint Release() + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[2]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public void SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT Enablement) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[3]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this), Enablement); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(4)] + public void SetPageFaultEnablement(D3D12_DRED_ENABLEMENT Enablement) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[4]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this), Enablement); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(5)] + public void SetWatsonDumpEnablement(D3D12_DRED_ENABLEMENT Enablement) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[5]))((ID3D12DeviceRemovedExtendedDataSettings*)Unsafe.AsPointer(ref this), Enablement); + } + } +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems index 72e40d22..47368458 100644 --- a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems +++ b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems @@ -98,6 +98,7 @@ + @@ -188,6 +189,7 @@ +