diff options
| author | Jolymmiles <75259437+Jolymmiles@users.noreply.github.com> | 2026-11-22 19:01:54 +0400 |
|---|---|---|
| committer | Jolymmiles <75259437+Jolymmiles@users.noreply.github.com> | 2026-11-22 21:00:39 +0400 |
| commit | 6670b2bca0d86361ac1d707f4bb0caf660aeec2e (patch) | |
| tree | ad7b7520eb067434e202b0304439a6a35a450bec /runtime/src/vulkan_backend.h | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
d3d12: order timestamp resolves after their writes on MoltenVK
On Apple GPUs MoltenVK samples a vkCmdWriteTimestamp in a blit pass of its own at the end of the current Metal
encoder. Nothing orders the vkCmdCopyQueryPoolResults resolve after that pass. The copy read the old value, or 0 for
a timestamp never written, so d3d12_vulkan_device failed under Wine on a Mac (t1 == 0). A minimal Vulkan program
reproduces it natively on MoltenVK 1.4.3 and 1.4.2: the copy was right 0 to 43 times in 200, in the same command
buffer, the next one or the next submit. KosmicKrisp and llvmpipe were right in every case.
Where driverID is MoltenVK, the queue sets and waits for a fresh VkEvent before each timestamp resolve that copies.
A submission with such a resolve also waits on the queue's timeline for the submission before it. Both measured
200/200 and 500/500. The timestamps are still the GPU's own. The events go back to a pool after a host reset once
their submission completes. d3d12_vulkan_device also checks a resolve in the execution after the write. Documented
in docs/d3d12_vulkan.md section 4.6.
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 93d7e5259..38f048097 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -80,7 +80,8 @@ struct VulkanDeviceFunctions { | |||
| 80 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ | 80 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ |
| 81 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ | 81 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ |
| 82 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ | 82 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ |
| 83 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) | 83 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) \ |
| 84 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) | ||
| 84 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 85 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 85 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 86 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 86 | #undef LEONIDA_VK_MEMBER | 87 | #undef LEONIDA_VK_MEMBER |
| @@ -1002,6 +1003,10 @@ public: | |||
| 1002 | // (timestampValidBits 0). Set by the device before any queue uses it. | 1003 | // (timestampValidBits 0). Set by the device before any queue uses it. |
| 1003 | double timestamp_period() const { return timestamp_period_; } | 1004 | double timestamp_period() const { return timestamp_period_; } |
| 1004 | void set_timestamp_period(double period) { timestamp_period_ = period; } | 1005 | void set_timestamp_period(double period) { timestamp_period_ = period; } |
| 1006 | // True where a timestamp resolve must be ordered explicitly after the timestamps it copies (MoltenVK on Apple | ||
| 1007 | // GPUs, docs/d3d12_vulkan.md section 4.6). Set by the device before any queue uses it. | ||
| 1008 | bool order_timestamp_copies() const { return order_timestamp_copies_; } | ||
| 1009 | void set_order_timestamp_copies(bool order) { order_timestamp_copies_ = order; } | ||
| 1005 | 1010 | ||
| 1006 | // The device address of the GPU address arena: console VAs are offsets from it (docs section 3.3). | 1011 | // The device address of the GPU address arena: console VAs are offsets from it (docs section 3.3). |
| 1007 | // execute() copies replay_ under a shared resource_mutex_, so writers hold it uniquely. | 1012 | // execute() copies replay_ under a shared resource_mutex_, so writers hold it uniquely. |
| @@ -1283,6 +1288,7 @@ private: | |||
| 1283 | std::function<void(bool)> bindless_commit_; | 1288 | std::function<void(bool)> bindless_commit_; |
| 1284 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts | 1289 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts |
| 1285 | double timestamp_period_ = 0; | 1290 | double timestamp_period_ = 0; |
| 1291 | bool order_timestamp_copies_ = false; | ||
| 1286 | VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; | 1292 | VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; |
| 1287 | ReplayFunctions replay_; | 1293 | ReplayFunctions replay_; |
| 1288 | std::unique_ptr<PredicateReducer> predicate_reducer_; | 1294 | std::unique_ptr<PredicateReducer> predicate_reducer_; |
| @@ -1395,6 +1401,8 @@ public: | |||
| 1395 | } | 1401 | } |
| 1396 | if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); | 1402 | if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); |
| 1397 | if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); | 1403 | if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); |
| 1404 | for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end()); | ||
| 1405 | for (VkEvent event : free_events_) vk.vkDestroyEvent(context_.device(), event, nullptr); | ||
| 1398 | } | 1406 | } |
| 1399 | 1407 | ||
| 1400 | VulkanQueue(const VulkanQueue&) = delete; | 1408 | VulkanQueue(const VulkanQueue&) = delete; |
| @@ -1628,6 +1636,24 @@ public: | |||
| 1628 | CommandSlot* slot = acquire(); | 1636 | CommandSlot* slot = acquire(); |
| 1629 | if (!slot) return ReplayResult::unsupported; | 1637 | if (!slot) return ReplayResult::unsupported; |
| 1630 | command_buffer = slot->buffer; | 1638 | command_buffer = slot->buffer; |
| 1639 | // MoltenVK writes a timestamp at the end of a Metal encoder and resolves it with a blit that nothing orders | ||
| 1640 | // after that write, so a copy in the same or the next command buffer reads the old value (docs section 4.6). | ||
| 1641 | // An event set and waited for in this command buffer orders it after this submission's timestamps; a wait for | ||
| 1642 | // this queue's previous submission orders it after earlier ones still running. | ||
| 1643 | bool ordered_timestamps = false; | ||
| 1644 | const auto& events = context_.vk; | ||
| 1645 | if (context_.order_timestamp_copies() && events.vkCreateEvent && events.vkDestroyEvent && events.vkResetEvent && | ||
| 1646 | events.vkCmdSetEvent && events.vkCmdWaitEvents) | ||
| 1647 | replay.order_timestamp_copy = [this, slot, &ordered_timestamps](VkCommandBuffer cb) { | ||
| 1648 | const VkEvent event = take_event(); | ||
| 1649 | if (!event) return; | ||
| 1650 | slot->events.push_back(event); | ||
| 1651 | const auto& vk = context_.vk; | ||
| 1652 | vk.vkCmdSetEvent(cb, event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | ||
| 1653 | vk.vkCmdWaitEvents(cb, 1, &event, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | ||
| 1654 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, nullptr, 0, nullptr, 0, nullptr); | ||
| 1655 | ordered_timestamps = true; | ||
| 1656 | }; | ||
| 1631 | const auto discard = [&] { | 1657 | const auto discard = [&] { |
| 1632 | if (context_.vk.vkResetCommandBuffer(command_buffer, 0) == VK_SUCCESS) slot->ticket = 0; | 1658 | if (context_.vk.vkResetCommandBuffer(command_buffer, 0) == VK_SUCCESS) slot->ticket = 0; |
| 1633 | }; | 1659 | }; |
| @@ -1724,6 +1750,11 @@ public: | |||
| 1724 | } | 1750 | } |
| 1725 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; | 1751 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; |
| 1726 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held | 1752 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held |
| 1753 | const bool wait_previous = ordered_timestamps && submitted_; | ||
| 1754 | if (wait_previous) { | ||
| 1755 | waits_.push_back(progress_); | ||
| 1756 | wait_values_.push_back(submitted_); | ||
| 1757 | } | ||
| 1727 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); | 1758 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); |
| 1728 | if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission | 1759 | if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission |
| 1729 | std::lock_guard lock(context_.storage_mutex_); | 1760 | std::lock_guard lock(context_.storage_mutex_); |
| @@ -1736,6 +1767,10 @@ public: | |||
| 1736 | ++context_.storage_init_version_; | 1767 | ++context_.storage_init_version_; |
| 1737 | storage_init_seen_ = context_.storage_init_version_; | 1768 | storage_init_seen_ = context_.storage_init_version_; |
| 1738 | } | 1769 | } |
| 1770 | if (wait_previous && !submitted && !waits_.empty()) { // a failed submit keeps the waits for the next one | ||
| 1771 | waits_.pop_back(); | ||
| 1772 | wait_values_.pop_back(); | ||
| 1773 | } | ||
| 1739 | if (submitted) { | 1774 | if (submitted) { |
| 1740 | context_.tracker_ = std::move(next_tracker); | 1775 | context_.tracker_ = std::move(next_tracker); |
| 1741 | slot->predicate = std::move(batch); | 1776 | slot->predicate = std::move(batch); |
| @@ -1798,6 +1833,7 @@ private: | |||
| 1798 | VkCommandBuffer buffer; | 1833 | VkCommandBuffer buffer; |
| 1799 | std::uint64_t ticket; | 1834 | std::uint64_t ticket; |
| 1800 | std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; | 1835 | std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; |
| 1836 | std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes | ||
| 1801 | std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; | 1837 | std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; |
| 1802 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes | 1838 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes |
| 1803 | }; | 1839 | }; |
| @@ -1907,6 +1943,10 @@ private: | |||
| 1907 | for (auto& slot : buffers_) { | 1943 | for (auto& slot : buffers_) { |
| 1908 | if (slot.ticket > done) continue; | 1944 | if (slot.ticket > done) continue; |
| 1909 | slot.segments.clear(); | 1945 | slot.segments.clear(); |
| 1946 | for (VkEvent event : slot.events) | ||
| 1947 | if (context_.vk.vkResetEvent(context_.device(), event) == VK_SUCCESS) free_events_.push_back(event); | ||
| 1948 | else context_.vk.vkDestroyEvent(context_.device(), event, nullptr); | ||
| 1949 | slot.events.clear(); | ||
| 1910 | if (!reuse) reuse = &slot; | 1950 | if (!reuse) reuse = &slot; |
| 1911 | } | 1951 | } |
| 1912 | if (reuse) { | 1952 | if (reuse) { |
| @@ -1927,6 +1967,20 @@ private: | |||
| 1927 | return &buffers_.back(); | 1967 | return &buffers_.back(); |
| 1928 | } | 1968 | } |
| 1929 | 1969 | ||
| 1970 | // An unsignaled event for order_timestamp_copy, or null. MoltenVK decides when the command buffer is submitted | ||
| 1971 | // whether a set or wait does anything, from the event's state then, so each use gets an event of its own. | ||
| 1972 | VkEvent take_event() { | ||
| 1973 | if (!free_events_.empty()) { | ||
| 1974 | const VkEvent event = free_events_.back(); | ||
| 1975 | free_events_.pop_back(); | ||
| 1976 | return event; | ||
| 1977 | } | ||
| 1978 | VkEventCreateInfo info{VK_STRUCTURE_TYPE_EVENT_CREATE_INFO}; | ||
| 1979 | VkEvent event = VK_NULL_HANDLE; | ||
| 1980 | if (context_.vk.vkCreateEvent(context_.device(), &info, nullptr, &event) != VK_SUCCESS) return VK_NULL_HANDLE; | ||
| 1981 | return event; | ||
| 1982 | } | ||
| 1983 | |||
| 1930 | bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) { | 1984 | bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) { |
| 1931 | if (context_.is_device_lost()) return false; | 1985 | if (context_.is_device_lost()) return false; |
| 1932 | const std::uint64_t ticket = submitted_ + 1; | 1986 | const std::uint64_t ticket = submitted_ + 1; |
| @@ -1996,6 +2050,7 @@ private: | |||
| 1996 | VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done | 2050 | VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done |
| 1997 | std::uint64_t submitted_ = 0; | 2051 | std::uint64_t submitted_ = 0; |
| 1998 | std::vector<CommandSlot> buffers_; | 2052 | std::vector<CommandSlot> buffers_; |
| 2053 | std::vector<VkEvent> free_events_; // unsignaled, for order_timestamp_copy | ||
| 1999 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; | 2054 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; |
| 2000 | std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for | 2055 | std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for |
| 2001 | std::uint64_t last_sparse_ = 0; | 2056 | std::uint64_t last_sparse_ = 0; |