diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 09:15:00 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 09:15:00 -0500 |
| commit | 3430786f75ef7de27e8327643f8d737897e23623 (patch) | |
| tree | 13404215968bbb57ce805f4df1d21614169bbd62 /runtime/src/vulkan_present.h | |
| parent | 445e79b2c7a0fc02737d23cfac48f9ed025ed32a (diff) | |
| parent | db9b0904f0a8af502e3300c8a36572fa9194f4b7 (diff) | |
Merge pull request #1744 from Emirhan Karaca
Diffstat (limited to 'runtime/src/vulkan_present.h')
| -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 35f1cb556..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 | ||
| 41 | namespace leonida { | 40 | namespace leonida { |
| @@ -271,25 +270,34 @@ public: | |||
| 271 | 270 | ||
| 272 | bool recorded = false; | 271 | bool recorded = false; |
| 273 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); | 272 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); |
| 274 | std::optional<LayoutTracker> next_tracker; | 273 | bool rollback_source = false; |
| 274 | VkImageLayout prev_layout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 275 | if (binding) { | 275 | if (binding) { |
| 276 | // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). | 276 | // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). |
| 277 | // 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 |
| 278 | // 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. |
| 279 | std::shared_lock resource_lock(context_.resource_mutex_); | 279 | std::shared_lock resource_lock(context_.resource_mutex_); |
| 280 | const ResourceBinding* current = context_.resolve(source); | 280 | const ResourceBinding* current = context_.resolve(source); |
| 281 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { | 281 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { |
| 282 | tracker_lock.lock(); | 282 | tracker_lock.lock(); |
| 283 | next_tracker.emplace(context_.tracker_); | 283 | rollback_source = context_.tracker_.tracked(source); |
| 284 | 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); | ||
| 285 | recorded = true; | 286 | recorded = true; |
| 286 | } | 287 | } |
| 287 | } else { | 288 | } else { |
| 288 | record_blank(frame.commands, image); | 289 | record_blank(frame.commands, image); |
| 289 | recorded = true; | 290 | recorded = true; |
| 290 | } | 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 | }; | ||
| 291 | 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() |
| 292 | 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 | } | ||
| 293 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; | 301 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 294 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | 302 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
| 295 | submit_info.waitSemaphoreCount = 1; | 303 | submit_info.waitSemaphoreCount = 1; |
| @@ -322,10 +330,12 @@ public: | |||
| 322 | { | 330 | { |
| 323 | std::lock_guard lock(context_.submit_mutex()); | 331 | std::lock_guard lock(context_.submit_mutex()); |
| 324 | 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(); | ||
| 325 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); | 337 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); |
| 326 | } | 338 | } |
| 327 | if (submitted == VK_SUCCESS && next_tracker) context_.tracker_ = std::move(*next_tracker); | ||
| 328 | if (tracker_lock.owns_lock()) tracker_lock.unlock(); | ||
| 329 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); | 339 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); |
| 330 | ++frame_index_; | 340 | ++frame_index_; |
| 331 | // 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 |
| @@ -401,7 +411,7 @@ private: | |||
| 401 | } | 411 | } |
| 402 | 412 | ||
| 403 | 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, |
| 404 | const PresentRect* source_rect, LayoutTracker& tracker) { | 414 | const PresentRect* source_rect) { |
| 405 | auto& vk = context_.vk; | 415 | auto& vk = context_.vk; |
| 406 | const ReplayFunctions& fn = context_.replay_; | 416 | const ReplayFunctions& fn = context_.replay_; |
| 407 | vk.vkResetCommandBuffer(commands, 0); | 417 | vk.vkResetCommandBuffer(commands, 0); |
| @@ -414,8 +424,8 @@ private: | |||
| 414 | // 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 |
| 415 | // zero-filled first, as any committed resource is. | 425 | // zero-filled first, as any committed resource is. |
| 416 | const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); | 426 | const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); |
| 417 | auto pieces = detail::current(&tracker, source, range, common); | 427 | auto pieces = detail::current(&context_.tracker_, source, range, common); |
| 418 | detail::initialize(fn, commands, binding, source, pieces, common, &tracker); | 428 | detail::initialize(fn, commands, binding, source, pieces, common, &context_.tracker_); |
| 419 | for (const auto& piece : pieces) | 429 | for (const auto& piece : pieces) |
| 420 | detail::image_barrier(fn, commands, binding, piece.range, piece.layout, | 430 | detail::image_barrier(fn, commands, binding, piece.range, piece.layout, |
| 421 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, | 431 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, |