diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index 49bba493..a4ee66e4 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -1,8 +1,12 @@ -using System.ComponentModel; +using System; using System.Threading.Tasks; +using ComputeSharp.Interop; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using TerraFX.Interop.DirectX; + +#pragma warning disable CA1416 namespace ComputeSharp.Tests.DeviceLost; @@ -16,26 +20,15 @@ public partial class DeviceLostTests { GraphicsDevice graphicsDevice = device.Get(); - //bool win32ExceptionThrown = false; TaskCompletionSource<(object? Sender, DeviceLostReason Reason)> tcs = new(); // Register the device lost callback graphicsDevice.DeviceLost += (s, e) => tcs.SetResult((s, e)); - try - { - using ReadOnlyBuffer bufferA = graphicsDevice.AllocateReadOnlyBuffer(128); - using ReadWriteBuffer bufferB = graphicsDevice.AllocateReadWriteBuffer(128); - - graphicsDevice.For(128, new ComputeShaderGettingStuck(bufferA, bufferB, 12345678, 87654321, 42)); - } - catch (Win32Exception) - { - // This is not guaranteed to be thrown, but handle it just in case - } + RemoveDevice(graphicsDevice); // Wait up to a second for the event to be raised (it's raised asynchronously on a thread pool thread) - await Task.WhenAny(tcs.Task, Task.Delay(1000)); + await Task.WhenAny(tcs.Task, Task.Delay(100)); // Ensure the event has been raised, and get the results Assert.IsTrue(tcs.Task.IsCompleted); @@ -45,36 +38,33 @@ public partial class DeviceLostTests Assert.IsNotNull(sender); Assert.AreSame(sender, graphicsDevice); - Assert.IsTrue(reason is DeviceLostReason.DeviceHung or DeviceLostReason.DeviceReset); + Assert.AreEqual(reason, DeviceLostReason.DeviceRemoved); } - [AutoConstructor] - [EmbeddedBytecode(DispatchAxis.X)] - internal readonly partial struct ComputeShaderGettingStuck : IComputeShader + /// + /// Removes the underlying device for a given instance. + /// + /// The target instance. + private static unsafe void RemoveDevice(GraphicsDevice graphicsDevice) { - private readonly ReadOnlyBuffer bufferA; - private readonly ReadWriteBuffer bufferB; - private readonly float a; - private readonly float b; - private readonly int target; + ID3D12Device5* d3D12Device = default; + Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID; - /// - public void Execute() + if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0) { - if (ThreadIds.X == target) + Assert.Inconclusive(); + } + + try + { + d3D12Device->RemoveDevice(); + } + finally + { + if (d3D12Device is not null) { - for (float x = 0; x < a; x += 0.1f) - { - for (float y = 0; y < b; y += 0.1f) - { - bufferB[ThreadIds.X] += bufferA[ThreadIds.X] * x * y; - } - } + d3D12Device->Release(); } - else - { - bufferB[ThreadIds.X] = bufferA[ThreadIds.X]; - } } } } diff --git a/tests/ComputeSharp.Tests.DeviceLost/TerraFX.Interop.Windows/ID3D12Device5.cs b/tests/ComputeSharp.Tests.DeviceLost/TerraFX.Interop.Windows/ID3D12Device5.cs new file mode 100644 index 00000000..d81d75aa --- /dev/null +++ b/tests/ComputeSharp.Tests.DeviceLost/TerraFX.Interop.Windows/ID3D12Device5.cs @@ -0,0 +1,33 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d3d12.h in the Windows SDK for Windows 10.0.20348.0 +// Original source is Copyright © Microsoft. All rights reserved. + +#if !NET6_0_OR_GREATER + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace TerraFX.Interop.DirectX +{ + [Guid("8B4F173B-2FEA-4B80-8F58-4307191AB95D")] + public unsafe partial struct ID3D12Device5 + { + public void** lpVtbl; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public uint Release() + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[2]))((ID3D12Device5*)Unsafe.AsPointer(ref this)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void RemoveDevice() + { + ((delegate* unmanaged[Stdcall])(lpVtbl[58]))((ID3D12Device5*)Unsafe.AsPointer(ref this)); + } + } +} + +#endif