upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_backend.h
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 10:39:32 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 10:39:32 -0500
commit8b61d266bba4e5a5d4b553b81e7c8d66c644bfba (patch)
tree3278f334a28108aea1a812aa6055cb07c22ae76d /runtime/src/vulkan_backend.h
parent525edbc9b749e941d523ec4cf9e7a37bd8852126 (diff)
parentb01bed6efdee5823e94b90643075e4560c5ed70b (diff)
Merge pull request #1933 from Emirhan Karaca
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h165
1 files changed, 120 insertions, 45 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index db14934c1..93d7e5259 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -500,56 +500,79 @@ public:
500 500
501 void enqueue(VkSemaphore sem, std::uint64_t value, void* event_handle) { 501 void enqueue(VkSemaphore sem, std::uint64_t value, void* event_handle) {
502 if (!sem || !event_handle) return; 502 if (!sem || !event_handle) return;
503 if (device_lost_.load(std::memory_order_acquire)) { 503 SignalEventFn immediate_fn = nullptr;
504 SignalEventFn fn = nullptr;
505 {
506 std::lock_guard lock(mutex_);
507 fn = signal_event_;
508 }
509 if (fn) fn(event_handle);
510 return;
511 }
512 { 504 {
513 std::lock_guard lock(mutex_); 505 std::lock_guard lock(mutex_);
514 pending_.push_back({sem, value, event_handle, {}}); 506 if (stop_ || device_lost_.load(std::memory_order_acquire)) {
515 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); }); 507 immediate_fn = signal_event_;
516 signal_wakeup_locked(); 508 } else {
509 pending_.push_back({sem, value, event_handle, {}});
510 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
511 signal_wakeup_locked();
512 }
513 }
514 if (immediate_fn) {
515 immediate_fn(event_handle);
516 return;
517 } 517 }
518 cv_.notify_one(); 518 cv_.notify_one();
519 } 519 }
520 520
521 void enqueue_callback(VkSemaphore sem, std::uint64_t value, std::function<void()> cb) { 521 void enqueue_callback(VkSemaphore sem, std::uint64_t value, std::function<void()> cb) {
522 if (!sem || !cb) return; 522 if (!sem || !cb) return;
523 if (device_lost_.load(std::memory_order_acquire)) { 523 std::function<void()> immediate_cb;
524 cb(); 524 {
525 std::lock_guard lock(mutex_);
526 if (stop_ || device_lost_.load(std::memory_order_acquire)) {
527 immediate_cb = std::move(cb);
528 } else {
529 pending_.push_back({sem, value, nullptr, std::move(cb)});
530 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
531 signal_wakeup_locked();
532 }
533 }
534 if (immediate_cb) {
535 immediate_cb();
525 return; 536 return;
526 } 537 }
538 cv_.notify_one();
539 }
540
541 // Wakes run() when another path marks the device lost so pending waits complete and stop()/cancel() never hang.
542 void notify_device_lost() {
527 { 543 {
528 std::lock_guard lock(mutex_); 544 std::lock_guard lock(mutex_);
529 pending_.push_back({sem, value, nullptr, std::move(cb)});
530 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
531 signal_wakeup_locked(); 545 signal_wakeup_locked();
532 } 546 }
533 cv_.notify_one(); 547 cv_.notify_all();
534 } 548 }
535 549
536 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and 550 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and
537 // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore. 551 // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore.
538 void cancel(VkSemaphore sem) { 552 void cancel(VkSemaphore sem) {
539 if (!sem) return; 553 if (!sem) return;
540 std::unique_lock lock(mutex_); 554 std::vector<Entry> cancelled;
541 std::erase_if(pending_, [this, sem](const Entry& e) { 555 SignalEventFn fn = nullptr;
542 if (e.semaphore != sem) return false; 556 {
543 if (e.callback) e.callback(); 557 std::unique_lock lock(mutex_);
544 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle); 558 fn = signal_event_;
545 return true; 559 std::size_t write = 0;
546 }); 560 for (std::size_t i = 0; i < pending_.size(); ++i) {
547 if (!thread_.joinable()) return; 561 if (pending_[i].semaphore == sem) {
548 if (waiting_) { 562 cancelled.push_back(std::move(pending_[i]));
549 const std::uint64_t target_epoch = epoch_ + 1; 563 } else {
550 signal_wakeup_locked(); 564 if (write != i) pending_[write] = std::move(pending_[i]);
551 epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; }); 565 ++write;
566 }
567 }
568 pending_.resize(write);
569 if (thread_.joinable() && waiting_) {
570 const std::uint64_t target_epoch = epoch_ + 1;
571 signal_wakeup_locked();
572 epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; });
573 }
552 } 574 }
575 dispatch_entries(cancelled, fn);
553 } 576 }
554 577
555 void stop() { 578 void stop() {
@@ -564,10 +587,18 @@ public:
564 } 587 }
565 588
566private: 589private:
590 static void dispatch_entries(std::vector<Entry>& entries, SignalEventFn signal_event) {
591 for (Entry& e : entries) {
592 if (e.callback) e.callback();
593 else if (signal_event && e.event_handle) signal_event(e.event_handle);
594 }
595 }
596
567 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1, 597 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1,
568 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. 598 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if
599 // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop().
569 void signal_wakeup_locked() { 600 void signal_wakeup_locked() {
570 if (!waiting_ || !wakeup_sem_ || device_lost_.load(std::memory_order_relaxed)) return; 601 if (!waiting_ || !wakeup_sem_) return;
571 const std::uint64_t next = ++wakeup_val_; 602 const std::uint64_t next = ++wakeup_val_;
572 VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; 603 VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO};
573 si.semaphore = wakeup_sem_; 604 si.semaphore = wakeup_sem_;
@@ -578,7 +609,15 @@ private:
578 void run() { 609 void run() {
579 std::unique_lock lock(mutex_); 610 std::unique_lock lock(mutex_);
580 while (true) { 611 while (true) {
581 cv_.wait(lock, [this] { return stop_ || !pending_.empty(); }); 612 cv_.wait(lock, [this] { return stop_ || !pending_.empty() || device_lost_.load(std::memory_order_acquire); });
613 if (device_lost_.load(std::memory_order_acquire)) {
614 std::vector<Entry> drained = std::move(pending_);
615 pending_.clear();
616 const SignalEventFn fn = signal_event_;
617 lock.unlock();
618 dispatch_entries(drained, fn);
619 return;
620 }
582 if (stop_) return; 621 if (stop_) return;
583 622
584 semaphores_.clear(); 623 semaphores_.clear();
@@ -605,25 +644,53 @@ private:
605 ++epoch_; 644 ++epoch_;
606 epoch_cv_.notify_all(); 645 epoch_cv_.notify_all();
607 646
608 if (res == VK_ERROR_DEVICE_LOST) { 647 if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) {
609 device_lost_.store(true, std::memory_order_release); 648 device_lost_.store(true, std::memory_order_release);
610 for (const Entry& e : pending_) { 649 std::vector<Entry> drained = std::move(pending_);
611 if (e.callback) e.callback();
612 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle);
613 }
614 pending_.clear(); 650 pending_.clear();
651 const SignalEventFn fn = signal_event_;
652 lock.unlock();
653 dispatch_entries(drained, fn);
615 return; 654 return;
616 } 655 }
617 if (stop_) return; 656 if (stop_) return;
618 657
619 std::erase_if(pending_, [this](const Entry& e) { 658 std::vector<Entry> ready;
659 bool lost = false;
660 std::size_t write = 0;
661 for (std::size_t i = 0; i < pending_.size(); ++i) {
620 std::uint64_t done = 0; 662 std::uint64_t done = 0;
621 if (vk_.vkGetSemaphoreCounterValue(device_, e.semaphore, &done) != VK_SUCCESS) return false; 663 const VkResult cr = vk_.vkGetSemaphoreCounterValue(device_, pending_[i].semaphore, &done);
622 if (done < e.value) return false; 664 if (cr == VK_ERROR_DEVICE_LOST) {
623 if (e.callback) e.callback(); 665 lost = true;
624 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle); 666 for (std::size_t j = i; j < pending_.size(); ++j) ready.push_back(std::move(pending_[j]));
625 return true; 667 break;
626 }); 668 }
669 if (cr == VK_SUCCESS && done >= pending_[i].value) {
670 ready.push_back(std::move(pending_[i]));
671 } else {
672 if (write != i) pending_[write] = std::move(pending_[i]);
673 ++write;
674 }
675 }
676 if (lost) {
677 pending_.resize(write);
678 for (Entry& e : pending_) ready.push_back(std::move(e));
679 pending_.clear();
680 device_lost_.store(true, std::memory_order_release);
681 const SignalEventFn fn = signal_event_;
682 lock.unlock();
683 dispatch_entries(ready, fn);
684 return;
685 }
686 pending_.resize(write);
687 if (!ready.empty()) {
688 const SignalEventFn fn = signal_event_;
689 lock.unlock();
690 dispatch_entries(ready, fn);
691 lock.lock();
692 if (stop_) return;
693 }
627 } 694 }
628 } 695 }
629 696
@@ -1126,7 +1193,15 @@ public:
1126 // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one. 1193 // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one.
1127 std::mutex& submit_mutex() { return submit_mutex_; } 1194 std::mutex& submit_mutex() { return submit_mutex_; }
1128 1195
1129 void mark_device_lost() noexcept { device_lost_.store(true, std::memory_order_release); } 1196 void mark_device_lost() noexcept {
1197 device_lost_.store(true, std::memory_order_release);
1198 if (fence_dispatcher_) {
1199 try {
1200 fence_dispatcher_->notify_device_lost();
1201 } catch (...) {
1202 }
1203 }
1204 }
1130 bool is_device_lost() const noexcept { return device_lost_.load(std::memory_order_acquire); } 1205 bool is_device_lost() const noexcept { return device_lost_.load(std::memory_order_acquire); }
1131 1206
1132 void set_signal_event_fn(FenceWaitDispatcher::SignalEventFn fn) { 1207 void set_signal_event_fn(FenceWaitDispatcher::SignalEventFn fn) {