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