diff options
| author | Emirhan Karaca <emirhan.karaca@std.yildiz.edu.tr> | 2026-11-22 16:47:54 +0300 |
|---|---|---|
| committer | Emirhan Karaca <emirhan.karaca@std.yildiz.edu.tr> | 2026-11-22 16:47:54 +0300 |
| commit | db9b0904f0a8af502e3300c8a36572fa9194f4b7 (patch) | |
| tree | b12984c134b96b2bcb0a4fe4ee6cc7637d6f8c7e /runtime/src | |
| parent | 89b047cd8fab50675a43d76722a6a8a881f55dc2 (diff) | |
Runtime: O(1) LayoutTracker rollback and unlock tracker_mutex before vkQueuePresentKHR in VulkanPresenter
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_present.h | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 5d59a301f..2974e30da 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 | ||
| 41 | namespace leonida { | 40 | namespace leonida { |
| @@ -270,25 +269,34 @@ public: | |||
| 270 | 269 | ||
| 271 | bool recorded = false; | 270 | bool recorded = false; |
| 272 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); | 271 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); |
| 273 | std::optional<LayoutTracker> next_tracker; | 272 | bool rollback_source = false; |
| 273 | VkImageLayout prev_layout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 274 | if (binding) { | 274 | if (binding) { |
| 275 | // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). | 275 | // 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 | 276 | // 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. | 277 | // rolls back a failed submit in O(1) without copying the entire LayoutTracker map. |
| 278 | std::shared_lock resource_lock(context_.resource_mutex_); | 278 | std::shared_lock resource_lock(context_.resource_mutex_); |
| 279 | const ResourceBinding* current = context_.resolve(source); | 279 | const ResourceBinding* current = context_.resolve(source); |
| 280 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { | 280 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { |
| 281 | tracker_lock.lock(); | 281 | tracker_lock.lock(); |
| 282 | next_tracker.emplace(context_.tracker_); | 282 | rollback_source = context_.tracker_.tracked(source); |
| 283 | record(frame.commands, *current, source, image, source_rect, *next_tracker); | 283 | if (rollback_source) prev_layout = context_.tracker_.layout(source, 0); |
| 284 | record(frame.commands, *current, source, image, source_rect); | ||
| 284 | recorded = true; | 285 | recorded = true; |
| 285 | } | 286 | } |
| 286 | } else { | 287 | } else { |
| 287 | record_blank(frame.commands, image); | 288 | record_blank(frame.commands, image); |
| 288 | recorded = true; | 289 | recorded = true; |
| 289 | } | 290 | } |
| 291 | const auto rollback = [&] { | ||
| 292 | if (rollback_source) | ||
| 293 | context_.tracker_.set(source, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, prev_layout); | ||
| 294 | }; | ||
| 290 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() | 295 | 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); | 296 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) { |
| 297 | rollback(); | ||
| 298 | return stop(reset); | ||
| 299 | } | ||
| 292 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; | 300 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 293 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | 301 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
| 294 | submit_info.waitSemaphoreCount = 1; | 302 | submit_info.waitSemaphoreCount = 1; |
| @@ -321,10 +329,12 @@ public: | |||
| 321 | { | 329 | { |
| 322 | std::lock_guard lock(context_.submit_mutex()); | 330 | std::lock_guard lock(context_.submit_mutex()); |
| 323 | submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); | 331 | submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); |
| 332 | if (submitted != VK_SUCCESS) rollback(); | ||
| 333 | // Commit/rollback happened before releasing tracker_mutex_ inside submit_mutex(), preserving submission | ||
| 334 | // order without holding tracker_mutex_ across vkQueuePresentKHR. | ||
| 335 | if (tracker_lock.owns_lock()) tracker_lock.unlock(); | ||
| 324 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); | 336 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); |
| 325 | } | 337 | } |
| 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); | 338 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); |
| 329 | ++frame_index_; | 339 | ++frame_index_; |
| 330 | // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain | 340 | // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain |
| @@ -400,7 +410,7 @@ private: | |||
| 400 | } | 410 | } |
| 401 | 411 | ||
| 402 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, | 412 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, |
| 403 | const PresentRect* source_rect, LayoutTracker& tracker) { | 413 | const PresentRect* source_rect) { |
| 404 | auto& vk = context_.vk; | 414 | auto& vk = context_.vk; |
| 405 | const ReplayFunctions& fn = context_.replay_; | 415 | const ReplayFunctions& fn = context_.replay_; |
| 406 | vk.vkResetCommandBuffer(commands, 0); | 416 | vk.vkResetCommandBuffer(commands, 0); |
| @@ -413,8 +423,8 @@ private: | |||
| 413 | // PRESENT is D3D12's common state: GENERAL for an ordinary texture. A never-written back buffer is | 423 | // 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. | 424 | // zero-filled first, as any committed resource is. |
| 415 | const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); | 425 | const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); |
| 416 | auto pieces = detail::current(&tracker, source, range, common); | 426 | auto pieces = detail::current(&context_.tracker_, source, range, common); |
| 417 | detail::initialize(fn, commands, binding, source, pieces, common, &tracker); | 427 | detail::initialize(fn, commands, binding, source, pieces, common, &context_.tracker_); |
| 418 | for (const auto& piece : pieces) | 428 | for (const auto& piece : pieces) |
| 419 | detail::image_barrier(fn, commands, binding, piece.range, piece.layout, | 429 | detail::image_barrier(fn, commands, binding, piece.range, piece.layout, |
| 420 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, | 430 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, |