正在尝试 "minimum viable" SharpDX 12 设置;错误 0x887A0005 "device removed"

Attempting a "minimum viable" SharpDX 12 setup; error 0x887A0005 "device removed"

目标:编写 "minimum viable",没有多余的代码或额外的代码来设置 SharpDX 12(一个围绕 DirectX 12 的 C# 包装器),创建一个 window,并执行一个当前循环来清除屏幕到 RGBA (0, 0, 1, 1).

预期:尽我所能遵循 DirectX 12 设置教程后,我会得到 window 蓝色背景。

结果:在运行通过循环,在第"swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);"行出现错误信息“SharpDX.SharpDXException: HRESULT: [0x887A0005]、模块:[SharpDX.DXGI]、ApiCode:[DXGI_ERROR_DEVICE_REMOVED/DeviceRemoved]、消息:GPU 设备实例已暂停。使用 GetDeviceRemovedReason 确定适当的操作。

在SharpDX.Result.CheckError() 在 SharpDX.DXGI.SwapChain.Present(Int32 syncInterval,PresentFlags 标志) 在 DirectX12BlueScreen.Program.Main(String[] args) in *\DirectX12BlueScreen\DirectX12BlueScreen\Program.cs:line 114" 打印到控制台 window(我将其用于调试输出)

我要找的是:为什么会出现这个错误(设备肯定没有被移除);我的代码的哪一部分导致了错误;以及问题的解决方案,如果前两条信息没有提供该信息。

由于完整的代码是165行,我把循环贴在这里,完整的代码在pastebin中:https://pastebin.com/vctaYkNC

while(form.Visible)
{
    commandAllocator[0].Reset();
    commandList.Reset(commandAllocator[0], null);
    commandList.SetViewport(viewport: viewport);
    commandList.SetScissorRectangles(rectangle: scissorsRectangle);
    commandList.ResourceBarrierTransition(
        resource: renderTargets[0],
        stateBefore: ResourceStates.Present,
        stateAfter: ResourceStates.RenderTarget);
    commandList.ClearRenderTargetView(
        renderTargetView:
        rtvDescriptorHeap.CPUDescriptorHandleForHeapStart,
        colorRGBA: new RawColor4(0f, 0f, 1f, 1f));
    commandList.ResourceBarrierTransition(
        resource: renderTargets[0],
        stateBefore: ResourceStates.RenderTarget,
        stateAfter: ResourceStates.Present);
    commandList.Close();
    commandQueue.ExecuteCommandList(commandList);
    swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
    Application.DoEvents();
}

编辑:使用 PhillipH 提供的信息,我现在有一个更准确的错误读数: "System.Runtime.InteropServices.SEHException (0x80004005): 外部组件抛出异常。 在 SharpDX.Direct3D12.GraphicsCommandList.ClearRenderTargetView(CpuDescriptorHandle renderTargetView, RawColor4 colorRGBA, Int32 numRects, RawRectangle[] rectsRef) 在 SharpDX.Direct3D12.GraphicsCommandList.ClearRenderTargetView(CpuDescriptorHandle renderTargetView, RawColor4 colorRGBA) 在 DirectX12BlueScreen.Program.Main(String[] args) 在 *\DirectX12BlueScreen\DirectX12BlueScreen\Program.cs:line 138"

我没有正确设置渲染目标,也根本没有使用栅栏。我使用的是 SwapChain 而不是 SwapChain3,因此无法获取当前缓冲区索引。正确的代码已放入 pastebin 以供将来参考。

它按预期成功渲染了三分钟,所以我想我可以说这个问题已经解决了。

感谢 PhillipH,没有他的帮助我仍然想知道如何获得正确的错误消息。

https://pastebin.com/Ku7wqHpJ

工作渲染循环:

while (form.Visible)
{
    commandAllocator[0].Reset();
    commandList.Reset(commandAllocator[0], null);
    commandList.ResourceBarrierTransition(
        resource: renderTargets[currentBufferIndex],
        stateBefore: ResourceStates.Present,
        stateAfter: ResourceStates.RenderTarget);
    renderTargetView =
        rtvDescriptorHeap.CPUDescriptorHandleForHeapStart +
        currentBufferIndex *
        renderTargetViewHeapSize;
    commandList.ClearRenderTargetView(
        renderTargetView: renderTargetView,
        colorRGBA: new RawColor4(0f, 0f, 1f, 1f));
    commandList.ResourceBarrierTransition(
        resource: renderTargets[currentBufferIndex],
        stateBefore: ResourceStates.RenderTarget,
        stateAfter: ResourceStates.Present);
    commandList.Close();
    commandQueue.ExecuteCommandList(commandList);
    swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
    long fenceToWaitFor = fenceValue;
    commandQueue.Signal(fence[0], fenceValue);
    ++fenceValue;
    if (fence[0].CompletedValue < fenceValue)
    {
        fence[0].SetEventOnCompletion(fenceToWaitFor,
            fenceEvent.SafeWaitHandle.DangerousGetHandle());
        fenceEvent.WaitOne();
        fenceEvent.Reset();
    }

    currentBufferIndex = swapChain.CurrentBackBufferIndex;
    Application.DoEvents();
}