diff options
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 814f53549..8f9759c89 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -29,6 +29,7 @@ | |||
| 29 | #include <map> | 29 | #include <map> |
| 30 | #include <memory> | 30 | #include <memory> |
| 31 | #include <mutex> | 31 | #include <mutex> |
| 32 | #include <unordered_set> | ||
| 32 | #include <shared_mutex> | 33 | #include <shared_mutex> |
| 33 | #include <span> | 34 | #include <span> |
| 34 | #include <thread> | 35 | #include <thread> |
| @@ -819,6 +820,15 @@ public: | |||
| 819 | } | 820 | } |
| 820 | } | 821 | } |
| 821 | 822 | ||
| 823 | // An image that got a storage (unordered-access) view. A shader can write it before any command names it, so the | ||
| 824 | // layer would not know it was used and the first barrier or copy that does would zero-fill it (vulkan_replay.h, | ||
| 825 | // initialize). The next submission therefore moves each such image to GENERAL, the layout its descriptor names, | ||
| 826 | // zero-filled, ahead of its lists. An image already tracked beyond UNDEFINED is left as it is. | ||
| 827 | void note_storage_image(ResourceId id) { | ||
| 828 | std::lock_guard lock(storage_mutex_); | ||
| 829 | storage_pending_.insert(id); | ||
| 830 | } | ||
| 831 | |||
| 822 | void remove_resource(ResourceId id) { | 832 | void remove_resource(ResourceId id) { |
| 823 | VkImage img = VK_NULL_HANDLE; | 833 | VkImage img = VK_NULL_HANDLE; |
| 824 | { | 834 | { |
| @@ -1124,6 +1134,14 @@ private: | |||
| 1124 | std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; | 1134 | std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; |
| 1125 | std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; | 1135 | std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; |
| 1126 | LayoutTracker tracker_; | 1136 | LayoutTracker tracker_; |
| 1137 | std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only | ||
| 1138 | std::unordered_set<ResourceId> storage_pending_; | ||
| 1139 | // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter | ||
| 1140 | // that says which one: every other queue's next submission waits for it, so no queue uses such an image before | ||
| 1141 | // the zero-fill that put it in GENERAL has run. | ||
| 1142 | VkSemaphore storage_init_semaphore_ = VK_NULL_HANDLE; | ||
| 1143 | std::uint64_t storage_init_value_ = 0; | ||
| 1144 | std::uint64_t storage_init_version_ = 0; | ||
| 1127 | std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; | 1145 | std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; |
| 1128 | std::function<bool(VkCommandBuffer)> bindless_prepare_; | 1146 | std::function<bool(VkCommandBuffer)> bindless_prepare_; |
| 1129 | std::unique_ptr<Depth24Converter> depth24_; | 1147 | std::unique_ptr<Depth24Converter> depth24_; |
| @@ -1439,6 +1457,33 @@ public: | |||
| 1439 | return ReplayResult::unsupported; | 1457 | return ReplayResult::unsupported; |
| 1440 | } | 1458 | } |
| 1441 | auto next_tracker = context_.tracker_; | 1459 | auto next_tracker = context_.tracker_; |
| 1460 | // Images with a UAV view are moved to GENERAL (zeroed) by the first submission that can use a shader on them: | ||
| 1461 | // not a copy queue (its family may not clear images), and one whose lists dispatch or draw. | ||
| 1462 | bool runs_shaders = false; | ||
| 1463 | for (const CommandList* list : lists) | ||
| 1464 | for (const auto& command : list->commands()) | ||
| 1465 | runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) || | ||
| 1466 | std::holds_alternative<ExecuteIndirect>(command); | ||
| 1467 | std::vector<ResourceId> storage_images; | ||
| 1468 | { | ||
| 1469 | std::lock_guard lock(context_.storage_mutex_); | ||
| 1470 | if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ && | ||
| 1471 | context_.storage_init_version_ != storage_init_seen_) { | ||
| 1472 | waits_.push_back(context_.storage_init_semaphore_); | ||
| 1473 | wait_values_.push_back(context_.storage_init_value_); | ||
| 1474 | storage_init_seen_ = context_.storage_init_version_; | ||
| 1475 | } | ||
| 1476 | if (kind_ != QueueKind::copy && runs_shaders) | ||
| 1477 | storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end()); | ||
| 1478 | if (!storage_images.empty()) context_.storage_pending_.clear(); | ||
| 1479 | } | ||
| 1480 | for (const ResourceId id : storage_images) { | ||
| 1481 | const ResourceBinding* image = context_.resolve(id); | ||
| 1482 | if (!image || !image->image) continue; | ||
| 1483 | const VkImageSubresourceRange range{image->aspects, 0, image->mip_levels, 0, image->array_layers}; | ||
| 1484 | auto pieces = detail::current(&next_tracker, id, range, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 1485 | detail::initialize(replay, command_buffer, *image, id, pieces, VK_IMAGE_LAYOUT_GENERAL, &next_tracker); | ||
| 1486 | } | ||
| 1442 | bool wrote_readback = false; | 1487 | bool wrote_readback = false; |
| 1443 | VkPipelineStageFlags2 readback_stages = 0; | 1488 | VkPipelineStageFlags2 readback_stages = 0; |
| 1444 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists | 1489 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists |
| @@ -1481,6 +1526,17 @@ public: | |||
| 1481 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; | 1526 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; |
| 1482 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held | 1527 | resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held |
| 1483 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); | 1528 | const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); |
| 1529 | if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission | ||
| 1530 | std::lock_guard lock(context_.storage_mutex_); | ||
| 1531 | context_.storage_pending_.insert(storage_images.begin(), storage_images.end()); | ||
| 1532 | } | ||
| 1533 | if (submitted && !storage_images.empty()) { | ||
| 1534 | std::lock_guard lock(context_.storage_mutex_); | ||
| 1535 | context_.storage_init_semaphore_ = progress_; | ||
| 1536 | context_.storage_init_value_ = submitted_; | ||
| 1537 | ++context_.storage_init_version_; | ||
| 1538 | storage_init_seen_ = context_.storage_init_version_; | ||
| 1539 | } | ||
| 1484 | if (submitted) { | 1540 | if (submitted) { |
| 1485 | context_.tracker_ = std::move(next_tracker); | 1541 | context_.tracker_ = std::move(next_tracker); |
| 1486 | slot->predicate = std::move(batch); | 1542 | slot->predicate = std::move(batch); |
| @@ -1632,6 +1688,7 @@ private: | |||
| 1632 | std::uint64_t submitted_ = 0; | 1688 | std::uint64_t submitted_ = 0; |
| 1633 | std::vector<CommandSlot> buffers_; | 1689 | std::vector<CommandSlot> buffers_; |
| 1634 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; | 1690 | std::unique_ptr<OcclusionResolve> occlusion_resolve_; |
| 1691 | std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for | ||
| 1635 | std::vector<VkSemaphore> waits_; | 1692 | std::vector<VkSemaphore> waits_; |
| 1636 | std::vector<std::uint64_t> wait_values_; | 1693 | std::vector<std::uint64_t> wait_values_; |
| 1637 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended | 1694 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended |