diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 13:00:38 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 13:00:38 +0100 |
| commit | c03b573a161c1bf0f4cab9d6ab7eb8e712187ecc (patch) | |
| tree | 1cbd0648d560f1e7cf9d6342803d9c13a97894d4 /runtime/src | |
| parent | 6728a00c2a27d9e21785d262f5ab8479c819dc89 (diff) | |
PresentX tear: review fixes
- find_tear_mode rejects a mode whose surface caps lack the swapchain's image usage or extent.
- present_timing: a repeated or stale token is never late.
- percent_scanned is measured at commit, so it agrees with the display time.
- The "no effect" log distinguishes the kill switch from a missing extension or mode.
- LEONIDA_PRESENT_NO_TEAR is read with _dupenv_s: any non-empty value but "0" disables.
- xbox_present_test: the real-time comment is back where it belongs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/frame_pacer.h | 19 | ||||
| -rw-r--r-- | runtime/src/vulkan_present.h | 12 |
2 files changed, 22 insertions, 9 deletions
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index bd78049ca..7540795d9 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h | |||
| @@ -267,7 +267,8 @@ public: | |||
| 267 | } | 267 | } |
| 268 | // How late the frame of `token` is if it is presented now (see the model above). late_us stays 0 when the frame | 268 | // How late the frame of `token` is if it is presented now (see the model above). late_us stays 0 when the frame |
| 269 | // could not be shown at once anyway: unpaced, null or unknown token, or earlier frames still waiting for their | 269 | // could not be shown at once anyway: unpaced, null or unknown token, or earlier frames still waiting for their |
| 270 | // vblank (a flip cannot overtake them). | 270 | // vblank (a flip cannot overtake them), or the token was already presented or is older than the last presented |
| 271 | // (a repeated or stale token never tears). | ||
| 271 | struct PresentTiming { | 272 | struct PresentTiming { |
| 272 | std::uint64_t late_us = 0; | 273 | std::uint64_t late_us = 0; |
| 273 | std::uint32_t period_us = 0; // 0: unpaced | 274 | std::uint32_t period_us = 0; // 0: unpaced |
| @@ -281,17 +282,17 @@ public: | |||
| 281 | timing.period_us = period_us_; | 282 | timing.period_us = period_us_; |
| 282 | if (!pending_.empty()) return timing; | 283 | if (!pending_.empty()) return timing; |
| 283 | for (const Record& r : records_) | 284 | for (const Record& r : records_) |
| 284 | if (r.token == token) { | 285 | if (r.token == token && !r.presented && token > last_presented_) { |
| 285 | const std::uint64_t due = r.origin_us - std::min<std::uint64_t>(r.origin_us, offset_us_) + period_us_; | 286 | const std::uint64_t due = r.origin_us - std::min<std::uint64_t>(r.origin_us, offset_us_) + period_us_; |
| 286 | if (now > due) timing.late_us = now - due; | 287 | if (now > due) timing.late_us = now - due; |
| 287 | } | 288 | } |
| 288 | return timing; | 289 | return timing; |
| 289 | } | 290 | } |
| 290 | 291 | ||
| 291 | // `sync` immediate: the frame is shown at once (display time = present time, percent_scanned from `late_us`) | 292 | // `sync` immediate: the frame is shown at once (display time = present time) instead of at the next vblank, and |
| 292 | // instead of at the next vblank. The caller got that from decide_present_sync and the presenter confirmed it. | 293 | // percent_scanned is the lateness measured at this commit, so it agrees with the display time. The caller got |
| 293 | void commit_present_token(std::uint64_t token, std::uint32_t plane_count, PresentSync sync = PresentSync::vblank, | 294 | // `sync` from decide_present_sync and the presenter confirmed it. |
| 294 | std::uint64_t late_us = 0) { | 295 | void commit_present_token(std::uint64_t token, std::uint32_t plane_count, PresentSync sync = PresentSync::vblank) { |
| 295 | (void)plane_count; | 296 | (void)plane_count; |
| 296 | std::lock_guard guard(mutex_); | 297 | std::lock_guard guard(mutex_); |
| 297 | const std::uint64_t now = clock_.now_us(); | 298 | const std::uint64_t now = clock_.now_us(); |
| @@ -309,6 +310,12 @@ public: | |||
| 309 | float percent = 0.0f; | 310 | float percent = 0.0f; |
| 310 | if (sync == PresentSync::immediate && pending_.empty()) { | 311 | if (sync == PresentSync::immediate && pending_.empty()) { |
| 311 | flip = now; | 312 | flip = now; |
| 313 | std::uint64_t late_us = 0; | ||
| 314 | for (const Record& r : records_) | ||
| 315 | if (r.token == token) { | ||
| 316 | const std::uint64_t due = r.origin_us - std::min<std::uint64_t>(r.origin_us, offset_us_) + period_us_; | ||
| 317 | if (now > due) late_us = now - due; | ||
| 318 | } | ||
| 312 | percent = scan_percent(late_us, period_us_); | 319 | percent = scan_percent(late_us, period_us_); |
| 313 | ++immediate_presents_; | 320 | ++immediate_presents_; |
| 314 | } | 321 | } |
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index cc782a17d..148ddb7ae 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -647,7 +647,7 @@ private: | |||
| 647 | VkSwapchainPresentModesCreateInfoKHR switchable{VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_KHR}; | 647 | VkSwapchainPresentModesCreateInfoKHR switchable{VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_KHR}; |
| 648 | std::array<VkPresentModeKHR, 2> switchable_modes{VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_MAX_ENUM_KHR}; | 648 | std::array<VkPresentModeKHR, 2> switchable_modes{VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_MAX_ENUM_KHR}; |
| 649 | VkPresentModeKHR tear = VK_PRESENT_MODE_MAX_ENUM_KHR; | 649 | VkPresentModeKHR tear = VK_PRESENT_MODE_MAX_ENUM_KHR; |
| 650 | if (vsync && mode == VK_PRESENT_MODE_FIFO_KHR) tear = find_tear_mode(modes, info.minImageCount); | 650 | if (vsync && mode == VK_PRESENT_MODE_FIFO_KHR) tear = find_tear_mode(modes, info.minImageCount, info.imageUsage, info.imageExtent); |
| 651 | if (tear != VK_PRESENT_MODE_MAX_ENUM_KHR) { | 651 | if (tear != VK_PRESENT_MODE_MAX_ENUM_KHR) { |
| 652 | switchable_modes[1] = tear; | 652 | switchable_modes[1] = tear; |
| 653 | switchable.presentModeCount = 2; | 653 | switchable.presentModeCount = 2; |
| @@ -702,8 +702,10 @@ private: | |||
| 702 | #ifdef LEONIDA_SWAPCHAIN_MAINTENANCE1 | 702 | #ifdef LEONIDA_SWAPCHAIN_MAINTENANCE1 |
| 703 | // The mode a FIFO swapchain can also present in, so that a late frame can be shown at once: IMMEDIATE, else | 703 | // The mode a FIFO swapchain can also present in, so that a late frame can be shown at once: IMMEDIATE, else |
| 704 | // FIFO_RELAXED, each only if the surface lists it and reports it compatible with FIFO (VK_KHR_surface_maintenance1). | 704 | // FIFO_RELAXED, each only if the surface lists it and reports it compatible with FIFO (VK_KHR_surface_maintenance1). |
| 705 | // `min_images` is raised to what that mode needs too. MAX_ENUM when there is none or the extension is not in use. | 705 | // `min_images` is raised to what that mode needs too. The mode must also accept the swapchain's image usage and |
| 706 | VkPresentModeKHR find_tear_mode(const std::vector<VkPresentModeKHR>& modes, std::uint32_t& min_images) { | 706 | // extent (its capabilities can differ from FIFO's). MAX_ENUM when there is none or the extension is not in use. |
| 707 | VkPresentModeKHR find_tear_mode(const std::vector<VkPresentModeKHR>& modes, std::uint32_t& min_images, | ||
| 708 | VkImageUsageFlags usage, VkExtent2D extent) { | ||
| 707 | if (!maintenance1_ || !vkGetPhysicalDeviceSurfaceCapabilities2KHR_) return VK_PRESENT_MODE_MAX_ENUM_KHR; | 709 | if (!maintenance1_ || !vkGetPhysicalDeviceSurfaceCapabilities2KHR_) return VK_PRESENT_MODE_MAX_ENUM_KHR; |
| 708 | // The capabilities of the surface for one present mode, and the modes it can switch to from there. | 710 | // The capabilities of the surface for one present mode, and the modes it can switch to from there. |
| 709 | const auto ask = [&](VkPresentModeKHR asked, VkSurfaceCapabilitiesKHR& caps, | 711 | const auto ask = [&](VkPresentModeKHR asked, VkSurfaceCapabilitiesKHR& caps, |
| @@ -734,6 +736,10 @@ private: | |||
| 734 | continue; | 736 | continue; |
| 735 | VkSurfaceCapabilitiesKHR mode_caps{}; | 737 | VkSurfaceCapabilitiesKHR mode_caps{}; |
| 736 | if (!ask(wanted, mode_caps, nullptr, nullptr)) continue; | 738 | if (!ask(wanted, mode_caps, nullptr, nullptr)) continue; |
| 739 | if ((mode_caps.supportedUsageFlags & usage) != usage) continue; | ||
| 740 | if (extent.width < mode_caps.minImageExtent.width || extent.height < mode_caps.minImageExtent.height || | ||
| 741 | extent.width > mode_caps.maxImageExtent.width || extent.height > mode_caps.maxImageExtent.height) | ||
| 742 | continue; | ||
| 737 | // One image count that every mode of the swapchain accepts. | 743 | // One image count that every mode of the swapchain accepts. |
| 738 | std::uint32_t count = std::max({min_images, fifo_caps.minImageCount, mode_caps.minImageCount}); | 744 | std::uint32_t count = std::max({min_images, fifo_caps.minImageCount, mode_caps.minImageCount}); |
| 739 | const std::uint32_t most = std::min(fifo_caps.maxImageCount ? fifo_caps.maxImageCount : UINT32_MAX, | 745 | const std::uint32_t most = std::min(fifo_caps.maxImageCount ? fifo_caps.maxImageCount : UINT32_MAX, |