36 VkPipelineCacheCreateInfo create_info = {};
37 create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
39 device.
vk_handle(), &create_info,
nullptr, &vk_pipeline_cache_static_);
42 device.
vk_handle(), &create_info,
nullptr, &vk_pipeline_cache_non_static_);
51 const bool is_static_shader,
52 VkPipeline vk_pipeline_base,
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);
62VkPipeline VKPipelineMap<VKComputeInfo>::create(
const VKComputeInfo &compute_info,
63 VkPipelineCache vk_pipeline_cache,
64 VkPipeline vk_pipeline_base,
69 VkComputePipelineCreateInfo vk_compute_pipeline_create_info = {
70 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
73 {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
76 VK_SHADER_STAGE_COMPUTE_BIT,
85 VkSpecializationInfo vk_specialization_info;
86 Array<VkSpecializationMapEntry> vk_specialization_map_entries(
88 if (do_specialization_constants) {
89 vk_compute_pipeline_create_info.stage.pSpecializationInfo = &vk_specialization_info;
91 vk_specialization_map_entries[
index] = {
94 vk_specialization_info = {
uint32_t(vk_specialization_map_entries.size()),
95 vk_specialization_map_entries.data(),
105 VkPipeline
pipeline = VK_NULL_HANDLE;
106 device.functions.vkCreateComputePipelines(device.vk_handle(),
109 &vk_compute_pipeline_create_info,
115 "Compiled compute pipeline %s in %fms ",
117 (end_time - start_time) * 1000.0);
129 const bool is_static_shader,
130 VkPipeline vk_pipeline_base,
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);
145 VkPipelineCache vk_pipeline_cache,
146 VkPipeline vk_pipeline_base,
151 builder.
build_full(device, graphics_info, vk_pipeline_base);
154 VkPipeline pipeline = VK_NULL_HANDLE;
165 "Compiled graphics pipeline %s in %fms ",
167 (end_time - start_time) * 1000.0);
172 VkPipelineCache vk_pipeline_cache,
173 VkPipeline vk_pipeline_base,
185 std::array<VkPipeline, 3> pipeline_libraries = {
186 vertex_input_lib, shaders_lib, fragment_output_lib};
189 VkPipeline pipeline = VK_NULL_HANDLE;
190 VkPipelineLibraryCreateInfoKHR vk_pipeline_library_create_info = {
191 VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR,
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,
216 device.
functions.vkCreateGraphicsPipelines(
217 device.
vk_handle(), vk_pipeline_cache, 1, &linking_pipeline_create_info,
nullptr, &pipeline);
221 "Linking graphics pipeline %s in %fms ",
223 (end_time - start_link_time) * 1000.0);
225 "Compiling graphics pipeline %s in %fms ",
227 (end_time - start_time) * 1000.0);
232VkPipeline VKPipelineMap<VKGraphicsInfo>::create(
const VKGraphicsInfo &graphics_info,
233 VkPipelineCache vk_pipeline_cache,
234 VkPipeline vk_pipeline_base,
243 graphics_info, vk_pipeline_cache, vk_pipeline_base, name);
249 result <<
"info.pipeline_state()\n";
252 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
253 result <<
"GPU_PRIM_POINTS";
255 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
256 result <<
"GPU_PRIM_LINES";
258 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
259 result <<
"GPU_PRIM_LINE_STRIP";
261 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
262 result <<
"GPU_PRIM_TRIS";
264 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
265 result <<
"GPU_PRIM_TRI_STRIP";
267 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
268 result <<
"GPU_PRIM_TRI_FAN";
270 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
271 result <<
"GPU_PRIM_LINES_ADJ";
273 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
274 result <<
"GPU_PRIM_TRIS_ADJ";
285 write_masks.append(
"GPU_WRITE_COLOR");
289 write_masks.append(
"GPU_WRITE_RED");
292 write_masks.append(
"GPU_WRITE_GREEN");
295 write_masks.append(
"GPU_WRITE_BLUE");
298 write_masks.append(
"GPU_WRITE_ALPHA");
303 write_masks.append(
"GPU_WRITE_DEPTH");
306 write_masks.append(
"GPU_WRITE_STENCIL");
308 if (write_masks.is_empty()) {
309 write_masks.append(
"GPU_WRITE_NONE");
311 for (
const std::string &write_mask : write_masks) {
313 if (&write_mask != &write_masks.last()) {
321 result <<
"GPU_BLEND_NONE";
324 result <<
"GPU_BLEND_ALPHA";
327 result <<
"GPU_BLEND_ALPHA_PREMULT";
330 result <<
"GPU_BLEND_ADDITIVE";
333 result <<
"GPU_BLEND_ADDITIVE_PREMULT";
336 result <<
"GPU_BLEND_MULTIPLY";
339 result <<
"GPU_BLEND_SUBTRACT";
342 result <<
"GPU_BLEND_INVERT";
345 result <<
"GPU_BLEND_MIN";
348 result <<
"GPU_BLEND_MAX";
351 result <<
"GPU_BLEND_OIT";
354 result <<
"GPU_BLEND_BACKGROUND";
357 result <<
"GPU_BLEND_CUSTOM";
360 result <<
"GPU_BLEND_ALPHA_UNDER_PREMUL";
363 result <<
"GPU_BLEND_OVERLAY_MASK_FROM_ALPHA";
366 result <<
"GPU_BLEND_TRANSPARENCY";
375 result <<
"GPU_CULL_NONE";
378 result <<
"GPU_CULL_FRONT";
381 result <<
"GPU_CULL_BACK";
390 result <<
"GPU_DEPTH_NONE";
393 result <<
"GPU_DEPTH_LESS";
396 result <<
"GPU_DEPTH_LESS_EQUAL";
399 result <<
"GPU_DEPTH_EQUAL";
402 result <<
"GPU_DEPTH_GREATER";
405 result <<
"GPU_DEPTH_GREATER_EQUAL";
408 result <<
"GPU_DEPTH_ALWAYS";
417 result <<
"GPU_STENCIL_NONE";
420 result <<
"GPU_STENCIL_ALWAYS";
423 result <<
"GPU_STENCIL_EQUAL";
426 result <<
"GPU_STENCIL_NEQUAL";
435 result <<
"GPU_STENCIL_OP_NONE";
438 result <<
"GPU_STENCIL_OP_REPLACE";
441 result <<
"GPU_STENCIL_OP_COUNT_DEPTH_PASS";
444 result <<
"GPU_STENCIL_OP_COUNT_DEPTH_FAIL";
453 result <<
"GPU_VERTEX_FIRST";
456 result <<
"GPU_VERTEX_LAST";
466 result <<
" .depth_format(gpu::TextureTargetFormat::"
471 result <<
" .stencil_format(gpu::TextureTargetFormat::"
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);
498VkPipeline VKPipelineMap<VKGraphicsInfo::VertexIn>::create(
500 VkPipelineCache vk_pipeline_cache,
501 VkPipeline vk_pipeline_base,
509 VkPipeline pipeline = VK_NULL_HANDLE;
519 CLOG_TRACE(&
LOG,
"Compiled vertex input library in %fms ", (end_time - start_time) * 1000.0);
531 bool created =
false;
532 return shaders_libs_.get_or_create(
533 shaders_info, vk_pipeline_cache_non_static_, VK_NULL_HANDLE,
"ShadersLib", created);
537VkPipeline VKPipelineMap<VKGraphicsInfo::Shaders>::create(
539 VkPipelineCache vk_pipeline_cache,
540 VkPipeline vk_pipeline_base,
547 builder.
build_shaders_lib(shaders_info, extensions, workarounds, vk_pipeline_base);
550 VkPipeline pipeline = VK_NULL_HANDLE;
560 CLOG_TRACE(&
LOG,
"Compiled shaders library in %fms ", (end_time - start_time) * 1000.0);
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);
579VkPipeline VKPipelineMap<VKGraphicsInfo::FragmentOut>::create(
581 VkPipelineCache vk_pipeline_cache,
582 VkPipeline vk_pipeline_base,
590 VkPipeline pipeline = VK_NULL_HANDLE;
600 CLOG_TRACE(&
LOG,
"Compiled fragment output library in %fms ", (end_time - start_time) * 1000.0);
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);
616 const VkDevice vk_device = device.
vk_handle();
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);
624 device.
functions.vkDestroyPipelineCache(device.
vk_handle(), vk_pipeline_cache_static_,
nullptr);
626 device.
vk_handle(), vk_pipeline_cache_non_static_,
nullptr);
634struct VKPipelineCachePrefixHeader {
644 uint8_t pipeline_cache_uuid[VK_UUID_SIZE];
646 VKPipelineCachePrefixHeader()
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);
656 memset(commit_hash, 0,
sizeof(commit_hash));
661static std::string pipeline_cache_filepath_get()
663 static char tmp_dir_buffer[1024];
666 std::string cache_dir = std::string(tmp_dir_buffer) +
"vk-pipeline-cache" +
SEP_STR;
668 std::string cache_file = cache_dir +
"static.bin";
682 std::string cache_file = pipeline_cache_filepath_get();
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);
694 file.read(buffer.
data(), data_size);
698 VKPipelineCachePrefixHeader prefix;
699 VKPipelineCachePrefixHeader &read_prefix = *
reinterpret_cast<VKPipelineCachePrefixHeader *
>(
701 prefix.data_size = read_prefix.data_size;
702 if (memcmp(&read_prefix, &prefix,
sizeof(VKPipelineCachePrefixHeader)) != 0) {
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.",
713 CLOG_INFO(&
LOG,
"Initialize static pipeline cache from disk [%s].", cache_file.c_str());
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;
721 device.
vk_handle(), &create_info,
nullptr, &vk_pipeline_cache);
724 device.
vk_handle(), vk_pipeline_cache_static_, 1, &vk_pipeline_cache);
725 device.
functions.vkDestroyPipelineCache(device.
vk_handle(), vk_pipeline_cache,
nullptr);
741 device.
vk_handle(), vk_pipeline_cache_static_, &data_size,
nullptr);
744 device.
vk_handle(), vk_pipeline_cache_static_, &data_size, buffer.
data());
746 std::string cache_file = pipeline_cache_filepath_get();
747 CLOG_INFO(&
LOG,
"Writing static pipeline cache to disk [%s].", cache_file.c_str());
749 fstream file(cache_file, std::ios::binary | std::ios::out);
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);
#define BLENDER_VERSION_PATCH
File and directory operations.
Platform independent time functions.
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_TRACE(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr const char * c_str() const
VkDevice vk_handle() const
const VKExtensions & extensions_get() const
const VkPhysicalDeviceProperties & physical_device_properties_get() const
VolkDeviceTable functions
const VKWorkarounds & workarounds_get() const
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)
void object_label(GLenum type, GLuint object, const char *name)
std::string to_gpu_format_string(VkFormat format)
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)
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
@ GPU_BLEND_ALPHA_UNDER_PREMUL
@ GPU_BLEND_ADDITIVE_PREMULT
@ GPU_BLEND_ALPHA_PREMULT
@ GPU_DEPTH_GREATER_EQUAL
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
double BLI_time_now_seconds()
PipelineGraphic pipeline(vert_main, frag_main)
Vector< shader::SpecializationConstant::Value > specialization_constants
VkShaderModule vk_shader_module
VkPipelineLayout vk_pipeline_layout
bool graphics_pipeline_library
VkFormat stencil_attachment_format
Vector< VkFormat, GPU_FB_MAX_COLOR_ATTACHMENT > color_attachment_formats
VkFormat depth_attachment_format
VkPipelineLayout vk_pipeline_layout
VkPrimitiveTopology vk_topology
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)
VkGraphicsPipelineCreateInfo vk_graphics_pipeline_create_info