Skip to content

Commit

Permalink
renderer_vulkan: fix deadlock when resizing the SDL window
Browse files Browse the repository at this point in the history
 Commit 400da1a add ability to handle recreation
of the swapchain which fix black screen under XWayland. Unfortunately, that commit
doesn't account for rapid swapchain recreation caused by resizing window.

 This commit aim to solve that issue by skipping frames to recreate the swapchain.

 Also delay resetting the frame's present done fence until after we know for sure
we will be submitting work with it.
  • Loading branch information
ngoquang2708 committed Dec 24, 2024
1 parent 0a4453b commit eabfe13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/video_core/renderer_vulkan/vk_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,22 @@ Frame* Presenter::PrepareFrameInternal(VideoCore::ImageId image_id, bool is_eop)
}

void Presenter::Present(Frame* frame) {
// Recreate the swapchain if the window was resized.
if (window.GetWidth() != swapchain.GetExtent().width ||
window.GetHeight() != swapchain.GetExtent().height) {
if (swapchain.AcquireNextImage()) {
PresentInternal(frame);
} else {
swapchain.Recreate(window.GetWidth(), window.GetHeight());
}
// Free the frame for reuse
std::scoped_lock fl{free_mutex};
free_queue.push(frame);
free_cv.notify_one();

if (!swapchain.AcquireNextImage()) {
swapchain.Recreate(window.GetWidth(), window.GetHeight());
}
DebugState.IncFlipFrameNum();
}

void Presenter::PresentInternal(Frame* frame) {
// Reset fence for queue submission.
instance.GetDevice().resetFences(frame->present_done);

ImGui::Core::NewFrame();

Expand Down Expand Up @@ -736,13 +743,6 @@ void Presenter::Present(Frame* frame) {
if (!swapchain.Present()) {
swapchain.Recreate(window.GetWidth(), window.GetHeight());
}

// Free the frame for reuse
std::scoped_lock fl{free_mutex};
free_queue.push(frame);
free_cv.notify_one();

DebugState.IncFlipFrameNum();
}

Frame* Presenter::GetRenderFrame() {
Expand Down Expand Up @@ -776,9 +776,6 @@ Frame* Presenter::GetRenderFrame() {
}
}

// Reset fence for next queue submission.
device.resetFences(frame->present_done);

// If the window dimensions changed, recreate this frame
if (frame->width != window.GetWidth() || frame->height != window.GetHeight()) {
RecreateFrame(frame, window.GetWidth(), window.GetHeight());
Expand Down
1 change: 1 addition & 0 deletions src/video_core/renderer_vulkan/vk_presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Presenter {

private:
void CreatePostProcessPipeline();
void PresentInternal(Frame *frame);
Frame* PrepareFrameInternal(VideoCore::ImageId image_id, bool is_eop = true);
Frame* GetRenderFrame();

Expand Down
1 change: 1 addition & 0 deletions src/video_core/renderer_vulkan/vk_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void Swapchain::Create(u32 width_, u32 height_, vk::SurfaceKHR surface_) {
}

void Swapchain::Recreate(u32 width_, u32 height_) {
LOG_DEBUG(Render_Vulkan, "width={} height={}", width_, height_);
Create(width_, height_, surface);
}

Expand Down

0 comments on commit eabfe13

Please sign in to comment.