diff --git a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj index 6e94f279..5be6880a 100644 --- a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj @@ -34,6 +34,17 @@ Speed + + + + + + + diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 272a33ff..0ad1927a 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -28,6 +28,11 @@ + + + + + diff --git a/src/ComputeSharp/ComputeSharp.targets b/src/ComputeSharp/ComputeSharp.targets index 72cc9d66..991faa81 100644 --- a/src/ComputeSharp/ComputeSharp.targets +++ b/src/ComputeSharp/ComputeSharp.targets @@ -57,4 +57,26 @@ + + + + + + + + + + + + + diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs index e5e96e46..4ed1e4b7 100644 --- a/src/ComputeSharp/Properties/Configuration.cs +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -2,59 +2,148 @@ using System; #if DEBUG using System.Diagnostics; #endif +using System.Runtime.CompilerServices; /// /// A container for all shared configuration switches for ComputeSharp. /// +/// +/// +/// This type uses a very specific setup for configuration switches to ensure ILLink can work the best. +/// This mirrors the architecture of feature switches in the runtime as well, and it's needed so that +/// no static constructor is generated for the type. +/// +/// +/// For more info, see . +/// +/// internal static class Configuration { /// /// The configuration property name for . /// - private const string EnableDebugOutput = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; + private const string IsDebugOutputEnabledPropertyName = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; /// /// The configuration property name for . /// - private const string EnableDeviceRemovedExtendedDataInfo = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"; + private const string IsDeviceRemovedExtendedDataEnabledPropertyName = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"; /// /// The configuration property name for . /// - private const string DisableGpuTimeout = "COMPUTESHARP_DISABLE_GPU_TIMEOUT"; + private const string IsGpuTimeoutDisabledPropertyName = "COMPUTESHARP_DISABLE_GPU_TIMEOUT"; /// - /// Indicates whether or not the debug output is enabled. + /// The backing field for . /// - public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput); + private static int isDebugOutputEnabledConfigurationValue; /// - /// Indicates whether or not the debug output is enabled. + /// The backing field for . /// - public static readonly bool IsDeviceRemovedExtendedDataEnabled = GetConfigurationValue(EnableDeviceRemovedExtendedDataInfo); + private static int isDeviceRemovedExtendedDataEnabledConfigurationValue; /// - /// Indicates whether or not the GPU timeout is disabled. + /// The backing field for . /// - public static readonly bool IsGpuTimeoutDisabled = GetConfigurationValue(DisableGpuTimeout); + private static int isGpuTimeoutDisabledConfigurationValue; + + /// + /// Gets a value indicating whether or not the debug output is enabled (defaults to ). + /// + public static bool IsDebugOutputEnabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => GetConfigurationValue(IsDebugOutputEnabledPropertyName, ref isDebugOutputEnabledConfigurationValue); + } + + /// + /// Gets a value indicating whether or not the debug output is enabled (defaults to ). + /// + public static bool IsDeviceRemovedExtendedDataEnabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => GetConfigurationValue(IsDeviceRemovedExtendedDataEnabledPropertyName, ref isDeviceRemovedExtendedDataEnabledConfigurationValue); + } + + /// + /// Gets a value indicating whether or not the GPU timeout is disabled (defaults to ). + /// + public static bool IsGpuTimeoutDisabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => GetConfigurationValue(IsGpuTimeoutDisabledPropertyName, ref isGpuTimeoutDisabledConfigurationValue); + } /// /// Gets a configuration value for a specified property. /// /// The property name to retrieve the value for. + /// The cached result for the target configuration value. /// The value of the specified configuration setting. - private static bool GetConfigurationValue(string propertyName) + private static bool GetConfigurationValue(string propertyName, ref int cachedResult) { -#if DEBUG - if (Debugger.IsAttached && propertyName != DisableGpuTimeout) + // The cached switch value has 3 states: + // 0: unknown. + // 1: true + // -1: false + // + // This method doesn't need to worry about concurrent accesses to the cached result, + // as even if the configuration value is retrieved twice, that'll always be the same. + if (cachedResult < 0) + { + return false; + } + + if (cachedResult > 0) { return true; } -#endif - if (AppContext.TryGetSwitch(propertyName, out bool isEnabled)) + // Get the configuration switch value, or its default + if (!AppContext.TryGetSwitch(propertyName, out bool isEnabled)) { - return isEnabled; + isEnabled = GetDefaultConfigurationValue(propertyName); + } + + // Update the cached result + cachedResult = isEnabled ? 1 : -1; + + return isEnabled; + } + + /// + /// Gets the default configuration value for a given feature switch. + /// + /// The property name to retrieve the value for. + /// The default value for the target . + private static bool GetDefaultConfigurationValue(string propertyName) + { + // Debug output (always enabled in DEBUG if there is an attached debugger) + if (propertyName == IsDebugOutputEnabledPropertyName) + { +#if DEBUG + return Debugger.IsAttached; +#else + return false; +#endif + } + + // Device removed extended data (same as for the debug output) + if (propertyName == IsDeviceRemovedExtendedDataEnabledPropertyName) + { +#if DEBUG + return Debugger.IsAttached; +#else + return false; +#endif + } + + // Disable GPU timeout (always false by default) + if (propertyName == IsGpuTimeoutDisabledPropertyName) + { + return false; } return false; diff --git a/src/ComputeSharp/Properties/ILLink.Substitutions.xml b/src/ComputeSharp/Properties/ILLink.Substitutions.xml new file mode 100644 index 00000000..3885337c --- /dev/null +++ b/src/ComputeSharp/Properties/ILLink.Substitutions.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file