From 92fbf7518566e70a40c741dd8446e2557c8454e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Feb 2023 18:51:51 +0100 Subject: [PATCH] Remove ThrowHelper APIs in D2D1 projects, switch to new extensions --- .../Polyfills/ArgumentExceptionExtensions.cs | 13 ++++ .../InvalidOperationExceptionExtensions.cs | 13 ++++ .../NotSupportedExceptionExtensions.cs | 25 +++++++ .../Buffers/GraphicsEffectSourceBuffer.cs | 6 +- .../Buffers/ResourceTextureManagerBuffer.cs | 6 +- .../Collections/FixedCountList{T}.cs | 17 +---- .../Extensions/ID2D1EffectExtensions.cs | 5 +- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 6 +- ...derEffect{T}.Properties.Interop.Sources.cs | 11 +-- ...erties.ResourceTextureManagerCollection.cs | 22 ++---- ...PixelShaderEffect{T}.Properties.Sources.cs | 19 +----- src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs | 67 ------------------- .../Buffers/PinnedBufferMemoryManager.cs | 6 +- .../Interop/D2D1EffectRegistrationData.cs | 19 +++--- .../Shaders/Interop/D2D1PixelShader.cs | 23 ++----- .../Loaders/D2D1ShaderBytecodeLoader.cs | 21 ++---- .../Dispatching/ShaderBytecodeLoader.cs | 2 +- 17 files changed, 87 insertions(+), 194 deletions(-) delete mode 100644 src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs index 7ba9a88f..f13c0f3f 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs @@ -20,6 +20,19 @@ internal static class ArgumentExceptionExtensions throw new ArgumentException(null, parameterName); } + /// + /// Throws an for a given parameter name and message. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The name of the parameter to report in the exception. + /// The message to include in the exception. + /// Thrown with . + [DoesNotReturn] + public static void Throw(this ArgumentException? _, string? parameterName, string? message) + { + throw new ArgumentException(message, parameterName); + } + /// /// Throws an for a given parameter name. /// diff --git a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs index 9028f05b..38a6cd11 100644 --- a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs @@ -8,6 +8,19 @@ namespace System; /// internal static class InvalidOperationExceptionExtensions { + /// + /// Throws an for a given parameter name. + /// + /// The type to let the compiler assume it returns. + /// Dummy value to invoke the extension upon (always pass . + /// This method never returns any values. + /// Thrown with no parameters. + [DoesNotReturn] + public static T Throw(this InvalidOperationException? _) + { + throw new NotSupportedException(); + } + /// /// Throws an if is . /// diff --git a/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs index 601b7876..210c3f33 100644 --- a/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace System; @@ -30,4 +31,28 @@ internal static class NotSupportedExceptionExtensions { throw new NotSupportedException(); } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this NotSupportedException? _, [DoesNotReturnIf(true)] bool condition) + { + if (condition) + { + Throw(); + } + } + + /// + /// Throws an for a given parameter name. + /// + /// Always thrown with no parameters. + [DoesNotReturn] + private static void Throw() + { + throw new NotSupportedException(); + } } diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs index 364e72b4..56a0bfba 100644 --- a/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using Windows.Graphics.Effects; namespace ComputeSharp.D2D1.Uwp.Buffers; @@ -103,10 +102,7 @@ internal struct GraphicsEffectSourceBuffer [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if ((uint)index >= 16) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); - } + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16); ref SourceReference r0 = ref Unsafe.As(ref this); ref SourceReference r1 = ref Unsafe.Add(ref r0, index); diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs index c9ab2eab..af57a6db 100644 --- a/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; namespace ComputeSharp.D2D1.Uwp.Buffers; @@ -103,10 +102,7 @@ internal struct ResourceTextureManagerBuffer [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if ((uint)index >= 16) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); - } + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16); ref D2D1ResourceTextureManager? r0 = ref Unsafe.As(ref this); ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index); diff --git a/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs b/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs index cd456511..74dd8fe9 100644 --- a/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using ComputeSharp.D2D1.Helpers; namespace ComputeSharp.D2D1.Uwp.Collections; @@ -22,10 +21,7 @@ internal static class FixedCountList Span span = array.AsSpan(index); - if (list.Indices.Length > span.Length) - { - ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items."); - } + default(ArgumentException).ThrowIf(list.Indices.Length > span.Length, nameof(array)); foreach (int i in list.Indices) { @@ -42,18 +38,11 @@ internal static class FixedCountList public static void CopyTo(IFixedCountList list, Array array, int index) { default(ArgumentNullException).ThrowIfNull(array); - - if ((uint)index >= array.Length) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The starting array index is out of range."); - } + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, array.Length); int remainingLength = array.Length - index; - if (list.Indices.Length > remainingLength) - { - ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items."); - } + default(ArgumentException).ThrowIf(list.Indices.Length > remainingLength, nameof(array)); foreach (int i in list.Indices) { diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index e937b79c..7e3cfc78 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -3,7 +3,6 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; @@ -158,9 +157,7 @@ internal static unsafe class ID2D1EffectExtensions } // There was no way to retrieve the correct managed wrapper - ThrowHelper.ThrowInvalidOperationException("Cannot retrieve a D2D1ResourceTextureManager instance wrapping a native object that was set externally."); - - return null; + return default(InvalidOperationException).Throw(); } /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 985fbddf..fb7eb262 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -3,7 +3,6 @@ using System.Runtime.InteropServices; using System.Threading; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Uwp.Extensions; @@ -350,10 +349,7 @@ unsafe partial class PixelShaderEffect if (source is null) { // If the source is null and that is not valid here (eg. when drawing), just throw - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - ThrowHelper.ThrowInvalidOperationException("The effect has one or more sources set to null, and null effect inputs are not valid at this time."); - } + default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE); } else { diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs index e28378db..30db4eda 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -3,7 +3,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; @@ -147,10 +146,7 @@ unsafe partial class PixelShaderEffect } // If unrealization isn't requested for failures, just throw - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop)."); - } + default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE); Unrealize(value, index); @@ -184,10 +180,7 @@ unsafe partial class PixelShaderEffect // on that specific source will just fail during the recursive traversal, but that couldn't be checked from here anyway. if (sourceCanvasDeviceType == WIN2D_GET_DEVICE_ASSOCIATION_TYPE_CREATION_DEVICE && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get())) { - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device."); - } + default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE); Unrealize(value, index); diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index d35ab020..b83600cf 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Shaders.Loaders; using ComputeSharp.D2D1.Uwp.Buffers; @@ -56,16 +55,17 @@ partial class PixelShaderEffect { get { - ValidateIndex(index); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16, nameof(index)); + default(ArgumentOutOfRangeException).ThrowIf((IndexBitmask & (1 << index)) == 0, nameof(index)); return Owner.GetResourceTextureManager(index); } set { + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16, nameof(index)); + default(ArgumentOutOfRangeException).ThrowIf((IndexBitmask & (1 << index)) == 0, nameof(index)); default(ArgumentNullException).ThrowIfNull(value); - ValidateIndex(index); - Owner.SetResourceTextureManager(value, index); } } @@ -218,20 +218,6 @@ partial class PixelShaderEffect throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); } - /// - /// Validates the input index for . - /// - /// The index to validate. - /// Thrown if is out of range. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ValidateIndex(int index) - { - if ((uint)index >= 16 || (IndexBitmask & (1 << index)) == 0) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid resource texture manager index for the current effect."); - } - } - /// /// Gets the bitmask of indices for resource textures in the target shader. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs index 2746b9c4..5c313d8c 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Collections; @@ -50,13 +49,13 @@ partial class PixelShaderEffect { get { - ValidateIndex(index); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, D2D1PixelShader.GetInputCount(), nameof(index)); return Owner.GetSource(index); } set { - ValidateIndex(index); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, D2D1PixelShader.GetInputCount(), nameof(index)); Owner.SetSource(value, index); } @@ -205,20 +204,6 @@ partial class PixelShaderEffect throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection."); } - /// - /// Validates the input index for . - /// - /// The index to validate. - /// Thrown if is out of range. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ValidateIndex(int index) - { - if ((uint)index >= D2D1PixelShader.GetInputCount()) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid source index for the current effect."); - } - } - /// /// Gets the collection of valid indices for the current effect. /// diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs deleted file mode 100644 index e164d403..00000000 --- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; - -namespace ComputeSharp.D2D1.Helpers; - -/// -/// Helper methods to efficiently throw exceptions. -/// -internal static class ThrowHelper -{ - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with the specified parameters. - [DoesNotReturn] - public static void ThrowArgumentException(string? name, string? message) - { - throw new ArgumentException(message, name); - } - - /// - /// Throws a new . - /// - /// The argument name. - /// Thrown with the specified parameter. - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeException(string? name) - { - throw new ArgumentOutOfRangeException(name); - } - - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with the specified parameter. - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeException(string? name, string? message) - { - throw new ArgumentOutOfRangeException(name, message); - } - - /// - /// Throws a new . - /// - /// The message to include in the exception. - /// Thrown with the specified parameter. - [DoesNotReturn] - public static void ThrowInvalidOperationException(string? message) - { - throw new InvalidOperationException(message); - } - - /// - /// Throws a new . - /// - /// The message to include in the exception. - /// Thrown with the specified parameter. - [DoesNotReturn] - public static void ThrowNotSupportedException(string? message) - { - throw new NotSupportedException(message); - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs index d4a4b09b..72f366df 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs @@ -2,7 +2,6 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.D2D1.Helpers; namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; @@ -40,10 +39,7 @@ internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager /// public override unsafe MemoryHandle Pin(int elementIndex = 0) { - if ((uint)elementIndex >= this.length) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(elementIndex)); - } + default(ArgumentOutOfRangeException).ThrowIfNotInRange(elementIndex, 0, this.length); return new(this.buffer + elementIndex); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs index 5f7e6715..4dfc7fa8 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices; using System.Text; -using ComputeSharp.D2D1.Helpers; namespace ComputeSharp.D2D1.Interop; @@ -94,31 +93,31 @@ public static class D2D1EffectRegistrationData case LoadingResult.Success: return data; case LoadingResult.EmptyBuffer: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is empty."); + default(ArgumentException).Throw(nameof(blob), "The input blob is empty."); break; case LoadingResult.BufferTooShort: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too short."); + default(ArgumentException).Throw(nameof(blob), "The input blob is too short."); break; case LoadingResult.BufferTooLong: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too long."); + default(ArgumentException).Throw(nameof(blob), "The input blob is too long."); break; case LoadingResult.MismatchedBlobVersionId: - ThrowHelper.ThrowArgumentException(nameof(blob), "The blob version id from the input blob does not match."); + default(ArgumentException).Throw(nameof(blob), "The blob version id from the input blob does not match."); break; case LoadingResult.InvalidNumberOfInputs: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid number of inputs."); + default(ArgumentException).Throw(nameof(blob), "The input blob has an invalid number of inputs."); break; case LoadingResult.InvalidPropertyXml: - ThrowHelper.ThrowArgumentException(nameof(blob), "The property XML value in the input blob is invalid."); + default(ArgumentException).Throw(nameof(blob), "The property XML value in the input blob is invalid."); break; case LoadingResult.InvalidNumberOfBindings: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob declares an invalid number of bindings."); + default(ArgumentException).Throw(nameof(blob), "The input blob declares an invalid number of bindings."); break; case LoadingResult.InvalidBindingPropertyName: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid property name for one of its property bindings."); + default(ArgumentException).Throw(nameof(blob), "The input blob has an invalid property name for one of its property bindings."); break; default: - ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob failed to be loaded for an unknown reason."); + default(ArgumentException).Throw(nameof(blob), "The input blob failed to be loaded for an unknown reason."); break; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index e4b91749..3f09285b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Shaders.Interop.Buffers; using ComputeSharp.D2D1.Shaders.Loaders; using TerraFX.Interop.DirectX; @@ -132,10 +131,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions compileOptions) where T : unmanaged, ID2D1PixelShader { - if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) - { - ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); - } + default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions)); return LoadOrCompileBytecode(null, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _); } @@ -167,10 +163,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions compileOptions, out D2D1ShaderProfile shaderProfile) where T : unmanaged, ID2D1PixelShader { - if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) - { - ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); - } + default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions)); return LoadOrCompileBytecode(null, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out shaderProfile, out _); } @@ -193,10 +186,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions compileOptions) where T : unmanaged, ID2D1PixelShader { - if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) - { - ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); - } + default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions)); return LoadOrCompileBytecode(shaderProfile, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _); } @@ -280,10 +270,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - if ((uint)index >= shader.GetInputCount()) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is outside of range for the target pixel shader type."); - } + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, (int)shader.GetInputCount()); return (D2D1PixelShaderInputType)shader.GetInputType((uint)index); } @@ -395,7 +382,7 @@ public static class D2D1PixelShader { if (!TryGetConstantBuffer(in shader, span, out int writtenBytes)) { - ThrowHelper.ThrowArgumentException(nameof(span), "The destination span is too short."); + default(ArgumentException).Throw(nameof(span)); } return writtenBytes; diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs index 4f3badee..108610f1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs @@ -3,7 +3,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.__Internals; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -49,16 +48,9 @@ internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void ID2D1BytecodeLoader.LoadDynamicBytecode(IntPtr handle) { - if (this.embeddedBytecodePtr is not null || - this.d3DBlob.Get() is not null) - { - ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); - } - - if (handle == IntPtr.Zero) - { - ThrowHelper.ThrowNotSupportedException("Runtime shader compilation is not supported by the current configuration."); - } + default(InvalidOperationException).ThrowIf(this.embeddedBytecodePtr is not null); + default(InvalidOperationException).ThrowIf(this.d3DBlob.Get() is not null); + default(NotSupportedException).ThrowIf(handle == IntPtr.Zero); this.d3DBlob = new ComPtr((ID3DBlob*)handle); } @@ -67,11 +59,8 @@ internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader [MethodImpl(MethodImplOptions.AggressiveInlining)] void ID2D1BytecodeLoader.LoadEmbeddedBytecode(ReadOnlySpan bytecode) { - if (this.embeddedBytecodePtr is not null || - this.d3DBlob.Get() is not null) - { - ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); - } + default(InvalidOperationException).ThrowIf(this.embeddedBytecodePtr is not null); + default(InvalidOperationException).ThrowIf(this.d3DBlob.Get() is not null); this.embeddedBytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(bytecode)); this.embeddedBytecodeSize = bytecode.Length; diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs index 0c6ddda1..2971e844 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -35,7 +35,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader public unsafe void LoadDynamicBytecode(IntPtr handle) { default(InvalidOperationException).ThrowIf(this.cachedShader is not null); - default(InvalidOperationException).ThrowIf(handle == IntPtr.Zero); + default(NotSupportedException).ThrowIf(handle == IntPtr.Zero); this.cachedShader = new ICachedShader.Dynamic((IDxcBlob*)handle); }