diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 22:35:34 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 22:35:34 -0500 |
| commit | 6ed716a99a7416f1e8a60c2e38e128876ab752ec (patch) | |
| tree | 3c35526aa8b96594252c82c278ab19b9aa3a98ba /runtime/src/vulkan_backend.h | |
| parent | 028a414fade7058a96e5933d1325f374a0026588 (diff) | |
| parent | bfef489cb6307fa4e1dc6ed2c40bfc0ce120d066 (diff) | |
Merge main into RT-QUEUE-SIGNAL (#430)
Everything below this rung (#414, #416, #427) and the rung's own routing are in
main already, through #984 and #939, in main's form; what is left of #430 is
its test of queue slot 17, ported onto main's layout detection.
Conflicts, all resolved to main's form:
- runtime/shims/d3d12_xs_device.cpp: main's queue slot 16/17 rule (a fence
there is the newer layout's Signal/Wait, for any caller) replaces the
rung's caller-gated rule; main's PresentX routing (slot 30, or 32 once the
newer layout is detected, LEONIDA_XS_PRESENT_X_SLOT) and lazily built
presenter replace the fixed slot 32 of #414; main's list proxy already
translates the CPU handles of console list slots 50 to 54 (#416) and
drops unordered access from a depth-stencil texture at slot 76 (#427).
- runtime/shims/d3d12_xs_device.h, runtime/shims/xbox_present.cpp,
runtime/shims/xbox_present.h, runtime/src/frame_pacer.h: main's shared
FrameTokenSource (Logan's piece of #414, as #984 carried it).
- runtime/shims/d3d12_xs_desc_clean.h, runtime/test/device_proxy_test.cpp:
main's without_depth_stencil_uav and its slot 25/76 test (#427).
- runtime/test/xs_proxy_mock_test.cpp: main's file, which already has
check_list_cpu_handles (#416's cases and more); the rung's slot 16/17 case
is ported to main's form at the end of check_xbox_queue_slots: slot 16 and
17 with a fence reach PC Signal and Wait with the real fence, not the PC
getters, and the fence proxy's vtable pointer is unchanged. The rung's
host-caller and no-fence expectations are dropped: under main's rule a
fence at 16 proves the newer layout, after which those calls answer S_OK.
Auto-merged, then set to main's content:
- runtime/test/queue_proxy_test.cpp: #414's call of queue slot 32 as an
overridden slot; on main slot 32 is routed only on the newer layout and the
sweep of the console tail already calls it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 614 |
1 files changed, 533 insertions, 81 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e3e4defe8..5c3e884ba 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | #include "vulkan_gpu_labels.h" | ||
| 2 | 3 | ||
| 3 | // The execution side of the D3D12 layer, without COM: the per-device Vulkan context (resources, layout tracker, | 4 | // The execution side of the D3D12 layer, without COM: the per-device Vulkan context (resources, layout tracker, |
| 4 | // image views), queues that execute command lists, and timeline-semaphore fences. The COM objects in | 5 | // image views), queues that execute command lists, and timeline-semaphore fences. The COM objects in |
| @@ -11,17 +12,20 @@ | |||
| 11 | #endif | 12 | #endif |
| 12 | #include "binary_occlusion_spirv.h" | 13 | #include "binary_occlusion_spirv.h" |
| 13 | #include "buffer_va_index.h" | 14 | #include "buffer_va_index.h" |
| 15 | #include "image_memory.h" | ||
| 14 | #include "occlusion_sum_spirv.h" | 16 | #include "occlusion_sum_spirv.h" |
| 15 | #include "queue_holds.h" | 17 | #include "queue_holds.h" |
| 16 | #include "retire_tickets.h" | 18 | #include "retire_tickets.h" |
| 17 | #include "statistics_sum_spirv.h" | 19 | #include "statistics_sum_spirv.h" |
| 18 | #include "vulkan13_features.h" | 20 | #include "vulkan13_features.h" |
| 19 | #include "vulkan_depth24.h" | 21 | #include "vulkan_depth24.h" |
| 22 | #include "vulkan_title_image_pool.h" | ||
| 20 | #include "vulkan_indirect_dispatch_count.h" | 23 | #include "vulkan_indirect_dispatch_count.h" |
| 21 | #include "vulkan_scratch.h" | 24 | #include "vulkan_scratch.h" |
| 22 | #include "vulkan_replay.h" | 25 | #include "vulkan_replay.h" |
| 23 | #include "queue_families.h" | 26 | #include "queue_families.h" |
| 24 | #include "resource_tiling.h" | 27 | #include "resource_tiling.h" |
| 28 | #include "pipeline_profile.h" | ||
| 25 | #include "xdxr_build.h" | 29 | #include "xdxr_build.h" |
| 26 | 30 | ||
| 27 | #include <algorithm> | 31 | #include <algorithm> |
| @@ -46,6 +50,15 @@ | |||
| 46 | 50 | ||
| 47 | namespace leonida { | 51 | namespace leonida { |
| 48 | 52 | ||
| 53 | // Which check of the queue's replay refused a batch ("ExecuteCommandLists failed: unsupported"), by source line, | ||
| 54 | // the first time each one does. The batch is then not submitted at all. | ||
| 55 | inline ReplayResult queue_unsupported(int line) { | ||
| 56 | static std::atomic<bool> seen[4096]; | ||
| 57 | if (line < 0 || line >= 4096 || !seen[line].exchange(true, std::memory_order_relaxed)) | ||
| 58 | std::fprintf(stderr, "[rt] d3d12: queue replay refused a batch at vulkan_backend.h:%d\n", line); | ||
| 59 | return ReplayResult::unsupported; | ||
| 60 | } | ||
| 61 | |||
| 49 | // A CPU-written Vulkan-address instance pointer array, kept until the submission that builds with it completes. | 62 | // A CPU-written Vulkan-address instance pointer array, kept until the submission that builds with it completes. |
| 50 | // Completed stages are pooled per queue and reused, so a per-frame TLAS build does not allocate memory each frame. | 63 | // Completed stages are pooled per queue and reused, so a per-frame TLAS build does not allocate memory each frame. |
| 51 | struct KhrInstanceStage { | 64 | struct KhrInstanceStage { |
| @@ -85,7 +98,7 @@ struct VulkanDeviceFunctions { | |||
| 85 | X(vkCmdClearAttachments) X(vkDeviceWaitIdle) X(vkCreateImage) X(vkDestroyImage) \ | 98 | X(vkCmdClearAttachments) X(vkDeviceWaitIdle) X(vkCreateImage) X(vkDestroyImage) \ |
| 86 | X(vkGetImageMemoryRequirements) X(vkBindImageMemory) X(vkCreateBuffer) X(vkDestroyBuffer) \ | 99 | X(vkGetImageMemoryRequirements) X(vkBindImageMemory) X(vkCreateBuffer) X(vkDestroyBuffer) \ |
| 87 | X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) X(vkFreeMemory) X(vkMapMemory) \ | 100 | X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) X(vkFreeMemory) X(vkMapMemory) \ |
| 88 | X(vkUnmapMemory) X(vkCmdCopyBuffer) X(vkCmdCopyBufferToImage) X(vkCmdCopyImageToBuffer) X(vkCmdCopyImage) \ | 101 | X(vkUnmapMemory) X(vkCmdCopyBuffer) X(vkCmdCopyBufferToImage) X(vkCmdCopyImageToBuffer) X(vkCmdCopyImage) X(vkCmdBlitImage) \ |
| 89 | X(vkCmdResolveImage) X(vkCmdFillBuffer) X(vkCmdUpdateBuffer) X(vkCreateSampler) X(vkDestroySampler) \ | 102 | X(vkCmdResolveImage) X(vkCmdFillBuffer) X(vkCmdUpdateBuffer) X(vkCreateSampler) X(vkDestroySampler) \ |
| 90 | X(vkCmdBeginConditionalRenderingEXT) X(vkCmdEndConditionalRenderingEXT) X(vkCreateQueryPool) \ | 103 | X(vkCmdBeginConditionalRenderingEXT) X(vkCmdEndConditionalRenderingEXT) X(vkCreateQueryPool) \ |
| 91 | X(vkDestroyQueryPool) X(vkCmdResetQueryPool) X(vkCmdBeginQuery) X(vkCmdEndQuery) X(vkCmdWriteTimestamp) \ | 104 | X(vkDestroyQueryPool) X(vkCmdResetQueryPool) X(vkCmdBeginQuery) X(vkCmdEndQuery) X(vkCmdWriteTimestamp) \ |
| @@ -101,10 +114,11 @@ struct VulkanDeviceFunctions { | |||
| 101 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ | 114 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ |
| 102 | X(vkCmdPushConstants) X(vkCmdDispatch) \ | 115 | X(vkCmdPushConstants) X(vkCmdDispatch) \ |
| 103 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ | 116 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ |
| 117 | X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \ | ||
| 104 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ | 118 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ |
| 105 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ | 119 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ |
| 106 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ | 120 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ |
| 107 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ | 121 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ |
| 108 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ | 122 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ |
| 109 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ | 123 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ |
| 110 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) | 124 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) |
| @@ -244,12 +258,16 @@ public: | |||
| 244 | 258 | ||
| 245 | // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the | 259 | // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the |
| 246 | // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). | 260 | // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). |
| 247 | // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see | 261 | // Segment i is in `mesh_queries` when mesh[i] is set (two counters: fragment and compute; see |
| 248 | // statistics_sum.comp), otherwise in `queries` (all eleven). | 262 | // statistics_sum.comp), otherwise in `queries` (all eleven). A mesh segment's clipping invocations come from |
| 249 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, | 263 | // `primitives_queries`, the mesh-primitives count of that segment (D3D-MESH-CLIPPING-COUNTER); it is null when |
| 250 | std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { | 264 | // the device has no meshShaderQueries, and the field then reads 0. |
| 265 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, | ||
| 266 | VkQueryPool primitives_queries, const std::uint8_t* mesh, std::uint32_t first, | ||
| 267 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { | ||
| 251 | constexpr VkDeviceSize result = statistics_counters * word_size; | 268 | constexpr VkDeviceSize result = statistics_counters * word_size; |
| 252 | constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes | 269 | constexpr VkDeviceSize mesh_slot = 7 * word_size; // PSInvocations, where the mesh pool's first counter goes |
| 270 | constexpr VkDeviceSize clipping_slot = 5 * word_size; // CInvocations, where the mesh-primitives count goes | ||
| 253 | begin_scratch_write(cb); | 271 | begin_scratch_write(cb); |
| 254 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); | 272 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); |
| 255 | bind(cb, statistics_pipeline_); | 273 | bind(cb, statistics_pipeline_); |
| @@ -258,9 +276,18 @@ public: | |||
| 258 | std::uint32_t chunk = 1; | 276 | std::uint32_t chunk = 1; |
| 259 | while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) | 277 | while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) |
| 260 | ++chunk; | 278 | ++chunk; |
| 279 | if (in_mesh) { | ||
| 280 | // The mesh pool holds only the fragment and compute counters, so the clipping field of a mesh | ||
| 281 | // segment reads 0 unless the mesh-primitives count fills it (D3D-MESH-CLIPPING-COUNTER). | ||
| 282 | vk_.vkCmdFillBuffer(cb, scratch_, result, chunk * result, 0); | ||
| 283 | barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); | ||
| 284 | } | ||
| 261 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, | 285 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, |
| 262 | result + (in_mesh ? mesh_slot : 0), result, | 286 | result + (in_mesh ? mesh_slot : 0), result, |
| 263 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 287 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 288 | if (in_mesh && primitives_queries) | ||
| 289 | vk_.vkCmdCopyQueryPoolResults(cb, primitives_queries, first, chunk, scratch_, result + clipping_slot, | ||
| 290 | result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | ||
| 264 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, | 291 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, |
| 265 | shader_access); | 292 | shader_access); |
| 266 | dispatch(cb, chunk, 1, in_mesh); | 293 | dispatch(cb, chunk, 1, in_mesh); |
| @@ -412,6 +439,7 @@ private: | |||
| 412 | }; | 439 | }; |
| 413 | 440 | ||
| 414 | struct SparseResource; | 441 | struct SparseResource; |
| 442 | using ImageMemory = ImageMemoryPool<VkDeviceMemory>; | ||
| 415 | 443 | ||
| 416 | // Zero-allocation O(1) Ring Buffer deferred deletion queue tied to the device timeline semaphore. | 444 | // Zero-allocation O(1) Ring Buffer deferred deletion queue tied to the device timeline semaphore. |
| 417 | class DeferredDeletionQueue { | 445 | class DeferredDeletionQueue { |
| @@ -421,8 +449,10 @@ public: | |||
| 421 | image_view, | 449 | image_view, |
| 422 | image, | 450 | image, |
| 423 | sparse_resource, | 451 | sparse_resource, |
| 452 | pooled_title_image, | ||
| 424 | buffer, | 453 | buffer, |
| 425 | device_memory, | 454 | device_memory, |
| 455 | image_memory, // an ImageMemory::Range*, deleted: its bytes go back to their block | ||
| 426 | acceleration_structure, | 456 | acceleration_structure, |
| 427 | framebuffer, | 457 | framebuffer, |
| 428 | }; | 458 | }; |
| @@ -498,6 +528,9 @@ public: | |||
| 498 | case Kind::image: | 528 | case Kind::image: |
| 499 | vk.vkDestroyImage(device, reinterpret_cast<VkImage>(item.handle), nullptr); | 529 | vk.vkDestroyImage(device, reinterpret_cast<VkImage>(item.handle), nullptr); |
| 500 | break; | 530 | break; |
| 531 | case Kind::pooled_title_image: | ||
| 532 | delete reinterpret_cast<PooledTitleImage*>(item.handle); | ||
| 533 | break; | ||
| 501 | case Kind::sparse_resource: | 534 | case Kind::sparse_resource: |
| 502 | // Releases the resource and its page backing together after outstanding submissions. | 535 | // Releases the resource and its page backing together after outstanding submissions. |
| 503 | delete reinterpret_cast<std::shared_ptr<SparseResource>*>(item.handle); | 536 | delete reinterpret_cast<std::shared_ptr<SparseResource>*>(item.handle); |
| @@ -508,6 +541,9 @@ public: | |||
| 508 | case Kind::device_memory: | 541 | case Kind::device_memory: |
| 509 | vk.vkFreeMemory(device, reinterpret_cast<VkDeviceMemory>(item.handle), nullptr); | 542 | vk.vkFreeMemory(device, reinterpret_cast<VkDeviceMemory>(item.handle), nullptr); |
| 510 | break; | 543 | break; |
| 544 | case Kind::image_memory: | ||
| 545 | delete reinterpret_cast<ImageMemory::Range*>(item.handle); | ||
| 546 | break; | ||
| 511 | case Kind::acceleration_structure: | 547 | case Kind::acceleration_structure: |
| 512 | if (vk.vkDestroyAccelerationStructureKHR) | 548 | if (vk.vkDestroyAccelerationStructureKHR) |
| 513 | vk.vkDestroyAccelerationStructureKHR( | 549 | vk.vkDestroyAccelerationStructureKHR( |
| @@ -536,6 +572,22 @@ private: | |||
| 536 | void* bindless_release_user_ = nullptr; | 572 | void* bindless_release_user_ = nullptr; |
| 537 | }; | 573 | }; |
| 538 | 574 | ||
| 575 | // Sets a device's lost flag and says so once, on stderr, at the moment it is first seen. From then on every fence of | ||
| 576 | // the device reads UINT64_MAX and device calls return DXGI_ERROR_DEVICE_REMOVED, so a title takes its own | ||
| 577 | // device-removed path (on the retail boot an int3 at game_scarlett_master.exe+0x2474606, reached from its frame wait | ||
| 578 | // reading a fence's completed value as ~0); without this line that later stop is the first sign of the loss. | ||
| 579 | // True when this call set the flag. device_lost_reports counts the lines said in this process (for tests). | ||
| 580 | inline std::atomic<unsigned> device_lost_reports{0}; | ||
| 581 | inline bool set_device_lost(std::atomic<bool>& flag, const char* where) noexcept { | ||
| 582 | if (flag.exchange(true, std::memory_order_acq_rel)) return false; | ||
| 583 | device_lost_reports.fetch_add(1, std::memory_order_relaxed); | ||
| 584 | std::fprintf(stderr, | ||
| 585 | "[rt] vulkan: the device was lost (%s); its fences now read UINT64_MAX and device calls return " | ||
| 586 | "DXGI_ERROR_DEVICE_REMOVED\n", | ||
| 587 | where); | ||
| 588 | return true; | ||
| 589 | } | ||
| 590 | |||
| 539 | // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). | 591 | // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). |
| 540 | // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 | 592 | // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 |
| 541 | // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. | 593 | // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. |
| @@ -587,6 +639,8 @@ public: | |||
| 587 | FenceWaitDispatcher(const FenceWaitDispatcher&) = delete; | 639 | FenceWaitDispatcher(const FenceWaitDispatcher&) = delete; |
| 588 | FenceWaitDispatcher& operator=(const FenceWaitDispatcher&) = delete; | 640 | FenceWaitDispatcher& operator=(const FenceWaitDispatcher&) = delete; |
| 589 | 641 | ||
| 642 | void set_device_fault(DeviceFaultDiagnostics* diagnostics) { diagnostics_ = diagnostics; } | ||
| 643 | |||
| 590 | void set_signal_event_fn(SignalEventFn fn) { | 644 | void set_signal_event_fn(SignalEventFn fn) { |
| 591 | std::lock_guard lock(mutex_); | 645 | std::lock_guard lock(mutex_); |
| 592 | signal_event_ = fn; | 646 | signal_event_ = fn; |
| @@ -763,7 +817,8 @@ private: | |||
| 763 | VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 817 | VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 764 | si.semaphore = wakeup_sem_; | 818 | si.semaphore = wakeup_sem_; |
| 765 | si.value = next; | 819 | si.value = next; |
| 766 | vk_.vkSignalSemaphore(device_, &si); | 820 | DeviceFaultDiagnostics::observe(diagnostics_, vk_.vkSignalSemaphore(device_, &si), |
| 821 | "vkSignalSemaphore (fence wakeup)"); | ||
| 767 | } | 822 | } |
| 768 | 823 | ||
| 769 | void run() { | 824 | void run() { |
| @@ -800,7 +855,8 @@ private: | |||
| 800 | // is pending (d3d12_queue_wait_timeout's _Exit, or quitting with a fence outstanding) never finishes | 855 | // is pending (d3d12_queue_wait_timeout's _Exit, or quitting with a fence outstanding) never finishes |
| 801 | // exiting, and TerminateProcess cannot end it either. A timeout returns here and the loop re-arms the | 856 | // exiting, and TerminateProcess cannot end it either. A timeout returns here and the loop re-arms the |
| 802 | // same wait; completions still wake it at once through the ANY wait. | 857 | // same wait; completions still wake it at once through the ANY wait. |
| 803 | const VkResult res = vk_.vkWaitSemaphores(device_, &wi, kWaitSliceNs); | 858 | const VkResult res = DeviceFaultDiagnostics::observe( |
| 859 | diagnostics_, vk_.vkWaitSemaphores(device_, &wi, kWaitSliceNs), "vkWaitSemaphores (fence dispatcher)"); | ||
| 804 | 860 | ||
| 805 | lock.lock(); | 861 | lock.lock(); |
| 806 | waiting_ = false; | 862 | waiting_ = false; |
| @@ -808,7 +864,7 @@ private: | |||
| 808 | epoch_cv_.notify_all(); | 864 | epoch_cv_.notify_all(); |
| 809 | 865 | ||
| 810 | if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { | 866 | if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { |
| 811 | device_lost_.store(true, std::memory_order_release); | 867 | set_device_lost(device_lost_, "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); |
| 812 | std::vector<Entry> drained = std::move(pending_); | 868 | std::vector<Entry> drained = std::move(pending_); |
| 813 | pending_.clear(); | 869 | pending_.clear(); |
| 814 | dispatch_outside_lock(lock, drained, signal_event_); | 870 | dispatch_outside_lock(lock, drained, signal_event_); |
| @@ -821,7 +877,9 @@ private: | |||
| 821 | std::size_t write = 0; | 877 | std::size_t write = 0; |
| 822 | for (std::size_t i = 0; i < pending_.size(); ++i) { | 878 | for (std::size_t i = 0; i < pending_.size(); ++i) { |
| 823 | std::uint64_t done = 0; | 879 | std::uint64_t done = 0; |
| 824 | const VkResult cr = vk_.vkGetSemaphoreCounterValue(device_, pending_[i].semaphore, &done); | 880 | const VkResult cr = DeviceFaultDiagnostics::observe( |
| 881 | diagnostics_, vk_.vkGetSemaphoreCounterValue(device_, pending_[i].semaphore, &done), | ||
| 882 | "vkGetSemaphoreCounterValue (fence dispatcher)"); | ||
| 825 | if (cr == VK_ERROR_DEVICE_LOST) { | 883 | if (cr == VK_ERROR_DEVICE_LOST) { |
| 826 | lost = true; | 884 | lost = true; |
| 827 | for (std::size_t j = i; j < pending_.size(); ++j) ready.push_back(std::move(pending_[j])); | 885 | for (std::size_t j = i; j < pending_.size(); ++j) ready.push_back(std::move(pending_[j])); |
| @@ -838,7 +896,8 @@ private: | |||
| 838 | pending_.resize(write); | 896 | pending_.resize(write); |
| 839 | for (Entry& e : pending_) ready.push_back(std::move(e)); | 897 | for (Entry& e : pending_) ready.push_back(std::move(e)); |
| 840 | pending_.clear(); | 898 | pending_.clear(); |
| 841 | device_lost_.store(true, std::memory_order_release); | 899 | set_device_lost(device_lost_, |
| 900 | "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); | ||
| 842 | dispatch_outside_lock(lock, ready, signal_event_); | 901 | dispatch_outside_lock(lock, ready, signal_event_); |
| 843 | return; | 902 | return; |
| 844 | } | 903 | } |
| @@ -853,6 +912,7 @@ private: | |||
| 853 | VkDevice device_; | 912 | VkDevice device_; |
| 854 | const VulkanDeviceFunctions& vk_; | 913 | const VulkanDeviceFunctions& vk_; |
| 855 | std::atomic<bool>& device_lost_; | 914 | std::atomic<bool>& device_lost_; |
| 915 | DeviceFaultDiagnostics* diagnostics_ = nullptr; // configured before the dispatcher starts | ||
| 856 | SignalEventFn signal_event_ = nullptr; | 916 | SignalEventFn signal_event_ = nullptr; |
| 857 | VkSemaphore wakeup_sem_ = VK_NULL_HANDLE; | 917 | VkSemaphore wakeup_sem_ = VK_NULL_HANDLE; |
| 858 | std::uint64_t wakeup_val_ = 0; | 918 | std::uint64_t wakeup_val_ = 0; |
| @@ -923,6 +983,10 @@ struct TileRange { | |||
| 923 | #endif | 983 | #endif |
| 924 | class VulkanContext { | 984 | class VulkanContext { |
| 925 | public: | 985 | public: |
| 986 | // mesh_queries: VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so a mesh segment's | ||
| 987 | // clipping invocations can come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Set by the device | ||
| 988 | // that created the context. | ||
| 989 | bool mesh_queries = false; | ||
| 926 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). | 990 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). |
| 927 | // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. | 991 | // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. |
| 928 | // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. | 992 | // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. |
| @@ -930,6 +994,9 @@ public: | |||
| 930 | bool conditional_rendering = false, bool ray_tracing = false, bool xdxr = false) | 994 | bool conditional_rendering = false, bool ray_tracing = false, bool xdxr = false) |
| 931 | : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { | 995 | : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { |
| 932 | loaded_ = vk.load(get, device); | 996 | loaded_ = vk.load(get, device); |
| 997 | if (loaded_ && gpu_labels_.initialize(device, get)) { | ||
| 998 | replay_.gpu_label = [this](VkCommandBuffer cb, const label::Write& write) { gpu_labels_.record(cb, write); }; | ||
| 999 | } | ||
| 933 | replay_.pipeline_barrier = vk.vkCmdPipelineBarrier; | 1000 | replay_.pipeline_barrier = vk.vkCmdPipelineBarrier; |
| 934 | replay_.clear_color_image = vk.vkCmdClearColorImage; | 1001 | replay_.clear_color_image = vk.vkCmdClearColorImage; |
| 935 | replay_.clear_depth_stencil_image = vk.vkCmdClearDepthStencilImage; | 1002 | replay_.clear_depth_stencil_image = vk.vkCmdClearDepthStencilImage; |
| @@ -941,6 +1008,7 @@ public: | |||
| 941 | replay_.copy_buffer_to_image = vk.vkCmdCopyBufferToImage; | 1008 | replay_.copy_buffer_to_image = vk.vkCmdCopyBufferToImage; |
| 942 | replay_.copy_image_to_buffer = vk.vkCmdCopyImageToBuffer; | 1009 | replay_.copy_image_to_buffer = vk.vkCmdCopyImageToBuffer; |
| 943 | replay_.copy_image = vk.vkCmdCopyImage; | 1010 | replay_.copy_image = vk.vkCmdCopyImage; |
| 1011 | replay_.blit_image = vk.vkCmdBlitImage; | ||
| 944 | replay_.resolve_image = vk.vkCmdResolveImage; | 1012 | replay_.resolve_image = vk.vkCmdResolveImage; |
| 945 | replay_.fill_buffer = vk.vkCmdFillBuffer; | 1013 | replay_.fill_buffer = vk.vkCmdFillBuffer; |
| 946 | replay_.update_buffer = vk.vkCmdUpdateBuffer; | 1014 | replay_.update_buffer = vk.vkCmdUpdateBuffer; |
| @@ -970,6 +1038,8 @@ public: | |||
| 970 | replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer; | 1038 | replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer; |
| 971 | replay_.set_viewport = vk.vkCmdSetViewport; | 1039 | replay_.set_viewport = vk.vkCmdSetViewport; |
| 972 | replay_.set_scissor = vk.vkCmdSetScissor; | 1040 | replay_.set_scissor = vk.vkCmdSetScissor; |
| 1041 | replay_.set_viewport_with_count = vk.vkCmdSetViewportWithCount; | ||
| 1042 | replay_.set_scissor_with_count = vk.vkCmdSetScissorWithCount; | ||
| 973 | replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; | 1043 | replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; |
| 974 | replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; | 1044 | replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; |
| 975 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; | 1045 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; |
| @@ -1000,7 +1070,7 @@ public: | |||
| 1000 | // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. | 1070 | // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. |
| 1001 | replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { | 1071 | replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { |
| 1002 | BufferVaIndex<ResourceId>::Hit hit{}; | 1072 | BufferVaIndex<ResourceId>::Hit hit{}; |
| 1003 | if (!buffer_vas_.find(va, hit)) return HostReadStatus::absent; | 1073 | if (!buffer_vas_.find(va, hit, bytes)) return HostReadStatus::absent; |
| 1004 | const ResourceBinding* b = resolve(hit.id); | 1074 | const ResourceBinding* b = resolve(hit.id); |
| 1005 | if (!b) return HostReadStatus::absent; | 1075 | if (!b) return HostReadStatus::absent; |
| 1006 | if (!b->host) return HostReadStatus::not_cpu_written; | 1076 | if (!b->host) return HostReadStatus::not_cpu_written; |
| @@ -1086,6 +1156,8 @@ public: | |||
| 1086 | } | 1156 | } |
| 1087 | 1157 | ||
| 1088 | ~VulkanContext() { | 1158 | ~VulkanContext() { |
| 1159 | progress_watch_ = {}; // stop before queues, timelines or diagnostic state go away | ||
| 1160 | gpu_labels_.stop(device_); | ||
| 1089 | fence_dispatcher_.reset(); | 1161 | fence_dispatcher_.reset(); |
| 1090 | deletion_queue_.drain(UINT64_MAX, device_, vk); | 1162 | deletion_queue_.drain(UINT64_MAX, device_, vk); |
| 1091 | for (const auto& [key, framebuffer] : resolve_framebuffers_) | 1163 | for (const auto& [key, framebuffer] : resolve_framebuffers_) |
| @@ -1098,6 +1170,22 @@ public: | |||
| 1098 | VulkanContext& operator=(const VulkanContext&) = delete; | 1170 | VulkanContext& operator=(const VulkanContext&) = delete; |
| 1099 | 1171 | ||
| 1100 | bool loaded() const { return loaded_; } | 1172 | bool loaded() const { return loaded_; } |
| 1173 | |||
| 1174 | void configure_device_fault(PFN_vkGetDeviceProcAddr get, DeviceFaultExtensions enabled) { | ||
| 1175 | device_fault_ = std::make_unique<DeviceFaultDiagnostics>(get, device_, enabled); | ||
| 1176 | if (fence_dispatcher_) fence_dispatcher_->set_device_fault(device_fault_.get()); | ||
| 1177 | if (loaded_) progress_watch_ = std::jthread([this](std::stop_token stop) { watch_queue_progress(stop); }); | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | void add_diagnostic_queue(VkQueue queue) { | ||
| 1181 | if (device_fault_) device_fault_->add_queue(queue); | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | DeviceFaultDiagnostics* device_fault() const { return device_fault_.get(); } | ||
| 1185 | |||
| 1186 | VkResult observe_result(VkResult result, const char* operation) const noexcept { | ||
| 1187 | return DeviceFaultDiagnostics::observe(device_fault_.get(), result, operation); | ||
| 1188 | } | ||
| 1101 | void configure_predication(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory, | 1189 | void configure_predication(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory, |
| 1102 | VkDeviceSize max_range, std::uint32_t family) { | 1190 | VkDeviceSize max_range, std::uint32_t family) { |
| 1103 | std::unique_lock lock(resource_mutex_); | 1191 | std::unique_lock lock(resource_mutex_); |
| @@ -1116,6 +1204,13 @@ public: | |||
| 1116 | std::unique_lock lock(resource_mutex_); | 1204 | std::unique_lock lock(resource_mutex_); |
| 1117 | counted_dispatch_ = std::make_unique<IndirectDispatchCounter>(get, device_, memory, limits); | 1205 | counted_dispatch_ = std::make_unique<IndirectDispatchCounter>(get, device_, memory, limits); |
| 1118 | } | 1206 | } |
| 1207 | // Indexed quad lists are drawn from triangle indices a compute pass writes into the copy scratch | ||
| 1208 | // (quad_index_expand.h). Call only with bufferDeviceAddress enabled and after an enable_copy_scratch whose chunks | ||
| 1209 | // have INDEX_BUFFER and SHADER_DEVICE_ADDRESS usage on memory allocated with a device address. | ||
| 1210 | void configure_quad_indices(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceLimits& limits) { | ||
| 1211 | std::unique_lock lock(resource_mutex_); | ||
| 1212 | quad_indices_ = std::make_unique<QuadIndexExpander>(get, device_, limits); | ||
| 1213 | } | ||
| 1119 | VkDevice device() const { return device_; } | 1214 | VkDevice device() const { return device_; } |
| 1120 | 1215 | ||
| 1121 | // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets | 1216 | // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets |
| @@ -1135,6 +1230,11 @@ public: | |||
| 1135 | std::unique_lock lock(resource_mutex_); | 1230 | std::unique_lock lock(resource_mutex_); |
| 1136 | set_resource_locked(id, binding); | 1231 | set_resource_locked(id, binding); |
| 1137 | } | 1232 | } |
| 1233 | // Whether `va` is in a buffer this context knows by VA (the index buffer_at searches). | ||
| 1234 | bool knows_buffer_va(std::uint64_t va, std::uint64_t bytes = 1) { | ||
| 1235 | std::shared_lock lock(resource_mutex_); | ||
| 1236 | return buffer_vas_.covers(va, bytes); | ||
| 1237 | } | ||
| 1138 | void add_resource(ResourceId id, const ResourceBinding& binding, VkImageLayout current_layout) { | 1238 | void add_resource(ResourceId id, const ResourceBinding& binding, VkImageLayout current_layout) { |
| 1139 | std::unique_lock rlock(resource_mutex_); | 1239 | std::unique_lock rlock(resource_mutex_); |
| 1140 | set_resource_locked(id, binding); | 1240 | set_resource_locked(id, binding); |
| @@ -1196,6 +1296,7 @@ public: | |||
| 1196 | } | 1296 | } |
| 1197 | { | 1297 | { |
| 1198 | std::lock_guard lock(storage_mutex_); // a buffer destroyed before any submission is not filled | 1298 | std::lock_guard lock(storage_mutex_); // a buffer destroyed before any submission is not filled |
| 1299 | storage_pending_.erase(id); | ||
| 1199 | std::erase_if(zero_pending_, [id](const PendingZero& zero) { return zero.id == id; }); | 1300 | std::erase_if(zero_pending_, [id](const PendingZero& zero) { return zero.id == id; }); |
| 1200 | } | 1301 | } |
| 1201 | if (img) { | 1302 | if (img) { |
| @@ -1268,6 +1369,37 @@ public: | |||
| 1268 | replay_.zero_size = size; | 1369 | replay_.zero_size = size; |
| 1269 | } | 1370 | } |
| 1270 | 1371 | ||
| 1372 | void set_depth_range_unrestricted(bool enabled) { | ||
| 1373 | std::unique_lock lock(resource_mutex_); | ||
| 1374 | replay_.depth_range_unrestricted = enabled; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | struct GeometryUpload { | ||
| 1378 | std::uint64_t source{}, size{}; | ||
| 1379 | VkBuffer buffer{}; | ||
| 1380 | VkDeviceSize offset{}; | ||
| 1381 | std::shared_ptr<void> owner; | ||
| 1382 | }; | ||
| 1383 | using GeometryProvider = std::function<bool(std::uint64_t,std::uint64_t,GeometryUpload&)>; | ||
| 1384 | void set_geometry_provider(GeometryProvider provider) { | ||
| 1385 | std::unique_lock lock(resource_mutex_); | ||
| 1386 | geometry_provider_=std::move(provider); | ||
| 1387 | } | ||
| 1388 | |||
| 1389 | // LOCAL: the copy-in fallback for vertex and index views in title memory (ReplayFunctions::title_memory). | ||
| 1390 | void set_title_memory(std::function<bool(std::uint64_t, std::uint64_t, VkBuffer&, VkDeviceSize&)> copy) { | ||
| 1391 | std::unique_lock lock(resource_mutex_); | ||
| 1392 | replay_.title_memory = std::move(copy); | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | // Pre-expanded 32-bit triangle indices for non-indexed quad lists (ReplayFunctions::quad_indices). | ||
| 1396 | void set_quad_indices(VkBuffer buffer, VkDeviceSize offset, std::uint32_t count) { | ||
| 1397 | std::unique_lock lock(resource_mutex_); | ||
| 1398 | replay_.quad_indices = buffer; | ||
| 1399 | replay_.quad_indices_offset = offset; | ||
| 1400 | replay_.quad_index_count = count; | ||
| 1401 | } | ||
| 1402 | |||
| 1271 | // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. | 1403 | // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. |
| 1272 | void set_color_attachment_format(std::function<bool(VkFormat)> check) { | 1404 | void set_color_attachment_format(std::function<bool(VkFormat)> check) { |
| 1273 | std::unique_lock lock(resource_mutex_); | 1405 | std::unique_lock lock(resource_mutex_); |
| @@ -1279,24 +1411,77 @@ public: | |||
| 1279 | } | 1411 | } |
| 1280 | 1412 | ||
| 1281 | // Pipelines are registered by an opaque id (the PSO object) for as long as the PSO lives. | 1413 | // Pipelines are registered by an opaque id (the PSO object) for as long as the PSO lives. |
| 1282 | void add_pipeline(std::uintptr_t id, const ComputePipeline& pipeline) { | 1414 | void add_pipeline(std::uintptr_t id, const ComputePipeline& pipeline, const char* shader = nullptr) { |
| 1283 | std::unique_lock lock(resource_mutex_); | 1415 | std::unique_lock lock(resource_mutex_); |
| 1284 | pipelines_[id] = pipeline; | 1416 | pipelines_[id] = pipeline; |
| 1417 | if (shader && device_fault_) device_fault_->pipeline_names[id] = {shader}; | ||
| 1285 | } | 1418 | } |
| 1286 | void remove_pipeline(std::uintptr_t id) { | 1419 | void remove_pipeline(std::uintptr_t id) { |
| 1287 | std::unique_lock lock(resource_mutex_); | 1420 | std::unique_lock lock(resource_mutex_); |
| 1288 | pipelines_.erase(id); | 1421 | pipelines_.erase(id); |
| 1289 | graphics_pipelines_.erase(id); | 1422 | graphics_pipelines_.erase(id); |
| 1423 | if (device_fault_) device_fault_->pipeline_names.erase(id); | ||
| 1290 | } | 1424 | } |
| 1291 | void add_graphics_pipeline(std::uintptr_t id, const GraphicsPipeline& pipeline) { | 1425 | void add_graphics_pipeline(std::uintptr_t id, const GraphicsPipeline& pipeline, |
| 1426 | const CheckpointShaders* shaders = nullptr) { | ||
| 1292 | std::unique_lock lock(resource_mutex_); | 1427 | std::unique_lock lock(resource_mutex_); |
| 1293 | graphics_pipelines_[id] = pipeline; | 1428 | graphics_pipelines_[id] = pipeline; |
| 1429 | if (shaders && device_fault_) device_fault_->pipeline_names[id] = *shaders; | ||
| 1294 | } | 1430 | } |
| 1295 | // Configure before queue creation, when the selected queue family supports compute. | 1431 | // Configure before queue creation, when the selected queue family supports compute. |
| 1296 | void set_occlusion_resolve_memory(const VkPhysicalDeviceMemoryProperties& memory) { | 1432 | void set_occlusion_resolve_memory(const VkPhysicalDeviceMemoryProperties& memory) { |
| 1297 | occlusion_resolve_memory_ = memory; | 1433 | occlusion_resolve_memory_ = memory; |
| 1298 | } | 1434 | } |
| 1299 | 1435 | ||
| 1436 | struct HostTextureStats { std::uint64_t copies = 0, waits = 0; }; | ||
| 1437 | HostTextureStats host_texture_stats() { | ||
| 1438 | std::lock_guard lock(submit_mutex_); | ||
| 1439 | return host_texture_stats_; | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | // CPU writes to a title texture have no application fence. Prevent another queue from | ||
| 1443 | // claiming a new layout or submitting work between the completion check and the host copy. | ||
| 1444 | // An unsatisfied console Wait lives in QueueHolds, outside the Vulkan queues. | ||
| 1445 | template<class Copy> | ||
| 1446 | VkResult write_host_texture(ResourceId id, std::uint32_t subresource, Copy&& copy) { | ||
| 1447 | std::lock_guard layout_lock(tracker_mutex_); | ||
| 1448 | std::lock_guard queue_lock(submit_mutex_); | ||
| 1449 | if (tracker_.layout(id, subresource) != VK_IMAGE_LAYOUT_GENERAL) return VK_NOT_READY; | ||
| 1450 | if (image_work_pending_) { | ||
| 1451 | ++host_texture_stats_.waits; | ||
| 1452 | const auto result = observe_result(vk.vkDeviceWaitIdle(device_), "vkDeviceWaitIdle (title texture upload)"); | ||
| 1453 | if (result != VK_SUCCESS) return result; | ||
| 1454 | image_work_pending_ = false; | ||
| 1455 | } | ||
| 1456 | if (!copy()) return VK_ERROR_UNKNOWN; | ||
| 1457 | ++host_texture_stats_.copies; | ||
| 1458 | return VK_SUCCESS; | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | // A complete CPU-side replacement of these color subresources. Serialize with replay and | ||
| 1462 | // present while recording the old layouts, finishing prior reads and publishing the copy. | ||
| 1463 | template<class Copy> | ||
| 1464 | VkResult write_staged_title_image(ResourceId id, const VkImageSubresourceRange& range, Copy&& copy) { | ||
| 1465 | std::lock_guard layout_lock(tracker_mutex_); | ||
| 1466 | std::lock_guard queue_lock(submit_mutex_); | ||
| 1467 | LayoutPieces pieces; | ||
| 1468 | if (!tracker_.pieces(id, range, pieces) || pieces.empty()) return VK_ERROR_UNKNOWN; | ||
| 1469 | if (image_work_pending_) { | ||
| 1470 | const auto r = observe_result(vk.vkDeviceWaitIdle(device_), "vkDeviceWaitIdle (title staging)"); | ||
| 1471 | if (r != VK_SUCCESS) return r; | ||
| 1472 | image_work_pending_ = false; | ||
| 1473 | } | ||
| 1474 | const auto r = observe_result(copy(replay_, pieces), "title staging upload"); | ||
| 1475 | if (r == VK_SUCCESS) { | ||
| 1476 | for (const auto& p : pieces) | ||
| 1477 | if (p.layout == VK_IMAGE_LAYOUT_UNDEFINED) tracker_.set(id, p.range, VK_IMAGE_LAYOUT_GENERAL); | ||
| 1478 | } | ||
| 1479 | return r; | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | // Present and probe submissions have their own fences, outside the queue retirement timelines. | ||
| 1483 | void note_image_work_locked() { image_work_pending_ = true; } | ||
| 1484 | |||
| 1300 | // For tests and traces. | 1485 | // For tests and traces. |
| 1301 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { | 1486 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { |
| 1302 | std::lock_guard lock(tracker_mutex_); | 1487 | std::lock_guard lock(tracker_mutex_); |
| @@ -1308,6 +1493,23 @@ public: | |||
| 1308 | return resources_.find(id) != resources_.end(); | 1493 | return resources_.find(id) != resources_.end(); |
| 1309 | } | 1494 | } |
| 1310 | 1495 | ||
| 1496 | // The registered buffer whose VA range holds `va`, preferring one that holds [va, va + bytes) as buffer_at does: its | ||
| 1497 | // ID, first VA and size in bytes. False when none does, or when `acquire(id)` returns false. `acquire` runs with | ||
| 1498 | // the registry locked, so the resource cannot finish being removed under it: it takes the caller's reference | ||
| 1499 | // (Resource's destructor removes its entry first, which waits for this lock). Used where a console call names a | ||
| 1500 | // buffer by VA only (Xbox device slot 70). | ||
| 1501 | template<class Acquire> | ||
| 1502 | bool buffer_resource_at(std::uint64_t va, std::uint64_t bytes, ResourceId& id, std::uint64_t& start, | ||
| 1503 | std::uint64_t& size, Acquire&& acquire) { | ||
| 1504 | std::shared_lock lock(resource_mutex_); | ||
| 1505 | BufferVaIndex<ResourceId>::Hit hit{}; | ||
| 1506 | if (!buffer_vas_.find(va, hit, bytes) || !acquire(hit.id)) return false; | ||
| 1507 | id = hit.id; | ||
| 1508 | start = hit.start; | ||
| 1509 | size = hit.size; | ||
| 1510 | return true; | ||
| 1511 | } | ||
| 1512 | |||
| 1311 | bool ray_tracing_supported() const { return ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR != nullptr; } | 1513 | bool ray_tracing_supported() const { return ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR != nullptr; } |
| 1312 | bool xdxr_acceleration_structures() const { return xdxr_ != nullptr; } | 1514 | bool xdxr_acceleration_structures() const { return xdxr_ != nullptr; } |
| 1313 | 1515 | ||
| @@ -1382,6 +1584,56 @@ public: | |||
| 1382 | // written: the device brings the bindless set 0 of the translated shaders up to date there (vulkan_bindless.h) | 1584 | // written: the device brings the bindless set 0 of the translated shaders up to date there (vulkan_bindless.h) |
| 1383 | // and records what the set needs first. When it fails, nothing of that call is executed. | 1585 | // and records what the set needs first. When it fails, nothing of that call is executed. |
| 1384 | void set_transfer_only_family(std::uint32_t family) { transfer_only_family_ = family; } | 1586 | void set_transfer_only_family(std::uint32_t family) { transfer_only_family_ = family; } |
| 1587 | void set_graphics_family(std::uint32_t family) { graphics_family_ = family; } | ||
| 1588 | void set_compute_copy_aspects(std::function<VkImageAspectFlags(VkFormat)> check) { | ||
| 1589 | replay_.compute_copy_aspects = std::move(check); | ||
| 1590 | } | ||
| 1591 | |||
| 1592 | // `before` is called at the start of every submission of command lists (a held batch: when it is released), | ||
| 1593 | // before any lock here is taken: the device re-copies its slot-133 shadow buffers from the title's pages there. | ||
| 1594 | // Set once, before the first queue exists. | ||
| 1595 | void set_before_execute(std::function<void()> before) { before_execute_ = std::move(before); } | ||
| 1596 | |||
| 1597 | // Table VAs are offsets in this device's arena. Install its scanner before creating any queues. | ||
| 1598 | // (The per-device scan is from the patch descriptor-table-refresh, part 2, of | ||
| 1599 | // Sigtryggur-Mani-Gudmundsson/graphics-runtime-patches at d9a0bc1b, GPL-3.0.) | ||
| 1600 | using TableScanner = std::function<void(std::uint64_t, std::uint32_t)>; | ||
| 1601 | void set_table_scanner(TableScanner scan) { table_scanner_ = std::move(scan); } | ||
| 1602 | void set_cpu_texture_publisher(std::function<bool()> f) { cpu_texture_publisher_ = std::move(f); } | ||
| 1603 | bool publish_cpu_textures() const { return !cpu_texture_publisher_ || cpu_texture_publisher_(); } | ||
| 1604 | void set_sample_table_resolver(std::function<bool(std::uint64_t,std::uint32_t)> f) { sample_table_resolver_ = std::move(f); } | ||
| 1605 | bool prepare_sample_table(std::uint64_t address, std::uint32_t count) const { | ||
| 1606 | return !sample_table_resolver_ || sample_table_resolver_(address,count); | ||
| 1607 | } | ||
| 1608 | // False when this context has no scanner of its own. | ||
| 1609 | bool scan_table(std::uint64_t va, std::uint32_t descriptors) const { | ||
| 1610 | if (!table_scanner_) return false; | ||
| 1611 | table_scanner_(va, descriptors); | ||
| 1612 | return true; | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | // `before` is called at the start of every submission of command lists (a held batch: when it is released), | ||
| 1616 | // before any lock here is taken: the device re-copies its slot-133 shadow buffers from the title's pages there. | ||
| 1617 | // Set once, before the first queue exists. | ||
| 1618 | using InputProbe = std::function<void(std::span<const CommandList* const>, std::uint64_t, const char*)>; | ||
| 1619 | void set_input_probe(InputProbe probe) { input_probe_ = std::move(probe); } | ||
| 1620 | bool diagnostic_buffer(std::uint64_t va, std::uint64_t& host, std::uint64_t& room) { | ||
| 1621 | std::shared_lock lock(resource_mutex_); | ||
| 1622 | BufferVaIndex<ResourceId>::Hit hit{}; | ||
| 1623 | if (!buffer_vas_.find(va, hit)) return false; | ||
| 1624 | const auto* b = resolve(hit.id); | ||
| 1625 | if (!b || va - b->va >= b->size) return false; | ||
| 1626 | const auto offset = va - b->va; | ||
| 1627 | host = b->host ? reinterpret_cast<std::uint64_t>(b->host + offset) : 0; | ||
| 1628 | room = b->size - offset; | ||
| 1629 | return true; | ||
| 1630 | } | ||
| 1631 | const char* diagnostic_compute_name(std::uintptr_t id) { | ||
| 1632 | std::shared_lock lock(resource_mutex_); | ||
| 1633 | if (!device_fault_) return nullptr; | ||
| 1634 | const auto it = device_fault_->pipeline_names.find(id); | ||
| 1635 | return it == device_fault_->pipeline_names.end() ? nullptr : it->second[0]; | ||
| 1636 | } | ||
| 1385 | 1637 | ||
| 1386 | void set_bindless_prepare(std::function<bool(VkCommandBuffer)> prepare) { | 1638 | void set_bindless_prepare(std::function<bool(VkCommandBuffer)> prepare) { |
| 1387 | std::unique_lock lock(resource_mutex_); | 1639 | std::unique_lock lock(resource_mutex_); |
| @@ -1393,7 +1645,8 @@ public: | |||
| 1393 | void enable_depth24(PFN_vkGetDeviceProcAddr get, Depth24Converter::Allocate allocate, | 1645 | void enable_depth24(PFN_vkGetDeviceProcAddr get, Depth24Converter::Allocate allocate, |
| 1394 | std::uint64_t max_storage_range = 1u << 27) { | 1646 | std::uint64_t max_storage_range = 1u << 27) { |
| 1395 | std::unique_lock lock(resource_mutex_); | 1647 | std::unique_lock lock(resource_mutex_); |
| 1396 | depth24_ = std::make_unique<Depth24Converter>(get, device_, std::move(allocate), max_storage_range); | 1648 | depth24_ = std::make_unique<Depth24Converter>(get, device_, std::move(allocate), max_storage_range, |
| 1649 | device_fault_.get()); | ||
| 1397 | if (!depth24_->loaded()) { | 1650 | if (!depth24_->loaded()) { |
| 1398 | depth24_.reset(); | 1651 | depth24_.reset(); |
| 1399 | return; | 1652 | return; |
| @@ -1405,10 +1658,13 @@ public: | |||
| 1405 | } | 1658 | } |
| 1406 | 1659 | ||
| 1407 | // Copies the replay stages through a buffer (depth <-> colour, vulkan_replay.h) take device-local scratch from | 1660 | // Copies the replay stages through a buffer (depth <-> colour, vulkan_replay.h) take device-local scratch from |
| 1408 | // `allocate`. Without this such a copy is refused as unsupported. | 1661 | // `allocate`. Without this such a copy is refused as unsupported. `extra_usage` is added to the chunks' transfer |
| 1409 | void enable_copy_scratch(PFN_vkGetDeviceProcAddr get, ScratchBuffers::Allocate allocate) { | 1662 | // usage, for configure_quad_indices. |
| 1663 | void enable_copy_scratch(PFN_vkGetDeviceProcAddr get, ScratchBuffers::Allocate allocate, | ||
| 1664 | VkBufferUsageFlags extra_usage = 0, std::vector<std::uint32_t> families = {}) { | ||
| 1410 | std::unique_lock lock(resource_mutex_); | 1665 | std::unique_lock lock(resource_mutex_); |
| 1411 | scratch_ = std::make_unique<ScratchBuffers>(get, device_, std::move(allocate)); | 1666 | scratch_ = std::make_unique<ScratchBuffers>(get, device_, std::move(allocate), extra_usage, std::move(families), |
| 1667 | device_fault_.get()); | ||
| 1412 | if (!scratch_->loaded()) scratch_.reset(); | 1668 | if (!scratch_->loaded()) scratch_.reset(); |
| 1413 | } | 1669 | } |
| 1414 | 1670 | ||
| @@ -1472,7 +1728,8 @@ public: | |||
| 1472 | wait.semaphoreCount = std::uint32_t(semaphores.size()); | 1728 | wait.semaphoreCount = std::uint32_t(semaphores.size()); |
| 1473 | wait.pSemaphores = semaphores.data(); | 1729 | wait.pSemaphores = semaphores.data(); |
| 1474 | wait.pValues = values.data(); | 1730 | wait.pValues = values.data(); |
| 1475 | vk.vkWaitSemaphores(device_, &wait, UINT64_MAX); // outside submit_mutex_: a signal may need it | 1731 | // Outside submit_mutex_: a signal may need it. |
| 1732 | observe_result(vk.vkWaitSemaphores(device_, &wait, UINT64_MAX), "vkWaitSemaphores (retirement)"); | ||
| 1476 | } | 1733 | } |
| 1477 | return completed_retire_ticket(); | 1734 | return completed_retire_ticket(); |
| 1478 | } | 1735 | } |
| @@ -1483,12 +1740,24 @@ public: | |||
| 1483 | void note_bindless_upload() { bindless_upload_pending_ = true; } | 1740 | void note_bindless_upload() { bindless_upload_pending_ = true; } |
| 1484 | 1741 | ||
| 1485 | std::uint64_t completed_retire_ticket() { | 1742 | std::uint64_t completed_retire_ticket() { |
| 1486 | std::lock_guard lock(submit_mutex_); | 1743 | bool lost = false; |
| 1487 | if (is_device_lost()) return 0; | 1744 | std::uint64_t ticket = 0; |
| 1488 | return retire_tickets_.completed([this](VkSemaphore semaphore, std::uint64_t value) { | 1745 | { |
| 1489 | std::uint64_t done = 0; | 1746 | std::lock_guard lock(submit_mutex_); |
| 1490 | return vk.vkGetSemaphoreCounterValue(device_, semaphore, &done) == VK_SUCCESS && done >= value; | 1747 | if (is_device_lost()) return 0; |
| 1491 | }); | 1748 | ticket = retire_tickets_.completed([this, &lost](VkSemaphore semaphore, std::uint64_t value) { |
| 1749 | std::uint64_t done = 0; | ||
| 1750 | const VkResult result = observe_result(vk.vkGetSemaphoreCounterValue(device_, semaphore, &done), | ||
| 1751 | "vkGetSemaphoreCounterValue (retirement)"); | ||
| 1752 | if (result == VK_ERROR_DEVICE_LOST) lost = true; | ||
| 1753 | return result == VK_SUCCESS && done >= value; | ||
| 1754 | }); | ||
| 1755 | } | ||
| 1756 | // Outside submit_mutex_, as in submit(): notifying the fence dispatcher can run callbacks that submit. | ||
| 1757 | // A DEVICE_LOST seen here is the first Vulkan operation to discover it; it must not be mistaken for | ||
| 1758 | // "this ticket has not completed". | ||
| 1759 | if (lost) mark_device_lost(); | ||
| 1760 | return lost ? 0 : ticket; | ||
| 1492 | } | 1761 | } |
| 1493 | 1762 | ||
| 1494 | void drain_deleted_resources(std::uint64_t current_ticket) { | 1763 | void drain_deleted_resources(std::uint64_t current_ticket) { |
| @@ -1500,6 +1769,7 @@ public: | |||
| 1500 | void record_submit_locked(VkSemaphore progress, std::uint64_t value) { | 1769 | void record_submit_locked(VkSemaphore progress, std::uint64_t value) { |
| 1501 | const auto ticket = submitted_ticket_.fetch_add(1, std::memory_order_acq_rel) + 1; | 1770 | const auto ticket = submitted_ticket_.fetch_add(1, std::memory_order_acq_rel) + 1; |
| 1502 | retire_tickets_.record(ticket, progress, value); | 1771 | retire_tickets_.record(ticket, progress, value); |
| 1772 | note_image_work_locked(); | ||
| 1503 | } | 1773 | } |
| 1504 | void retire_queue_idle_locked(VkSemaphore progress) { | 1774 | void retire_queue_idle_locked(VkSemaphore progress) { |
| 1505 | retire_tickets_.queue_idle(progress); | 1775 | retire_tickets_.queue_idle(progress); |
| @@ -1510,8 +1780,8 @@ public: | |||
| 1510 | // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one. | 1780 | // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one. |
| 1511 | std::mutex& submit_mutex() { return submit_mutex_; } | 1781 | std::mutex& submit_mutex() { return submit_mutex_; } |
| 1512 | 1782 | ||
| 1513 | void mark_device_lost() noexcept { | 1783 | void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { |
| 1514 | device_lost_.store(true, std::memory_order_release); | 1784 | set_device_lost(device_lost_, where); |
| 1515 | if (fence_dispatcher_) { | 1785 | if (fence_dispatcher_) { |
| 1516 | try { | 1786 | try { |
| 1517 | fence_dispatcher_->notify_device_lost(); | 1787 | fence_dispatcher_->notify_device_lost(); |
| @@ -1569,7 +1839,14 @@ private: | |||
| 1569 | 1839 | ||
| 1570 | const ResourceBinding* resolve(ResourceId id) const { | 1840 | const ResourceBinding* resolve(ResourceId id) const { |
| 1571 | const auto it = resources_.find(id); | 1841 | const auto it = resources_.find(id); |
| 1572 | return it == resources_.end() ? nullptr : &it->second; | 1842 | if (it == resources_.end()) return nullptr; |
| 1843 | const auto& binding = it->second; | ||
| 1844 | if (auto* memory = binding.deferred_memory; memory && !memory->ensure()) return nullptr; | ||
| 1845 | // The caller holds resource_mutex_, so the registered owner's lifetime includes this bind. | ||
| 1846 | if (binding.ensure_residency && !binding.ensure_residency(binding.residency_owner)) return nullptr; | ||
| 1847 | if (auto* seen = binding.replay_seen; seen && !seen->load(std::memory_order_relaxed)) | ||
| 1848 | seen->store(true, std::memory_order_relaxed); | ||
| 1849 | return &binding; | ||
| 1573 | } | 1850 | } |
| 1574 | 1851 | ||
| 1575 | // Called from prepare; protected by views_mutex_ so concurrent shared_lock readers are safe. | 1852 | // Called from prepare; protected by views_mutex_ so concurrent shared_lock readers are safe. |
| @@ -1657,6 +1934,52 @@ private: | |||
| 1657 | return framebuffer; | 1934 | return framebuffer; |
| 1658 | } | 1935 | } |
| 1659 | 1936 | ||
| 1937 | std::uint64_t poll_retirement_locked(VkResult& result) { | ||
| 1938 | return retire_tickets_.completed([&](VkSemaphore semaphore, std::uint64_t value) { | ||
| 1939 | std::uint64_t done = 0; | ||
| 1940 | result = observe_result(vk.vkGetSemaphoreCounterValue(device_, semaphore, &done), | ||
| 1941 | "vkGetSemaphoreCounterValue (retirement)"); | ||
| 1942 | return result == VK_SUCCESS && done >= value; | ||
| 1943 | }); | ||
| 1944 | } | ||
| 1945 | |||
| 1946 | void watch_queue_progress(std::stop_token stop) { | ||
| 1947 | using Watch = SubmissionProgressWatch; | ||
| 1948 | Watch watch; | ||
| 1949 | std::uint64_t retired = 0; | ||
| 1950 | std::mutex timer_mutex; | ||
| 1951 | std::condition_variable_any timer; | ||
| 1952 | std::unique_lock timer_lock(timer_mutex); | ||
| 1953 | while (!stop.stop_requested()) { | ||
| 1954 | timer.wait_for(timer_lock, stop, std::chrono::seconds(1), [] { return false; }); | ||
| 1955 | if (stop.stop_requested() || is_device_lost()) break; | ||
| 1956 | auto state = Watch::State::submit_lock_busy; | ||
| 1957 | VkResult result = VK_SUCCESS; | ||
| 1958 | std::uint64_t submitted; | ||
| 1959 | { | ||
| 1960 | // Keep queue timelines alive as in completed_retire_ticket(). A submit or idle wait | ||
| 1961 | // blocked inside the driver can hold this mutex indefinitely. | ||
| 1962 | std::unique_lock lock(submit_mutex_, std::try_to_lock); | ||
| 1963 | submitted = submitted_retire_ticket(); | ||
| 1964 | if (lock.owns_lock()) { | ||
| 1965 | if (is_device_lost()) break; | ||
| 1966 | retired = poll_retirement_locked(result); | ||
| 1967 | state = retired < submitted ? Watch::State::pending : Watch::State::idle; | ||
| 1968 | } | ||
| 1969 | } | ||
| 1970 | if (result == VK_ERROR_DEVICE_LOST) { | ||
| 1971 | mark_device_lost(); | ||
| 1972 | break; | ||
| 1973 | } | ||
| 1974 | // A failed counter query does not measure GPU progress. | ||
| 1975 | if (result != VK_SUCCESS) state = Watch::State::idle; | ||
| 1976 | if (watch.sample(state, retired, Watch::Clock::now())) | ||
| 1977 | device_fault_->report_stall(state == Watch::State::pending ? "queue retirement has not advanced" | ||
| 1978 | : "could not sample the submit lock", | ||
| 1979 | submitted, retired); | ||
| 1980 | } | ||
| 1981 | } | ||
| 1982 | |||
| 1660 | VkDevice device_; | 1983 | VkDevice device_; |
| 1661 | bool loaded_ = false; | 1984 | bool loaded_ = false; |
| 1662 | bool ray_tracing_ = false; | 1985 | bool ray_tracing_ = false; |
| @@ -1666,9 +1989,13 @@ private: | |||
| 1666 | alignas(64) std::mutex tracker_mutex_; | 1989 | alignas(64) std::mutex tracker_mutex_; |
| 1667 | alignas(64) std::mutex views_mutex_; | 1990 | alignas(64) std::mutex views_mutex_; |
| 1668 | alignas(64) std::mutex submit_mutex_; | 1991 | alignas(64) std::mutex submit_mutex_; |
| 1992 | bool image_work_pending_ = false; // under submit_mutex_ | ||
| 1993 | HostTextureStats host_texture_stats_; // under submit_mutex_ | ||
| 1669 | RetireTickets<VkSemaphore> retire_tickets_; | 1994 | RetireTickets<VkSemaphore> retire_tickets_; |
| 1670 | std::atomic<std::uint64_t> submitted_ticket_{0}; | 1995 | std::atomic<std::uint64_t> submitted_ticket_{0}; |
| 1671 | DeferredDeletionQueue deletion_queue_; | 1996 | DeferredDeletionQueue deletion_queue_; |
| 1997 | std::unique_ptr<DeviceFaultDiagnostics> device_fault_; | ||
| 1998 | std::jthread progress_watch_; | ||
| 1672 | std::unique_ptr<FenceWaitDispatcher> fence_dispatcher_; | 1999 | std::unique_ptr<FenceWaitDispatcher> fence_dispatcher_; |
| 1673 | QueueHolds<VkSemaphore> holds_; | 2000 | QueueHolds<VkSemaphore> holds_; |
| 1674 | std::unordered_map<ResourceId, ResourceBinding> resources_; | 2001 | std::unordered_map<ResourceId, ResourceBinding> resources_; |
| @@ -1696,6 +2023,12 @@ private: | |||
| 1696 | std::map<std::tuple<VkFormat, std::uint32_t, VkImageLayout, VkImageLayout, VkResolveModeFlagBits>, VkRenderPass> | 2023 | std::map<std::tuple<VkFormat, std::uint32_t, VkImageLayout, VkImageLayout, VkResolveModeFlagBits>, VkRenderPass> |
| 1697 | resolve_passes_; | 2024 | resolve_passes_; |
| 1698 | std::map<std::tuple<VkImage, VkImage, VkImageView, VkImageView, VkRenderPass>, VkFramebuffer> resolve_framebuffers_; | 2025 | std::map<std::tuple<VkImage, VkImage, VkImageView, VkImageView, VkRenderPass>, VkFramebuffer> resolve_framebuffers_; |
| 2026 | std::function<void()> before_execute_; | ||
| 2027 | InputProbe input_probe_; | ||
| 2028 | TableScanner table_scanner_; | ||
| 2029 | GeometryProvider geometry_provider_; | ||
| 2030 | std::function<bool(std::uint64_t,std::uint32_t)> sample_table_resolver_; | ||
| 2031 | std::function<bool()> cpu_texture_publisher_; | ||
| 1699 | std::function<bool(VkCommandBuffer)> bindless_prepare_; | 2032 | std::function<bool(VkCommandBuffer)> bindless_prepare_; |
| 1700 | std::function<VkDescriptorSet()> bindless_current_; | 2033 | std::function<VkDescriptorSet()> bindless_current_; |
| 1701 | bool bindless_upload_pending_ = false; // tracker_mutex_ | 2034 | bool bindless_upload_pending_ = false; // tracker_mutex_ |
| @@ -1711,12 +2044,15 @@ private: | |||
| 1711 | double timestamp_period_ = 0; | 2044 | double timestamp_period_ = 0; |
| 1712 | bool order_timestamp_copies_ = false; | 2045 | bool order_timestamp_copies_ = false; |
| 1713 | VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; | 2046 | VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; |
| 2047 | label::Completions gpu_labels_; | ||
| 1714 | ReplayFunctions replay_; | 2048 | ReplayFunctions replay_; |
| 1715 | std::unique_ptr<PredicateReducer> predicate_reducer_; | 2049 | std::unique_ptr<PredicateReducer> predicate_reducer_; |
| 1716 | std::unique_ptr<PredicatedBufferCopier> predicated_copier_; | 2050 | std::unique_ptr<PredicatedBufferCopier> predicated_copier_; |
| 1717 | std::unique_ptr<IndirectDispatchCounter> counted_dispatch_; | 2051 | std::unique_ptr<IndirectDispatchCounter> counted_dispatch_; |
| 2052 | std::unique_ptr<QuadIndexExpander> quad_indices_; | ||
| 1718 | std::uint32_t predicate_family_ = UINT32_MAX; | 2053 | std::uint32_t predicate_family_ = UINT32_MAX; |
| 1719 | std::uint32_t transfer_only_family_ = UINT32_MAX; | 2054 | std::uint32_t transfer_only_family_ = UINT32_MAX; |
| 2055 | std::uint32_t graphics_family_ = UINT32_MAX; | ||
| 1720 | DebugLabelFunctions labels_; | 2056 | DebugLabelFunctions labels_; |
| 1721 | }; | 2057 | }; |
| 1722 | #ifdef _MSC_VER | 2058 | #ifdef _MSC_VER |
| @@ -1748,12 +2084,29 @@ public: | |||
| 1748 | std::uint64_t completed() const { | 2084 | std::uint64_t completed() const { |
| 1749 | if (context_.is_device_lost()) return UINT64_MAX; | 2085 | if (context_.is_device_lost()) return UINT64_MAX; |
| 1750 | std::uint64_t value = 0; | 2086 | std::uint64_t value = 0; |
| 1751 | const VkResult result = context_.vk.vkGetSemaphoreCounterValue(context_.device(), semaphore_, &value); | 2087 | const VkResult result = context_.observe_result( |
| 2088 | context_.vk.vkGetSemaphoreCounterValue(context_.device(), semaphore_, &value), "vkGetSemaphoreCounterValue"); | ||
| 1752 | if (result == VK_ERROR_DEVICE_LOST) { | 2089 | if (result == VK_ERROR_DEVICE_LOST) { |
| 1753 | context_.mark_device_lost(); | 2090 | context_.mark_device_lost(); |
| 1754 | return UINT64_MAX; | 2091 | return UINT64_MAX; |
| 1755 | } | 2092 | } |
| 1756 | if (result != VK_SUCCESS) return 0; | 2093 | if (result != VK_SUCCESS) return 0; |
| 2094 | if (value == UINT64_MAX) { | ||
| 2095 | // A lost device can show itself here first: measured on NVIDIA 621.24 and reported on 617.14, the counter | ||
| 2096 | // query succeeds with UINT64_MAX (and vkWaitSemaphores with a zero timeout succeeds too) before any call | ||
| 2097 | // has returned VK_ERROR_DEVICE_LOST. vkDeviceWaitIdle is what then says so, and it is also what a driver | ||
| 2098 | // needs to have returned before vkGetDeviceFaultInfoEXT answers (LEONIDA_DEVICE_FAULT=1). When | ||
| 2099 | // vkDeviceWaitIdle succeeds, nothing is recorded and the value is returned as the driver gave it. | ||
| 2100 | VkResult idle = VK_SUCCESS; | ||
| 2101 | { | ||
| 2102 | std::lock_guard lock(context_.submit_mutex()); | ||
| 2103 | idle = context_.observe_result(context_.vk.vkDeviceWaitIdle(context_.device()), | ||
| 2104 | "vkDeviceWaitIdle (a fence counter of UINT64_MAX)"); | ||
| 2105 | } | ||
| 2106 | if (idle == VK_ERROR_DEVICE_LOST) | ||
| 2107 | context_.mark_device_lost( | ||
| 2108 | "a fence's counter read UINT64_MAX and vkDeviceWaitIdle then returned VK_ERROR_DEVICE_LOST"); | ||
| 2109 | } | ||
| 1757 | return value; | 2110 | return value; |
| 1758 | } | 2111 | } |
| 1759 | 2112 | ||
| @@ -1764,13 +2117,15 @@ public: | |||
| 1764 | // it marks the context lost on a query error and prevents the dispatcher from waking. | 2117 | // it marks the context lost on a query error and prevents the dispatcher from waking. |
| 1765 | if (context_.is_device_lost()) return false; | 2118 | if (context_.is_device_lost()) return false; |
| 1766 | std::uint64_t current = 0; | 2119 | std::uint64_t current = 0; |
| 1767 | if (context_.vk.vkGetSemaphoreCounterValue(context_.device(), semaphore_, ¤t) == VK_SUCCESS && | 2120 | if (context_.observe_result(context_.vk.vkGetSemaphoreCounterValue(context_.device(), semaphore_, ¤t), |
| 2121 | "vkGetSemaphoreCounterValue") == VK_SUCCESS && | ||
| 1768 | value <= current) | 2122 | value <= current) |
| 1769 | return false; | 2123 | return false; |
| 1770 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 2124 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 1771 | info.semaphore = semaphore_; | 2125 | info.semaphore = semaphore_; |
| 1772 | info.value = value; | 2126 | info.value = value; |
| 1773 | const VkResult result = context_.vk.vkSignalSemaphore(context_.device(), &info); | 2127 | const VkResult result = context_.observe_result(context_.vk.vkSignalSemaphore(context_.device(), &info), |
| 2128 | "vkSignalSemaphore"); | ||
| 1774 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); | 2129 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); |
| 1775 | if (result != VK_SUCCESS) return false; | 2130 | if (result != VK_SUCCESS) return false; |
| 1776 | context_.holds().signaled(semaphore_, value); // queues that held a batch for this value run it now | 2131 | context_.holds().signaled(semaphore_, value); // queues that held a batch for this value run it now |
| @@ -1784,7 +2139,8 @@ public: | |||
| 1784 | info.semaphoreCount = 1; | 2139 | info.semaphoreCount = 1; |
| 1785 | info.pSemaphores = &semaphore_; | 2140 | info.pSemaphores = &semaphore_; |
| 1786 | info.pValues = &value; | 2141 | info.pValues = &value; |
| 1787 | const VkResult result = context_.vk.vkWaitSemaphores(context_.device(), &info, timeout_ns); | 2142 | const VkResult result = context_.observe_result( |
| 2143 | context_.vk.vkWaitSemaphores(context_.device(), &info, timeout_ns), "vkWaitSemaphores"); | ||
| 1788 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); | 2144 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); |
| 1789 | return result == VK_SUCCESS; | 2145 | return result == VK_SUCCESS; |
| 1790 | } | 2146 | } |
| @@ -1857,7 +2213,7 @@ public: | |||
| 1857 | if (queue_) { | 2213 | if (queue_) { |
| 1858 | std::lock_guard lock(context_.submit_mutex()); | 2214 | std::lock_guard lock(context_.submit_mutex()); |
| 1859 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); | 2215 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); |
| 1860 | const VkResult idle_result = vk.vkQueueWaitIdle(queue_); | 2216 | const VkResult idle_result = context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle"); |
| 1861 | if (idle_result == VK_SUCCESS) { | 2217 | if (idle_result == VK_SUCCESS) { |
| 1862 | if (progress_) context_.retire_queue_idle_locked(progress_); | 2218 | if (progress_) context_.retire_queue_idle_locked(progress_); |
| 1863 | } else { | 2219 | } else { |
| @@ -1978,18 +2334,21 @@ public: | |||
| 1978 | using PinFn = std::function<std::shared_ptr<void>()>; | 2334 | using PinFn = std::function<std::shared_ptr<void>()>; |
| 1979 | 2335 | ||
| 1980 | ReplayResult execute(std::span<const CommandList* const> lists, const PinFn& pin = {}) { | 2336 | ReplayResult execute(std::span<const CommandList* const> lists, const PinFn& pin = {}) { |
| 2337 | auto* diagnostics = context_.device_fault(); | ||
| 2338 | const auto execute_call = diagnostics ? diagnostics->next_execute_call() : 0; | ||
| 1981 | ReplayResult result = ReplayResult::success; | 2339 | ReplayResult result = ReplayResult::success; |
| 1982 | bool held = false; | 2340 | bool held = false; |
| 1983 | std::vector<std::shared_ptr<void>> retired; // destroyed after state_mutex_ is released | 2341 | std::vector<std::shared_ptr<void>> retired; // destroyed after state_mutex_ is released |
| 1984 | { | 2342 | { |
| 1985 | std::lock_guard state(state_mutex_); | 2343 | std::lock_guard state(state_mutex_); |
| 1986 | if (!must_hold()) { | 2344 | if (!must_hold()) { |
| 1987 | result = execute_now(lists); | 2345 | result = execute_now(lists, execute_call); |
| 1988 | } else if (context_.is_device_lost()) { | 2346 | } else if (context_.is_device_lost()) { |
| 1989 | result = ReplayResult::unsupported; | 2347 | result = queue_unsupported(__LINE__); |
| 1990 | } else { | 2348 | } else { |
| 1991 | Deferred op; | 2349 | Deferred op; |
| 1992 | op.kind = Deferred::Kind::batch; | 2350 | op.kind = Deferred::Kind::batch; |
| 2351 | op.execute_call = execute_call; | ||
| 1993 | op.lists.reserve(lists.size()); | 2352 | op.lists.reserve(lists.size()); |
| 1994 | for (const CommandList* list : lists) op.lists.push_back(*list); | 2353 | for (const CommandList* list : lists) op.lists.push_back(*list); |
| 1995 | if (pin) op.pins.push_back(pin()); | 2354 | if (pin) op.pins.push_back(pin()); |
| @@ -2068,7 +2427,7 @@ public: | |||
| 2068 | 2427 | ||
| 2069 | bool idle() { | 2428 | bool idle() { |
| 2070 | std::lock_guard lock(context_.submit_mutex()); | 2429 | std::lock_guard lock(context_.submit_mutex()); |
| 2071 | return context_.vk.vkQueueWaitIdle(queue_) == VK_SUCCESS; | 2430 | return context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle") == VK_SUCCESS; |
| 2072 | } | 2431 | } |
| 2073 | 2432 | ||
| 2074 | private: | 2433 | private: |
| @@ -2154,11 +2513,11 @@ private: | |||
| 2154 | !funcs.vkAllocateMemory || !funcs.vkFreeMemory || !funcs.vkBindBufferMemory || !funcs.vkMapMemory || | 2513 | !funcs.vkAllocateMemory || !funcs.vkFreeMemory || !funcs.vkBindBufferMemory || !funcs.vkMapMemory || |
| 2155 | !funcs.vkUnmapMemory || !funcs.vkGetBufferDeviceAddress || !vk.host_at || !vk.buffer_at || | 2514 | !funcs.vkUnmapMemory || !funcs.vkGetBufferDeviceAddress || !vk.host_at || !vk.buffer_at || |
| 2156 | !build.instance_count) | 2515 | !build.instance_count) |
| 2157 | return ReplayResult::unsupported; | 2516 | return queue_unsupported(__LINE__); |
| 2158 | const std::uint64_t bytes = 8ull * build.instance_count; | 2517 | const std::uint64_t bytes = 8ull * build.instance_count; |
| 2159 | const std::uint8_t* guest = nullptr; | 2518 | const std::uint8_t* guest = nullptr; |
| 2160 | const HostReadStatus status = vk.host_at(build.instances_va, bytes, guest); | 2519 | const HostReadStatus status = vk.host_at(build.instances_va, bytes, guest); |
| 2161 | if (status == HostReadStatus::not_cpu_written) return ReplayResult::unsupported; | 2520 | if (status == HostReadStatus::not_cpu_written) return queue_unsupported(__LINE__); |
| 2162 | if (status != HostReadStatus::ok) return ReplayResult::invalid_copy; | 2521 | if (status != HostReadStatus::ok) return ReplayResult::invalid_copy; |
| 2163 | 2522 | ||
| 2164 | KhrInstanceStage stage{}; | 2523 | KhrInstanceStage stage{}; |
| @@ -2181,7 +2540,7 @@ private: | |||
| 2181 | info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | | 2540 | info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | |
| 2182 | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; | 2541 | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; |
| 2183 | if (funcs.vkCreateBuffer(context_.device(), &info, nullptr, &stage.buffer) != VK_SUCCESS) | 2542 | if (funcs.vkCreateBuffer(context_.device(), &info, nullptr, &stage.buffer) != VK_SUCCESS) |
| 2184 | return ReplayResult::unsupported; | 2543 | return queue_unsupported(__LINE__); |
| 2185 | VkMemoryRequirements requirements{}; | 2544 | VkMemoryRequirements requirements{}; |
| 2186 | funcs.vkGetBufferMemoryRequirements(context_.device(), stage.buffer, &requirements); | 2545 | funcs.vkGetBufferMemoryRequirements(context_.device(), stage.buffer, &requirements); |
| 2187 | const auto& memory = context_.occlusion_resolve_memory_; | 2546 | const auto& memory = context_.occlusion_resolve_memory_; |
| @@ -2197,7 +2556,7 @@ private: | |||
| 2197 | } | 2556 | } |
| 2198 | if (type == memory.memoryTypeCount) { | 2557 | if (type == memory.memoryTypeCount) { |
| 2199 | discard(); | 2558 | discard(); |
| 2200 | return ReplayResult::unsupported; | 2559 | return queue_unsupported(__LINE__); |
| 2201 | } | 2560 | } |
| 2202 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; | 2561 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; |
| 2203 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; | 2562 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; |
| @@ -2207,7 +2566,7 @@ private: | |||
| 2207 | if (funcs.vkAllocateMemory(context_.device(), &allocation, nullptr, &stage.memory) != VK_SUCCESS || | 2566 | if (funcs.vkAllocateMemory(context_.device(), &allocation, nullptr, &stage.memory) != VK_SUCCESS || |
| 2208 | funcs.vkBindBufferMemory(context_.device(), stage.buffer, stage.memory, 0) != VK_SUCCESS) { | 2567 | funcs.vkBindBufferMemory(context_.device(), stage.buffer, stage.memory, 0) != VK_SUCCESS) { |
| 2209 | discard(); | 2568 | discard(); |
| 2210 | return ReplayResult::unsupported; | 2569 | return queue_unsupported(__LINE__); |
| 2211 | } | 2570 | } |
| 2212 | stage.size = bytes; | 2571 | stage.size = bytes; |
| 2213 | } | 2572 | } |
| @@ -2215,7 +2574,7 @@ private: | |||
| 2215 | void* mapped = nullptr; | 2574 | void* mapped = nullptr; |
| 2216 | if (funcs.vkMapMemory(context_.device(), stage.memory, 0, bytes, 0, &mapped) != VK_SUCCESS) { | 2575 | if (funcs.vkMapMemory(context_.device(), stage.memory, 0, bytes, 0, &mapped) != VK_SUCCESS) { |
| 2217 | discard(); | 2576 | discard(); |
| 2218 | return ReplayResult::unsupported; | 2577 | return queue_unsupported(__LINE__); |
| 2219 | } | 2578 | } |
| 2220 | // Validate and translate in one pass straight into the staging buffer; no second vector of addresses. | 2579 | // Validate and translate in one pass straight into the staging buffer; no second vector of addresses. |
| 2221 | for (std::uint32_t i = 0; i < build.instance_count; ++i) { | 2580 | for (std::uint32_t i = 0; i < build.instance_count; ++i) { |
| @@ -2241,21 +2600,25 @@ private: | |||
| 2241 | stage.address = funcs.vkGetBufferDeviceAddress(context_.device(), &device_address); | 2600 | stage.address = funcs.vkGetBufferDeviceAddress(context_.device(), &device_address); |
| 2242 | if (!stage.address) { | 2601 | if (!stage.address) { |
| 2243 | discard(); | 2602 | discard(); |
| 2244 | return ReplayResult::unsupported; | 2603 | return queue_unsupported(__LINE__); |
| 2245 | } | 2604 | } |
| 2246 | stages.push_back(std::move(stage)); | 2605 | stages.push_back(std::move(stage)); |
| 2247 | address = stages.back().address; | 2606 | address = stages.back().address; |
| 2248 | return ReplayResult::success; | 2607 | return ReplayResult::success; |
| 2249 | } | 2608 | } |
| 2250 | 2609 | ||
| 2251 | ReplayResult execute_now(std::span<const CommandList* const> lists) { | 2610 | ReplayResult execute_now(std::span<const CommandList* const> lists, std::uint64_t execute_call) { |
| 2252 | if (context_.is_device_lost() || !wait_for_room()) return ReplayResult::unsupported; | 2611 | if (context_.is_device_lost() || !wait_for_room()) return queue_unsupported(__LINE__); |
| 2612 | if (context_.before_execute_) context_.before_execute_(); | ||
| 2613 | if (context_.input_probe_) | ||
| 2614 | context_.input_probe_(lists, execute_call, kind_ == QueueKind::direct ? "direct" : | ||
| 2615 | kind_ == QueueKind::compute ? "compute" : "copy"); | ||
| 2253 | if (kind_ != QueueKind::direct) | 2616 | if (kind_ != QueueKind::direct) |
| 2254 | for (const auto* list : lists) | 2617 | for (const auto* list : lists) |
| 2255 | for (const auto& command : list->commands()) | 2618 | for (const auto& command : list->commands()) |
| 2256 | if (std::holds_alternative<ClearRenderTarget>(command) || | 2619 | if (std::holds_alternative<ClearRenderTarget>(command) || |
| 2257 | std::holds_alternative<ClearDepthStencil>(command)) | 2620 | std::holds_alternative<ClearDepthStencil>(command)) |
| 2258 | return ReplayResult::unsupported; | 2621 | return queue_unsupported(__LINE__); |
| 2259 | VkCommandBuffer command_buffer = VK_NULL_HANDLE; | 2622 | VkCommandBuffer command_buffer = VK_NULL_HANDLE; |
| 2260 | struct KhrGuard { | 2623 | struct KhrGuard { |
| 2261 | VulkanQueue* queue; | 2624 | VulkanQueue* queue; |
| @@ -2301,6 +2664,25 @@ private: | |||
| 2301 | // use only the first lists.size() entries, so stale entries are never prepared, queried or emitted. | 2664 | // use only the first lists.size() entries, so stale entries are never prepared, queried or emitted. |
| 2302 | if (prepared.size() < lists.size()) prepared.resize(lists.size()); | 2665 | if (prepared.size() < lists.size()) prepared.resize(lists.size()); |
| 2303 | ReplayFunctions replay = context_.replay_; | 2666 | ReplayFunctions replay = context_.replay_; |
| 2667 | unsigned mesh_draws = 0; | ||
| 2668 | if (pipeline_profile::enabled()) replay.mesh_draw_count = &mesh_draws; | ||
| 2669 | std::vector<VulkanContext::GeometryUpload> geometry; | ||
| 2670 | if (context_.geometry_provider_) { | ||
| 2671 | replay.title_memory = [&](std::uint64_t va, std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) { | ||
| 2672 | if (!va || !bytes || bytes>UINT64_MAX-va) return false; | ||
| 2673 | for (const auto& upload : geometry) { | ||
| 2674 | if (va < upload.source || va-upload.source > upload.size || bytes > upload.size-(va-upload.source)) continue; | ||
| 2675 | buffer=upload.buffer; offset=upload.offset+(va-upload.source); | ||
| 2676 | return true; | ||
| 2677 | } | ||
| 2678 | VulkanContext::GeometryUpload upload; | ||
| 2679 | if (!context_.geometry_provider_(va,bytes,upload)) return false; | ||
| 2680 | buffer=upload.buffer;offset=upload.offset; | ||
| 2681 | geometry.push_back(std::move(upload)); | ||
| 2682 | return true; | ||
| 2683 | }; | ||
| 2684 | } | ||
| 2685 | replay.graphics_family = family_ == context_.graphics_family_; | ||
| 2304 | replay.queue_stages = kind_ == QueueKind::copy ? detail::queue_stages_copy | 2686 | replay.queue_stages = kind_ == QueueKind::copy ? detail::queue_stages_copy |
| 2305 | : kind_ == QueueKind::compute ? detail::queue_stages_compute | 2687 | : kind_ == QueueKind::compute ? detail::queue_stages_compute |
| 2306 | : 0; | 2688 | : 0; |
| @@ -2323,12 +2705,12 @@ private: | |||
| 2323 | std::unique_ptr<IndirectDispatchCounter::Batch> counted_batch; | 2705 | std::unique_ptr<IndirectDispatchCounter::Batch> counted_batch; |
| 2324 | if (captures) { | 2706 | if (captures) { |
| 2325 | if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) | 2707 | if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) |
| 2326 | return ReplayResult::unsupported; | 2708 | return queue_unsupported(__LINE__); |
| 2327 | // The helper's lazy pipeline is shared by queues. Serialize preparation with the tracker lock, | 2709 | // The helper's lazy pipeline is shared by queues. Serialize preparation with the tracker lock, |
| 2328 | // retaining main's resource -> tracker -> submit lock order through emit and submission. | 2710 | // retaining main's resource -> tracker -> submit lock order through emit and submission. |
| 2329 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | 2711 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); |
| 2330 | batch = context_.predicate_reducer_->batch(captures); | 2712 | batch = context_.predicate_reducer_->batch(captures); |
| 2331 | if (!batch) return ReplayResult::unsupported; | 2713 | if (!batch) return queue_unsupported(__LINE__); |
| 2332 | replay.prepare_predicate = [&](VkBuffer buffer, VkDeviceSize offset, PredicateCapture& out) { | 2714 | replay.prepare_predicate = [&](VkBuffer buffer, VkDeviceSize offset, PredicateCapture& out) { |
| 2333 | return batch->prepare(buffer, offset, out); | 2715 | return batch->prepare(buffer, offset, out); |
| 2334 | }; | 2716 | }; |
| @@ -2371,6 +2753,19 @@ private: | |||
| 2371 | context_.counted_dispatch_->emit(cb, job); | 2753 | context_.counted_dispatch_->emit(cb, job); |
| 2372 | }; | 2754 | }; |
| 2373 | } | 2755 | } |
| 2756 | if (kind_ != QueueKind::copy && context_.quad_indices_ && context_.scratch_) { | ||
| 2757 | replay.prepare_quad_indices = [this, &tracker_lock]( | ||
| 2758 | VkBuffer indices, VkDeviceSize offset, VkIndexType type, std::uint64_t readable, std::uint32_t quads, | ||
| 2759 | VkBuffer scratch, VkDeviceSize scratch_offset, QuadIndexExpansion& out) { | ||
| 2760 | // Pipeline creation is lazy and shared across queues; serialize it with other replay preparation. | ||
| 2761 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | ||
| 2762 | return context_.quad_indices_->prepare(indices, offset, type, readable, quads, scratch, | ||
| 2763 | scratch_offset, out); | ||
| 2764 | }; | ||
| 2765 | replay.emit_quad_indices = [this](VkCommandBuffer cb, const QuadIndexExpansion& job) { | ||
| 2766 | context_.quad_indices_->emit(cb, job); | ||
| 2767 | }; | ||
| 2768 | } | ||
| 2374 | if (context_.depth24_ && !transfer_only) { | 2769 | if (context_.depth24_ && !transfer_only) { |
| 2375 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, | 2770 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, |
| 2376 | std::vector<Depth24Job>& parts) { | 2771 | std::vector<Depth24Job>& parts) { |
| @@ -2394,9 +2789,11 @@ private: | |||
| 2394 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); | 2789 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); |
| 2395 | }; | 2790 | }; |
| 2396 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, | 2791 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, |
| 2397 | const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, | 2792 | VkQueryPool primitives_pool, const std::uint8_t* mesh, |
| 2398 | VkBuffer dst, VkDeviceSize offset) { | 2793 | std::uint32_t first, std::uint32_t count, VkBuffer dst, |
| 2399 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); | 2794 | VkDeviceSize offset) { |
| 2795 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst, | ||
| 2796 | offset); | ||
| 2400 | }; | 2797 | }; |
| 2401 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | 2798 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, |
| 2402 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2799 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { |
| @@ -2411,7 +2808,7 @@ private: | |||
| 2411 | if (result != ReplayResult::success) return result; | 2808 | if (result != ReplayResult::success) return result; |
| 2412 | if (prepared[i].segment_count > UINT32_MAX - segments || | 2809 | if (prepared[i].segment_count > UINT32_MAX - segments || |
| 2413 | prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments) | 2810 | prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments) |
| 2414 | return ReplayResult::unsupported; | 2811 | return queue_unsupported(__LINE__); |
| 2415 | segments += prepared[i].segment_count; | 2812 | segments += prepared[i].segment_count; |
| 2416 | statistics_segments += prepared[i].statistics_segment_count; | 2813 | statistics_segments += prepared[i].statistics_segment_count; |
| 2417 | has_queries = has_queries || !prepared[i].queries.empty(); | 2814 | has_queries = has_queries || !prepared[i].queries.empty(); |
| @@ -2431,30 +2828,38 @@ private: | |||
| 2431 | return vk.vkCreateQueryPool(out->device, &info, nullptr, &out->pool) == VK_SUCCESS; | 2828 | return vk.vkCreateQueryPool(out->device, &info, nullptr, &out->pool) == VK_SUCCESS; |
| 2432 | }; | 2829 | }; |
| 2433 | std::shared_ptr<OcclusionSegmentPool> physical, statistics; | 2830 | std::shared_ptr<OcclusionSegmentPool> physical, statistics; |
| 2434 | if (segments && !segment_pool(VK_QUERY_TYPE_OCCLUSION, segments, physical)) return ReplayResult::unsupported; | 2831 | if (segments && !segment_pool(VK_QUERY_TYPE_OCCLUSION, segments, physical)) return queue_unsupported(__LINE__); |
| 2435 | // Without the compute helper a split query could not be summed: leave statistics queries whole, as before | 2832 | // Without the compute helper a split query could not be summed: leave statistics queries whole, as before |
| 2436 | // (QueryReplay::plan refuses a list whose statistics query holds a mesh draw then). | 2833 | // (QueryReplay::plan refuses a list whose statistics query holds a mesh draw then). |
| 2437 | if (statistics_segments && can_resolve_occlusion) { | 2834 | if (statistics_segments && can_resolve_occlusion) { |
| 2438 | if (!segment_pool(VK_QUERY_TYPE_PIPELINE_STATISTICS, statistics_segments, statistics)) | 2835 | if (!segment_pool(VK_QUERY_TYPE_PIPELINE_STATISTICS, statistics_segments, statistics)) |
| 2439 | return ReplayResult::unsupported; | 2836 | return queue_unsupported(__LINE__); |
| 2440 | for (const auto& list : prepared) | 2837 | for (const auto& list : prepared) |
| 2441 | statistics->mesh.insert(statistics->mesh.end(), list.statistics_mesh.begin(), | 2838 | statistics->mesh.insert(statistics->mesh.end(), list.statistics_mesh.begin(), |
| 2442 | list.statistics_mesh.end()); | 2839 | list.statistics_mesh.end()); |
| 2443 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { | 2840 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { |
| 2444 | statistics->mesh.clear(); | 2841 | statistics->mesh.clear(); |
| 2445 | } else { | 2842 | } else { |
| 2446 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping | 2843 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the |
| 2447 | // invocations and primitives, fragment and compute invocations (D3D-291). | 2844 | // fragment and compute invocations, and no other (D3D-291). |
| 2448 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | 2845 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; |
| 2449 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; | 2846 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; |
| 2450 | info.queryCount = statistics_segments; | 2847 | info.queryCount = statistics_segments; |
| 2451 | info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | | 2848 | info.pipelineStatistics = kMeshStatisticsFlags; |
| 2452 | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | | ||
| 2453 | VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | | ||
| 2454 | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; | ||
| 2455 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != | 2849 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != |
| 2456 | VK_SUCCESS) | 2850 | VK_SUCCESS) |
| 2457 | return ReplayResult::unsupported; | 2851 | return queue_unsupported(__LINE__); |
| 2852 | // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, | ||
| 2853 | // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature, | ||
| 2854 | // or the clipping field of a mesh segment stays 0. | ||
| 2855 | if (context_.mesh_queries) { | ||
| 2856 | VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | ||
| 2857 | primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT; | ||
| 2858 | primitives.queryCount = statistics_segments; | ||
| 2859 | if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr, | ||
| 2860 | &statistics->primitives_pool) != VK_SUCCESS) | ||
| 2861 | return queue_unsupported(__LINE__); | ||
| 2862 | } | ||
| 2458 | } | 2863 | } |
| 2459 | } | 2864 | } |
| 2460 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after | 2865 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after |
| @@ -2473,16 +2878,16 @@ private: | |||
| 2473 | if (queries.needs_sum || queries.needs_binary || queries.needs_statistics_sum || queries.needs_sum_binary) { | 2878 | if (queries.needs_sum || queries.needs_binary || queries.needs_statistics_sum || queries.needs_sum_binary) { |
| 2474 | if (!occlusion_resolve_) { | 2879 | if (!occlusion_resolve_) { |
| 2475 | auto helper = std::make_unique<OcclusionResolve>(context_.vk, context_.device()); | 2880 | auto helper = std::make_unique<OcclusionResolve>(context_.vk, context_.device()); |
| 2476 | if (!helper->initialize(occlusion_memory)) return ReplayResult::unsupported; | 2881 | if (!helper->initialize(occlusion_memory)) return queue_unsupported(__LINE__); |
| 2477 | occlusion_resolve_ = std::move(helper); | 2882 | occlusion_resolve_ = std::move(helper); |
| 2478 | } | 2883 | } |
| 2479 | if (!occlusion_resolve_->prepare(queries.needs_sum, queries.needs_binary, queries.needs_statistics_sum, | 2884 | if (!occlusion_resolve_->prepare(queries.needs_sum, queries.needs_binary, queries.needs_statistics_sum, |
| 2480 | queries.needs_sum_binary)) | 2885 | queries.needs_sum_binary)) |
| 2481 | return ReplayResult::unsupported; | 2886 | return queue_unsupported(__LINE__); |
| 2482 | } | 2887 | } |
| 2483 | 2888 | ||
| 2484 | CommandSlot* slot = acquire(); | 2889 | CommandSlot* slot = acquire(); |
| 2485 | if (!slot) return ReplayResult::unsupported; | 2890 | if (!slot) return queue_unsupported(__LINE__); |
| 2486 | slot->khr_instances = std::move(khr_guard.stages); // freed when this slot is reused | 2891 | slot->khr_instances = std::move(khr_guard.stages); // freed when this slot is reused |
| 2487 | khr_guard.stages.clear(); | 2892 | khr_guard.stages.clear(); |
| 2488 | slot->khr_as_queries = std::move(khr_guard.as_queries); | 2893 | slot->khr_as_queries = std::move(khr_guard.as_queries); |
| @@ -2511,13 +2916,28 @@ private: | |||
| 2511 | }; | 2916 | }; |
| 2512 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | 2917 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; |
| 2513 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | 2918 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 2514 | if (context_.vk.vkBeginCommandBuffer(command_buffer, &begin) != VK_SUCCESS) { | 2919 | context_.gpu_labels_.begin_record(command_buffer); |
| 2920 | if (context_.observe_result(context_.vk.vkBeginCommandBuffer(command_buffer, &begin), | ||
| 2921 | "vkBeginCommandBuffer") != VK_SUCCESS) { | ||
| 2515 | discard(); | 2922 | discard(); |
| 2516 | return ReplayResult::unsupported; | 2923 | return queue_unsupported(__LINE__); |
| 2924 | } | ||
| 2925 | // D3D12 ends every ExecuteCommandLists with all its work done and its writes visible to the next one on the | ||
| 2926 | // queue (implicit state decay relies on it), and a list may then touch the same memory without a barrier: | ||
| 2927 | // two submissions copying into one buffer, say. Vulkan orders submissions on a queue only by what their | ||
| 2928 | // barriers say, so this one starts with a full memory barrier against everything submitted before it | ||
| 2929 | // (synchronization validation: WRITE_AFTER_WRITE between vk_queue_test's back-to-back copies). | ||
| 2930 | if (context_.vk.vkCmdPipelineBarrier) { // a mocked context may leave it out | ||
| 2931 | VkMemoryBarrier full{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 2932 | full.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 2933 | full.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 2934 | context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | ||
| 2935 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &full, 0, nullptr, 0, nullptr); | ||
| 2517 | } | 2936 | } |
| 2518 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | 2937 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); |
| 2519 | if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { | 2938 | if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { |
| 2520 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; | 2939 | const bool ended = context_.observe_result(context_.vk.vkEndCommandBuffer(command_buffer), |
| 2940 | "vkEndCommandBuffer") == VK_SUCCESS; | ||
| 2521 | resource_lock.unlock(); | 2941 | resource_lock.unlock(); |
| 2522 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); | 2942 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); |
| 2523 | if (!submitted) discard(); | 2943 | if (!submitted) discard(); |
| @@ -2531,7 +2951,7 @@ private: | |||
| 2531 | } | 2951 | } |
| 2532 | if (bindless_commit) bindless_commit(submitted); | 2952 | if (bindless_commit) bindless_commit(submitted); |
| 2533 | tracker_lock.unlock(); | 2953 | tracker_lock.unlock(); |
| 2534 | return ReplayResult::unsupported; | 2954 | return queue_unsupported(__LINE__); |
| 2535 | } | 2955 | } |
| 2536 | replay.bindless_set = context_.bindless_current_ ? context_.bindless_current_() : VK_NULL_HANDLE; | 2956 | replay.bindless_set = context_.bindless_current_ ? context_.bindless_current_() : VK_NULL_HANDLE; |
| 2537 | const bool bindless_uploaded = std::exchange(context_.bindless_upload_pending_, false); | 2957 | const bool bindless_uploaded = std::exchange(context_.bindless_upload_pending_, false); |
| @@ -2554,8 +2974,7 @@ private: | |||
| 2554 | storage_init_seen_ = context_.storage_init_version_; | 2974 | storage_init_seen_ = context_.storage_init_version_; |
| 2555 | } | 2975 | } |
| 2556 | // The set this submission binds may be a new generation whose table another queue's submission uploads: | 2976 | // The set this submission binds may be a new generation whose table another queue's submission uploads: |
| 2557 | // wait for that one, as for the storage images (the table's buffers are used by one queue family only: | 2977 | // wait for that one, as for the storage images. The table and fallback images share the device's families. |
| 2558 | // every D3D12 queue of the device is made in info.queue_family). | ||
| 2559 | if (context_.bindless_upload_semaphore_ && context_.bindless_upload_semaphore_ != progress_ && | 2978 | if (context_.bindless_upload_semaphore_ && context_.bindless_upload_semaphore_ != progress_ && |
| 2560 | context_.bindless_upload_version_ != bindless_upload_seen_) { | 2979 | context_.bindless_upload_version_ != bindless_upload_seen_) { |
| 2561 | waits_.push_back(context_.bindless_upload_semaphore_); | 2980 | waits_.push_back(context_.bindless_upload_semaphore_); |
| @@ -2586,9 +3005,23 @@ private: | |||
| 2586 | bool wrote_readback = false; | 3005 | bool wrote_readback = false; |
| 2587 | VkPipelineStageFlags2 readback_stages = 0; | 3006 | VkPipelineStageFlags2 readback_stages = 0; |
| 2588 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists | 3007 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists |
| 3008 | CheckpointSubmission diagnostics; | ||
| 3009 | PFN_vkCmdSetCheckpointNV checkpoint = nullptr; | ||
| 3010 | if (execute_call) { | ||
| 3011 | auto& fault = *context_.device_fault(); | ||
| 3012 | checkpoint = fault.checkpoint_command(); | ||
| 3013 | const char* queue_kind = kind_ == QueueKind::direct ? "direct" : | ||
| 3014 | kind_ == QueueKind::compute ? "compute" : "copy"; | ||
| 3015 | diagnostics = fault.begin_submission(execute_call, queue_, queue_kind, lists.size()); | ||
| 3016 | } | ||
| 2589 | for (std::size_t i = 0; i < lists.size(); ++i) { | 3017 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 2590 | const auto& list = prepared[i]; | 3018 | const auto& list = prepared[i]; |
| 2591 | emit(list, command_buffer, replay, &next_tracker, &open_labels); | 3019 | if (execute_call && i < diagnostics.lists.size()) { |
| 3020 | const CheckpointRecorder recorder{checkpoint, diagnostics, context_.device_fault()->pipeline_names, i}; | ||
| 3021 | emit_commands<true>(list, command_buffer, replay, &next_tracker, &open_labels, &recorder); | ||
| 3022 | } else { | ||
| 3023 | emit(list, command_buffer, replay, &next_tracker, &open_labels); | ||
| 3024 | } | ||
| 2592 | wrote_readback |= list.wrote_readback; | 3025 | wrote_readback |= list.wrote_readback; |
| 2593 | readback_stages |= list.readback_src_stages; | 3026 | readback_stages |= list.readback_src_stages; |
| 2594 | } | 3027 | } |
| @@ -2623,14 +3056,19 @@ private: | |||
| 2623 | 0, 1, &host, 0, nullptr, 0, nullptr); | 3056 | 0, 1, &host, 0, nullptr, 0, nullptr); |
| 2624 | } | 3057 | } |
| 2625 | } | 3058 | } |
| 2626 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; | 3059 | const bool ended = context_.observe_result(context_.vk.vkEndCommandBuffer(command_buffer), |
| 3060 | "vkEndCommandBuffer") == VK_SUCCESS; | ||
| 2627 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held | 3061 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held |
| 2628 | const bool wait_previous = ordered_timestamps && submitted_; | 3062 | const bool wait_previous = ordered_timestamps && submitted_; |
| 2629 | if (wait_previous) { | 3063 | if (wait_previous) { |
| 2630 | waits_.push_back(progress_); | 3064 | waits_.push_back(progress_); |
| 2631 | wait_values_.push_back(submitted_); | 3065 | wait_values_.push_back(submitted_); |
| 2632 | } | 3066 | } |
| 2633 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); | 3067 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0, |
| 3068 | execute_call ? &diagnostics : nullptr); | ||
| 3069 | if (mesh_draws) | ||
| 3070 | std::fprintf(stderr, "[rt] pso-mesh unix_ms=%lld draws=%u submitted=%d\n", | ||
| 3071 | pipeline_profile::unix_ms(), mesh_draws, int(submitted)); | ||
| 2634 | if (!submitted && (!storage_images.empty() || !zero_buffers.empty())) { | 3072 | if (!submitted && (!storage_images.empty() || !zero_buffers.empty())) { |
| 2635 | // not initialized after all: try again with the next submission | 3073 | // not initialized after all: try again with the next submission |
| 2636 | std::lock_guard lock(context_.storage_mutex_); | 3074 | std::lock_guard lock(context_.storage_mutex_); |
| @@ -2662,6 +3100,7 @@ private: | |||
| 2662 | slot->counted_dispatch = std::move(counted_batch); | 3100 | slot->counted_dispatch = std::move(counted_batch); |
| 2663 | queries.commit(); | 3101 | queries.commit(); |
| 2664 | slot->segments = std::move(queries.retained); | 3102 | slot->segments = std::move(queries.retained); |
| 3103 | slot->geometry = std::move(geometry); | ||
| 2665 | } else { | 3104 | } else { |
| 2666 | discard(); | 3105 | discard(); |
| 2667 | } | 3106 | } |
| @@ -2677,7 +3116,7 @@ private: | |||
| 2677 | // the queue (it keeps a single upload obligation). | 3116 | // the queue (it keeps a single upload obligation). |
| 2678 | if (bindless_commit) bindless_commit(submitted); | 3117 | if (bindless_commit) bindless_commit(submitted); |
| 2679 | tracker_lock.unlock(); | 3118 | tracker_lock.unlock(); |
| 2680 | return submitted ? ReplayResult::success : ReplayResult::unsupported; | 3119 | return submitted ? ReplayResult::success : queue_unsupported(__LINE__); |
| 2681 | } | 3120 | } |
| 2682 | 3121 | ||
| 2683 | void marker_now(DebugMarker::Kind kind, const char* label) { | 3122 | void marker_now(DebugMarker::Kind kind, const char* label) { |
| @@ -2705,6 +3144,7 @@ private: | |||
| 2705 | enum class Kind : std::uint8_t { batch, signal, marker }; | 3144 | enum class Kind : std::uint8_t { batch, signal, marker }; |
| 2706 | Kind kind = Kind::batch; | 3145 | Kind kind = Kind::batch; |
| 2707 | std::uint64_t seq = 0; // QueueHolds::next_seq(): the order operations were held in, across queues | 3146 | std::uint64_t seq = 0; // QueueHolds::next_seq(): the order operations were held in, across queues |
| 3147 | std::uint64_t execute_call = 0; // original ExecuteCommandLists call for a held batch | ||
| 2708 | std::vector<VkSemaphore> waits; | 3148 | std::vector<VkSemaphore> waits; |
| 2709 | std::vector<std::uint64_t> wait_values; | 3149 | std::vector<std::uint64_t> wait_values; |
| 2710 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call | 3150 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call |
| @@ -2789,7 +3229,7 @@ private: | |||
| 2789 | for (const CommandList& list : op.lists) lists.push_back(&list); | 3229 | for (const CommandList& list : op.lists) lists.push_back(&list); |
| 2790 | // The pipeline states and root signatures of the lists are in op.pins (see execute()): they live until | 3230 | // The pipeline states and root signatures of the lists are in op.pins (see execute()): they live until |
| 2791 | // the batch has been recorded and then, with the fences, until its submission has completed (below). | 3231 | // the batch has been recorded and then, with the fences, until its submission has completed (below). |
| 2792 | const ReplayResult result = execute_now(lists); | 3232 | const ReplayResult result = execute_now(lists, op.execute_call); |
| 2793 | if (result != ReplayResult::success) { | 3233 | if (result != ReplayResult::success) { |
| 2794 | int none = static_cast<int>(ReplayResult::success); | 3234 | int none = static_cast<int>(ReplayResult::success); |
| 2795 | held_failure_.compare_exchange_strong(none, static_cast<int>(result)); | 3235 | held_failure_.compare_exchange_strong(none, static_cast<int>(result)); |
| @@ -2896,6 +3336,7 @@ private: | |||
| 2896 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes | 3336 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes |
| 2897 | std::vector<KhrInstanceStage> khr_instances; // KHR ARRAY_OF_POINTERS staging, same lifetime | 3337 | std::vector<KhrInstanceStage> khr_instances; // KHR ARRAY_OF_POINTERS staging, same lifetime |
| 2898 | std::vector<KhrAsQueryPool> khr_as_queries; // KHR postbuild COMPACTED_SIZE query pools, same lifetime | 3338 | std::vector<KhrAsQueryPool> khr_as_queries; // KHR postbuild COMPACTED_SIZE query pools, same lifetime |
| 3339 | std::vector<VulkanContext::GeometryUpload> geometry; | ||
| 2899 | }; | 3340 | }; |
| 2900 | 3341 | ||
| 2901 | struct PendingBindings { | 3342 | struct PendingBindings { |
| @@ -2918,6 +3359,7 @@ private: | |||
| 2918 | auto& vk = context_.vk; | 3359 | auto& vk = context_.vk; |
| 2919 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | 3360 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; |
| 2920 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | 3361 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 3362 | context_.gpu_labels_.begin_record(command_buffer); | ||
| 2921 | if (vk.vkBeginCommandBuffer(command_buffer, &begin) != VK_SUCCESS) { | 3363 | if (vk.vkBeginCommandBuffer(command_buffer, &begin) != VK_SUCCESS) { |
| 2922 | slot->ticket = 0; | 3364 | slot->ticket = 0; |
| 2923 | return false; | 3365 | return false; |
| @@ -3013,7 +3455,8 @@ private: | |||
| 3013 | pending_bindings_.push_back(std::move(pending)); | 3455 | pending_bindings_.push_back(std::move(pending)); |
| 3014 | { | 3456 | { |
| 3015 | std::lock_guard lock(context_.submit_mutex()); | 3457 | std::lock_guard lock(context_.submit_mutex()); |
| 3016 | const auto result = vk.vkQueueBindSparse(queue_, 1, &info, VK_NULL_HANDLE); | 3458 | const auto result = context_.observe_result(vk.vkQueueBindSparse(queue_, 1, &info, VK_NULL_HANDLE), |
| 3459 | "vkQueueBindSparse"); | ||
| 3017 | if (result != VK_SUCCESS) { | 3460 | if (result != VK_SUCCESS) { |
| 3018 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); | 3461 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); |
| 3019 | pending_bindings_.pop_back(); | 3462 | pending_bindings_.pop_back(); |
| @@ -3035,7 +3478,8 @@ private: | |||
| 3035 | std::uint64_t completed() const { | 3478 | std::uint64_t completed() const { |
| 3036 | if (context_.is_device_lost()) return 0; | 3479 | if (context_.is_device_lost()) return 0; |
| 3037 | std::uint64_t done = 0; | 3480 | std::uint64_t done = 0; |
| 3038 | const VkResult result = context_.vk.vkGetSemaphoreCounterValue(context_.device(), progress_, &done); | 3481 | const VkResult result = context_.observe_result( |
| 3482 | context_.vk.vkGetSemaphoreCounterValue(context_.device(), progress_, &done), "vkGetSemaphoreCounterValue"); | ||
| 3039 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); | 3483 | if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost(); |
| 3040 | // A failed timeline query cannot be used as a resource-retirement or command-buffer reuse ticket. | 3484 | // A failed timeline query cannot be used as a resource-retirement or command-buffer reuse ticket. |
| 3041 | return result == VK_SUCCESS ? done : 0; | 3485 | return result == VK_SUCCESS ? done : 0; |
| @@ -3056,6 +3500,7 @@ private: | |||
| 3056 | for (auto& slot : buffers_) { | 3500 | for (auto& slot : buffers_) { |
| 3057 | if (slot.ticket > done) continue; | 3501 | if (slot.ticket > done) continue; |
| 3058 | slot.segments.clear(); | 3502 | slot.segments.clear(); |
| 3503 | slot.geometry.clear(); | ||
| 3059 | recycle_khr_stages(slot.khr_instances); | 3504 | recycle_khr_stages(slot.khr_instances); |
| 3060 | recycle_khr_as_queries(slot.khr_as_queries); | 3505 | recycle_khr_as_queries(slot.khr_as_queries); |
| 3061 | for (VkEvent event : slot.events) | 3506 | for (VkEvent event : slot.events) |
| @@ -3103,7 +3548,8 @@ private: | |||
| 3103 | info.semaphoreCount = 1; | 3548 | info.semaphoreCount = 1; |
| 3104 | info.pSemaphores = &progress_; | 3549 | info.pSemaphores = &progress_; |
| 3105 | info.pValues = &oldest; | 3550 | info.pValues = &oldest; |
| 3106 | return context_.vk.vkWaitSemaphores(context_.device(), &info, kQueuedWaitNs); | 3551 | return context_.observe_result(context_.vk.vkWaitSemaphores(context_.device(), &info, kQueuedWaitNs), |
| 3552 | "vkWaitSemaphores (queue progress)"); | ||
| 3107 | } | 3553 | } |
| 3108 | 3554 | ||
| 3109 | CommandSlot* acquire() { | 3555 | CommandSlot* acquire() { |
| @@ -3143,7 +3589,8 @@ private: | |||
| 3143 | return event; | 3589 | return event; |
| 3144 | } | 3590 | } |
| 3145 | 3591 | ||
| 3146 | bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) { | 3592 | bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value, |
| 3593 | CheckpointSubmission* diagnostics = nullptr) { | ||
| 3147 | if (context_.is_device_lost()) return false; | 3594 | if (context_.is_device_lost()) return false; |
| 3148 | const std::uint64_t ticket = submitted_ + 1; | 3595 | const std::uint64_t ticket = submitted_ + 1; |
| 3149 | if (last_sparse_) { | 3596 | if (last_sparse_) { |
| @@ -3192,7 +3639,12 @@ private: | |||
| 3192 | submit.pCommandBuffers = &command_buffer; | 3639 | submit.pCommandBuffers = &command_buffer; |
| 3193 | submit.signalSemaphoreCount = signal_count; | 3640 | submit.signalSemaphoreCount = signal_count; |
| 3194 | submit.pSignalSemaphores = signals; | 3641 | submit.pSignalSemaphores = signals; |
| 3195 | res = context_.vk.vkQueueSubmit(queue_, 1, &submit, VK_NULL_HANDLE); | 3642 | // Publish before the driver can execute a marker or a concurrent fence wait can see the loss. |
| 3643 | if (diagnostics) context_.device_fault()->publish(std::move(*diagnostics)); | ||
| 3644 | const auto label_fence=context_.gpu_labels_.submit_fence(command_buffer); | ||
| 3645 | res = context_.observe_result(context_.vk.vkQueueSubmit(queue_, 1, &submit, label_fence), | ||
| 3646 | "vkQueueSubmit"); | ||
| 3647 | context_.gpu_labels_.submitted(command_buffer,res==VK_SUCCESS); | ||
| 3196 | if (res == VK_SUCCESS) { | 3648 | if (res == VK_SUCCESS) { |
| 3197 | context_.record_submit_locked(progress_, ticket); | 3649 | context_.record_submit_locked(progress_, ticket); |
| 3198 | if (signals_fence) context_.holds().signaled(fence, value); | 3650 | if (signals_fence) context_.holds().signaled(fence, value); |