Blender V5.3
vk_pipeline_pool.cc
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#include <sstream>
10
11#include "BKE_appdir.hh"
12#include "BKE_blender_version.h"
13
14#include "BLI_fileops.hh"
15#include "BLI_path_utils.hh"
16#include "BLI_time.hh"
17
18#include "CLG_log.h"
19
20#include "vk_backend.hh"
22#include "vk_pipeline_pool.hh"
23
24namespace blender {
25
26#ifdef WITH_BUILDINFO
27extern "C" char build_hash[];
28#endif
29static CLG_LogRef LOG = {"gpu.vulkan"};
30
31namespace gpu {
32
34{
35 VKDevice &device = VKBackend::get().device;
36 VkPipelineCacheCreateInfo create_info = {};
37 create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
38 device.functions.vkCreatePipelineCache(
39 device.vk_handle(), &create_info, nullptr, &vk_pipeline_cache_static_);
40 debug::object_label(vk_pipeline_cache_static_, "VkPipelineCache.Static");
41 device.functions.vkCreatePipelineCache(
42 device.vk_handle(), &create_info, nullptr, &vk_pipeline_cache_non_static_);
43 debug::object_label(vk_pipeline_cache_non_static_, "VkPipelineCache.Dynamic");
44}
45
46/* -------------------------------------------------------------------- */
49
51 const bool is_static_shader,
52 VkPipeline vk_pipeline_base,
53 StringRefNull name)
54{
55 bool created = false;
56 VkPipelineCache vk_pipeline_cache = is_static_shader ? vk_pipeline_cache_static_ :
57 vk_pipeline_cache_non_static_;
58 return compute_.get_or_create(compute_info, vk_pipeline_cache, vk_pipeline_base, name, created);
59}
60
61template<>
62VkPipeline VKPipelineMap<VKComputeInfo>::create(const VKComputeInfo &compute_info,
63 VkPipelineCache vk_pipeline_cache,
64 VkPipeline vk_pipeline_base,
65 StringRefNull name)
66{
67 /* Building compute pipeline create info */
68 const bool do_specialization_constants = !compute_info.specialization_constants.is_empty();
69 VkComputePipelineCreateInfo vk_compute_pipeline_create_info = {
70 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
71 nullptr,
72 0,
73 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
74 nullptr,
75 0,
76 VK_SHADER_STAGE_COMPUTE_BIT,
77 compute_info.vk_shader_module,
78 "main",
79 nullptr},
80 compute_info.vk_pipeline_layout,
81 vk_pipeline_base,
82 0};
83
84 /* Specialization constants */
85 VkSpecializationInfo vk_specialization_info;
86 Array<VkSpecializationMapEntry> vk_specialization_map_entries(
87 compute_info.specialization_constants.size());
88 if (do_specialization_constants) {
89 vk_compute_pipeline_create_info.stage.pSpecializationInfo = &vk_specialization_info;
90 for (uint32_t index : IndexRange(compute_info.specialization_constants.size())) {
91 vk_specialization_map_entries[index] = {
92 index, uint32_t(index * sizeof(uint32_t)), sizeof(uint32_t)};
93 }
94 vk_specialization_info = {uint32_t(vk_specialization_map_entries.size()),
95 vk_specialization_map_entries.data(),
96 compute_info.specialization_constants.size() * sizeof(uint32_t),
97 compute_info.specialization_constants.data()};
98 }
99
100 /* Create pipeline. */
101 VKBackend &backend = VKBackend::get();
102 VKDevice &device = backend.device;
103
104 double start_time = BLI_time_now_seconds();
105 VkPipeline pipeline = VK_NULL_HANDLE;
106 device.functions.vkCreateComputePipelines(device.vk_handle(),
107 vk_pipeline_cache,
108 1,
109 &vk_compute_pipeline_create_info,
110 nullptr,
111 &pipeline);
112 double end_time = BLI_time_now_seconds();
113 debug::object_label(pipeline, name);
115 "Compiled compute pipeline %s in %fms ",
116 name.c_str(),
117 (end_time - start_time) * 1000.0);
118
119 return pipeline;
120}
121
123
124/* -------------------------------------------------------------------- */
127
129 const bool is_static_shader,
130 VkPipeline vk_pipeline_base,
131 StringRefNull name,
132 bool &r_created)
133{
135 graphics_info.shaders.state == graphics_info.fragment_out.state,
136 "VKGraphicsInfo.shader.state and VKGraphicsInfo.fragment_out.state should be identical, "
137 "otherwise an incorrect fragment output library will be linked.");
138 VkPipelineCache vk_pipeline_cache = is_static_shader ? vk_pipeline_cache_static_ :
139 vk_pipeline_cache_non_static_;
140 return graphics_.get_or_create(
141 graphics_info, vk_pipeline_cache, vk_pipeline_base, name, r_created);
142}
143
144static VkPipeline create_graphics_pipeline_no_libs(const VKGraphicsInfo &graphics_info,
145 VkPipelineCache vk_pipeline_cache,
146 VkPipeline vk_pipeline_base,
147 StringRefNull name)
148{
149 VKDevice &device = VKBackend::get().device;
151 builder.build_full(device, graphics_info, vk_pipeline_base);
152
153 /* Build pipeline. */
154 VkPipeline pipeline = VK_NULL_HANDLE;
155 double start_time = BLI_time_now_seconds();
156 device.functions.vkCreateGraphicsPipelines(device.vk_handle(),
157 vk_pipeline_cache,
158 1,
160 nullptr,
161 &pipeline);
162 double end_time = BLI_time_now_seconds();
163 debug::object_label(pipeline, name);
165 "Compiled graphics pipeline %s in %fms ",
166 name.c_str(),
167 (end_time - start_time) * 1000.0);
168 return pipeline;
169}
170
171static VkPipeline create_graphics_pipeline_libs(const VKGraphicsInfo &graphics_info,
172 VkPipelineCache vk_pipeline_cache,
173 VkPipeline vk_pipeline_base,
174 StringRefNull name)
175{
176 double start_time = BLI_time_now_seconds();
177 VKDevice &device = VKBackend::get().device;
178
179 VkPipeline vertex_input_lib = device.pipelines.get_or_create_vertex_input_lib(
180 graphics_info.vertex_in);
181 VkPipeline shaders_lib = device.pipelines.get_or_create_shaders_lib(graphics_info.shaders);
182 VkPipeline fragment_output_lib = device.pipelines.get_or_create_fragment_output_lib(
183 graphics_info.fragment_out);
184
185 std::array<VkPipeline, 3> pipeline_libraries = {
186 vertex_input_lib, shaders_lib, fragment_output_lib};
187
188 /* Linking */
189 VkPipeline pipeline = VK_NULL_HANDLE;
190 VkPipelineLibraryCreateInfoKHR vk_pipeline_library_create_info = {
191 VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR,
192 nullptr,
193 uint32_t(pipeline_libraries.size()),
194 pipeline_libraries.data()};
195 VkGraphicsPipelineCreateInfo linking_pipeline_create_info = {
196 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
197 &vk_pipeline_library_create_info,
198 VK_PIPELINE_CREATE_LINK_TIME_OPTIMIZATION_BIT_EXT,
199 0,
200 nullptr,
201 nullptr,
202 nullptr,
203 nullptr,
204 nullptr,
205 nullptr,
206 nullptr,
207 nullptr,
208 nullptr,
209 nullptr,
210 graphics_info.shaders.vk_pipeline_layout,
211 VK_NULL_HANDLE,
212 0,
213 vk_pipeline_base,
214 0};
215 double start_link_time = BLI_time_now_seconds();
216 device.functions.vkCreateGraphicsPipelines(
217 device.vk_handle(), vk_pipeline_cache, 1, &linking_pipeline_create_info, nullptr, &pipeline);
218 double end_time = BLI_time_now_seconds();
219 debug::object_label(pipeline, name);
221 "Linking graphics pipeline %s in %fms ",
222 name.c_str(),
223 (end_time - start_link_time) * 1000.0);
225 "Compiling graphics pipeline %s in %fms ",
226 name.c_str(),
227 (end_time - start_time) * 1000.0);
228 return pipeline;
229}
230
231template<>
232VkPipeline VKPipelineMap<VKGraphicsInfo>::create(const VKGraphicsInfo &graphics_info,
233 VkPipelineCache vk_pipeline_cache,
234 VkPipeline vk_pipeline_base,
235 StringRefNull name)
236{
237 VKDevice &device = VKBackend::get().device;
238 const VKExtensions &extensions = device.extensions_get();
239 if (extensions.graphics_pipeline_library) {
240 return create_graphics_pipeline_libs(graphics_info, vk_pipeline_cache, vk_pipeline_base, name);
241 }
243 graphics_info, vk_pipeline_cache, vk_pipeline_base, name);
244}
245
247{
248 std::stringstream result;
249 result << "info.pipeline_state()\n";
250 result << " .primitive(";
251 switch (vertex_in.vk_topology) {
252 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
253 result << "GPU_PRIM_POINTS";
254 break;
255 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
256 result << "GPU_PRIM_LINES";
257 break;
258 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
259 result << "GPU_PRIM_LINE_STRIP";
260 break;
261 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
262 result << "GPU_PRIM_TRIS";
263 break;
264 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
265 result << "GPU_PRIM_TRI_STRIP";
266 break;
267 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
268 result << "GPU_PRIM_TRI_FAN";
269 break;
270 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
271 result << "GPU_PRIM_LINES_ADJ";
272 break;
273 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
274 result << "GPU_PRIM_TRIS_ADJ";
275 break;
276 default:
278 };
279 result << ")\n";
280 result << " .state(";
281 /* Write mask */
282 Vector<std::string> write_masks;
283 if (fragment_out.state.write_mask & GPU_WRITE_COLOR) {
284 if ((fragment_out.state.write_mask & GPU_WRITE_COLOR) == GPU_WRITE_COLOR) {
285 write_masks.append("GPU_WRITE_COLOR");
286 }
287 else {
288 if (fragment_out.state.write_mask & GPU_WRITE_RED) {
289 write_masks.append("GPU_WRITE_RED");
290 }
291 if (fragment_out.state.write_mask & GPU_WRITE_GREEN) {
292 write_masks.append("GPU_WRITE_GREEN");
293 }
294 if (fragment_out.state.write_mask & GPU_WRITE_BLUE) {
295 write_masks.append("GPU_WRITE_BLUE");
296 }
297 if (fragment_out.state.write_mask & GPU_WRITE_ALPHA) {
298 write_masks.append("GPU_WRITE_ALPHA");
299 }
300 }
301 }
302 if (fragment_out.state.write_mask & GPU_WRITE_DEPTH) {
303 write_masks.append("GPU_WRITE_DEPTH");
304 }
305 if (fragment_out.state.write_mask & GPU_WRITE_STENCIL) {
306 write_masks.append("GPU_WRITE_STENCIL");
307 }
308 if (write_masks.is_empty()) {
309 write_masks.append("GPU_WRITE_NONE");
310 }
311 for (const std::string &write_mask : write_masks) {
312 result << write_mask;
313 if (&write_mask != &write_masks.last()) {
314 result << " | ";
315 }
316 }
317 /* Blending mode */
318 result << ",\n ";
319 switch (fragment_out.state.blend) {
320 case GPU_BLEND_NONE:
321 result << "GPU_BLEND_NONE";
322 break;
323 case GPU_BLEND_ALPHA:
324 result << "GPU_BLEND_ALPHA";
325 break;
327 result << "GPU_BLEND_ALPHA_PREMULT";
328 break;
330 result << "GPU_BLEND_ADDITIVE";
331 break;
333 result << "GPU_BLEND_ADDITIVE_PREMULT";
334 break;
336 result << "GPU_BLEND_MULTIPLY";
337 break;
339 result << "GPU_BLEND_SUBTRACT";
340 break;
341 case GPU_BLEND_INVERT:
342 result << "GPU_BLEND_INVERT";
343 break;
344 case GPU_BLEND_MIN:
345 result << "GPU_BLEND_MIN";
346 break;
347 case GPU_BLEND_MAX:
348 result << "GPU_BLEND_MAX";
349 break;
350 case GPU_BLEND_OIT:
351 result << "GPU_BLEND_OIT";
352 break;
354 result << "GPU_BLEND_BACKGROUND";
355 break;
356 case GPU_BLEND_CUSTOM:
357 result << "GPU_BLEND_CUSTOM";
358 break;
360 result << "GPU_BLEND_ALPHA_UNDER_PREMUL";
361 break;
363 result << "GPU_BLEND_OVERLAY_MASK_FROM_ALPHA";
364 break;
366 result << "GPU_BLEND_TRANSPARENCY";
367 break;
368 default:
370 }
371 /* Culling test */
372 result << ",\n ";
373 switch (shaders.state.culling_test) {
374 case GPU_CULL_NONE:
375 result << "GPU_CULL_NONE";
376 break;
377 case GPU_CULL_FRONT:
378 result << "GPU_CULL_FRONT";
379 break;
380 case GPU_CULL_BACK:
381 result << "GPU_CULL_BACK";
382 break;
383 default:
385 }
386 /* Depth test */
387 result << ",\n ";
388 switch (shaders.state.depth_test) {
389 case GPU_DEPTH_NONE:
390 result << "GPU_DEPTH_NONE";
391 break;
392 case GPU_DEPTH_LESS:
393 result << "GPU_DEPTH_LESS";
394 break;
396 result << "GPU_DEPTH_LESS_EQUAL";
397 break;
398 case GPU_DEPTH_EQUAL:
399 result << "GPU_DEPTH_EQUAL";
400 break;
402 result << "GPU_DEPTH_GREATER";
403 break;
405 result << "GPU_DEPTH_GREATER_EQUAL";
406 break;
407 case GPU_DEPTH_ALWAYS:
408 result << "GPU_DEPTH_ALWAYS";
409 break;
410 default:
412 }
413 /* Stencil test */
414 result << ",\n ";
415 switch (shaders.state.stencil_test) {
416 case GPU_STENCIL_NONE:
417 result << "GPU_STENCIL_NONE";
418 break;
420 result << "GPU_STENCIL_ALWAYS";
421 break;
423 result << "GPU_STENCIL_EQUAL";
424 break;
426 result << "GPU_STENCIL_NEQUAL";
427 break;
428 default:
430 }
431 /* Stencil operation */
432 result << ",\n ";
433 switch (shaders.state.stencil_op) {
435 result << "GPU_STENCIL_OP_NONE";
436 break;
438 result << "GPU_STENCIL_OP_REPLACE";
439 break;
441 result << "GPU_STENCIL_OP_COUNT_DEPTH_PASS";
442 break;
444 result << "GPU_STENCIL_OP_COUNT_DEPTH_FAIL";
445 break;
446 default:
448 }
449 /* Provoking vertex */
450 result << ",\n ";
451 switch (shaders.state.provoking_vert) {
452 case GPU_VERTEX_FIRST:
453 result << "GPU_VERTEX_FIRST";
454 break;
455 case GPU_VERTEX_LAST:
456 result << "GPU_VERTEX_LAST";
457 break;
458 default:
460 }
461 result << ")\n";
462 /* viewports */
463 result << " .viewports(" << shaders.viewport_count << ")\n";
464 /* Depth format */
465 if (fragment_out.depth_attachment_format != VK_FORMAT_UNDEFINED) {
466 result << " .depth_format(gpu::TextureTargetFormat::"
468 }
469 /* Stencil format */
470 if (fragment_out.stencil_attachment_format != VK_FORMAT_UNDEFINED) {
471 result << " .stencil_format(gpu::TextureTargetFormat::"
473 }
474 /* Color formats */
475 for (const VkFormat format : fragment_out.color_attachment_formats) {
476 result << " .color_format(gpu::TextureTargetFormat::" << to_gpu_format_string(format)
477 << ")\n";
478 }
479 result << ";";
480 return result.str();
481}
482
484
485/* -------------------------------------------------------------------- */
488
490 const VKGraphicsInfo::VertexIn &vertex_input_info)
491{
492 bool created = false;
493 return vertex_input_libs_.get_or_create(
494 vertex_input_info, vk_pipeline_cache_static_, VK_NULL_HANDLE, "VertexInLib", created);
495}
496
497template<>
498VkPipeline VKPipelineMap<VKGraphicsInfo::VertexIn>::create(
499 const VKGraphicsInfo::VertexIn &vertex_input_info,
500 VkPipelineCache vk_pipeline_cache,
501 VkPipeline vk_pipeline_base,
502 StringRefNull name)
503{
504 VKDevice &device = VKBackend::get().device;
506 builder.build_vertex_input_lib(device, vertex_input_info, vk_pipeline_base);
507
508 /* Build pipeline. */
509 VkPipeline pipeline = VK_NULL_HANDLE;
510 double start_time = BLI_time_now_seconds();
511 device.functions.vkCreateGraphicsPipelines(device.vk_handle(),
512 vk_pipeline_cache,
513 1,
515 nullptr,
516 &pipeline);
517 double end_time = BLI_time_now_seconds();
518 debug::object_label(pipeline, name);
519 CLOG_TRACE(&LOG, "Compiled vertex input library in %fms ", (end_time - start_time) * 1000.0);
520 return pipeline;
521}
522
524
525/* -------------------------------------------------------------------- */
528
530{
531 bool created = false;
532 return shaders_libs_.get_or_create(
533 shaders_info, vk_pipeline_cache_non_static_, VK_NULL_HANDLE, "ShadersLib", created);
534}
535
536template<>
537VkPipeline VKPipelineMap<VKGraphicsInfo::Shaders>::create(
538 const VKGraphicsInfo::Shaders &shaders_info,
539 VkPipelineCache vk_pipeline_cache,
540 VkPipeline vk_pipeline_base,
541 StringRefNull name)
542{
543 VKDevice &device = VKBackend::get().device;
544 const VKExtensions &extensions = device.extensions_get();
545 const VKWorkarounds &workarounds = device.workarounds_get();
547 builder.build_shaders_lib(shaders_info, extensions, workarounds, vk_pipeline_base);
548
549 /* Build pipeline. */
550 VkPipeline pipeline = VK_NULL_HANDLE;
551 double start_time = BLI_time_now_seconds();
552 device.functions.vkCreateGraphicsPipelines(device.vk_handle(),
553 vk_pipeline_cache,
554 1,
556 nullptr,
557 &pipeline);
558 double end_time = BLI_time_now_seconds();
559 debug::object_label(pipeline, name);
560 CLOG_TRACE(&LOG, "Compiled shaders library in %fms ", (end_time - start_time) * 1000.0);
561 return pipeline;
562}
563
565
566/* -------------------------------------------------------------------- */
569
571 const VKGraphicsInfo::FragmentOut &fragment_output_info)
572{
573 bool created = false;
574 return fragment_output_libs_.get_or_create(
575 fragment_output_info, vk_pipeline_cache_static_, VK_NULL_HANDLE, "FragmentOutLib", created);
576}
577
578template<>
579VkPipeline VKPipelineMap<VKGraphicsInfo::FragmentOut>::create(
580 const VKGraphicsInfo::FragmentOut &fragment_output_info,
581 VkPipelineCache vk_pipeline_cache,
582 VkPipeline vk_pipeline_base,
583 StringRefNull name)
584{
585 VKDevice &device = VKBackend::get().device;
587 builder.build_fragment_output_lib(fragment_output_info, vk_pipeline_base);
588
589 /* Build pipeline. */
590 VkPipeline pipeline = VK_NULL_HANDLE;
591 double start_time = BLI_time_now_seconds();
592 device.functions.vkCreateGraphicsPipelines(device.vk_handle(),
593 vk_pipeline_cache,
594 1,
596 nullptr,
597 &pipeline);
598 double end_time = BLI_time_now_seconds();
599 debug::object_label(pipeline, name);
600 CLOG_TRACE(&LOG, "Compiled fragment output library in %fms ", (end_time - start_time) * 1000.0);
601 return pipeline;
602}
603
605
606void VKPipelinePool::discard(VKDiscardPool &discard_pool, VkPipelineLayout vk_pipeline_layout)
607{
608 graphics_.discard(discard_pool, vk_pipeline_layout);
609 compute_.discard(discard_pool, vk_pipeline_layout);
610 shaders_libs_.discard(discard_pool, vk_pipeline_layout);
611 /* vertex_input_libs_ and fragment_output_libs_ are NOT dependent on vk_pipeline_layout. */
612}
613
615{
616 const VkDevice vk_device = device.vk_handle();
617
618 graphics_.free_data(vk_device, device.functions);
619 compute_.free_data(vk_device, device.functions);
620 vertex_input_libs_.free_data(vk_device, device.functions);
621 shaders_libs_.free_data(vk_device, device.functions);
622 fragment_output_libs_.free_data(vk_device, device.functions);
623
624 device.functions.vkDestroyPipelineCache(device.vk_handle(), vk_pipeline_cache_static_, nullptr);
625 device.functions.vkDestroyPipelineCache(
626 device.vk_handle(), vk_pipeline_cache_non_static_, nullptr);
627}
628
629/* -------------------------------------------------------------------- */
632
633#ifdef WITH_BUILDINFO
634struct VKPipelineCachePrefixHeader {
635 /* `BC` stands for "Blender Cache" + 2 bytes for file versioning. */
636 uint32_t magic = 0xBC00;
637 uint32_t blender_version = BLENDER_VERSION;
638 uint32_t blender_version_patch = BLENDER_VERSION_PATCH;
639 char commit_hash[8];
640 uint32_t data_size;
641 uint32_t vendor_id;
642 uint32_t device_id;
643 uint32_t driver_version;
644 uint8_t pipeline_cache_uuid[VK_UUID_SIZE];
645
646 VKPipelineCachePrefixHeader()
647 {
648 const VKDevice &device = VKBackend::get().device;
649 data_size = 0;
650 const VkPhysicalDeviceProperties &properties = device.physical_device_properties_get();
651 vendor_id = properties.vendorID;
652 device_id = properties.deviceID;
653 driver_version = properties.driverVersion;
654 memcpy(&pipeline_cache_uuid, &properties.pipelineCacheUUID, VK_UUID_SIZE);
655
656 memset(commit_hash, 0, sizeof(commit_hash));
657 STRNCPY(commit_hash, build_hash);
658 }
659};
660
661static std::string pipeline_cache_filepath_get()
662{
663 static char tmp_dir_buffer[1024];
664 BKE_appdir_folder_caches(tmp_dir_buffer, sizeof(tmp_dir_buffer));
665
666 std::string cache_dir = std::string(tmp_dir_buffer) + "vk-pipeline-cache" + SEP_STR;
667 BLI_dir_create_recursive(cache_dir.c_str());
668 std::string cache_file = cache_dir + "static.bin";
669 return cache_file;
670}
671#endif
672
674{
675#ifdef WITH_BUILDINFO
676 /* Don't read the shader cache when GPU debugging is enabled. When enabled we use different
677 * shaders and compilation settings. Previous generated pipelines will not be used. */
678 if (bool(G.debug & G_DEBUG_GPU)) {
679 return;
680 }
681
682 std::string cache_file = pipeline_cache_filepath_get();
683 if (!BLI_exists(cache_file.c_str())) {
684 return;
685 }
686
687 /* Prevent old cache files from being deleted if they're still being used. */
688 BLI_file_touch(cache_file.c_str());
689 /* Read cached binary. */
690 fstream file(cache_file, std::ios::binary | std::ios::in | std::ios::ate);
691 std::streamsize data_size = file.tellg();
692 file.seekg(0, std::ios::beg);
693 Vector<char> buffer(data_size);
694 file.read(buffer.data(), data_size);
695 file.close();
696
697 /* Validate the prefix header. */
698 VKPipelineCachePrefixHeader prefix;
699 VKPipelineCachePrefixHeader &read_prefix = *reinterpret_cast<VKPipelineCachePrefixHeader *>(
700 buffer.data());
701 prefix.data_size = read_prefix.data_size;
702 if (memcmp(&read_prefix, &prefix, sizeof(VKPipelineCachePrefixHeader)) != 0) {
703 /* Headers are different, most likely the cache will not work and potentially crash the driver.
704 * [https://medium.com/@zeuxcg/creating-a-robust-pipeline-cache-with-vulkan-961d09416cda]
705 */
706 CLOG_INFO(&LOG,
707 "Pipeline cache on disk [%s] is ignored as it was written by a different driver or "
708 "Blender version. Cache will be overwritten when exiting.",
709 cache_file.c_str());
710 return;
711 }
712
713 CLOG_INFO(&LOG, "Initialize static pipeline cache from disk [%s].", cache_file.c_str());
714 VKDevice &device = VKBackend::get().device;
715 VkPipelineCacheCreateInfo create_info = {};
716 create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
717 create_info.initialDataSize = read_prefix.data_size;
718 create_info.pInitialData = buffer.data() + sizeof(VKPipelineCachePrefixHeader);
719 VkPipelineCache vk_pipeline_cache = VK_NULL_HANDLE;
720 device.functions.vkCreatePipelineCache(
721 device.vk_handle(), &create_info, nullptr, &vk_pipeline_cache);
722
723 device.functions.vkMergePipelineCaches(
724 device.vk_handle(), vk_pipeline_cache_static_, 1, &vk_pipeline_cache);
725 device.functions.vkDestroyPipelineCache(device.vk_handle(), vk_pipeline_cache, nullptr);
726#endif
727}
728
730{
731#ifdef WITH_BUILDINFO
732 /* Don't write the pipeline cache when GPU debugging is enabled. When enabled we use different
733 * shaders and compilation settings. Writing them to disk will clutter the pipeline cache. */
734 if (bool(G.debug & G_DEBUG_GPU)) {
735 return;
736 }
737
738 VKDevice &device = VKBackend::get().device;
739 size_t data_size;
740 device.functions.vkGetPipelineCacheData(
741 device.vk_handle(), vk_pipeline_cache_static_, &data_size, nullptr);
742 Vector<char> buffer(data_size);
743 device.functions.vkGetPipelineCacheData(
744 device.vk_handle(), vk_pipeline_cache_static_, &data_size, buffer.data());
745
746 std::string cache_file = pipeline_cache_filepath_get();
747 CLOG_INFO(&LOG, "Writing static pipeline cache to disk [%s].", cache_file.c_str());
748
749 fstream file(cache_file, std::ios::binary | std::ios::out);
750
751 VKPipelineCachePrefixHeader header;
752 header.data_size = data_size;
753 file.write(reinterpret_cast<char *>(&header), sizeof(VKPipelineCachePrefixHeader));
754 file.write(buffer.data(), data_size);
755#endif
756}
757
759
760} // namespace gpu
761} // namespace blender
#define BLENDER_VERSION_PATCH
#define BLENDER_VERSION
File and directory operations.
Platform independent time functions.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr const char * c_str() const
static VKBackend & get()
VKPipelinePool pipelines
Definition vk_device.hh:274
VkDevice vk_handle() const
Definition vk_device.hh:338
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:294
VolkDeviceTable functions
Definition vk_device.hh:280
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:394
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)
#define index(i)
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
std::string to_gpu_format_string(VkFormat format)
Definition vk_common.cc:138
static CLG_LogRef LOG
static VkPipeline create_graphics_pipeline_no_libs(const VKGraphicsInfo &graphics_info, VkPipelineCache vk_pipeline_cache, VkPipeline vk_pipeline_base, StringRefNull name)
static VkPipeline create_graphics_pipeline_libs(const VKGraphicsInfo &graphics_info, VkPipelineCache vk_pipeline_cache, VkPipeline vk_pipeline_base, StringRefNull name)
BMesh Iterators.
@ GPU_CULL_FRONT
Definition GPU_state.hh:158
@ GPU_CULL_NONE
Definition GPU_state.hh:157
@ GPU_CULL_BACK
Definition GPU_state.hh:159
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
Definition GPU_state.hh:153
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
Definition GPU_state.hh:152
@ GPU_STENCIL_OP_NONE
Definition GPU_state.hh:149
@ GPU_STENCIL_OP_REPLACE
Definition GPU_state.hh:150
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
@ GPU_BLEND_MIN
Definition GPU_state.hh:102
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
Definition GPU_state.hh:122
@ GPU_BLEND_TRANSPARENCY
Definition GPU_state.hh:128
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_OIT
Definition GPU_state.hh:109
@ GPU_BLEND_MAX
Definition GPU_state.hh:104
@ GPU_BLEND_ALPHA_UNDER_PREMUL
Definition GPU_state.hh:117
@ GPU_BLEND_ADDITIVE_PREMULT
Definition GPU_state.hh:93
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
@ GPU_BLEND_ADDITIVE
Definition GPU_state.hh:92
@ GPU_BLEND_BACKGROUND
Definition GPU_state.hh:111
@ GPU_BLEND_SUBTRACT
Definition GPU_state.hh:95
@ GPU_BLEND_ALPHA_PREMULT
Definition GPU_state.hh:91
@ GPU_BLEND_INVERT
Definition GPU_state.hh:100
@ GPU_BLEND_MULTIPLY
Definition GPU_state.hh:94
@ GPU_BLEND_CUSTOM
Definition GPU_state.hh:116
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
@ GPU_DEPTH_ALWAYS
Definition GPU_state.hh:133
@ GPU_DEPTH_GREATER
Definition GPU_state.hh:137
@ GPU_DEPTH_LESS
Definition GPU_state.hh:134
@ GPU_DEPTH_GREATER_EQUAL
Definition GPU_state.hh:138
@ GPU_DEPTH_EQUAL
Definition GPU_state.hh:136
unsigned char uint8_t
Definition BKE_text.h:79
char build_hash[]
Definition bpy_app.cc:73
@ GPU_STENCIL_ALWAYS
Definition GPU_state.hh:143
@ GPU_STENCIL_EQUAL
Definition GPU_state.hh:144
@ GPU_STENCIL_NONE
Definition GPU_state.hh:142
@ GPU_STENCIL_NEQUAL
Definition GPU_state.hh:145
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:318
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
Definition appdir.cc:206
@ GPU_WRITE_ALPHA
Definition GPU_state.hh:24
@ GPU_WRITE_COLOR
Definition GPU_state.hh:27
@ GPU_WRITE_RED
Definition GPU_state.hh:21
@ GPU_WRITE_BLUE
Definition GPU_state.hh:23
@ GPU_WRITE_STENCIL
Definition GPU_state.hh:26
@ GPU_WRITE_GREEN
Definition GPU_state.hh:22
@ GPU_WRITE_DEPTH
Definition GPU_state.hh:25
static CLG_LogRef LOG
Definition logging.cpp:12
double BLI_time_now_seconds()
Definition time.cc:30
@ GPU_VERTEX_LAST
Definition GPU_state.hh:163
@ GPU_VERTEX_FIRST
Definition GPU_state.hh:164
PipelineGraphic pipeline(vert_main, frag_main)
Vector< shader::SpecializationConstant::Value > specialization_constants
VkPipelineLayout vk_pipeline_layout
Vector< VkFormat, GPU_FB_MAX_COLOR_ATTACHMENT > color_attachment_formats
std::string pipeline_info_source() const
void build_vertex_input_lib(VKDevice &device, const VKGraphicsInfo::VertexIn &vertex_input_info, VkPipeline vk_pipeline_base)
void build_full(VKDevice &device, const VKGraphicsInfo &graphics_info, VkPipeline vk_pipeline_base)
void build_fragment_output_lib(const VKGraphicsInfo::FragmentOut &fragment_output_info, VkPipeline vk_pipeline_base)
void build_shaders_lib(const VKGraphicsInfo::Shaders &shaders_info, const VKExtensions &extensions, const VKWorkarounds &workarounds, VkPipeline vk_pipeline_base)
int size() const
Definition vector.hh:130
#define SEP_STR
Definition unit.cc:41