diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 02:04:12 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 05:28:34 +0100 |
| commit | 7677932bdf508e9d9be23927e6611ac77d4ba0d2 (patch) | |
| tree | ce26b8a0ad283e9d58c7f4953625fbcbd624629a /runtime/src/vulkan_present.h | |
| parent | 5241cf2cfec1e7fbc76125e29a2f3a3f5ad62c1f (diff) | |
presenter: format-feature checks, srgb state set after recreate, abortable acquire
Blit support comes from vkGetPhysicalDeviceFormatProperties (BLIT_SRC source,
BLIT_DST swapchain, linear filter only with SAMPLED_IMAGE_FILTER_LINEAR), cached
per format. The swapchain encoding is recorded only once the new swapchain
exists, so a failed recreate is retried. The image acquire polls an abort
callback every 50 ms instead of waiting for ever.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_present.h')
| -rw-r--r-- | runtime/src/vulkan_present.h | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 0ac9f1be7..ea7b0aaac 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -22,6 +22,8 @@ | |||
| 22 | #include <cstdint> | 22 | #include <cstdint> |
| 23 | #include <cstdio> | 23 | #include <cstdio> |
| 24 | #include <cstring> | 24 | #include <cstring> |
| 25 | #include <functional> | ||
| 26 | #include <map> | ||
| 25 | #include <vector> | 27 | #include <vector> |
| 26 | 28 | ||
| 27 | namespace leonida { | 29 | namespace leonida { |
| @@ -70,6 +72,7 @@ public: | |||
| 70 | LEONIDA_INSTANCE_FN(vkDestroySurfaceKHR) | 72 | LEONIDA_INSTANCE_FN(vkDestroySurfaceKHR) |
| 71 | LEONIDA_INSTANCE_FN(vkGetDeviceProcAddr) | 73 | LEONIDA_INSTANCE_FN(vkGetDeviceProcAddr) |
| 72 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceMemoryProperties) | 74 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceMemoryProperties) |
| 75 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceFormatProperties) | ||
| 73 | #undef LEONIDA_INSTANCE_FN | 76 | #undef LEONIDA_INSTANCE_FN |
| 74 | if (!vkGetDeviceProcAddr_ || !vkDestroySurfaceKHR_) return; | 77 | if (!vkGetDeviceProcAddr_ || !vkDestroySurfaceKHR_) return; |
| 75 | const VkDevice device = context.device(); | 78 | const VkDevice device = context.device(); |
| @@ -110,7 +113,7 @@ public: | |||
| 110 | vk.vkCreateSemaphore(device, &semaphore, nullptr, &frame.acquired) != VK_SUCCESS) | 113 | vk.vkCreateSemaphore(device, &semaphore, nullptr, &frame.acquired) != VK_SUCCESS) |
| 111 | return; | 114 | return; |
| 112 | } | 115 | } |
| 113 | ok_ = recreate(true); | 116 | ok_ = recreate(true, false); |
| 114 | } | 117 | } |
| 115 | 118 | ||
| 116 | ~VulkanPresenter() { | 119 | ~VulkanPresenter() { |
| @@ -160,7 +163,10 @@ public: | |||
| 160 | if (!found || !found->image || found->aspects != VK_IMAGE_ASPECT_COLOR_BIT) return VK_ERROR_UNKNOWN; | 163 | if (!found || !found->image || found->aspects != VK_IMAGE_ASPECT_COLOR_BIT) return VK_ERROR_UNKNOWN; |
| 161 | binding = *found; | 164 | binding = *found; |
| 162 | } | 165 | } |
| 163 | if (binding.samples > 1 || detail::blit_unsupported_format(binding.format)) | 166 | // Multisampled sources (no resolve into a temporary yet), integer formats (a UNORM swapchain cannot take them) |
| 167 | // and anything the GPU cannot blit from are refused. | ||
| 168 | if (binding.samples > 1 || detail::blit_unsupported_format(binding.format) || | ||
| 169 | !(format_features(binding.format) & VK_FORMAT_FEATURE_BLIT_SRC_BIT)) | ||
| 164 | return VK_ERROR_FORMAT_NOT_SUPPORTED; | 170 | return VK_ERROR_FORMAT_NOT_SUPPORTED; |
| 165 | if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right || | 171 | if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right || |
| 166 | source_rect->top >= source_rect->bottom || | 172 | source_rect->top >= source_rect->bottom || |
| @@ -170,8 +176,12 @@ public: | |||
| 170 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format)); | 176 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format)); |
| 171 | } | 177 | } |
| 172 | 178 | ||
| 179 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with | ||
| 180 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. | ||
| 181 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } | ||
| 182 | |||
| 173 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. | 183 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. |
| 174 | VkResult present_blank(bool vsync) { return submit(nullptr, ResourceId{}, vsync, nullptr, want_srgb_); } | 184 | VkResult present_blank(bool vsync) { return submit(nullptr, ResourceId{}, vsync, nullptr, swapchain_srgb_); } |
| 175 | 185 | ||
| 176 | // From now on every presented frame is also copied to host memory (the swapchain gets TRANSFER_SRC usage), for | 186 | // From now on every presented frame is also copied to host memory (the swapchain gets TRANSFER_SRC usage), for |
| 177 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. | 187 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. |
| @@ -180,7 +190,7 @@ public: | |||
| 180 | if (!ok_) return false; | 190 | if (!ok_) return false; |
| 181 | capture_ = true; | 191 | capture_ = true; |
| 182 | stale_ = true; | 192 | stale_ = true; |
| 183 | if (!recreate(vsync_) || (swapchain_ && !capture_buffer_)) { | 193 | if (!recreate(vsync_, swapchain_srgb_) || (swapchain_ && !capture_buffer_)) { |
| 184 | capture_ = false; | 194 | capture_ = false; |
| 185 | return false; | 195 | return false; |
| 186 | } | 196 | } |
| @@ -203,15 +213,17 @@ public: | |||
| 203 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | 213 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, |
| 204 | bool srgb) { | 214 | bool srgb) { |
| 205 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 215 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 206 | if (vsync != vsync_ || stale_ || srgb != want_srgb_) { | 216 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_) { |
| 207 | want_srgb_ = srgb; | 217 | // The swapchain's colour encoding is only recorded once the new swapchain exists, so a failed |
| 208 | if (!recreate(vsync)) return VK_ERROR_OUT_OF_DATE_KHR; | 218 | // recreate is tried again at the next present. |
| 219 | if (!recreate(vsync, srgb)) return VK_ERROR_OUT_OF_DATE_KHR; | ||
| 209 | } | 220 | } |
| 210 | if (!swapchain_) { // minimised: try again next frame | 221 | if (!swapchain_) { // minimised: try again next frame |
| 211 | stale_ = true; | 222 | stale_ = true; |
| 212 | return VK_SUCCESS; | 223 | return VK_SUCCESS; |
| 213 | } | 224 | } |
| 214 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it | 225 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it |
| 226 | if (binding && !(format_features(format_) & VK_FORMAT_FEATURE_BLIT_DST_BIT)) return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 215 | auto& vk = context_.vk; | 227 | auto& vk = context_.vk; |
| 216 | const VkDevice device = context_.device(); | 228 | const VkDevice device = context_.device(); |
| 217 | Frame& frame = frames_[frame_index_ % frames_.size()]; | 229 | Frame& frame = frames_[frame_index_ % frames_.size()]; |
| @@ -219,11 +231,11 @@ public: | |||
| 219 | return stop(waited); | 231 | return stop(waited); |
| 220 | 232 | ||
| 221 | std::uint32_t image = 0; | 233 | std::uint32_t image = 0; |
| 222 | VkResult acquired = vkAcquireNextImageKHR_(device, swapchain_, UINT64_MAX, frame.acquired, VK_NULL_HANDLE, &image); | 234 | VkResult acquired = acquire(frame.acquired, &image); |
| 223 | if (acquired == VK_ERROR_OUT_OF_DATE_KHR) { | 235 | if (acquired == VK_ERROR_OUT_OF_DATE_KHR) { |
| 224 | if (!recreate(vsync)) return acquired; | 236 | if (!recreate(vsync, swapchain_srgb_)) return acquired; |
| 225 | if (!swapchain_) return VK_SUCCESS; | 237 | if (!swapchain_) return VK_SUCCESS; |
| 226 | acquired = vkAcquireNextImageKHR_(device, swapchain_, UINT64_MAX, frame.acquired, VK_NULL_HANDLE, &image); | 238 | acquired = acquire(frame.acquired, &image); |
| 227 | } | 239 | } |
| 228 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired | 240 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired |
| 229 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; | 241 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; |
| @@ -374,7 +386,7 @@ private: | |||
| 374 | blit.dstOffsets[1] = {dw, dh, 1}; | 386 | blit.dstOffsets[1] = {dw, dh, 1}; |
| 375 | vkCmdBlitImage_(commands, binding.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], | 387 | vkCmdBlitImage_(commands, binding.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], |
| 376 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, | 388 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 377 | sw == dw && sh == dh ? VK_FILTER_NEAREST : VK_FILTER_LINEAR); | 389 | sw == dw && sh == dh ? VK_FILTER_NEAREST : blit_filter(binding.format)); |
| 378 | 390 | ||
| 379 | finish_image(commands, image); | 391 | finish_image(commands, image); |
| 380 | for (const auto& piece : pieces) | 392 | for (const auto& piece : pieces) |
| @@ -384,6 +396,29 @@ private: | |||
| 384 | vk.vkEndCommandBuffer(commands); | 396 | vk.vkEndCommandBuffer(commands); |
| 385 | } | 397 | } |
| 386 | 398 | ||
| 399 | // vkGetPhysicalDeviceFormatProperties, optimal tiling, cached per format. | ||
| 400 | VkFormatFeatureFlags format_features(VkFormat format) { | ||
| 401 | const auto it = features_.find(format); | ||
| 402 | if (it != features_.end()) return it->second; | ||
| 403 | VkFormatProperties properties{}; | ||
| 404 | if (vkGetPhysicalDeviceFormatProperties_) vkGetPhysicalDeviceFormatProperties_(physical_, format, &properties); | ||
| 405 | return features_[format] = properties.optimalTilingFeatures; | ||
| 406 | } | ||
| 407 | // Scaling blits filter bilinearly when the source format allows it, else take the nearest texel. | ||
| 408 | VkFilter blit_filter(VkFormat source) { | ||
| 409 | return (format_features(source) & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) ? VK_FILTER_LINEAR | ||
| 410 | : VK_FILTER_NEAREST; | ||
| 411 | } | ||
| 412 | |||
| 413 | VkResult acquire(VkSemaphore semaphore, std::uint32_t* image) { | ||
| 414 | for (;;) { | ||
| 415 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, | ||
| 416 | VK_NULL_HANDLE, image); | ||
| 417 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; | ||
| 418 | if (abort_ && abort_()) return VK_NOT_READY; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 387 | // Clears the swapchain image to opaque black. | 422 | // Clears the swapchain image to opaque black. |
| 388 | void record_blank(VkCommandBuffer commands, std::uint32_t image) { | 423 | void record_blank(VkCommandBuffer commands, std::uint32_t image) { |
| 389 | auto& vk = context_.vk; | 424 | auto& vk = context_.vk; |
| @@ -477,7 +512,7 @@ private: | |||
| 477 | // swapchain and is retried on the next present. Nothing is published until every piece exists: on any failure | 512 | // swapchain and is retried on the next present. Nothing is published until every piece exists: on any failure |
| 478 | // the presenter is left with no swapchain and stale_ set, so the next present tries again, and no image is ever | 513 | // the presenter is left with no swapchain and stale_ set, so the next present tries again, and no image is ever |
| 479 | // acquired without its per-image semaphore. | 514 | // acquired without its per-image semaphore. |
| 480 | bool recreate(bool vsync) { | 515 | bool recreate(bool vsync, bool srgb) { |
| 481 | auto& vk = context_.vk; | 516 | auto& vk = context_.vk; |
| 482 | const VkDevice device = context_.device(); | 517 | const VkDevice device = context_.device(); |
| 483 | { | 518 | { |
| @@ -492,6 +527,7 @@ private: | |||
| 492 | if (caps.currentExtent.width == 0 || caps.currentExtent.height == 0) { | 527 | if (caps.currentExtent.width == 0 || caps.currentExtent.height == 0) { |
| 493 | release(old); | 528 | release(old); |
| 494 | vsync_ = vsync; | 529 | vsync_ = vsync; |
| 530 | swapchain_srgb_ = srgb; | ||
| 495 | stale_ = false; | 531 | stale_ = false; |
| 496 | return true; | 532 | return true; |
| 497 | } | 533 | } |
| @@ -509,7 +545,7 @@ private: | |||
| 509 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. | 545 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. |
| 510 | VkSurfaceFormatKHR chosen = formats[0]; | 546 | VkSurfaceFormatKHR chosen = formats[0]; |
| 511 | bool found = false; | 547 | bool found = false; |
| 512 | for (const VkFormat wanted : want_srgb_ ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} | 548 | for (const VkFormat wanted : srgb ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} |
| 513 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { | 549 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { |
| 514 | for (const auto& f : formats) | 550 | for (const auto& f : formats) |
| 515 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | 551 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { |
| @@ -519,7 +555,7 @@ private: | |||
| 519 | } | 555 | } |
| 520 | if (found) break; | 556 | if (found) break; |
| 521 | } | 557 | } |
| 522 | if (!found && want_srgb_) { | 558 | if (!found && srgb) { |
| 523 | if (!warned_srgb_) std::fprintf(stderr, "[rt] present: the surface has no sRGB 8-bit format; an sRGB back buffer is shown darker\n"); | 559 | if (!warned_srgb_) std::fprintf(stderr, "[rt] present: the surface has no sRGB 8-bit format; an sRGB back buffer is shown darker\n"); |
| 524 | warned_srgb_ = true; | 560 | warned_srgb_ = true; |
| 525 | for (const auto& f : formats) | 561 | for (const auto& f : formats) |
| @@ -595,6 +631,7 @@ private: | |||
| 595 | if (!four_bytes || !make_capture_buffer()) return false; | 631 | if (!four_bytes || !make_capture_buffer()) return false; |
| 596 | } | 632 | } |
| 597 | vsync_ = vsync; | 633 | vsync_ = vsync; |
| 634 | swapchain_srgb_ = srgb; | ||
| 598 | stale_ = false; | 635 | stale_ = false; |
| 599 | return true; | 636 | return true; |
| 600 | } | 637 | } |
| @@ -626,7 +663,9 @@ private: | |||
| 626 | bool vsync_ = true; | 663 | bool vsync_ = true; |
| 627 | bool stale_ = false; | 664 | bool stale_ = false; |
| 628 | bool ok_ = false; | 665 | bool ok_ = false; |
| 629 | bool want_srgb_ = false; // the swapchain format follows the source being presented | 666 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) |
| 667 | std::function<bool()> abort_; | ||
| 668 | std::map<VkFormat, VkFormatFeatureFlags> features_; | ||
| 630 | bool warned_srgb_ = false; | 669 | bool warned_srgb_ = false; |
| 631 | bool capture_ = false; | 670 | bool capture_ = false; |
| 632 | VkBuffer capture_buffer_ = VK_NULL_HANDLE; | 671 | VkBuffer capture_buffer_ = VK_NULL_HANDLE; |
| @@ -651,6 +690,7 @@ private: | |||
| 651 | PFN_vkCmdBlitImage vkCmdBlitImage_ = nullptr; | 690 | PFN_vkCmdBlitImage vkCmdBlitImage_ = nullptr; |
| 652 | PFN_vkCmdClearColorImage vkCmdClearColorImage_ = nullptr; | 691 | PFN_vkCmdClearColorImage vkCmdClearColorImage_ = nullptr; |
| 653 | PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties_ = nullptr; | 692 | PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties_ = nullptr; |
| 693 | PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties_ = nullptr; | ||
| 654 | PFN_vkCreateFence vkCreateFence_ = nullptr; | 694 | PFN_vkCreateFence vkCreateFence_ = nullptr; |
| 655 | PFN_vkDestroyFence vkDestroyFence_ = nullptr; | 695 | PFN_vkDestroyFence vkDestroyFence_ = nullptr; |
| 656 | PFN_vkWaitForFences vkWaitForFences_ = nullptr; | 696 | PFN_vkWaitForFences vkWaitForFences_ = nullptr; |