upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_backend.h
diff options
context:
space:
mode:
authorEmma <47298336+zzEmmazz@users.noreply.github.com>2026-11-23 05:49:06 +0200
committerEmma <47298336+zzEmmazz@users.noreply.github.com>2026-11-23 05:49:06 +0200
commitdddf6aaa9af4d16227955ecf0c632ff3690bb13e (patch)
tree80d5abaaed2fd119b5d9f22fc65645690fa9bc90 /runtime/src/vulkan_backend.h
parentd2e6b31db24e7ca93a34f0b53b063d32ff944070 (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge main 7b7d73f8: the device function list keeps main's pipeline cache entries and the render pass ones
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h71
1 files changed, 67 insertions, 4 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 24a2155c5..644fbd9c3 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -17,6 +17,7 @@
17#include "statistics_sum_spirv.h" 17#include "statistics_sum_spirv.h"
18#include "vulkan13_features.h" 18#include "vulkan13_features.h"
19#include "vulkan_depth24.h" 19#include "vulkan_depth24.h"
20#include "vulkan_indirect_dispatch_count.h"
20#include "vulkan_scratch.h" 21#include "vulkan_scratch.h"
21#include "vulkan_replay.h" 22#include "vulkan_replay.h"
22#include "queue_families.h" 23#include "queue_families.h"
@@ -85,6 +86,7 @@ struct VulkanDeviceFunctions {
85 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ 86 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
86 X(vkCmdSetBlendConstants) X(vkCmdDraw) \ 87 X(vkCmdSetBlendConstants) X(vkCmdDraw) \
87 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) \ 88 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) \
89 X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \
88 X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) \ 90 X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) \
89 X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ 91 X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \
90 X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) 92 X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass)
@@ -872,7 +874,7 @@ public:
872 // buffer as out of bounds, which is no reason to drop the submission. 874 // buffer as out of bounds, which is no reason to drop the submission.
873 replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) { 875 replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) {
874 BufferVaIndex<ResourceId>::Hit hit{}; 876 BufferVaIndex<ResourceId>::Hit hit{};
875 if (!buffer_vas_.find(va, hit)) return false; 877 if (!buffer_vas_.find(va, hit, size)) return false; // prefers an aliasing buffer that holds all of it
876 const ResourceBinding* b = resolve(hit.id); 878 const ResourceBinding* b = resolve(hit.id);
877 if (!b) return false; 879 if (!b) return false;
878 buffer = b->buffer; 880 buffer = b->buffer;
@@ -974,6 +976,13 @@ public:
974 std::unique_lock lock(resource_mutex_); 976 std::unique_lock lock(resource_mutex_);
975 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits); 977 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits);
976 } 978 }
979 // Counts for DISPATCH-only ExecuteIndirect are read by a helper compute shader from GPU memory.
980 // Requires Vulkan bufferDeviceAddress, already enabled for the D3D12 address arena.
981 void configure_counted_dispatch(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory,
982 const VkPhysicalDeviceLimits& limits) {
983 std::unique_lock lock(resource_mutex_);
984 counted_dispatch_ = std::make_unique<IndirectDispatchCounter>(get, device_, memory, limits);
985 }
977 VkDevice device() const { return device_; } 986 VkDevice device() const { return device_; }
978 987
979 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets 988 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets
@@ -1011,6 +1020,14 @@ public:
1011 storage_pending_.insert(id); 1020 storage_pending_.insert(id);
1012 } 1021 }
1013 1022
1023 // A device-local buffer D3D12 says starts zeroed (D3D-71): the host cannot write it, so the next submission on
1024 // any queue fills it with zeros ahead of its lists. That submission publishes the same initialisation semaphore as
1025 // the storage images above, so a list on another queue that uses the buffer first waits for the fill.
1026 void zero_on_next_submit(ResourceId id, VkBuffer buffer) {
1027 std::lock_guard lock(storage_mutex_);
1028 zero_pending_.push_back({id, buffer});
1029 }
1030
1014 void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) { 1031 void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) {
1015 std::unique_lock lock(resource_mutex_); 1032 std::unique_lock lock(resource_mutex_);
1016 set_resource_locked(id, binding); 1033 set_resource_locked(id, binding);
@@ -1028,6 +1045,10 @@ public:
1028 resources_.erase(it); 1045 resources_.erase(it);
1029 sparse_buffers_.erase(id); 1046 sparse_buffers_.erase(id);
1030 } 1047 }
1048 {
1049 std::lock_guard lock(storage_mutex_); // a buffer destroyed before any submission is not filled
1050 std::erase_if(zero_pending_, [id](const PendingZero& zero) { return zero.id == id; });
1051 }
1031 if (img) { 1052 if (img) {
1032 std::lock_guard vlock(views_mutex_); 1053 std::lock_guard vlock(views_mutex_);
1033 for (auto f = resolve_framebuffers_.begin(); f != resolve_framebuffers_.end();) { 1054 for (auto f = resolve_framebuffers_.begin(); f != resolve_framebuffers_.end();) {
@@ -1437,8 +1458,13 @@ private:
1437 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; 1458 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_;
1438 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 1459 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
1439 LayoutTracker tracker_; 1460 LayoutTracker tracker_;
1440 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only 1461 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ and zero_pending_ only
1441 std::unordered_set<ResourceId> storage_pending_; 1462 std::unordered_set<ResourceId> storage_pending_;
1463 struct PendingZero {
1464 ResourceId id;
1465 VkBuffer buffer;
1466 };
1467 std::vector<PendingZero> zero_pending_; // zero_on_next_submit
1442 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter 1468 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter
1443 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before 1469 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before
1444 // the zero-fill that put it in GENERAL has run. 1470 // the zero-fill that put it in GENERAL has run.
@@ -1461,6 +1487,7 @@ private:
1461 ReplayFunctions replay_; 1487 ReplayFunctions replay_;
1462 std::unique_ptr<PredicateReducer> predicate_reducer_; 1488 std::unique_ptr<PredicateReducer> predicate_reducer_;
1463 std::unique_ptr<PredicatedBufferCopier> predicated_copier_; 1489 std::unique_ptr<PredicatedBufferCopier> predicated_copier_;
1490 std::unique_ptr<IndirectDispatchCounter> counted_dispatch_;
1464 std::uint32_t predicate_family_ = UINT32_MAX; 1491 std::uint32_t predicate_family_ = UINT32_MAX;
1465 std::uint32_t transfer_only_family_ = UINT32_MAX; 1492 std::uint32_t transfer_only_family_ = UINT32_MAX;
1466 DebugLabelFunctions labels_; 1493 DebugLabelFunctions labels_;
@@ -1584,6 +1611,12 @@ public:
1584 if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now 1611 if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now
1585 if (context_.scratch_) context_.scratch_->forget(progress_); 1612 if (context_.scratch_) context_.scratch_->forget(progress_);
1586 } 1613 }
1614 if (progress_) {
1615 // This queue is idle, so whatever initialisation it published (storage images, zero-filled buffers) has
1616 // finished: the next submission on another queue must not wait on a semaphore that is about to go.
1617 std::lock_guard lock(context_.storage_mutex_);
1618 if (context_.storage_init_semaphore_ == progress_) context_.storage_init_semaphore_ = VK_NULL_HANDLE;
1619 }
1587 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); 1620 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr);
1588 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); 1621 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr);
1589 for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end()); 1622 for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end());
@@ -1785,6 +1818,7 @@ private:
1785 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures; 1818 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures;
1786 std::unique_ptr<PredicateReducer::Batch> batch; 1819 std::unique_ptr<PredicateReducer::Batch> batch;
1787 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch; 1820 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch;
1821 std::unique_ptr<IndirectDispatchCounter::Batch> counted_batch;
1788 if (captures) { 1822 if (captures) {
1789 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) 1823 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_)
1790 return ReplayResult::unsupported; 1824 return ReplayResult::unsupported;
@@ -1822,6 +1856,19 @@ private:
1822 return context_.scratch_->get(bytes, buffer, offset); 1856 return context_.scratch_->get(bytes, buffer, offset);
1823 }; 1857 };
1824 } 1858 }
1859 if (kind_ != QueueKind::copy && context_.counted_dispatch_) {
1860 replay.prepare_counted_dispatch = [this, &tracker_lock, &counted_batch](
1861 VkBuffer args, VkDeviceSize args_offset, VkBuffer count, VkDeviceSize count_offset,
1862 std::uint32_t stride, std::uint32_t maximum, CountedDispatch& out) {
1863 // Pipeline creation is lazy and shared across queues; serialize it with other replay preparation.
1864 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1865 if (!counted_batch) counted_batch = context_.counted_dispatch_->batch();
1866 return counted_batch->prepare(args, args_offset, count, count_offset, stride, maximum, out);
1867 };
1868 replay.emit_counted_dispatch = [this](VkCommandBuffer cb, const CountedDispatch& job) {
1869 context_.counted_dispatch_->emit(cb, job);
1870 };
1871 }
1825 if (context_.depth24_ && !transfer_only) { 1872 if (context_.depth24_ && !transfer_only) {
1826 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, 1873 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job,
1827 std::vector<Depth24Job>& parts) { 1874 std::vector<Depth24Job>& parts) {
@@ -1966,6 +2013,7 @@ private:
1966 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) || 2013 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) ||
1967 std::holds_alternative<ExecuteIndirect>(command); 2014 std::holds_alternative<ExecuteIndirect>(command);
1968 std::vector<ResourceId> storage_images; 2015 std::vector<ResourceId> storage_images;
2016 std::vector<VulkanContext::PendingZero> zero_buffers;
1969 { 2017 {
1970 std::lock_guard lock(context_.storage_mutex_); 2018 std::lock_guard lock(context_.storage_mutex_);
1971 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ && 2019 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ &&
@@ -1977,6 +2025,16 @@ private:
1977 if (kind_ != QueueKind::copy && runs_shaders) 2025 if (kind_ != QueueKind::copy && runs_shaders)
1978 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end()); 2026 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end());
1979 if (!storage_images.empty()) context_.storage_pending_.clear(); 2027 if (!storage_images.empty()) context_.storage_pending_.clear();
2028 zero_buffers.swap(context_.zero_pending_); // any queue can fill a buffer
2029 }
2030 for (const auto& zero : zero_buffers)
2031 context_.vk.vkCmdFillBuffer(command_buffer, zero.buffer, 0, VK_WHOLE_SIZE, 0);
2032 if (!zero_buffers.empty()) {
2033 VkMemoryBarrier filled{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
2034 filled.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
2035 filled.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
2036 context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
2037 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &filled, 0, nullptr, 0, nullptr);
1980 } 2038 }
1981 for (const ResourceId id : storage_images) { 2039 for (const ResourceId id : storage_images) {
1982 const ResourceBinding* image = context_.resolve(id); 2040 const ResourceBinding* image = context_.resolve(id);
@@ -2032,11 +2090,13 @@ private:
2032 wait_values_.push_back(submitted_); 2090 wait_values_.push_back(submitted_);
2033 } 2091 }
2034 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 2092 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
2035 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission 2093 if (!submitted && (!storage_images.empty() || !zero_buffers.empty())) {
2094 // not initialized after all: try again with the next submission
2036 std::lock_guard lock(context_.storage_mutex_); 2095 std::lock_guard lock(context_.storage_mutex_);
2037 context_.storage_pending_.insert(storage_images.begin(), storage_images.end()); 2096 context_.storage_pending_.insert(storage_images.begin(), storage_images.end());
2097 context_.zero_pending_.insert(context_.zero_pending_.end(), zero_buffers.begin(), zero_buffers.end());
2038 } 2098 }
2039 if (submitted && !storage_images.empty()) { 2099 if (submitted && (!storage_images.empty() || !zero_buffers.empty())) {
2040 std::lock_guard lock(context_.storage_mutex_); 2100 std::lock_guard lock(context_.storage_mutex_);
2041 context_.storage_init_semaphore_ = progress_; 2101 context_.storage_init_semaphore_ = progress_;
2042 context_.storage_init_value_ = submitted_; 2102 context_.storage_init_value_ = submitted_;
@@ -2051,6 +2111,7 @@ private:
2051 context_.tracker_ = std::move(next_tracker); 2111 context_.tracker_ = std::move(next_tracker);
2052 slot->predicate = std::move(batch); 2112 slot->predicate = std::move(batch);
2053 slot->predicated_copy = std::move(copy_batch); 2113 slot->predicated_copy = std::move(copy_batch);
2114 slot->counted_dispatch = std::move(counted_batch);
2054 queries.commit(); 2115 queries.commit();
2055 slot->segments = std::move(queries.retained); 2116 slot->segments = std::move(queries.retained);
2056 } else { 2117 } else {
@@ -2205,6 +2266,7 @@ private:
2205 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; 2266 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments;
2206 std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes 2267 std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes
2207 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; 2268 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy;
2269 std::unique_ptr<IndirectDispatchCounter::Batch> counted_dispatch;
2208 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes 2270 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes
2209 }; 2271 };
2210 2272
@@ -2323,6 +2385,7 @@ private:
2323 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; 2385 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr;
2324 reuse->predicate.reset(); 2386 reuse->predicate.reset();
2325 reuse->predicated_copy.reset(); 2387 reuse->predicated_copy.reset();
2388 reuse->counted_dispatch.reset();
2326 reuse->ticket = UINT64_MAX; // in use until submitted 2389 reuse->ticket = UINT64_MAX; // in use until submitted
2327 return reuse; 2390 return reuse;
2328 } 2391 }