From 56031f443a8c3568ec521a2cfaf6f1572aab6aa0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Feb 2023 16:49:43 +0100 Subject: [PATCH] Add and use ArgumentOutOfRangeException.ThrowIfNotInRange --- .../ArgumentOutOfRangeExceptionExtensions.cs | 17 +++++++ .../Resources/Abstract/StructuredBuffer{T}.cs | 17 +++---- .../Resources/Abstract/Texture1D{T}.cs | 16 +++---- .../Resources/Abstract/Texture2D{T}.cs | 32 ++++++------- .../Resources/Abstract/Texture3D{T}.cs | 48 +++++++++---------- .../Graphics/Resources/ConstantBuffer{T}.cs | 9 ++-- .../Resources/Views/TextureView2D{T}.cs | 6 +-- .../Resources/Views/TextureView3D{T}.cs | 12 ++--- 8 files changed, 88 insertions(+), 69 deletions(-) diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs index 75f30528..0b5ca0d3 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -38,6 +38,23 @@ 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 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 for a given parameter name. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 00364fc6..3df5b542 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics.CodeAnalysis; using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Commands; @@ -36,7 +37,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -109,9 +110,9 @@ public abstract class StructuredBuffer : Buffer { Guard.IsBetweenOrEqualTo(count, 0, Length); Guard.IsBetweenOrEqualTo(count, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -157,9 +158,9 @@ public abstract class StructuredBuffer : Buffer { Guard.IsBetweenOrEqualTo(count, 0, Length); Guard.IsBetweenOrEqualTo(count, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -200,7 +201,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -277,9 +278,9 @@ public abstract class StructuredBuffer : Buffer { Guard.IsBetweenOrEqualTo(count, 0, Length); Guard.IsBetweenOrEqualTo(count, 0, source.Length); - Guard.IsInRange(sourceOffset, 0, source.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, source.Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index bc359b54..c1033ca7 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -165,7 +165,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsGreaterThanOrEqualTo(size, width); @@ -247,8 +247,8 @@ 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); + 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)); @@ -307,8 +307,8 @@ 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); + 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)); @@ -363,7 +363,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); Guard.IsGreaterThanOrEqualTo(size, width); @@ -445,8 +445,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, source.Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(width, 1, source.Width); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 36eed0a5..987506d1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -177,8 +177,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetY, 0, Height); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); @@ -267,10 +267,10 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(width, 1, destination.Width); @@ -336,10 +336,10 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(width, 1, destination.Width); @@ -400,8 +400,8 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetY, 0, Height); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); @@ -490,10 +490,10 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(width, 1, source.Width); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7edc62d3..7765a3f6 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -189,9 +189,9 @@ 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); + 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); @@ -288,12 +288,12 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(depth, 1, Depth); @@ -366,12 +366,12 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(depth, 1, Depth); @@ -438,9 +438,9 @@ 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); + 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); @@ -537,12 +537,12 @@ 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); + 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); Guard.IsBetweenOrEqualTo(width, 1, Width); Guard.IsBetweenOrEqualTo(height, 1, Height); Guard.IsBetweenOrEqualTo(depth, 1, Depth); diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 20b0a09a..68084ee9 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -61,7 +62,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -89,9 +90,9 @@ public sealed class ConstantBuffer : Buffer { Guard.IsBetweenOrEqualTo(length, 0, Length); Guard.IsBetweenOrEqualTo(length, 0, destination.Length); - Guard.IsInRange(sourceOffset, 0, Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); - Guard.IsInRange(destinationOffset, 0, destination.Length); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); @@ -126,7 +127,7 @@ 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); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs index 200a6c08..ed04a448 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs @@ -127,8 +127,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); } @@ -297,7 +297,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..d1d5ddb7 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs @@ -152,9 +152,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); } @@ -336,8 +336,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 +350,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)),