Blender V5.3
gl_shader.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <iomanip>
10
11#include "BKE_appdir.hh"
12#include "BKE_global.hh"
13
14#include "BLI_fileops.hh"
15#include "BLI_path_utils.hh"
16#include "BLI_string.hh"
17#include "BLI_time.hh"
18#include "BLI_vector.hh"
19
20#include "BLI_system.hh"
21#include BLI_SYSTEM_PID_H
22
23#include "GPU_capabilities.hh"
24#include "GPU_debug.hh"
25#include "GPU_platform.hh"
28
29#include "gl_debug.hh"
30#include "gl_vertex_buffer.hh"
31
33#include "gl_shader.hh"
35
36#include <sstream>
37#include <stdio.h>
38
39#include <fmt/format.h>
40#include <fmt/ranges.h>
41
42#include <chrono>
43#include <filesystem>
44#include <fstream>
45#include <iostream>
46#include <sstream>
47#include <stdio.h>
48#include <string>
49
50namespace blender {
51
52#ifdef WIN32
53# define popen _popen
54# define pclose _pclose
55#endif
56
57using namespace blender::gpu;
58using namespace blender::gpu::shader;
59
60/* -------------------------------------------------------------------- */
63
65{
66#if 0 /* Would be nice to have, but for now the Deferred compilation \
67 * does not have a GPUContext. */
68 BLI_assert(GLContext::get() != nullptr);
69#endif
70}
71
73{
74#if 0 /* Would be nice to have, but for now the Deferred compilation \
75 * does not have a GPUContext. */
76 BLI_assert(GLContext::get() != nullptr);
77#endif
78}
79
80void GLShader::init(const shader::ShaderCreateInfo &info, bool is_codegen_only)
81{
82 is_codegen_only_ = is_codegen_only;
83
84 /* Extract the constants names from info and store them locally. */
85 for (const SpecializationConstant &constant : info.specialization_constants_) {
86 specialization_constant_names_.append(constant.name.c_str());
87 }
88
89 /* NOTE: This is not threadsafe with regards to the specialization constants state access.
90 * The shader creation must be externally synchronized. */
91 main_program_ = program_cache_
92 .lookup_or_add_cb(constants->values,
93 []() { return std::make_unique<GLProgram>(); })
94 .get();
95 if (!main_program_->program_id) {
96 main_program_->program_id = glCreateProgram();
97 debug::object_label(GL_PROGRAM, main_program_->program_id, name);
98 }
99}
100
102
103/* -------------------------------------------------------------------- */
106
107static const char *to_string(const Interpolation &interp)
108{
109 switch (interp) {
111 return "smooth";
113 return "flat";
115 return "noperspective";
116 default:
117 return "unknown";
118 }
119}
120
121static const char *to_string(const Type &type)
122{
123 switch (type) {
124 case Type::float_t:
125 return "float";
126 case Type::float2_t:
127 return "vec2";
128 case Type::float3_t:
129 return "vec3";
130 case Type::float4_t:
131 return "vec4";
132 case Type::float3x3_t:
133 return "mat3";
134 case Type::float4x4_t:
135 return "mat4";
136 case Type::uint_t:
137 return "uint";
138 case Type::uint2_t:
139 return "uvec2";
140 case Type::uint3_t:
141 return "uvec3";
142 case Type::uint4_t:
143 return "uvec4";
144 case Type::int_t:
145 return "int";
146 case Type::int2_t:
147 return "ivec2";
148 case Type::int3_t:
149 return "ivec3";
150 case Type::int4_t:
151 return "ivec4";
152 case Type::bool_t:
153 return "bool";
154 /* Alias special types. */
155 case Type::uchar_t:
156 case Type::ushort_t:
157 return "uint";
158 case Type::uchar2_t:
159 case Type::ushort2_t:
160 return "uvec2";
161 case Type::uchar3_t:
162 case Type::ushort3_t:
163 return "uvec3";
164 case Type::uchar4_t:
165 case Type::ushort4_t:
166 return "uvec4";
167 case Type::char_t:
168 case Type::short_t:
169 return "int";
170 case Type::char2_t:
171 case Type::short2_t:
172 return "ivec2";
173 case Type::char3_t:
174 case Type::short3_t:
175 return "ivec3";
176 case Type::char4_t:
177 case Type::short4_t:
178 return "ivec4";
180 return "vec3";
181 }
183 return "unknown";
184}
185
187{
188 switch (type) {
189 case Type::float_t:
190 case Type::float2_t:
191 case Type::float3_t:
192 case Type::float4_t:
193 case Type::float3x3_t:
194 case Type::float4x4_t:
195 return Type::float_t;
196 case Type::uint_t:
197 case Type::uint2_t:
198 case Type::uint3_t:
199 case Type::uint4_t:
200 return Type::uint_t;
201 case Type::int_t:
202 case Type::int2_t:
203 case Type::int3_t:
204 case Type::int4_t:
205 case Type::bool_t:
206 return Type::int_t;
207 /* Alias special types. */
208 case Type::uchar_t:
209 case Type::uchar2_t:
210 case Type::uchar3_t:
211 case Type::uchar4_t:
212 case Type::ushort_t:
213 case Type::ushort2_t:
214 case Type::ushort3_t:
215 case Type::ushort4_t:
216 return Type::uint_t;
217 case Type::char_t:
218 case Type::char2_t:
219 case Type::char3_t:
220 case Type::char4_t:
221 case Type::short_t:
222 case Type::short2_t:
223 case Type::short3_t:
224 case Type::short4_t:
225 return Type::int_t;
227 return Type::float_t;
228 }
230 return Type::float_t;
231}
232
233static const char *to_string(const TextureFormat &type)
234{
235 switch (type) {
236 case TextureFormat::UINT_8_8_8_8:
237 return "rgba8ui";
238 case TextureFormat::SINT_8_8_8_8:
239 return "rgba8i";
240 case TextureFormat::UNORM_8_8_8_8:
241 return "rgba8";
242 case TextureFormat::UINT_32_32_32_32:
243 return "rgba32ui";
244 case TextureFormat::SINT_32_32_32_32:
245 return "rgba32i";
246 case TextureFormat::SFLOAT_32_32_32_32:
247 return "rgba32f";
248 case TextureFormat::UINT_16_16_16_16:
249 return "rgba16ui";
250 case TextureFormat::SINT_16_16_16_16:
251 return "rgba16i";
252 case TextureFormat::SFLOAT_16_16_16_16:
253 return "rgba16f";
254 case TextureFormat::UNORM_16_16_16_16:
255 return "rgba16";
256 case TextureFormat::UINT_8_8:
257 return "rg8ui";
258 case TextureFormat::SINT_8_8:
259 return "rg8i";
260 case TextureFormat::UNORM_8_8:
261 return "rg8";
262 case TextureFormat::UINT_32_32:
263 return "rg32ui";
264 case TextureFormat::SINT_32_32:
265 return "rg32i";
266 case TextureFormat::SFLOAT_32_32:
267 return "rg32f";
268 case TextureFormat::UINT_16_16:
269 return "rg16ui";
270 case TextureFormat::SINT_16_16:
271 return "rg16i";
272 case TextureFormat::SFLOAT_16_16:
273 return "rg16f";
274 case TextureFormat::UNORM_16_16:
275 return "rg16";
276 case TextureFormat::UINT_8:
277 return "r8ui";
278 case TextureFormat::SINT_8:
279 return "r8i";
280 case TextureFormat::UNORM_8:
281 return "r8";
282 case TextureFormat::UINT_32:
283 return "r32ui";
284 case TextureFormat::SINT_32:
285 return "r32i";
286 case TextureFormat::SFLOAT_32:
287 return "r32f";
288 case TextureFormat::UINT_16:
289 return "r16ui";
290 case TextureFormat::SINT_16:
291 return "r16i";
292 case TextureFormat::SFLOAT_16:
293 return "r16f";
294 case TextureFormat::UNORM_16:
295 return "r16";
296 case TextureFormat::UFLOAT_11_11_10:
297 return "r11f_g11f_b10f";
298 case TextureFormat::UNORM_10_10_10_2:
299 return "rgb10_a2";
300 default:
301 return "unknown";
302 }
303}
304
305static const char *to_string(const PrimitiveIn &layout)
306{
307 switch (layout) {
309 return "points";
311 return "lines";
313 return "lines_adjacency";
315 return "triangles";
317 return "triangles_adjacency";
318 default:
319 return "unknown";
320 }
321}
322
323static const char *to_string(const PrimitiveOut &layout)
324{
325 switch (layout) {
327 return "points";
329 return "line_strip";
331 return "triangle_strip";
332 default:
333 return "unknown";
334 }
335}
336
337static const char *to_string(const DepthWrite &value)
338{
339 switch (value) {
340 case DepthWrite::ANY:
341 return "depth_any";
343 return "depth_greater";
344 case DepthWrite::LESS:
345 return "depth_less";
346 default:
347 return "depth_unchanged";
348 }
349}
350
351static void print_image_type(std::ostream &os,
352 const ImageType &type,
354{
355 switch (type) {
356 case ImageType::IntBuffer:
357 case ImageType::Int1D:
358 case ImageType::Int1DArray:
359 case ImageType::Int2D:
360 case ImageType::Int2DArray:
361 case ImageType::Int3D:
362 case ImageType::IntCube:
363 case ImageType::IntCubeArray:
364 case ImageType::AtomicInt2D:
365 case ImageType::AtomicInt2DArray:
366 case ImageType::AtomicInt3D:
367 os << "i";
368 break;
369 case ImageType::UintBuffer:
370 case ImageType::Uint1D:
371 case ImageType::Uint1DArray:
372 case ImageType::Uint2D:
373 case ImageType::Uint2DArray:
374 case ImageType::Uint3D:
375 case ImageType::UintCube:
376 case ImageType::UintCubeArray:
377 case ImageType::AtomicUint2D:
378 case ImageType::AtomicUint2DArray:
379 case ImageType::AtomicUint3D:
380 os << "u";
381 break;
382 default:
383 break;
384 }
385
387 os << "image";
388 }
389 else {
390 os << "sampler";
391 }
392
393 switch (type) {
394 case ImageType::FloatBuffer:
395 case ImageType::IntBuffer:
396 case ImageType::UintBuffer:
397 os << "Buffer";
398 break;
399 case ImageType::Float1D:
400 case ImageType::Float1DArray:
401 case ImageType::Int1D:
402 case ImageType::Int1DArray:
403 case ImageType::Uint1D:
404 case ImageType::Uint1DArray:
405 os << "1D";
406 break;
407 case ImageType::Float2D:
408 case ImageType::Float2DArray:
409 case ImageType::Int2D:
410 case ImageType::Int2DArray:
411 case ImageType::AtomicInt2D:
412 case ImageType::AtomicInt2DArray:
413 case ImageType::Uint2D:
414 case ImageType::Uint2DArray:
415 case ImageType::AtomicUint2D:
416 case ImageType::AtomicUint2DArray:
417 case ImageType::Shadow2D:
418 case ImageType::Shadow2DArray:
419 case ImageType::Depth2D:
420 case ImageType::Depth2DArray:
421 os << "2D";
422 break;
423 case ImageType::Float3D:
424 case ImageType::Int3D:
425 case ImageType::Uint3D:
426 case ImageType::AtomicInt3D:
427 case ImageType::AtomicUint3D:
428 os << "3D";
429 break;
430 case ImageType::FloatCube:
431 case ImageType::FloatCubeArray:
432 case ImageType::IntCube:
433 case ImageType::IntCubeArray:
434 case ImageType::UintCube:
435 case ImageType::UintCubeArray:
436 case ImageType::ShadowCube:
437 case ImageType::ShadowCubeArray:
438 case ImageType::DepthCube:
439 case ImageType::DepthCubeArray:
440 os << "Cube";
441 break;
442 default:
443 break;
444 }
445
446 switch (type) {
447 case ImageType::Float1DArray:
448 case ImageType::Float2DArray:
449 case ImageType::FloatCubeArray:
450 case ImageType::Int1DArray:
451 case ImageType::Int2DArray:
452 case ImageType::IntCubeArray:
453 case ImageType::Uint1DArray:
454 case ImageType::Uint2DArray:
455 case ImageType::AtomicUint2DArray:
456 case ImageType::UintCubeArray:
457 case ImageType::Shadow2DArray:
458 case ImageType::ShadowCubeArray:
459 case ImageType::Depth2DArray:
460 case ImageType::DepthCubeArray:
461 os << "Array";
462 break;
463 default:
464 break;
465 }
466
467 switch (type) {
468 case ImageType::Shadow2D:
469 case ImageType::Shadow2DArray:
470 case ImageType::ShadowCube:
471 case ImageType::ShadowCubeArray:
472 os << "Shadow";
473 break;
474 default:
475 break;
476 }
477 os << " ";
478}
479
480static std::ostream &print_qualifier(std::ostream &os, const Qualifier &qualifiers)
481{
482 if (!flag_is_set(qualifiers, Qualifier::no_restrict)) {
483 os << "restrict ";
484 }
485 if (!flag_is_set(qualifiers, Qualifier::read)) {
486 os << "writeonly ";
487 }
488 if (!flag_is_set(qualifiers, Qualifier::write)) {
489 os << "readonly ";
490 }
491 return os;
492}
493
494static void print_resource(std::ostream &os,
496 const ShaderCreateInfo & /*info*/)
497{
498 {
499 os << "layout(binding = " << res.slot;
501 os << ", " << to_string(res.image.format);
502 }
504 os << ", std140";
505 }
507 os << ", std430";
508 }
509 os << ") ";
510 }
511
512 switch (res.bind_type) {
514 os << "uniform ";
515 print_image_type(os, res.sampler.type, res.bind_type);
516 os << res.sampler.name << ";";
517 break;
519 os << "uniform ";
520 print_qualifier(os, res.image.qualifiers);
521 print_image_type(os, res.image.type, res.bind_type);
522 os << res.image.name << ";";
523 break;
525 os << "uniform _" << res.uniformbuf.name.str_no_array() << " { ";
526 os << res.uniformbuf.type_name << " " << res.uniformbuf.name << "; };";
527 break;
529 print_qualifier(os, res.storagebuf.qualifiers);
530 os << "buffer _";
531 os << res.storagebuf.name.str_no_array() << " { ";
532 os << res.storagebuf.type_name << " " << res.storagebuf.name << "; };";
533 break;
536 break;
537 }
538}
539
540static void print_resource(std::ostream &os,
542 const ShaderCreateInfo &info,
543 StringRefNull res_frequency,
544 StringRefNull &active_info_name)
545{
546 if (assign_if_different(active_info_name, res.info_name)) {
547 os << "\n#define CREATE_INFO_RES_" << res_frequency << "_" << res.info_name << " \\\n";
548 }
549 print_resource(os, res, info);
550 os << " \\\n";
551}
552
553static void print_interface(std::ostream &os,
554 const StringRefNull &prefix,
555 const StageInterfaceInfo &iface,
556 const StringRefNull &suffix = "")
557{
558 os << prefix << " " << iface.name << "{" << std::endl;
559 for (const StageInterfaceInfo::InOut &inout : iface.inouts) {
560 os << " " << to_string(inout.interp) << " " << to_string(inout.type) << " " << inout.name
561 << ";\n";
562 }
563 os << "}";
564 os << (iface.instance_name.is_empty() ? "" : "\n") << iface.instance_name << suffix << ";\n";
565}
566
567std::string GLShader::resources_declare(const ShaderCreateInfo &info) const
568{
569 std::stringstream ss;
570
571 ss << "\n#line " << __LINE__ << " \"" << __FILE__ << "\"\n";
572
573 /* Compilation Constants (pass-through). */
574 for (const CompilationConstant &sc : info.compilation_constants_) {
575 ss << "const ";
576 switch (sc.type) {
577 case Type::int_t:
578 ss << "int " << sc.name << "=" << std::to_string(sc.value.i) << ";\n";
579 break;
580 case Type::uint_t:
581 ss << "uint " << sc.name << "=" << std::to_string(sc.value.u) << "u;\n";
582 break;
583 case Type::bool_t:
584 ss << "bool " << sc.name << "=" << (sc.value.u ? "true" : "false") << ";\n";
585 break;
586 default:
588 break;
589 }
590 }
591 {
592 StringRefNull active_info = "";
594 if (assign_if_different(active_info, sv.info_name)) {
595 ss << "\n#define CREATE_INFO_RES_SHARED_VARS_" << sv.info_name << " \\\n";
596 }
597 ss << "shared " << to_string(sv.type) << " " << sv.name << ";";
598 ss << " \\\n";
599 }
600 ss << "\n";
601 }
602 {
603 StringRefNull active_info = "";
604 for (const ShaderCreateInfo::Resource &res : info.pass_resources_) {
605 print_resource(ss, res, info, "PASS", active_info);
606 }
607 ss << "\n";
608 }
609 {
610 StringRefNull active_info = "";
611 for (const ShaderCreateInfo::Resource &res : info.batch_resources_) {
612 print_resource(ss, res, info, "BATCH", active_info);
613 }
614 ss << "\n";
615 }
616 {
617 StringRefNull active_info = "";
618 for (const ShaderCreateInfo::Resource &res : info.geometry_resources_) {
619 print_resource(ss, res, info, "GEOMETRY", active_info);
620 }
621 ss << "\n";
622 }
623 /* Push Constants. */
624 int location = 0;
626 /* See #131227: Work around legacy Intel bug when using layout locations. */
627 if (!info.specialization_constants_.is_empty()) {
628 ss << "layout(location = " << location << ") ";
629 location += std::max(1, uniform.array_size);
630 }
631 ss << "uniform " << to_string(uniform.type) << " " << uniform.name;
632 if (uniform.array_size > 0) {
633 ss << "[" << uniform.array_size << "]";
634 }
635 ss << ";\n";
636 }
637 ss << "\n";
638 return ss.str();
639}
640
642 const shader::SpecializationConstants &constants_state) const
643{
644 std::stringstream ss;
645
646 for (int constant_index : IndexRange(constants_state.types.size())) {
647 const StringRefNull name = specialization_constant_names_[constant_index];
648 gpu::shader::Type constant_type = constants_state.types[constant_index];
649 const SpecializationConstant::Value &value = constants_state.values[constant_index];
650
651 switch (constant_type) {
652 case Type::int_t:
653 ss << "const int " << name << "=" << std::to_string(value.i) << ";\n";
654 break;
655 case Type::uint_t:
656 ss << "const uint " << name << "=" << std::to_string(value.u) << "u;\n";
657 break;
658 case Type::bool_t:
659 ss << "const bool " << name << "=" << (value.u ? "true" : "false") << ";\n";
660 break;
661 case Type::float_t:
662 /* Use uint representation to allow exact same bit pattern even if NaN. */
663 ss << "const float " << name << "= uintBitsToFloat(" << std::to_string(value.u) << "u);\n";
664 break;
665 default:
667 break;
668 }
669 }
670 return ss.str();
671}
672
673static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
674{
675 std::stringstream ss;
676 /* Prototype for the original main. */
677 ss << "\n";
678 ss << "void main_function_();\n";
679 /* Wrapper to the main function in order to inject code processing on globals. */
680 ss << "void main() {\n";
681 ss << pre_main;
682 ss << " main_function_();\n";
683 ss << post_main;
684 ss << "}\n";
685 /* Rename the original main. */
686 ss << "#define main main_function_\n";
687 ss << "\n";
688 return ss.str();
689}
690
692{
693 std::stringstream ss;
694 std::string post_main;
695
696 /* Inputs. */
697 for (const ShaderCreateInfo::VertIn &attr : info.vertex_inputs_) {
698 ss << "layout(location = " << attr.index << ") ";
699 ss << "in " << to_string(attr.type) << " " << attr.name << ";\n";
700 }
701 /* Interfaces. */
702 for (const StageInterfaceInfo *iface : info.vertex_out_interfaces_) {
703 print_interface(ss, "out", *iface);
704 }
705 const bool has_geometry_stage = do_geometry_shader_injection(&info) ||
707 const bool do_layer_output = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
708 const bool do_viewport_output = flag_is_set(info.builtins_combined(),
710 if (has_geometry_stage) {
711 if (do_layer_output) {
712 ss << "out int gpu_Layer;\n";
713 }
714 if (do_viewport_output) {
715 ss << "out int gpu_ViewportIndex;\n";
716 }
717 }
718 else {
719 if (do_layer_output) {
720 ss << "#define gpu_Layer gl_Layer\n";
721 }
722 if (do_viewport_output) {
723 ss << "#define gpu_ViewportIndex gl_ViewportIndex\n";
724 }
725 }
726 if (flag_is_set(info.builtins_combined(), BuiltinBits::CLIP_CONTROL)) {
727 if (!has_geometry_stage) {
728 /* Assume clip range is set to 0..1 and remap the range just like Vulkan and Metal.
729 * If geometry stage is needed, do that remapping inside the geometry shader stage. */
730 post_main += "gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
731 }
732 }
733 if (flag_is_set(info.builtins_combined(), BuiltinBits::BARYCENTRIC_COORD)) {
735 /* Disabled or unsupported. */
736 }
737 else if (epoxy_has_gl_extension("GL_AMD_shader_explicit_vertex_parameter")) {
738 /* Need this for stable barycentric. */
739 ss << "flat out vec4 gpu_pos_flat;\n";
740 ss << "out vec4 gpu_pos;\n";
741
742 post_main += " gpu_pos = gpu_pos_flat = gl_Position;\n";
743 }
744 }
745 ss << "\n";
746
747 if (post_main.empty() == false) {
748 std::string pre_main;
749 ss << main_function_wrapper(pre_main, post_main);
750 }
751 return ss.str();
752}
753
755{
756 std::stringstream ss;
757 std::string pre_main, post_main;
758
759 /* Interfaces. */
761 info.geometry_source_.is_empty() ? info.vertex_out_interfaces_.as_span() :
762 info.geometry_out_interfaces_.as_span();
763
764 for (const StageInterfaceInfo *iface : in_interfaces) {
765 print_interface(ss, "in", *iface);
766 }
767 if (flag_is_set(info.builtins_combined(), BuiltinBits::LAYER)) {
768 ss << "#define gpu_Layer gl_Layer\n";
769 }
770 if (flag_is_set(info.builtins_combined(), BuiltinBits::VIEWPORT_INDEX)) {
771 ss << "#define gpu_ViewportIndex gl_ViewportIndex\n";
772 }
773 if (flag_is_set(info.builtins_combined(), BuiltinBits::BARYCENTRIC_COORD)) {
775 ss << "flat in vec4 gpu_pos[3];\n";
776 ss << "smooth in vec3 gpu_BaryCoord;\n";
777 ss << "noperspective in vec3 gpu_BaryCoordNoPersp;\n";
778 }
779 else if (epoxy_has_gl_extension("GL_AMD_shader_explicit_vertex_parameter")) {
780 /* NOTE(fclem): This won't work with geometry shader. Hopefully, we don't need geometry
781 * shader workaround if this extension/feature is detected. */
782 /* Stable Barycentric Coordinates. */
783 ss << "flat in vec4 gpu_pos_flat;\n";
784 ss << "__explicitInterpAMD in vec4 gpu_pos;\n";
785 /* Globals. */
786 ss << "vec3 gpu_BaryCoord;\n";
787 ss << "vec3 gpu_BaryCoordNoPersp;\n";
788 ss << "\n";
789 ss << "vec2 stable_bary_(vec2 in_bary) {\n";
790 ss << " vec3 bary = vec3(in_bary, 1.0 - in_bary.x - in_bary.y);\n";
791 ss << " if (interpolateAtVertexAMD(gpu_pos, 0) == gpu_pos_flat) { return bary.zxy; }\n";
792 ss << " if (interpolateAtVertexAMD(gpu_pos, 2) == gpu_pos_flat) { return bary.yzx; }\n";
793 ss << " return bary.xyz;\n";
794 ss << "}\n";
795 ss << "\n";
796
797 pre_main += " gpu_BaryCoord = stable_bary_(gl_BaryCoordSmoothAMD);\n";
798 pre_main += " gpu_BaryCoordNoPersp = stable_bary_(gl_BaryCoordNoPerspAMD);\n";
799 }
800 }
801 if (info.early_fragment_test_) {
802 ss << "layout(early_fragment_tests) in;\n";
803 }
804 ss << "layout(" << to_string(info.depth_write_) << ") out float gl_FragDepth;\n";
805
806 /* Sub-pass Inputs. */
807 for (const ShaderCreateInfo::SubpassIn &input : info.subpass_inputs_) {
809 /* Declare as inout but do not write to it. */
810 ss << "layout(location = " << std::to_string(input.index) << ") inout "
811 << to_string(input.type) << " " << input.name << ";\n";
812 }
813 else {
814 std::string image_name = "gpu_subpass_img_";
815 image_name += std::to_string(input.index);
816
817 /* Declare global for input. */
818 ss << to_string(input.type) << " " << input.name << ";\n";
819
820 /* IMPORTANT: We assume that the frame-buffer will be layered or not based on the layer
821 * built-in flag. */
822 bool is_layered_fb = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
823 bool is_layered_input = ELEM(
824 input.img_type, ImageType::Uint2DArray, ImageType::Int2DArray, ImageType::Float2DArray);
825
826 /* Declare image. */
827 using Resource = ShaderCreateInfo::Resource;
828 /* NOTE(fclem): Using the attachment index as resource index might be problematic as it might
829 * collide with other resources. */
830 Resource res(info, Resource::BindType::SAMPLER, input.index, nullptr);
831 res.sampler.type = input.img_type;
832 res.sampler.name = image_name;
833 print_resource(ss, res, info);
834
835 char swizzle[] = "xyzw";
836 swizzle[to_component_count(input.type)] = '\0';
837
838 std::string texel_co = (is_layered_input) ?
839 ((is_layered_fb) ? "ivec3(gl_FragCoord.xy, gpu_Layer)" :
840 /* This should fetch the attached layer.
841 * But this is not simple to set. For now
842 * assume it is always the first layer. */
843 "ivec3(gl_FragCoord.xy, 0)") :
844 "ivec2(gl_FragCoord.xy)";
845
846 std::stringstream ss_pre;
847 /* Populate the global before main using imageLoad. */
848 ss_pre << " " << input.name << " = texelFetch(" << image_name << ", " << texel_co << ", 0)."
849 << swizzle << ";\n";
850
851 pre_main += ss_pre.str();
852 }
853 }
854 /* Outputs. */
856 ss << "layout(location = " << output.index;
857 switch (output.blend) {
858 case DualBlend::SRC_0:
859 ss << ", index = 0";
860 break;
861 case DualBlend::SRC_1:
862 ss << ", index = 1";
863 break;
864 default:
865 break;
866 }
867 ss << ") ";
868 ss << "out " << to_string(output.type) << " " << output.name << ";\n";
869 }
870 ss << "\n";
871
872 if (!pre_main.empty() || !post_main.empty()) {
873 ss << main_function_wrapper(pre_main, post_main);
874 }
875 return ss.str();
876}
877
879{
880 int max_verts = info.geometry_layout_.max_vertices;
881 int invocations = info.geometry_layout_.invocations;
882
883 std::stringstream ss;
884 /* Geometry Layout. */
885 ss << "layout(" << to_string(info.geometry_layout_.primitive_in);
886 if (invocations != -1) {
887 ss << ", invocations = " << invocations;
888 }
889 ss << ") in;\n";
890
891 ss << "layout(" << to_string(info.geometry_layout_.primitive_out)
892 << ", max_vertices = " << max_verts << ") out;\n";
893 ss << "\n";
894 return ss.str();
895}
896
898{
899 std::stringstream ss;
900
901 /* Interfaces. */
902 for (const StageInterfaceInfo *iface : info.vertex_out_interfaces_) {
903 std::string suffix = "_in[]";
904 if (iface->instance_name.is_empty()) {
905 suffix = iface->name + suffix;
906 }
907 print_interface(ss, "in", *iface, suffix);
908 }
909 ss << "\n";
910 for (const StageInterfaceInfo *iface : info.geometry_out_interfaces_) {
911 std::string suffix = "_out";
912 if (iface->instance_name.is_empty()) {
913 suffix = iface->name + suffix;
914 }
915 print_interface(ss, "out", *iface, suffix);
916 }
917 ss << "\n";
918 return ss.str();
919}
920
922{
923 std::stringstream ss;
924 /* Compute Layout. */
925 ss << "layout(";
926 ss << " local_size_x = " << info.compute_layout_.local_size_x;
927 ss << ", local_size_y = " << info.compute_layout_.local_size_y;
928 ss << ", local_size_z = " << info.compute_layout_.local_size_z;
929 ss << ") in;\n";
930 ss << "\n";
931 return ss.str();
932}
933
934
935/* -------------------------------------------------------------------- */
939
940std::string GLShader::workaround_geometry_shader_source_create(
941 const shader::ShaderCreateInfo &info)
942{
943 std::stringstream ss;
944
945 const bool do_layer_output = flag_is_set(info.builtins_combined(), BuiltinBits::LAYER);
946 const bool do_viewport_output = flag_is_set(info.builtins_combined(),
948 const bool do_barycentric_workaround = !GLContext::native_barycentric_support &&
949 flag_is_set(info.builtins_combined(),
951
952 shader::ShaderCreateInfo info_modified = info;
953 info_modified.geometry_out_interfaces_ = info_modified.vertex_out_interfaces_;
959
960 ss << geometry_layout_declare(info_modified);
961 ss << geometry_interface_declare(info_modified);
962 if (do_layer_output) {
963 ss << "in int gpu_Layer[];\n";
964 }
965 if (do_viewport_output) {
966 ss << "in int gpu_ViewportIndex[];\n";
967 }
968
969 if (do_barycentric_workaround) {
970 ss << "flat out vec4 gpu_pos[3];\n";
971 ss << "smooth out vec3 gpu_BaryCoord;\n";
972 ss << "noperspective out vec3 gpu_BaryCoordNoPersp;\n";
973 }
974 ss << "\n";
975
976 ss << "void main()\n";
977 ss << "{\n";
978 if (do_barycentric_workaround) {
979 ss << " gpu_pos[0] = gl_in[0].gl_Position;\n";
980 ss << " gpu_pos[1] = gl_in[1].gl_Position;\n";
981 ss << " gpu_pos[2] = gl_in[2].gl_Position;\n";
982 }
983 for (auto i : IndexRange(3)) {
984 for (const StageInterfaceInfo *iface : info_modified.vertex_out_interfaces_) {
985 std::string instance_in = (iface->instance_name.is_empty() ? iface->name :
986 iface->instance_name) +
987 "_in";
988 std::string instance_out = (iface->instance_name.is_empty() ? iface->name :
989 iface->instance_name) +
990 "_out";
991 for (auto &inout : iface->inouts) {
992 ss << " " << instance_out << "." << inout.name;
993 ss << " = " << instance_in << "[" << i << "]." << inout.name << ";\n";
994 }
995 }
996 if (do_barycentric_workaround) {
997 ss << " gpu_BaryCoordNoPersp = gpu_BaryCoord =";
998 ss << " vec3(" << int(i == 0) << ", " << int(i == 1) << ", " << int(i == 2) << ");\n";
999 }
1000 ss << " gl_Position = gl_in[" << i << "].gl_Position;\n";
1001 if (flag_is_set(info.builtins_combined(), BuiltinBits::CLIP_CONTROL)) {
1002 /* Assume clip range is set to 0..1 and remap the range just like Vulkan and Metal. */
1003 ss << "gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
1004 }
1005 if (do_layer_output) {
1006 ss << " gl_Layer = gpu_Layer[" << i << "];\n";
1007 }
1008 if (do_viewport_output) {
1009 ss << " gl_ViewportIndex = gpu_ViewportIndex[" << i << "];\n";
1010 }
1011 ss << " EmitVertex();\n";
1012 }
1013 ss << "}\n";
1014 return ss.str();
1015}
1016
1017bool GLShader::do_geometry_shader_injection(const shader::ShaderCreateInfo *info) const
1018{
1019 BuiltinBits builtins = info->builtins_combined();
1021 flag_is_set(builtins, BuiltinBits::BARYCENTRIC_COORD))
1022 {
1023 return true;
1024 }
1026 flag_is_set(builtins, BuiltinBits::LAYER))
1027 {
1028 return true;
1029 }
1031 flag_is_set(builtins, BuiltinBits::VIEWPORT_INDEX))
1032 {
1033 return true;
1034 }
1035 return false;
1036}
1037
1039
1040/* -------------------------------------------------------------------- */
1043
1045{
1047 static std::string patch = []() {
1048 std::stringstream ss;
1049 /* Version need to go first. */
1050 ss << "#version 430\n";
1051
1052 /* Enable extensions for features that are not part of our base GLSL version
1053 * don't use an extension for something already available! */
1054 {
1055 /* Required extension. */
1056 ss << "#extension GL_ARB_shader_draw_parameters : enable\n";
1057 ss << "#define GPU_ARB_shader_draw_parameters\n";
1058 ss << "#define gpu_BaseInstance gl_BaseInstanceARB\n";
1059 ss << "#define GPU_ARB_clip_control\n";
1060 }
1062 ss << "#extension GL_ARB_shader_viewport_layer_array: enable\n";
1063 }
1065 ss << "#extension GL_AMD_vertex_shader_layer: enable\n";
1066 }
1068 ss << "#extension GL_AMD_vertex_shader_viewport_index: enable\n";
1069 }
1071 ss << "#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1072 }
1073
1074 /* Vulkan GLSL compatibility. */
1075 ss << "#define gpu_InstanceIndex (gl_InstanceID + gpu_BaseInstance)\n";
1076
1077 /* Array compatibility. */
1078 ss << "#define gpu_Array(_type) _type[]\n";
1079
1080 /* Needs to have this defined upfront for configuring shader defines. */
1081 ss << "#define GPU_VERTEX_SHADER\n";
1082
1083 shader::GeneratedSource extensions{"gpu_shader_glsl_extension.glsl", {}, ss.str()};
1084 shader::GeneratedSourceList sources{extensions};
1085 return fmt::to_string(fmt::join(
1086 gpu_shader_dependency_get_resolved_source("gpu_shader_compat_glsl.glsl", sources), ""));
1087 }();
1088 return patch;
1089}
1090
1092{
1094 static std::string patch = []() {
1095 std::stringstream ss;
1096 /* Version need to go first. */
1097 ss << "#version 430\n";
1098
1100 ss << "#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1101 }
1102 ss << "#define GPU_ARB_clip_control\n";
1103
1104 /* Array compatibility. */
1105 ss << "#define gpu_Array(_type) _type[]\n";
1106
1107 /* Needs to have this defined upfront for configuring shader defines. */
1108 ss << "#define GPU_GEOMETRY_SHADER\n";
1109
1110 shader::GeneratedSource extensions{"gpu_shader_glsl_extension.glsl", {}, ss.str()};
1111 shader::GeneratedSourceList sources{extensions};
1112 return fmt::to_string(fmt::join(
1113 gpu_shader_dependency_get_resolved_source("gpu_shader_compat_glsl.glsl", sources), ""));
1114 }();
1115 return patch;
1116}
1117
1119{
1121 static std::string patch = []() {
1122 std::stringstream ss;
1123 /* Version need to go first. */
1124 ss << "#version 430\n";
1125
1127 ss << "#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1128 }
1130 ss << "#extension GL_EXT_shader_framebuffer_fetch: enable\n";
1131 }
1133 ss << "#extension GL_ARB_derivative_control: enable\n";
1134 ss << "#define GPU_ARB_derivative_control\n";
1135 }
1137 ss << "#extension GL_ARB_shader_stencil_export: enable\n";
1138 ss << "#define GPU_ARB_shader_stencil_export\n";
1139 }
1140 ss << "#define GPU_ARB_clip_control\n";
1141
1142 /* Array compatibility. */
1143 ss << "#define gpu_Array(_type) _type[]\n";
1144
1145 /* Needs to have this defined upfront for configuring shader defines. */
1146 ss << "#define GPU_FRAGMENT_SHADER\n";
1147
1148 shader::GeneratedSource extensions{"gpu_shader_glsl_extension.glsl", {}, ss.str()};
1149 shader::GeneratedSourceList sources{extensions};
1150 return fmt::to_string(fmt::join(
1151 gpu_shader_dependency_get_resolved_source("gpu_shader_compat_glsl.glsl", sources), ""));
1152 }();
1153 return patch;
1154}
1155
1157{
1159 static std::string patch = []() {
1160 std::stringstream ss;
1161 /* Version need to go first. */
1162 ss << "#version 430\n";
1163
1164 /* Array compatibility. */
1165 ss << "#define gpu_Array(_type) _type[]\n";
1166
1167 /* Needs to have this defined upfront for configuring shader defines. */
1168 ss << "#define GPU_COMPUTE_SHADER\n";
1169
1170 ss << "#define GPU_ARB_clip_control\n";
1171
1172 shader::GeneratedSource extensions{"gpu_shader_glsl_extension.glsl", {}, ss.str()};
1173 shader::GeneratedSourceList sources{extensions};
1174 return fmt::to_string(fmt::join(
1175 gpu_shader_dependency_get_resolved_source("gpu_shader_compat_glsl.glsl", sources), ""));
1176 }();
1177 return patch;
1178}
1179
1180StringRefNull GLShader::glsl_patch_get(GLenum gl_stage)
1181{
1182 if (gl_stage == GL_VERTEX_SHADER) {
1183 return glsl_patch_vertex_get();
1184 }
1185 if (gl_stage == GL_GEOMETRY_SHADER) {
1186 return glsl_patch_geometry_get();
1187 }
1188 if (gl_stage == GL_FRAGMENT_SHADER) {
1189 return glsl_patch_fragment_get();
1190 }
1191 if (gl_stage == GL_COMPUTE_SHADER) {
1192 return glsl_patch_compute_get();
1193 }
1195 return "";
1196}
1197
1198static StringRefNull stage_name_get(GLenum gl_stage)
1199{
1200 switch (gl_stage) {
1201 case GL_VERTEX_SHADER:
1202 return "vertex";
1203 case GL_GEOMETRY_SHADER:
1204 return "geometry";
1205 case GL_FRAGMENT_SHADER:
1206 return "fragment";
1207 case GL_COMPUTE_SHADER:
1208 return "compute";
1209 }
1210 return "";
1211}
1212
1213GLuint GLShader::create_shader_stage(GLenum gl_stage,
1215 GLSources &gl_sources,
1216 const shader::SpecializationConstants &constants_state)
1217{
1218 /* Patch the shader sources to include specialization constants. */
1219 std::string constants_source;
1220 Vector<StringRefNull> recreated_sources;
1221 if (has_specialization_constants()) {
1222 constants_source = constants_declare(constants_state);
1223 if (sources.is_empty()) {
1224 recreated_sources = gl_sources.sources_get();
1225 sources = recreated_sources;
1226 }
1227 }
1228
1229 /* Patch the shader code using the first source slot. */
1230 sources[SOURCES_INDEX_VERSION] = glsl_patch_get(gl_stage);
1231 sources[SOURCES_INDEX_SPECIALIZATION_CONSTANTS] = constants_source;
1232
1233 if (is_codegen_only_) {
1234 gl_sources[SOURCES_INDEX_VERSION].source = std::string(sources[SOURCES_INDEX_VERSION]);
1235 gl_sources[SOURCES_INDEX_SPECIALIZATION_CONSTANTS].source = std::string(
1237 }
1238
1240 /* Store the generated source for printing in case the link fails. */
1241 StringRefNull source_type = stage_name_get(gl_stage);
1242
1243 debug_source += "\n\n----------" + source_type + "----------\n\n";
1244 for (StringRefNull source : sources) {
1245 debug_source.append(source);
1246 }
1247 }
1248
1249 if (is_codegen_only_) {
1250 /* Only build the sources. */
1251 return 0;
1252 }
1253
1254 GLuint shader = glCreateShader(gl_stage);
1255 if (shader == 0) {
1256 fprintf(stderr, "GLShader: Error: Could not create shader object.\n");
1257 return 0;
1258 }
1259
1260 std::string concat_source = fmt::to_string(fmt::join(sources, ""));
1261
1262 std::string full_name = this->name_get() + "_" + stage_name_get(gl_stage);
1263
1264 dump_source_to_disk(this->name_get(), full_name, ".glsl", concat_source);
1265 if (!this->skip_preprocessor) {
1266 concat_source = run_preprocessor(concat_source, G.debug & G_DEBUG_GPU_SHADER_NO_DCE);
1267 dump_source_to_disk(this->name_get(), full_name + ".expanded", ".glsl", concat_source);
1268 }
1269
1270 /* Patch line directives so that we can make error reporting consistent. */
1271 size_t start_pos = 0;
1272 while ((start_pos = concat_source.find("#line ", start_pos)) != std::string::npos) {
1273 concat_source[start_pos] = '/';
1274 concat_source[start_pos + 1] = '/';
1275 }
1276
1277 const char *str_ptr = concat_source.c_str();
1278 glShaderSource(shader, 1, &str_ptr, nullptr);
1279 glCompileShader(shader);
1280
1281 GLint status;
1282 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
1283 if (!status || (G.debug & G_DEBUG_GPU)) {
1284 char log[5000] = "";
1285 glGetShaderInfoLog(shader, sizeof(log), nullptr, log);
1286 if (log[0] != '\0') {
1287 GLLogParser parser;
1288 switch (gl_stage) {
1289 case GL_VERTEX_SHADER:
1290 this->print_log(sources, log, "VertShader", !status, &parser);
1291 break;
1292 case GL_GEOMETRY_SHADER:
1293 this->print_log(sources, log, "GeomShader", !status, &parser);
1294 break;
1295 case GL_FRAGMENT_SHADER:
1296 this->print_log(sources, log, "FragShader", !status, &parser);
1297 break;
1298 case GL_COMPUTE_SHADER:
1299 this->print_log(sources, log, "ComputeShader", !status, &parser);
1300 break;
1301 }
1302 }
1303 }
1304 if (!status) {
1305 glDeleteShader(shader);
1306 compilation_failed_ = true;
1307 return 0;
1308 }
1309
1310 debug::object_label(gl_stage, shader, name);
1311 return shader;
1312}
1313
1314void GLShader::update_program_and_sources(GLSources &stage_sources,
1315 MutableSpan<StringRefNull> sources)
1316{
1317 const bool store_sources = has_specialization_constants() || is_codegen_only_;
1318 if (store_sources && stage_sources.is_empty()) {
1319 stage_sources = sources;
1320 }
1321}
1322
1325{
1326 update_program_and_sources(vertex_sources_, sources);
1327 main_program_->vert_shader = create_shader_stage(
1328 GL_VERTEX_SHADER, sources, vertex_sources_, *constants);
1329}
1330
1333{
1334 update_program_and_sources(geometry_sources_, sources);
1335 main_program_->geom_shader = create_shader_stage(
1336 GL_GEOMETRY_SHADER, sources, geometry_sources_, *constants);
1337}
1338
1341{
1342 update_program_and_sources(fragment_sources_, sources);
1343 main_program_->frag_shader = create_shader_stage(
1344 GL_FRAGMENT_SHADER, sources, fragment_sources_, *constants);
1345}
1346
1349{
1350 update_program_and_sources(compute_sources_, sources);
1351 main_program_->compute_shader = create_shader_stage(
1352 GL_COMPUTE_SHADER, sources, compute_sources_, *constants);
1353}
1354
1356{
1357 if (compilation_failed_) {
1358 return false;
1359 }
1360
1361 if (info && do_geometry_shader_injection(info)) {
1362 std::string source = workaround_geometry_shader_source_create(*info);
1363 Vector<StringRefNull> sources;
1364 sources.append("version");
1365 sources.append("");
1366 sources.append(source);
1367 geometry_shader_from_glsl(*info, sources);
1368 }
1369
1370 if (is_codegen_only_) {
1371 return true;
1372 }
1373
1374 main_program_->program_link(name);
1375 return post_finalize(info);
1376}
1377
1379{
1380 GLuint program_id = main_program_->program_id;
1381 GLint status;
1382 glGetProgramiv(program_id, GL_LINK_STATUS, &status);
1383 if (!status) {
1384 char log[5000];
1385 glGetProgramInfoLog(program_id, sizeof(log), nullptr, log);
1387 print_log({debug_source}, log, "Linking", true, &parser);
1388 return false;
1389 }
1390
1391 /* Reset for specialization constants variations. */
1392 is_codegen_only_ = false;
1393
1394 if (info != nullptr) {
1395 interface = new GLShaderInterface(main_program_->program_id, *info);
1396 }
1397 else {
1398 interface = new GLShaderInterface(main_program_->program_id);
1399 }
1400
1401 return true;
1402}
1403
1405
1406/* -------------------------------------------------------------------- */
1409
1411{
1412 GLProgram &program = program_get(constants_state);
1413 glUseProgram(program.program_id);
1414}
1415
1417{
1418#ifndef NDEBUG
1419 glUseProgram(0);
1420#endif
1421}
1422
1424
1425/* -------------------------------------------------------------------- */
1428
1429void GLShader::uniform_float(int location, int comp_len, int array_size, const float *data)
1430{
1431 switch (comp_len) {
1432 case 1:
1433 glUniform1fv(location, array_size, data);
1434 break;
1435 case 2:
1436 glUniform2fv(location, array_size, data);
1437 break;
1438 case 3:
1439 glUniform3fv(location, array_size, data);
1440 break;
1441 case 4:
1442 glUniform4fv(location, array_size, data);
1443 break;
1444 case 9:
1445 glUniformMatrix3fv(location, array_size, 0, data);
1446 break;
1447 case 16:
1448 glUniformMatrix4fv(location, array_size, 0, data);
1449 break;
1450 default:
1451 BLI_assert(0);
1452 break;
1453 }
1454}
1455
1456void GLShader::uniform_int(int location, int comp_len, int array_size, const int *data)
1457{
1458 switch (comp_len) {
1459 case 1:
1460 glUniform1iv(location, array_size, data);
1461 break;
1462 case 2:
1463 glUniform2iv(location, array_size, data);
1464 break;
1465 case 3:
1466 glUniform3iv(location, array_size, data);
1467 break;
1468 case 4:
1469 glUniform4iv(location, array_size, data);
1470 break;
1471 default:
1472 BLI_assert(0);
1473 break;
1474 }
1475}
1476
1478
1479/* -------------------------------------------------------------------- */
1483{
1485 source = "";
1486 source_ref = other;
1487 }
1488 else {
1489 source = other;
1490 source_ref = std::nullopt;
1491 }
1492}
1493
1495{
1496 clear();
1497 reserve(other.size());
1498
1499 for (StringRefNull other_source : other) {
1500 /* Don't store empty string as compilers can optimize these away and result in pointing to a
1501 * string that isn't c-str compliant anymore. */
1502 if (other_source.is_empty()) {
1503 continue;
1504 }
1505 append(GLSource(other_source));
1506 }
1507
1508 return *this;
1509}
1510
1512{
1514 result.reserve(size());
1515
1516 for (const GLSource &source : *this) {
1517 if (source.source_ref) {
1518 result.append(*source.source_ref);
1519 }
1520 else {
1521 result.append(source.source);
1522 }
1523 }
1524 return result;
1525}
1526
1527std::string GLSources::to_string() const
1528{
1529 std::string result;
1530 for (const GLSource &source : *this) {
1531 if (source.source_ref) {
1532 result.append(*source.source_ref);
1533 }
1534 else {
1535 result.append(source.source);
1536 }
1537 }
1538 return result;
1539}
1540
1542{
1543 size_t result = 0;
1544 result += comp.empty() ? 0 : comp.size() + sizeof('\0');
1545 result += vert.empty() ? 0 : vert.size() + sizeof('\0');
1546 result += geom.empty() ? 0 : geom.size() + sizeof('\0');
1547 result += frag.empty() ? 0 : frag.size() + sizeof('\0');
1548 return result;
1549}
1550
1552
1553/* -------------------------------------------------------------------- */
1556
1557GLShader::GLProgram::~GLProgram()
1558{
1559 /* This can run from any thread even without a GLContext bound. */
1560 /* Invalid handles are silently ignored. */
1561 GLContext::shader_free(vert_shader);
1562 GLContext::shader_free(geom_shader);
1563 GLContext::shader_free(frag_shader);
1564 GLContext::shader_free(compute_shader);
1565 GLContext::program_free(program_id);
1566}
1567
1568void GLShader::GLProgram::program_link(StringRefNull shader_name)
1569{
1570 if (this->program_id == 0) {
1571 this->program_id = glCreateProgram();
1572 debug::object_label(GL_PROGRAM, this->program_id, shader_name.c_str());
1573 }
1574
1575 GLuint program_id = this->program_id;
1576
1577 if (this->vert_shader) {
1578 glAttachShader(program_id, this->vert_shader);
1579 }
1580 if (this->geom_shader) {
1581 glAttachShader(program_id, this->geom_shader);
1582 }
1583 if (this->frag_shader) {
1584 glAttachShader(program_id, this->frag_shader);
1585 }
1586 if (this->compute_shader) {
1587 glAttachShader(program_id, this->compute_shader);
1588 }
1589 glLinkProgram(program_id);
1590}
1591
1592GLShader::GLProgram &GLShader::program_get(const shader::SpecializationConstants *constants_state)
1593{
1594 BLI_assert(constants_state == nullptr || this->has_specialization_constants() == true);
1595
1596 if (constants_state == nullptr) {
1597 /* Early exit for shaders that doesn't use specialization constants. */
1598 BLI_assert(main_program_);
1599 return *main_program_;
1600 }
1601
1602 program_cache_mutex_.lock();
1603
1604 GLProgram &program = *program_cache_.lookup_or_add_cb(
1605 constants_state->values, []() { return std::make_unique<GLProgram>(); });
1606
1607 program_cache_mutex_.unlock();
1608
1609 /* Avoid two threads trying to specialize the same shader at the same time. */
1610 std::scoped_lock lock(program.compilation_mutex);
1611
1612 if (program.program_id != 0) {
1613 /* Specialization is already compiled. */
1614 return program;
1615 }
1616
1617 if (!vertex_sources_.is_empty()) {
1618 program.vert_shader = create_shader_stage(
1619 GL_VERTEX_SHADER, {}, vertex_sources_, *constants_state);
1620 }
1621 if (!geometry_sources_.is_empty()) {
1622 program.geom_shader = create_shader_stage(
1623 GL_GEOMETRY_SHADER, {}, geometry_sources_, *constants_state);
1624 }
1625 if (!fragment_sources_.is_empty()) {
1626 program.frag_shader = create_shader_stage(
1627 GL_FRAGMENT_SHADER, {}, fragment_sources_, *constants_state);
1628 }
1629 if (!compute_sources_.is_empty()) {
1630 program.compute_shader = create_shader_stage(
1631 GL_COMPUTE_SHADER, {}, compute_sources_, *constants_state);
1632 }
1633
1634 if (is_codegen_only_) {
1635 program.program_id = glCreateProgram();
1636 debug::object_label(GL_PROGRAM, program.program_id, name);
1637 return program;
1638 }
1639
1642
1643 program.program_link(name);
1644
1645 /* Ensure the specialization compiled correctly.
1646 * Specialization compilation should never fail, but adding this check seems to bypass an
1647 * internal Nvidia driver issue (See #142046). */
1648 GLint status;
1649 glGetProgramiv(program.program_id, GL_LINK_STATUS, &status);
1650 BLI_assert(status);
1651
1654
1655 return program;
1656}
1657
1659{
1661 result.comp = compute_sources_.to_string();
1662 result.vert = vertex_sources_.to_string();
1663 result.geom = geometry_sources_.to_string();
1664 result.frag = fragment_sources_.to_string();
1665 return result;
1666}
1667
1669
1670/* -------------------------------------------------------------------- */
1673
1675{
1676 dynamic_cast<GLShader *>(specialization.shader)->program_get(&specialization.constants);
1677}
1678
1680
1681#if BLI_SUBPROCESS_SUPPORT
1682
1683/* -------------------------------------------------------------------- */
1686
1687GLCompilerWorker::GLCompilerWorker()
1688{
1689 using namespace std::chrono;
1690 /* This function has to be thread-safe. */
1691 static std::atomic<size_t> g_pipe_id = 0;
1692 size_t pipe_id = g_pipe_id++;
1693
1694 /* Use a timestamp on top of the PID.
1695 * If a Blender session crashes without unlinking its shared memory, and the PID is reused, we
1696 * may run into a name collision otherwise. */
1697 static size_t time_id =
1698 duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
1699
1700 std::string name = "BLENDER_SHADER_COMPILER_" + std::to_string(getpid()) + "_" +
1701 std::to_string(time_id) + "_" + std::to_string(pipe_id);
1702
1703 shared_mem_ = std::make_unique<SharedMemory>(
1704 name, compilation_subprocess_shared_memory_size, true);
1705 start_semaphore_ = std::make_unique<SharedSemaphore>(name + "_START", false);
1706 end_semaphore_ = std::make_unique<SharedSemaphore>(name + "_END", false);
1707 close_semaphore_ = std::make_unique<SharedSemaphore>(name + "_CLOSE", false);
1708
1709 subprocess_.create({"--compilation-subprocess", name.c_str()});
1710}
1711
1712GLCompilerWorker::~GLCompilerWorker()
1713{
1714 close_semaphore_->increment();
1715 /* Flag start so the subprocess can reach the close semaphore. */
1716 start_semaphore_->increment();
1717}
1718
1719void GLCompilerWorker::compile(const GLSourcesBaked &sources)
1720{
1721 BLI_assert(state_ == AVAILABLE);
1722
1723 ShaderSourceHeader *shared_src = reinterpret_cast<ShaderSourceHeader *>(shared_mem_->get_data());
1724 char *next_src = shared_src->sources;
1725
1726 auto add_src = [&](const std::string &src) {
1727 if (!src.empty()) {
1728 const size_t src_size = src.size() + 1;
1729 memcpy(next_src, src.c_str(), src_size);
1730 next_src += src_size;
1731 }
1732 };
1733
1734 add_src(sources.comp);
1735 add_src(sources.vert);
1736 add_src(sources.geom);
1737 add_src(sources.frag);
1738
1739 BLI_assert(size_t(next_src) <= size_t(shared_src) + compilation_subprocess_shared_memory_size);
1740
1741 if (!sources.comp.empty()) {
1742 BLI_assert(sources.vert.empty() && sources.geom.empty() && sources.frag.empty());
1743 shared_src->type = ShaderSourceHeader::Type::COMPUTE;
1744 }
1745 else {
1746 BLI_assert(sources.comp.empty() && !sources.vert.empty() && !sources.frag.empty());
1747 shared_src->type = sources.geom.empty() ?
1748 ShaderSourceHeader::Type::GRAPHICS :
1749 ShaderSourceHeader::Type::GRAPHICS_WITH_GEOMETRY_STAGE;
1750 }
1751
1752 start_semaphore_->increment();
1753
1754 state_ = COMPILATION_REQUESTED;
1755 compilation_start = BLI_time_now_seconds();
1756}
1757
1758bool GLCompilerWorker::block_until_ready()
1759{
1760 BLI_assert(ELEM(state_, COMPILATION_REQUESTED, COMPILATION_READY));
1761 if (state_ == COMPILATION_READY) {
1762 return true;
1763 }
1764
1765 auto delete_cached_binary = [&]() {
1766 /* If the subprocess crashed when loading the binary,
1767 * its name should be stored in shared memory.
1768 * Delete it to prevent more crashes in the future. */
1769 char str_start[] = "SOURCE_HASH:";
1770 char *shared_mem = reinterpret_cast<char *>(shared_mem_->get_data());
1771 if (BLI_str_startswith(shared_mem, str_start)) {
1772 std::string path = GL_shader_cache_dir_get() + SEP_STR +
1773 std::string(shared_mem + sizeof(str_start) - 1);
1774 if (BLI_exists(path.c_str())) {
1775 BLI_delete(path.c_str(), false, false);
1776 }
1777 }
1778 };
1779
1780 while (!end_semaphore_->try_decrement(1000)) {
1781 if (is_lost()) {
1782 delete_cached_binary();
1783 return false;
1784 }
1785 }
1786
1787 state_ = COMPILATION_READY;
1788 return true;
1789}
1790
1791bool GLCompilerWorker::is_lost()
1792{
1793 /* Use a timeout for hanged processes. */
1794 float max_timeout_seconds = 30.0f;
1795 return !subprocess_.is_running() ||
1796 (state_ == COMPILATION_REQUESTED &&
1797 (BLI_time_now_seconds() - compilation_start) > max_timeout_seconds);
1798}
1799
1800bool GLCompilerWorker::load_program_binary(GLint program)
1801{
1802 if (!block_until_ready()) {
1803 return false;
1804 }
1805
1806 ShaderBinaryHeader *binary = static_cast<ShaderBinaryHeader *>(shared_mem_->get_data());
1807
1808 state_ = COMPILATION_FINISHED;
1809
1810 if (binary->size > 0) {
1811 GPU_debug_group_begin("Load Binary");
1812 glProgramBinary(program, binary->format, binary->data, binary->size);
1814 return true;
1815 }
1816
1817 return false;
1818}
1819
1820void GLCompilerWorker::release()
1821{
1822 state_ = AVAILABLE;
1823}
1824
1826
1827/* -------------------------------------------------------------------- */
1830
1831GLSubprocessShaderCompiler::~GLSubprocessShaderCompiler()
1832{
1833 /* Must be called before we destruct the GLCompilerWorkers. */
1834 destruct_compilation_worker();
1835
1836 for (GLCompilerWorker *worker : workers_) {
1837 delete worker;
1838 }
1839}
1840
1841GLCompilerWorker *GLSubprocessShaderCompiler::get_compiler_worker()
1842{
1843 auto new_worker = [&]() {
1844 GLCompilerWorker *result = new GLCompilerWorker();
1845 std::lock_guard lock(workers_mutex_);
1846 workers_.append(result);
1847 return result;
1848 };
1849
1850 static thread_local GLCompilerWorker *worker = new_worker();
1851
1852 if (worker->is_lost()) {
1853 std::cerr << "ERROR: Compilation subprocess lost\n";
1854 {
1855 std::lock_guard lock(workers_mutex_);
1856 workers_.remove_first_occurrence_and_reorder(worker);
1857 }
1858 delete worker;
1859 worker = new_worker();
1860 }
1861
1862 return worker;
1863}
1864
1865Shader *GLSubprocessShaderCompiler::compile_shader(const shader::ShaderCreateInfo &info)
1866{
1867 const_cast<ShaderCreateInfo *>(&info)->finalize();
1868 GLShader *shader = static_cast<GLShader *>(compile(info, true));
1869 GLSourcesBaked sources = shader->get_sources();
1870
1871 size_t required_size = sources.size();
1872 bool do_async_compilation = required_size <= sizeof(ShaderSourceHeader::sources);
1873 if (!do_async_compilation) {
1874 /* TODO: Can't reuse? */
1875 delete shader;
1876 return compile(info, false);
1877 }
1878
1879 GLCompilerWorker *worker = get_compiler_worker();
1880 worker->compile(sources);
1881
1882 GPU_debug_group_begin("Subprocess Compilation");
1883
1884 /* This path is always called for the default shader compilation. Not for specialization.
1885 * Use the default constant template. */
1886 const shader::SpecializationConstants &constants = GPU_shader_get_default_constant_state(shader);
1887
1888 if (!worker->load_program_binary(shader->program_cache_.lookup(constants.values)->program_id) ||
1889 !shader->post_finalize(&info))
1890 {
1891 /* Compilation failed, try to compile it locally. */
1892 delete shader;
1893 shader = nullptr;
1894 }
1895
1897
1898 worker->release();
1899
1900 if (!shader) {
1901 return compile(info, false);
1902 }
1903
1904 return shader;
1905}
1906
1907void GLSubprocessShaderCompiler::specialize_shader(const ShaderSpecialization &specialization)
1908{
1909 static std::mutex mutex;
1910
1911 GLShader *shader = static_cast<GLShader *>(specialization.shader);
1912
1913 auto program_get = [&]() -> GLShader::GLProgram * {
1914 if (shader->program_cache_.contains(specialization.constants.values)) {
1915 return shader->program_cache_.lookup(specialization.constants.values).get();
1916 }
1917 return nullptr;
1918 };
1919
1920 auto program_release = [&]() {
1921 /* Compilation failed, local compilation will be tried later on shader bind. */
1922 GLShader::GLProgram *program = program_get();
1923 glDeleteProgram(program->program_id);
1924 program->program_id = 0;
1925 };
1926
1927 GLSourcesBaked sources;
1928 {
1929 std::lock_guard lock(mutex);
1930
1931 if (program_get()) {
1932 /* Already compiled. */
1933 return;
1934 }
1935
1937 shader->is_codegen_only_ = true;
1938 shader->program_get(&specialization.constants);
1939 shader->is_codegen_only_ = false;
1940 sources = shader->get_sources();
1941
1942 size_t required_size = sources.size();
1943 bool do_async_compilation = required_size <= sizeof(ShaderSourceHeader::sources);
1944 if (!do_async_compilation) {
1945 program_release();
1946 return;
1947 }
1948 }
1949
1950 GPU_debug_group_begin("Subprocess Specialization");
1951
1952 GLCompilerWorker *worker = get_compiler_worker();
1953 worker->compile(sources);
1954 worker->block_until_ready();
1955
1956 std::lock_guard lock(mutex);
1957
1958 if (!worker->load_program_binary(program_get()->program_id)) {
1959 program_release();
1960 }
1961
1963
1964 worker->release();
1965}
1966
1968
1969#endif
1970
1971} // namespace blender
File and directory operations.
Platform independent time functions.
#define GPU_DEBUG_SHADER_SPECIALIZATION_GROUP
Definition GPU_debug.hh:71
volatile int lock
#define UNUSED_FUNCTION(x)
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define ELEM(...)
#define output
constexpr IndexRange()=default
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const char * c_str() const
constexpr bool is_empty() const
static bool layered_rendering_support
Definition gl_context.hh:66
static bool framebuffer_fetch_support
Definition gl_context.hh:61
static void shader_free(GLuint shader_id)
static bool vertex_shader_layer_support
Definition gl_context.hh:68
static GLContext * get()
static bool native_barycentric_support
Definition gl_context.hh:69
static void program_free(GLuint program_id)
static bool vertex_shader_viewport_index_support
Definition gl_context.hh:67
static bool derivative_control_support
Definition gl_context.hh:75
virtual void specialize_shader(const ShaderSpecialization &specialization) override
GLSourcesBaked get_sources()
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:691
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:567
std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:754
void vertex_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void compute_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:878
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
void uniform_float(int location, int comp_len, int array_size, const float *data) override
void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void init(const shader::ShaderCreateInfo &info, bool is_codegen_only) override
Definition gl_shader.cc:80
void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
bool post_finalize(const shader::ShaderCreateInfo *info=nullptr)
GLShader(const char *name)
Definition gl_shader.cc:64
void bind(const shader::SpecializationConstants *constants_state) override
void uniform_int(int location, int comp_len, int array_size, const int *data) override
std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:921
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:897
void unbind() override
std::string constants_declare(const shader::SpecializationConstants &constants_state) const
Definition gl_shader.cc:641
Vector< StringRefNull > sources_get() const
std::string to_string() const
GLSources & operator=(Span< StringRefNull > other)
virtual void specialize_shader(const ShaderSpecialization &)
virtual Shader * compile_shader(const shader::ShaderCreateInfo &info)
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
StringRefNull name_get() const
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
void print_log(Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
Shader(const char *name)
#define uniform(slot)
#define log
#define DEBUG_LOG_SHADER_SRC_ON_ERROR
#define SOURCES_INDEX_SPECIALIZATION_CONSTANTS
#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
Vector< shader::GeneratedSource, 0 > GeneratedSourceList
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_name)
BLI_INLINE int to_component_count(const Type &type)
StringRefNull gpu_shader_dependency_get_filename_from_source_string(const StringRef source_string)
Find the name of the file from which the given string was generated.
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
const char * full_name()
Definition messages.cc:638
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void print_interface(std::ostream &os, const StringRefNull &prefix, const StageInterfaceInfo &iface, const StringRefNull &suffix="")
Definition gl_shader.cc:553
static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type)
Definition gl_shader.cc:351
bool assign_if_different(T &old_value, T &&new_value)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
static StringRefNull glsl_patch_compute_get()
static StringRefNull stage_name_get(GLenum gl_stage)
static StringRefNull glsl_patch_geometry_get()
@ G_DEBUG_GPU_SHADER_NO_DCE
Global G
Definition blender.cc:57
static StringRefNull glsl_patch_fragment_get()
BMesh const char void * data
static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
Definition gl_shader.cc:673
static void print_resource(std::ostream &os, const ShaderCreateInfo::Resource &res, const ShaderCreateInfo &)
Definition gl_shader.cc:494
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void GPU_debug_group_end()
Definition gpu_debug.cc:34
static Type UNUSED_FUNCTION to_component_type(const Type &type)
Definition gl_shader.cc:186
static StringRefNull glsl_patch_vertex_get()
int bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static std::ostream & print_qualifier(std::ostream &os, const Qualifier &qualifiers)
Definition gl_shader.cc:480
double BLI_time_now_seconds()
Definition time.cc:30
bool GPU_stencil_export_support()
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
const gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(gpu::Shader *sh)
gpu::shader::SpecializationConstants constants
std::optional< StringRefNull > source_ref
Definition gl_shader.hh:40
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
void reserve(int new_size)
Definition vector.hh:78
#define SEP_STR
Definition unit.cc:41