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
  • Loading branch information
ngoquang2708 committed Dec 28, 2024
1 parent 122fe22 commit 50e4f7d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/video_core/renderer_vulkan/vk_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,22 @@ void Presenter::Present(Frame* frame) {

if (!swapchain.AcquireNextImage()) {
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 the frame for reuse
std::scoped_lock fl{free_mutex};
free_queue.push(frame);
free_cv.notify_one();
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();

const vk::Image swapchain_image = swapchain.Image();
Expand Down Expand Up @@ -733,9 +747,7 @@ void Presenter::Present(Frame* frame) {

// Present to swapchain.
std::scoped_lock submit_lock{Scheduler::submit_mutex};
if (!swapchain.Present()) {
swapchain.Recreate(window.GetWidth(), window.GetHeight());
}
swapchain.Present();

// Free the frame for reuse
std::scoped_lock fl{free_mutex};
Expand Down Expand Up @@ -776,9 +788,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
5 changes: 2 additions & 3 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, "Recreate the swapchain: width={} height={}", width_, height_);
Create(width_, height_, surface);
}

Expand Down Expand Up @@ -112,7 +113,7 @@ bool Swapchain::AcquireNextImage() {
return !needs_recreation;
}

bool Swapchain::Present() {
void Swapchain::Present() {

const vk::PresentInfoKHR present_info = {
.waitSemaphoreCount = 1,
Expand All @@ -131,8 +132,6 @@ bool Swapchain::Present() {
}

frame_index = (frame_index + 1) % image_count;

return !needs_recreation;
}

void Swapchain::FindPresentFormat() {
Expand Down
2 changes: 1 addition & 1 deletion src/video_core/renderer_vulkan/vk_swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Swapchain {
bool AcquireNextImage();

/// Presents the current image and move to the next one
bool Present();
void Present();

vk::SurfaceKHR GetSurface() const {
return surface;
Expand Down

0 comments on commit 50e4f7d

Please sign in to comment.