Skip to content

Commit d624070

Browse files
miniksaDHowett
authored andcommitted
Stop crash on window snap by preventing torn device resources state (#1768)
* Stop crash on window snap by preventing torn device resources state when quick on-the-fly resizing operations happen. #1572 (cherry picked from commit d848507)
1 parent 0f3d25c commit d624070

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/renderer/dx/DxRenderer.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,24 @@ void DxEngine::_InvalidOr(RECT rc) noexcept
690690
else if (_displaySizePixels.cy != clientSize.cy ||
691691
_displaySizePixels.cx != clientSize.cx)
692692
{
693+
// OK, we're going to play a dangerous game here for the sake of optimizing resize
694+
// First, set up a complete clear of all device resources if something goes terribly wrong.
695+
auto resetDeviceResourcesOnFailure = wil::scope_exit([&] {
696+
_ReleaseDeviceResources();
697+
});
698+
699+
// Now let go of a few of the device resources that get in the way of resizing buffers in the swap chain
693700
_dxgiSurface.Reset();
694701
_d2dRenderTarget.Reset();
695-
_dxgiSwapChain->ResizeBuffers(2, clientSize.cx, clientSize.cy, DXGI_FORMAT_B8G8R8A8_UNORM, 0);
702+
703+
// Change the buffer size and recreate the render target (and surface)
704+
RETURN_IF_FAILED(_dxgiSwapChain->ResizeBuffers(2, clientSize.cx, clientSize.cy, DXGI_FORMAT_B8G8R8A8_UNORM, 0));
696705
RETURN_IF_FAILED(_PrepareRenderTarget());
706+
707+
// OK we made it past the parts that can cause errors. We can release our failure handler.
708+
resetDeviceResourcesOnFailure.release();
709+
710+
// And persist the new size.
697711
_displaySizePixels = clientSize;
698712
}
699713

0 commit comments

Comments
 (0)