diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index f8982662..7fc70f5b 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -1,4 +1,4 @@ - + Exe @@ -21,6 +21,7 @@ + diff --git a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj index bf266575..386056d1 100644 --- a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj +++ b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj @@ -12,6 +12,7 @@ + diff --git a/samples/ComputeSharp.Sample/Helpers/Formatting.cs b/samples/ComputeSharp.Sample/Helpers/Formatting.cs index 54c23634..ff4a1abb 100644 --- a/samples/ComputeSharp.Sample/Helpers/Formatting.cs +++ b/samples/ComputeSharp.Sample/Helpers/Formatting.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Linq; -using CommunityToolkit.Diagnostics; namespace ComputeSharp.Sample; @@ -19,12 +18,6 @@ internal static class Formatting /// The name of the matrix to print. public static void PrintMatrix(float[] array, int width, int height, string name) { - Guard.IsNotNull(array); - Guard.IsGreaterThan(width, 0); - Guard.IsGreaterThan(height, 0); - Guard.IsEqualTo(array.Length, (long)width * height, nameof(array)); - Guard.IsNotNullOrEmpty(name); - int pad = 48 - name.Length; string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; diff --git a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj index a078c4d8..05f3877e 100644 --- a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj @@ -26,8 +26,10 @@ --> true + false true true + true false true Speed diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 62a03e70..3c5b8b74 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -160,6 +160,9 @@ + + 8.1.0 + 8.1.0 diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj index 33c791d5..4b8cb8cf 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj @@ -29,6 +29,7 @@ + diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs new file mode 100644 index 00000000..f13c0f3f --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs @@ -0,0 +1,75 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ArgumentExceptionExtensions +{ + /// + /// Throws an for a given parameter name. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The name of the parameter to report in the exception. + /// Thrown with . + [DoesNotReturn] + public static void Throw(this ArgumentException? _, string? parameterName) + { + 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. + /// + /// The type to let the compiler assume it returns. + /// Dummy value to invoke the extension upon (always pass . + /// The name of the parameter to report in the exception. + /// This method never returns any values. + /// Thrown with . + [DoesNotReturn] + public static T Throw(this ArgumentException? _, string? parameterName) + { + throw new ArgumentException(null, parameterName); + } + + /// + /// 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? _, [DoesNotReturnIf(true)] 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(null, parameterName); + } +} diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs new file mode 100644 index 00000000..640cc2ec --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs @@ -0,0 +1,61 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ArgumentNullExceptionExtensions +{ + /// + /// Throws an for a given parameter name. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The name of the parameter to report in the exception. + /// Thrown with . + [DoesNotReturn] + public static void Throw(this ArgumentNullException? _, string? parameterName) + { + throw new ArgumentNullException(parameterName); + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The reference type argument to validate as non-. + /// The name of the parameter with which corresponds. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNull(this ArgumentNullException? _, [NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? parameterName = null) + { + if (argument is null) + { + Throw(parameterName); + } + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The pointer argument to validate as non-. + /// The name of the parameter with which corresponds. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void ThrowIfNull(this ArgumentNullException? _, [NotNull] void* argument, [CallerArgumentExpression(nameof(argument))] string? parameterName = null) + { + if (argument is null) + { + Throw(parameterName); + } + } + + /// + [DoesNotReturn] + private static void Throw(string? parameterName) + { + throw new ArgumentNullException(parameterName); + } +} diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs new file mode 100644 index 00000000..0e617bdf --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -0,0 +1,173 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ArgumentOutOfRangeExceptionExtensions +{ + /// + /// Throws an if is not zero. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as zero. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNotZero(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value != 0) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is negative. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-negative. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegative(this ArgumentOutOfRangeException? _, int value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < 0) + { + 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); + } + } + + /// + /// Throws an if is greater than . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfGreaterThan(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value > other) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is less than . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfLessThan(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < other) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is less than . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfLessThan(this ArgumentOutOfRangeException? _, long value, long other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < other) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is not in the specified range. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-zero or non-negative. + /// The inclusive starting value for the range. + /// The exclusive ending value for the range. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNotInRange(this ArgumentOutOfRangeException? _, int value, int minimum, int maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < minimum || value >= maximum) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is not in the specified range. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-zero or non-negative. + /// The inclusive starting value for the range. + /// The inclusive ending value for the range. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNotBetweenOrEqual(this ArgumentOutOfRangeException? _, int value, int minimum, int maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < minimum || value > maximum) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is not in the specified range. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as non-zero or non-negative. + /// The inclusive starting value for the range. + /// The inclusive ending value for the range. + /// The name of the parameter with which corresponds. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNotBetweenOrEqual(this ArgumentOutOfRangeException? _, long value, long minimum, long maximum, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < minimum || value > maximum) + { + Throw(parameterName, value); + } + } + + /// + /// 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, int value) + { + throw new ArgumentOutOfRangeException(parameterName, value, null); + } + + /// + [DoesNotReturn] + private static void Throw(string? parameterName, long value) + { + throw new ArgumentOutOfRangeException(parameterName, value, null); + } +} diff --git a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs new file mode 100644 index 00000000..38a6cd11 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs @@ -0,0 +1,47 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +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 . + /// + /// 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 InvalidOperationException? _, [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 InvalidOperationException(); + } +} diff --git a/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs new file mode 100644 index 00000000..210c3f33 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/NotSupportedExceptionExtensions.cs @@ -0,0 +1,58 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class NotSupportedExceptionExtensions +{ + /// + /// Throws an . + /// + /// Dummy value to invoke the extension upon (always pass . + /// Thrown with no parameters. + [DoesNotReturn] + public static void Throw(this NotSupportedException? _) + { + throw new NotSupportedException(); + } + + /// + /// 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 NotSupportedException? _) + { + 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.Core/Polyfills/ObjectDisposedExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ObjectDisposedExceptionExtensions.cs new file mode 100644 index 00000000..21335f80 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ObjectDisposedExceptionExtensions.cs @@ -0,0 +1,60 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ObjectDisposedExceptionExtensions +{ + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition indicating whether has been disposed. + /// The instance that can be signaled as being disposed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this ObjectDisposedException? _, [DoesNotReturnIf(true)] bool condition, object disposableInstance) + { + if (condition) + { + Throw(disposableInstance); + } + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The instance to check to determine whether the target instance has been disposed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNull(this ObjectDisposedException? _, [NotNull] object? validationObject) + { + if (validationObject is null) + { + Throw(); + } + } + + /// + /// Throws an . + /// + /// Thrown with no parameters. + [DoesNotReturn] + private static void Throw() + { + throw new ObjectDisposedException(null); + } + + /// + /// Throws an for a given target instance. + /// + /// The target instance that was disposed. + /// Thrown for . + [DoesNotReturn] + private static void Throw(object targetObject) + { + throw new ObjectDisposedException(targetObject.ToString()); + } +} diff --git a/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs new file mode 100644 index 00000000..f24bc389 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs @@ -0,0 +1,61 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System.ComponentModel; + +/// +/// Throw helper extensions for . +/// +internal static class Win32ExceptionExtensions +{ + /// + /// Throws an for a given error code. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The error code to report. + [DoesNotReturn] + public static void Throw(this Win32Exception? _, int hresult) + { + throw new Win32Exception(hresult); + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + /// The error code to report. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this Win32Exception? _, [DoesNotReturnIf(true)] bool condition, int hresult) + { + if (condition) + { + Throw(hresult); + } + } + + /// + /// Throws an if a given error code represents an error. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The error code to check. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfFailed(this Win32Exception? _, int hresult) + { + if (hresult < 0) + { + Throw(hresult); + } + } + + /// + /// Throws an for a given error code. + /// + /// The error code to report. + /// Thrown with the given error code. + [DoesNotReturn] + private static void Throw(int hresult) + { + throw new Win32Exception(hresult); + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index e04c8b81..f3fd8190 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -22,6 +22,7 @@ + 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/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs index 694bf1a0..4d630cb2 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; @@ -69,10 +68,7 @@ unsafe partial class CanvasEffect { lock (this.lockObject) { - if (this.isDisposed) - { - ThrowObjectDisposedException(); - } + default(ObjectDisposedException).ThrowIf(this.isDisposed, this); this.canvasImage ??= CreateCanvasImage(); @@ -99,13 +95,4 @@ unsafe partial class CanvasEffect canvasImageUnknown.CopyTo(canvasImageInterop).Assert(); } - - /// - /// Throws a new . - /// - [DoesNotReturn] - public void ThrowObjectDisposedException() - { - throw new ObjectDisposedException(ToString(), "The current effect has been disposed and can no longer be used."); - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs b/src/ComputeSharp.D2D1.Uwp/Collections/FixedCountList{T}.cs index 1695dd81..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; @@ -18,17 +17,11 @@ internal static class FixedCountList /// The initial index within . public static void CopyTo(IFixedCountList list, T[] array, int index) { - if (array is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(array); 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) { @@ -44,22 +37,12 @@ internal static class FixedCountList /// The initial index within . public static void CopyTo(IFixedCountList list, Array array, int index) { - if (array is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null."); - } - - if ((uint)index >= array.Length) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The starting array index is out of range."); - } + default(ArgumentNullException).ThrowIfNull(array); + 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 1505c86e..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); @@ -220,7 +213,7 @@ unsafe partial class PixelShaderEffect else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) { // If the source is null and this is not explicitly allowed, the invocation is not valid - ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null."); + default(ArgumentNullException).Throw(nameof(value)); } // Save the managed wrapper and realized image diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index 9e3f9c4e..e458491f 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,18 +55,16 @@ 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 { - if (value is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(value), "Input resource texture managers cannot be null."); - } - - ValidateIndex(index); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16, nameof(index)); + default(ArgumentOutOfRangeException).ThrowIf((IndexBitmask & (1 << index)) == 0, nameof(index)); + default(ArgumentNullException).ThrowIfNull(value); Owner.SetResourceTextureManager(value, index); } @@ -111,30 +108,30 @@ partial class PixelShaderEffect bool ICollection.IsSynchronized => true; /// - object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for ResourceTextureManagerCollection."); + object ICollection.SyncRoot => default(NotSupportedException).Throw(); /// void ICollection.Add(D2D1ResourceTextureManager? item) { - throw new NotSupportedException("ICollection.Add is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// int IList.Add(object value) { - throw new NotSupportedException("IList.Add is not supported for ResourceTextureManagerCollection."); + return default(NotSupportedException).Throw(); } /// void ICollection.Clear() { - throw new NotSupportedException("ICollection.Clear is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.Clear() { - throw new NotSupportedException("IList.Clear is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// @@ -188,51 +185,37 @@ partial class PixelShaderEffect /// void IList.Insert(int index, D2D1ResourceTextureManager? item) { - throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.Insert(int index, object value) { - throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// bool ICollection.Remove(D2D1ResourceTextureManager? item) { - throw new NotSupportedException("ICollection.Remove is not supported for ResourceTextureManagerCollection."); + return default(NotSupportedException).Throw(); } /// void IList.Remove(object value) { - throw new NotSupportedException("IList.Remove is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - 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."); - } + default(NotSupportedException).Throw(); } /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs index 2746b9c4..3d36a9ea 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); } @@ -95,30 +94,30 @@ partial class PixelShaderEffect bool ICollection.IsSynchronized => true; /// - object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for SourceCollection."); + object ICollection.SyncRoot => default(NotSupportedException).Throw(); /// void ICollection.Add(IGraphicsEffectSource? item) { - throw new NotSupportedException("ICollection.Add is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// int IList.Add(object value) { - throw new NotSupportedException("IList.Add is not supported for SourceCollection."); + return default(NotSupportedException).Throw(); } /// void ICollection.Clear() { - throw new NotSupportedException("ICollection.Clear is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.Clear() { - throw new NotSupportedException("IList.Clear is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// @@ -172,51 +171,37 @@ partial class PixelShaderEffect /// void IList.Insert(int index, IGraphicsEffectSource? item) { - throw new NotSupportedException("IList.Insert is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.Insert(int index, object value) { - throw new NotSupportedException("IList.Insert is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// bool ICollection.Remove(IGraphicsEffectSource? item) { - throw new NotSupportedException("ICollection.Remove is not supported for SourceCollection."); + return default(NotSupportedException).Throw(); } /// void IList.Remove(object value) { - throw new NotSupportedException("IList.Remove is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - 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."); - } + default(NotSupportedException).Throw(); } /// diff --git a/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs index 656d8d96..ed1cb658 100644 --- a/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs @@ -1,6 +1,5 @@ using System.ComponentModel; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using TerraFX.Interop.Windows; @@ -21,10 +20,7 @@ internal static class HRESULTExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this HRESULT result) { - if ((int)result < 0) - { - ThrowWin32Exception(result); - } + default(Win32Exception).ThrowIfFailed((int)result); } /// @@ -35,19 +31,6 @@ internal static class HRESULTExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this int result) { - if (result < 0) - { - ThrowWin32Exception(result); - } - } - - /// - /// Throws a . - /// - /// The input return code. - [DoesNotReturn] - private static void ThrowWin32Exception(int result) - { - throw new Win32Exception(result); + default(Win32Exception).ThrowIfFailed(result); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs deleted file mode 100644 index f80e64f4..00000000 --- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs +++ /dev/null @@ -1,93 +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 type of expected result. - /// The argument name. - /// The message to include in the exception. - /// Thrown with the specified parameters. - /// This method always throws, so it actually never returns a value. - [DoesNotReturn] - public static T ThrowArgumentException(string? name, string? message) - { - throw new ArgumentException(message, name); - } - - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with the specified parameters. - [DoesNotReturn] - public static void ThrowArgumentNullException(string? name, string? message) - { - throw new ArgumentNullException(name, message); - } - - /// - /// 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/ArrayPoolBinaryWriter{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs index b84c8372..ba7c3cf5 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs @@ -57,10 +57,7 @@ internal ref struct ArrayPoolBufferWriter { T[]? array = this.array; - if (array is null) - { - ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException(); - } + default(ObjectDisposedException).ThrowIfNull(array); return array.AsSpan(0, this.index); } @@ -70,28 +67,14 @@ internal ref struct ArrayPoolBufferWriter /// Advances the current writer. /// /// The amount to advance. - /// - /// Must be called after . - /// This and the methods below are to enable mutating extensions. - /// + /// Must be called after . internal void Advance(int count) { T[]? array = this.array; - if (array is null) - { - ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException(); - } - - if (count < 0) - { - ArrayPoolBufferWriterHelpers.ThrowArgumentOutOfRangeExceptionForNegativeCount(); - } - - if (this.index > array.Length - count) - { - ArrayPoolBufferWriterHelpers.ThrowArgumentExceptionForAdvancedTooFar(); - } + default(ObjectDisposedException).ThrowIfNull(array); + default(ArgumentOutOfRangeException).ThrowIfNegative(count); + default(ArgumentException).ThrowIf(this.index > array.Length - count); this.index += count; } @@ -118,15 +101,8 @@ internal ref struct ArrayPoolBufferWriter { T[]? array = this.array; - if (array is null) - { - ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException(); - } - - if (sizeHint < 0) - { - ArrayPoolBufferWriterHelpers.ThrowArgumentOutOfRangeExceptionForNegativeSizeHint(); - } + default(ObjectDisposedException).ThrowIfNull(array); + default(ArgumentOutOfRangeException).ThrowIfNegative(sizeHint); if (sizeHint == 0) { @@ -181,46 +157,4 @@ internal ref struct ArrayPoolBufferWriter ArrayPool.Shared.Return(array); } -} - -/// -/// Private helpers for . -/// -file static class ArrayPoolBufferWriterHelpers -{ - /// - /// Throws an when the requested count is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeExceptionForNegativeCount() - { - throw new ArgumentOutOfRangeException("count", "The count can't be a negative value."); - } - - /// - /// Throws an when the size hint is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeExceptionForNegativeSizeHint() - { - throw new ArgumentOutOfRangeException("sizeHint", "The size hint can't be a negative value."); - } - - /// - /// Throws an when the requested count is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentExceptionForAdvancedTooFar() - { - throw new ArgumentException("The buffer writer has advanced too far."); - } - - /// - /// Throws an when the array is . - /// - [DoesNotReturn] - public static void ThrowObjectDisposedException() - { - throw new ObjectDisposedException("The current buffer has already been disposed."); - } } \ 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 e49184e8..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; @@ -433,10 +420,7 @@ public static class D2D1PixelShader public static unsafe void SetConstantBufferForD2D1DrawInfo(void* d2D1DrawInfo, in T shader) where T : unmanaged, ID2D1PixelShader { - if (d2D1DrawInfo is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1DrawInfo), "The input ID2D1DrawInfo object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1DrawInfo); D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 3ce44ca3..d9adec9c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Shaders.Interop.Buffers; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; @@ -31,10 +30,7 @@ public static unsafe class D2D1PixelShaderEffect public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { - if (d2D1Factory1 is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Factory1); effectId = default; @@ -475,15 +471,8 @@ public static unsafe class D2D1PixelShaderEffect public static void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { - if (d2D1DeviceContext is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1DeviceContext), "The input ID2D1DeviceContext object cannot be null."); - } - - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The pointer to the target ID2D1Effect result cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1DeviceContext); + default(ArgumentNullException).ThrowIfNull(d2D1Effect); fixed (Guid* pGuid = &PixelShaderEffect.For.Instance.Id) { @@ -504,10 +493,7 @@ public static unsafe class D2D1PixelShaderEffect public static void SetConstantBufferForD2D1Effect(void* d2D1Effect, in T shader) where T : unmanaged, ID2D1PixelShader { - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1DeviceContext object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Effect); D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); @@ -524,15 +510,8 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, void* resourceTextureManager, int resourceTextureIndex) { - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); - } - - if (resourceTextureManager is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input ID2D1ResourceTextureManager object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Effect); + default(ArgumentNullException).ThrowIfNull(resourceTextureManager); ((ID2D1Effect*)d2D1Effect)->SetValue( index: D2D1PixelShaderEffectProperty.ResourceTextureManager0 + (uint)resourceTextureIndex, @@ -551,15 +530,8 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, D2D1ResourceTextureManager resourceTextureManager, int resourceTextureIndex) { - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); - } - - if (resourceTextureManager is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input D2D1ResourceTextureManager object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Effect); + default(ArgumentNullException).ThrowIfNull(resourceTextureManager); using ComPtr resourceTextureManager2 = default; @@ -581,15 +553,8 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . public static void SetTransformMapperForD2D1Effect(void* d2D1Effect, void* transformMapper) { - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); - } - - if (transformMapper is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(transformMapper), "The input ID2D1TransformMapper object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Effect); + default(ArgumentNullException).ThrowIfNull(transformMapper); ((ID2D1Effect*)d2D1Effect)->SetValue( index: D2D1PixelShaderEffectProperty.TransformMapper, @@ -608,15 +573,8 @@ public static unsafe class D2D1PixelShaderEffect public static void SetTransformMapperForD2D1Effect(void* d2D1Effect, D2D1TransformMapper transformMapper) where T : unmanaged, ID2D1PixelShader { - if (d2D1Effect is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); - } - - if (transformMapper is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(transformMapper), "The input D2D1TransformMapper object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(d2D1Effect); + default(ArgumentNullException).ThrowIfNull(transformMapper); using ComPtr transformMapper2 = default; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs index 3e0d1ff2..1e065117 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs @@ -1,5 +1,5 @@ +using System; using System.Numerics; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Shaders.Interop.Factories; namespace ComputeSharp.D2D1.Interop; @@ -41,10 +41,7 @@ public static class D2D1TransformMapperFactory /// The resulting instance for an inflate transform. public static D2D1TransformMapper Inflate(Accessor accessor) { - if (accessor is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(accessor); return new D2D1InflateTransformMapper.DynamicAmount { Accessor = accessor }; } @@ -56,10 +53,7 @@ public static class D2D1TransformMapperFactory /// The resulting instance for an inflate transform. public static D2D1TransformMapper Inflate(Accessor<(int Left, int Top, int Right, int Bottom)> accessor) { - if (accessor is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor<(int Left, int Top, int Right, int Bottom)> object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(accessor); return new D2D1InflateTransformMapper.DynamicLeftTopRightBottomAmount { Accessor = accessor }; } @@ -81,10 +75,7 @@ public static class D2D1TransformMapperFactory /// The resulting instance for an affine transform. public static D2D1TransformMapper Transform(Accessor accessor) { - if (accessor is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor object cannot be null."); - } + default(ArgumentNullException).ThrowIfNull(accessor); return new D2D1AffineTransformMapper.DynamicMatrix { Accessor = accessor }; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs index c7663a96..a87f958d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs @@ -95,7 +95,9 @@ internal abstract class D2D1AffineTransformMapper : D2D1TransformMapper.Transform. + // Additionally, we explicitly don't use a throw helper here to avoid loading the two string literals. + throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.", "matrix"); } Throw(); 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.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs index 7ec3bb05..f92c8808 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs @@ -3,7 +3,6 @@ using System; using System.IO; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using TerraFX.Interop.Windows; namespace ComputeSharp.Shaders.Translation; @@ -31,7 +30,7 @@ unsafe partial class ShaderCompiler { Architecture.X64 => "win-x64", Architecture.Arm64 => "win-arm64", - _ => ThrowHelper.ThrowNotSupportedException("Invalid process architecture.") + _ => default(NotSupportedException).Throw() }; // Test whether the native libraries are present in the same folder of the executable diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index 05db8cad..d35d92b4 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading; -using CommunityToolkit.Diagnostics; using ComputeSharp.Interop; #if WINDOWS_UWP using ComputeSharp.Uwp.Extensions; @@ -28,7 +27,7 @@ partial class SwapChainManager /// The instance to use to render frames. public async void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner) { - Guard.IsNotNull(shaderRunner); + default(ArgumentNullException).ThrowIfNull(shaderRunner); using (await this.setupSemaphore.LockAsync()) { diff --git a/src/ComputeSharp.UI/ShaderRunner{T}.cs b/src/ComputeSharp.UI/ShaderRunner{T}.cs index 08bca82b..8b355863 100644 --- a/src/ComputeSharp.UI/ShaderRunner{T}.cs +++ b/src/ComputeSharp.UI/ShaderRunner{T}.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; #if WINDOWS_UWP namespace ComputeSharp.Uwp; @@ -40,7 +39,7 @@ public sealed class ShaderRunner : IShaderRunner /// The instance used to create shaders to run. public ShaderRunner(Func shaderFactory) { - Guard.IsNotNull(shaderFactory); + default(ArgumentNullException).ThrowIfNull(shaderFactory); this.shaderFactory = shaderFactory; } @@ -51,7 +50,7 @@ public sealed class ShaderRunner : IShaderRunner /// The instance used to create shaders to run. public ShaderRunner(Func shaderFactory) { - Guard.IsNotNull(shaderFactory); + default(ArgumentNullException).ThrowIfNull(shaderFactory); this.statefulShaderFactory = shaderFactory; } diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 79918a4f..f10eabdd 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -18,10 +18,6 @@ - - - - diff --git a/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs index ec80ea6f..f614e7c9 100644 --- a/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs +++ b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs @@ -1,7 +1,4 @@ using System; -#if !SOURCE_GENERATOR -using CommunityToolkit.Diagnostics; -#endif namespace ComputeSharp; @@ -64,7 +61,7 @@ public sealed class EmbeddedBytecodeAttribute : Attribute DispatchAxis.XZ => (8, 1, 8), DispatchAxis.YZ => (1, 8, 8), DispatchAxis.XYZ => (4, 4, 4), - _ => ThrowHelper.ThrowArgumentException<(int, int, int)>(nameof(dispatchAxis), "Invalid dispatch axis value.") + _ => default(ArgumentException).Throw<(int, int, int)>(nameof(dispatchAxis)) }; #endif } diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index a4cb2df8..11a4e2cf 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -94,10 +93,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { lock (this.d3D12DescriptorHandlePairs) { - if (this.size <= 0) - { - ThrowHelper.ThrowInvalidOperationException("There are no descriptor handles left in the pool."); - } + default(InvalidOperationException).ThrowIf(this.size <= 0); d3D12ResourceDescriptorHandles = this.d3D12DescriptorHandlePairs[this.head++]; @@ -118,10 +114,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { lock (this.d3D12DescriptorHandlePairs) { - if (this.size >= DescriptorsPerHeap) - { - ThrowHelper.ThrowInvalidOperationException("The pool of descriptor heaps is already full."); - } + default(InvalidOperationException).ThrowIf(this.size >= DescriptorsPerHeap); this.d3D12DescriptorHandlePairs[this.tail++] = d3D12ResourceDescriptorHandles; diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs index 4d4c5863..25ab893d 100644 --- a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -1,6 +1,5 @@ using System; using System.Text; -using CommunityToolkit.Diagnostics; namespace ComputeSharp.Exceptions; @@ -36,7 +35,7 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException 1 => $"{nameof(GraphicsDevice.IsReadOnlyTexture1DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture1DSupportedForType)}().", 2 => $"{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}().", 3 => $"{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}().", - _ => ThrowHelper.ThrowArgumentException("Invalid texture rank.") + _ => default(ArgumentException).Throw(nameof(rank)) }); _ = builder.Append("As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."); diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index c6cd4389..4f40f97f 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; #if !NET6_0_OR_GREATER using GC = ComputeSharp.NetStandard.GC; @@ -22,7 +21,7 @@ public static class BufferExtensions public static T[] ToArray(this Buffer source) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); return source.ToArray(0, source.Length); } @@ -38,8 +37,8 @@ public static class BufferExtensions public static T[] ToArray(this Buffer source, int offset, int count) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsGreaterThanOrEqualTo(count, 0); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentOutOfRangeException).ThrowIfNegative(count); T[] data = GC.AllocateUninitializedArray(count); @@ -57,8 +56,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, T[] destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), 0); } @@ -75,8 +74,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); Span span = destination.AsSpan(destinationOffset, count); @@ -92,7 +91,7 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Span destination) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0); } @@ -107,7 +106,7 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Span destination, int sourceOffset) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(ref MemoryMarshal.GetReference(destination), sourceOffset, destination.Length); } @@ -121,8 +120,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Buffer destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, source.Length); } @@ -139,8 +138,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffset, destinationOffset, count); } @@ -154,8 +153,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, T[] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), 0); } @@ -172,8 +171,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlySpan span = source.AsSpan(sourceOffset, count); @@ -189,7 +188,7 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0); } @@ -204,7 +203,7 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, ReadOnlySpan source, int destinationOffset) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(ref MemoryMarshal.GetReference(source), destinationOffset, source.Length); } @@ -218,8 +217,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, Buffer source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Length); } @@ -236,8 +235,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffset, destinationOffset, count); } diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index 826d8f71..322b9166 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -1,5 +1,5 @@ +using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Interop; namespace ComputeSharp; @@ -19,7 +19,7 @@ public static partial class GraphicsDeviceExtensions public static void For(this GraphicsDevice device, int x, in T shader) where T : struct, IComputeShader { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using ComputeContext context = device.CreateComputeContext(); @@ -37,7 +37,7 @@ public static partial class GraphicsDeviceExtensions public static void For(this GraphicsDevice device, int x, int y, in T shader) where T : struct, IComputeShader { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using ComputeContext context = device.CreateComputeContext(); @@ -56,7 +56,7 @@ public static partial class GraphicsDeviceExtensions public static void For(this GraphicsDevice device, int x, int y, int z, in T shader) where T : struct, IComputeShader { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using ComputeContext context = device.CreateComputeContext(); @@ -78,7 +78,7 @@ public static partial class GraphicsDeviceExtensions public static void For(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader) where T : struct, IComputeShader { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using ComputeContext context = device.CreateComputeContext(); @@ -96,8 +96,8 @@ public static partial class GraphicsDeviceExtensions where T : struct, IPixelShader where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(texture); using ComputeContext context = device.CreateComputeContext(); @@ -116,8 +116,8 @@ public static partial class GraphicsDeviceExtensions where T : struct, IPixelShader where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(texture); using ComputeContext context = device.CreateComputeContext(); @@ -149,7 +149,7 @@ public static partial class GraphicsDeviceExtensions /// public static ComputeContext CreateComputeContext(this GraphicsDevice device) { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index 320d05dc..631cb35b 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; @@ -22,7 +21,7 @@ partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, length, allocationMode); } @@ -38,8 +37,8 @@ partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateConstantBuffer(source.AsSpan()); } @@ -55,7 +54,7 @@ partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ConstantBuffer buffer = new(device, source.Length, AllocationMode.Default); @@ -75,8 +74,8 @@ partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ConstantBuffer constantBuffer = new(device, source.Length, AllocationMode.Default); @@ -97,7 +96,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, length, allocationMode); } @@ -113,8 +112,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyBuffer(source.AsSpan()); } @@ -130,7 +129,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyBuffer buffer = new(device, source.Length, AllocationMode.Default); @@ -150,8 +149,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyBuffer readWriteBuffer = new(device, source.Length, AllocationMode.Default); @@ -171,7 +170,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture1D AllocateReadOnlyTexture1D(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -187,8 +186,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture1D AllocateReadOnlyTexture1D(this GraphicsDevice device, T[] source, int width) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture1D(source.AsSpan(0, width)); } @@ -205,8 +204,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture1D AllocateReadOnlyTexture1D(this GraphicsDevice device, T[] source, int offset, int width) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture1D(source.AsSpan(offset, width)); } @@ -221,8 +220,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture1D AllocateReadOnlyTexture1D(this GraphicsDevice device, T[] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -241,7 +240,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture1D AllocateReadOnlyTexture1D(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -263,7 +262,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -281,8 +280,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture1D(source.AsSpan(0, width)); } @@ -301,8 +300,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture1D(source.AsSpan(offset, width)); } @@ -319,8 +318,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -341,7 +340,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -362,7 +361,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -379,8 +378,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture2D(source.AsSpan(), width, height); } @@ -398,8 +397,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture2D(source.AsSpan(offset), width, height); } @@ -414,8 +413,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -436,7 +435,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -459,7 +458,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -478,8 +477,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture2D(source.AsSpan(), width, height); } @@ -499,8 +498,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture2D(source.AsSpan(offset), width, height); } @@ -517,8 +516,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -541,7 +540,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -563,7 +562,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -581,8 +580,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture3D(source.AsSpan(), width, height, depth); } @@ -601,8 +600,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture3D(source.AsSpan(offset), width, height, depth); } @@ -621,8 +620,8 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -644,7 +643,7 @@ partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -668,7 +667,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -688,8 +687,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture3D(source.AsSpan(), width, height, depth); } @@ -710,8 +709,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadOnlyTexture3D(source.AsSpan(offset), width, height, depth); } @@ -732,8 +731,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadOnlyTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -757,7 +756,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -778,7 +777,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, length, allocationMode); } @@ -794,8 +793,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteBuffer(source.AsSpan()); } @@ -811,7 +810,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteBuffer buffer = new(device, source.Length, AllocationMode.Default); @@ -831,8 +830,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteBuffer readWriteBuffer = new(device, source.Length, AllocationMode.Default); @@ -852,7 +851,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture1D AllocateReadWriteTexture1D(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -868,8 +867,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture1D AllocateReadWriteTexture1D(this GraphicsDevice device, T[] source, int width) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture1D(source.AsSpan(0, width)); } @@ -886,8 +885,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture1D AllocateReadWriteTexture1D(this GraphicsDevice device, T[] source, int offset, int width) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture1D(source.AsSpan(offset, width)); } @@ -902,8 +901,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture1D AllocateReadWriteTexture1D(this GraphicsDevice device, T[] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -922,7 +921,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture1D AllocateReadWriteTexture1D(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -944,7 +943,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -962,8 +961,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture1D(source.AsSpan(0, width)); } @@ -982,8 +981,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture1D(source.AsSpan(offset, width)); } @@ -1000,8 +999,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -1022,7 +1021,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture1D texture = new(device, source.Length, AllocationMode.Default); @@ -1043,7 +1042,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -1060,8 +1059,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture2D(source.AsSpan(), width, height); } @@ -1079,8 +1078,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture2D(source.AsSpan(offset), width, height); } @@ -1095,8 +1094,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -1117,7 +1116,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -1140,7 +1139,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -1159,8 +1158,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture2D(source.AsSpan(), width, height); } @@ -1180,8 +1179,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture2D(source.AsSpan(offset), width, height); } @@ -1198,8 +1197,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -1222,7 +1221,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -1244,7 +1243,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -1262,8 +1261,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture3D(source.AsSpan(), width, height, depth); } @@ -1282,8 +1281,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture3D(source.AsSpan(offset), width, height, depth); } @@ -1302,8 +1301,8 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] source) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -1325,7 +1324,7 @@ partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -1349,7 +1348,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -1369,8 +1368,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture3D(source.AsSpan(), width, height, depth); } @@ -1391,8 +1390,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); return device.AllocateReadWriteTexture3D(source.AsSpan(offset), width, height, depth); } @@ -1413,8 +1412,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(source); ReadWriteTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -1438,7 +1437,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -1459,7 +1458,7 @@ partial class GraphicsDeviceExtensions public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, length, allocationMode); } @@ -1475,7 +1474,7 @@ partial class GraphicsDeviceExtensions public static UploadTexture1D AllocateUploadTexture1D(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -1492,7 +1491,7 @@ partial class GraphicsDeviceExtensions public static UploadTexture2D AllocateUploadTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -1510,7 +1509,7 @@ partial class GraphicsDeviceExtensions public static UploadTexture3D AllocateUploadTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -1527,7 +1526,7 @@ partial class GraphicsDeviceExtensions public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, length, allocationMode); } @@ -1543,7 +1542,7 @@ partial class GraphicsDeviceExtensions public static ReadBackTexture1D AllocateReadBackTexture1D(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, allocationMode); } @@ -1560,7 +1559,7 @@ partial class GraphicsDeviceExtensions public static ReadBackTexture2D AllocateReadBackTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, allocationMode); } @@ -1578,7 +1577,7 @@ partial class GraphicsDeviceExtensions public static ReadBackTexture3D AllocateReadBackTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return new(device, width, height, depth, allocationMode); } @@ -1595,8 +1594,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(filename); return device.LoadReadOnlyTexture2D(filename.AsSpan()); } @@ -1613,7 +1612,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); @@ -1636,7 +1635,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); @@ -1659,8 +1658,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(stream); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, stream); @@ -1683,8 +1682,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(filename); return device.LoadReadWriteTexture2D(filename.AsSpan()); } @@ -1701,7 +1700,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); @@ -1724,7 +1723,7 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); @@ -1747,8 +1746,8 @@ partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(stream); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, stream); @@ -1769,8 +1768,8 @@ partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, string filename) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(filename); return device.LoadUploadTexture2D(filename.AsSpan()); } @@ -1785,7 +1784,7 @@ partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, ReadOnlySpan filename) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return WICHelper.Instance.LoadTexture(device, filename); } @@ -1800,7 +1799,7 @@ partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, ReadOnlySpan span) where T : unmanaged { - Guard.IsNotNull(device); + default(ArgumentNullException).ThrowIfNull(device); return WICHelper.Instance.LoadTexture(device, span); } @@ -1815,8 +1814,8 @@ partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, Stream stream) where T : unmanaged { - Guard.IsNotNull(device); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(device); + default(ArgumentNullException).ThrowIfNull(stream); return WICHelper.Instance.LoadTexture(device, stream); } diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index cc66f091..973b978d 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -1,6 +1,6 @@ #if NET6_0_OR_GREATER -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -66,7 +66,7 @@ internal static unsafe class AllocatorExtensions ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST), ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), - _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr allocation = default; @@ -117,7 +117,7 @@ internal static unsafe class AllocatorExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr allocation = default; @@ -170,7 +170,7 @@ internal static unsafe class AllocatorExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr allocation = default; @@ -225,7 +225,7 @@ internal static unsafe class AllocatorExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr allocation = default; diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs index 77710d00..9dce4803 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs @@ -1,7 +1,6 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -38,10 +37,7 @@ internal static class HRESULTExtensions // single branch can then be inlined without bloating the code size in the caller. if (!Configuration.IsDebugOutputEnabled) { - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } + default(Win32Exception).ThrowIfFailed(result); } else { @@ -51,15 +47,11 @@ internal static class HRESULTExtensions { bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } + // First, check whether the current call has failed, and report that + default(Win32Exception).ThrowIfFailed(result); - if (hasErrorsOrWarnings) - { - ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue."); - } + // If that's not the case, then throw if any errors are present in the info queue + default(Win32Exception).ThrowIf(hasErrorsOrWarnings, E.E_FAIL); } AssertWithDebugInfo(result); diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 08c1e529..19f04410 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,8 +1,6 @@ using System; #if NET6_0_OR_GREATER using System.Runtime.CompilerServices; -#else -using CommunityToolkit.Diagnostics; #endif using ComputeSharp.Core.Extensions; #if !NET6_0_OR_GREATER @@ -82,7 +80,7 @@ internal static unsafe class ID3D12DeviceExtensions ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST), ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), - _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr d3D12Resource = default; @@ -142,7 +140,7 @@ internal static unsafe class ID3D12DeviceExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr d3D12Resource = default; @@ -204,7 +202,7 @@ internal static unsafe class ID3D12DeviceExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr d3D12Resource = default; @@ -268,7 +266,7 @@ internal static unsafe class ID3D12DeviceExtensions { ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), - _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + _ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType)) }; using ComPtr d3D12Resource = default; diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs index 8e3a90a5..54c34722 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs @@ -4,7 +4,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; #if NET6_0_OR_GREATER -using CommunityToolkit.Diagnostics; using TerraFX.Interop; #endif using TerraFX.Interop.Windows; @@ -49,7 +48,7 @@ unsafe partial class IWICStreamExtensions { #if NET6_0_OR_GREATER /// - static Guid* INativeGuid.NativeGuid => (Guid*)ThrowHelper.ThrowNotSupportedException(); + static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); #else /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs index 57b348da..c471afd5 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs @@ -3,7 +3,6 @@ using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; -using CommunityToolkit.Diagnostics; #if NET6_0_OR_GREATER using TerraFX.Interop; #endif @@ -51,7 +50,7 @@ internal static unsafe partial class IWICStreamExtensions { #if NET6_0_OR_GREATER /// - static Guid* INativeGuid.NativeGuid => (Guid*)ThrowHelper.ThrowNotSupportedException(); + static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); #else /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] @@ -571,7 +570,7 @@ internal static unsafe partial class IWICStreamExtensions } else { - ThrowHelper.ThrowNotSupportedException(); + default(NotSupportedException).Throw(); } pstatstg->type = (uint)STGTY_STREAM; diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs index b35105da..2d9132de 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class ReadBackBufferExtensions public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Length); } @@ -35,8 +35,8 @@ public static class ReadBackBufferExtensions public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffset, destinationOffset, count); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture1DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture1DExtensions.cs index 7720a3fd..dbb09aef 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture1DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture1DExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class ReadBackTexture1DExtensions public static void CopyFrom(this ReadBackTexture1D destination, Texture1D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Width); } @@ -34,8 +34,8 @@ public static class ReadBackTexture1DExtensions public static void CopyFrom(this ReadBackTexture1D destination, Texture1D source, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, 0, width); } @@ -52,8 +52,8 @@ public static class ReadBackTexture1DExtensions public static void CopyFrom(this ReadBackTexture1D destination, Texture1D source, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs index fd196cca..0ef1e871 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Buffers; using System.IO; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; @@ -21,8 +20,8 @@ public static class ReadBackTexture2DExtensions public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -40,8 +39,8 @@ public static class ReadBackTexture2DExtensions public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -61,8 +60,8 @@ public static class ReadBackTexture2DExtensions public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -76,8 +75,8 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, string filename) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(filename); WICHelper.Instance.SaveTexture(texture.View, filename.AsSpan()); } @@ -91,7 +90,7 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, ReadOnlySpan filename) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); WICHelper.Instance.SaveTexture(texture.View, filename); } @@ -106,8 +105,8 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, Stream stream, ImageFormat format) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(stream); WICHelper.Instance.SaveTexture(texture.View, stream, format); } @@ -122,8 +121,8 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, IBufferWriter writer, ImageFormat format) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(writer); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(writer); WICHelper.Instance.SaveTexture(texture.View, writer, format); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs index ab07892a..9c10faf0 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class ReadBackTexture3DExtensions public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -38,8 +38,8 @@ public static class ReadBackTexture3DExtensions public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -62,8 +62,8 @@ public static class ReadBackTexture3DExtensions public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture1DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture1DExtensions.cs index 565cdb04..bd75f279 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture1DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture1DExtensions.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; namespace ComputeSharp; @@ -29,7 +28,7 @@ public static class ReadWriteTexture1DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture1D AsReadOnly(this ReadWriteTexture1D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -55,7 +54,7 @@ public static class ReadWriteTexture1DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture1D AsReadOnly(this ReadWriteTexture1D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -81,7 +80,7 @@ public static class ReadWriteTexture1DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture1D AsReadOnly(this ReadWriteTexture1D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -107,7 +106,7 @@ public static class ReadWriteTexture1DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture1D AsReadOnly(this ReadWriteTexture1D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -137,7 +136,7 @@ public static class ReadWriteTexture1DExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs index efa1f2da..163e88d3 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; namespace ComputeSharp; @@ -29,7 +28,7 @@ public static class ReadWriteTexture2DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -55,7 +54,7 @@ public static class ReadWriteTexture2DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -81,7 +80,7 @@ public static class ReadWriteTexture2DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -107,7 +106,7 @@ public static class ReadWriteTexture2DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -137,7 +136,7 @@ public static class ReadWriteTexture2DExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs index bc0621ab..ca96756c 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; namespace ComputeSharp; @@ -29,7 +28,7 @@ public static class ReadWriteTexture3DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -55,7 +54,7 @@ public static class ReadWriteTexture3DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -81,7 +80,7 @@ public static class ReadWriteTexture3DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -107,7 +106,7 @@ public static class ReadWriteTexture3DExtensions /// Thrown if the current instance is not in a readonly state. public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } @@ -137,7 +136,7 @@ public static class ReadWriteTexture3DExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); return texture.AsReadOnly(); } diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index 087201a9..7a466caf 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class StructuredBufferExtensions public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, source.Length); } @@ -35,8 +35,8 @@ public static class StructuredBufferExtensions public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffset, destinationOffset, count); } @@ -50,8 +50,8 @@ public static class StructuredBufferExtensions public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Length); } @@ -68,8 +68,8 @@ public static class StructuredBufferExtensions public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffset, destinationOffset, count); } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs index 40e37268..5b054655 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture1DExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Resources; namespace ComputeSharp; @@ -19,7 +18,7 @@ public static class Texture1DExtensions public static T[] ToArray(this Texture1D source) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); T[] data = new T[source.Width]; @@ -39,8 +38,8 @@ public static class Texture1DExtensions public static unsafe T[] ToArray(this Texture1D source, int x, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); T[] data = new T[width]; @@ -61,8 +60,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), 0, source.Width); } @@ -78,8 +77,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination, Range x) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), x); } @@ -96,8 +95,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), sourceOffsetX, width); } @@ -112,8 +111,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination, int destinationOffset) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), 0, source.Width); } @@ -130,8 +129,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination, int destinationOffset, Range x) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), x); } @@ -149,8 +148,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, T[] destination, int destinationOffset, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, width); } @@ -165,7 +164,7 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Span destination) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, source.Width); } @@ -181,7 +180,7 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Span destination, Range x) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); (int offsetX, int width) = x.GetOffsetAndLength(source.Width); @@ -200,7 +199,7 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Span destination, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, width); } @@ -214,8 +213,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Texture1D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, source.Width); } @@ -231,8 +230,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Texture1D destination, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, 0, width); } @@ -249,8 +248,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, Texture1D destination, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width); } @@ -264,8 +263,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, ReadBackTexture1D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, source.Width); } @@ -281,8 +280,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, ReadBackTexture1D destination, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, 0, width); } @@ -299,8 +298,8 @@ public static class Texture1DExtensions public static void CopyTo(this Texture1D source, ReadBackTexture1D destination, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width); } @@ -314,8 +313,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, T[] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), 0, destination.Width); } @@ -331,8 +330,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, T[] source, Range x) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), x); } @@ -349,8 +348,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, T[] source, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), destinationOffsetX, width); } @@ -367,8 +366,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, T[] source, int sourceOffset, Range x) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), x); } @@ -386,8 +385,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, T[] source, int sourceOffset, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, width); } @@ -402,7 +401,7 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, destination.Width); } @@ -418,7 +417,7 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, ReadOnlySpan source, Range x) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); (int offsetX, int width) = x.GetOffsetAndLength(destination.Width); @@ -437,7 +436,7 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, ReadOnlySpan source, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, width); } @@ -451,8 +450,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, Texture1D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, 0, 0, source.Width); } @@ -468,8 +467,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, Texture1D source, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, 0, width); } @@ -486,8 +485,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, Texture1D source, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width); } @@ -501,8 +500,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, UploadTexture1D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Width); } @@ -518,8 +517,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, UploadTexture1D source, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, 0, width); } @@ -536,8 +535,8 @@ public static class Texture1DExtensions public static void CopyFrom(this Texture1D destination, UploadTexture1D source, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, width); } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index f3015fa5..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; @@ -22,7 +21,7 @@ public static class Texture2DExtensions public static T[,] ToArray(this Texture2D source) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); T[,] data = new T[source.Height, source.Width]; @@ -44,9 +43,9 @@ public static class Texture2DExtensions public static unsafe T[,] ToArray(this Texture2D source, int x, int y, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); - Guard.IsGreaterThanOrEqualTo(height, 0); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); + default(ArgumentOutOfRangeException).ThrowIfNegative(height); T[,] data = new T[height, width]; @@ -67,10 +66,10 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[,] destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); - Guard.IsEqualTo(destination.GetLength(0), source.Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), source.Width, nameof(destination)); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(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); } @@ -84,8 +83,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), 0, 0, source.Width, source.Height); } @@ -102,8 +101,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), x, y); } @@ -122,8 +121,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(), sourceOffsetX, sourceOffsetY, width, height); } @@ -138,8 +137,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, source.Width, source.Height); } @@ -157,8 +156,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), x, y); } @@ -178,8 +177,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, width, height); } @@ -194,7 +193,7 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Span destination) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, source.Width, source.Height); } @@ -211,7 +210,7 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Span destination, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); (int offsetX, int width) = x.GetOffsetAndLength(source.Width); (int offsetY, int height) = y.GetOffsetAndLength(source.Height); @@ -233,7 +232,7 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Span destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, width, height); } @@ -247,8 +246,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Texture2D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -266,8 +265,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -287,8 +286,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -302,8 +301,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, ReadBackTexture2D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -321,8 +320,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -342,8 +341,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -357,10 +356,10 @@ public static class Texture2DExtensions public static unsafe void CopyFrom(this Texture2D destination, T[,] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); - Guard.IsEqualTo(source.GetLength(0), destination.Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), destination.Width, nameof(source)); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(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); } @@ -374,8 +373,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), 0, 0, destination.Width, destination.Height); } @@ -392,8 +391,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), x, y); } @@ -412,8 +411,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, width, height); } @@ -431,8 +430,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source, int sourceOffset, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), x, y); } @@ -452,8 +451,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source, int sourceOffset, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, width, height); } @@ -468,7 +467,7 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, destination.Width, destination.Height); } @@ -485,7 +484,7 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, ReadOnlySpan source, Range x, Range y) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); (int offsetX, int width) = x.GetOffsetAndLength(destination.Width); (int offsetY, int height) = y.GetOffsetAndLength(destination.Height); @@ -507,7 +506,7 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, ReadOnlySpan source, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, width, height); } @@ -521,8 +520,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, Texture2D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); } @@ -540,8 +539,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -561,8 +560,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -576,8 +575,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, UploadTexture2D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -595,8 +594,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -616,8 +615,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -631,8 +630,8 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, string filename) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(filename); texture.Save(filename.AsSpan()); } @@ -646,7 +645,7 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, ReadOnlySpan filename) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); @@ -665,8 +664,8 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, Stream stream, ImageFormat format) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(stream); using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); @@ -685,8 +684,8 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, IBufferWriter writer, ImageFormat format) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(writer); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(writer); using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index ade73640..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; @@ -24,7 +23,7 @@ public static class Texture3DExtensions public static T[,,] ToArray(this Texture3D source) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); T[,,] data = new T[source.Depth, source.Height, source.Width]; @@ -48,10 +47,10 @@ public static class Texture3DExtensions public static unsafe T[,,] ToArray(this Texture3D source, int x, int y, int z, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsGreaterThanOrEqualTo(width, 0); - Guard.IsGreaterThanOrEqualTo(height, 0); - Guard.IsGreaterThanOrEqualTo(depth, 0); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentOutOfRangeException).ThrowIfNegative(width); + default(ArgumentOutOfRangeException).ThrowIfNegative(height); + default(ArgumentOutOfRangeException).ThrowIfNegative(depth); T[,,] data = new T[depth, height, width]; @@ -76,11 +75,11 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[,,] destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(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(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(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); } @@ -95,8 +94,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[] destination, int destinationOffset) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -115,8 +114,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[] destination, int destinationOffset, Range x, Range y, Range z) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), x, y, z); } @@ -138,8 +137,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[] destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffset, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } @@ -153,7 +152,7 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Span destination) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -171,7 +170,7 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Span destination, Range x, Range y, Range z) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); (int offsetX, int width) = x.GetOffsetAndLength(source.Width); (int offsetY, int height) = y.GetOffsetAndLength(source.Height); @@ -196,7 +195,7 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Span destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } @@ -210,8 +209,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Texture3D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -231,8 +230,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -255,8 +254,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -270,8 +269,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, ReadBackTexture3D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -291,8 +290,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -315,8 +314,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -334,11 +333,11 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[,,] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(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(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(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); } @@ -352,8 +351,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), 0, 0, 0, destination.Width, destination.Height, destination.Depth); } @@ -371,8 +370,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source, Range x, Range y, Range z) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), x, y, z); } @@ -393,8 +392,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -413,8 +412,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source, int sourceOffset, Range x, Range y, Range z) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), x, y, z); } @@ -436,8 +435,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source, int sourceOffset, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -452,7 +451,7 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, ReadOnlySpan source) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, 0, destination.Width, destination.Height, destination.Depth); } @@ -470,7 +469,7 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, ReadOnlySpan source, Range x, Range y, Range z) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); (int offsetX, int width) = x.GetOffsetAndLength(destination.Width); (int offsetY, int height) = y.GetOffsetAndLength(destination.Height); @@ -495,7 +494,7 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, ReadOnlySpan source, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -509,8 +508,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, Texture3D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -530,8 +529,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -554,8 +553,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -569,8 +568,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, UploadTexture3D source) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -590,8 +589,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -614,8 +613,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(destination); - Guard.IsNotNull(source); + default(ArgumentNullException).ThrowIfNull(destination); + default(ArgumentNullException).ThrowIfNull(source); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs index 35de31cc..f1fccb95 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class UploadBufferExtensions public static void CopyTo(this UploadBuffer source, StructuredBuffer destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, source.Length); } @@ -35,8 +35,8 @@ public static class UploadBufferExtensions public static void CopyTo(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffset, destinationOffset, count); } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture1DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture1DExtensions.cs index ef1d9844..9a0104b8 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture1DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture1DExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class UploadTexture1DExtensions public static void CopyTo(this UploadTexture1D source, Texture1D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, source.Width); } @@ -34,8 +34,8 @@ public static class UploadTexture1DExtensions public static void CopyTo(this UploadTexture1D source, Texture1D destination, int sourceOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, 0, width); } @@ -52,8 +52,8 @@ public static class UploadTexture1DExtensions public static void CopyTo(this UploadTexture1D source, Texture1D destination, int sourceOffsetX, int destinationOffsetX, int width) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, width); } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs index ac2a4273..7e8531ed 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; @@ -20,8 +19,8 @@ public static class UploadTexture2DExtensions public static void CopyTo(this UploadTexture2D source, Texture2D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); } @@ -39,8 +38,8 @@ public static class UploadTexture2DExtensions public static void CopyTo(this UploadTexture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -60,8 +59,8 @@ public static class UploadTexture2DExtensions public static void CopyTo(this UploadTexture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -75,8 +74,8 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, string filename) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(filename); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(filename); texture.Load(filename.AsSpan()); } @@ -90,7 +89,7 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, ReadOnlySpan filename) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); WICHelper.Instance.LoadTexture(texture.View, filename); } @@ -105,7 +104,7 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, ReadOnlySpan span) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); WICHelper.Instance.LoadTexture(texture.View, span); } @@ -119,8 +118,8 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, Stream stream) where T : unmanaged { - Guard.IsNotNull(texture); - Guard.IsNotNull(stream); + default(ArgumentNullException).ThrowIfNull(texture); + default(ArgumentNullException).ThrowIfNull(stream); WICHelper.Instance.LoadTexture(texture.View, stream); } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs index 6590c16e..fcc41a6b 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Diagnostics; +using System; using ComputeSharp.Resources; namespace ComputeSharp; @@ -17,8 +17,8 @@ public static class UploadTexture3DExtensions public static void CopyTo(this UploadTexture3D source, Texture3D destination) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -38,8 +38,8 @@ public static class UploadTexture3DExtensions public static void CopyTo(this UploadTexture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -62,8 +62,8 @@ public static class UploadTexture3DExtensions public static void CopyTo(this UploadTexture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - Guard.IsNotNull(source); - Guard.IsNotNull(destination); + default(ArgumentNullException).ThrowIfNull(source); + default(ArgumentNullException).ThrowIfNull(destination); destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs index 7d12c742..15f9f470 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs @@ -35,7 +35,7 @@ unsafe partial class GraphicsDevice // For the same reason, this method doesn't have to worry about raising the event either. if (Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)) != S.S_OK) { - static void Throw(object self) => throw new InvalidOperationException($"The device \"{self}\" has been lost and can no longer be used."); + static void Throw(object self) => throw new InvalidOperationException($"""The device "{self}" has been lost and can no longer be used."""); Throw(this); } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs index ea8e6097..897a7ce3 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Concurrent; +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Sources; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; @@ -59,7 +59,7 @@ unsafe partial class GraphicsDevice d3D12Fence = this.d3D12CopyFence; d3D12FenceValue = ref this.nextD3D12CopyFenceValue; break; - default: ThrowHelper.ThrowArgumentException(); return; + default: default(ArgumentException).Throw(nameof(commandList)); return; } // Execute the command list @@ -215,7 +215,7 @@ unsafe partial class GraphicsDevice NativeMemory.Free(callbackContext); - ThrowHelper.ThrowWin32Exception("Failed to register the compute context completion callback."); + default(Win32Exception).Throw(E.E_FAIL); } return waitForFenceValueTaskSource; diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs index a4fe798e..5d9a5c6b 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; namespace ComputeSharp; @@ -71,7 +70,7 @@ partial class GraphicsDevice /// public static IEnumerable QueryDevices(Predicate predicate) { - Guard.IsNotNull(predicate); + default(ArgumentNullException).ThrowIfNull(predicate); return new DeviceHelper.DeviceQuery(predicate); } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 7c2af3eb..08d5cbf1 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Commands.Interop; #if NET6_0_OR_GREATER using ComputeSharp.Core.Extensions; @@ -440,7 +439,7 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject this.copyCommandListPool.Rent(this.d3D12Device.Get(), null, out d3D12CommandList, out d3D12CommandAllocator); break; default: - _ = ThrowHelper.ThrowArgumentException>(); + default(ArgumentException).Throw(nameof(d3D12CommandListType)); d3D12CommandList = null; d3D12CommandAllocator = null; break; diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index 38be3c55..d0bb296d 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -1,6 +1,6 @@ +using System; using System.Numerics; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.DXGI_FORMAT; @@ -46,7 +46,7 @@ internal static class DXGIFormatHelper if (typeof(T) == typeof(Rg16)) return DXGI_FORMAT_R8G8_UNORM; if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM; - return ThrowHelper.ThrowArgumentException("Invalid texture type."); + return default(ArgumentException).Throw(nameof(T)); } /// @@ -89,7 +89,7 @@ internal static class DXGIFormatHelper return true; } - return ThrowHelper.ThrowArgumentException("Invalid texture type."); + return default(ArgumentException).Throw(nameof(T)); } /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index d9e1c2f1..05eaf2e9 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -206,7 +206,7 @@ partial class DeviceHelper /// public void Reset() { - throw new NotSupportedException(); + default(NotSupportedException).Throw(); } /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs index 2ede7bee..6d51f371 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -72,17 +72,26 @@ partial class DeviceHelper // If the device removal reason is available, also include that in the output message if (result != S.S_OK) { - string deviceRemovalReason = (int)result switch + string? deviceRemovalReason = (int)result switch { DXGI.DXGI_ERROR_DEVICE_HUNG => nameof(DXGI.DXGI_ERROR_DEVICE_HUNG), DXGI.DXGI_ERROR_DEVICE_REMOVED => nameof(DXGI.DXGI_ERROR_DEVICE_REMOVED), DXGI.DXGI_ERROR_DEVICE_RESET => nameof(DXGI.DXGI_ERROR_DEVICE_RESET), DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR => nameof(DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR), DXGI.DXGI_ERROR_INVALID_CALL => nameof(DXGI.DXGI_ERROR_INVALID_CALL), - _ => ThrowHelper.ThrowArgumentOutOfRangeException("Invalid GetDeviceRemovedReason HRESULT.") + _ => null }; - _ = builder.AppendLine($"[Reason]: {deviceRemovalReason}"); + // If it is a known HRESULT (this should always be the case), print the nicely formatted value + if (deviceRemovalReason is not null) + { + _ = builder.AppendLine($"[Reason]: {deviceRemovalReason}"); + } + else + { + // Otherwise just format the HRESULT as a hexadecimal number + _ = builder.AppendLine($"[Reason]: 0x{(int)result:X8}"); + } } } @@ -98,20 +107,14 @@ partial class DeviceHelper riid: Windows.__uuidof(), ppvObject: d3D12DeviceRemovedExtendedData.GetVoidAddressOf()); - if (hresult != S.S_OK) - { - ThrowHelper.ThrowWin32Exception(hresult); - } + default(Win32Exception).ThrowIfFailed(hresult); D3D12_DRED_AUTO_BREADCRUMBS_OUTPUT1 d3D12DredAutoBreadcrumbsOutput; // Get the auto breadcrumbs for the current event hresult = d3D12DeviceRemovedExtendedData.Get()->GetAutoBreadcrumbsOutput1(&d3D12DredAutoBreadcrumbsOutput); - if (hresult != S.S_OK) - { - ThrowHelper.ThrowWin32Exception(hresult); - } + default(Win32Exception).ThrowIfFailed(hresult); _ = builder.AppendLine("[DRED breadcrumbs START] ==============================="); diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 3debaf70..5e008830 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.DirectX; @@ -45,7 +44,7 @@ internal static partial class DeviceHelper return GetOrCreateDevice(d3D12Device.Get(), dxgiAdapter.Get(), &dxgiDescription1); } - return ThrowHelper.ThrowNotSupportedException("Failed to retrieve the default device."); + return default(NotSupportedException).Throw(); } /// @@ -105,10 +104,7 @@ internal static partial class DeviceHelper d3D12DeviceCandidate.GetVoidAddressOf()); // Check and throw if the device is a device lost state and not disposed properly - if (createDeviceResult.IsDeviceLostReason()) - { - ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); - } + default(InvalidOperationException).ThrowIf(createDeviceResult.IsDeviceLostReason()); // Check for success, and then also filter out devices that support SM6.0. This can // only be checked on a concrete ID3D12Device instance, so it can't be done earlier. @@ -151,10 +147,7 @@ internal static partial class DeviceHelper Windows.__uuidof(), d3D12DeviceCandidate.GetVoidAddressOf()); - if (createDeviceResult.IsDeviceLostReason()) - { - ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); - } + default(InvalidOperationException).ThrowIf(createDeviceResult.IsDeviceLostReason()); // There is no need to check for SM6.0, as it's guaranteed to be supported by Windows if (Windows.SUCCEEDED(createDeviceResult)) diff --git a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs index fcc39ee1..ec13519f 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using TerraFX.Interop.Windows; #if NET6_0_OR_GREATER using Path = System.IO.Path; @@ -33,7 +32,7 @@ internal static class WICFormatHelper if (typeof(T) == typeof(R8)) return GUID.GUID_WICPixelFormat8bppGray; if (typeof(T) == typeof(R16)) return GUID.GUID_WICPixelFormat16bppGray; - return ThrowHelper.ThrowArgumentException("Invalid texture type."); + return default(ArgumentException).Throw(nameof(T)); } /// @@ -53,7 +52,7 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = GUID.GUID_WICPixelFormat32bppBGRA; else if (typeof(T) == typeof(R8)) intermediateFormat = GUID.GUID_WICPixelFormat24bppBGR; else if (typeof(T) == typeof(R16)) intermediateFormat = GUID.GUID_WICPixelFormat24bppBGR; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else if (containerFormat == GUID.GUID_ContainerFormatPng) { @@ -62,7 +61,7 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default; else if (typeof(T) == typeof(R8)) intermediateFormat = default; else if (typeof(T) == typeof(R16)) intermediateFormat = GUID.GUID_WICPixelFormat8bppGray; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else if (containerFormat == GUID.GUID_ContainerFormatJpeg) { @@ -71,7 +70,7 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = GUID.GUID_WICPixelFormat24bppBGR; else if (typeof(T) == typeof(R8)) intermediateFormat = default; else if (typeof(T) == typeof(R16)) intermediateFormat = GUID.GUID_WICPixelFormat8bppGray; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else if (containerFormat == GUID.GUID_ContainerFormatWmp) { @@ -80,7 +79,7 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default; else if (typeof(T) == typeof(R8)) intermediateFormat = default; else if (typeof(T) == typeof(R16)) intermediateFormat = default; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else if (containerFormat == GUID.GUID_ContainerFormatTiff) { @@ -89,7 +88,7 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default; else if (typeof(T) == typeof(R8)) intermediateFormat = default; else if (typeof(T) == typeof(R16)) intermediateFormat = default; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else if (containerFormat == GUID.GUID_ContainerFormatDds) { @@ -98,11 +97,11 @@ internal static class WICFormatHelper else if (typeof(T) == typeof(Rgba64)) intermediateFormat = GUID.GUID_WICPixelFormat32bppBGRA; else if (typeof(T) == typeof(R8)) intermediateFormat = GUID.GUID_WICPixelFormat32bppBGRA; else if (typeof(T) == typeof(R16)) intermediateFormat = GUID.GUID_WICPixelFormat32bppBGRA; - else intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid texture type."); + else intermediateFormat = default(ArgumentException).Throw(nameof(T)); } else { - intermediateFormat = ThrowHelper.ThrowArgumentException("Invalid container format."); + intermediateFormat = default(ArgumentException).Throw(nameof(containerFormat)); } return intermediateFormat != default; @@ -128,7 +127,7 @@ internal static class WICFormatHelper ".wmp" => GUID.GUID_ContainerFormatWmp, ".tiff" => GUID.GUID_ContainerFormatTiff, ".dds" => GUID.GUID_ContainerFormatDds, - _ => ThrowHelper.ThrowArgumentException("Invalid filename."), + _ => default(ArgumentException).Throw(nameof(filename)) }; } @@ -148,7 +147,7 @@ internal static class WICFormatHelper ImageFormat.Wmp => GUID.GUID_ContainerFormatWmp, ImageFormat.Tiff => GUID.GUID_ContainerFormatTiff, ImageFormat.Dds => GUID.GUID_ContainerFormatDds, - _ => ThrowHelper.ThrowArgumentException("Invalid format."), + _ => default(ArgumentException).Throw(nameof(format)) }; } } \ No newline at end of file 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/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 1a090f20..5884f50e 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -1,7 +1,6 @@ using System; using System.ComponentModel; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Interop; using ComputeSharp.Resources; using TerraFX.Interop.DirectX; @@ -65,7 +64,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(buffer); + default(ArgumentNullException).ThrowIfNull(buffer); using ReferenceTracker.Lease _0 = buffer.GetReferenceTracker().GetLease(); @@ -87,7 +86,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture1D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -109,7 +108,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture1D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -131,7 +130,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture1D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -140,7 +139,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -154,7 +153,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture1D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -163,7 +162,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -177,7 +176,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture1D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -186,7 +185,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -200,7 +199,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -222,7 +221,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -244,7 +243,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -253,7 +252,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -267,7 +266,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture2D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -276,7 +275,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -290,7 +289,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture2D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -299,7 +298,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -313,7 +312,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -335,7 +334,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease _0 = texture.GetReferenceTracker().GetLease(); @@ -357,7 +356,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -366,7 +365,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -380,7 +379,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture3D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -389,7 +388,7 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } /// @@ -403,7 +402,7 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture3D texture, GraphicsDevice device) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); if (texture is IGraphicsResource resource) { @@ -412,6 +411,6 @@ public static class GraphicsResourceHelper return *(ulong*)&d3D12GpuDescriptorHandle; } - return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance."); + return default(ArgumentException).Throw(nameof(texture)); } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs index ae4c0587..15ed47be 100644 --- a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs +++ b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs @@ -68,7 +68,7 @@ internal struct ReferenceTracker : IDisposable if (!leaseTaken) { - // We can't use ThrowHelper here as we only want to invoke ToString if we + // We can't use a throw helper here as we only want to invoke ToString if we // are about to throw an exception. The JIT will recognize this pattern // as this method has a single basic block that always throws an exception. static void Throw(object trackedObject) => throw new ObjectDisposedException(trackedObject.ToString()); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 5fb1dcf8..8ffee4de 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -1,7 +1,7 @@ +using System; using System.Buffers; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands.Interop; @@ -75,12 +75,12 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph if (resourceType == ResourceType.Constant) { - Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); } else { // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue - Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255); } using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 00364fc6..9416d22a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -1,5 +1,5 @@ +using System; using System.Diagnostics.CodeAnalysis; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -35,9 +35,9 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count) { - Guard.IsBetweenOrEqualTo(count, 0, Length); - Guard.IsInRange(sourceOffset, 0, Length); - Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffset + count, Length, nameof(sourceOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -107,12 +107,12 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count) { - Guard.IsBetweenOrEqualTo(count, 0, Length); - Guard.IsBetweenOrEqualTo(count, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); - Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); - Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffset + count, Length, nameof(sourceOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffset + count, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -155,12 +155,12 @@ public abstract class StructuredBuffer : Buffer /// The number of items to read. internal unsafe void CopyTo(ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) { - Guard.IsBetweenOrEqualTo(count, 0, Length); - Guard.IsBetweenOrEqualTo(count, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); - Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); - Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffset + count, Length, nameof(sourceOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffset + count, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -199,9 +199,9 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); - Guard.IsInRange(offset, 0, Length); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -275,12 +275,12 @@ public abstract class StructuredBuffer : Buffer /// The number of items to read. internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int destinationOffset, int count) { - Guard.IsBetweenOrEqualTo(count, 0, Length); - Guard.IsBetweenOrEqualTo(count, 0, source.Length); - Guard.IsInRange(sourceOffset, 0, source.Length); - Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, Length); - Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, source.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, source.Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffset + count, source.Length, nameof(sourceOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffset + count, Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index bc359b54..85fb6708 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -78,7 +77,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); @@ -165,10 +164,10 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// The width of the memory area to copy. internal void CopyTo(ref T destination, int size, int sourceOffsetX, int width) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsGreaterThanOrEqualTo(size, width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, width); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -247,12 +246,12 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// The width of the memory area to copy. internal void CopyTo(Texture1D destination, int sourceOffsetX, int destinationOffsetX, int width) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -307,12 +306,12 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// The width of the memory area to copy. internal void CopyTo(ReadBackTexture1D destination, int sourceOffsetX, int destinationOffsetX, int width) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -363,10 +362,10 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// The width of the memory area to write to. internal void CopyFrom(ref T source, int size, int destinationOffsetX, int width) { - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsGreaterThanOrEqualTo(size, width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, width); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -445,12 +444,12 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// The width of the memory area to write to. internal void CopyFrom(UploadTexture1D source, int sourceOffsetX, int destinationOffsetX, int width) { - Guard.IsInRange(sourceOffsetX, 0, source.Width); - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, source.Width); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, source.Width); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 36eed0a5..668c7208 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -79,8 +78,8 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); - Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); @@ -177,13 +176,13 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// The height of the memory area to copy. internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int width, int height) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -267,18 +266,18 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// The height of the memory area to copy. internal void CopyTo(Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsInRange(destinationOffsetY, 0, destination.Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(height, 1, destination.Height); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -336,18 +335,18 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// The height of the memory area to copy. internal void CopyTo(ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsInRange(destinationOffsetY, 0, destination.Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(height, 1, destination.Height); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -400,13 +399,13 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// The height of the memory area to write to. internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int width, int height) { - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetY, 0, Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -490,18 +489,18 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// The height of the memory area to write to. internal void CopyFrom(UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { - Guard.IsInRange(sourceOffsetX, 0, source.Width); - Guard.IsInRange(sourceOffsetY, 0, source.Height); - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetY, 0, Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(width, 1, source.Width); - Guard.IsBetweenOrEqualTo(height, 1, source.Height); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, source.Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, source.Height); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7edc62d3..95bcf9f5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -80,9 +79,9 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); - Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); - Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); @@ -189,16 +188,16 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// The depth of the memory area to copy. internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsInRange(sourceOffsetZ, 0, Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetZ, 0, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height * depth); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -288,24 +287,24 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// The depth of the memory area to copy. internal void CopyTo(Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsInRange(sourceOffsetZ, 0, Depth); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsInRange(destinationOffsetY, 0, destination.Height); - Guard.IsInRange(destinationOffsetZ, 0, destination.Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(height, 1, destination.Height); - Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); - Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetZ, 0, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetZ, 0, destination.Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, destination.Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -366,24 +365,24 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// The depth of the memory area to copy. internal void CopyTo(ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { - Guard.IsInRange(sourceOffsetX, 0, Width); - Guard.IsInRange(sourceOffsetY, 0, Height); - Guard.IsInRange(sourceOffsetZ, 0, Depth); - Guard.IsInRange(destinationOffsetX, 0, destination.Width); - Guard.IsInRange(destinationOffsetY, 0, destination.Height); - Guard.IsInRange(destinationOffsetZ, 0, destination.Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(height, 1, destination.Height); - Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); - Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); - Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetZ, 0, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetZ, 0, destination.Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, destination.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, destination.Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -438,16 +437,16 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// The depth of the memory area to write to. internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetY, 0, Height); - Guard.IsInRange(destinationOffsetZ, 0, Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); - Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetZ, 0, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height * depth); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -537,24 +536,24 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// The depth of the memory area to write to. internal void CopyFrom(UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { - Guard.IsInRange(sourceOffsetX, 0, source.Width); - Guard.IsInRange(sourceOffsetY, 0, source.Height); - Guard.IsInRange(sourceOffsetZ, 0, source.Depth); - Guard.IsInRange(destinationOffsetX, 0, Width); - Guard.IsInRange(destinationOffsetY, 0, Height); - Guard.IsInRange(destinationOffsetZ, 0, Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); - Guard.IsBetweenOrEqualTo(width, 1, source.Width); - Guard.IsBetweenOrEqualTo(height, 1, source.Height); - Guard.IsBetweenOrEqualTo(depth, 1, source.Depth); - Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); - Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); - Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ)); - Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); - Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, source.Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetZ, 0, source.Depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetZ, 0, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, source.Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, source.Depth); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index db0e49e3..9e4cbffe 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -2,7 +2,6 @@ using System; using System.Buffers; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -58,7 +57,7 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject this.referenceTracker = new ReferenceTracker(this); // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue - Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); @@ -192,7 +191,7 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject /// public override MemoryHandle Pin(int elementIndex = 0) { - Guard.IsEqualTo(elementIndex, 0); + default(ArgumentOutOfRangeException).ThrowIfNotZero(elementIndex); using ReferenceTracker.Lease _0 = this.buffer.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs index 4a846452..0b985869 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs @@ -1,7 +1,6 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -60,7 +59,7 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); @@ -197,7 +196,7 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj /// public override MemoryHandle Pin(int elementIndex = 0) { - Guard.IsEqualTo(elementIndex, 0); + default(ArgumentOutOfRangeException).ThrowIfNotZero(elementIndex); using ReferenceTracker.Lease _0 = this.buffer.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 7dd4d2a3..2d315481 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -1,5 +1,5 @@ +using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -59,8 +59,8 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); - Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index cadca41b..474c434b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -1,5 +1,5 @@ +using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -60,9 +60,9 @@ public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObj { this.referenceTracker = new ReferenceTracker(this); - Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); - Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); - Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 20b0a09a..eab1d9e7 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -1,7 +1,7 @@ +using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; @@ -60,9 +60,9 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); - Guard.IsInRange(offset, 0, Length); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -87,12 +87,12 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); - Guard.IsBetweenOrEqualTo(length, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); - Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); - Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffset + length, Length, nameof(sourceOffset)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffset + length, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -125,9 +125,9 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); - Guard.IsInRange(offset, 0, Length); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs b/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs index 6d06aefc..42b8aa4d 100644 --- a/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs +++ b/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs @@ -1,5 +1,5 @@ +using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using TerraFX.Interop.DirectX; using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES; @@ -28,6 +28,6 @@ internal static class ResourceStateHelper return D3D12_RESOURCE_STATE_UNORDERED_ACCESS; } - return ThrowHelper.ThrowArgumentException("The target resource state is not valid."); + return default(ArgumentException).Throw(nameof(resourceState)); } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs index 200a6c08..dacdb730 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs @@ -1,7 +1,6 @@ using System; using System.ComponentModel; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; #pragma warning disable CS0809, CA1065 @@ -127,8 +126,8 @@ public readonly unsafe ref struct TextureView2D [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - Guard.IsInRange(x, 0, this.width); - Guard.IsInRange(y, 0, this.height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(x, 0, this.width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(y, 0, this.height); return ref *((T*)((byte*)this.pointer + (y * this.strideInBytes)) + x); } @@ -177,7 +176,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) { @@ -194,11 +193,8 @@ public readonly unsafe ref struct TextureView2D /// Thrown when doesn't match the size of the current instance. public void CopyTo(TextureView2D destination) { - if (destination.width != this.width || - destination.height != this.height) - { - ThrowHelper.ThrowArgumentException(); - } + default(ArgumentException).ThrowIf(destination.width != this.width, nameof(destination)); + default(ArgumentException).ThrowIf(destination.height != this.height, nameof(destination)); if (IsEmpty) { @@ -297,7 +293,7 @@ public readonly unsafe ref struct TextureView2D /// The resulting row . public Span GetRowSpan(int y) { - Guard.IsInRange(y, 0, this.height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(y, 0, this.height); return new((byte*)this.pointer + (y * this.strideInBytes), this.width); } diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs index e5883bc7..de271ed9 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs @@ -1,7 +1,6 @@ using System; using System.ComponentModel; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; #pragma warning disable CS0809, CA1065 @@ -152,9 +151,9 @@ public readonly unsafe ref struct TextureView3D [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - Guard.IsInRange(x, 0, this.width); - Guard.IsInRange(y, 0, this.height); - Guard.IsInRange(z, 0, this.depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(x, 0, this.width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(y, 0, this.height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(z, 0, this.depth); return ref *((T*)((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes)) + x); } @@ -224,12 +223,9 @@ public readonly unsafe ref struct TextureView3D /// Thrown when doesn't match the size of the current instance. public void CopyTo(TextureView3D destination) { - if (destination.width != this.width || - destination.height != this.height || - destination.depth != this.depth) - { - ThrowHelper.ThrowArgumentException(); - } + default(ArgumentException).ThrowIf(destination.width != this.width, nameof(destination)); + default(ArgumentException).ThrowIf(destination.height != this.height, nameof(destination)); + default(ArgumentException).ThrowIf(destination.depth != this.depth, nameof(destination)); if (IsEmpty) { @@ -336,8 +332,8 @@ public readonly unsafe ref struct TextureView3D /// The resulting row . public Span GetRowSpan(int y, int z) { - Guard.IsInRange(y, 0, this.height); - Guard.IsInRange(z, 0, this.depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(y, 0, this.height); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(z, 0, this.depth); return new((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes), this.width); } @@ -350,7 +346,7 @@ public readonly unsafe ref struct TextureView3D /// The resulting row . public TextureView2D GetDepthView(int z) { - Guard.IsInRange(z, 0, this.depth); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(z, 0, this.depth); return new( (T*)((byte*)this.pointer + (z * this.height * this.strideInBytes)), diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 2fb57469..b7be311f 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; @@ -60,7 +59,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { get { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); return this.device; } @@ -72,7 +71,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// The to insert the barrier for. internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(); @@ -92,7 +91,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, bool isNormalized) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -112,7 +111,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, Float4 value) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -179,23 +178,22 @@ public struct ComputeContext : IDisposable, IAsyncDisposable ref T shader) where T : struct, IComputeShader { - ThrowInvalidOperationExceptionIfDeviceIsNull(); - - Guard.IsGreaterThan(x, 0); - Guard.IsGreaterThan(y, 0); - Guard.IsGreaterThan(z, 0); - Guard.IsBetweenOrEqualTo(threadsX, 1, 1024); - Guard.IsBetweenOrEqualTo(threadsY, 1, 1024); - Guard.IsBetweenOrEqualTo(threadsZ, 1, 64); - Guard.IsLessThanOrEqualTo(threadsX * threadsY * threadsZ, 1024, "threadsXYZ"); + default(InvalidOperationException).ThrowIf(this.device is null); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(x); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); + default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsX, 1, 1024); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsY, 1, 1024); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsZ, 1, 64); + default(ArgumentOutOfRangeException).ThrowIfGreaterThan(threadsX * threadsY * threadsZ, 1024, "threadsXYZ"); int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1); int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1); int groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1); - Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); - Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); - Guard.IsBetweenOrEqualTo(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); @@ -221,7 +219,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable where T : struct, IPixelShader where TPixel : unmanaged { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); int x = texture.Width; int y = texture.Height; @@ -231,8 +229,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1); int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1); - Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); - Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); @@ -263,7 +261,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_RESOURCE_STATES d3D12ResourceStatesBefore, D3D12_RESOURCE_STATES d3D12ResourceStatesAfter) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -274,7 +272,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); GraphicsDevice device = this.device; @@ -301,7 +299,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask DisposeAsync() { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); GraphicsDevice device = this.device; @@ -342,10 +340,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable // original context would not see the changes done by the following queued dispatches. ref CommandList commandList = ref Unsafe.AsRef(in this.commandList); - if (!commandList.IsAllocated) - { - ThrowHelper.ThrowInvalidOperationException("The current compute context has not yet been initialized."); - } + default(InvalidOperationException).ThrowIf(!commandList.IsAllocated); return ref commandList; } @@ -377,18 +372,4 @@ public struct ComputeContext : IDisposable, IAsyncDisposable return ref commandList; } - - /// - /// Throws an if is . - /// - /// Thrown if is . - [MemberNotNull(nameof(device))] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private readonly void ThrowInvalidOperationExceptionIfDeviceIsNull() - { - if (this.device is null) - { - ThrowHelper.ThrowInvalidOperationException("The current compute context is not in a valid state."); - } - } } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs index 4d72f814..2971e844 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Shaders.Models; using TerraFX.Interop.DirectX; @@ -27,26 +26,16 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader [MethodImpl(MethodImplOptions.AggressiveInlining)] public ICachedShader GetCachedShader() { - if (this.cachedShader is not ICachedShader cachedShader) - { - return ThrowHelper.ThrowInvalidOperationException("The shader has not been initialized."); - } + default(InvalidOperationException).ThrowIf(this.cachedShader is null); - return cachedShader; + return this.cachedShader; } /// public unsafe void LoadDynamicBytecode(IntPtr handle) { - if (this.cachedShader 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.cachedShader is not null); + default(NotSupportedException).ThrowIf(handle == IntPtr.Zero); this.cachedShader = new ICachedShader.Dynamic((IDxcBlob*)handle); } @@ -55,10 +44,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LoadEmbeddedBytecode(ReadOnlySpan bytecode) { - if (this.cachedShader is not null) - { - ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); - } + default(InvalidOperationException).ThrowIf(this.cachedShader is not null); this.cachedShader = new ICachedShader.Embedded(bytecode); } 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) { diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 395f78fe..a3ce97a0 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,5 +1,5 @@ +using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; @@ -23,7 +23,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { - Guard.IsNotNull(buffer); + default(ArgumentNullException).ThrowIfNull(buffer); using ReferenceTracker.Lease lease = default; @@ -39,7 +39,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture1D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -55,7 +55,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -71,7 +71,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -89,7 +89,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -107,7 +107,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -125,7 +125,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -141,7 +141,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, IReadWriteNormalizedTexture1D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -157,7 +157,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, IReadWriteNormalizedTexture2D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -173,7 +173,7 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, IReadWriteNormalizedTexture3D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -189,7 +189,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { - Guard.IsNotNull(buffer); + default(ArgumentNullException).ThrowIfNull(buffer); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice); @@ -205,7 +205,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture1D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); @@ -221,7 +221,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); @@ -237,7 +237,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); @@ -255,7 +255,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -273,7 +273,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -291,7 +291,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -307,7 +307,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteNormalizedTexture1D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -325,7 +325,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteNormalizedTexture2D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -343,7 +343,7 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteNormalizedTexture3D texture) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -364,7 +364,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -383,7 +383,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -402,7 +402,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -419,7 +419,7 @@ public static class ComputeContextExtensions public static unsafe void Fill(this in ComputeContext context, IReadWriteNormalizedTexture1D texture, TPixel value) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -442,7 +442,7 @@ public static class ComputeContextExtensions public static unsafe void Fill(this in ComputeContext context, IReadWriteNormalizedTexture2D texture, TPixel value) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -465,7 +465,7 @@ public static class ComputeContextExtensions public static unsafe void Fill(this in ComputeContext context, IReadWriteNormalizedTexture3D texture, TPixel value) where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); @@ -549,7 +549,7 @@ public static class ComputeContextExtensions where T : struct, IPixelShader where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); context.Run(texture, ref Unsafe.AsRef(default(T))); } @@ -566,7 +566,7 @@ public static class ComputeContextExtensions where T : struct, IPixelShader where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); context.Run(texture, ref Unsafe.AsRef(in shader)); } @@ -579,7 +579,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -596,7 +596,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -613,7 +613,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -630,7 +630,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -651,7 +651,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -668,7 +668,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -685,7 +685,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -702,7 +702,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -719,7 +719,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -740,7 +740,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -757,7 +757,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -774,7 +774,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -791,7 +791,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -808,7 +808,7 @@ public static class ComputeContextExtensions /// The state to transition the input resource to. public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; @@ -829,7 +829,7 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - Guard.IsNotNull(texture); + default(ArgumentNullException).ThrowIfNull(texture); using ReferenceTracker.Lease lease = default; diff --git a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj index 9e55b7d2..26319e57 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj +++ b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj b/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj index a6298c32..9e941a11 100644 --- a/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj +++ b/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 9a94155a..928d7b64 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index dec3ee8d..b7d12abe 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -7,6 +7,7 @@ +