diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs index 8a6e2986..75f30528 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -19,7 +19,22 @@ internal static class ArgumentOutOfRangeExceptionExtensions { if (value < 0) { - Throw(parameterName); + Throw(parameterName, value); + } + } + + /// + /// Throws an if is negative or zero. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-zero or non-negative. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegativeOrZero(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value <= 0) + { + Throw(parameterName, value); } } @@ -27,10 +42,11 @@ internal static class ArgumentOutOfRangeExceptionExtensions /// Throws an for a given parameter name. /// /// The name of the parameter to report in the exception. + /// The invalid value that was used. /// Thrown with . [DoesNotReturn] - private static void Throw(string? parameterName) + private static void Throw(string? parameterName, int value) { - throw new ArgumentOutOfRangeException(parameterName); + throw new ArgumentOutOfRangeException(parameterName, value, null); } } diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 2fb57469..954e62c5 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -181,9 +181,9 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - Guard.IsGreaterThan(x, 0); - Guard.IsGreaterThan(y, 0); - Guard.IsGreaterThan(z, 0); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(x); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); Guard.IsBetweenOrEqualTo(threadsX, 1, 1024); Guard.IsBetweenOrEqualTo(threadsY, 1, 1024); Guard.IsBetweenOrEqualTo(threadsZ, 1, 64);