diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs new file mode 100644 index 00000000..3f7a7699 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ArgumentExceptionExtensions +{ + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this ArgumentException? _, bool condition, string? parameterName = null) + { + if (condition) + { + Throw(parameterName); + } + } + + /// + /// Throws an for a given parameter name. + /// + /// The name of the parameter to report in the exception. + /// Thrown with . + [DoesNotReturn] + private static void Throw(string? parameterName) + { + throw new ArgumentException(parameterName); + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index c6b57168..16f288ab 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -2,7 +2,6 @@ using System; using System.Buffers; using System.IO; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; @@ -69,8 +68,8 @@ public static class Texture2DExtensions { default(ArgumentNullException).ThrowIfNull(source); default(ArgumentNullException).ThrowIfNull(destination); - Guard.IsEqualTo(destination.GetLength(0), source.Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), source.Width, nameof(destination)); + default(ArgumentException).ThrowIf(destination.GetLength(0) != source.Height, nameof(destination)); + default(ArgumentException).ThrowIf(destination.GetLength(1) != source.Width, nameof(destination)); source.CopyTo(ref destination[0, 0], destination.Length, 0, 0, source.Width, source.Height); } @@ -359,8 +358,8 @@ public static class Texture2DExtensions { default(ArgumentNullException).ThrowIfNull(destination); default(ArgumentNullException).ThrowIfNull(source); - Guard.IsEqualTo(source.GetLength(0), destination.Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), destination.Width, nameof(source)); + default(ArgumentException).ThrowIf(source.GetLength(0) != destination.Height, nameof(source)); + default(ArgumentException).ThrowIf(source.GetLength(1) != destination.Width, nameof(source)); destination.CopyFrom(ref source[0, 0], source.Length, 0, 0, destination.Width, destination.Height); } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index 6dcfbedf..e38b9859 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -78,9 +77,9 @@ public static class Texture3DExtensions { default(ArgumentNullException).ThrowIfNull(source); default(ArgumentNullException).ThrowIfNull(destination); - Guard.IsEqualTo(destination.GetLength(0), source.Depth, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), source.Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(2), source.Width, nameof(destination)); + default(ArgumentException).ThrowIf(destination.GetLength(0) != source.Depth, nameof(destination)); + default(ArgumentException).ThrowIf(destination.GetLength(1) != source.Height, nameof(destination)); + default(ArgumentException).ThrowIf(destination.GetLength(2) != source.Width, nameof(destination)); source.CopyTo(ref destination[0, 0, 0], destination.Length, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -336,9 +335,9 @@ public static class Texture3DExtensions { default(ArgumentNullException).ThrowIfNull(destination); default(ArgumentNullException).ThrowIfNull(source); - Guard.IsEqualTo(source.GetLength(0), destination.Depth, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), destination.Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(2), destination.Width, nameof(source)); + default(ArgumentException).ThrowIf(source.GetLength(0) != destination.Depth, nameof(source)); + default(ArgumentException).ThrowIf(source.GetLength(1) != destination.Height, nameof(source)); + default(ArgumentException).ThrowIf(source.GetLength(2) != destination.Width, nameof(source)); destination.CopyFrom(ref source[0, 0, 0], source.Length, 0, 0, 0, destination.Width, destination.Height, destination.Depth); } diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index 791fd01f..c1fde3b6 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -2,7 +2,6 @@ using System; using System.Buffers; using System.IO; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.Windows; @@ -305,8 +304,8 @@ internal sealed unsafe class WICHelper wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert(); // Validate the target texture has the right size - Guard.IsEqualTo((uint)texture.Width, width, nameof(texture.Width)); - Guard.IsEqualTo((uint)texture.Height, height, nameof(texture.Height)); + default(ArgumentException).ThrowIf((uint)texture.Width != width, nameof(texture.Width)); + default(ArgumentException).ThrowIf((uint)texture.Height != height, nameof(texture.Height)); Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType(); Guid wicActualPixelFormatGuid; diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs index ed04a448..bfd0b0b9 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs @@ -177,7 +177,7 @@ public readonly unsafe ref struct TextureView2D } else { - Guard.HasSizeGreaterThanOrEqualTo(destination, Length); + default(ArgumentException).ThrowIf(destination.Length != Length, nameof(destination)); for (int y = 0, j = 0; y < this.height; y++, j += this.width) { diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs index 6218557c..eb92cd8a 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Shaders.Extensions; using ComputeSharp.__Internals; using TerraFX.Interop.DirectX; @@ -34,7 +33,7 @@ internal readonly unsafe struct ShaderDispatchMetadataLoader : IDispatchMetadata /// public unsafe void LoadMetadataHandle(ReadOnlySpan serializedMetadata, ReadOnlySpan resourceDescriptors, out IntPtr result) { - Guard.HasSizeEqualTo(serializedMetadata, 5); + default(ArgumentException).ThrowIf(serializedMetadata.Length != 5, nameof(serializedMetadata)); fixed (IntPtr* p = &result) {