Add device lost unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:09 +02:00
1 親 15682c44e6
コミット ea81ce27f9
3ファイル変更
+137

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

+15
ファイルの表示
@@ -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}
+34
ファイルの表示
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Platforms>AnyCPU;x64;ARM64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp.Core\ComputeSharp.Core.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\ComputeSharp.Tests\Attributes\AdditionalDataAttribute.cs" Link="Attributes\AdditionalDataAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\AdditionalResourceAttribute.cs" Link="Attributes\AdditionalResourceAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\AllDevicesAttribute.cs" Link="Attributes\AllDevicesAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\CombinatorialTestMethodAttribute.cs" Link="Attributes\CombinatorialTestMethodAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\DataAttribute.cs" Link="Attributes\DataAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\DeviceAttribute.cs" Link="Attributes\DeviceAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\ResourceAttribute.cs" Link="Attributes\ResourceAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Extensions\DeviceExtensions.cs" Link="Extensions\DeviceExtensions.cs" />
<Compile Include="..\ComputeSharp.Tests\Extensions\GraphicsDeviceExtensions.cs" Link="Extensions\GraphicsDeviceExtensions.cs" />
</ItemGroup>
</Project>
+88
ファイルの表示
@@ -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<float> bufferA = graphicsDevice.AllocateReadOnlyBuffer<float>(128);
using ReadWriteBuffer<float> bufferB = graphicsDevice.AllocateReadWriteBuffer<float>(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<float> bufferA;
private readonly ReadWriteBuffer<float> bufferB;
private readonly float a;
private readonly float b;
private readonly int target;
/// <inheritdoc/>
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];
}
}
}
}