diff --git a/ComputeSharp.sln b/ComputeSharp.sln
index 06fd1a98..54268e97 100644
--- a/ComputeSharp.sln
+++ b/ComputeSharp.sln
@@ -122,6 +122,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ComputeSharp.NuGet", "Compu
tests\ComputeSharp.NuGet\Program.cs = tests\ComputeSharp.NuGet\Program.cs
EndProjectSection
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.DeviceLost", "tests\ComputeSharp.Tests.DeviceLost\ComputeSharp.Tests.DeviceLost.csproj", "{5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -444,6 +446,18 @@ Global
{14EF12A3-90E6-4683-BD71-9896AF87026B}.Release|ARM64.Build.0 = Release|Any CPU
{14EF12A3-90E6-4683-BD71-9896AF87026B}.Release|x64.ActiveCfg = Release|Any CPU
{14EF12A3-90E6-4683-BD71-9896AF87026B}.Release|x64.Build.0 = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|ARM64.ActiveCfg = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|ARM64.Build.0 = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Debug|x64.Build.0 = Debug|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|ARM64.ActiveCfg = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|ARM64.Build.0 = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|x64.ActiveCfg = Release|Any CPU
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -475,6 +489,7 @@ Global
{CBA545B4-31EF-4E9F-A1A8-2A1290B54046} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
{C0B903AA-F32C-4760-B4B6-199AC4FE1C5A} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
{E3403F19-A0D8-4317-8A54-322C7A13F2A2} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
+ {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC}
diff --git a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj
new file mode 100644
index 00000000..8fe2d777
--- /dev/null
+++ b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj
@@ -0,0 +1,34 @@
+
+
+
+ net472;netcoreapp3.1;net6.0
+ false
+ AnyCPU;x64;ARM64
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs
new file mode 100644
index 00000000..6f830d1f
--- /dev/null
+++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs
@@ -0,0 +1,88 @@
+using System.ComponentModel;
+using System.Threading.Tasks;
+using ComputeSharp.Tests.Attributes;
+using ComputeSharp.Tests.Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.Tests.DeviceLost;
+
+[TestClass]
+[TestCategory("DeviceLost")]
+public partial class DeviceLostTests
+{
+ [CombinatorialTestMethod]
+ [AllDevices]
+ public async Task DeviceLost_RaiseEvent(Device device)
+ {
+ 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));
+
+ // The shader above will always hang, but it's not guaranteed that the DirectX runtime will detect
+ // the error right when executing that command. As such, we can force it to be detected by running
+ // any other command after that. At this point the device is lost, so any operation will fail.
+ graphicsDevice.For(128, new ComputeShaderGettingStuck(bufferA, bufferB, 2, 2, 42));
+ }
+ catch (Win32Exception)
+ {
+ win32ExceptionThrown = true;
+ }
+
+ // The previous shader must hang and cause an exception
+ Assert.IsTrue(win32ExceptionThrown);
+
+ // 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));
+
+ // Ensure the event has been raised, and get the results
+ Assert.IsTrue(tcs.Task.IsCompleted);
+ Assert.AreEqual(tcs.Task.Status, TaskStatus.RanToCompletion);
+
+ (object? sender, DeviceLostReason reason) = tcs.Task.Result;
+
+ Assert.IsNotNull(sender);
+ Assert.AreSame(sender, graphicsDevice);
+ Assert.IsTrue(reason is DeviceLostReason.DeviceHung or DeviceLostReason.DeviceReset);
+ }
+
+ [AutoConstructor]
+ [EmbeddedBytecode(DispatchAxis.X)]
+ internal readonly partial struct ComputeShaderGettingStuck : IComputeShader
+ {
+ private readonly ReadOnlyBuffer bufferA;
+ private readonly ReadWriteBuffer bufferB;
+ private readonly float a;
+ private readonly float b;
+ private readonly int target;
+
+ ///
+ public void Execute()
+ {
+ if (ThreadIds.X == target)
+ {
+ 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;
+ }
+ }
+ }
+ else
+ {
+ bufferB[ThreadIds.X] = bufferA[ThreadIds.X];
+ }
+ }
+ }
+}