Blender V5.3
gpu_shader_create_info.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_map.hh"
12#include "BLI_set.hh"
13#include "BLI_string_ref.hh"
14
15#include "BKE_global.hh"
16
17#include "GPU_capabilities.hh"
18#include "GPU_context.hh"
19#include "GPU_platform.hh"
20#include "GPU_shader.hh"
21
25
26#undef GPU_SHADER_NAMED_INTERFACE_INFO
27#undef GPU_SHADER_INTERFACE_INFO
28#undef GPU_SHADER_CREATE_INFO
29#undef GPU_SHADER_NAMED_INTERFACE_END
30#undef GPU_SHADER_INTERFACE_END
31#undef GPU_SHADER_CREATE_END
32
33namespace blender {
34
35namespace gpu::shader {
36
39
42
43/* -------------------------------------------------------------------- */
47
49{
50 if (iface.instance_name.is_empty()) {
51 return true;
52 }
53
54 bool use_flat = false;
55 bool use_smooth = false;
56 bool use_noperspective = false;
57 for (const StageInterfaceInfo::InOut &attr : iface.inouts) {
58 switch (attr.interp) {
60 use_flat = true;
61 break;
63 use_smooth = true;
64 break;
66 use_noperspective = true;
67 break;
68 }
69 }
70 int num_used_interpolation_types = (use_flat ? 1 : 0) + (use_smooth ? 1 : 0) +
71 (use_noperspective ? 1 : 0);
72
73#if 0
74 if (num_used_interpolation_types > 1) {
75 std::cout << "'" << iface.name << "' uses multiple interpolation types\n";
76 }
77#endif
78
79 return num_used_interpolation_types <= 1;
80}
81
83{
84 /* Vulkan doesn't support setting an interpolation mode per attribute in a struct. */
85 for (const StageInterfaceInfo *iface : vertex_out_interfaces_) {
86 if (!is_vulkan_compatible_interface(*iface)) {
87 return false;
88 }
89 }
90 for (const StageInterfaceInfo *iface : geometry_out_interfaces_) {
91 if (!is_vulkan_compatible_interface(*iface)) {
92 return false;
93 }
94 }
95
96 return true;
97}
98
100
102{
103 bool last_char_is_underscore = false;
104 /* Escape the shader name to be able to use it inside an identifier. */
105 for (char &c : name_) {
106 if (!std::isalnum(c)) {
107 c = '_';
108 }
109 if (c == '_' && last_char_is_underscore) {
110 /* Avoid GLSL warning about double underscore being reserved. */
111 c = 'w';
112 }
113 last_char_is_underscore = c == '_';
114 }
115}
116
118{
119 std::string defines;
120 defines += "#define CREATE_INFO_" + name_ + "\n";
121 for (const auto &additional_info : additional_infos_) {
122 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(
124
125 if (additional_info.conditions.evaluate(constants)) {
126 defines += info.resource_guard_defines(constants);
127 }
128 }
129 return defines;
130}
131
133{
134 std::string defines;
135 for (const StageInterfaceInfo *interface : this->vertex_out_interfaces_) {
136 defines += "#define IFACE_INFO_" + interface->name + "\n";
137 }
138 return defines;
139}
140
141void ShaderCreateInfo::finalize(const bool recursive)
142{
143 if (finalized_) {
144 return;
145 }
146 finalized_ = true;
147
148 auto deps_merged = std::make_unique<Set<StringRefNull>>();
149
151
152 for (const auto &additional_info : additional_infos_) {
153
154 /* Fetch create info. */
155 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(
157
158 if (recursive) {
159 const_cast<ShaderCreateInfo &>(info).finalize(recursive);
160 }
161 else {
163 }
164
166
167 /* NOTE: EEVEE Materials can result in nested includes. To avoid duplicate
168 * shader resources, we need to avoid inserting duplicates.
169 * TODO: Optimize create info preparation to include each individual "additional_info"
170 * only a single time. */
171 vertex_inputs_.extend_non_duplicates(info.vertex_inputs_);
172 fragment_outputs_.extend_non_duplicates(info.fragment_outputs_);
173 vertex_out_interfaces_.extend_non_duplicates(info.vertex_out_interfaces_);
174 geometry_out_interfaces_.extend_non_duplicates(info.geometry_out_interfaces_);
175 subpass_inputs_.extend_non_duplicates(info.subpass_inputs_);
176 specialization_constants_.extend_non_duplicates(info.specialization_constants_);
177 compilation_constants_.extend_non_duplicates(info.compilation_constants_);
178
180
182
183 /* Insert with duplicate check. */
184 defines_.extend_non_duplicates(info.defines_);
185 typedef_sources_.extend_non_duplicates(info.typedef_sources_);
186
187 for (const auto &res : info.push_constants_) {
189 }
190 for (const auto &res : info.pass_resources_) {
192 }
193 for (const auto &res : info.batch_resources_) {
195 }
196 for (const auto &res : info.geometry_resources_) {
198 }
199
200 /* API-specific parameters.
201 * We will only copy API-specific parameters if they are otherwise unassigned. */
202#ifdef WITH_METAL_BACKEND
203 if (mtl_max_threads_per_threadgroup_ == 0) {
204 mtl_max_threads_per_threadgroup_ = info.mtl_max_threads_per_threadgroup_;
205 }
206#endif
207
208 if (info.early_fragment_test_) {
211 }
212 /* Modify depth write if has been changed from default.
213 * `UNCHANGED` implies gl_FragDepth is not used at all. */
216 }
217
218 /* Inherit builtin bits from additional info. */
219 builtins_.extend(info.builtins_);
220
221 /* TODO(fclem): We need to reintroduce this check before compiling.
222 * The issue is that the new SRT paradigm allows for conflicting resources if they are not
223 * defined at the same time (using compilation constants). */
224 // validate_merge(info);
225
226 if (!deps_merged->add(info.name_)) {
227 assert_no_overlap(info, false, "additional info already merged via another info");
228 }
229
230 if (info.compute_layout_.local_size_x != -1) {
232 info, compute_layout_.local_size_x == -1, "Compute layout already defined");
234 }
235
236 if (!info.vertex_source_.is_empty()) {
237 assert_no_overlap(info, vertex_source_.is_empty(), "Vertex source already existing");
239 }
240 if (!info.geometry_source_.is_empty()) {
241 assert_no_overlap(info, geometry_source_.is_empty(), "Geometry source already existing");
244 }
245 if (!info.fragment_source_.is_empty()) {
246 assert_no_overlap(info, fragment_source_.is_empty(), "Fragment source already existing");
248 }
249 if (!info.compute_source_.is_empty()) {
250 assert_no_overlap(info, compute_source_.is_empty(), "Compute source already existing");
252 }
253
254 if (info.vertex_entry_fn_ != "main") {
255 assert_no_overlap(info, vertex_entry_fn_ == "main", "Vertex function already existing");
257 }
258 if (info.geometry_entry_fn_ != "main") {
259 assert_no_overlap(info, geometry_entry_fn_ == "main", "Geometry function already existing");
261 }
262 if (info.fragment_entry_fn_ != "main") {
263 assert_no_overlap(info, fragment_entry_fn_ == "main", "Fragment function already existing");
265 }
266 if (info.compute_entry_fn_ != "main") {
267 assert_no_overlap(info, compute_entry_fn_ == "main", "Compute function already existing");
269 }
270 }
271
272 if (!geometry_source_.is_empty() && flag_is_set(this->builtins_combined(), BuiltinBits::LAYER)) {
273 std::cout << name_
274 << ": Validation failed. BuiltinBits::LAYER shouldn't be used with geometry shaders."
275 << std::endl;
276 BLI_assert(0);
277 }
278}
279
282 Span<ConditionFn> additional_conditions) const
283{
284 res_copy.conditions.extend(additional_conditions);
286 resource_vector.append(res_copy);
287};
288
291 Span<ConditionFn> additional_conditions) const
292{
293 res_copy.conditions.extend(additional_conditions);
295 resource_vector.append(res_copy);
296};
297
299 const bool test,
300 const StringRefNull error) const
301{
302 if (!test) {
303 std::cout << name_ << ": Validation failed while merging " << info.name_ << " : ";
304 std::cout << error << std::endl;
305 BLI_assert(0);
306 }
307}
308
310 int &images,
311 int &samplers,
312 int &ubos,
313 int &ssbos,
314 int &acceleration_structures) const
315{
316 switch (res.bind_type) {
318 res.slot = ubos++;
319 break;
321 res.slot = ssbos++;
322 break;
324 res.slot = samplers++;
325 break;
327 res.slot = images++;
328 break;
330 res.slot = acceleration_structures++;
331 break;
332 }
333}
334
336{
337 std::string error;
338
339 /* At least a vertex shader and a fragment shader are required, or only a compute shader. */
340 if (this->compute_source_.is_empty()) {
341 if (this->vertex_source_.is_empty()) {
342 error += "Missing vertex shader in " + this->name_ + ".\n";
343 }
344 if (this->fragment_source_.is_empty()) {
345 error += "Missing fragment shader in " + this->name_ + ".\n";
346 }
347 }
348 else {
349 if (!this->vertex_source_.is_empty()) {
350 error += "Compute shader has vertex_source_ shader attached in " + this->name_ + ".\n";
351 }
352 if (!this->geometry_source_.is_empty()) {
353 error += "Compute shader has geometry_source_ shader attached in " + this->name_ + ".\n";
354 }
355 if (!this->fragment_source_.is_empty()) {
356 error += "Compute shader has fragment_source_ shader attached in " + this->name_ + ".\n";
357 }
358 }
359
360 if (!this->geometry_source_.is_empty()) {
361 if (flag_is_set(this->builtins_combined(), BuiltinBits::BARYCENTRIC_COORD)) {
362 error += "Shader " + this->name_ +
363 " has geometry stage and uses barycentric coordinates. This is not allowed as "
364 "fallback injects a geometry stage.\n";
365 }
366 if (flag_is_set(this->builtins_combined(), BuiltinBits::VIEWPORT_INDEX)) {
367 error += "Shader " + this->name_ +
368 " has geometry stage and uses multi-viewport. This is not allowed as "
369 "fallback injects a geometry stage.\n";
370 }
371 if (flag_is_set(this->builtins_combined(), BuiltinBits::LAYER)) {
372 error += "Shader " + this->name_ +
373 " has geometry stage and uses layer output. This is not allowed as "
374 "fallback injects a geometry stage.\n";
375 }
376 }
377
378 if ((G.debug & G_DEBUG_GPU) == 0) {
379 return error;
380 }
381
382#if 0 /* TODO(fclem): See if this is still needed without any named interface. */
383 if (flag_is_set(this->builtins_combined(),
386 {
387 for (const StageInterfaceInfo *interface : this->vertex_out_interfaces_) {
388 if (interface->instance_name.is_empty()) {
389 error += "Shader " + this->name_ + " uses interface " + interface->name +
390 " that doesn't contain an instance name, but is required for the fallback "
391 "geometry shader.\n";
392 }
393 }
394 }
395#endif
396
397 for (const StageInterfaceInfo *interface : this->vertex_out_interfaces_) {
398 for (const StageInterfaceInfo::InOut &inout : interface->inouts) {
399 if (inout.name.is_array()) {
400 error += "Shader " + this->name_ + " : \"" + interface->name + "." + inout.name + "\":";
401 error += " Array types are not allowed in shader stage interfaces.\n";
402 }
404 error += "Shader " + this->name_ + " : \"" + interface->name + "." + inout.name + "\":";
405 error += " Matrix types are not allowed in shader stage interfaces.\n";
406 }
407 }
408 }
409
410 if (!this->is_vulkan_compatible()) {
411 error += this->name_ +
412 " contains a stage interface using an instance name and mixed interpolation modes. "
413 "This is not compatible with Vulkan and need to be adjusted.\n";
414 }
415
416 /* Validate specialization constants. */
417 for (int i = 0; i < specialization_constants_.size(); i++) {
418 for (int j = i + 1; j < specialization_constants_.size(); j++) {
420 error += this->name_ + " contains two specialization constants with the name: " +
421 std::string(specialization_constants_[i].name);
422 }
423 }
424 }
425
426 /* Validate compilation constants. */
427 for (int i = 0; i < compilation_constants_.size(); i++) {
428 for (int j = i + 1; j < compilation_constants_.size(); j++) {
429 if (compilation_constants_[i].name == compilation_constants_[j].name) {
430 error += this->name_ + " contains two compilation constants with the name: " +
431 std::string(compilation_constants_[i].name);
432 }
433 }
434 }
435
436 /* Validate shared variables. */
437 for (int i = 0; i < shared_variables_.size(); i++) {
438 for (int j = i + 1; j < shared_variables_.size(); j++) {
439 if (shared_variables_[i].name == shared_variables_[j].name) {
440 error += this->name_ + " contains two specialization constants with the name: " +
441 std::string(shared_variables_[i].name);
442 }
443 }
444 }
445
446 /* Check same bind-points usage. */
447 Set<int> images, samplers, ubos, ssbos, acceleration_structures;
448
449 auto register_resource = [&](const Resource &res) -> bool {
450 switch (res.bind_type) {
452 return ubos.add(res.slot);
454 return ssbos.add(res.slot);
456 return samplers.add(res.slot);
458 return images.add(res.slot);
460 return acceleration_structures.add(res.slot);
461 default:
462 return false;
463 }
464 };
465
466 auto print_error_msg = [&](const Resource &res, const Vector<Resource> &resources) {
467 auto print_resource_name = [&](const Resource &res) {
468 switch (res.bind_type) {
470 error += "Uniform Buffer " + res.uniformbuf.name;
471 break;
473 error += "Storage Buffer " + res.storagebuf.name;
474 break;
476 error += "Sampler " + res.sampler.name;
477 break;
479 error += "Image " + res.image.name;
480 break;
482 error += "Acceleration Structure " + res.acceleration_structure.name;
483 break;
484 default:
485 error += "Unknown Type";
486 break;
487 }
488 };
489
490 for (const Resource &_res : resources) {
491 if (&res != &_res && res.bind_type == _res.bind_type && res.slot == _res.slot) {
492 error += name_ + ": Validation failed : Overlapping ";
493 print_resource_name(res);
494 error += " and ";
495 print_resource_name(_res);
496 error += " at binding location " + std::to_string(res.slot) + "\n";
497 }
498 }
499 };
500
501 for (const auto &res : batch_resources_) {
502 if (register_resource(res) == false) {
503 print_error_msg(res, resources_get_all_());
504 }
505 }
506
507 for (const auto &res : pass_resources_) {
508 if (register_resource(res) == false) {
509 print_error_msg(res, resources_get_all_());
510 }
511 }
512
513 for (const auto &res : geometry_resources_) {
514 if (register_resource(res) == false) {
515 print_error_msg(res, resources_get_all_());
516 }
517 }
518
519 return error;
520}
521
523{
524 /* Check same bind-points usage in OGL. */
525 Set<int> images, samplers, ubos, ssbos, acceleration_structures;
526
527 auto register_resource = [&](const Resource &res) -> bool {
528 switch (res.bind_type) {
530 return images.add(res.slot);
532 return samplers.add(res.slot);
534 return ubos.add(res.slot);
536 return ssbos.add(res.slot);
538 return acceleration_structures.add(res.slot);
539 default:
540 return false;
541 }
542 };
543
544 auto print_error_msg = [&](const Resource &res, const Vector<Resource> &resources) {
545 auto print_resource_name = [&](const Resource &res) {
546 switch (res.bind_type) {
548 std::cout << "Uniform Buffer " << res.uniformbuf.name;
549 break;
551 std::cout << "Storage Buffer " << res.storagebuf.name;
552 break;
554 std::cout << "Sampler " << res.sampler.name;
555 break;
557 std::cout << "Image " << res.image.name;
558 break;
560 std::cout << "Acceleration Structure " << res.acceleration_structure.name;
561 break;
562 default:
563 std::cout << "Unknown Type";
564 break;
565 }
566 };
567
568 for (const Resource &_res : resources) {
569 if (&res != &_res && res.bind_type == _res.bind_type && res.slot == _res.slot) {
570 std::cout << name_ << ": Validation failed : Overlapping ";
571 print_resource_name(res);
572 std::cout << " and ";
573 print_resource_name(_res);
574 std::cout << " at (" << res.slot << ") while merging " << other_info.name_ << std::endl;
575 }
576 }
577 };
578
579 for (auto &res : batch_resources_) {
580 if (register_resource(res) == false) {
581 print_error_msg(res, resources_get_all_());
582 }
583 }
584
585 for (auto &res : pass_resources_) {
586 if (register_resource(res) == false) {
587 print_error_msg(res, resources_get_all_());
588 }
589 }
590
591 for (auto &res : geometry_resources_) {
592 if (register_resource(res) == false) {
593 print_error_msg(res, resources_get_all_());
594 }
595 }
596}
597
599{
600 uint32_t attr_bits = 0;
601 for (auto &attr : vertex_inputs_) {
602 if (attr.type == Type::float3x3_t) {
603 std::cout << name_ << ": \"" << attr.name << "\" : float3x3 unsupported as vertex attribute."
604 << std::endl;
605 BLI_assert(0);
606 }
607 if (attr.type == Type::float4x4_t) {
608 std::cout << name_ << ": \"" << attr.name << "\" : float4x4 unsupported as vertex attribute."
609 << std::endl;
610 BLI_assert(0);
611 }
612 if (attr.name.is_array()) {
613 std::cout << name_ << ": \"" << attr.name
614 << "\" : arrays are unsupported as vertex attribute." << std::endl;
615 BLI_assert(0);
616 }
617 if (attr.index >= 16 || attr.index < 0) {
618 std::cout << name_ << ": Invalid index for attribute \"" << attr.name << "\"" << std::endl;
619 BLI_assert(0);
620 }
621 uint32_t attr_new = 0;
622 if (attr.type == Type::float4x4_t) {
623 for (int i = 0; i < 4; i++) {
624 attr_new |= 1 << (attr.index + i);
625 }
626 }
627 else {
628 attr_new |= 1 << attr.index;
629 }
630
631 if ((attr_bits & attr_new) != 0) {
632 std::cout << name_ << ": Attribute \"" << attr.name
633 << "\" overlap one or more index from another attribute."
634 " Note that mat4 takes up 4 indices.";
635 if (other_info) {
636 std::cout << " While merging " << other_info->name_ << std::endl;
637 }
638 std::cout << std::endl;
639 BLI_assert(0);
640 }
641 attr_bits |= attr_new;
642 }
643}
644
645} // namespace gpu::shader
646
647using namespace blender::gpu::shader;
648
649#define GPU_SHADER_NAMED_INTERFACE_INFO(_interface, _inst_name) \
650 StageInterfaceInfo *ptr_##_interface = new StageInterfaceInfo(#_interface, #_inst_name); \
651 StageInterfaceInfo &_interface = *ptr_##_interface; \
652 g_interfaces->add_new(#_interface, ptr_##_interface); \
653 _interface
654
655#define GPU_SHADER_INTERFACE_INFO(_interface) \
656 StageInterfaceInfo *ptr_##_interface = new StageInterfaceInfo(#_interface); \
657 StageInterfaceInfo &_interface = *ptr_##_interface; \
658 g_interfaces->add_new(#_interface, ptr_##_interface); \
659 _interface
660
661#define GPU_SHADER_CREATE_INFO(_info) \
662 ShaderCreateInfo *ptr_##_info = new ShaderCreateInfo(#_info); \
663 ShaderCreateInfo &_info = *ptr_##_info; \
664 g_create_infos->add_new(#_info, ptr_##_info); \
665 _info
666
667#define GPU_SHADER_NAMED_INTERFACE_END(_inst_name) ;
668#define GPU_SHADER_INTERFACE_END() ;
669#define GPU_SHADER_CREATE_END() ;
670
671/* WORKAROUND(@fclem): Undefine constants defined by system headers to fix warnings.
672 * This is caused by the macros copy system made for compatibility of the Create info system before
673 * BSL 5.3. Can be phased out after BSL 5.3 is default everywhere. */
674#undef M_PI
675#undef M_TAU
676#undef M_PI_2
677#undef M_PI_4
678#undef M_SQRT2
679#undef M_SQRT1_2
680#undef M_SQRT3
681#undef M_SQRT1_3
682#undef M_1_PI
683#undef M_E
684#undef M_LOG2E
685#undef M_LOG10E
686#undef M_LN2
687#undef M_LN10
688#undef NAN_FLT
689#undef UNPACK2
690#undef UNPACK3
691#undef UNPACK4
692#undef SHRT_MAX
693#undef INT_MAX
694#undef USHRT_MAX
695#undef UINT_MAX
696#undef FLT_MAX
697#undef FLT_MIN
698#undef FLT_EPSILON
699
700#ifdef _MSC_VER
701/* Disable optimization for this function with MSVC. It does not like the fact
702 * shaders info are declared in the same function (same basic block or not does
703 * not change anything).
704 * Since it is just a function called to register shaders (once),
705 * the fact it's optimized or not does not matter, it's not on any hot
706 * code path. */
707# pragma optimize("", off)
708#endif
709
710/* Split functions to avoid stack overflow on windows. */
712{
713/* Declare, register and construct the infos. */
714#include "glsl_compositor_infos_list.hh"
715}
716
717static void init_draw_infos()
718{
719/* Declare, register and construct the infos. */
720#include "glsl_draw_infos_list.hh"
721}
722
723static void init_eevee_infos()
724{
725/* Declare, register and construct the infos. */
726#include "glsl_eevee_infos_list.hh"
727
729 /* WORKAROUND: Adding a dummy buffer that isn't used fixes a bug inside the Qualcomm driver. */
731 12, Qualifier::read_write, "uint", "dummy_workaround_buf[]");
732 }
733}
734
736{
737/* Declare, register and construct the infos. */
738#include "glsl_select_id_infos_list.hh"
739/* Requires select id infos. */
740#include "glsl_overlay_infos_list.hh"
741}
742
743static void init_image_infos()
744{
745/* Declare, register and construct the infos. */
746#include "glsl_image_infos_list.hh"
747}
748
750{
751/* Declare, register and construct the infos. */
752#include "glsl_gpencil_infos_list.hh"
753}
754
756{
757/* Declare, register and construct the infos. */
758#include "glsl_workbench_infos_list.hh"
759}
760
761static void init_gpu_infos()
762{
763/* Declare, register and construct the infos. */
764#include "glsl_gpu_infos_list.hh"
765}
766
767static void init_ocio_infos()
768{
769/* Declare, register and construct the infos. */
770#include "glsl_ocio_infos_list.hh"
771}
772
773static void init_osd_infos()
774{
775/* Declare, register and construct the infos. */
776#ifdef WITH_OPENSUBDIV
777# include "glsl_osd_infos_list.hh"
778#endif
779}
780
782{
785
796
797 for (ShaderCreateInfo *info : g_create_infos->values()) {
798 info->is_generated_ = false;
799
801 builtins = builtins | gpu_shader_dependency_get_builtins(info->vertex_source_);
802 builtins = builtins | gpu_shader_dependency_get_builtins(info->fragment_source_);
803 builtins = builtins | gpu_shader_dependency_get_builtins(info->geometry_source_);
804 builtins = builtins | gpu_shader_dependency_get_builtins(info->compute_source_);
805
806 info->builtins(builtins);
807
808 if (!info->compute_source_.is_empty()) {
809 info->shared_variables_.extend(
810 gpu_shader_dependency_get_shared_variables(info->compute_source_));
811 }
812
813#if GPU_SHADER_PRINTF_ENABLE
814 const bool is_material_shader = StringRefNull(info->name_).startswith("eevee_surf_");
815 if (flag_is_set(info->builtins_combined(), BuiltinBits::USE_PRINTF) ||
816 (gpu_shader_dependency_force_gpu_print_injection() && is_material_shader))
817 {
818 info->additional_info("gpu_print");
819 }
820#endif
821
822#ifndef NDEBUG
823 /* Automatically amend the create info for ease of use of the debug feature. */
824 if (flag_is_set(info->builtins_combined(), BuiltinBits::USE_DEBUG_DRAW)) {
825 info->additional_info("DebugDraw");
826 }
827#endif
828 }
829
830 for (ShaderCreateInfo *info : g_create_infos->values()) {
831 info->finalize(true);
832 }
833
834 /* TEST */
835 // gpu_shader_create_info_compile(nullptr);
836}
837#ifdef _MSC_VER
838# pragma optimize("", on)
839#endif
840
842{
843 for (auto *value : g_create_infos->values()) {
844 delete value;
845 }
846 delete g_create_infos;
847
848 for (auto *value : g_interfaces->values()) {
849 delete value;
850 }
851 delete g_interfaces;
852}
853
854bool gpu_shader_create_info_compile_all(const char *name_starts_with_filter)
855{
856 using namespace blender::gpu;
857 int success = 0;
858 int skipped_filter = 0;
859 int skipped = 0;
860 int total = 0;
861
863
864 for (ShaderCreateInfo *info : g_create_infos->values()) {
865 info->finalize();
866 if (info->do_static_compilation_) {
867 if (name_starts_with_filter &&
868 !StringRefNull(info->name_).startswith(StringRefNull(name_starts_with_filter)))
869 {
870 skipped_filter++;
871 continue;
872 }
873 if ((info->metal_backend_only_ && GPU_backend_get_type() != GPU_BACKEND_METAL) ||
874 (GPU_geometry_shader_support() == false && info->geometry_source_ != nullptr))
875 {
876 skipped++;
877 continue;
878 }
879 if (flag_is_set(info->builtins_combined(), BuiltinBits::RAY_QUERY) &&
881 {
882 skipped++;
883 continue;
884 }
885 total++;
886
887 handles.append(
888 GPU_shader_async_compilation(reinterpret_cast<const GPUShaderCreateInfo *>(info)));
889 }
890 }
891
893
894 for (AsyncCompilationHandle handle : handles) {
896 success++;
897#if 0 /* TODO(fclem): This is too verbose for now. Make it a cmake option. */
898 /* Test if any resource is optimized out and print a warning if that's the case. */
899 /* TODO(fclem): Limit this to OpenGL backend. */
900 const ShaderInterface *interface = shader->interface;
901
902 Vector<ShaderCreateInfo::Resource> all_resources = info->resources_get_all_();
903
904 for (ShaderCreateInfo::Resource &res : all_resources) {
905 StringRefNull name = "";
906 const ShaderInput *input = nullptr;
907
908 switch (res.bind_type) {
910 input = interface->ubo_get(res.slot);
911 name = res.uniformbuf.name;
912 break;
914 input = interface->ssbo_get(res.slot);
915 name = res.storagebuf.name;
916 break;
918 input = interface->texture_get(res.slot);
919 name = res.sampler.name;
920 break;
922 input = interface->texture_get(res.slot);
923 name = res.image.name;
924 break;
926 input = interface->acceleration_structure_get(res.slot);
927 name = res.acceleration_structure.name;
928 break;
929 }
930
931 if (input == nullptr) {
932 std::cerr << "Error: " << info->name_;
933 std::cerr << ": Resource « " << name << " » not found in the shader interface\n";
934 }
935 else if (input->location == -1) {
936 std::cerr << "Warning: " << info->name_;
937 std::cerr << ": Resource « " << name << " » is optimized out\n";
938 }
939 }
940#endif
942 }
943 }
944
945 printf("Shader Test compilation result: %d / %d passed", success, total);
946 if (skipped_filter > 0) {
947 printf(" (skipped %d when filtering)", skipped_filter);
948 }
949 if (skipped > 0) {
950 printf(" (skipped %d for compatibility reasons)", skipped);
951 }
952 printf("\n");
953 return success == total;
954}
955
956const GPUShaderCreateInfo *gpu_shader_create_info_get(const char *info_name)
957{
958 if (g_create_infos->contains(info_name) == false) {
959 printf("Error: Cannot find shader create info named \"%s\"\n", info_name);
960 return nullptr;
961 }
962 ShaderCreateInfo *info = g_create_infos->lookup(info_name);
963 return reinterpret_cast<const GPUShaderCreateInfo *>(info);
964}
965
966} // namespace blender
#define ELEM(...)
ValueIterator values() const &
Definition BLI_map.hh:884
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_map.hh:353
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRefNull()
void append(const T &value)
constexpr bool is_empty() const
PipelineGraphic eevee_deferred_tile_classify(eevee::deferred::tiles::fullscreen_vert, eevee::deferred::tiles::classify_tiles, eevee::deferred::tiles::TileClassification{ .use_stencil_ref_out=true, })
Span< ShaderCreateInfo::SharedVariable > gpu_shader_dependency_get_shared_variables(const StringRefNull shader_source_name)
static CreateInfoDictionary * g_create_infos
Map< StringRef, ShaderCreateInfo * > CreateInfoDictionary
BuiltinBits gpu_shader_dependency_get_builtins(const StringRefNull shader_source_name)
Map< StringRef, StageInterfaceInfo * > InterfaceDictionary
static InterfaceDictionary * g_interfaces
bool gpu_shader_dependency_force_gpu_print_injection()
static bool is_vulkan_compatible_interface(const StageInterfaceInfo &iface)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_shader_compiler_wait_for_all()
bool GPU_geometry_shader_support()
bool gpu_shader_create_info_compile_all(const char *name_starts_with_filter)
static void init_compositor_infos()
static void init_ocio_infos()
GPUBackendType GPU_backend_get_type()
void gpu_shader_create_info_init()
static void init_workbench_infos()
bool GPU_ray_query_support()
static void init_gpencil_infos()
static void init_overlay_infos()
void gpu_shader_create_info_exit()
static void init_eevee_infos()
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
static void init_gpu_infos()
static void init_draw_infos()
int64_t AsyncCompilationHandle
Definition GPU_shader.hh:87
static void init_image_infos()
const GPUShaderCreateInfo * gpu_shader_create_info_get(const char *info_name)
static void init_osd_infos()
gpu::Shader * GPU_shader_async_compilation_finalize(AsyncCompilationHandle &handle)
void GPU_shader_free(gpu::Shader *shader)
bool GPU_stencil_classify_buffer_workaround()
AsyncCompilationHandle GPU_shader_async_compilation(const GPUShaderCreateInfo *info, CompilationPriority priority=CompilationPriority::High)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
void validate_vertex_attributes(const ShaderCreateInfo *other_info=nullptr)
Vector< SpecializationConstant, 0 > specialization_constants_
void extend_predicate(Vector< Resource, 0 > &resource_vector, ShaderCreateInfo::Resource res_copy, Span< ConditionFn > additional_conditions) const
void assert_no_overlap(const ShaderCreateInfo &info, const bool test, const StringRefNull error) const
Vector< CompilationConstant, 0 > compilation_constants_
void finalize(const bool recursive=false)
std::string resource_guard_defines(Span< CompilationConstant > constants) const
Vector< StageInterfaceInfoHandle, 0 > geometry_out_interfaces_
void validate_merge(const ShaderCreateInfo &other_info)
Vector< StageInterfaceInfoHandle, 0 > vertex_out_interfaces_
Self & additional_info(StringRefNull info_name)
void set_resource_slot(Resource &res, int &images, int &samplers, int &ubos, int &ssbos, int &acceleration_structures) const
Vector< std::array< StringRefNull, 2 >, 0 > defines_