From c3d1056a7b8981f77d8cc8225719024a186f947e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Jan 2022 18:48:24 +0100 Subject: [PATCH] Add ArgumentNullException validation for public params --- .../Graphics/Extensions/BufferExtensions.cs | 35 ++++ .../GraphicsDeviceExtensions.Dispatching.cs | 17 ++ .../GraphicsDeviceExtensions.Resources.cs | 164 ++++++++++++++++++ .../Extensions/ReadBackBufferExtensions.cs | 7 + .../Extensions/ReadBackTexture2DExtensions.cs | 14 ++ .../Extensions/ReadBackTexture3DExtensions.cs | 10 ++ .../ReadWriteTexture2DExtensions.cs | 11 ++ .../ReadWriteTexture3DExtensions.cs | 11 ++ .../Extensions/StructuredBufferExtensions.cs | 13 ++ .../Extensions/Texture2DExtensions.cs | 93 ++++++++++ .../Extensions/Texture3DExtensions.cs | 79 +++++++++ .../Extensions/UploadBufferExtensions.cs | 7 + .../Extensions/UploadTexture2DExtensions.cs | 15 ++ .../Extensions/UploadTexture3DExtensions.cs | 10 ++ .../Graphics/GraphicsDevice.GetDevice.cs | 3 + .../__Internals/GraphicsResourceHelper.cs | 22 +++ .../Extensions/ComputeContextExtensions.cs | 61 +++++++ 17 files changed, 572 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index c7bd0b5f..a486d482 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -22,6 +22,8 @@ public static class BufferExtensions public static T[] ToArray(this Buffer source) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + return source.ToArray(0, source.Length); } @@ -36,6 +38,7 @@ public static class BufferExtensions public static T[] ToArray(this Buffer source, int offset, int count) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); T[] data = GC.AllocateUninitializedArray(count); @@ -54,6 +57,9 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, T[] destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(), 0); } @@ -69,6 +75,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + Span span = destination.AsSpan(destinationOffset, count); source.CopyTo(span, sourceOffset); @@ -83,6 +92,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Span destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0); } @@ -96,6 +107,8 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Span destination, int sourceOffset) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(ref MemoryMarshal.GetReference(destination), sourceOffset, destination.Length); } @@ -108,6 +121,9 @@ public static class BufferExtensions public static void CopyTo(this Buffer source, Buffer destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, source.Length); } @@ -123,6 +139,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } @@ -135,6 +154,9 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, T[] source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), 0); } @@ -150,6 +172,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlySpan span = source.AsSpan(sourceOffset, count); destination.CopyFrom(span, destinationOffset); @@ -164,6 +189,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0); } @@ -177,6 +204,8 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, ReadOnlySpan source, int destinationOffset) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), destinationOffset, source.Length); } @@ -189,6 +218,9 @@ public static class BufferExtensions public static void CopyFrom(this Buffer destination, Buffer source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, source.Length); } @@ -204,6 +236,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(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 c097058a..02ebe90d 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -1,4 +1,5 @@ using ComputeSharp.Shaders; +using Microsoft.Toolkit.Diagnostics; using System.Runtime.CompilerServices; namespace ComputeSharp; @@ -18,6 +19,8 @@ public static partial class GraphicsDeviceExtensions public static void For(this GraphicsDevice device, int x, in T shader) where T : struct, IComputeShader { + Guard.IsNotNull(device, nameof(device)); + ShaderRunner.Run(device, x, 1, 1, ref Unsafe.AsRef(in shader)); } @@ -32,6 +35,8 @@ 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, nameof(device)); + ShaderRunner.Run(device, x, y, 1, ref Unsafe.AsRef(in shader)); } @@ -47,6 +52,8 @@ 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, nameof(device)); + ShaderRunner.Run(device, x, y, z, ref Unsafe.AsRef(in shader)); } @@ -65,6 +72,8 @@ 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, nameof(device)); + ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader)); } @@ -79,6 +88,9 @@ public static partial class GraphicsDeviceExtensions where T : struct, IPixelShader where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(texture, nameof(texture)); + ShaderRunner.Run(device, texture, ref Unsafe.AsRef(default(T))); } @@ -94,6 +106,9 @@ public static partial class GraphicsDeviceExtensions where T : struct, IPixelShader where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(texture, nameof(texture)); + ShaderRunner.Run(device, texture, ref Unsafe.AsRef(in shader)); } @@ -122,6 +137,8 @@ public static partial class GraphicsDeviceExtensions /// public static ComputeContext CreateComputeContext(this GraphicsDevice device) { + Guard.IsNotNull(device, nameof(device)); + device.ThrowIfDisposed(); return new(device); diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index 09e50274..10f00162 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -20,6 +20,8 @@ public static partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, length, allocationMode); } @@ -33,6 +35,9 @@ public static partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateConstantBuffer(source.AsSpan()); } @@ -46,6 +51,8 @@ public static partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ConstantBuffer buffer = new(device, source.Length, AllocationMode.Default); buffer.CopyFrom(source); @@ -63,6 +70,9 @@ public static partial class GraphicsDeviceExtensions public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ConstantBuffer constantBuffer = new(device, source.Length, AllocationMode.Default); constantBuffer.CopyFrom(source); @@ -81,6 +91,8 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, length, allocationMode); } @@ -94,6 +106,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyBuffer(source.AsSpan()); } @@ -107,6 +122,8 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadOnlyBuffer buffer = new(device, source.Length, AllocationMode.Default); buffer.CopyFrom(source); @@ -124,6 +141,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlyBuffer readWriteBuffer = new(device, source.Length, AllocationMode.Default); readWriteBuffer.CopyFrom(source); @@ -143,6 +163,8 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -158,6 +180,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture2D(source.AsSpan(), width, height); } @@ -174,6 +199,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture2D(source.AsSpan(offset), width, height); } @@ -187,6 +215,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlyTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -206,6 +237,8 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); texture.CopyFrom(source); @@ -227,6 +260,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -244,6 +279,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture2D(source.AsSpan(), width, height); } @@ -262,6 +300,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture2D(source.AsSpan(offset), width, height); } @@ -277,6 +318,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlyTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -298,6 +342,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); texture.CopyFrom(source); @@ -318,6 +364,8 @@ public static 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, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -334,6 +382,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture3D(source.AsSpan(), width, height, depth); } @@ -351,6 +402,9 @@ public static 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, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture3D(source.AsSpan(offset), width, height, depth); } @@ -368,6 +422,9 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlyTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -388,6 +445,8 @@ public static partial class GraphicsDeviceExtensions public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.CopyFrom(source); @@ -410,6 +469,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -428,6 +489,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture3D(source.AsSpan(), width, height, depth); } @@ -447,6 +511,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadOnlyTexture3D(source.AsSpan(offset), width, height, depth); } @@ -466,6 +533,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadOnlyTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -488,6 +558,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.CopyFrom(source); @@ -506,6 +578,8 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, length, allocationMode); } @@ -519,6 +593,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteBuffer(source.AsSpan()); } @@ -532,6 +609,8 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadWriteBuffer buffer = new(device, source.Length, AllocationMode.Default); buffer.CopyFrom(source); @@ -549,6 +628,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadWriteBuffer readWriteBuffer = new(device, source.Length, AllocationMode.Default); readWriteBuffer.CopyFrom(source); @@ -568,6 +650,8 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -583,6 +667,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture2D(source.AsSpan(), width, height); } @@ -599,6 +686,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture2D(source.AsSpan(offset), width, height); } @@ -612,6 +702,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadWriteTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -631,6 +724,8 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); texture.CopyFrom(source); @@ -652,6 +747,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -669,6 +766,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture2D(source.AsSpan(), width, height); } @@ -687,6 +787,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture2D(source.AsSpan(offset), width, height); } @@ -702,6 +805,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadWriteTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -723,6 +829,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); texture.CopyFrom(source); @@ -743,6 +851,8 @@ public static 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, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -759,6 +869,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture3D(source.AsSpan(), width, height, depth); } @@ -776,6 +889,9 @@ public static 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, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture3D(source.AsSpan(offset), width, height, depth); } @@ -793,6 +909,9 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] source) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadWriteTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -813,6 +932,8 @@ public static partial class GraphicsDeviceExtensions public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.CopyFrom(source); @@ -835,6 +956,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -853,6 +976,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture3D(source.AsSpan(), width, height, depth); } @@ -872,6 +998,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + return device.AllocateReadWriteTexture3D(source.AsSpan(offset), width, height, depth); } @@ -891,6 +1020,9 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + Guard.IsNotNull(source, nameof(source)); + ReadWriteTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); texture.CopyFrom(source); @@ -913,6 +1045,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.CopyFrom(source); @@ -931,6 +1065,8 @@ public static partial class GraphicsDeviceExtensions public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, length, allocationMode); } @@ -946,6 +1082,8 @@ public static partial class GraphicsDeviceExtensions public static UploadTexture2D AllocateUploadTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -962,6 +1100,8 @@ public static 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, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -976,6 +1116,8 @@ public static partial class GraphicsDeviceExtensions public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, length, allocationMode); } @@ -991,6 +1133,8 @@ public static partial class GraphicsDeviceExtensions public static ReadBackTexture2D AllocateReadBackTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return new(device, width, height, allocationMode); } @@ -1007,6 +1151,8 @@ public static 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, nameof(device)); + return new(device, width, height, depth, allocationMode); } @@ -1022,6 +1168,7 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(filename, nameof(filename)); return device.LoadReadOnlyTexture2D(filename.AsSpan()); @@ -1039,6 +1186,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); @@ -1060,6 +1209,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); ReadOnlyTexture2D texture = device.AllocateReadOnlyTexture2D(upload.Width, upload.Height); @@ -1081,6 +1232,7 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(stream, nameof(stream)); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, stream); @@ -1104,6 +1256,7 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(filename, nameof(filename)); return device.LoadReadWriteTexture2D(filename.AsSpan()); @@ -1121,6 +1274,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); ReadWriteTexture2D texture = device.AllocateReadWriteTexture2D(upload.Width, upload.Height); @@ -1142,6 +1297,8 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); + using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); ReadWriteTexture2D texture = device.AllocateReadWriteTexture2D(upload.Width, upload.Height); @@ -1163,6 +1320,7 @@ public static partial class GraphicsDeviceExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(stream, nameof(stream)); using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, stream); @@ -1184,6 +1342,7 @@ public static partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, string filename) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(filename, nameof(filename)); return device.LoadUploadTexture2D(filename.AsSpan()); @@ -1199,6 +1358,8 @@ public static partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, ReadOnlySpan filename) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return WICHelper.Instance.LoadTexture(device, filename); } @@ -1212,6 +1373,8 @@ public static partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, ReadOnlySpan span) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); + return WICHelper.Instance.LoadTexture(device, span); } @@ -1225,6 +1388,7 @@ public static partial class GraphicsDeviceExtensions public static UploadTexture2D LoadUploadTexture2D(this GraphicsDevice device, Stream stream) where T : unmanaged { + Guard.IsNotNull(device, nameof(device)); Guard.IsNotNull(stream, nameof(stream)); return WICHelper.Instance.LoadTexture(device, stream); diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs index ba0f8009..0941f343 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs @@ -1,4 +1,5 @@ using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -16,6 +17,9 @@ public static class ReadBackBufferExtensions public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, source.Length); } @@ -31,6 +35,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs index 5d949928..f8859f2a 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs @@ -21,6 +21,9 @@ public static class ReadBackTexture2DExtensions public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -37,6 +40,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -55,6 +61,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -67,6 +76,7 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, string filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(filename, nameof(filename)); WICHelper.Instance.SaveTexture(texture.View, filename.AsSpan()); @@ -81,6 +91,8 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, ReadOnlySpan filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + WICHelper.Instance.SaveTexture(texture.View, filename); } @@ -94,6 +106,7 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, Stream stream, ImageFormat format) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(stream, nameof(stream)); WICHelper.Instance.SaveTexture(texture.View, stream, format); @@ -109,6 +122,7 @@ public static class ReadBackTexture2DExtensions public static void Save(this ReadBackTexture2D texture, IBufferWriter writer, ImageFormat format) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(writer, nameof(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 80024868..330a63d4 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs @@ -1,4 +1,5 @@ using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -16,6 +17,9 @@ public static class ReadBackTexture3DExtensions public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -34,6 +38,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -55,6 +62,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs index 7b9f48dc..6a1d7006 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -28,6 +29,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -52,6 +55,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -76,6 +81,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -100,6 +107,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -128,6 +137,8 @@ public static class ReadWriteTexture2DExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + return texture.AsReadOnly(); } } diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs index fd2f9657..c8462f6b 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -28,6 +29,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -52,6 +55,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -76,6 +81,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -100,6 +107,8 @@ 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, nameof(texture)); + return texture.AsReadOnly(); } @@ -128,6 +137,8 @@ public static class ReadWriteTexture3DExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + return texture.AsReadOnly(); } } diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index 8d97bd5f..9953e30e 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -1,4 +1,5 @@ using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -16,6 +17,9 @@ public static class StructuredBufferExtensions public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, source.Length); } @@ -31,6 +35,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } @@ -43,6 +50,9 @@ public static class StructuredBufferExtensions public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, source.Length); } @@ -58,6 +68,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index 5ffe3984..0ea1a9c6 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -22,6 +22,8 @@ public static class Texture2DExtensions public static T[,] ToArray(this Texture2D source) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + T[,] data = new T[source.Height, source.Width]; source.CopyTo(data); @@ -42,6 +44,7 @@ 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, nameof(source)); Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); @@ -64,6 +67,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[,] destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); Guard.IsEqualTo(destination.GetLength(0), source.Height, nameof(destination)); Guard.IsEqualTo(destination.GetLength(1), source.Width, nameof(destination)); @@ -79,6 +84,9 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(), 0, 0, source.Width, source.Height); } @@ -94,6 +102,9 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, Range x, Range y) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(), x, y); } #endif @@ -111,6 +122,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(), sourceOffsetX, sourceOffsetY, width, height); } @@ -124,6 +138,9 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, source.Width, source.Height); } @@ -140,6 +157,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), x, y); } #endif @@ -158,6 +178,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, width, height); } @@ -171,6 +194,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Span destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, source.Width, source.Height); } @@ -186,6 +211,8 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Span destination, Range x, Range y) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + var (offsetX, width) = x.GetOffsetAndLength(source.Width); var (offsetY, height) = y.GetOffsetAndLength(source.Height); @@ -206,6 +233,8 @@ 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, nameof(source)); + source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, width, height); } @@ -218,6 +247,9 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, Texture2D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -234,6 +266,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -252,6 +287,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -264,6 +302,9 @@ public static class Texture2DExtensions public static void CopyTo(this Texture2D source, ReadBackTexture2D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -280,6 +321,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -298,6 +342,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -310,6 +357,8 @@ public static class Texture2DExtensions public static unsafe void CopyFrom(this Texture2D destination, T[,] source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); Guard.IsEqualTo(source.GetLength(0), destination.Height, nameof(source)); Guard.IsEqualTo(source.GetLength(1), destination.Width, nameof(source)); @@ -325,6 +374,9 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), 0, 0, destination.Width, destination.Height); } @@ -340,6 +392,9 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, T[] source, Range x, Range y) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), x, y); } #endif @@ -357,6 +412,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, width, height); } @@ -373,6 +431,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(sourceOffset), x, y); } #endif @@ -391,6 +452,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, width, height); } @@ -404,6 +468,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0, 0, destination.Width, destination.Height); } @@ -419,6 +485,8 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, ReadOnlySpan source, Range x, Range y) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + var (offsetX, width) = x.GetOffsetAndLength(destination.Width); var (offsetY, height) = y.GetOffsetAndLength(destination.Height); @@ -439,6 +507,8 @@ 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, nameof(destination)); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, width, height); } @@ -451,6 +521,9 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, Texture2D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); } @@ -467,6 +540,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -485,6 +561,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -497,6 +576,9 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, UploadTexture2D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } @@ -513,6 +595,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -531,6 +616,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -543,6 +631,7 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, string filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(filename, nameof(filename)); texture.Save(filename.AsSpan()); @@ -557,6 +646,8 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, ReadOnlySpan filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); texture.CopyTo(readback); @@ -574,6 +665,7 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, Stream stream, ImageFormat format) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(stream, nameof(stream)); using ReadBackTexture2D readback = texture.GraphicsDevice.AllocateReadBackTexture2D(texture.Width, texture.Height); @@ -593,6 +685,7 @@ public static class Texture2DExtensions public static void Save(this Texture2D texture, IBufferWriter writer, ImageFormat format) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(writer, nameof(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 51016305..d9664901 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -24,6 +24,8 @@ public static class Texture3DExtensions public static T[,,] ToArray(this Texture3D source) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + T[,,] data = new T[source.Depth, source.Height, source.Width]; source.CopyTo(data); @@ -46,6 +48,7 @@ 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, nameof(source)); Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); Guard.IsGreaterThanOrEqualTo(depth, 0, nameof(depth)); @@ -73,6 +76,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[,,] destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(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)); @@ -90,6 +95,9 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, T[] destination, int destinationOffset) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -107,6 +115,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), x, y, z); } #endif @@ -127,6 +138,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } @@ -139,6 +153,8 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Span destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -155,6 +171,8 @@ 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, nameof(source)); + var (offsetX, width) = x.GetOffsetAndLength(source.Width); var (offsetY, height) = y.GetOffsetAndLength(source.Height); var (offsetZ, depth) = z.GetOffsetAndLength(source.Depth); @@ -178,6 +196,8 @@ 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, nameof(source)); + source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } @@ -190,6 +210,9 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, Texture3D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -208,6 +231,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -229,6 +255,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -241,6 +270,9 @@ public static class Texture3DExtensions public static void CopyTo(this Texture3D source, ReadBackTexture3D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -259,6 +291,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -280,6 +315,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -296,6 +334,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[,,] source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(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)); @@ -312,6 +352,9 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, T[] source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), 0, 0, 0, destination.Width, destination.Height, destination.Depth); } @@ -328,6 +371,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), x, y, z); } #endif @@ -347,6 +393,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -364,6 +413,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(sourceOffset), x, y, z); } #endif @@ -384,6 +436,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -397,6 +452,8 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, ReadOnlySpan source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0, 0, 0, destination.Width, destination.Height, destination.Depth); } @@ -413,6 +470,8 @@ 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, nameof(destination)); + var (offsetX, width) = x.GetOffsetAndLength(destination.Width); var (offsetY, height) = y.GetOffsetAndLength(destination.Height); var (offsetZ, depth) = z.GetOffsetAndLength(destination.Depth); @@ -436,6 +495,8 @@ 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, nameof(destination)); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -448,6 +509,9 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, Texture3D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -466,6 +530,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -487,6 +554,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } @@ -499,6 +569,9 @@ public static class Texture3DExtensions public static void CopyFrom(this Texture3D destination, UploadTexture3D source) where T : unmanaged { + Guard.IsNotNull(destination, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -517,6 +590,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(source)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -538,6 +614,9 @@ 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, nameof(destination)); + Guard.IsNotNull(source, nameof(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 10054354..4c2339ad 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs @@ -1,4 +1,5 @@ using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -16,6 +17,9 @@ public static class UploadBufferExtensions public static void CopyTo(this UploadBuffer source, StructuredBuffer destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0, 0, source.Length); } @@ -31,6 +35,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs index 6b97447c..1ad1a6f6 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs @@ -20,6 +20,9 @@ public static class UploadTexture2DExtensions public static void CopyTo(this UploadTexture2D source, Texture2D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); } @@ -36,6 +39,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } @@ -54,6 +60,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); } @@ -66,6 +75,7 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, string filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(filename, nameof(filename)); texture. Load(filename.AsSpan()); @@ -80,6 +90,8 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, ReadOnlySpan filename) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + WICHelper.Instance.LoadTexture(texture.View, filename); } @@ -93,6 +105,8 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, ReadOnlySpan span) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + WICHelper.Instance.LoadTexture(texture.View, span); } @@ -105,6 +119,7 @@ public static class UploadTexture2DExtensions public static void Load(this UploadTexture2D texture, Stream stream) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); Guard.IsNotNull(stream, nameof(stream)); WICHelper.Instance.LoadTexture(texture.View, stream); diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs index 52778dbc..e2dfa2d4 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs @@ -1,4 +1,5 @@ using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -16,6 +17,9 @@ public static class UploadTexture3DExtensions public static void CopyTo(this UploadTexture3D source, Texture3D destination) where T : unmanaged { + Guard.IsNotNull(source, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } @@ -34,6 +38,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } @@ -55,6 +62,9 @@ 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, nameof(source)); + Guard.IsNotNull(destination, nameof(destination)); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs index c39a276f..abe3b600 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp; @@ -46,6 +47,8 @@ partial class GraphicsDevice /// public static IEnumerable QueryDevices(Predicate predicate) { + Guard.IsNotNull(predicate, nameof(predicate)); + return new DeviceHelper.DeviceQuery(predicate); } } diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index b1a846f6..72da4805 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -62,6 +62,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(buffer, nameof(buffer)); + buffer.ThrowIfDisposed(); buffer.ThrowIfDeviceMismatch(device); @@ -81,6 +83,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); @@ -100,6 +104,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); @@ -119,6 +125,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); @@ -140,6 +148,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture2D texture, GraphicsDevice device) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); @@ -161,6 +171,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture2D texture, GraphicsDevice device) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); @@ -182,6 +194,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); @@ -201,6 +215,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); @@ -220,6 +236,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); @@ -241,6 +259,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyNormalizedTexture3D texture, GraphicsDevice device) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); @@ -262,6 +282,8 @@ public static class GraphicsResourceHelper public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteNormalizedTexture3D texture, GraphicsDevice device) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + if (texture is IGraphicsResource resource) { D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index ffe0449b..2ee2537f 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,6 +1,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; #pragma warning disable CS0618 @@ -21,6 +22,8 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { + Guard.IsNotNull(buffer, nameof(buffer)); + context.Barrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -33,6 +36,8 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -45,6 +50,8 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -59,6 +66,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -73,6 +82,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -85,6 +96,8 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, IReadWriteNormalizedTexture2D texture) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -97,6 +110,8 @@ public static class ComputeContextExtensions public static unsafe void Barrier(this in ComputeContext context, IReadWriteNormalizedTexture3D texture) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); } @@ -109,6 +124,8 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { + Guard.IsNotNull(buffer, nameof(buffer)); + var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice); context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false); @@ -123,6 +140,8 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); @@ -137,6 +156,8 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); @@ -153,6 +174,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); @@ -169,6 +192,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); @@ -183,6 +208,8 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteNormalizedTexture2D texture) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); @@ -197,6 +224,8 @@ public static class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteNormalizedTexture3D texture) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); @@ -214,6 +243,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); @@ -231,6 +262,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); @@ -246,6 +279,8 @@ public static class ComputeContextExtensions public static unsafe void Fill(this in ComputeContext context, IReadWriteNormalizedTexture2D texture, TPixel value) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Fill( @@ -265,6 +300,8 @@ public static class ComputeContextExtensions public static unsafe void Fill(this in ComputeContext context, IReadWriteNormalizedTexture3D texture, TPixel value) where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); context.Fill( @@ -345,6 +382,8 @@ public static class ComputeContextExtensions where T : struct, IPixelShader where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Run(texture, ref Unsafe.AsRef(default(T))); } @@ -360,6 +399,8 @@ public static class ComputeContextExtensions where T : struct, IPixelShader where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + context.Run(texture, ref Unsafe.AsRef(in shader)); } @@ -371,6 +412,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -384,6 +427,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -397,6 +442,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -410,6 +457,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -427,6 +476,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -440,6 +491,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -453,6 +506,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -466,6 +521,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -479,6 +536,8 @@ 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, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After); @@ -496,6 +555,8 @@ public static class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { + Guard.IsNotNull(texture, nameof(texture)); + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); context.Transition(d3D12Resource, states.Before, states.After);