From e02b2c70599d2bdf9e540732a5a68c9b527de3b9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 27 Dec 2021 18:57:00 +0100 Subject: [PATCH] Add checks for null device in ComputeContext --- src/ComputeSharp/Shaders/ComputeContext.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 3ea01570..c228e9c0 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -109,6 +109,8 @@ public ref struct ComputeContext ref T shader) where T : struct, IComputeShader { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + Guard.IsGreaterThan(x, 0, nameof(x)); Guard.IsGreaterThan(y, 0, nameof(y)); Guard.IsGreaterThan(z, 0, nameof(z)); @@ -149,6 +151,8 @@ public ref struct ComputeContext where T : struct, IPixelShader where TPixel : unmanaged { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + int x = texture.Width; int y = texture.Height; const int threadsX = 8; @@ -181,6 +185,8 @@ public ref struct ComputeContext /// public void Dispose() { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + if (!this.commandList.IsAllocated) { return; @@ -216,4 +222,17 @@ public ref struct ComputeContext return ref context.commandList; } + + /// + /// Throws an if is . + /// + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private readonly void ThrowInvalidOperationExceptionIfDeviceIsNull() + { + if (this.device is null) + { + ThrowHelper.ThrowInvalidOperationException("The current compute context has not been initialized correctly."); + } + } }