renderer_vulkan: fix deadlock when resizing the SDL window (#1860)

* renderer_vulkan: Fix deadlock when resizing the SDL window

* Address review comment
This commit is contained in:
Quang Ngô 2024-12-29 18:22:35 +07:00 committed by GitHub
parent ee72d99947
commit 1bc27135e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 8 deletions

View file

@ -628,6 +628,13 @@ Frame* Presenter::PrepareFrameInternal(VideoCore::ImageId image_id, bool is_eop)
} }
void Presenter::Present(Frame* frame) { void Presenter::Present(Frame* frame) {
// Free the frame for reuse
const auto free_frame = [&] {
std::scoped_lock fl{free_mutex};
free_queue.push(frame);
free_cv.notify_one();
};
// Recreate the swapchain if the window was resized. // Recreate the swapchain if the window was resized.
if (window.GetWidth() != swapchain.GetExtent().width || if (window.GetWidth() != swapchain.GetExtent().width ||
window.GetHeight() != swapchain.GetExtent().height) { window.GetHeight() != swapchain.GetExtent().height) {
@ -636,8 +643,19 @@ void Presenter::Present(Frame* frame) {
if (!swapchain.AcquireNextImage()) { if (!swapchain.AcquireNextImage()) {
swapchain.Recreate(window.GetWidth(), window.GetHeight()); swapchain.Recreate(window.GetWidth(), window.GetHeight());
if (!swapchain.AcquireNextImage()) {
// User resizes the window too fast and GPU can't keep up. Skip this frame.
LOG_WARNING(Render_Vulkan, "Skipping frame!");
free_frame();
return;
}
} }
// Reset fence for queue submission. Do it here instead of GetRenderFrame() because we may
// skip frame because of slow swapchain recreation. If a frame skip occurs, we skip signal
// the frame's present fence and future GetRenderFrame() call will hang waiting for this frame.
instance.GetDevice().resetFences(frame->present_done);
ImGui::Core::NewFrame(); ImGui::Core::NewFrame();
const vk::Image swapchain_image = swapchain.Image(); const vk::Image swapchain_image = swapchain.Image();
@ -737,11 +755,7 @@ void Presenter::Present(Frame* frame) {
swapchain.Recreate(window.GetWidth(), window.GetHeight()); swapchain.Recreate(window.GetWidth(), window.GetHeight());
} }
// Free the frame for reuse free_frame();
std::scoped_lock fl{free_mutex};
free_queue.push(frame);
free_cv.notify_one();
DebugState.IncFlipFrameNum(); DebugState.IncFlipFrameNum();
} }
@ -776,9 +790,6 @@ Frame* Presenter::GetRenderFrame() {
} }
} }
// Reset fence for next queue submission.
device.resetFences(frame->present_done);
// If the window dimensions changed, recreate this frame // If the window dimensions changed, recreate this frame
if (frame->width != window.GetWidth() || frame->height != window.GetHeight()) { if (frame->width != window.GetWidth() || frame->height != window.GetHeight()) {
RecreateFrame(frame, window.GetWidth(), window.GetHeight()); RecreateFrame(frame, window.GetWidth(), window.GetHeight());

View file

@ -84,6 +84,7 @@ void Swapchain::Create(u32 width_, u32 height_, vk::SurfaceKHR surface_) {
} }
void Swapchain::Recreate(u32 width_, u32 height_) { void Swapchain::Recreate(u32 width_, u32 height_) {
LOG_DEBUG(Render_Vulkan, "Recreate the swapchain: width={} height={}", width_, height_);
Create(width_, height_, surface); Create(width_, height_, surface);
} }