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, 69 insertions, 2 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index c04d09ea6..ea7063d92 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -3,7 +3,8 @@
3// The execution side of the D3D12 layer, without COM: the per-device Vulkan context (resources, layout tracker, 3// 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 4// image views), queues that execute command lists, and timeline-semaphore fences. The COM objects in
5// shims/d3d12_com.cpp are thin wrappers over these. Needs Vulkan 1.2 timeline semaphores; rectangle clears and 5// shims/d3d12_com.cpp are thin wrappers over these. Needs Vulkan 1.2 timeline semaphores; rectangle clears and
6// enhanced barriers need the Vulkan 1.3 functions (dynamicRendering, synchronization2) to be enabled. 6// enhanced barriers need the Vulkan 1.3 functions (dynamicRendering, synchronization2) to be enabled, as core
7// features or through their KHR extensions on a 1.2 device (vulkan13_features.h).
7 8
8#ifndef VK_NO_PROTOTYPES 9#ifndef VK_NO_PROTOTYPES
9#define VK_NO_PROTOTYPES 10#define VK_NO_PROTOTYPES
@@ -12,6 +13,7 @@
12#include "occlusion_sum_spirv.h" 13#include "occlusion_sum_spirv.h"
13#include "retire_tickets.h" 14#include "retire_tickets.h"
14#include "statistics_sum_spirv.h" 15#include "statistics_sum_spirv.h"
16#include "vulkan13_features.h"
15#include "vulkan_depth24.h" 17#include "vulkan_depth24.h"
16#include "vulkan_scratch.h" 18#include "vulkan_scratch.h"
17#include "vulkan_replay.h" 19#include "vulkan_replay.h"
@@ -28,6 +30,7 @@
28#include <map> 30#include <map>
29#include <memory> 31#include <memory>
30#include <mutex> 32#include <mutex>
33#include <unordered_set>
31#include <shared_mutex> 34#include <shared_mutex>
32#include <span> 35#include <span>
33#include <thread> 36#include <thread>
@@ -85,7 +88,7 @@ struct VulkanDeviceFunctions {
85 // extension is not enabled, and the query functions are optional (a list that uses queries without them is 88 // extension is not enabled, and the query functions are optional (a list that uses queries without them is
86 // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results. 89 // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results.
87 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { 90 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) {
88#define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); 91#define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(leonida::device_proc_or_khr(get, device, #name));
89 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) 92 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD)
90#undef LEONIDA_VK_LOAD 93#undef LEONIDA_VK_LOAD
91 return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer && 94 return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer &&
@@ -745,6 +748,14 @@ public:
745 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va, from); 748 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va, from);
746 } 749 }
747 }; 750 };
751 replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes) {
752 const std::uint64_t base = replay_.heap_base;
753 xdxr_->record_copy(cb, base + src, base + dst, bytes);
754 };
755 replay_.xdxr_postbuild = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dest) {
756 const std::uint64_t base = replay_.heap_base;
757 xdxr_->record_postbuild(cb, base + src, base + dest);
758 };
748 } else { 759 } else {
749 xdxr_.reset(); 760 xdxr_.reset();
750 } 761 }
@@ -818,6 +829,15 @@ public:
818 } 829 }
819 } 830 }
820 831
832 // An image that got a storage (unordered-access) view. A shader can write it before any command names it, so the
833 // layer would not know it was used and the first barrier or copy that does would zero-fill it (vulkan_replay.h,
834 // initialize). The next submission therefore moves each such image to GENERAL, the layout its descriptor names,
835 // zero-filled, ahead of its lists. An image already tracked beyond UNDEFINED is left as it is.
836 void note_storage_image(ResourceId id) {
837 std::lock_guard lock(storage_mutex_);
838 storage_pending_.insert(id);
839 }
840
821 void remove_resource(ResourceId id) { 841 void remove_resource(ResourceId id) {
822 VkImage img = VK_NULL_HANDLE; 842 VkImage img = VK_NULL_HANDLE;
823 { 843 {
@@ -1125,6 +1145,14 @@ private:
1125 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; 1145 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_;
1126 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 1146 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
1127 LayoutTracker tracker_; 1147 LayoutTracker tracker_;
1148 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only
1149 std::unordered_set<ResourceId> storage_pending_;
1150 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter
1151 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before
1152 // the zero-fill that put it in GENERAL has run.
1153 VkSemaphore storage_init_semaphore_ = VK_NULL_HANDLE;
1154 std::uint64_t storage_init_value_ = 0;
1155 std::uint64_t storage_init_version_ = 0;
1128 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; 1156 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_;
1129 std::function<bool(VkCommandBuffer)> bindless_prepare_; 1157 std::function<bool(VkCommandBuffer)> bindless_prepare_;
1130 std::unique_ptr<Depth24Converter> depth24_; 1158 std::unique_ptr<Depth24Converter> depth24_;
@@ -1451,6 +1479,33 @@ public:
1451 return ReplayResult::unsupported; 1479 return ReplayResult::unsupported;
1452 } 1480 }
1453 auto next_tracker = context_.tracker_; 1481 auto next_tracker = context_.tracker_;
1482 // Images with a UAV view are moved to GENERAL (zeroed) by the first submission that can use a shader on them:
1483 // not a copy queue (its family may not clear images), and one whose lists dispatch or draw.
1484 bool runs_shaders = false;
1485 for (const CommandList* list : lists)
1486 for (const auto& command : list->commands())
1487 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) ||
1488 std::holds_alternative<ExecuteIndirect>(command);
1489 std::vector<ResourceId> storage_images;
1490 {
1491 std::lock_guard lock(context_.storage_mutex_);
1492 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ &&
1493 context_.storage_init_version_ != storage_init_seen_) {
1494 waits_.push_back(context_.storage_init_semaphore_);
1495 wait_values_.push_back(context_.storage_init_value_);
1496 storage_init_seen_ = context_.storage_init_version_;
1497 }
1498 if (kind_ != QueueKind::copy && runs_shaders)
1499 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end());
1500 if (!storage_images.empty()) context_.storage_pending_.clear();
1501 }
1502 for (const ResourceId id : storage_images) {
1503 const ResourceBinding* image = context_.resolve(id);
1504 if (!image || !image->image) continue;
1505 const VkImageSubresourceRange range{image->aspects, 0, image->mip_levels, 0, image->array_layers};
1506 auto pieces = detail::current(&next_tracker, id, range, VK_IMAGE_LAYOUT_UNDEFINED);
1507 detail::initialize(replay, command_buffer, *image, id, pieces, VK_IMAGE_LAYOUT_GENERAL, &next_tracker);
1508 }
1454 bool wrote_readback = false; 1509 bool wrote_readback = false;
1455 VkPipelineStageFlags2 readback_stages = 0; 1510 VkPipelineStageFlags2 readback_stages = 0;
1456 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists 1511 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists
@@ -1493,6 +1548,17 @@ public:
1493 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 1548 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
1494 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held 1549 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held
1495 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1550 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1551 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission
1552 std::lock_guard lock(context_.storage_mutex_);
1553 context_.storage_pending_.insert(storage_images.begin(), storage_images.end());
1554 }
1555 if (submitted && !storage_images.empty()) {
1556 std::lock_guard lock(context_.storage_mutex_);
1557 context_.storage_init_semaphore_ = progress_;
1558 context_.storage_init_value_ = submitted_;
1559 ++context_.storage_init_version_;
1560 storage_init_seen_ = context_.storage_init_version_;
1561 }
1496 if (submitted) { 1562 if (submitted) {
1497 context_.tracker_ = std::move(next_tracker); 1563 context_.tracker_ = std::move(next_tracker);
1498 slot->predicate = std::move(batch); 1564 slot->predicate = std::move(batch);
@@ -1644,6 +1710,7 @@ private:
1644 std::uint64_t submitted_ = 0; 1710 std::uint64_t submitted_ = 0;
1645 std::vector<CommandSlot> buffers_; 1711 std::vector<CommandSlot> buffers_;
1646 std::unique_ptr<OcclusionResolve> occlusion_resolve_; 1712 std::unique_ptr<OcclusionResolve> occlusion_resolve_;
1713 std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for
1647 std::vector<VkSemaphore> waits_; 1714 std::vector<VkSemaphore> waits_;
1648 std::vector<std::uint64_t> wait_values_; 1715 std::vector<std::uint64_t> wait_values_;
1649 std::uint32_t open_labels_ = 0; // queue labels begun and not ended 1716 std::uint32_t open_labels_ = 0; // queue labels begun and not ended