upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:12:44 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:12:44 -0500
commitdb317bd3ee92560dbf9e4a27183ebc8a478c4a78 (patch)
tree3c1d132409574fe1b04847692ece346ed4211071 /runtime/src
parent75bf835356d7bd61192f613224f9092586afcd0c (diff)
parent6670b2bca0d86361ac1d707f4bb0caf660aeec2e (diff)
Merge pull request #1947 from Jolymmiles
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/vulkan_backend.h57
-rw-r--r--runtime/src/vulkan_replay.h6
2 files changed, 62 insertions, 1 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 086b6772a..03028f7fc 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -81,7 +81,8 @@ struct VulkanDeviceFunctions {
81 X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ 81 X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \
82 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ 82 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
83 X(vkCmdSetBlendConstants) X(vkCmdDraw) \ 83 X(vkCmdSetBlendConstants) X(vkCmdDraw) \
84 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) 84 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) \
85 X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents)
85#define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; 86#define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr;
86 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) 87 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER)
87#undef LEONIDA_VK_MEMBER 88#undef LEONIDA_VK_MEMBER
@@ -1003,6 +1004,10 @@ public:
1003 // (timestampValidBits 0). Set by the device before any queue uses it. 1004 // (timestampValidBits 0). Set by the device before any queue uses it.
1004 double timestamp_period() const { return timestamp_period_; } 1005 double timestamp_period() const { return timestamp_period_; }
1005 void set_timestamp_period(double period) { timestamp_period_ = period; } 1006 void set_timestamp_period(double period) { timestamp_period_ = period; }
1007 // True where a timestamp resolve must be ordered explicitly after the timestamps it copies (MoltenVK on Apple
1008 // GPUs, docs/d3d12_vulkan.md section 4.6). Set by the device before any queue uses it.
1009 bool order_timestamp_copies() const { return order_timestamp_copies_; }
1010 void set_order_timestamp_copies(bool order) { order_timestamp_copies_ = order; }
1006 1011
1007 // The device address of the GPU address arena: console VAs are offsets from it (docs section 3.3). 1012 // The device address of the GPU address arena: console VAs are offsets from it (docs section 3.3).
1008 // execute() copies replay_ under a shared resource_mutex_, so writers hold it uniquely. 1013 // execute() copies replay_ under a shared resource_mutex_, so writers hold it uniquely.
@@ -1292,6 +1297,7 @@ private:
1292 std::function<void(bool)> bindless_commit_; 1297 std::function<void(bool)> bindless_commit_;
1293 std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts 1298 std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts
1294 double timestamp_period_ = 0; 1299 double timestamp_period_ = 0;
1300 bool order_timestamp_copies_ = false;
1295 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; 1301 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{};
1296 ReplayFunctions replay_; 1302 ReplayFunctions replay_;
1297 std::unique_ptr<PredicateReducer> predicate_reducer_; 1303 std::unique_ptr<PredicateReducer> predicate_reducer_;
@@ -1405,6 +1411,8 @@ public:
1405 } 1411 }
1406 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); 1412 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr);
1407 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); 1413 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr);
1414 for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end());
1415 for (VkEvent event : free_events_) vk.vkDestroyEvent(context_.device(), event, nullptr);
1408 } 1416 }
1409 1417
1410 VulkanQueue(const VulkanQueue&) = delete; 1418 VulkanQueue(const VulkanQueue&) = delete;
@@ -1648,6 +1656,24 @@ public:
1648 CommandSlot* slot = acquire(); 1656 CommandSlot* slot = acquire();
1649 if (!slot) return ReplayResult::unsupported; 1657 if (!slot) return ReplayResult::unsupported;
1650 command_buffer = slot->buffer; 1658 command_buffer = slot->buffer;
1659 // MoltenVK writes a timestamp at the end of a Metal encoder and resolves it with a blit that nothing orders
1660 // after that write, so a copy in the same or the next command buffer reads the old value (docs section 4.6).
1661 // An event set and waited for in this command buffer orders it after this submission's timestamps; a wait for
1662 // this queue's previous submission orders it after earlier ones still running.
1663 bool ordered_timestamps = false;
1664 const auto& events = context_.vk;
1665 if (context_.order_timestamp_copies() && events.vkCreateEvent && events.vkDestroyEvent && events.vkResetEvent &&
1666 events.vkCmdSetEvent && events.vkCmdWaitEvents)
1667 replay.order_timestamp_copy = [this, slot, &ordered_timestamps](VkCommandBuffer cb) {
1668 const VkEvent event = take_event();
1669 if (!event) return;
1670 slot->events.push_back(event);
1671 const auto& vk = context_.vk;
1672 vk.vkCmdSetEvent(cb, event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1673 vk.vkCmdWaitEvents(cb, 1, &event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
1674 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, nullptr, 0, nullptr, 0, nullptr);
1675 ordered_timestamps = true;
1676 };
1651 const auto discard = [&] { 1677 const auto discard = [&] {
1652 if (context_.vk.vkResetCommandBuffer(command_buffer, 0) == VK_SUCCESS) slot->ticket = 0; 1678 if (context_.vk.vkResetCommandBuffer(command_buffer, 0) == VK_SUCCESS) slot->ticket = 0;
1653 }; 1679 };
@@ -1744,6 +1770,11 @@ public:
1744 } 1770 }
1745 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 1771 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
1746 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held 1772 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held
1773 const bool wait_previous = ordered_timestamps && submitted_;
1774 if (wait_previous) {
1775 waits_.push_back(progress_);
1776 wait_values_.push_back(submitted_);
1777 }
1747 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1778 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1748 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission 1779 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission
1749 std::lock_guard lock(context_.storage_mutex_); 1780 std::lock_guard lock(context_.storage_mutex_);
@@ -1756,6 +1787,10 @@ public:
1756 ++context_.storage_init_version_; 1787 ++context_.storage_init_version_;
1757 storage_init_seen_ = context_.storage_init_version_; 1788 storage_init_seen_ = context_.storage_init_version_;
1758 } 1789 }
1790 if (wait_previous && !submitted && !waits_.empty()) { // a failed submit keeps the waits for the next one
1791 waits_.pop_back();
1792 wait_values_.pop_back();
1793 }
1759 if (submitted) { 1794 if (submitted) {
1760 context_.tracker_ = std::move(next_tracker); 1795 context_.tracker_ = std::move(next_tracker);
1761 slot->predicate = std::move(batch); 1796 slot->predicate = std::move(batch);
@@ -1818,6 +1853,7 @@ private:
1818 VkCommandBuffer buffer; 1853 VkCommandBuffer buffer;
1819 std::uint64_t ticket; 1854 std::uint64_t ticket;
1820 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; 1855 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments;
1856 std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes
1821 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; 1857 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy;
1822 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes 1858 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes
1823 }; 1859 };
@@ -1927,6 +1963,10 @@ private:
1927 for (auto& slot : buffers_) { 1963 for (auto& slot : buffers_) {
1928 if (slot.ticket > done) continue; 1964 if (slot.ticket > done) continue;
1929 slot.segments.clear(); 1965 slot.segments.clear();
1966 for (VkEvent event : slot.events)
1967 if (context_.vk.vkResetEvent(context_.device(), event) == VK_SUCCESS) free_events_.push_back(event);
1968 else context_.vk.vkDestroyEvent(context_.device(), event, nullptr);
1969 slot.events.clear();
1930 if (!reuse) reuse = &slot; 1970 if (!reuse) reuse = &slot;
1931 } 1971 }
1932 if (reuse) { 1972 if (reuse) {
@@ -1947,6 +1987,20 @@ private:
1947 return &buffers_.back(); 1987 return &buffers_.back();
1948 } 1988 }
1949 1989
1990 // An unsignaled event for order_timestamp_copy, or null. MoltenVK decides when the command buffer is submitted
1991 // whether a set or wait does anything, from the event's state then, so each use gets an event of its own.
1992 VkEvent take_event() {
1993 if (!free_events_.empty()) {
1994 const VkEvent event = free_events_.back();
1995 free_events_.pop_back();
1996 return event;
1997 }
1998 VkEventCreateInfo info{VK_STRUCTURE_TYPE_EVENT_CREATE_INFO};
1999 VkEvent event = VK_NULL_HANDLE;
2000 if (context_.vk.vkCreateEvent(context_.device(), &info, nullptr, &event) != VK_SUCCESS) return VK_NULL_HANDLE;
2001 return event;
2002 }
2003
1950 bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) { 2004 bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) {
1951 if (context_.is_device_lost()) return false; 2005 if (context_.is_device_lost()) return false;
1952 const std::uint64_t ticket = submitted_ + 1; 2006 const std::uint64_t ticket = submitted_ + 1;
@@ -2016,6 +2070,7 @@ private:
2016 VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done 2070 VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done
2017 std::uint64_t submitted_ = 0; 2071 std::uint64_t submitted_ = 0;
2018 std::vector<CommandSlot> buffers_; 2072 std::vector<CommandSlot> buffers_;
2073 std::vector<VkEvent> free_events_; // unsignaled, for order_timestamp_copy
2019 std::unique_ptr<OcclusionResolve> occlusion_resolve_; 2074 std::unique_ptr<OcclusionResolve> occlusion_resolve_;
2020 std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for 2075 std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for
2021 std::uint64_t last_sparse_ = 0; 2076 std::uint64_t last_sparse_ = 0;
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 8f7a71a9a..b9fc06464 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -793,6 +793,9 @@ struct ReplayFunctions {
793 PFN_vkCmdEndQuery end_query = nullptr; 793 PFN_vkCmdEndQuery end_query = nullptr;
794 PFN_vkCmdWriteTimestamp write_timestamp = nullptr; 794 PFN_vkCmdWriteTimestamp write_timestamp = nullptr;
795 PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; 795 PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr;
796 // Set only where a copy of timestamp results is not ordered after the timestamps written before it (MoltenVK on
797 // Apple GPUs, docs/d3d12_vulkan.md section 4.6): records what orders them, before each timestamp resolve.
798 std::function<void(VkCommandBuffer)> order_timestamp_copy;
796 std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t, std::uint32_t, VkBuffer, VkDeviceSize)> 799 std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t, std::uint32_t, VkBuffer, VkDeviceSize)>
797 sum_occlusion; 800 sum_occlusion;
798 PFN_vkCmdBindPipeline bind_pipeline = nullptr; // dispatches 801 PFN_vkCmdBindPipeline bind_pipeline = nullptr; // dispatches
@@ -2858,6 +2861,9 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu
2858 const bool suspend = predicate.buffer && !query.resolves.empty(); 2861 const bool suspend = predicate.buffer && !query.resolves.empty();
2859 end_statistics(); 2862 end_statistics();
2860 if (suspend) vk.end_conditional_rendering(cb); 2863 if (suspend) vk.end_conditional_rendering(cb);
2864 if (resolve.type == d3d12_query::timestamp && vk.order_timestamp_copy &&
2865 std::any_of(query.resolves.begin(), query.resolves.end(), [](const auto& run) { return run.pool; }))
2866 vk.order_timestamp_copy(cb);
2861 for (const auto& run : query.resolves) { 2867 for (const auto& run : query.resolves) {
2862 if (!run.pool) { 2868 if (!run.pool) {
2863 vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0); 2869 vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0);