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:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 17:22:43 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 17:22:43 +0100
commit3d1dbf1cef8b9240cce69b2bcdba0c09ea0f1d5e (patch)
tree5d84c2e541aa31bcf23d9921dde7e9cb92b2ac59 /runtime/src/vulkan_backend.h
parent2560485f76c45779ec93a2a6dc001c93211f7b3f (diff)
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
Merge main into test/coverage-gaps (#1299)
Conflicts: runtime/test/shaders/check.py, scripts/run_gpu_tests.py AI-assisted.
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h78
1 files changed, 74 insertions, 4 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 73232109e..8683ae2de 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"
@@ -27,6 +29,7 @@
27#include <map> 29#include <map>
28#include <memory> 30#include <memory>
29#include <mutex> 31#include <mutex>
32#include <unordered_set>
30#include <shared_mutex> 33#include <shared_mutex>
31#include <span> 34#include <span>
32#include <thread> 35#include <thread>
@@ -84,7 +87,7 @@ struct VulkanDeviceFunctions {
84 // extension is not enabled, and the query functions are optional (a list that uses queries without them is 87 // extension is not enabled, and the query functions are optional (a list that uses queries without them is
85 // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results. 88 // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results.
86 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { 89 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) {
87#define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); 90#define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(leonida::device_proc_or_khr(get, device, #name));
88 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) 91 LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD)
89#undef LEONIDA_VK_LOAD 92#undef LEONIDA_VK_LOAD
90 return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer && 93 return vkCreateCommandPool && vkDestroyCommandPool && vkAllocateCommandBuffers && vkResetCommandBuffer &&
@@ -732,15 +735,26 @@ public:
732 const std::uint64_t base = replay_.heap_base; 735 const std::uint64_t base = replay_.heap_base;
733 XdxrLayout layout; 736 XdxrLayout layout;
734 layout.compute(xdxr_elements(b), b.is_tlas); 737 layout.compute(xdxr_elements(b), b.is_tlas);
738 // PERFORM_UPDATE refits the source (the destination itself when no source is given, as D3D12
739 // allows for an in-place update); a full build otherwise.
740 const std::uint64_t from = b.update ? base + (b.src_va ? b.src_va : b.dst_va) : 0;
735 if (b.is_tlas) { 741 if (b.is_tlas) {
736 xdxr_->record_tlas(cb, layout, b.instances_va ? base + b.instances_va : 0, base + b.dst_va, 742 xdxr_->record_tlas(cb, layout, b.instances_va ? base + b.instances_va : 0, base + b.dst_va,
737 base + b.scratch_va, base); 743 base + b.scratch_va, base, from);
738 } else { 744 } else {
739 std::vector<XdxrTriangles> geometries; 745 std::vector<XdxrTriangles> geometries;
740 xdxr_inputs(b, &geometries, base); 746 xdxr_inputs(b, &geometries, base);
741 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va); 747 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va, from);
742 } 748 }
743 }; 749 };
750 replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes) {
751 const std::uint64_t base = replay_.heap_base;
752 xdxr_->record_copy(cb, base + src, base + dst, bytes);
753 };
754 replay_.xdxr_postbuild = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dest) {
755 const std::uint64_t base = replay_.heap_base;
756 xdxr_->record_postbuild(cb, base + src, base + dest);
757 };
744 } else { 758 } else {
745 xdxr_.reset(); 759 xdxr_.reset();
746 } 760 }
@@ -814,6 +828,15 @@ public:
814 } 828 }
815 } 829 }
816 830
831 // An image that got a storage (unordered-access) view. A shader can write it before any command names it, so the
832 // layer would not know it was used and the first barrier or copy that does would zero-fill it (vulkan_replay.h,
833 // initialize). The next submission therefore moves each such image to GENERAL, the layout its descriptor names,
834 // zero-filled, ahead of its lists. An image already tracked beyond UNDEFINED is left as it is.
835 void note_storage_image(ResourceId id) {
836 std::lock_guard lock(storage_mutex_);
837 storage_pending_.insert(id);
838 }
839
817 void remove_resource(ResourceId id) { 840 void remove_resource(ResourceId id) {
818 VkImage img = VK_NULL_HANDLE; 841 VkImage img = VK_NULL_HANDLE;
819 { 842 {
@@ -1119,6 +1142,14 @@ private:
1119 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; 1142 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_;
1120 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 1143 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
1121 LayoutTracker tracker_; 1144 LayoutTracker tracker_;
1145 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only
1146 std::unordered_set<ResourceId> storage_pending_;
1147 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter
1148 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before
1149 // the zero-fill that put it in GENERAL has run.
1150 VkSemaphore storage_init_semaphore_ = VK_NULL_HANDLE;
1151 std::uint64_t storage_init_value_ = 0;
1152 std::uint64_t storage_init_version_ = 0;
1122 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; 1153 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_;
1123 std::function<bool(VkCommandBuffer)> bindless_prepare_; 1154 std::function<bool(VkCommandBuffer)> bindless_prepare_;
1124 std::unique_ptr<Depth24Converter> depth24_; 1155 std::unique_ptr<Depth24Converter> depth24_;
@@ -1434,6 +1465,33 @@ public:
1434 return ReplayResult::unsupported; 1465 return ReplayResult::unsupported;
1435 } 1466 }
1436 auto next_tracker = context_.tracker_; 1467 auto next_tracker = context_.tracker_;
1468 // Images with a UAV view are moved to GENERAL (zeroed) by the first submission that can use a shader on them:
1469 // not a copy queue (its family may not clear images), and one whose lists dispatch or draw.
1470 bool runs_shaders = false;
1471 for (const CommandList* list : lists)
1472 for (const auto& command : list->commands())
1473 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) ||
1474 std::holds_alternative<ExecuteIndirect>(command);
1475 std::vector<ResourceId> storage_images;
1476 {
1477 std::lock_guard lock(context_.storage_mutex_);
1478 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ &&
1479 context_.storage_init_version_ != storage_init_seen_) {
1480 waits_.push_back(context_.storage_init_semaphore_);
1481 wait_values_.push_back(context_.storage_init_value_);
1482 storage_init_seen_ = context_.storage_init_version_;
1483 }
1484 if (kind_ != QueueKind::copy && runs_shaders)
1485 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end());
1486 if (!storage_images.empty()) context_.storage_pending_.clear();
1487 }
1488 for (const ResourceId id : storage_images) {
1489 const ResourceBinding* image = context_.resolve(id);
1490 if (!image || !image->image) continue;
1491 const VkImageSubresourceRange range{image->aspects, 0, image->mip_levels, 0, image->array_layers};
1492 auto pieces = detail::current(&next_tracker, id, range, VK_IMAGE_LAYOUT_UNDEFINED);
1493 detail::initialize(replay, command_buffer, *image, id, pieces, VK_IMAGE_LAYOUT_GENERAL, &next_tracker);
1494 }
1437 bool wrote_readback = false; 1495 bool wrote_readback = false;
1438 VkPipelineStageFlags2 readback_stages = 0; 1496 VkPipelineStageFlags2 readback_stages = 0;
1439 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists 1497 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists
@@ -1476,6 +1534,17 @@ public:
1476 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 1534 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
1477 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held 1535 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held
1478 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1536 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1537 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission
1538 std::lock_guard lock(context_.storage_mutex_);
1539 context_.storage_pending_.insert(storage_images.begin(), storage_images.end());
1540 }
1541 if (submitted && !storage_images.empty()) {
1542 std::lock_guard lock(context_.storage_mutex_);
1543 context_.storage_init_semaphore_ = progress_;
1544 context_.storage_init_value_ = submitted_;
1545 ++context_.storage_init_version_;
1546 storage_init_seen_ = context_.storage_init_version_;
1547 }
1479 if (submitted) { 1548 if (submitted) {
1480 context_.tracker_ = std::move(next_tracker); 1549 context_.tracker_ = std::move(next_tracker);
1481 slot->predicate = std::move(batch); 1550 slot->predicate = std::move(batch);
@@ -1627,6 +1696,7 @@ private:
1627 std::uint64_t submitted_ = 0; 1696 std::uint64_t submitted_ = 0;
1628 std::vector<CommandSlot> buffers_; 1697 std::vector<CommandSlot> buffers_;
1629 std::unique_ptr<OcclusionResolve> occlusion_resolve_; 1698 std::unique_ptr<OcclusionResolve> occlusion_resolve_;
1699 std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for
1630 std::vector<VkSemaphore> waits_; 1700 std::vector<VkSemaphore> waits_;
1631 std::vector<std::uint64_t> wait_values_; 1701 std::vector<std::uint64_t> wait_values_;
1632 std::uint32_t open_labels_ = 0; // queue labels begun and not ended 1702 std::uint32_t open_labels_ = 0; // queue labels begun and not ended