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:
authorTheGeogeo <57634077+TheGeogeo@users.noreply.github.com>2026-11-25 02:56:13 +0100
committerTheGeogeo <57634077+TheGeogeo@users.noreply.github.com>2026-11-25 04:46:33 +0100
commit3e1c49e1bd0ab725c00400a52326fdd80add4840 (patch)
tree6c939858a57457492a5b79a5613bf2f5c382a1e8 /runtime/src/vulkan_backend.h
parenta3980cd437b28a047f8e0da3a698df32a34ecbc0 (diff)
d3d12: compile a submission's pipelines on worker threads (RT-PIPELINE-COMPILE-WORKERS)
prepare() leaves the pipelines that are not built yet to PipelineCompiles, once per pipeline and variant. VulkanQueue::execute_now compiles them together on the device's workers with the resource lock held, places them in the prepared commands and then writes the command buffer, so every draw and dispatch has its own pipeline. A refused compile gives the result prepare() alone would have given. LEONIDA_PIPELINE_THREADS=0 keeps the path of before. Graphics pipelines of 1 MiB of SPIR-V or more and compute pipelines over 128 KiB compile one after another, and LEONIDA_ASYNC_PIPELINES keeps its own worker.
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h30
1 files changed, 29 insertions, 1 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 5c3e884ba..9946331f9 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -14,6 +14,7 @@
14#include "buffer_va_index.h" 14#include "buffer_va_index.h"
15#include "image_memory.h" 15#include "image_memory.h"
16#include "occlusion_sum_spirv.h" 16#include "occlusion_sum_spirv.h"
17#include "pipeline_workers.h"
17#include "queue_holds.h" 18#include "queue_holds.h"
18#include "retire_tickets.h" 19#include "retire_tickets.h"
19#include "statistics_sum_spirv.h" 20#include "statistics_sum_spirv.h"
@@ -1428,6 +1429,17 @@ public:
1428 graphics_pipelines_[id] = pipeline; 1429 graphics_pipelines_[id] = pipeline;
1429 if (shaders && device_fault_) device_fault_->pipeline_names[id] = *shaders; 1430 if (shaders && device_fault_) device_fault_->pipeline_names[id] = *shaders;
1430 } 1431 }
1432 // Workers for the pipelines a submission needs and that are not built yet. Null (the default): each is built where
1433 // prepare() meets it, on the submitting thread. Replacing it waits for submissions that are compiling, and ends the
1434 // workers it had.
1435 void set_pipeline_workers(std::unique_ptr<PipelineWorkers> workers) {
1436 std::unique_lock lock(resource_mutex_);
1437 pipeline_workers_.swap(workers);
1438 }
1439 PipelineWorkers::Stats pipeline_worker_stats() {
1440 std::shared_lock lock(resource_mutex_);
1441 return pipeline_workers_ ? pipeline_workers_->stats() : PipelineWorkers::Stats{};
1442 }
1431 // Configure before queue creation, when the selected queue family supports compute. 1443 // Configure before queue creation, when the selected queue family supports compute.
1432 void set_occlusion_resolve_memory(const VkPhysicalDeviceMemoryProperties& memory) { 1444 void set_occlusion_resolve_memory(const VkPhysicalDeviceMemoryProperties& memory) {
1433 occlusion_resolve_memory_ = memory; 1445 occlusion_resolve_memory_ = memory;
@@ -2003,6 +2015,7 @@ private:
2003 std::unordered_map<ResourceId, std::shared_ptr<SparseResource>> sparse_resources_; 2015 std::unordered_map<ResourceId, std::shared_ptr<SparseResource>> sparse_resources_;
2004 std::unordered_map<std::uintptr_t, ComputePipeline> pipelines_; 2016 std::unordered_map<std::uintptr_t, ComputePipeline> pipelines_;
2005 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; 2017 std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_;
2018 std::unique_ptr<PipelineWorkers> pipeline_workers_;
2006 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 2019 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
2007 LayoutTracker tracker_; 2020 LayoutTracker tracker_;
2008 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ and zero_pending_ only 2021 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ and zero_pending_ only
@@ -2803,9 +2816,18 @@ private:
2803 const auto resolve = [this](ResourceId id) { return context_.resolve(id); }; 2816 const auto resolve = [this](ResourceId id) { return context_.resolve(id); };
2804 std::uint32_t segments = 0, statistics_segments = 0; 2817 std::uint32_t segments = 0, statistics_segments = 0;
2805 bool has_queries = false; 2818 bool has_queries = false;
2819 // With workers, the pipelines the lists need and that are not built yet are compiled together below instead of
2820 // one after another in prepare(). resource_lock is held until they are all built, which keeps the pipeline
2821 // states that own them alive (remove_pipeline takes it uniquely).
2822 PipelineCompiles compiles;
2823 if (context_.pipeline_workers_) replay.compiles = &compiles;
2824 ReplayResult prepare_result = ReplayResult::success;
2806 for (std::size_t i = 0; i < lists.size(); ++i) { 2825 for (std::size_t i = 0; i < lists.size(); ++i) {
2807 const auto result = prepare(*lists[i], resolve, replay, prepared[i], can_resolve_occlusion); 2826 const auto result = prepare(*lists[i], resolve, replay, prepared[i], can_resolve_occlusion);
2808 if (result != ReplayResult::success) return result; 2827 if (result != ReplayResult::success) {
2828 prepare_result = result;
2829 break;
2830 }
2809 if (prepared[i].segment_count > UINT32_MAX - segments || 2831 if (prepared[i].segment_count > UINT32_MAX - segments ||
2810 prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments) 2832 prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments)
2811 return queue_unsupported(__LINE__); 2833 return queue_unsupported(__LINE__);
@@ -2813,6 +2835,12 @@ private:
2813 statistics_segments += prepared[i].statistics_segment_count; 2835 statistics_segments += prepared[i].statistics_segment_count;
2814 has_queries = has_queries || !prepared[i].queries.empty(); 2836 has_queries = has_queries || !prepared[i].queries.empty();
2815 } 2837 }
2838 prepare_result = build_pipelines(
2839 compiles, std::span(prepared).first(lists.size()), prepare_result,
2840 [this](std::size_t count, const std::function<void(std::size_t)>& job) {
2841 context_.pipeline_workers_->run(count, job);
2842 });
2843 if (prepare_result != ReplayResult::success) return prepare_result;
2816 const auto segment_pool = [this](VkQueryType type, std::uint32_t count, 2844 const auto segment_pool = [this](VkQueryType type, std::uint32_t count,
2817 std::shared_ptr<OcclusionSegmentPool>& out) { 2845 std::shared_ptr<OcclusionSegmentPool>& out) {
2818 const auto& vk = context_.vk; 2846 const auto& vk = context_.vk;