upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/command_list.h5
-rw-r--r--runtime/src/vulkan_backend.h57
-rw-r--r--runtime/src/vulkan_replay.h19
3 files changed, 81 insertions, 0 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index 9c74994e1..282863f7c 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -565,6 +565,11 @@ struct ExecuteIndirect {
565 std::uint64_t argument_offset = 0; 565 std::uint64_t argument_offset = 0;
566 ResourceId count_buffer = 0; // 0: fixed max_count; non-zero: vkCmdDraw*IndirectCount 566 ResourceId count_buffer = 0; // 0: fixed max_count; non-zero: vkCmdDraw*IndirectCount
567 std::uint64_t count_offset = 0; 567 std::uint64_t count_offset = 0;
568 // kind == dispatch: the compute pipeline and root arguments in effect, as Dispatch records them, so the dispatches
569 // run that pipeline with those arguments whatever the command buffer had bound. A pipeline of 0 leaves the binding
570 // as it is.
571 std::uintptr_t pipeline = 0;
572 std::array<std::uint32_t, 32> user_data{};
568}; 573};
569 574
570// Triangle geometry descriptor for a BLAS build (ID3D12GraphicsCommandList4::BuildRaytracingAccelerationStructure). 575// Triangle geometry descriptor for a BLAS build (ID3D12GraphicsCommandList4::BuildRaytracingAccelerationStructure).
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
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 0b46198a9..711bd810c 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -2392,6 +2392,13 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2392 (ind->kind == IndirectKind::dispatch && !vk.dispatch_indirect)) 2392 (ind->kind == IndirectKind::dispatch && !vk.dispatch_indirect))
2393 return ReplayResult::unsupported; 2393 return ReplayResult::unsupported;
2394 } 2394 }
2395 if (ind->kind == IndirectKind::dispatch && ind->pipeline) { // the pipeline and root arguments it was recorded
2396 if (!vk.bind_pipeline || !vk.push_constants || !vk.compute_pipeline) return ReplayResult::unsupported;
2397 const ComputePipeline* pipeline = vk.compute_pipeline(ind->pipeline);
2398 if (!pipeline || !pipeline->pipeline) return ReplayResult::unresolved_resource;
2399 if (pipeline->bindless_set && !vk.bind_descriptor_sets) return ReplayResult::unsupported;
2400 p.pipeline = *pipeline;
2401 }
2395 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) { 2402 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) {
2396 if (vk.xdxr_check) { 2403 if (vk.xdxr_check) {
2397 if (!vk.xdxr_check(*build)) return ReplayResult::unsupported; 2404 if (!vk.xdxr_check(*build)) return ReplayResult::unsupported;
@@ -3113,6 +3120,18 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
3113 } else if (ind->kind == IndirectKind::draw_indexed) { 3120 } else if (ind->kind == IndirectKind::draw_indexed) {
3114 vk.draw_indexed_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride); 3121 vk.draw_indexed_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride);
3115 } else if (ind->kind == IndirectKind::dispatch) { 3122 } else if (ind->kind == IndirectKind::dispatch) {
3123 if (ind->pipeline) {
3124 ShaderUserData data{};
3125 std::copy(ind->user_data.begin(), ind->user_data.end(), data.s);
3126 data.heap_base = vk.heap_base;
3127 data.code_base = p.pipeline.code_base;
3128 vk.bind_pipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, p.pipeline.pipeline);
3129 if (p.pipeline.bindless_set)
3130 vk.bind_descriptor_sets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, p.pipeline.layout, 0,
3131 1, &p.pipeline.bindless_set, 0, nullptr);
3132 vk.push_constants(command_buffer, p.pipeline.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(data),
3133 &data);
3134 }
3116 for (std::uint32_t k = 0; k < ind->max_count; ++k) { 3135 for (std::uint32_t k = 0; k < ind->max_count; ++k) {
3117 vk.dispatch_indirect(command_buffer, p.binding.buffer, 3136 vk.dispatch_indirect(command_buffer, p.binding.buffer,
3118 arguments + VkDeviceSize(k) * ind->stride); 3137 arguments + VkDeviceSize(k) * ind->stride);