Add checks and exceptions for multiple disposals of ComputeContext

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-30 22:30:25 +01:00
1 親 a0428b4887
コミット 438280ad23
5ファイル変更
+81 -4

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)System\Collections.Generic\DictionaryExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections.Generic\QueueExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics.CodeAnalysis\MemberNotNullAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Enum.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\GC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IntPtrExtensions.cs" />
@@ -0,0 +1,31 @@
namespace System.Diagnostics.CodeAnalysis;
/// <summary>
/// Specifies that the method or property will ensure that the listed field and property members have not-null values.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
internal sealed class MemberNotNullAttribute : Attribute
{
/// <summary>
/// Initializes the attribute with a field or property member.
/// </summary>
/// <param name="member">The field or property member that is promised to be not-null.</param>
public MemberNotNullAttribute(string member)
{
Members = new[] { member };
}
/// <summary>
/// Initializes the attribute with the list of field and property members.
/// </summary>
/// <param name="members">The list of field and property members that are promised to be not-null.</param>
public MemberNotNullAttribute(params string[] members)
{
Members = members;
}
/// <summary>
/// Gets field or property member names.
/// </summary>
public string[] Members { get; }
}
+9 -1
ファイルの表示
@@ -110,7 +110,15 @@ public static partial class GraphicsDeviceExtensions
/// // Dispatch shaders here...
/// }
/// </code>
/// All dispatched shaders will be executed as soon as the context goes out of scope.
/// <para>All dispatched shaders will be executed as soon as the context goes out of scope.</para>
/// <para>Asynchronous execution is also supported, through the <see cref="System.IAsyncDisposable"/> interface:</para>
/// <code>
/// await using (var context = device.CreateComputeContext())
/// {
/// // Dispatch shaders here, they will be executed asynchronously
/// }
/// </code>
/// Copying or not disposing the <see cref="ComputeContext"/> instance results in undefined behavior.
/// </remarks>
public static ComputeContext CreateComputeContext(this GraphicsDevice device)
{
+9 -3
ファイルの表示
@@ -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
/// <summary>
/// The <see cref="GraphicsDevice"/> instance owning the current context.
/// </summary>
private readonly GraphicsDevice device;
private GraphicsDevice? device;
/// <summary>
/// The current <see cref="CommandList"/> 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 <see cref="InvalidOperationException"/> if <see cref="device"/> is <see langword="null"/>.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if <see cref="device"/> is <see langword="null"/>.</exception>
[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.");
}
}
}
+31
ファイルの表示
@@ -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<int> buffer = device.Get().AllocateReadWriteBuffer<int>(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<int> buffer = device.Get().AllocateReadWriteBuffer<int>(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()