diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 3d8e7be8..330d197f 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -11,6 +11,7 @@ + diff --git a/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs new file mode 100644 index 00000000..5659d922 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/Diagnostics.CodeAnalysis/MemberNotNullAttribute.cs @@ -0,0 +1,31 @@ +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Specifies that the method or property will ensure that the listed field and property members have not-null values. +/// +[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] +internal sealed class MemberNotNullAttribute : Attribute +{ + /// + /// Initializes the attribute with a field or property member. + /// + /// The field or property member that is promised to be not-null. + public MemberNotNullAttribute(string member) + { + Members = new[] { member }; + } + + /// + /// Initializes the attribute with the list of field and property members. + /// + /// The list of field and property members that are promised to be not-null. + public MemberNotNullAttribute(params string[] members) + { + Members = members; + } + + /// + /// Gets field or property member names. + /// + public string[] Members { get; } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index 8679210c..32465be7 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -110,7 +110,15 @@ public static partial class GraphicsDeviceExtensions /// // Dispatch shaders here... /// } /// - /// All dispatched shaders will be executed as soon as the context goes out of scope. + /// All dispatched shaders will be executed as soon as the context goes out of scope. + /// Asynchronous execution is also supported, through the interface: + /// + /// await using (var context = device.CreateComputeContext()) + /// { + /// // Dispatch shaders here, they will be executed asynchronously + /// } + /// + /// Copying or not disposing the instance results in undefined behavior. /// public static ComputeContext CreateComputeContext(this GraphicsDevice device) { diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 5e4f59a8..12e884ad 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; using ComputeSharp.__Internals; @@ -21,7 +22,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// /// The instance owning the current context. /// - private readonly GraphicsDevice device; + private GraphicsDevice? device; /// /// The current instance used to dispatch shaders. @@ -238,6 +239,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); + this.device = null; + if (!this.commandList.IsAllocated) { return; @@ -252,6 +255,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); + this.device = null; + if (!this.commandList.IsAllocated) { #if NET6_0_OR_GREATER @@ -308,7 +313,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable } else { - context.commandList = new CommandList(context.device, pipelineState); + context.commandList = new CommandList(context.device!, pipelineState); } return ref context.commandList; @@ -318,12 +323,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// Throws an if is . /// /// Thrown if is . + [MemberNotNull(nameof(device))] [MethodImpl(MethodImplOptions.AggressiveInlining)] private readonly void ThrowInvalidOperationExceptionIfDeviceIsNull() { if (this.device is null) { - ThrowHelper.ThrowInvalidOperationException("The current compute context has not been initialized correctly."); + ThrowHelper.ThrowInvalidOperationException("The current compute context is not in a valid state."); } } } diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 38e74a87..a4a3e4ff 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -534,6 +534,37 @@ public partial class ComputeContextTests Assert.Fail(); } + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public void For_AfterDispose_ThrowsException(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(128); + + ComputeContext context = device.Get().CreateComputeContext(); + + context.Dispose(); + context.For(512, new OffsetComputeShader(buffer, 0)); + + Assert.Fail(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public async Task For_AfterDisposeAsync_ThrowsException(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(128); + + ComputeContext context = device.Get().CreateComputeContext(); + + await context.DisposeAsync(); + + context.For(512, new OffsetComputeShader(buffer, 0)); + + Assert.Fail(); + } + [TestMethod] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_GetGraphicsDevice_ThrowsException()