upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_present.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/vulkan_present.h')
-rw-r--r--runtime/src/vulkan_present.h60
1 files changed, 43 insertions, 17 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h
index 5d59a301f..d9f53baa5 100644
--- a/runtime/src/vulkan_present.h
+++ b/runtime/src/vulkan_present.h
@@ -35,7 +35,6 @@
35#include <cstring> 35#include <cstring>
36#include <functional> 36#include <functional>
37#include <map> 37#include <map>
38#include <optional>
39#include <vector> 38#include <vector>
40 39
41namespace leonida { 40namespace leonida {
@@ -75,9 +74,10 @@ class VulkanPresenter {
75public: 74public:
76 VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, 75 VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance,
77 VkPhysicalDevice physical, VkQueue queue, std::uint32_t family, VkSurfaceKHR surface, 76 VkPhysicalDevice physical, VkQueue queue, std::uint32_t family, VkSurfaceKHR surface,
78 bool swapchain_maintenance1 = false) 77 bool swapchain_maintenance1 = false,
78 std::function<VkExtent2D()> extent_fn = nullptr)
79 : context_(context), instance_(instance), physical_(physical), queue_(queue), surface_(surface), 79 : context_(context), instance_(instance), physical_(physical), queue_(queue), surface_(surface),
80 maintenance1_(swapchain_maintenance1) { 80 maintenance1_(swapchain_maintenance1), extent_fn_(std::move(extent_fn)) {
81#define LEONIDA_INSTANCE_FN(name) name##_ = reinterpret_cast<PFN_##name>(get_instance(instance, #name)); 81#define LEONIDA_INSTANCE_FN(name) name##_ = reinterpret_cast<PFN_##name>(get_instance(instance, #name));
82 LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceSupportKHR) 82 LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceSupportKHR)
83 LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) 83 LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
@@ -270,25 +270,34 @@ public:
270 270
271 bool recorded = false; 271 bool recorded = false;
272 std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); 272 std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock);
273 std::optional<LayoutTracker> next_tracker; 273 bool rollback_source = false;
274 VkImageLayout prev_layout = VK_IMAGE_LAYOUT_UNDEFINED;
274 if (binding) { 275 if (binding) {
275 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). 276 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h).
276 // Release resource_mutex_ before vkQueueSubmit while keeping tracker_mutex_ held so layout updates 277 // record() only touches subresource 0 (mip 0, layer 0, color) of `source`, so saving that one layout
277 // commit in submission order and roll back if vkQueueSubmit fails. 278 // rolls back a failed submit in O(1) without copying the entire LayoutTracker map.
278 std::shared_lock resource_lock(context_.resource_mutex_); 279 std::shared_lock resource_lock(context_.resource_mutex_);
279 const ResourceBinding* current = context_.resolve(source); 280 const ResourceBinding* current = context_.resolve(source);
280 if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { 281 if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
281 tracker_lock.lock(); 282 tracker_lock.lock();
282 next_tracker.emplace(context_.tracker_); 283 rollback_source = context_.tracker_.tracked(source);
283 record(frame.commands, *current, source, image, source_rect, *next_tracker); 284 if (rollback_source) prev_layout = context_.tracker_.layout(source, 0);
285 record(frame.commands, *current, source, image, source_rect);
284 recorded = true; 286 recorded = true;
285 } 287 }
286 } else { 288 } else {
287 record_blank(frame.commands, image); 289 record_blank(frame.commands, image);
288 recorded = true; 290 recorded = true;
289 } 291 }
292 const auto rollback = [&] {
293 if (rollback_source)
294 context_.tracker_.set(source, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, prev_layout);
295 };
290 if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() 296 if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable()
291 if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) return stop(reset); 297 if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) {
298 rollback();
299 return stop(reset);
300 }
292 const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; 301 const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT;
293 VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; 302 VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO};
294 submit_info.waitSemaphoreCount = 1; 303 submit_info.waitSemaphoreCount = 1;
@@ -321,10 +330,12 @@ public:
321 { 330 {
322 std::lock_guard lock(context_.submit_mutex()); 331 std::lock_guard lock(context_.submit_mutex());
323 submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); 332 submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done);
333 if (submitted != VK_SUCCESS) rollback();
334 // Commit/rollback happened before releasing tracker_mutex_ inside submit_mutex(), preserving submission
335 // order without holding tracker_mutex_ across vkQueuePresentKHR.
336 if (tracker_lock.owns_lock()) tracker_lock.unlock();
324 if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); 337 if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present);
325 } 338 }
326 if (submitted == VK_SUCCESS && next_tracker) context_.tracker_ = std::move(*next_tracker);
327 if (tracker_lock.owns_lock()) tracker_lock.unlock();
328 if (submitted != VK_SUCCESS) return give_back(frame, submitted); 339 if (submitted != VK_SUCCESS) return give_back(frame, submitted);
329 ++frame_index_; 340 ++frame_index_;
330 // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain 341 // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain
@@ -400,7 +411,7 @@ private:
400 } 411 }
401 412
402 void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, 413 void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image,
403 const PresentRect* source_rect, LayoutTracker& tracker) { 414 const PresentRect* source_rect) {
404 auto& vk = context_.vk; 415 auto& vk = context_.vk;
405 const ReplayFunctions& fn = context_.replay_; 416 const ReplayFunctions& fn = context_.replay_;
406 vk.vkResetCommandBuffer(commands, 0); 417 vk.vkResetCommandBuffer(commands, 0);
@@ -413,8 +424,8 @@ private:
413 // PRESENT is D3D12's common state: GENERAL for an ordinary texture. A never-written back buffer is 424 // PRESENT is D3D12's common state: GENERAL for an ordinary texture. A never-written back buffer is
414 // zero-filled first, as any committed resource is. 425 // zero-filled first, as any committed resource is.
415 const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); 426 const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL);
416 auto pieces = detail::current(&tracker, source, range, common); 427 auto pieces = detail::current(&context_.tracker_, source, range, common);
417 detail::initialize(fn, commands, binding, source, pieces, common, &tracker); 428 detail::initialize(fn, commands, binding, source, pieces, common, &context_.tracker_);
418 for (const auto& piece : pieces) 429 for (const auto& piece : pieces)
419 detail::image_barrier(fn, commands, binding, piece.range, piece.layout, 430 detail::image_barrier(fn, commands, binding, piece.range, piece.layout,
420 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, 431 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT,
@@ -595,7 +606,21 @@ private:
595 tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; 606 tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR;
596 VkSurfaceCapabilitiesKHR caps{}; 607 VkSurfaceCapabilitiesKHR caps{};
597 if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR_(physical_, surface_, &caps) != VK_SUCCESS) return false; 608 if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR_(physical_, surface_, &caps) != VK_SUCCESS) return false;
598 if (caps.currentExtent.width == 0 || caps.currentExtent.height == 0) { 609 VkExtent2D extent = caps.currentExtent;
610 // When the platform leaves the swapchain extent to the application (0xFFFFFFFF, as on Intel UHD / Win32 WSI),
611 // query the window client area or fall back to the last known extent, and clamp to the surface bounds (D3D-165).
612 if (extent.width == 0xFFFFFFFF || extent.height == 0xFFFFFFFF) {
613 if (extent_fn_) extent = extent_fn_();
614 // ponytail: if no window extent function was supplied, fall back to last extent_ or minImageExtent
615 if (extent.width == 0xFFFFFFFF || extent.height == 0xFFFFFFFF || (extent.width == 0 && extent.height == 0)) {
616 extent = extent_.width && extent_.height ? extent_ : caps.minImageExtent;
617 }
618 if (extent.width && extent.height) {
619 extent.width = std::clamp(extent.width, caps.minImageExtent.width, caps.maxImageExtent.width);
620 extent.height = std::clamp(extent.height, caps.minImageExtent.height, caps.maxImageExtent.height);
621 }
622 }
623 if (extent.width == 0 || extent.height == 0) {
599 release(swapchain_); 624 release(swapchain_);
600 swapchain_ = VK_NULL_HANDLE; 625 swapchain_ = VK_NULL_HANDLE;
601 vsync_ = vsync; 626 vsync_ = vsync;
@@ -659,7 +684,7 @@ private:
659 if (caps.maxImageCount) info.minImageCount = std::min(info.minImageCount, caps.maxImageCount); 684 if (caps.maxImageCount) info.minImageCount = std::min(info.minImageCount, caps.maxImageCount);
660 info.imageFormat = chosen.format; 685 info.imageFormat = chosen.format;
661 info.imageColorSpace = chosen.colorSpace; 686 info.imageColorSpace = chosen.colorSpace;
662 info.imageExtent = caps.currentExtent; 687 info.imageExtent = extent;
663 info.imageArrayLayers = 1; 688 info.imageArrayLayers = 1;
664 info.imageUsage = usage; 689 info.imageUsage = usage;
665 info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; 690 info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
@@ -713,7 +738,7 @@ private:
713 images_ = std::move(images); 738 images_ = std::move(images);
714 ready_ = std::move(ready); 739 ready_ = std::move(ready);
715 format_ = chosen.format; 740 format_ = chosen.format;
716 extent_ = caps.currentExtent; 741 extent_ = extent;
717 if (capture_) { 742 if (capture_) {
718 // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. 743 // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set.
719 const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM || 744 const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM ||
@@ -812,6 +837,7 @@ private:
812 bool ok_ = false; 837 bool ok_ = false;
813 bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) 838 bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists)
814 std::function<bool()> abort_; 839 std::function<bool()> abort_;
840 std::function<VkExtent2D()> extent_fn_;
815 std::map<VkFormat, VkFormatFeatureFlags> features_; 841 std::map<VkFormat, VkFormatFeatureFlags> features_;
816 bool warned_srgb_ = false; 842 bool warned_srgb_ = false;
817 bool capture_ = false; 843 bool capture_ = false;