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:
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 03ab7ba26..d247b8cb0 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#define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; 91#define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr;
90 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) 92 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER)
@@ -866,7 +868,7 @@ public:
866 // buffer as out of bounds, which is no reason to drop the submission. 868 // buffer as out of bounds, which is no reason to drop the submission.
867 replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) { 869 replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) {
868 BufferVaIndex<ResourceId>::Hit hit{}; 870 BufferVaIndex<ResourceId>::Hit hit{};
869 if (!buffer_vas_.find(va, hit)) return false; 871 if (!buffer_vas_.find(va, hit, size)) return false; // prefers an aliasing buffer that holds all of it
870 const ResourceBinding* b = resolve(hit.id); 872 const ResourceBinding* b = resolve(hit.id);
871 if (!b) return false; 873 if (!b) return false;
872 buffer = b->buffer; 874 buffer = b->buffer;
@@ -953,6 +955,13 @@ public:
953 std::unique_lock lock(resource_mutex_); 955 std::unique_lock lock(resource_mutex_);
954 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits); 956 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits);
955 } 957 }
958 // Counts for DISPATCH-only ExecuteIndirect are read by a helper compute shader from GPU memory.
959 // Requires Vulkan bufferDeviceAddress, already enabled for the D3D12 address arena.
960 void configure_counted_dispatch(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory,
961 const VkPhysicalDeviceLimits& limits) {
962 std::unique_lock lock(resource_mutex_);
963 counted_dispatch_ = std::make_unique<IndirectDispatchCounter>(get, device_, memory, limits);
964 }
956 VkDevice device() const { return device_; } 965 VkDevice device() const { return device_; }
957 966
958 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets 967 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets
@@ -990,6 +999,14 @@ public:
990 storage_pending_.insert(id); 999 storage_pending_.insert(id);
991 } 1000 }
992 1001
1002 // A device-local buffer D3D12 says starts zeroed (D3D-71): the host cannot write it, so the next submission on
1003 // any queue fills it with zeros ahead of its lists. That submission publishes the same initialisation semaphore as
1004 // the storage images above, so a list on another queue that uses the buffer first waits for the fill.
1005 void zero_on_next_submit(ResourceId id, VkBuffer buffer) {
1006 std::lock_guard lock(storage_mutex_);
1007 zero_pending_.push_back({id, buffer});
1008 }
1009
993 void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) { 1010 void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) {
994 std::unique_lock lock(resource_mutex_); 1011 std::unique_lock lock(resource_mutex_);
995 set_resource_locked(id, binding); 1012 set_resource_locked(id, binding);
@@ -1007,6 +1024,10 @@ public:
1007 resources_.erase(it); 1024 resources_.erase(it);
1008 sparse_buffers_.erase(id); 1025 sparse_buffers_.erase(id);
1009 } 1026 }
1027 {
1028 std::lock_guard lock(storage_mutex_); // a buffer destroyed before any submission is not filled
1029 std::erase_if(zero_pending_, [id](const PendingZero& zero) { return zero.id == id; });
1030 }
1010 if (img) { 1031 if (img) {
1011 std::lock_guard vlock(views_mutex_); 1032 std::lock_guard vlock(views_mutex_);
1012 for (auto v = views_.begin(); v != views_.end();) { 1033 for (auto v = views_.begin(); v != views_.end();) {
@@ -1346,8 +1367,13 @@ private:
1346 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; 1367 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_;
1347 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 1368 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
1348 LayoutTracker tracker_; 1369 LayoutTracker tracker_;
1349 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only 1370 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ and zero_pending_ only
1350 std::unordered_set<ResourceId> storage_pending_; 1371 std::unordered_set<ResourceId> storage_pending_;
1372 struct PendingZero {
1373 ResourceId id;
1374 VkBuffer buffer;
1375 };
1376 std::vector<PendingZero> zero_pending_; // zero_on_next_submit
1351 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter 1377 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter
1352 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before 1378 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before
1353 // the zero-fill that put it in GENERAL has run. 1379 // the zero-fill that put it in GENERAL has run.
@@ -1366,6 +1392,7 @@ private:
1366 ReplayFunctions replay_; 1392 ReplayFunctions replay_;
1367 std::unique_ptr<PredicateReducer> predicate_reducer_; 1393 std::unique_ptr<PredicateReducer> predicate_reducer_;
1368 std::unique_ptr<PredicatedBufferCopier> predicated_copier_; 1394 std::unique_ptr<PredicatedBufferCopier> predicated_copier_;
1395 std::unique_ptr<IndirectDispatchCounter> counted_dispatch_;
1369 std::uint32_t predicate_family_ = UINT32_MAX; 1396 std::uint32_t predicate_family_ = UINT32_MAX;
1370 std::uint32_t transfer_only_family_ = UINT32_MAX; 1397 std::uint32_t transfer_only_family_ = UINT32_MAX;
1371 DebugLabelFunctions labels_; 1398 DebugLabelFunctions labels_;
@@ -1489,6 +1516,12 @@ public:
1489 if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now 1516 if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now
1490 if (context_.scratch_) context_.scratch_->forget(progress_); 1517 if (context_.scratch_) context_.scratch_->forget(progress_);
1491 } 1518 }
1519 if (progress_) {
1520 // This queue is idle, so whatever initialisation it published (storage images, zero-filled buffers) has
1521 // finished: the next submission on another queue must not wait on a semaphore that is about to go.
1522 std::lock_guard lock(context_.storage_mutex_);
1523 if (context_.storage_init_semaphore_ == progress_) context_.storage_init_semaphore_ = VK_NULL_HANDLE;
1524 }
1492 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); 1525 if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr);
1493 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); 1526 if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr);
1494 for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end()); 1527 for (auto& slot : buffers_) free_events_.insert(free_events_.end(), slot.events.begin(), slot.events.end());
@@ -1690,6 +1723,7 @@ private:
1690 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures; 1723 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures;
1691 std::unique_ptr<PredicateReducer::Batch> batch; 1724 std::unique_ptr<PredicateReducer::Batch> batch;
1692 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch; 1725 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch;
1726 std::unique_ptr<IndirectDispatchCounter::Batch> counted_batch;
1693 if (captures) { 1727 if (captures) {
1694 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) 1728 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_)
1695 return ReplayResult::unsupported; 1729 return ReplayResult::unsupported;
@@ -1727,6 +1761,19 @@ private:
1727 return context_.scratch_->get(bytes, buffer, offset); 1761 return context_.scratch_->get(bytes, buffer, offset);
1728 }; 1762 };
1729 } 1763 }
1764 if (kind_ != QueueKind::copy && context_.counted_dispatch_) {
1765 replay.prepare_counted_dispatch = [this, &tracker_lock, &counted_batch](
1766 VkBuffer args, VkDeviceSize args_offset, VkBuffer count, VkDeviceSize count_offset,
1767 std::uint32_t stride, std::uint32_t maximum, CountedDispatch& out) {
1768 // Pipeline creation is lazy and shared across queues; serialize it with other replay preparation.
1769 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1770 if (!counted_batch) counted_batch = context_.counted_dispatch_->batch();
1771 return counted_batch->prepare(args, args_offset, count, count_offset, stride, maximum, out);
1772 };
1773 replay.emit_counted_dispatch = [this](VkCommandBuffer cb, const CountedDispatch& job) {
1774 context_.counted_dispatch_->emit(cb, job);
1775 };
1776 }
1730 if (context_.depth24_ && !transfer_only) { 1777 if (context_.depth24_ && !transfer_only) {
1731 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, 1778 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job,
1732 std::vector<Depth24Job>& parts) { 1779 std::vector<Depth24Job>& parts) {
@@ -1871,6 +1918,7 @@ private:
1871 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) || 1918 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) ||
1872 std::holds_alternative<ExecuteIndirect>(command); 1919 std::holds_alternative<ExecuteIndirect>(command);
1873 std::vector<ResourceId> storage_images; 1920 std::vector<ResourceId> storage_images;
1921 std::vector<VulkanContext::PendingZero> zero_buffers;
1874 { 1922 {
1875 std::lock_guard lock(context_.storage_mutex_); 1923 std::lock_guard lock(context_.storage_mutex_);
1876 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ && 1924 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ &&
@@ -1882,6 +1930,16 @@ private:
1882 if (kind_ != QueueKind::copy && runs_shaders) 1930 if (kind_ != QueueKind::copy && runs_shaders)
1883 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end()); 1931 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end());
1884 if (!storage_images.empty()) context_.storage_pending_.clear(); 1932 if (!storage_images.empty()) context_.storage_pending_.clear();
1933 zero_buffers.swap(context_.zero_pending_); // any queue can fill a buffer
1934 }
1935 for (const auto& zero : zero_buffers)
1936 context_.vk.vkCmdFillBuffer(command_buffer, zero.buffer, 0, VK_WHOLE_SIZE, 0);
1937 if (!zero_buffers.empty()) {
1938 VkMemoryBarrier filled{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
1939 filled.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1940 filled.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
1941 context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
1942 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &filled, 0, nullptr, 0, nullptr);
1885 } 1943 }
1886 for (const ResourceId id : storage_images) { 1944 for (const ResourceId id : storage_images) {
1887 const ResourceBinding* image = context_.resolve(id); 1945 const ResourceBinding* image = context_.resolve(id);
@@ -1937,11 +1995,13 @@ private:
1937 wait_values_.push_back(submitted_); 1995 wait_values_.push_back(submitted_);
1938 } 1996 }
1939 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1997 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1940 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission 1998 if (!submitted && (!storage_images.empty() || !zero_buffers.empty())) {
1999 // not initialized after all: try again with the next submission
1941 std::lock_guard lock(context_.storage_mutex_); 2000 std::lock_guard lock(context_.storage_mutex_);
1942 context_.storage_pending_.insert(storage_images.begin(), storage_images.end()); 2001 context_.storage_pending_.insert(storage_images.begin(), storage_images.end());
2002 context_.zero_pending_.insert(context_.zero_pending_.end(), zero_buffers.begin(), zero_buffers.end());
1943 } 2003 }
1944 if (submitted && !storage_images.empty()) { 2004 if (submitted && (!storage_images.empty() || !zero_buffers.empty())) {
1945 std::lock_guard lock(context_.storage_mutex_); 2005 std::lock_guard lock(context_.storage_mutex_);
1946 context_.storage_init_semaphore_ = progress_; 2006 context_.storage_init_semaphore_ = progress_;
1947 context_.storage_init_value_ = submitted_; 2007 context_.storage_init_value_ = submitted_;
@@ -1956,6 +2016,7 @@ private:
1956 context_.tracker_ = std::move(next_tracker); 2016 context_.tracker_ = std::move(next_tracker);
1957 slot->predicate = std::move(batch); 2017 slot->predicate = std::move(batch);
1958 slot->predicated_copy = std::move(copy_batch); 2018 slot->predicated_copy = std::move(copy_batch);
2019 slot->counted_dispatch = std::move(counted_batch);
1959 queries.commit(); 2020 queries.commit();
1960 slot->segments = std::move(queries.retained); 2021 slot->segments = std::move(queries.retained);
1961 } else { 2022 } else {
@@ -2110,6 +2171,7 @@ private:
2110 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; 2171 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments;
2111 std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes 2172 std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes
2112 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; 2173 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy;
2174 std::unique_ptr<IndirectDispatchCounter::Batch> counted_dispatch;
2113 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes 2175 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes
2114 }; 2176 };
2115 2177
@@ -2228,6 +2290,7 @@ private:
2228 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; 2290 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr;
2229 reuse->predicate.reset(); 2291 reuse->predicate.reset();
2230 reuse->predicated_copy.reset(); 2292 reuse->predicated_copy.reset();
2293 reuse->counted_dispatch.reset();
2231 reuse->ticket = UINT64_MAX; // in use until submitted 2294 reuse->ticket = UINT64_MAX; // in use until submitted
2232 return reuse; 2295 return reuse;
2233 } 2296 }