diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs index 0b5ca0d3..b3f9a599 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -55,6 +55,40 @@ internal static class ArgumentOutOfRangeExceptionExtensions } } + /// + /// 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. /// @@ -66,4 +100,11 @@ internal static class ArgumentOutOfRangeExceptionExtensions { 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/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 3df5b542..94dcec18 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -36,7 +36,7 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count) { - Guard.IsBetweenOrEqualTo(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); @@ -108,8 +108,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, destination.Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); @@ -156,8 +156,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, destination.Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); @@ -200,7 +200,7 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); @@ -276,8 +276,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(count, 0, source.Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, source.Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset)); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, Length); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index c1033ca7..ccc3a131 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -78,7 +78,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(); @@ -166,7 +166,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr internal void CopyTo(ref T destination, int size, int sourceOffsetX, int width) { default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsGreaterThanOrEqualTo(size, width); @@ -249,9 +249,9 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr { default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -309,9 +309,9 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr { default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, destination.Width); - Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, destination.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -364,7 +364,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr internal void CopyFrom(ref T source, int size, int destinationOffsetX, int width) { default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); Guard.IsGreaterThanOrEqualTo(size, width); @@ -447,8 +447,8 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr { default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, source.Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(width, 1, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, source.Width); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 987506d1..cc44c080 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -79,8 +79,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(); @@ -179,8 +179,8 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr { default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); @@ -271,12 +271,12 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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)); + 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)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); @@ -340,12 +340,12 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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)); + 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)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); @@ -402,8 +402,8 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr { default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); @@ -494,10 +494,10 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, source.Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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); + 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); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7765a3f6..9a1caa4c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -80,9 +80,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(); @@ -192,9 +192,9 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetZ, 0, Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); @@ -294,15 +294,15 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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)); + 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)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); @@ -372,15 +372,15 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, destination.Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, destination.Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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)); + 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)); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); @@ -441,9 +441,9 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetZ, 0, Depth); - Guard.IsBetweenOrEqualTo(width, 1, Width); - Guard.IsBetweenOrEqualTo(height, 1, Height); - Guard.IsBetweenOrEqualTo(depth, 1, Depth); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, Depth); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); @@ -543,12 +543,12 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); default(ArgumentOutOfRangeException).ThrowIfNotInRange(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); + 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); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ)); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index db0e49e3..a41710ef 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -58,7 +58,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(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs index 4a846452..762bc93a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs @@ -60,7 +60,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(); 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 68084ee9..db6c45af 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -61,7 +61,7 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); @@ -88,8 +88,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, destination.Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); @@ -126,7 +126,7 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { - Guard.IsBetweenOrEqualTo(length, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(length, 0, Length); default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 954e62c5..2c1932ff 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -184,18 +184,18 @@ public struct ComputeContext : IDisposable, IAsyncDisposable default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(x); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); - Guard.IsBetweenOrEqualTo(threadsX, 1, 1024); - Guard.IsBetweenOrEqualTo(threadsY, 1, 1024); - Guard.IsBetweenOrEqualTo(threadsZ, 1, 64); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsX, 1, 1024); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsY, 1, 1024); + default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(threadsZ, 1, 64); Guard.IsLessThanOrEqualTo(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); @@ -231,8 +231,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);