Blender V5.3
vk_shader.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <sstream>
10
11#include "vk_shader.hh"
12
13#include "vk_backend.hh"
14#include "vk_framebuffer.hh"
16#include "vk_shader_log.hh"
17#include "vk_state_manager.hh"
19
20#include "BLI_vector.hh"
21
22#include "BKE_global.hh"
23
24#include "CLG_log.h"
25
26#include <fmt/format.h>
27#include <fmt/ranges.h>
28
29namespace blender {
30
31static CLG_LogRef LOG = {"gpu.vulkan"};
32
33using namespace blender::gpu::shader;
34
35namespace gpu {
36
37/* -------------------------------------------------------------------- */
40
41static const char *to_string(const Interpolation &interp)
42{
43 switch (interp) {
45 return "smooth";
47 return "flat";
49 return "noperspective";
50 default:
51 return "unknown";
52 }
53}
54
55static const char *to_string(const Type &type)
56{
57 switch (type) {
58 case Type::float_t:
59 return "float";
60 case Type::float2_t:
61 return "vec2";
62 case Type::float3_t:
63 return "vec3";
64 case Type::float4_t:
65 return "vec4";
67 return "mat3";
69 return "mat4";
70 case Type::uint_t:
71 return "uint";
72 case Type::uint2_t:
73 return "uvec2";
74 case Type::uint3_t:
75 return "uvec3";
76 case Type::uint4_t:
77 return "uvec4";
78 case Type::int_t:
79 return "int";
80 case Type::int2_t:
81 return "ivec2";
82 case Type::int3_t:
83 return "ivec3";
84 case Type::int4_t:
85 return "ivec4";
86 case Type::bool_t:
87 return "bool";
88 default:
89 return "unknown";
90 }
91}
92
93static const char *to_string(const TextureFormat &type)
94{
95 switch (type) {
96 case TextureFormat::UINT_8_8_8_8:
97 return "rgba8ui";
98 case TextureFormat::SINT_8_8_8_8:
99 return "rgba8i";
100 case TextureFormat::UNORM_8_8_8_8:
101 return "rgba8";
102 case TextureFormat::UINT_32_32_32_32:
103 return "rgba32ui";
104 case TextureFormat::SINT_32_32_32_32:
105 return "rgba32i";
106 case TextureFormat::SFLOAT_32_32_32_32:
107 return "rgba32f";
108 case TextureFormat::UINT_16_16_16_16:
109 return "rgba16ui";
110 case TextureFormat::SINT_16_16_16_16:
111 return "rgba16i";
112 case TextureFormat::SFLOAT_16_16_16_16:
113 return "rgba16f";
114 case TextureFormat::UNORM_16_16_16_16:
115 return "rgba16";
116 case TextureFormat::UINT_8_8:
117 return "rg8ui";
118 case TextureFormat::SINT_8_8:
119 return "rg8i";
120 case TextureFormat::UNORM_8_8:
121 return "rg8";
122 case TextureFormat::UINT_32_32:
123 return "rg32ui";
124 case TextureFormat::SINT_32_32:
125 return "rg32i";
126 case TextureFormat::SFLOAT_32_32:
127 return "rg32f";
128 case TextureFormat::UINT_16_16:
129 return "rg16ui";
130 case TextureFormat::SINT_16_16:
131 return "rg16i";
132 case TextureFormat::SFLOAT_16_16:
133 return "rg16f";
134 case TextureFormat::UNORM_16_16:
135 return "rg16";
136 case TextureFormat::UINT_8:
137 return "r8ui";
138 case TextureFormat::SINT_8:
139 return "r8i";
140 case TextureFormat::UNORM_8:
141 return "r8";
142 case TextureFormat::UINT_32:
143 return "r32ui";
144 case TextureFormat::SINT_32:
145 return "r32i";
146 case TextureFormat::SFLOAT_32:
147 return "r32f";
148 case TextureFormat::UINT_16:
149 return "r16ui";
150 case TextureFormat::SINT_16:
151 return "r16i";
152 case TextureFormat::SFLOAT_16:
153 return "r16f";
154 case TextureFormat::UNORM_16:
155 return "r16";
156 case TextureFormat::UFLOAT_11_11_10:
157 return "r11f_g11f_b10f";
158 case TextureFormat::UNORM_10_10_10_2:
159 return "rgb10_a2";
160 default:
161 return "unknown";
162 }
163}
164
165static const char *to_string(const PrimitiveIn &layout)
166{
167 switch (layout) {
169 return "points";
171 return "lines";
173 return "lines_adjacency";
175 return "triangles";
177 return "triangles_adjacency";
178 default:
179 return "unknown";
180 }
181}
182
183static const char *to_string(const PrimitiveOut &layout)
184{
185 switch (layout) {
187 return "points";
189 return "line_strip";
191 return "triangle_strip";
192 default:
193 return "unknown";
194 }
195}
196
197static const char *to_string(const DepthWrite &value)
198{
199 switch (value) {
200 case DepthWrite::ANY:
201 return "depth_any";
203 return "depth_greater";
204 case DepthWrite::LESS:
205 return "depth_less";
206 default:
207 return "depth_unchanged";
208 }
209}
210
211static void print_image_type(std::ostream &os,
212 const ImageType &type,
214{
215 switch (type) {
216 case ImageType::IntBuffer:
217 case ImageType::Int1D:
218 case ImageType::Int1DArray:
219 case ImageType::Int2D:
220 case ImageType::Int2DArray:
221 case ImageType::Int3D:
222 case ImageType::IntCube:
223 case ImageType::IntCubeArray:
224 case ImageType::AtomicInt2D:
225 case ImageType::AtomicInt2DArray:
226 case ImageType::AtomicInt3D:
227 os << "i";
228 break;
229 case ImageType::UintBuffer:
230 case ImageType::Uint1D:
231 case ImageType::Uint1DArray:
232 case ImageType::Uint2D:
233 case ImageType::Uint2DArray:
234 case ImageType::Uint3D:
235 case ImageType::UintCube:
236 case ImageType::UintCubeArray:
237 case ImageType::AtomicUint2D:
238 case ImageType::AtomicUint2DArray:
239 case ImageType::AtomicUint3D:
240 os << "u";
241 break;
242 default:
243 break;
244 }
245
247 os << "image";
248 }
249 else {
250 os << "sampler";
251 }
252
253 switch (type) {
254 case ImageType::FloatBuffer:
255 case ImageType::IntBuffer:
256 case ImageType::UintBuffer:
257 os << "Buffer";
258 break;
259 case ImageType::Float1D:
260 case ImageType::Float1DArray:
261 case ImageType::Int1D:
262 case ImageType::Int1DArray:
263 case ImageType::Uint1D:
264 case ImageType::Uint1DArray:
265 os << "1D";
266 break;
267 case ImageType::Float2D:
268 case ImageType::Float2DArray:
269 case ImageType::Int2D:
270 case ImageType::Int2DArray:
271 case ImageType::Uint2D:
272 case ImageType::Uint2DArray:
273 case ImageType::Shadow2D:
274 case ImageType::Shadow2DArray:
275 case ImageType::Depth2D:
276 case ImageType::Depth2DArray:
277 case ImageType::AtomicInt2D:
278 case ImageType::AtomicInt2DArray:
279 case ImageType::AtomicUint2D:
280 case ImageType::AtomicUint2DArray:
281 os << "2D";
282 break;
283 case ImageType::Float3D:
284 case ImageType::Int3D:
285 case ImageType::AtomicInt3D:
286 case ImageType::Uint3D:
287 case ImageType::AtomicUint3D:
288 os << "3D";
289 break;
290 case ImageType::FloatCube:
291 case ImageType::FloatCubeArray:
292 case ImageType::IntCube:
293 case ImageType::IntCubeArray:
294 case ImageType::UintCube:
295 case ImageType::UintCubeArray:
296 case ImageType::ShadowCube:
297 case ImageType::ShadowCubeArray:
298 case ImageType::DepthCube:
299 case ImageType::DepthCubeArray:
300 os << "Cube";
301 break;
302 default:
303 break;
304 }
305
306 switch (type) {
307 case ImageType::Float1DArray:
308 case ImageType::Float2DArray:
309 case ImageType::FloatCubeArray:
310 case ImageType::Int1DArray:
311 case ImageType::Int2DArray:
312 case ImageType::IntCubeArray:
313 case ImageType::Uint1DArray:
314 case ImageType::Uint2DArray:
315 case ImageType::UintCubeArray:
316 case ImageType::Shadow2DArray:
317 case ImageType::ShadowCubeArray:
318 case ImageType::Depth2DArray:
319 case ImageType::DepthCubeArray:
320 case ImageType::AtomicUint2DArray:
321 os << "Array";
322 break;
323 default:
324 break;
325 }
326
327 switch (type) {
328 case ImageType::Shadow2D:
329 case ImageType::Shadow2DArray:
330 case ImageType::ShadowCube:
331 case ImageType::ShadowCubeArray:
332 os << "Shadow";
333 break;
334 default:
335 break;
336 }
337 os << " ";
338}
339
340static std::ostream &print_qualifier(std::ostream &os, const Qualifier &qualifiers)
341{
342 if (!flag_is_set(qualifiers, Qualifier::no_restrict)) {
343 os << "restrict ";
344 }
345 if (!flag_is_set(qualifiers, Qualifier::read)) {
346 os << "writeonly ";
347 }
348 if (!flag_is_set(qualifiers, Qualifier::write)) {
349 os << "readonly ";
350 }
351 return os;
352}
353
354static void print_resource(std::ostream &os,
355 const VKDescriptorSet::Location location,
357 const ShaderCreateInfo & /*info*/)
358{
359 os << "layout(binding = " << uint32_t(location);
361 os << ", " << to_string(res.image.format);
362 }
364 os << ", std140";
365 }
367 os << ", std430";
368 }
369 os << ") ";
370
371 switch (res.bind_type) {
373 os << "uniform ";
374 print_image_type(os, res.sampler.type, res.bind_type);
375 os << res.sampler.name << ";";
376 break;
378 os << "uniform ";
379 print_qualifier(os, res.image.qualifiers);
380 print_image_type(os, res.image.type, res.bind_type);
381 os << res.image.name << ";";
382 break;
384 os << "uniform _" << res.uniformbuf.name.str_no_array() << " { " << res.uniformbuf.type_name
385 << " " << res.uniformbuf.name << "; };";
386 break;
388 print_qualifier(os, res.storagebuf.qualifiers);
389 os << "buffer _" << res.storagebuf.name.str_no_array() << " { " << res.storagebuf.type_name
390 << " " << res.storagebuf.name << "; };";
391 break;
393 os << "uniform accelerationStructureEXT " << res.acceleration_structure.name << ";";
394 break;
395 }
396}
397
398static void print_resource(std::ostream &os,
399 const VKShaderInterface &shader_interface,
401 const ShaderCreateInfo &info)
402{
403 const VKDescriptorSet::Location location = shader_interface.descriptor_set_location(res);
404 print_resource(os, location, res, info);
405}
406
407static void print_resource(std::ostream &os,
408 const VKShaderInterface &shader_interface,
410 const ShaderCreateInfo &info,
411 StringRefNull res_frequency,
412 StringRefNull &active_info_name)
413{
414 if (assign_if_different(active_info_name, res.info_name)) {
415 os << "\n#define CREATE_INFO_RES_" << res_frequency << "_" << res.info_name << " \\\n";
416 }
417 print_resource(os, shader_interface, res, info);
418 os << " \\\n";
419}
420
421inline int get_location_count(const Type &type)
422{
423 if (type == shader::Type::float4x4_t) {
424 return 4;
425 }
426 if (type == shader::Type::float3x3_t) {
427 return 3;
428 }
429 return 1;
430}
431
432static void print_interface_as_attributes(std::ostream &os,
433 const std::string &prefix,
434 const StageInterfaceInfo &iface,
435 int &location,
436 const StringRefNull &suffix)
437{
438 for (const StageInterfaceInfo::InOut &inout : iface.inouts) {
439 os << "layout(location=" << location << ") " << prefix << " " << to_string(inout.interp) << " "
440 << to_string(inout.type) << " " << inout.name << suffix << ";\n";
441 location += get_location_count(inout.type);
442 }
443}
444
445static void print_interface_as_struct(std::ostream &os,
446 const std::string &prefix,
447 const StageInterfaceInfo &iface,
448 int &location,
449 const StringRefNull &suffix)
450{
451 std::string struct_name = prefix + iface.name;
452 Interpolation qualifier = iface.inouts[0].interp;
453
454 os << "struct " << struct_name << " {\n";
455 for (const StageInterfaceInfo::InOut &inout : iface.inouts) {
456 os << " " << to_string(inout.type) << " " << inout.name << ";\n";
457 }
458 os << "};\n";
459 os << "layout(location=" << location << ") " << prefix << " " << to_string(qualifier) << " "
460 << struct_name << " " << iface.instance_name << suffix << ";\n";
461
462 for (const StageInterfaceInfo::InOut &inout : iface.inouts) {
463 location += get_location_count(inout.type);
464 }
465}
466
467static void print_interface(std::ostream &os,
468 const std::string &prefix,
469 const StageInterfaceInfo &iface,
470 int &location,
471 const StringRefNull &suffix = "")
472{
473 if (iface.instance_name.is_empty()) {
474 print_interface_as_attributes(os, prefix, iface, location, suffix);
475 }
476 else {
477 print_interface_as_struct(os, prefix, iface, location, suffix);
478 }
479}
480
482static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
483{
484 std::stringstream ss;
485 /* Prototype for the original main. */
486 ss << "\n";
487 ss << "void main_function_();\n";
488 /* Wrapper to the main function in order to inject code processing on globals. */
489 ss << "void main() {\n";
490 ss << pre_main;
491 ss << " main_function_();\n";
492 ss << post_main;
493 ss << "}\n";
494 /* Rename the original main. */
495 ss << "#define main main_function_\n";
496 ss << "\n";
497 return ss.str();
498}
499
500static std::string combine_sources(Span<StringRefNull> sources)
501{
502 return fmt::to_string(fmt::join(sources, ""));
503}
504
506{
507 context_ = VKContext::get();
508}
509
510void VKShader::init(const shader::ShaderCreateInfo &info, bool /*is_codegen_only*/)
511{
512 VKShaderInterface *vk_interface = new VKShaderInterface();
513 vk_interface->init(info);
514 interface = vk_interface;
515 is_static_shader_ = info.do_static_compilation_;
516 is_compute_shader_ = !info.compute_source_.is_empty() || !info.compute_source_generated.empty();
517 max_input_attachment_index_ = 0;
518 for (const ShaderCreateInfo::SubpassIn &input : info.subpass_inputs_) {
519 max_input_attachment_index_ = max_uu(max_input_attachment_index_, uint32_t(input.index));
520 }
521 use_ray_query_ = bool(info.builtins_combined() & BuiltinBits::RAY_QUERY);
522}
523
525{
526 VKDevice &device = VKBackend::get().device;
528
529 if (vk_pipeline_layout != VK_NULL_HANDLE) {
530 device.pipelines.discard(discard_pool, vk_pipeline_layout);
532 vk_pipeline_layout = VK_NULL_HANDLE;
533 }
534 /* Unset not owning handles. */
535 vk_descriptor_set_layout_ = VK_NULL_HANDLE;
536}
537
538void VKShader::build_shader_module(MutableSpan<StringRefNull> sources,
539 shaderc_shader_kind stage,
540 VKShaderModule &r_shader_module)
541{
542 r_shader_module.is_ready = false;
543 const VKDevice &device = VKBackend::get().device;
544 std::string source_patch;
545
546 switch (stage) {
547 case shaderc_vertex_shader:
548 source_patch = device.glsl_vertex_patch_get(use_ray_query_);
549 break;
550 case shaderc_geometry_shader:
551 source_patch = device.glsl_geometry_patch_get();
552 break;
553 case shaderc_fragment_shader:
554 source_patch = device.glsl_fragment_patch_get(use_ray_query_);
555 break;
556 case shaderc_compute_shader:
557 source_patch = device.glsl_compute_patch_get(use_ray_query_);
558 break;
559 default:
560 BLI_assert_msg(0, "Only forced ShaderC shader kinds are supported.");
561 break;
562 }
563
564 sources[SOURCES_INDEX_VERSION] = source_patch;
565 r_shader_module.combined_sources = combine_sources(sources);
566 VKShaderCompiler::compile_module(*this, stage, r_shader_module);
567 r_shader_module.is_ready = true;
568}
569
572{
573 build_shader_module(sources, shaderc_vertex_shader, vertex_module);
574}
575
578{
579 build_shader_module(sources, shaderc_geometry_shader, geometry_module);
580}
581
584{
585 build_shader_module(sources, shaderc_fragment_shader, fragment_module);
586}
587
590{
591 build_shader_module(sources, shaderc_compute_shader, compute_module);
592}
593
594void VKShader::warm_cache(int /*limit*/)
595{
597}
598
600{
601 /* Add-ons that still use old API will crash as the shader create info isn't available.
602 * See #130555 */
603 if (info == nullptr) {
604 return false;
605 }
606
607 if (do_geometry_shader_injection(info)) {
608 std::string source = workaround_geometry_shader_source_create(*info);
609 Vector<StringRefNull> sources;
610 sources.append("version");
611 sources.append(source);
612 geometry_shader_from_glsl(*info, sources);
613 }
614
615 const VKShaderInterface &vk_interface = interface_get();
616 VKDevice &device = VKBackend::get().device;
617 if (!finalize_descriptor_set_layouts(device, vk_interface)) {
618 return false;
619 }
620 if (!finalize_pipeline_layout(device, vk_interface)) {
621 return false;
622 }
623
625 return finalize_post(info->pipelines_.as_span());
626}
627
629{
630 bool result = finalize_shader_module(vertex_module, "vertex") &&
631 finalize_shader_module(geometry_module, "geometry") &&
632 finalize_shader_module(fragment_module, "fragment") &&
633 finalize_shader_module(compute_module, "compute");
634
635 if (!result) {
636 return result;
637 }
638 /* Ensure that pipelines are already built. For compute shaders only the default state is
639 * compiled. For graphics shaders the pipeline state is read from the shader create info. */
640 if (is_compute_shader_) {
641 /* This is only done for the first shader compilation (not specialization).
642 * Give the default constants. */
644 }
645 else {
646 result &= ensure_graphics_pipelines(pipeline_states);
647 }
648
649 return result;
650}
651
652bool VKShader::finalize_shader_module(VKShaderModule &shader_module, const char *stage_name)
653{
655 bool compilation_succeeded = ELEM(shader_module.compilation_result.GetCompilationStatus(),
656 shaderc_compilation_status_null_result_object,
657 shaderc_compilation_status_success);
658 if (bool(shader_module.compilation_result.GetNumWarnings() +
659 shader_module.compilation_result.GetNumErrors()))
660 {
661 print_log({shader_module.original_sources},
662 shader_module.compilation_result.GetErrorMessage().c_str(),
663 stage_name,
664 bool(shader_module.compilation_result.GetNumErrors()),
665 &parser);
666 }
667
668 std::string full_name = std::string(name) + "_" + stage_name;
669 shader_module.finalize(full_name.c_str());
670 shader_module.original_sources.clear();
671 shader_module.combined_sources.clear();
672 shader_module.sources_hash.clear();
673 shader_module.compilation_result = {};
674 shader_module.spirv_binary.clear();
675 return compilation_succeeded;
676}
677
678bool VKShader::finalize_pipeline_layout(VKDevice &device,
679 const VKShaderInterface &shader_interface)
680{
681 const uint32_t layout_count = vk_descriptor_set_layout_ == VK_NULL_HANDLE ? 0 : 1;
682 VkPipelineLayoutCreateInfo pipeline_info = {};
683 VkPushConstantRange push_constant_range = {};
684 pipeline_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
685 pipeline_info.flags = 0;
686 pipeline_info.setLayoutCount = layout_count;
687 pipeline_info.pSetLayouts = &vk_descriptor_set_layout_;
688
689 /* Setup push constants. */
690 const VKPushConstants::Layout &push_constants_layout =
691 shader_interface.push_constants_layout_get();
692 if (push_constants_layout.storage_type_get() == VKPushConstants::StorageType::PUSH_CONSTANTS) {
693 push_constant_range.offset = 0;
694 push_constant_range.size = push_constants_layout.size_in_bytes();
695 push_constant_range.stageFlags = is_compute_shader_ ? VK_SHADER_STAGE_COMPUTE_BIT :
696 VK_SHADER_STAGE_ALL_GRAPHICS;
697 pipeline_info.pushConstantRangeCount = 1;
698 pipeline_info.pPushConstantRanges = &push_constant_range;
699 }
700
701 if (device.functions.vkCreatePipelineLayout(
702 device.vk_handle(), &pipeline_info, nullptr, &vk_pipeline_layout) != VK_SUCCESS)
703 {
704 return false;
705 };
706
708
709 return true;
710}
711
712bool VKShader::finalize_descriptor_set_layouts(VKDevice &vk_device,
713 const VKShaderInterface &shader_interface)
714{
715 bool created;
716 bool needed;
717
718 vk_descriptor_set_layout_ = vk_device.descriptor_set_layouts_get().get_or_create(
719 shader_interface.descriptor_set_layout_info_get(), created, needed);
720 if (created) {
721 debug::object_label(vk_descriptor_set_layout_, name_get());
722 }
723 if (!needed) {
724 BLI_assert(vk_descriptor_set_layout_ == VK_NULL_HANDLE);
725 return true;
726 }
727 return vk_descriptor_set_layout_ != VK_NULL_HANDLE;
728}
729
731{
732 VKContext *context = VKContext::get();
733 /* Copy constants state. */
734 context->specialization_constants_set(constants_state);
735
736 /* Intentionally empty. Binding of the pipeline are done just before drawing/dispatching.
737 * See #VKPipeline.update_and_bind */
738}
739
741
742void VKShader::uniform_float(int location, int comp_len, int array_size, const float *data)
743{
744 push_constants.push_constant_set(location, comp_len, array_size, data);
745}
746
747void VKShader::uniform_int(int location, int comp_len, int array_size, const int *data)
748{
749 push_constants.push_constant_set(location, comp_len, array_size, data);
750}
751
753{
754 const VKShaderInterface &vk_interface = interface_get();
755 std::stringstream ss;
756
757 ss << "\n#line " << __LINE__ << " \"" << __FILE__ << "\"\n";
758
759 /* Specialization Constants (pass-through). */
760 uint constant_id = 0;
762 ss << "layout (constant_id=" << constant_id++ << ") const ";
763 switch (sc.type) {
764 case Type::int_t:
765 ss << "int " << sc.name << "=" << std::to_string(sc.value.i) << ";\n";
766 break;
767 case Type::uint_t:
768 ss << "uint " << sc.name << "=" << std::to_string(sc.value.u) << "u;\n";
769 break;
770 case Type::bool_t:
771 ss << "bool " << sc.name << "=" << (sc.value.u ? "true" : "false") << ";\n";
772 break;
773 case Type::float_t:
774 /* Use uint representation to allow exact same bit pattern even if NaN. uintBitsToFloat
775 * isn't supported during global const initialization. */
776 ss << "uint " << sc.name << "_uint=" << std::to_string(sc.value.u) << "u;\n";
777 ss << "#define " << sc.name << " uintBitsToFloat(" << sc.name << "_uint)\n";
778 break;
779 default:
781 break;
782 }
783 }
784
785 /* Compilation Constants (pass-through). */
786 for (const CompilationConstant &sc : info.compilation_constants_) {
787 ss << "const ";
788 switch (sc.type) {
789 case Type::int_t:
790 ss << "int " << sc.name << "=" << std::to_string(sc.value.i) << ";\n";
791 break;
792 case Type::uint_t:
793 ss << "uint " << sc.name << "=" << std::to_string(sc.value.u) << "u;\n";
794 break;
795 case Type::bool_t:
796 ss << "bool " << sc.name << "=" << (sc.value.u ? "true" : "false") << ";\n";
797 break;
798 default:
800 break;
801 }
802 }
803
804 {
805 StringRefNull active_info = "";
807 if (assign_if_different(active_info, sv.info_name)) {
808 ss << "\n#define CREATE_INFO_RES_SHARED_VARS_" << sv.info_name << " \\\n";
809 }
810 ss << "shared " << to_string(sv.type) << " " << sv.name << ";";
811 ss << " \\\n";
812 }
813 ss << "\n";
814 }
815 {
816 StringRefNull active_info = "";
817 for (const ShaderCreateInfo::Resource &res : info.pass_resources_) {
818 print_resource(ss, vk_interface, res, info, "PASS", active_info);
819 }
820 ss << "\n";
821 }
822 {
823 StringRefNull active_info = "";
824 for (const ShaderCreateInfo::Resource &res : info.batch_resources_) {
825 print_resource(ss, vk_interface, res, info, "BATCH", active_info);
826 }
827 ss << "\n";
828 }
829 {
830 StringRefNull active_info = "";
831 for (const ShaderCreateInfo::Resource &res : info.geometry_resources_) {
832 print_resource(ss, vk_interface, res, info, "GEOMETRY", active_info);
833 }
834 ss << "\n";
835 }
836 /* Push constants. */
837 const VKPushConstants::Layout &push_constants_layout = vk_interface.push_constants_layout_get();
838 const VKPushConstants::StorageType push_constants_storage =
839 push_constants_layout.storage_type_get();
840 if (push_constants_storage != VKPushConstants::StorageType::NONE) {
841 /* Push Constants. */
842 if (push_constants_storage == VKPushConstants::StorageType::PUSH_CONSTANTS) {
843 ss << "layout(push_constant, std430) uniform constants\n";
844 }
845 else if (push_constants_storage == VKPushConstants::StorageType::BUFFER) {
846 ss << "layout(binding = " << push_constants_layout.descriptor_set_location_get()
847 << ", std140) uniform constants\n";
848 }
849 ss << "{\n";
851 ss << " " << to_string(uniform.type) << " " << uniform.name;
852 if (uniform.array_size > 0) {
853 ss << "[" << uniform.array_size << "]";
854 }
855 ss << ";\n";
856 }
857 ss << "};\n";
858 }
859
860 ss << "\n";
861 return ss.str();
862}
863
865{
866 std::stringstream ss;
867 std::string pre_main;
868 std::string post_main;
869
870 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
871 bool uses_clip_distances = flag_is_set(info.builtins_combined(), BuiltinBits::CLIP_DISTANCES);
872
873 /* Inputs. */
874 for (const ShaderCreateInfo::VertIn &attr : info.vertex_inputs_) {
875 ss << "layout(location = " << attr.index << ") ";
876 ss << "in " << to_string(attr.type) << " " << attr.name << ";\n";
877 }
878 /* Interfaces. */
879 int location = 0;
880 for (const StageInterfaceInfo *iface : info.vertex_out_interfaces_) {
881 print_interface(ss, "out", *iface, location);
882 }
883
884 if (uses_clip_distances && !extensions.shader_clip_distance) {
885 ss << "layout(location=" << location << ") out float vk_ClipDistance[6];\n";
886 pre_main += "#define gl_ClipDistance vk_ClipDistance\n";
887 location += 6;
888 }
889
890 const bool has_geometry_stage = do_geometry_shader_injection(&info) ||
892 const bool do_layer_output = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
893 const bool do_viewport_output = flag_is_set(info.builtins_combined(),
895 if (has_geometry_stage) {
896 if (do_layer_output) {
897 ss << "layout(location=" << (location++) << ") out int gpu_Layer;\n ";
898 }
899 if (do_viewport_output) {
900 ss << "layout(location=" << (location++) << ") out int gpu_ViewportIndex;\n";
901 }
902 }
903 else {
904 if (do_layer_output) {
905 ss << "#define gpu_Layer gl_Layer\n";
906 }
907 if (do_viewport_output) {
908 ss << "#define gpu_ViewportIndex gl_ViewportIndex\n";
909 }
910 }
911 ss << "\n";
912
913 /* Retarget depth from -1..1 to 0..1. This will be done by geometry stage, when geometry shaders
914 * are used. */
915 const bool retarget_depth = !has_geometry_stage;
916 if (retarget_depth) {
917 post_main += "gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
918 }
919
920 if (!pre_main.empty() || !post_main.empty()) {
921 ss << main_function_wrapper(pre_main, post_main);
922 }
923 return ss.str();
924}
925
926static Type to_component_type(const Type &type)
927{
928 switch (type) {
929 case Type::float_t:
930 case Type::float2_t:
931 case Type::float3_t:
932 case Type::float4_t:
933 case Type::float3x3_t:
934 case Type::float4x4_t:
935 return Type::float_t;
936 case Type::uint_t:
937 case Type::uint2_t:
938 case Type::uint3_t:
939 case Type::uint4_t:
940 return Type::uint_t;
941 case Type::int_t:
942 case Type::int2_t:
943 case Type::int3_t:
944 case Type::int4_t:
945 case Type::bool_t:
946 return Type::int_t;
947 /* Alias special types. */
948 case Type::uchar_t:
949 case Type::uchar2_t:
950 case Type::uchar3_t:
951 case Type::uchar4_t:
952 case Type::ushort_t:
953 case Type::ushort2_t:
954 case Type::ushort3_t:
955 case Type::ushort4_t:
956 return Type::uint_t;
957 case Type::char_t:
958 case Type::char2_t:
959 case Type::char3_t:
960 case Type::char4_t:
961 case Type::short_t:
962 case Type::short2_t:
963 case Type::short3_t:
964 case Type::short4_t:
965 return Type::int_t;
967 return Type::float_t;
968 }
970 return Type::float_t;
971}
972
974{
975 std::stringstream ss;
976 std::string pre_main;
977 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
978 bool uses_clip_distances = flag_is_set(info.builtins_combined(), BuiltinBits::CLIP_DISTANCES);
979
980 if (uses_clip_distances && !extensions.shader_clip_distance) {
981 pre_main += " for (int i = 0; i < 6; i++) {\n";
982 pre_main += " if (vk_ClipDistance[i] < 0.0) { discard; }\n";
983 pre_main += " }\n";
984 }
985
986 /* Interfaces. */
990 int location = 0;
991 for (const StageInterfaceInfo *iface : in_interfaces) {
992 print_interface(ss, "in", *iface, location);
993 }
994 if (uses_clip_distances && !extensions.shader_clip_distance) {
995 ss << "layout(location=" << location << ") in float vk_ClipDistance[6];\n";
996 location += 6;
997 }
998
999 if (flag_is_set(info.builtins_combined(), BuiltinBits::LAYER)) {
1000 ss << "#define gpu_Layer gl_Layer\n";
1001 }
1002 if (flag_is_set(info.builtins_combined(), BuiltinBits::VIEWPORT_INDEX)) {
1003 ss << "#define gpu_ViewportIndex gl_ViewportIndex\n";
1004 }
1005
1006 if (!extensions.fragment_shader_barycentric &&
1008 {
1009 ss << "layout(location=" << (location++) << ") smooth in vec3 gpu_BaryCoord;\n";
1010 ss << "layout(location=" << (location++) << ") noperspective in vec3 gpu_BaryCoordNoPersp;\n";
1011 }
1012
1013 if (info.early_fragment_test_) {
1014 ss << "layout(early_fragment_tests) in;\n";
1015 }
1016 const bool use_gl_frag_depth = info.depth_write_ != DepthWrite::UNCHANGED &&
1017 info.fragment_source_.find("gl_FragDepth") != std::string::npos;
1018 if (use_gl_frag_depth) {
1019 ss << "layout(" << to_string(info.depth_write_) << ") out float gl_FragDepth;\n";
1020 }
1021
1022 /* Sub-pass Inputs. */
1023 const VKShaderInterface &interface = interface_get();
1024 const bool use_local_read = extensions.dynamic_rendering_local_read;
1025
1026 if (use_local_read) {
1027 uint32_t subpass_input_binding_index = 0;
1028 for (const ShaderCreateInfo::SubpassIn &input : info.subpass_inputs_) {
1029 std::string input_attachment_name = "gpu_input_attachment_";
1030 input_attachment_name += std::to_string(input.index);
1031
1032 /* Declare global for input. */
1033 ss << to_string(input.type) << " " << input.name << ";\n";
1034
1035 Type component_type = to_component_type(input.type);
1036 char typePrefix;
1037 switch (component_type) {
1038 case Type::int_t:
1039 typePrefix = 'i';
1040 break;
1041 case Type::uint_t:
1042 typePrefix = 'u';
1043 break;
1044 default:
1045 typePrefix = ' ';
1046 break;
1047 }
1048 ss << "layout(input_attachment_index = " << (input.index)
1049 << ", binding = " << (subpass_input_binding_index++) << ") uniform " << typePrefix
1050 << "subpassInput " << input_attachment_name << "; \n";
1051
1052 std::stringstream ss_pre;
1053 static const std::string swizzle = "xyzw";
1054 /* Populate the global before main using subpassLoad. */
1055 ss_pre << " " << input.name << " = " << input.type << "( subpassLoad("
1056 << input_attachment_name << ")." << swizzle.substr(0, to_component_count(input.type))
1057 << " ); \n";
1058
1059 pre_main += ss_pre.str();
1060 }
1061 }
1062 else {
1063 for (const ShaderCreateInfo::SubpassIn &input : info.subpass_inputs_) {
1064 std::string image_name = "gpu_subpass_img_";
1065 image_name += std::to_string(input.index);
1066
1067 /* Declare global for input. */
1068 ss << to_string(input.type) << " " << input.name << ";\n";
1069
1070 /* IMPORTANT: We assume that the frame-buffer will be layered or not based on the layer
1071 * built-in flag. */
1072 bool is_layered_fb = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
1073 bool is_layered_input = ELEM(
1074 input.img_type, ImageType::Uint2DArray, ImageType::Int2DArray, ImageType::Float2DArray);
1075 /* Declare image. */
1076 using Resource = ShaderCreateInfo::Resource;
1077 /* NOTE(fclem): Using the attachment index as resource index might be problematic as it might
1078 * collide with other resources. */
1079 Resource res(info, Resource::BindType::SAMPLER, input.index, nullptr);
1080 res.sampler.type = input.img_type;
1081 res.sampler.name = image_name;
1082 print_resource(ss, interface, res, info);
1083
1084 char swizzle[] = "xyzw";
1085 swizzle[to_component_count(input.type)] = '\0';
1086
1087 std::string texel_co = (is_layered_input) ?
1088 ((is_layered_fb) ? "ivec3(gl_FragCoord.xy, gpu_Layer)" :
1089 /* This should fetch the attached layer.
1090 * But this is not simple to set. For now
1091 * assume it is always the first layer. */
1092 "ivec3(gl_FragCoord.xy, 0)") :
1093 "ivec2(gl_FragCoord.xy)";
1094
1095 std::stringstream ss_pre;
1096 /* Populate the global before main using imageLoad. */
1097 ss_pre << " " << input.name << " = texelFetch(" << image_name << ", " << texel_co << ", 0)."
1098 << swizzle << ";\n";
1099
1100 pre_main += ss_pre.str();
1101 }
1102 }
1103
1104 /* Outputs. */
1106 const int location = output.index;
1107 ss << "layout(location = " << location;
1108 switch (output.blend) {
1109 case DualBlend::SRC_0:
1110 ss << ", index = 0";
1111 break;
1112 case DualBlend::SRC_1:
1113 ss << ", index = 1";
1114 break;
1115 default:
1116 break;
1117 }
1118 ss << ") ";
1119 ss << "out " << to_string(output.type) << " " << output.name << ";\n";
1120 }
1121 ss << "\n";
1122
1123 if (pre_main.empty() == false) {
1124 std::string post_main;
1125 ss << main_function_wrapper(pre_main, post_main);
1126 }
1127 return ss.str();
1128}
1129
1132{
1133 for (auto [iface, cond] : ifaces) {
1134 if (iface->instance_name == name) {
1135 return iface;
1136 }
1137 }
1138 return nullptr;
1139}
1140
1141static void declare_emit_vertex(std::stringstream &ss)
1142{
1143 ss << "void gpu_EmitVertex() {\n";
1144 ss << " gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
1145 ss << " EmitVertex();\n";
1146 ss << "}\n";
1147}
1148
1150{
1151 int max_verts = info.geometry_layout_.max_vertices;
1152 int invocations = info.geometry_layout_.invocations;
1153
1154 std::stringstream ss;
1155 /* Geometry Layout. */
1156 ss << "layout(" << to_string(info.geometry_layout_.primitive_in);
1157 if (invocations != -1) {
1158 ss << ", invocations = " << invocations;
1159 }
1160 ss << ") in;\n";
1161
1162 ss << "layout(" << to_string(info.geometry_layout_.primitive_out)
1163 << ", max_vertices = " << max_verts << ") out;\n";
1164 ss << "\n";
1165 return ss.str();
1166}
1167
1169{
1170 std::stringstream ss;
1171
1172 /* Interfaces. */
1173 int location = 0;
1174 for (const StageInterfaceInfo *iface : info.vertex_out_interfaces_) {
1175 bool has_matching_output_iface = find_interface_by_name(info.geometry_out_interfaces_,
1176 iface->instance_name) != nullptr;
1177 const char *suffix = (has_matching_output_iface) ? "_in[]" : "[]";
1178 print_interface(ss, "in", *iface, location, suffix);
1179 }
1180 ss << "\n";
1181
1182 location = 0;
1183 for (const StageInterfaceInfo *iface : info.geometry_out_interfaces_) {
1184 bool has_matching_input_iface = find_interface_by_name(info.vertex_out_interfaces_,
1185 iface->instance_name) != nullptr;
1186 const char *suffix = (has_matching_input_iface) ? "_out" : "";
1187 print_interface(ss, "out", *iface, location, suffix);
1188 }
1189 ss << "\n";
1190
1192
1193 return ss.str();
1194}
1195
1197{
1198 std::stringstream ss;
1199 /* Compute Layout. */
1200 ss << "layout(";
1201 ss << " local_size_x = " << info.compute_layout_.local_size_x;
1202 ss << ", local_size_y = " << info.compute_layout_.local_size_y;
1203 ss << ", local_size_z = " << info.compute_layout_.local_size_z;
1204 ss << ") in;\n";
1205 ss << "\n";
1206 return ss.str();
1207}
1208
1209/* -------------------------------------------------------------------- */
1213
1214std::string VKShader::workaround_geometry_shader_source_create(
1215 const shader::ShaderCreateInfo &info)
1216{
1217 std::stringstream ss;
1218 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
1219
1220 const bool do_layer_output = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
1221 const bool do_viewport_output = flag_is_set(info.builtins_combined(),
1223 const bool do_barycentric_workaround = !extensions.fragment_shader_barycentric &&
1224 flag_is_set(info.builtins_combined(),
1226 const bool do_clip_distance_workaround = !extensions.shader_clip_distance &&
1227 flag_is_set(info.builtins_combined(),
1229
1230 shader::ShaderCreateInfo info_modified = info;
1231 info_modified.geometry_out_interfaces_ = info_modified.vertex_out_interfaces_;
1237
1238 ss << geometry_layout_declare(info_modified);
1239 ss << geometry_interface_declare(info_modified);
1240 int location = 0;
1241 for (const StageInterfaceInfo *iface : info.vertex_out_interfaces_) {
1242 for (const StageInterfaceInfo::InOut &inout : iface->inouts) {
1243 location += get_location_count(inout.type);
1244 }
1245 }
1246
1247 /* Must match the layout of the vertex stage, which emits the emulated clip distances right
1248 * after the interfaces. Input and output locations are separate spaces, so the pass through
1249 * uses the same location on both sides. */
1250 if (do_clip_distance_workaround) {
1251 ss << "layout(location=" << location << ") in float vk_ClipDistance_in[][6];\n";
1252 ss << "layout(location=" << location << ") out float vk_ClipDistance[6];\n";
1253 location += 6;
1254 }
1255
1256 int location_in = location;
1257 int location_out = location;
1258 if (do_layer_output) {
1259 ss << "layout(location=" << (location_in++) << ") in int gpu_Layer[];\n";
1260 }
1261 if (do_viewport_output) {
1262 ss << "layout(location=" << (location_in++) << ") in int gpu_ViewportIndex[];\n";
1263 }
1264 if (do_barycentric_workaround) {
1265 ss << "layout(location=" << (location_out++) << ") smooth out vec3 gpu_BaryCoord;\n";
1266 ss << "layout(location=" << (location_out++)
1267 << ") noperspective out vec3 gpu_BaryCoordNoPersp;\n";
1268 }
1269 ss << "\n";
1270
1271 ss << "void main()\n";
1272 ss << "{\n";
1273 for (int i : IndexRange(3)) {
1274 for (const StageInterfaceInfo *iface : info_modified.vertex_out_interfaces_) {
1275 bool has_matching_output_iface = find_interface_by_name(
1276 info_modified.geometry_out_interfaces_,
1277 iface->instance_name) != nullptr;
1278 const char *out_suffix = (has_matching_output_iface) ? "_out" : "";
1279 const char *in_suffix = (has_matching_output_iface) ? "_in" : "";
1280 if (iface->instance_name.is_empty()) {
1281 for (const StageInterfaceInfo::InOut &inout : iface->inouts) {
1282 ss << inout.name << out_suffix << " = " << inout.name << in_suffix << "[" << i << "];\n";
1283 }
1284 }
1285 else {
1286 for (const StageInterfaceInfo::InOut &inout : iface->inouts) {
1287 ss << " " << iface->instance_name << out_suffix << "." << inout.name;
1288 ss << " = " << iface->instance_name << in_suffix << "[" << i << "]." << inout.name
1289 << ";\n";
1290 }
1291 }
1292 }
1293 if (do_barycentric_workaround) {
1294 ss << " gpu_BaryCoordNoPersp = gpu_BaryCoord =";
1295 ss << " vec3(" << int(i == 0) << ", " << int(i == 1) << ", " << int(i == 2) << ");\n";
1296 }
1297 if (do_clip_distance_workaround) {
1298 ss << " vk_ClipDistance = vk_ClipDistance_in[" << i << "];\n";
1299 }
1300 ss << " gl_Position = gl_in[" << i << "].gl_Position;\n";
1301 if (do_layer_output) {
1302 ss << " gl_Layer = gpu_Layer[" << i << "];\n";
1303 }
1304 if (do_viewport_output) {
1305 ss << " gl_ViewportIndex = gpu_ViewportIndex[" << i << "];\n";
1306 }
1307 ss << " gpu_EmitVertex();\n";
1308 }
1309 ss << "}\n";
1310 return ss.str();
1311}
1312
1313bool VKShader::do_geometry_shader_injection(const shader::ShaderCreateInfo *info) const
1314{
1315 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
1316 BuiltinBits builtins = info->builtins_combined();
1317 if (!extensions.fragment_shader_barycentric &&
1318 flag_is_set(builtins, BuiltinBits::BARYCENTRIC_COORD))
1319 {
1320 return true;
1321 }
1322 if (flag_is_set(builtins, BuiltinBits::LAYER) ||
1323 flag_is_set(builtins, BuiltinBits::VIEWPORT_INDEX))
1324 {
1325 if (!extensions.shader_viewport_index_layer) {
1326 return true;
1327 }
1328 }
1329 return false;
1330}
1331
1333
1335 const shader::SpecializationConstants &constants_state)
1336{
1337 BLI_assert(is_compute_shader_);
1338 BLI_assert(compute_module.vk_shader_module != VK_NULL_HANDLE);
1339 BLI_assert(vk_pipeline_layout != VK_NULL_HANDLE);
1340
1341 /* Early exit when no specialization constants are used and the vk_pipeline_base_ is already
1342 * valid. This would handle most cases. */
1343 if (constants_state.values.is_empty() && vk_pipeline_base_ != VK_NULL_HANDLE) {
1344 return vk_pipeline_base_;
1345 }
1346
1347 VKComputeInfo compute_info = {};
1348 compute_info.specialization_constants.extend(constants_state.values);
1351
1352 VKDevice &device = VKBackend::get().device;
1353 /* Store result in local variable to ensure thread safety. */
1354 VkPipeline vk_pipeline = device.pipelines.get_or_create_compute_pipeline(
1355 compute_info, is_static_shader_, vk_pipeline_base_, name_get());
1356 if (vk_pipeline_base_ == VK_NULL_HANDLE) {
1357 vk_pipeline_base_ = vk_pipeline;
1358 }
1359 return vk_pipeline;
1360}
1361
1363{
1364 BLI_assert(!is_compute_shader_);
1365
1366 VKDevice &device = VKBackend::get().device;
1367 const VKExtensions &extensions = device.extensions_get();
1368 if (!extensions.vertex_input_dynamic_state) {
1369 return true;
1370 }
1371
1372 /* Viewports and scissors are part of the pipeline and only known when drawing. */
1374 return true;
1375 }
1376
1377 for (const shader::PipelineState &pipeline_state : pipeline_states) {
1378 const VkPrimitiveTopology vk_topology = to_vk_primitive_topology(pipeline_state.primitive_);
1379
1380 VKGraphicsInfo graphics_info = {};
1381 graphics_info.vertex_in.vk_topology = vk_topology;
1382 /* When vertex input dynamic state is enabled the actual vertex input doesn't matter. We use an
1383 * invalid key to ensure that same hash-keys are constructed for compatible graphics infos and
1384 * incorrect usages would still assert.*/
1386
1391 graphics_info.shaders.vk_topology = vk_topology;
1392 graphics_info.shaders.state = pipeline_state.state_;
1393 graphics_info.shaders.viewport_count = pipeline_state.viewport_count_;
1394 graphics_info.shaders.specialization_constants.extend(
1395 pipeline_state.specialization_constants_);
1396 graphics_info.shaders.has_depth = pipeline_state.depth_format_ != TextureTargetFormat::Invalid;
1397 graphics_info.shaders.has_stencil = pipeline_state.stencil_format_ !=
1399 graphics_info.shaders.max_input_attachment_index = max_input_attachment_index_;
1400
1401 /* Disable pipeline features that are dynamic to increase cache hits. */
1402 if (extensions.extended_dynamic_state) {
1403 graphics_info.shaders.state.invert_facing = false;
1404 }
1405
1407 pipeline_state.depth_format_);
1409 pipeline_state.stencil_format_);
1410 for (const TextureTargetFormat color_format : pipeline_state.color_formats_) {
1411 graphics_info.fragment_out.color_attachment_formats.append(to_vk_format(color_format));
1412 }
1413 graphics_info.fragment_out.state = pipeline_state.state_;
1414 graphics_info.shaders.color_attachment_count = uint32_t(
1416
1417 bool pipeline_created = false;
1418 VkPipeline vk_pipeline = device.pipelines.get_or_create_graphics_pipeline(
1419 graphics_info, is_static_shader_, vk_pipeline_base_, name_get(), pipeline_created);
1420 has_precompiled_pipelines_ = true;
1421 UNUSED_VARS_NDEBUG(pipeline_created);
1422 if (vk_pipeline == VK_NULL_HANDLE) {
1423 return false;
1424 }
1425 if (vk_pipeline_base_ == VK_NULL_HANDLE) {
1426 vk_pipeline_base_ = vk_pipeline;
1427 }
1428 }
1429
1430 return true;
1431}
1432
1434 GPUPrimType primitive,
1435 VKVertexInputDescriptionPool::Key vertex_input_description_key,
1436 VKStateManager &state_manager,
1437 const VKFrameBuffer &framebuffer,
1438 SpecializationConstants &constants_state)
1439{
1440 VKDevice &device = VKBackend::get().device;
1441 const VKExtensions &extensions = device.extensions_get();
1442
1443 BLI_assert(!is_compute_shader_);
1445 primitive != GPU_PRIM_POINTS || interface_get().is_point_shader(),
1446 "GPU_PRIM_POINTS is used with a shader that doesn't set point size before "
1447 "drawing fragments. Calling code should be adapted to use a shader that sets the "
1448 "gl_PointSize before entering the fragment stage. For example `GPU_SHADER_3D_POINT_*`.");
1449
1450 const VkPrimitiveTopology vk_topology = to_vk_primitive_topology(primitive);
1451 const VkFormat depth_attachment_format = framebuffer.depth_attachment_format_get();
1452 const VkFormat stencil_attachment_format = framebuffer.stencil_attachment_format_get();
1453
1454 VKGraphicsInfo graphics_info = {};
1455 graphics_info.vertex_in.vk_topology = vk_topology;
1456 /* When vertex input dynamic state is enabled the actual vertex input doesn't matter. We use an
1457 * invalid key to ensure that same hash-keys are constructed for compatible graphics infos and
1458 * incorrect usages would still assert.*/
1459 graphics_info.vertex_in.vertex_input_key = extensions.vertex_input_dynamic_state ?
1461 vertex_input_description_key;
1462
1467 graphics_info.shaders.vk_topology = vk_topology;
1468 graphics_info.shaders.state = state_manager.state;
1469 graphics_info.shaders.viewport_count = framebuffer.viewport_size();
1471 framebuffer.vk_viewports_append(graphics_info.shaders.viewports);
1472 framebuffer.vk_render_areas_append(graphics_info.shaders.scissors);
1473 }
1474 graphics_info.shaders.specialization_constants.extend(constants_state.values);
1475 graphics_info.shaders.has_depth = depth_attachment_format != VK_FORMAT_UNDEFINED;
1476 graphics_info.shaders.has_stencil = stencil_attachment_format != VK_FORMAT_UNDEFINED;
1477 graphics_info.shaders.max_input_attachment_index = max_input_attachment_index_;
1478 /* Cleanup state to increase cache hits. */
1479 if (!graphics_info.shaders.has_stencil || state_manager.state.stencil_test == GPU_STENCIL_NONE) {
1480 graphics_info.shaders.state.stencil_test = GPU_STENCIL_NONE;
1481 graphics_info.shaders.state.stencil_op = GPU_STENCIL_OP_NONE;
1482 }
1483 if (extensions.extended_dynamic_state) {
1484 graphics_info.shaders.state.invert_facing = false;
1485 }
1486
1487 graphics_info.fragment_out.depth_attachment_format = depth_attachment_format;
1488 graphics_info.fragment_out.stencil_attachment_format = stencil_attachment_format;
1489 graphics_info.fragment_out.color_attachment_formats.extend(
1490 framebuffer.color_attachment_formats_get());
1491 graphics_info.fragment_out.state = graphics_info.shaders.state;
1492 graphics_info.shaders.color_attachment_count = uint32_t(
1494
1495 /* Pipelines with baked in viewports and scissors are unlikely to be reused, don't store them
1496 * in the static pipeline cache that is written to disk. */
1497 const bool use_static_cache = is_static_shader_ &&
1499 bool pipeline_created = false;
1500 VkPipeline vk_pipeline = device.pipelines.get_or_create_graphics_pipeline(
1501 graphics_info, use_static_cache, vk_pipeline_base_, name_get(), pipeline_created);
1502
1503 UNUSED_VARS_NDEBUG(pipeline_created);
1504#ifndef NDEBUG
1505 if (has_precompiled_pipelines_ && pipeline_created) {
1506 /* Sanity check: Used to detect mismatches between shader create info states and actual used
1507 * pipeline states.
1508 *
1509 * NOTE: This could trigger false positives where input attributes are just different between
1510 * objects that require a new pipeline state. */
1511 CLOG_INFO(&LOG,
1512 "Pipeline states were compiled for `%s`, however a pipeline state triggered a new "
1513 "pipeline compilation.",
1514 name_get().c_str());
1515 if (G.debug & G_DEBUG_GPU) {
1516 CLOG_DEBUG(
1517 &LOG, "Missing pipeline state:\n%s", graphics_info.pipeline_info_source().c_str());
1518 }
1519 const VKContext &context = *VKContext::get();
1520 BLI_assert(!context.debug_pipeline_creation);
1521 }
1522#endif
1523
1524 if (vk_pipeline_base_ == VK_NULL_HANDLE) {
1525 vk_pipeline_base_ = vk_pipeline;
1526 }
1527 return vk_pipeline;
1528}
1529
1531{
1532 BLI_assert_msg(interface != nullptr,
1533 "Interface can be accessed after the VKShader has been initialized "
1534 "`VKShader::init`");
1535 return *static_cast<const VKShaderInterface *>(interface);
1536}
1537
1538} // namespace gpu
1539} // namespace blender
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define output
constexpr IndexRange()=default
constexpr bool is_empty() const
constexpr int64_t find(char c, int64_t pos=0) const
std::unique_ptr< const shader::SpecializationConstants > constants
ShaderInterface * interface
StringRefNull name_get() const
void print_log(Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
Shader(const char *name)
static VKBackend & get()
static VKContext * get()
VkDescriptorSetLayout get_or_create(const VKDescriptorSetLayoutInfo &info, bool &r_created, bool &r_needed)
VKPipelinePool pipelines
Definition vk_device.hh:274
VKDescriptorSetLayouts & descriptor_set_layouts_get()
Definition vk_device.hh:353
VkDevice vk_handle() const
Definition vk_device.hh:338
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
VolkDeviceTable functions
Definition vk_device.hh:280
std::string glsl_compute_patch_get(bool use_ray_query) const
Definition vk_device.cc:360
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:394
std::string glsl_vertex_patch_get(bool use_ray_query) const
Definition vk_device.cc:337
std::string glsl_geometry_patch_get() const
Definition vk_device.cc:345
std::string glsl_fragment_patch_get(bool use_ray_query) const
Definition vk_device.cc:352
static VKDiscardPool & discard_pool_get()
void discard_pipeline_layout(VkPipelineLayout vk_pipeline_layout)
VkFormat stencil_attachment_format_get() const
VkFormat depth_attachment_format_get() const
void vk_viewports_append(Vector< VkViewport, GPU_MAX_VIEWPORTS > &r_viewports) const
void vk_render_areas_append(Vector< VkRect2D, GPU_MAX_VIEWPORTS > &r_render_areas) const
Span< VkFormat > color_attachment_formats_get() const
Get color attachment formats in used by the framebuffer.
VkPipeline get_or_create_compute_pipeline(const VKComputeInfo &compute_info, bool is_static_shader, VkPipeline vk_pipeline_base, StringRefNull name)
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)
void push_constant_set(int32_t location, int32_t comp_len, int32_t array_size, const T *input_data)
static bool compile_module(VKShader &shader, shaderc_shader_kind stage, VKShaderModule &shader_module)
const VKPushConstants::Layout & push_constants_layout_get() const
VKDescriptorSet::Location descriptor_set_location(const shader::ShaderCreateInfo::Resource &resource) const
const VKDescriptorSetLayoutInfo & descriptor_set_layout_info_get() const
void init(const shader::ShaderCreateInfo &info)
shaderc::SpvCompilationResult compilation_result
void finalize(StringRefNull name)
void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
Definition vk_shader.cc:582
void unbind() override
Definition vk_shader.cc:740
VKShaderModule compute_module
Definition vk_shader.hh:67
std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition vk_shader.cc:973
VkPipeline ensure_and_get_compute_pipeline(const shader::SpecializationConstants &constants_state)
VKShaderModule fragment_module
Definition vk_shader.hh:66
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
void warm_cache(int limit) override
Definition vk_shader.cc:594
VKPushConstants push_constants
Definition vk_shader.hh:70
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
Definition vk_shader.cc:752
void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
Definition vk_shader.cc:576
std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override
VkPipelineLayout vk_pipeline_layout
Definition vk_shader.hh:69
bool ensure_graphics_pipelines(Span< shader::PipelineState > pipeline_states)
void vertex_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
Definition vk_shader.cc:570
void uniform_int(int location, int comp_len, int array_size, const int *data) override
Definition vk_shader.cc:747
void compute_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
Definition vk_shader.cc:588
VKShader(const char *name)
Definition vk_shader.cc:505
VKShaderModule vertex_module
Definition vk_shader.hh:64
void bind(const shader::SpecializationConstants *constants_state) override
Definition vk_shader.cc:730
VKShaderModule geometry_module
Definition vk_shader.hh:65
std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override
bool finalize_post(Span< shader::PipelineState > pipeline_states)
Definition vk_shader.cc:628
void uniform_float(int location, int comp_len, int array_size, const float *data) override
Definition vk_shader.cc:742
const VKShaderInterface & interface_get() const
VkPipeline ensure_and_get_graphics_pipeline(GPUPrimType primitive, VKVertexInputDescriptionPool::Key vertex_input_description_key, VKStateManager &state_manager, const VKFrameBuffer &framebuffer, shader::SpecializationConstants &constants_state)
void init(const shader::ShaderCreateInfo &info, bool is_codegen_only) override
Definition vk_shader.cc:510
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
Definition vk_shader.cc:599
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition vk_shader.cc:864
#define uniform(slot)
#define SOURCES_INDEX_VERSION
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
BLI_INLINE int to_component_count(const Type &type)
static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type)
Definition vk_shader.cc:211
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
int get_location_count(const Type &type)
Definition vk_shader.cc:421
static StageInterfaceInfo * find_interface_by_name(const Span< ShaderCreateInfo::StageInterfaceInfoHandle > ifaces, const StringRefNull name)
static void print_interface_as_attributes(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location, const StringRefNull &suffix)
Definition vk_shader.cc:432
VkFormat to_vk_format(const TextureFormat format)
Definition vk_common.cc:157
static void print_interface(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location, const StringRefNull &suffix="")
Definition vk_shader.cc:467
static void print_resource(std::ostream &os, const VKDescriptorSet::Location location, const ShaderCreateInfo::Resource &res, const ShaderCreateInfo &)
Definition vk_shader.cc:354
static std::string combine_sources(Span< StringRefNull > sources)
Definition vk_shader.cc:500
static std::ostream & print_qualifier(std::ostream &os, const Qualifier &qualifiers)
Definition vk_shader.cc:340
static Type to_component_type(const Type &type)
Definition vk_shader.cc:926
static void print_interface_as_struct(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location, const StringRefNull &suffix)
Definition vk_shader.cc:445
static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
Definition vk_shader.cc:482
VkPrimitiveTopology to_vk_primitive_topology(const GPUPrimType prim_type)
Definition vk_common.cc:666
static StringRefNull to_component_type(ImageType type)
static void declare_emit_vertex(std::stringstream &ss)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GPU_STENCIL_OP_NONE
Definition GPU_state.hh:149
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
MINLINE uint max_uu(uint a, uint b)
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
@ GPU_STENCIL_NONE
Definition GPU_state.hh:142
static CLG_LogRef LOG
Definition logging.cpp:12
Vector< shader::SpecializationConstant::Value > specialization_constants
VkPipelineLayout vk_pipeline_layout
Vector< VkFormat, GPU_FB_MAX_COLOR_ATTACHMENT > color_attachment_formats
Vector< VkRect2D, GPU_MAX_VIEWPORTS > scissors
Vector< VkViewport, GPU_MAX_VIEWPORTS > viewports
Vector< shader::SpecializationConstant::Value > specialization_constants
VKVertexInputDescriptionPool::Key vertex_input_key
std::string pipeline_info_source() const
VKDescriptorSet::Location descriptor_set_location_get() const
Description of a graphical pipeline to pre-compile during shader creation.
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & geometry_layout(PrimitiveIn prim_in, PrimitiveOut prim_out, int max_vertices, int invocations=-1)
Vector< SpecializationConstant, 0 > specialization_constants_
Vector< CompilationConstant, 0 > compilation_constants_
Vector< StageInterfaceInfoHandle, 0 > geometry_out_interfaces_
Vector< StageInterfaceInfoHandle, 0 > vertex_out_interfaces_
Vector< SpecializationConstant::Value, 8 > values
int size() const
Definition vector.hh:130
#define NOT_YET_IMPLEMENTED
Definition vk_common.hh:196