Blender V5.3
gpu_shader.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_colorspace.hh"
10#include "BLI_math_matrix_c.hh"
12#include "BLI_string.hh"
13
14#include "CLG_log.h"
15
16#include "GPU_capabilities.hh"
17#include "GPU_debug.hh"
18#include "GPU_matrix.hh"
19#include "GPU_platform.hh"
20
22
23#include "gpu_backend.hh"
25#include "gpu_profile_report.hh"
30#include "gpu_shader_private.hh"
31
32#include <filesystem>
33#include <string>
34
35namespace blender {
36
38
39static CLG_LogRef LOG = {"gpu.shader"};
40
41namespace gpu {
42
44 StringRef shader_name_with_stage_name,
45 StringRef extension,
46 StringRef source)
47{
48 StringRefNull pattern = G.gpu_debug_shader_source_name;
49 /* Support starting and/or ending with a wildcard. */
50 if (pattern == "*") {
51 /* If using a single wildcard, match everything. */
52 }
53 else if (pattern.startswith("*") && pattern.endswith("*")) {
54 std::string sub_str = pattern.substr(1, pattern.size() - 2);
55 if (shader_name.find(sub_str) == std::string::npos) {
56 return;
57 }
58 }
59 else if (pattern.startswith("*")) {
60 std::string sub_str = pattern.substr(1);
61 if (!shader_name.endswith(sub_str)) {
62 return;
63 }
64 }
65 else if (pattern.endswith("*")) {
66 std::string sub_str = pattern.substr(0, pattern.size() - 1);
67 if (!shader_name.startswith(sub_str)) {
68 return;
69 }
70 }
71 else if (shader_name != pattern) {
72 return;
73 }
74
75 namespace fs = std::filesystem;
76 fs::path shader_dir = fs::current_path() / "Shaders";
77 fs::create_directories(shader_dir);
78 fs::path file_path = shader_dir / (shader_name_with_stage_name + extension);
79
80 std::ofstream output_source_file(file_path);
81 if (output_source_file) {
82 output_source_file << source;
83 output_source_file.close();
84 std::cout << "Shader Source Debug: Writing file: " << file_path << "\n";
85 }
86 else {
87 std::cerr << "Shader Source Debug: Failed to open file: " << file_path << "\n";
88 }
89}
90
92{
93 std::string defines;
94 for (const auto &def : info.defines_) {
95 defines += "#define ";
96 defines += def[0];
97 defines += " ";
98 defines += def[1];
99 defines += "\n";
100 }
101 return defines;
102}
103
104} // namespace gpu
105
106using namespace blender::gpu;
107
108/* -------------------------------------------------------------------- */
111
112Shader::Shader(const char *sh_name)
113{
114 STRNCPY(this->name, sh_name);
115
116 /* Escape the shader name to be able to use it inside an identifier. */
117 for (char &c : name) {
118 if (c == '\0') {
119 break;
120 }
121 if (!std::isalnum(c)) {
122 c = '_';
123 }
124 }
125}
126
128{
129 BLI_assert_msg(Context::get() == nullptr || Context::get()->shader != this,
130 "Shader must be unbound from context before being freed");
131 delete interface;
132}
133
135{
136 BLI_assert(sources.is_empty());
137 /* Version and specialization constants needs to be first.
138 * Exact values will be added by implementation. */
139 sources.append("version");
140 /* Specialization constants will be inserted here. */
141 sources.append("\n");
142 /* Define to identify code usage in shading language. */
143 sources.append("#define GPU_SHADER\n");
144 /* some useful defines to detect GPU type */
146 sources.append("#define GPU_ATI\n");
147 }
149 sources.append("#define GPU_NVIDIA\n");
150 }
152 sources.append("#define GPU_INTEL\n");
153 }
155 sources.append("#define GPU_APPLE\n");
156 }
157 /* some useful defines to detect OS type */
159 sources.append("#define OS_WIN\n");
160 }
162 sources.append("#define OS_MAC\n");
163 }
165 sources.append("#define OS_UNIX\n");
166 }
167 /* API Definition */
169 switch (backend) {
171 sources.append("#define GPU_OPENGL\n");
172 break;
174 sources.append("#define GPU_METAL\n");
175 break;
177 sources.append("#define GPU_VULKAN\n");
178 break;
179 default:
180 BLI_assert_msg(false, "Invalid GPU Backend Type");
181 break;
182 }
183}
184
186{
187 delete shader;
188}
189
191
192/* -------------------------------------------------------------------- */
195
196const GPUShaderCreateInfo *GPU_shader_create_info_get(const char *info_name)
197{
198 return gpu_shader_create_info_get(info_name);
199}
200
201bool GPU_shader_create_info_check_error(const GPUShaderCreateInfo *_info, char r_error[128])
202{
203 using namespace blender::gpu::shader;
204 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
205 std::string error = info.check_error();
206 if (error.empty()) {
207 return true;
208 }
209
210 BLI_strncpy(r_error, error.c_str(), 128);
211 return false;
212}
213
215{
216 using namespace blender::gpu::shader;
217 const GPUShaderCreateInfo *_info = gpu_shader_create_info_get(info_name);
218 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
219 if (!info.do_static_compilation_) {
220 std::cerr << "Warning: Trying to compile \"" << info.name_
221 << "\" which was not marked for static compilation.\n";
222 }
223 return GPU_shader_create_from_info(_info);
224}
225
226gpu::Shader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
227{
228 using namespace blender::gpu::shader;
229 const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
230 return GPUBackend::get()->get_compiler()->compile(info, false);
231}
232
235{
236 if (original.is_empty()) {
237 return original;
238 }
240 original, "python_shader.glsl", shader::Language::GLSL, {});
241 auto [processed_str, metadata, error] = processor.convert();
242
243 if (error.has_value()) {
244 std::cerr << error->full_report << std::endl;
245 return "\n#error conversion failed\n";
246 }
247
248 for (auto builtin : metadata.builtins) {
250 }
251 /* WORKAROUND: We have an extra check in place on Metal for clip distances (see #160847). */
253 info.define("USE_WORLD_CLIP_PLANES");
254 }
255 return processed_str;
256};
257
258gpu::Shader *GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)
259{
260 using namespace blender::gpu::shader;
261 ShaderCreateInfo info = *const_cast<ShaderCreateInfo *>(
262 reinterpret_cast<const ShaderCreateInfo *>(_info));
263
264 const bool is_compute = !info.compute_source_generated.empty();
265
266 std::array<StringRefNull, 2> includes = {
267 "gpu_shader_python_base.glsl",
268 "gpu_shader_python_typedef_lib.glsl",
269 };
270
271 if (!info.typedef_source_generated.empty()) {
272 info.generated_sources.append(
273 {"gpu_shader_python_typedef_lib.glsl", {}, "\n" + info.typedef_source_generated});
274 }
275 else {
276 /* Add empty source to avoid warning and importing the placeholder file. */
277 info.generated_sources.append({"gpu_shader_python_typedef_lib.glsl", {}, "\n"});
278 }
279
280#ifdef __APPLE__
281 /* See usage for more info. */
282 info.define("WITH_MATRIX_EQ_OPERATORS");
283#endif
284
287
288 auto preprocess_source = [&](const std::string &input_src) {
289 std::string processed_str;
290 processed_str += "\n";
291 processed_str += "#ifdef CREATE_INFO_RES_PASS_pyGPU_Shader\n";
292 processed_str += "CREATE_INFO_RES_PASS_pyGPU_Shader\n";
293 processed_str += "#endif\n";
294 processed_str += GPU_shader_preprocess_source(input_src, info);
295 return processed_str;
296 };
297
298 if (is_compute) {
299 info.compute_source("gpu_shader_python_comp.glsl");
300 info.generated_sources.append({"gpu_shader_python_comp.glsl",
301 includes,
302 preprocess_source(info.compute_source_generated)});
303 }
304 else {
305 info.vertex_source("gpu_shader_python_vert.glsl");
306 info.generated_sources.append({"gpu_shader_python_vert.glsl",
307 includes,
308 preprocess_source(info.vertex_source_generated)});
309
310 info.fragment_source("gpu_shader_python_frag.glsl");
311 info.generated_sources.append({"gpu_shader_python_frag.glsl",
312 includes,
313 preprocess_source(info.fragment_source_generated)});
314 }
315
317 return result;
318}
319
321 CompilationPriority priority)
322{
323 using namespace blender::gpu::shader;
324 const ShaderCreateInfo *info_ = reinterpret_cast<const ShaderCreateInfo *>(info);
325 return GPUBackend::get()->get_compiler()->async_compilation(info_, priority);
326}
327
332
338
343
348
353
358
363
365{
366 printf("Compiling all static GPU shaders. This process takes a while.\n");
368}
369
374
376
377/* -------------------------------------------------------------------- */
380
382 const shader::SpecializationConstants *constants_state)
383{
384 Shader *shader = gpu_shader;
385
386 BLI_assert_msg(constants_state != nullptr || shader->constants->is_empty(),
387 "Shader requires specialization constants but none was passed");
388
389 Context *ctx = Context::get();
390
391 if (ctx->shader != shader) {
392 ctx->shader = shader;
393 shader->bind(constants_state);
394 GPU_matrix_bind(gpu_shader);
395 Shader::set_srgb_uniform(ctx, gpu_shader);
396 /* Blender working color space do not change during the drawing of the frame.
397 * So we can just set the uniform once. */
399 }
400 else {
401 if (constants_state) {
402 shader->bind(constants_state);
403 }
405 Shader::set_srgb_uniform(ctx, gpu_shader);
406 }
407 if (GPU_matrix_dirty_get()) {
408 GPU_matrix_bind(gpu_shader);
409 }
410 }
411#if GPU_SHADER_PRINTF_ENABLE
412 if (!ctx->printf_buf.is_empty()) {
414 }
415#endif
416}
417
419{
420 Context *ctx = Context::get();
421 if (ctx == nullptr) {
422 return;
423 }
424#ifndef NDEBUG
425 if (ctx->shader) {
426 ctx->shader->unbind();
427 }
428#endif
429 ctx->shader = nullptr;
430}
431
433{
434 Context *ctx = Context::get();
435 if (ctx) {
436 return ctx->shader;
437 }
438 return nullptr;
439}
440
442
443/* -------------------------------------------------------------------- */
446
448{
449 return shader->name_get().c_str();
450}
451
453
454/* -------------------------------------------------------------------- */
457
459{
460 BLI_assert(shader != nullptr);
461 BLI_assert(shader != parent);
462 if (shader != parent) {
463 Shader *shd_child = shader;
464 Shader *shd_parent = parent;
465 shd_child->parent_set(shd_parent);
466 }
467}
468
470{
471 shader->warm_cache(limit);
472}
473
475
476/* -------------------------------------------------------------------- */
479
484
486{
487 using namespace shader;
490 constants_tmp.types.append(sc.type);
491 constants_tmp.values.append(sc.value);
492 }
493 constants = std::make_unique<const shader::SpecializationConstants>(std::move(constants_tmp));
494}
495
497 const ShaderSpecialization &specialization, CompilationPriority priority)
498{
499 return GPUBackend::get()->get_compiler()->async_specialization(specialization, priority);
500}
501
506
511
513
514/* -------------------------------------------------------------------- */
517
519{
520 const ShaderInterface *interface = shader->interface;
521 const ShaderInput *uniform = interface->uniform_get(name);
522 return uniform ? uniform->location : -1;
523}
524
526{
527 const ShaderInterface *interface = shader->interface;
528 const ShaderInput *constant = interface->constant_get(name);
529 return constant ? constant->location : -1;
530}
531
533{
534 const ShaderInterface *interface = shader->interface;
535 return interface->uniform_builtin(GPUUniformBuiltin(builtin));
536}
537
539{
540 const ShaderInterface *interface = shader->interface;
541 const ShaderInput *ssbo = interface->ssbo_get(name);
542 return ssbo ? ssbo->location : -1;
543}
544
546{
547 const ShaderInterface *interface = shader->interface;
548 const ShaderInput *ubo = interface->ubo_get(name);
549 return ubo ? ubo->location : -1;
550}
551
553{
554 const ShaderInterface *interface = shader->interface;
555 const ShaderInput *ubo = interface->ubo_get(name);
556 return ubo ? ubo->binding : -1;
557}
558
560{
561 const ShaderInterface *interface = shader->interface;
562 const ShaderInput *tex = interface->uniform_get(name);
563 return tex ? tex->binding : -1;
564}
565
567{
568 const ShaderInterface *interface = shader->interface;
569 const ShaderInput *tlas = interface->tlas_get(name);
570 return tlas ? tlas->location : -1;
571}
572
574{
575 const ShaderInterface *interface = shader->interface;
576 return interface->valid_bindings_get(interface->inputs_, interface->attr_len_);
577}
578
580{
581 const ShaderInterface *interface = shader->interface;
582 return interface->ssbo_len_;
583}
584
585int GPU_shader_get_attribute(const gpu::Shader *shader, const char *name)
586{
587 const ShaderInterface *interface = shader->interface;
588 const ShaderInput *attr = interface->attr_get(name);
589 return attr ? attr->location : -1;
590}
591
593 int attr_location,
594 char r_name[256],
595 int *r_type)
596{
597 const ShaderInterface *interface = shader->interface;
598
599 const ShaderInput *attr = interface->attr_get(attr_location);
600 if (!attr) {
601 return false;
602 }
603
604 BLI_strncpy(r_name, interface->input_name_get(attr), 256);
605 *r_type = attr->location != -1 ? interface->attr_types_[attr->location] : -1;
606 return true;
607}
608
609bool GPU_shader_get_ssbo_input_info(const gpu::Shader *shader, int ssbo_location, char r_name[256])
610{
611 const ShaderInterface *interface = shader->interface;
612
613 const ShaderInput *ssbo_input = interface->ssbo_get(ssbo_location);
614 if (!ssbo_input) {
615 return false;
616 }
617
618 BLI_strncpy(r_name, interface->input_name_get(ssbo_input), 256);
619 return true;
620}
621
623
624/* -------------------------------------------------------------------- */
627
629 gpu::Shader *shader, int loc, int len, int array_size, const float *value)
630{
631 shader->uniform_float(loc, len, array_size, value);
632}
633
635 gpu::Shader *shader, int loc, int len, int array_size, const int *value)
636{
637 shader->uniform_int(loc, len, array_size, value);
638}
639
640void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
641{
642 const int loc = GPU_shader_get_uniform(sh, name);
643 GPU_shader_uniform_int_ex(sh, loc, 1, 1, &value);
644}
645
646void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
647{
648 GPU_shader_uniform_1i(sh, name, value ? 1 : 0);
649}
650
651void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
652{
653 const float data[2] = {x, y};
654 GPU_shader_uniform_2fv(sh, name, data);
655}
656
657void GPU_shader_uniform_3f(gpu::Shader *sh, const char *name, float x, float y, float z)
658{
659 const float data[3] = {x, y, z};
660 GPU_shader_uniform_3fv(sh, name, data);
661}
662
663void GPU_shader_uniform_4f(gpu::Shader *sh, const char *name, float x, float y, float z, float w)
664{
665 const float data[4] = {x, y, z, w};
666 GPU_shader_uniform_4fv(sh, name, data);
667}
668
669void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
670{
671 const int loc = GPU_shader_get_uniform(sh, name);
672 GPU_shader_uniform_float_ex(sh, loc, 1, 1, &value);
673}
674
675void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
676{
677 const int loc = GPU_shader_get_uniform(sh, name);
678 GPU_shader_uniform_float_ex(sh, loc, 2, 1, data);
679}
680
681void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
682{
683 const int loc = GPU_shader_get_uniform(sh, name);
684 GPU_shader_uniform_float_ex(sh, loc, 3, 1, data);
685}
686
687void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
688{
689 const int loc = GPU_shader_get_uniform(sh, name);
690 GPU_shader_uniform_float_ex(sh, loc, 4, 1, data);
691}
692
693void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
694{
695 const int loc = GPU_shader_get_uniform(sh, name);
696 GPU_shader_uniform_int_ex(sh, loc, 2, 1, data);
697}
698
699void GPU_shader_uniform_3iv(gpu::Shader *sh, const char *name, const int data[3])
700{
701 const int loc = GPU_shader_get_uniform(sh, name);
702 GPU_shader_uniform_int_ex(sh, loc, 3, 1, data);
703}
704
705void GPU_shader_uniform_mat4(gpu::Shader *sh, const char *name, const float data[4][4])
706{
707 const int loc = GPU_shader_get_uniform(sh, name);
708 GPU_shader_uniform_float_ex(sh, loc, 16, 1, reinterpret_cast<const float *>(data));
709}
710
711void GPU_shader_uniform_mat3(gpu::Shader *sh, const char *name, const float data[3][3])
712{
713 const int loc = GPU_shader_get_uniform(sh, name);
714 GPU_shader_uniform_float_ex(sh, loc, 9, 1, reinterpret_cast<const float *>(data));
715}
716
717void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
718{
719 float matrix[4][4];
720 copy_m4_m3(matrix, data);
721 GPU_shader_uniform_mat4(sh, name, matrix);
722}
723
724void GPU_shader_uniform_1f_array(gpu::Shader *sh, const char *name, int len, const float *val)
725{
726 const int loc = GPU_shader_get_uniform(sh, name);
727 GPU_shader_uniform_float_ex(sh, loc, 1, len, val);
728}
729
731 const char *name,
732 int len,
733 const float (*val)[2])
734{
735 const int loc = GPU_shader_get_uniform(sh, name);
736 GPU_shader_uniform_float_ex(sh, loc, 2, len, reinterpret_cast<const float *>(val));
737}
738
740 const char *name,
741 int len,
742 const float (*val)[4])
743{
744 const int loc = GPU_shader_get_uniform(sh, name);
745 GPU_shader_uniform_float_ex(sh, loc, 4, len, reinterpret_cast<const float *>(val));
746}
747
749
750namespace gpu {
751
752/* -------------------------------------------------------------------- */
762
771
779
780void Shader::set_framebuffer_srgb_target(int use_srgb_to_linear)
781{
782 Context *ctx = Context::get();
783 if (ctx->shader_builtin_srgb_transform != use_srgb_to_linear) {
784 ctx->shader_builtin_srgb_transform = use_srgb_to_linear;
786 }
787}
788
790
791/* -------------------------------------------------------------------- */
794
795Shader *ShaderCompiler::compile(const shader::ShaderCreateInfo &orig_info, bool is_codegen_only)
796{
797 using Clock = std::chrono::steady_clock;
798 using TimePoint = Clock::time_point;
799
800 using namespace blender::gpu::shader;
801 const_cast<ShaderCreateInfo &>(orig_info).finalize();
802 BLI_assert(orig_info.do_static_compilation_ || orig_info.is_generated_);
803
804 TimePoint start_time;
805
806 if (Context::get()) {
807 /* Context can be null in Vulkan compilation threads. */
809 GPU_debug_group_begin(orig_info.name_.c_str());
810 }
811 else if (G.profile_gpu) {
812 start_time = Clock::now();
813 }
814
815 CLOG_DEBUG(&LOG, "Compiling Shader \"%s\"", orig_info.name_.c_str());
816
817 Shader *shader = GPUBackend::get()->shader_alloc(orig_info.name_.c_str());
818
819 ShaderCreateInfo specialized_info = orig_info;
820
821 if (!specialized_info.compilation_constants_.is_empty()) {
822 auto predicate = [&](const ShaderCreateInfo::Resource &res) {
823 return !res.conditions.evaluate(specialized_info.compilation_constants_);
824 };
825 specialized_info.pass_resources_.remove_if(predicate);
826 specialized_info.batch_resources_.remove_if(predicate);
827 specialized_info.geometry_resources_.remove_if(predicate);
828 }
829
830 if (!specialized_info.vertex_inputs_.is_empty()) {
831 auto predicate = [&](const ShaderCreateInfo::VertIn &res) {
832 return !res.conditions.evaluate(specialized_info.compilation_constants_);
833 };
834 specialized_info.vertex_inputs_.remove_if(predicate);
835 }
836
837 if (!specialized_info.vertex_out_interfaces_.is_empty()) {
838 auto predicate = [&](const ShaderCreateInfo::StageInterfaceInfoHandle &res) {
839 return !res.conditions.evaluate(specialized_info.compilation_constants_);
840 };
841 specialized_info.vertex_out_interfaces_.remove_if(predicate);
842 }
843
844 if (!specialized_info.push_constants_.is_empty()) {
845 auto predicate = [&](const ShaderCreateInfo::PushConst &res) {
846 return !res.conditions.evaluate(specialized_info.compilation_constants_);
847 };
848 specialized_info.push_constants_.remove_if(predicate);
849 }
850
851 if (!specialized_info.builtins_.is_empty()) {
852 auto predicate = [&](const ShaderCreateInfo::BuiltinBit &res) {
853 return !res.conditions.evaluate(specialized_info.compilation_constants_);
854 };
855 specialized_info.builtins_.remove_if(predicate);
856 }
857 /* Flatten builtins into a single entry to speedup comparisons later on. */
858 BuiltinBits builtin_combined = BuiltinBits::NONE;
859 for (auto value : specialized_info.builtins_) {
860 builtin_combined = builtin_combined | value.bit;
861 }
862 specialized_info.builtins_.clear();
863 specialized_info.builtins(builtin_combined);
864
865 /* We merged infos keeping duplicates because of possible different condition per definitions.
866 * Deduplicate remaining ones to avoid errors. */
867 auto cleanup_duplicates = [&](auto &resources) {
868 auto tmp = resources;
869 resources.clear();
870 resources.extend_non_duplicates(tmp);
871 };
872 cleanup_duplicates(specialized_info.pass_resources_);
873 cleanup_duplicates(specialized_info.batch_resources_);
874 cleanup_duplicates(specialized_info.geometry_resources_);
875 cleanup_duplicates(specialized_info.push_constants_);
876
877 const std::string error = specialized_info.check_error();
878 if (!error.empty()) {
879 std::cerr << error << "\n";
880 return nullptr;
881 }
882
883 const shader::ShaderCreateInfo &info = shader->patch_create_info(specialized_info);
884
885 /* Needs to be called before init as GL uses the default specialization constants state to insert
886 * default shader inside a map. */
887 shader->specialization_constants_init(info);
888 shader->init(info, is_codegen_only);
889
890 shader->fragment_output_bits = 0;
891 for (const shader::ShaderCreateInfo::FragOut &frag_out : info.fragment_outputs_) {
892 shader->fragment_output_bits |= 1u << frag_out.index;
893 }
894
895 shader->skip_preprocessor = bool(specialized_info.builtins_combined() &
897
898 std::string defines = shader->defines_declare(info);
899 std::string resources = shader->resources_declare(info);
900
902 defines += info.iface_guard_defines();
903
904 if (!info.compute_entry_fn_.is_empty()) {
905 defines += "#define ENTRY_POINT_" + info.compute_entry_fn_ + "\n";
906 }
907 if (!info.fragment_entry_fn_.is_empty()) {
908 defines += "#define ENTRY_POINT_" + info.fragment_entry_fn_ + "\n";
909 }
910 if (!info.vertex_entry_fn_.is_empty()) {
911 defines += "#define ENTRY_POINT_" + info.vertex_entry_fn_ + "\n";
912 }
913
914 /* Compilation constants declaration for static branches evaluation.
915 * In the future, these can be compiled using function constants on metal to reduce compilation
916 * time. */
917 for (const auto &constant : info.compilation_constants_) {
918 defines += "#define SRT_CONSTANT_" + constant.name + " " + std::to_string(constant.value.i) +
919 "\n";
920 }
921
922 defines += "#define USE_GPU_SHADER_CREATE_INFO\n";
923
924 if (!info.vertex_source_.is_empty()) {
926 info.vertex_source_, info.generated_sources, info.name_);
927 std::string interface = shader->vertex_interface_declare(info);
928
929 Vector<StringRefNull> sources;
930 standard_defines(sources);
931 if (!info.geometry_source_.is_empty()) {
932 sources.append("#define USE_GEOMETRY_SHADER\n");
933 }
934 sources.append(defines);
935 sources.append(resources);
936 sources.append(interface);
937 sources.extend(code);
938
939 if (info.vertex_entry_fn_ != "main") {
940 sources.append("void main() { ");
941 sources.append(info.vertex_entry_fn_);
942 sources.append("(); }\n");
943 }
944
945 shader->vertex_shader_from_glsl(info, sources);
946 }
947
948 if (!info.fragment_source_.is_empty()) {
950 info.fragment_source_, info.generated_sources, info.name_);
951 std::string interface = shader->fragment_interface_declare(info);
952
953 Vector<StringRefNull> sources;
954 standard_defines(sources);
955 if (!info.geometry_source_.is_empty()) {
956 sources.append("#define USE_GEOMETRY_SHADER\n");
957 }
958 sources.append(defines);
959 sources.append(resources);
960 sources.append(interface);
961 sources.extend(code);
962
963 if (info.fragment_entry_fn_ != "main") {
964 sources.append("void main() { ");
965 sources.append(info.fragment_entry_fn_);
966 sources.append("(); }\n");
967 }
968
969 shader->fragment_shader_from_glsl(info, sources);
970 }
971
972 if (!info.geometry_source_.is_empty()) {
974 info.geometry_source_, info.generated_sources, info.name_);
975 std::string layout = shader->geometry_layout_declare(info);
976 std::string interface = shader->geometry_interface_declare(info);
977
978 Vector<StringRefNull> sources;
979 standard_defines(sources);
980 sources.append("#define GPU_GEOMETRY_SHADER\n");
981 sources.append(defines);
982 sources.append(resources);
983 sources.append(layout);
984 sources.append(interface);
985 sources.extend(code);
986
987 if (info.geometry_entry_fn_ != "main") {
988 sources.append("void main() { ");
989 sources.append(info.geometry_entry_fn_);
990 sources.append("(); }\n");
991 }
992
993 shader->geometry_shader_from_glsl(info, sources);
994 }
995
996 if (!info.compute_source_.is_empty()) {
998 info.compute_source_, info.generated_sources, info.name_);
999 std::string layout = shader->compute_layout_declare(info);
1000
1001 Vector<StringRefNull> sources;
1002 standard_defines(sources);
1003 sources.append(defines);
1004 sources.append(layout);
1005 sources.append(resources);
1006 sources.extend(code);
1007
1008 if (info.compute_entry_fn_ != "main") {
1009 sources.append("void main() { ");
1010 sources.append(info.compute_entry_fn_);
1011 sources.append("(); }\n");
1012 }
1013
1014 shader->compute_shader_from_glsl(info, sources);
1015 }
1016
1017 if (!shader->finalize(&info)) {
1018 delete shader;
1019 shader = nullptr;
1020 }
1021
1022 if (Context::get()) {
1023 /* Context can be null in Vulkan compilation threads. */
1026 }
1027 else if (G.profile_gpu) {
1028 TimePoint end_time = Clock::now();
1029 /* Note: Used by the vulkan backend. Use the same time_since_epoch as process_frame_timings. */
1031 start_time.time_since_epoch().count(),
1032 end_time.time_since_epoch().count());
1033 ProfileReport::get().add_group_cpu(info.name_.c_str(),
1034 start_time.time_since_epoch().count(),
1035 end_time.time_since_epoch().count());
1036 }
1037
1038 return shader;
1039}
1040
1054
1056 GPUWorker::ContextType context_type,
1057 bool support_specializations)
1058{
1059 support_specializations_ = support_specializations;
1060
1062 compilation_worker_ = std::make_unique<GPUWorker>(
1063 threads_count, context_type, do_work_static_cb);
1064 }
1065}
1066
1068{
1069 compilation_worker_.reset();
1070
1071 /* Ensure all the requested compilations have been retrieved. */
1072 BLI_assert(async_compilations_.is_empty());
1073}
1074
1076{
1077 return compile(info, false);
1078}
1079
1081 CompilationPriority priority)
1082{
1083 std::unique_lock lock(mutex_);
1084
1085 AsyncCompilation *compilation = MEM_new<AsyncCompilation>(__func__);
1086 compilation->info = info;
1087
1088 AsyncCompilationHandle handle = next_handle_++;
1089 async_compilations_.add(handle, compilation);
1090
1091 if (compilation_worker_) {
1092 compilation->work = std::make_unique<ParallelWork>(ParallelWork{this, compilation});
1093 compilation->work->id = compilation_worker_->push_work(compilation->work.get(),
1094 to_work_priority(priority));
1095 }
1096 else {
1097 compilation->shader = compile(*info, false);
1098 compilation->is_ready = true;
1099 }
1100
1101 return handle;
1102}
1103
1105{
1106 {
1107 std::unique_lock lock(mutex_);
1108
1109 AsyncCompilation *compilation = async_compilations_.pop(handle);
1110 if (compilation_worker_ && compilation_worker_->cancel_work(compilation->work->id)) {
1111 compilation->is_ready = true;
1112 }
1113
1114 /* If it was already being compiled, wait until it's ready so the calling thread can safely
1115 * delete the ShaderCreateInfos. */
1116 compilation_finished_notification_.wait(lock, [&]() { return compilation->is_ready == true; });
1117 GPU_shader_free(compilation->shader);
1118 compilation->shader = nullptr;
1119 MEM_delete(compilation);
1120
1121 handle = 0;
1122 }
1123
1124 /* Count this as a finished compilation, since wait_for_all might be waiting. */
1125 compilation_finished_notification_.notify_all();
1126}
1127
1129{
1130 std::lock_guard lock(mutex_);
1131
1132 return async_compilations_.lookup(handle)->is_ready;
1133}
1134
1136{
1137 std::unique_lock lock(mutex_);
1138 /* TODO: Move to be first on the queue. */
1139 compilation_finished_notification_.wait(
1140 lock, [&]() { return async_compilations_.lookup(handle)->is_ready == true; });
1141
1142 AsyncCompilation *compilation = async_compilations_.pop(handle);
1143 Shader *shader = compilation->shader;
1144 MEM_delete(compilation);
1145 handle = 0;
1146
1147 return shader;
1148}
1149
1151 const ShaderSpecialization &specialization, CompilationPriority priority)
1152{
1153 if (!compilation_worker_ || !support_specializations_) {
1154 return 0;
1155 }
1156
1157 std::lock_guard lock(mutex_);
1158
1159 AsyncCompilation *compilation = MEM_new<AsyncCompilation>(__func__);
1160 compilation->specialization = std::make_unique<ShaderSpecialization>(specialization);
1161
1162 AsyncCompilationHandle handle = next_handle_++;
1163 async_compilations_.add(handle, compilation);
1164
1165 compilation->work = std::make_unique<ParallelWork>(ParallelWork{this, compilation});
1166 compilation->work->id = compilation_worker_->push_work(compilation->work.get(),
1167 to_work_priority(priority));
1168
1169 return handle;
1170}
1171
1173{
1174 if (handle == 0) {
1175 return true;
1176 }
1177
1178 std::lock_guard lock(mutex_);
1179 if (async_compilations_.lookup(handle)->is_ready) {
1180 AsyncCompilation *compilation = async_compilations_.pop(handle);
1181 MEM_delete(compilation);
1182 handle = 0;
1183 }
1184
1185 return handle == 0;
1186}
1187
1188void ShaderCompiler::do_work_static_cb(void *payload)
1189{
1190 ParallelWork *work = reinterpret_cast<ParallelWork *>(payload);
1191 work->compiler->do_work(*work);
1192}
1193
1194void ShaderCompiler::do_work(ParallelWork &work)
1195{
1196 AsyncCompilation *compilation = work.compilation;
1197
1198 /* Compile */
1199 if (!compilation->is_specialization()) {
1200 compilation->shader = compile_shader(*compilation->info);
1201 }
1202 else {
1203 specialize_shader(*compilation->specialization);
1204 }
1205
1206 {
1207 std::unique_lock lock(mutex_);
1208 compilation->is_ready = true;
1209 }
1210
1211 compilation_finished_notification_.notify_all();
1212
1213 /* Pause must happen after the work has finished and before more work is requested,
1214 * otherwise we can run into deadlocks due to notifications desync. */
1215 std::unique_lock lock(mutex_);
1216 pause_finished_notification_.wait(lock, [&]() { return !is_paused_; });
1217}
1218
1219bool ShaderCompiler::is_compiling_impl()
1220{
1221 /* The mutex should be locked before calling this function. */
1222 BLI_assert(!mutex_.try_lock());
1223
1224 if (compilation_worker_ == nullptr) {
1225 return false;
1226 }
1227
1228 if (!compilation_worker_->is_empty()) {
1229 return true;
1230 }
1231
1232 for (AsyncCompilation *compilation : async_compilations_.values()) {
1233 if (!compilation->is_ready) {
1234 return true;
1235 }
1236 }
1237
1238 return false;
1239}
1240
1242{
1243 std::unique_lock lock(mutex_);
1244 return is_compiling_impl();
1245}
1246
1248{
1249 /* NOTE: We can't rely on BLI_thread_queue_wait_finish, since that only waits until the queue is
1250 * empty, but the works might still being processed. */
1251 std::unique_lock lock(mutex_);
1252 BLI_assert(!is_paused_);
1253 compilation_finished_notification_.wait(lock, [&]() { return !is_compiling_impl(); });
1254}
1255
1257{
1258 std::unique_lock lock(mutex_);
1259 BLI_assert(!is_paused_);
1260 is_paused_ = true;
1261}
1262
1264{
1265 {
1266 std::unique_lock lock(mutex_);
1267 BLI_assert(is_paused_);
1268 is_paused_ = false;
1269 }
1270 pause_finished_notification_.notify_all();
1271}
1272
1274
1275} // namespace gpu
1276} // namespace blender
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define GPU_DEBUG_SHADER_COMPILATION_GROUP
Definition GPU_debug.hh:70
volatile int lock
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void append(const T &value)
bool is_empty() const
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool endswith(StringRef suffix) const
Vector< StorageBuf * > printf_buf
static Context * get()
ShaderCompiler * get_compiler()
virtual void shader_cache_dir_clear_old()=0
static GPUBackend * get()
static ProfileReport & get()
AsyncCompilationHandle async_compilation(const shader::ShaderCreateInfo *info, CompilationPriority priority)
bool async_compilation_is_ready(AsyncCompilationHandle handle)
Shader * compile(const shader::ShaderCreateInfo &info, bool is_codegen_only)
bool async_specialization_is_ready(AsyncSpecializationHandle &handle)
void async_compilation_cancel(AsyncCompilationHandle &handle)
Shader * async_compilation_finalize(AsyncCompilationHandle &handle)
ShaderCompiler(uint32_t threads_count=1, GPUWorker::ContextType context_type=GPUWorker::ContextType::PerThread, bool support_specializations=false)
virtual Shader * compile_shader(const shader::ShaderCreateInfo &info)
AsyncSpecializationHandle async_specialization(const ShaderSpecialization &specialization, CompilationPriority priority)
virtual void unbind()=0
static void dump_source_to_disk(StringRef shader_name, StringRef shader_name_with_stage_name, StringRef extension, StringRef source)
Definition gpu_shader.cc:43
std::unique_ptr< const shader::SpecializationConstants > constants
ShaderInterface * interface
static std::string defines_declare(const shader::ShaderCreateInfo &info)
Definition gpu_shader.cc:91
void specialization_constants_init(const shader::ShaderCreateInfo &info)
void parent_set(Shader *parent)
static void set_scene_linear_to_xyz_uniform(gpu::Shader *shader)
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
static void set_srgb_uniform(Context *ctx, gpu::Shader *shader)
Result convert(metadata::Source external_sources_symbols={})
Definition processor.cc:387
#define GPU_SHADER_PRINTF_SLOT
#define uniform(slot)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::chrono::high_resolution_clock Clock
float3x3 scene_linear_to_rec709
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_name)
shader::BuiltinBits convert_builtin_bit(shader::metadata::Builtin builtin)
static ThreadQueueWorkPriority to_work_priority(CompilationPriority priority)
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
void GPU_shader_compiler_wait_for_all()
void GPU_shader_compiler_pause()
char * STRNCPY(char(&dst)[N], const char *src)
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
ThreadQueueWorkPriority
@ BLI_THREAD_QUEUE_WORK_PRIORITY_HIGH
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
@ BLI_THREAD_QUEUE_WORK_PRIORITY_LOW
unsigned int uint
Definition BKE_text.h:55
bool gpu_shader_create_info_compile_all(const char *name_starts_with_filter)
gpu::Shader * GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)
void GPU_shader_uniform_mat4(gpu::Shader *sh, const char *name, const float data[4][4])
void copy_m4_m3(float m1[4][4], const float m2[3][3])
int GPU_shader_get_constant(gpu::Shader *shader, const char *name)
void GPU_shader_compile_static()
void GPU_shader_uniform_3f(gpu::Shader *sh, const char *name, float x, float y, float z)
std::string GPU_shader_preprocess_source(StringRefNull original, gpu::shader::ShaderCreateInfo &info)
GPUBackendType GPU_backend_get_type()
void GPU_shader_async_compilation_cancel(AsyncCompilationHandle &handle)
bool GPU_use_main_context_workaround()
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
void GPU_shader_uniform_3iv(gpu::Shader *sh, const char *name, const int data[3])
char datatoc_gpu_shader_colorspace_lib_glsl[]
Definition gpu_shader.cc:37
void GPU_shader_uniform_1f_array(gpu::Shader *sh, const char *name, int len, const float *val)
int GPU_shader_get_attribute(const gpu::Shader *shader, const char *name)
void GPU_shader_async_specialization_cancel(AsyncSpecializationHandle &handle)
void GPU_shader_uniform_int_ex(gpu::Shader *shader, int location, int length, int array_size, const int *value)
CompilationPriority
Definition GPU_shader.hh:85
AsyncSpecializationHandle GPU_shader_async_specialization(const ShaderSpecialization &specialization, CompilationPriority priority=CompilationPriority::High)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_shader_set_parent(gpu::Shader *shader, gpu::Shader *parent)
@ GPU_UNIFORM_SCENE_LINEAR_XFORM
@ GPU_UNIFORM_SRGB_TRANSFORM
Global G
Definition blender.cc:57
void GPU_shader_uniform_4fv_array(gpu::Shader *sh, const char *name, int len, const float(*val)[4])
void GPU_shader_uniform_2fv_array(gpu::Shader *sh, const char *name, int len, const float(*val)[2])
void GPU_matrix_bind(gpu::Shader *shader)
unsigned int uint32_t
Definition BKE_text.h:81
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool GPU_shader_get_ssbo_input_info(const gpu::Shader *shader, int ssbo_location, char r_name[256])
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
bool GPU_shader_compiler_has_pending_work()
void GPU_shader_uniform_float_ex(gpu::Shader *shader, int location, int length, int array_size, const float *value)
int GPU_shader_get_tlas_binding(gpu::Shader *shader, const char *name)
void GPU_shader_cache_dir_clear_old()
uint GPU_shader_get_attribute_len(const gpu::Shader *shader)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
int64_t AsyncSpecializationHandle
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
int64_t AsyncCompilationHandle
Definition GPU_shader.hh:87
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_debug_group_end()
Definition gpu_debug.cc:34
int GPU_shader_get_uniform(gpu::Shader *shader, const char *name)
const GPUShaderCreateInfo * gpu_shader_create_info_get(const char *info_name)
gpu::Shader * GPU_shader_get_bound()
bool GPU_shader_async_compilation_is_ready(AsyncCompilationHandle handle)
uint GPU_shader_get_ssbo_input_len(const gpu::Shader *shader)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void GPU_shader_warm_cache(gpu::Shader *shader, int limit)
gpu::Shader * GPU_shader_async_compilation_finalize(AsyncCompilationHandle &handle)
const char * GPU_shader_get_name(gpu::Shader *shader)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
const GPUShaderCreateInfo * GPU_shader_create_info_get(const char *info_name)
signed int int32_t
Definition BKE_text.h:78
static CLG_LogRef LOG
Definition logging.cpp:12
static void standard_defines(Vector< StringRefNull > &sources)
bool GPU_matrix_dirty_get()
void GPU_shader_free(gpu::Shader *shader)
int GPU_shader_get_builtin_uniform(gpu::Shader *shader, int builtin)
void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
AsyncCompilationHandle GPU_shader_async_compilation(const GPUShaderCreateInfo *info, CompilationPriority priority=CompilationPriority::High)
void GPU_shader_uniform_mat3(gpu::Shader *sh, const char *name, const float data[3][3])
@ GPU_DEVICE_INTEL
@ GPU_DEVICE_NVIDIA
@ GPU_DEVICE_APPLE
bool GPU_shader_async_specialization_is_ready(AsyncSpecializationHandle &handle)
bool GPU_shader_get_attribute_info(const gpu::Shader *shader, int attr_location, char r_name[256], int *r_type)
void GPU_shader_uniform_4f(gpu::Shader *sh, const char *name, float x, float y, float z, float w)
void GPU_shader_compiler_resume()
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
bool GPU_shader_create_info_check_error(const GPUShaderCreateInfo *_info, char r_error[128])
int GPU_shader_get_uniform_block(gpu::Shader *shader, const char *name)
const gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(gpu::Shader *sh)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & compute_source(StringRefNull filename)
Self & fragment_source(StringRefNull filename)
Vector< SpecializationConstant, 0 > specialization_constants_
Vector< CompilationConstant, 0 > compilation_constants_
std::string resource_guard_defines(Span< CompilationConstant > constants) const
Vector< StageInterfaceInfoHandle, 0 > vertex_out_interfaces_
Self & builtins(BuiltinBits builtin, ConditionFn cond=nullptr)
Self & define(StringRefNull name, StringRefNull value="")
Vector< std::array< StringRefNull, 2 >, 0 > defines_
Vector< SpecializationConstant::Value, 8 > values