Blender V5.3
vk_pipeline_pool.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <cstring>
12
13#include "xxhash.h"
14
15#include "BLI_map.hh"
16#include "BLI_mutex.hh"
17#include "BLI_utility_mixins.hh"
18
19#include "gpu_state_private.hh"
20
21#include "vk_resource_pool.hh"
23
24namespace blender::gpu {
25class VKDevice;
26
55
64 struct VertexIn {
65 VkPrimitiveTopology vk_topology;
67
68 bool operator==(const VertexIn &other) const
69 {
70 return vk_topology == other.vk_topology && vertex_input_key == other.vertex_input_key;
71 }
72
73 uint64_t hash() const
74 {
76 hash = hash * 33 ^ vertex_input_key;
77 return hash;
78 }
79 };
80 struct Shaders {
81 VkPipelineLayout vk_pipeline_layout;
82 VkShaderModule vk_vertex_module;
83 VkShaderModule vk_geometry_module;
84 VkShaderModule vk_fragment_module;
85 VkPrimitiveTopology vk_topology;
98 /* Color attachment count of the render pass this shaders library is linked with.
99 *
100 * When VK_KHR_dynamic_rendering_local_read is used and the shader declares input attachments,
101 * VkPipelineRenderingCreateInfo::colorAttachmentCount and
102 * VkRenderingInputAttachmentIndexInfo::colorAttachmentCount must match the count declared
103 * by the fragment output library (VUID-VkGraphicsPipelineCreateInfo-renderPass-09531). */
105
106 bool operator==(const Shaders &other) const
107 {
108 return vk_vertex_module == other.vk_vertex_module &&
112 viewport_count == other.viewport_count && state == other.state &&
114 has_depth == other.has_depth && has_stencil == other.has_stencil &&
117 viewports.size() == other.viewports.size() &&
118 scissors.size() == other.scissors.size() &&
119 memcmp(viewports.data(),
120 other.viewports.data(),
121 viewports.size() * sizeof(VkViewport)) == 0 &&
122 memcmp(scissors.data(), other.scissors.data(), scissors.size() * sizeof(VkRect2D)) ==
123 0;
124 }
125
127 {
129 hash = hash * 33 ^ XXH3_64bits(viewports.data(), viewports.size() * sizeof(VkViewport));
130 hash = hash * 33 ^ XXH3_64bits(scissors.data(), scissors.size() * sizeof(VkRect2D));
135 hash = hash * 33 ^ uint64_t(vk_topology);
136 hash = hash * 33 ^ state.data;
137 hash = hash * 33 ^ specialization_constants.hash();
138 hash = hash * 33 ^ (uint64_t(has_depth) << 1 | uint64_t(has_stencil));
141 return hash;
142 }
143
144 VkPipelineLayout vk_pipeline_layout_get() const
145 {
146 return vk_pipeline_layout;
147 }
148 };
149 struct FragmentOut {
151
152 /* Dynamic rendering */
156
157 bool operator==(const FragmentOut &other) const
158 {
159#if 0
160 return hash() == other.hash();
161#else
165 {
166 return false;
167 }
168
169 return true;
170#endif
171 }
172
174 {
177 hash = hash * 33 ^ XXH3_64bits(color_attachment_formats.data(),
178 color_attachment_formats.size() * sizeof(VkFormat));
179 hash = hash * 33 ^ state.data;
180 return hash;
181 }
182 };
183
187
188 bool operator==(const VKGraphicsInfo &other) const
189 {
190 return vertex_in == other.vertex_in && shaders == other.shaders &&
191 fragment_out == other.fragment_out;
192 };
194 {
195 uint64_t hash = 0;
196 hash = hash * 33 ^ vertex_in.hash();
197 hash = hash * 33 ^ shaders.hash();
198 hash = hash * 33 ^ fragment_out.hash();
199 return hash;
200 }
201
202 VkPipelineLayout vk_pipeline_layout_get() const
203 {
205 }
206
208 std::string pipeline_info_source() const;
209};
210
214template<typename PipelineInfo> class VKPipelineMap {
216 Mutex mutex_;
217 std::condition_variable_any new_pipeline_added_;
218
219 public:
233 VkPipeline get_or_create(const PipelineInfo &pipeline_info,
234 VkPipelineCache vk_pipeline_cache,
235 VkPipeline vk_pipeline_base,
236 StringRefNull name,
237 bool &r_created)
238 {
239 bool do_wait_for_pipeline = false;
240 bool do_compile_pipeline = false;
241 r_created = false;
242 {
243 std::scoped_lock lock(mutex_);
244 const VkPipeline *found_pipeline = pipelines_.lookup_ptr(pipeline_info);
245 if (found_pipeline) {
246 if (*found_pipeline == VK_NULL_HANDLE) {
247 do_wait_for_pipeline = true;
248 }
249 else {
250 /* Early exit: pipeline_info found and has a valid pipeline. */
251 return *found_pipeline;
252 }
253 }
254 else {
255 pipelines_.add_new(pipeline_info, VK_NULL_HANDLE);
256 do_compile_pipeline = true;
257 }
258 }
259 if (do_wait_for_pipeline) {
260 return wait_for_completion(pipeline_info);
261 }
262 if (do_compile_pipeline) {
263 VkPipeline pipeline = create(pipeline_info, vk_pipeline_cache, vk_pipeline_base, name);
264 r_created = true;
265 /* Store result in the compute pipelines map. */
266 {
267 std::scoped_lock lock(mutex_);
268 VkPipeline &pipeline_item = pipelines_.lookup(pipeline_info);
269 pipeline_item = pipeline;
270 }
271 /* Notify other threads that a new pipeline is available. */
272 {
273 new_pipeline_added_.notify_all();
274 }
275 return pipeline;
276 }
278 return VK_NULL_HANDLE;
279 }
280
284 void discard(VKDiscardPool &discard_pool, VkPipelineLayout vk_pipeline_layout)
285 {
286 std::scoped_lock lock(mutex_);
287 pipelines_.remove_if([&](auto item) {
288 if (item.key.vk_pipeline_layout_get() == vk_pipeline_layout) {
289 discard_pool.discard_pipeline(item.value);
290 return true;
291 }
292 return false;
293 });
294 }
295
300 void free_data(VkDevice vk_device, const volk::VolkDeviceTable &functions)
301 {
302 std::scoped_lock lock(mutex_);
303 for (VkPipeline &vk_pipeline : pipelines_.values()) {
304 functions.vkDestroyPipeline(vk_device, vk_pipeline, nullptr);
305 }
306 pipelines_.clear();
307 }
308
312 {
313 return pipelines_.size();
314 }
315
316 private:
330 VkPipeline create(const PipelineInfo &pipeline_info,
331 VkPipelineCache vk_pipeline_cache,
332 VkPipeline vk_pipeline_base,
333 StringRefNull name);
339 VkPipeline wait_for_completion(const PipelineInfo &pipeline_info)
340 {
341 std::unique_lock<Mutex> lock(mutex_);
342 const VkPipeline *pipeline = nullptr;
343 new_pipeline_added_.wait(lock, [&]() {
344 pipeline = pipelines_.lookup_ptr(pipeline_info);
345 BLI_assert(pipeline != nullptr);
346 return *pipeline != VK_NULL_HANDLE;
347 });
348 return *pipeline;
349 }
350};
351
381 friend class VKDevice;
382
383 private:
384 VkPipelineCache vk_pipeline_cache_static_;
385 VkPipelineCache vk_pipeline_cache_non_static_;
386
389
390 /* Cached graphics libraries (VK_EXT_graphics_pipeline_library)
391 * When using graphics pipeline libraries the shaders is compiled in 3 chunks, vertex input,
392 * shaders and fragment output. */
395 VKPipelineMap<VKGraphicsInfo::FragmentOut> fragment_output_libs_;
396
397 public:
398 void init();
399
415 VkPipeline get_or_create_compute_pipeline(const VKComputeInfo &compute_info,
416 bool is_static_shader,
417 VkPipeline vk_pipeline_base,
418 StringRefNull name);
419
437 VkPipeline get_or_create_graphics_pipeline(const VKGraphicsInfo &graphics_info,
438 bool is_static_shader,
439 VkPipeline vk_pipeline_base,
440 StringRefNull name,
441 bool &r_created);
442
449 VkPipeline get_or_create_vertex_input_lib(const VKGraphicsInfo::VertexIn &vertex_input_info);
450
457 VkPipeline get_or_create_shaders_lib(const VKGraphicsInfo::Shaders &shaders_info);
458
466 const VKGraphicsInfo::FragmentOut &fragment_output_info);
467
471 void discard(VKDiscardPool &discard_pool, VkPipelineLayout vk_pipeline_layout);
472
479 void free_data(const VKDevice &device);
480
495 void read_from_disk();
496
511 void write_to_disk();
512};
513
514} // namespace blender::gpu
volatile int lock
#define BLI_assert_unreachable()
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
NonCopyable(const NonCopyable &other)=delete
void discard_pipeline(VkPipeline vk_pipeline)
void free_data(VkDevice vk_device, const volk::VolkDeviceTable &functions)
void discard(VKDiscardPool &discard_pool, VkPipelineLayout vk_pipeline_layout)
VkPipeline get_or_create(const PipelineInfo &pipeline_info, VkPipelineCache vk_pipeline_cache, VkPipeline vk_pipeline_base, StringRefNull name, bool &r_created)
VkPipeline get_or_create_vertex_input_lib(const VKGraphicsInfo::VertexIn &vertex_input_info)
VkPipeline get_or_create_compute_pipeline(const VKComputeInfo &compute_info, bool is_static_shader, VkPipeline vk_pipeline_base, StringRefNull name)
void free_data(const VKDevice &device)
VkPipeline get_or_create_fragment_output_lib(const VKGraphicsInfo::FragmentOut &fragment_output_info)
void discard(VKDiscardPool &discard_pool, VkPipelineLayout vk_pipeline_layout)
VkPipeline get_or_create_graphics_pipeline(const VKGraphicsInfo &graphics_info, bool is_static_shader, VkPipeline vk_pipeline_base, StringRefNull name, bool &r_created)
VkPipeline get_or_create_shaders_lib(const VKGraphicsInfo::Shaders &shaders_info)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint32_t
Definition BKE_text.h:81
std::mutex Mutex
Definition BLI_mutex.hh:51
Vector< shader::SpecializationConstant::Value > specialization_constants
VkPipelineLayout vk_pipeline_layout_get() const
VkPipelineLayout vk_pipeline_layout
bool operator==(const VKComputeInfo &other) const
Vector< VkFormat, GPU_FB_MAX_COLOR_ATTACHMENT > color_attachment_formats
bool operator==(const FragmentOut &other) const
Vector< VkRect2D, GPU_MAX_VIEWPORTS > scissors
Vector< VkViewport, GPU_MAX_VIEWPORTS > viewports
bool operator==(const Shaders &other) const
VkPipelineLayout vk_pipeline_layout_get() const
Vector< shader::SpecializationConstant::Value > specialization_constants
VKVertexInputDescriptionPool::Key vertex_input_key
bool operator==(const VertexIn &other) const
bool operator==(const VKGraphicsInfo &other) const
VkPipelineLayout vk_pipeline_layout_get() const
std::string pipeline_info_source() const
int size() const
Definition vector.hh:130