diff options
| author | Koro4e <96347170+Koro4e@users.noreply.github.com> | 2026-11-23 16:55:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 09:55:22 -0600 |
| commit | 059f79129b37b5a2f054d93b02ce4944e6b197fa (patch) | |
| tree | 312424ae3d1f732f81d4de170b9d090ab2089aa2 /runtime/src | |
| parent | 9b72d0d2cc81a4c49a0085e0efffa6a6f4e44fdd (diff) | |
presenter: recover a lost surface instead of a sticky E_FAIL (#6832)
Squash-merged after diff review of PR #6832 - presenter: recover a lost surface instead of a sticky E_FAIL
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_present.h | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index b2f5894de..8644346c5 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -109,9 +109,11 @@ public: | |||
| 109 | VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, | 109 | VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, |
| 110 | VkPhysicalDevice physical, VkQueue queue, std::uint32_t family, VkSurfaceKHR surface, | 110 | VkPhysicalDevice physical, VkQueue queue, std::uint32_t family, VkSurfaceKHR surface, |
| 111 | bool swapchain_maintenance1 = false, | 111 | bool swapchain_maintenance1 = false, |
| 112 | std::function<VkExtent2D()> extent_fn = nullptr) | 112 | std::function<VkExtent2D()> extent_fn = nullptr, |
| 113 | std::function<VkSurfaceKHR()> recreate_surface_fn = nullptr) | ||
| 113 | : context_(context), instance_(instance), physical_(physical), queue_(queue), surface_(surface), | 114 | : context_(context), instance_(instance), physical_(physical), queue_(queue), surface_(surface), |
| 114 | maintenance1_(swapchain_maintenance1), extent_fn_(std::move(extent_fn)) { | 115 | maintenance1_(swapchain_maintenance1), extent_fn_(std::move(extent_fn)), |
| 116 | recreate_surface_fn_(std::move(recreate_surface_fn)) { | ||
| 115 | #define LEONIDA_INSTANCE_FN(name) name##_ = reinterpret_cast<PFN_##name>(get_instance(instance, #name)); | 117 | #define LEONIDA_INSTANCE_FN(name) name##_ = reinterpret_cast<PFN_##name>(get_instance(instance, #name)); |
| 116 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceSupportKHR) | 118 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceSupportKHR) |
| 117 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) | 119 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) |
| @@ -421,6 +423,18 @@ private: | |||
| 421 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { | 423 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { |
| 422 | last_tore_ = false; | 424 | last_tore_ = false; |
| 423 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 425 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 426 | if (surface_lost_) { // the window's display went away under a presented swapchain | ||
| 427 | // Try to make a new surface for the same window (the hwnd is retained by the factory's callback). | ||
| 428 | // Until that succeeds, vkd3d's precedent keeps rendering to nothing: the frame is not shown, but | ||
| 429 | // the run continues instead of wedging on a sticky E_FAIL. | ||
| 430 | surface_ = recreate_surface_fn_ ? recreate_surface_fn_() : VK_NULL_HANDLE; | ||
| 431 | if (!surface_) { | ||
| 432 | captured_ = false; // nothing was shown: read_back() must not hand out the frame | ||
| 433 | return VK_SUCCESS; | ||
| 434 | } | ||
| 435 | surface_lost_ = false; | ||
| 436 | stale_ = true; // the old swapchain is gone: rebuild on the new surface below | ||
| 437 | } | ||
| 424 | // The swapchain's HDR10 format is only for composed planes that ask for it; a blit of an SDR source or a blank | 438 | // The swapchain's HDR10 format is only for composed planes that ask for it; a blit of an SDR source or a blank |
| 425 | // frame keeps whatever the swapchain has (black is black), except that a blit needs the SDR one. | 439 | // frame keeps whatever the swapchain has (black is black), except that a blit needs the SDR one. |
| 426 | bool want_hdr = swapchain_hdr_req_; | 440 | bool want_hdr = swapchain_hdr_req_; |
| @@ -458,6 +472,11 @@ private: | |||
| 458 | } | 472 | } |
| 459 | acquired = acquire(frame.acquired, &image); | 473 | acquired = acquire(frame.acquired, &image); |
| 460 | } | 474 | } |
| 475 | if (acquired == VK_ERROR_SURFACE_LOST_KHR) { | ||
| 476 | captured_ = false; // nothing was acquired, nothing was shown | ||
| 477 | mark_surface_lost(); | ||
| 478 | return VK_SUCCESS; | ||
| 479 | } | ||
| 461 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired | 480 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired |
| 462 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; | 481 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; |
| 463 | // An out-of-date retry above may have given the swapchain another extent: the compose target follows it. | 482 | // An out-of-date retry above may have given the swapchain another extent: the compose target follows it. |
| @@ -576,6 +595,13 @@ private: | |||
| 576 | captured_extent_ = extent_; | 595 | captured_extent_ = extent_; |
| 577 | captured_format_ = format_; | 596 | captured_format_ = format_; |
| 578 | } | 597 | } |
| 598 | if (result == VK_ERROR_SURFACE_LOST_KHR) { | ||
| 599 | // The surface was lost during the present (display disconnected, a driver reset of the window's | ||
| 600 | // plane): vkd3d keeps rendering to nothing, and the next present recreates surface and swapchain. | ||
| 601 | captured_ = false; | ||
| 602 | mark_surface_lost(); | ||
| 603 | result = VK_SUCCESS; | ||
| 604 | } | ||
| 579 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { | 605 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { |
| 580 | stale_ = true; | 606 | stale_ = true; |
| 581 | result = VK_SUCCESS; | 607 | result = VK_SUCCESS; |
| @@ -635,6 +661,25 @@ private: | |||
| 635 | return failure; | 661 | return failure; |
| 636 | } | 662 | } |
| 637 | 663 | ||
| 664 | // The window's surface is dead (VK_ERROR_SURFACE_LOST_KHR): the swapchain that presented into it is retired | ||
| 665 | // and the surface destroyed. The next submit_now() first tries the factory's recreate-surface callback; until | ||
| 666 | // it succeeds the presenter keeps the vkd3d "render to nothing" behaviour, not a sticky failure. | ||
| 667 | void mark_surface_lost() { | ||
| 668 | auto& vk = context_.vk; | ||
| 669 | const VkDevice device = context_.device(); | ||
| 670 | { | ||
| 671 | std::lock_guard lock(context_.submit_mutex()); | ||
| 672 | vk.vkQueueWaitIdle(queue_); // the old swapchain's images and semaphores may still be in use | ||
| 673 | release(swapchain_); | ||
| 674 | } | ||
| 675 | swapchain_ = VK_NULL_HANDLE; | ||
| 676 | if (surface_ && vkDestroySurfaceKHR_) vkDestroySurfaceKHR_(instance_, surface_, nullptr); | ||
| 677 | surface_ = VK_NULL_HANDLE; | ||
| 678 | surface_lost_ = true; | ||
| 679 | stale_ = true; | ||
| 680 | captured_ = false; | ||
| 681 | } | ||
| 682 | |||
| 638 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, | 683 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, |
| 639 | const PresentRect* source_rect) { | 684 | const PresentRect* source_rect) { |
| 640 | auto& vk = context_.vk; | 685 | auto& vk = context_.vk; |
| @@ -1250,6 +1295,8 @@ private: | |||
| 1250 | std::unique_ptr<PresentComposer> composer_; | 1295 | std::unique_ptr<PresentComposer> composer_; |
| 1251 | std::function<bool()> abort_; | 1296 | std::function<bool()> abort_; |
| 1252 | std::function<VkExtent2D()> extent_fn_; | 1297 | std::function<VkExtent2D()> extent_fn_; |
| 1298 | std::function<VkSurfaceKHR()> recreate_surface_fn_; // makes a new Win32 surface for the same window | ||
| 1299 | bool surface_lost_ = false; // set by mark_surface_lost(); cleared once a new surface was created | ||
| 1253 | std::map<VkFormat, VkFormatFeatureFlags> features_; | 1300 | std::map<VkFormat, VkFormatFeatureFlags> features_; |
| 1254 | bool warned_srgb_ = false; | 1301 | bool warned_srgb_ = false; |
| 1255 | bool capture_ = false; | 1302 | bool capture_ = false; |