From 5c17ea54011cf74dd181916b8a88095d5733003d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 28 Jan 2022 23:50:54 +0100 Subject: [PATCH] Add app config switch to enable the debug mode --- .../Extensions/Interop/HResultExtensions.cs | 45 ++++++++++++------- .../Graphics/Helpers/DeviceHelper.Factory.cs | 20 ++++----- .../Helpers/DeviceHelper.ID3D12InfoQueue.cs | 13 ++---- .../Helpers/DeviceHelper.IDXGIFactory6.cs | 13 +++--- src/ComputeSharp/Properties/Configuration.cs | 32 +++++++++++++ 5 files changed, 79 insertions(+), 44 deletions(-) create mode 100644 src/ComputeSharp/Properties/Configuration.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs index dbf2a6ce..33a84d4b 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs @@ -1,9 +1,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; -#if DEBUG using ComputeSharp.Graphics.Helpers; -#endif using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.Windows; @@ -34,23 +32,36 @@ internal static class HResultExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this int result) { -#if DEBUG - bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); + // This forward branch is predicted taken by the JIT, and when tier-1 JIT kicks in + // it will just be removed if the IsDebugOutputEnabled flag is not set. The resulting + // single branch can then be inlined without bloating the code size in the caller. + if (!Configuration.IsDebugOutputEnabled) + { + if (result < 0) + { + ThrowHelper.ThrowWin32Exception(result); + } + } + else + { + // Move the extended debug logic into a non inlineable method to help the inliner in the standard case + [MethodImpl(MethodImplOptions.NoInlining)] + static void AssertWithDebugInfo(int result) + { + bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } + if (result < 0) + { + ThrowHelper.ThrowWin32Exception(result); + } - if (hasErrorsOrWarnings) - { - ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue."); + if (hasErrorsOrWarnings) + { + ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue."); + } + } + + AssertWithDebugInfo(result); } -#else - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } -#endif } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index eb8d0c48..95b02175 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; -#if DEBUG using ComputeSharp.Graphics.Extensions; -#endif using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -15,12 +13,10 @@ internal static partial class DeviceHelper /// internal static readonly Dictionary DevicesCache = new(); -#if DEBUG /// /// The local map of instances for the existing devices. /// private static readonly Dictionary> D3D12InfoQueueMap = new(); -#endif /// /// Retrieves a instance for an object. @@ -41,9 +37,10 @@ internal static partial class DeviceHelper DevicesCache.Add(luid, device); -#if DEBUG - D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue()); -#endif + if (Configuration.IsDebugOutputEnabled) + { + D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue()); + } } return device; @@ -60,11 +57,12 @@ internal static partial class DeviceHelper { DevicesCache.Remove(device.Luid); -#if DEBUG - D3D12InfoQueueMap.Remove(device.Luid, out ComPtr queue); + if (Configuration.IsDebugOutputEnabled) + { + D3D12InfoQueueMap.Remove(device.Luid, out ComPtr queue); - queue.Dispose(); -#endif + queue.Dispose(); + } } } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs index c75ef8c2..01f906e9 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -1,16 +1,13 @@ -#if DEBUG - -#if NET6_0_OR_GREATER -using System; -#endif -using System.Diagnostics; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_MESSAGE_SEVERITY; -#if !NET6_0_OR_GREATER +#if NET6_0_OR_GREATER +using Enum = System.Enum; +#else using Enum = ComputeSharp.NetStandard.System.Enum; #endif @@ -119,5 +116,3 @@ internal static partial class DeviceHelper return hasErrorsOrWarnings; } } - -#endif diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs index 77e454a0..07e8a8f0 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs @@ -20,11 +20,11 @@ internal static partial class DeviceHelper /// /// The creation flags for instances. /// - private const uint IDXGIFactoryCreationFlags = + private static readonly uint IDXGIFactoryCreationFlags = #if DEBUG DXGI.DXGI_CREATE_FACTORY_DEBUG; #else - 0; + Configuration.IsDebugOutputEnabled ? (uint)DXGI.DXGI_CREATE_FACTORY_DEBUG : 0; #endif /// @@ -35,9 +35,10 @@ internal static partial class DeviceHelper { using ComPtr dxgiFactory4 = default; -#if DEBUG - EnableDebugMode(); -#endif + if (Configuration.IsDebugOutputEnabled) + { + EnableDebugMode(); + } DirectX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, Windows.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); @@ -60,7 +61,6 @@ internal static partial class DeviceHelper return; } -#if DEBUG /// /// Enables the debug layer for DirectX APIs. /// @@ -79,7 +79,6 @@ internal static partial class DeviceHelper d3D12Debug1.Get()->SetEnableSynchronizedCommandQueueValidation(Windows.TRUE); } } -#endif /// /// A custom fallback implementation to use on systems with no support for it. diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs new file mode 100644 index 00000000..5af7e41f --- /dev/null +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -0,0 +1,32 @@ +using System; + +/// +/// A container for all shared configuration switches for ComputeSharp. +/// +internal static class Configuration +{ + /// + /// The configuration property name for . + /// + private const string EnableDebugOutput = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; + + /// + /// Indicates whether or not the debug output is enabled. + /// + public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput); + + /// + /// Gets a configuration value for a specified property. + /// + /// The property name to retrieve the value for. + /// The value of the specified configuration setting. + private static bool GetConfigurationValue(string propertyName) + { + if (AppContext.TryGetSwitch(propertyName, out bool isEnabled)) + { + return isEnabled; + } + + return false; + } +} \ No newline at end of file