21#include BLI_SYSTEM_PID_H
39#include <fmt/format.h>
40#include <fmt/ranges.h>
54# define pclose _pclose
57using namespace blender::gpu;
58using namespace blender::gpu::shader;
82 is_codegen_only_ = is_codegen_only;
86 specialization_constant_names_.append(constant.
name.
c_str());
91 main_program_ = program_cache_
93 []() { return std::make_unique<GLProgram>(); })
95 if (!main_program_->program_id) {
96 main_program_->program_id = glCreateProgram();
115 return "noperspective";
236 case TextureFormat::UINT_8_8_8_8:
238 case TextureFormat::SINT_8_8_8_8:
240 case TextureFormat::UNORM_8_8_8_8:
242 case TextureFormat::UINT_32_32_32_32:
244 case TextureFormat::SINT_32_32_32_32:
246 case TextureFormat::SFLOAT_32_32_32_32:
248 case TextureFormat::UINT_16_16_16_16:
250 case TextureFormat::SINT_16_16_16_16:
252 case TextureFormat::SFLOAT_16_16_16_16:
254 case TextureFormat::UNORM_16_16_16_16:
256 case TextureFormat::UINT_8_8:
258 case TextureFormat::SINT_8_8:
260 case TextureFormat::UNORM_8_8:
262 case TextureFormat::UINT_32_32:
264 case TextureFormat::SINT_32_32:
266 case TextureFormat::SFLOAT_32_32:
268 case TextureFormat::UINT_16_16:
270 case TextureFormat::SINT_16_16:
272 case TextureFormat::SFLOAT_16_16:
274 case TextureFormat::UNORM_16_16:
276 case TextureFormat::UINT_8:
278 case TextureFormat::SINT_8:
280 case TextureFormat::UNORM_8:
282 case TextureFormat::UINT_32:
284 case TextureFormat::SINT_32:
286 case TextureFormat::SFLOAT_32:
288 case TextureFormat::UINT_16:
290 case TextureFormat::SINT_16:
292 case TextureFormat::SFLOAT_16:
294 case TextureFormat::UNORM_16:
296 case TextureFormat::UFLOAT_11_11_10:
297 return "r11f_g11f_b10f";
298 case TextureFormat::UNORM_10_10_10_2:
313 return "lines_adjacency";
317 return "triangles_adjacency";
331 return "triangle_strip";
343 return "depth_greater";
347 return "depth_unchanged";
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:
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:
394 case ImageType::FloatBuffer:
395 case ImageType::IntBuffer:
396 case ImageType::UintBuffer:
399 case ImageType::Float1D:
400 case ImageType::Float1DArray:
401 case ImageType::Int1D:
402 case ImageType::Int1DArray:
403 case ImageType::Uint1D:
404 case ImageType::Uint1DArray:
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:
423 case ImageType::Float3D:
424 case ImageType::Int3D:
425 case ImageType::Uint3D:
426 case ImageType::AtomicInt3D:
427 case ImageType::AtomicUint3D:
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:
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:
468 case ImageType::Shadow2D:
469 case ImageType::Shadow2DArray:
470 case ImageType::ShadowCube:
471 case ImageType::ShadowCubeArray:
499 os <<
"layout(binding = " << res.
slot;
501 os <<
", " <<
to_string(res.image.format);
516 os << res.sampler.name <<
";";
522 os << res.image.name <<
";";
525 os <<
"uniform _" << res.uniformbuf.name.str_no_array() <<
" { ";
526 os << res.uniformbuf.type_name <<
" " << res.uniformbuf.name <<
"; };";
531 os << res.storagebuf.name.str_no_array() <<
" { ";
532 os << res.storagebuf.type_name <<
" " << res.storagebuf.name <<
"; };";
547 os <<
"\n#define CREATE_INFO_RES_" << res_frequency <<
"_" << res.
info_name <<
" \\\n";
558 os << prefix <<
" " << iface.
name <<
"{" << std::endl;
569 std::stringstream ss;
571 ss <<
"\n#line " << __LINE__ <<
" \"" << __FILE__ <<
"\"\n";
578 ss <<
"int " << sc.
name <<
"=" << std::to_string(sc.
value.i) <<
";\n";
581 ss <<
"uint " << sc.
name <<
"=" << std::to_string(sc.
value.u) <<
"u;\n";
584 ss <<
"bool " << sc.
name <<
"=" << (sc.
value.u ?
"true" :
"false") <<
";\n";
595 ss <<
"\n#define CREATE_INFO_RES_SHARED_VARS_" << sv.
info_name <<
" \\\n";
628 ss <<
"layout(location = " << location <<
") ";
629 location += std::max(1,
uniform.array_size);
633 ss <<
"[" <<
uniform.array_size <<
"]";
644 std::stringstream ss;
651 switch (constant_type) {
653 ss <<
"const int " <<
name <<
"=" << std::to_string(value.i) <<
";\n";
656 ss <<
"const uint " <<
name <<
"=" << std::to_string(value.u) <<
"u;\n";
659 ss <<
"const bool " <<
name <<
"=" << (value.u ?
"true" :
"false") <<
";\n";
663 ss <<
"const float " <<
name <<
"= uintBitsToFloat(" << std::to_string(value.u) <<
"u);\n";
675 std::stringstream ss;
678 ss <<
"void main_function_();\n";
680 ss <<
"void main() {\n";
682 ss <<
" main_function_();\n";
686 ss <<
"#define main main_function_\n";
693 std::stringstream ss;
694 std::string post_main;
698 ss <<
"layout(location = " << attr.
index <<
") ";
705 const bool has_geometry_stage = do_geometry_shader_injection(&info) ||
710 if (has_geometry_stage) {
711 if (do_layer_output) {
712 ss <<
"out int gpu_Layer;\n";
714 if (do_viewport_output) {
715 ss <<
"out int gpu_ViewportIndex;\n";
719 if (do_layer_output) {
720 ss <<
"#define gpu_Layer gl_Layer\n";
722 if (do_viewport_output) {
723 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
727 if (!has_geometry_stage) {
730 post_main +=
"gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
737 else if (epoxy_has_gl_extension(
"GL_AMD_shader_explicit_vertex_parameter")) {
739 ss <<
"flat out vec4 gpu_pos_flat;\n";
740 ss <<
"out vec4 gpu_pos;\n";
742 post_main +=
" gpu_pos = gpu_pos_flat = gl_Position;\n";
747 if (post_main.empty() ==
false) {
748 std::string pre_main;
756 std::stringstream ss;
757 std::string pre_main, post_main;
768 ss <<
"#define gpu_Layer gl_Layer\n";
771 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
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";
779 else if (epoxy_has_gl_extension(
"GL_AMD_shader_explicit_vertex_parameter")) {
783 ss <<
"flat in vec4 gpu_pos_flat;\n";
784 ss <<
"__explicitInterpAMD in vec4 gpu_pos;\n";
786 ss <<
"vec3 gpu_BaryCoord;\n";
787 ss <<
"vec3 gpu_BaryCoordNoPersp;\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";
797 pre_main +=
" gpu_BaryCoord = stable_bary_(gl_BaryCoordSmoothAMD);\n";
798 pre_main +=
" gpu_BaryCoordNoPersp = stable_bary_(gl_BaryCoordNoPerspAMD);\n";
802 ss <<
"layout(early_fragment_tests) in;\n";
810 ss <<
"layout(location = " << std::to_string(input.
index) <<
") inout "
814 std::string image_name =
"gpu_subpass_img_";
815 image_name += std::to_string(input.
index);
823 bool is_layered_input =
ELEM(
824 input.
img_type, ImageType::Uint2DArray, ImageType::Int2DArray, ImageType::Float2DArray);
830 Resource res(info, Resource::BindType::SAMPLER, input.
index,
nullptr);
832 res.sampler.name = image_name;
835 char swizzle[] =
"xyzw";
838 std::string texel_co = (is_layered_input) ?
839 ((is_layered_fb) ?
"ivec3(gl_FragCoord.xy, gpu_Layer)" :
843 "ivec3(gl_FragCoord.xy, 0)") :
844 "ivec2(gl_FragCoord.xy)";
846 std::stringstream ss_pre;
848 ss_pre <<
" " << input.
name <<
" = texelFetch(" << image_name <<
", " << texel_co <<
", 0)."
851 pre_main += ss_pre.str();
856 ss <<
"layout(location = " <<
output.index;
872 if (!pre_main.empty() || !post_main.empty()) {
883 std::stringstream ss;
886 if (invocations != -1) {
887 ss <<
", invocations = " << invocations;
892 <<
", max_vertices = " << max_verts <<
") out;\n";
899 std::stringstream ss;
903 std::string suffix =
"_in[]";
905 suffix = iface->
name + suffix;
911 std::string suffix =
"_out";
913 suffix = iface->
name + suffix;
923 std::stringstream ss;
940std::string GLShader::workaround_geometry_shader_source_create(
943 std::stringstream ss;
962 if (do_layer_output) {
963 ss <<
"in int gpu_Layer[];\n";
965 if (do_viewport_output) {
966 ss <<
"in int gpu_ViewportIndex[];\n";
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";
976 ss <<
"void main()\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";
992 ss <<
" " << instance_out <<
"." <<
inout.name;
993 ss <<
" = " << instance_in <<
"[" << i <<
"]." <<
inout.name <<
";\n";
996 if (do_barycentric_workaround) {
997 ss <<
" gpu_BaryCoordNoPersp = gpu_BaryCoord =";
998 ss <<
" vec3(" << int(i == 0) <<
", " << int(i == 1) <<
", " << int(i == 2) <<
");\n";
1000 ss <<
" gl_Position = gl_in[" << i <<
"].gl_Position;\n";
1003 ss <<
"gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
1005 if (do_layer_output) {
1006 ss <<
" gl_Layer = gpu_Layer[" << i <<
"];\n";
1008 if (do_viewport_output) {
1009 ss <<
" gl_ViewportIndex = gpu_ViewportIndex[" << i <<
"];\n";
1011 ss <<
" EmitVertex();\n";
1021 flag_is_set(builtins, BuiltinBits::BARYCENTRIC_COORD))
1026 flag_is_set(builtins, BuiltinBits::LAYER))
1031 flag_is_set(builtins, BuiltinBits::VIEWPORT_INDEX))
1047 static std::string patch = []() {
1048 std::stringstream ss;
1050 ss <<
"#version 430\n";
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";
1062 ss <<
"#extension GL_ARB_shader_viewport_layer_array: enable\n";
1065 ss <<
"#extension GL_AMD_vertex_shader_layer: enable\n";
1068 ss <<
"#extension GL_AMD_vertex_shader_viewport_index: enable\n";
1071 ss <<
"#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1075 ss <<
"#define gpu_InstanceIndex (gl_InstanceID + gpu_BaseInstance)\n";
1078 ss <<
"#define gpu_Array(_type) _type[]\n";
1081 ss <<
"#define GPU_VERTEX_SHADER\n";
1085 return fmt::to_string(fmt::join(
1094 static std::string patch = []() {
1095 std::stringstream ss;
1097 ss <<
"#version 430\n";
1100 ss <<
"#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1102 ss <<
"#define GPU_ARB_clip_control\n";
1105 ss <<
"#define gpu_Array(_type) _type[]\n";
1108 ss <<
"#define GPU_GEOMETRY_SHADER\n";
1112 return fmt::to_string(fmt::join(
1121 static std::string patch = []() {
1122 std::stringstream ss;
1124 ss <<
"#version 430\n";
1127 ss <<
"#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1130 ss <<
"#extension GL_EXT_shader_framebuffer_fetch: enable\n";
1133 ss <<
"#extension GL_ARB_derivative_control: enable\n";
1134 ss <<
"#define GPU_ARB_derivative_control\n";
1137 ss <<
"#extension GL_ARB_shader_stencil_export: enable\n";
1138 ss <<
"#define GPU_ARB_shader_stencil_export\n";
1140 ss <<
"#define GPU_ARB_clip_control\n";
1143 ss <<
"#define gpu_Array(_type) _type[]\n";
1146 ss <<
"#define GPU_FRAGMENT_SHADER\n";
1150 return fmt::to_string(fmt::join(
1159 static std::string patch = []() {
1160 std::stringstream ss;
1162 ss <<
"#version 430\n";
1165 ss <<
"#define gpu_Array(_type) _type[]\n";
1168 ss <<
"#define GPU_COMPUTE_SHADER\n";
1170 ss <<
"#define GPU_ARB_clip_control\n";
1174 return fmt::to_string(fmt::join(
1182 if (gl_stage == GL_VERTEX_SHADER) {
1185 if (gl_stage == GL_GEOMETRY_SHADER) {
1188 if (gl_stage == GL_FRAGMENT_SHADER) {
1191 if (gl_stage == GL_COMPUTE_SHADER) {
1201 case GL_VERTEX_SHADER:
1203 case GL_GEOMETRY_SHADER:
1205 case GL_FRAGMENT_SHADER:
1207 case GL_COMPUTE_SHADER:
1213GLuint GLShader::create_shader_stage(GLenum gl_stage,
1219 std::string constants_source;
1221 if (has_specialization_constants()) {
1225 sources = recreated_sources;
1233 if (is_codegen_only_) {
1243 debug_source +=
"\n\n----------" + source_type +
"----------\n\n";
1244 for (StringRefNull source : sources) {
1245 debug_source.append(source);
1249 if (is_codegen_only_) {
1254 GLuint shader = glCreateShader(gl_stage);
1256 fprintf(stderr,
"GLShader: Error: Could not create shader object.\n");
1260 std::string concat_source = fmt::to_string(fmt::join(sources,
""));
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] =
'/';
1277 const char *str_ptr = concat_source.c_str();
1278 glShaderSource(shader, 1, &str_ptr,
nullptr);
1279 glCompileShader(shader);
1282 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
1284 char log[5000] =
"";
1285 glGetShaderInfoLog(shader,
sizeof(
log),
nullptr,
log);
1286 if (
log[0] !=
'\0') {
1289 case GL_VERTEX_SHADER:
1290 this->
print_log(sources,
log,
"VertShader", !status, &parser);
1292 case GL_GEOMETRY_SHADER:
1293 this->
print_log(sources,
log,
"GeomShader", !status, &parser);
1295 case GL_FRAGMENT_SHADER:
1296 this->
print_log(sources,
log,
"FragShader", !status, &parser);
1298 case GL_COMPUTE_SHADER:
1299 this->
print_log(sources,
log,
"ComputeShader", !status, &parser);
1305 glDeleteShader(shader);
1306 compilation_failed_ =
true;
1314void GLShader::update_program_and_sources(
GLSources &stage_sources,
1315 MutableSpan<StringRefNull> sources)
1317 const bool store_sources = has_specialization_constants() || is_codegen_only_;
1318 if (store_sources && stage_sources.is_empty()) {
1319 stage_sources = sources;
1326 update_program_and_sources(vertex_sources_, sources);
1327 main_program_->vert_shader = create_shader_stage(
1328 GL_VERTEX_SHADER, sources, vertex_sources_, *
constants);
1334 update_program_and_sources(geometry_sources_, sources);
1335 main_program_->geom_shader = create_shader_stage(
1336 GL_GEOMETRY_SHADER, sources, geometry_sources_, *
constants);
1342 update_program_and_sources(fragment_sources_, sources);
1343 main_program_->frag_shader = create_shader_stage(
1344 GL_FRAGMENT_SHADER, sources, fragment_sources_, *
constants);
1350 update_program_and_sources(compute_sources_, sources);
1351 main_program_->compute_shader = create_shader_stage(
1352 GL_COMPUTE_SHADER, sources, compute_sources_, *
constants);
1357 if (compilation_failed_) {
1361 if (info && do_geometry_shader_injection(info)) {
1362 std::string source = workaround_geometry_shader_source_create(*info);
1364 sources.append(
"version");
1366 sources.append(source);
1370 if (is_codegen_only_) {
1374 main_program_->program_link(
name);
1380 GLuint program_id = main_program_->program_id;
1382 glGetProgramiv(program_id, GL_LINK_STATUS, &status);
1385 glGetProgramInfoLog(program_id,
sizeof(
log),
nullptr,
log);
1392 is_codegen_only_ =
false;
1394 if (info !=
nullptr) {
1412 GLProgram &program = program_get(constants_state);
1413 glUseProgram(program.program_id);
1433 glUniform1fv(location, array_size,
data);
1436 glUniform2fv(location, array_size,
data);
1439 glUniform3fv(location, array_size,
data);
1442 glUniform4fv(location, array_size,
data);
1445 glUniformMatrix3fv(location, array_size, 0,
data);
1448 glUniformMatrix4fv(location, array_size, 0,
data);
1460 glUniform1iv(location, array_size,
data);
1463 glUniform2iv(location, array_size,
data);
1466 glUniform3iv(location, array_size,
data);
1469 glUniform4iv(location, array_size,
data);
1502 if (other_source.is_empty()) {
1516 for (
const GLSource &source : *
this) {
1530 for (
const GLSource &source : *
this) {
1557GLShader::GLProgram::~GLProgram()
1568void GLShader::GLProgram::program_link(
StringRefNull shader_name)
1570 if (this->program_id == 0) {
1571 this->program_id = glCreateProgram();
1575 GLuint program_id = this->program_id;
1577 if (this->vert_shader) {
1578 glAttachShader(program_id, this->vert_shader);
1580 if (this->geom_shader) {
1581 glAttachShader(program_id, this->geom_shader);
1583 if (this->frag_shader) {
1584 glAttachShader(program_id, this->frag_shader);
1586 if (this->compute_shader) {
1587 glAttachShader(program_id, this->compute_shader);
1589 glLinkProgram(program_id);
1592GLShader::GLProgram &GLShader::program_get(
const shader::SpecializationConstants *constants_state)
1594 BLI_assert(constants_state ==
nullptr || this->has_specialization_constants() ==
true);
1596 if (constants_state ==
nullptr) {
1599 return *main_program_;
1602 program_cache_mutex_.lock();
1604 GLProgram &program = *program_cache_.lookup_or_add_cb(
1605 constants_state->
values, []() { return std::make_unique<GLProgram>(); });
1607 program_cache_mutex_.unlock();
1610 std::scoped_lock
lock(program.compilation_mutex);
1612 if (program.program_id != 0) {
1617 if (!vertex_sources_.is_empty()) {
1618 program.vert_shader = create_shader_stage(
1619 GL_VERTEX_SHADER, {}, vertex_sources_, *constants_state);
1621 if (!geometry_sources_.is_empty()) {
1622 program.geom_shader = create_shader_stage(
1623 GL_GEOMETRY_SHADER, {}, geometry_sources_, *constants_state);
1625 if (!fragment_sources_.is_empty()) {
1626 program.frag_shader = create_shader_stage(
1627 GL_FRAGMENT_SHADER, {}, fragment_sources_, *constants_state);
1629 if (!compute_sources_.is_empty()) {
1630 program.compute_shader = create_shader_stage(
1631 GL_COMPUTE_SHADER, {}, compute_sources_, *constants_state);
1634 if (is_codegen_only_) {
1635 program.program_id = glCreateProgram();
1643 program.program_link(
name);
1649 glGetProgramiv(program.program_id, GL_LINK_STATUS, &status);
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();
1681#if BLI_SUBPROCESS_SUPPORT
1687GLCompilerWorker::GLCompilerWorker()
1689 using namespace std::chrono;
1691 static std::atomic<size_t> g_pipe_id = 0;
1692 size_t pipe_id = g_pipe_id++;
1697 static size_t time_id =
1698 duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
1700 std::string
name =
"BLENDER_SHADER_COMPILER_" + std::to_string(getpid()) +
"_" +
1701 std::to_string(time_id) +
"_" + std::to_string(pipe_id);
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);
1709 subprocess_.create({
"--compilation-subprocess",
name.c_str()});
1712GLCompilerWorker::~GLCompilerWorker()
1714 close_semaphore_->increment();
1716 start_semaphore_->increment();
1719void GLCompilerWorker::compile(
const GLSourcesBaked &sources)
1723 ShaderSourceHeader *shared_src =
reinterpret_cast<ShaderSourceHeader *
>(shared_mem_->get_data());
1724 char *next_src = shared_src->sources;
1726 auto add_src = [&](
const std::string &src) {
1728 const size_t src_size = src.size() + 1;
1729 memcpy(next_src, src.c_str(), src_size);
1730 next_src += src_size;
1734 add_src(sources.
comp);
1735 add_src(sources.
vert);
1736 add_src(sources.
geom);
1737 add_src(sources.
frag);
1739 BLI_assert(
size_t(next_src) <=
size_t(shared_src) + compilation_subprocess_shared_memory_size);
1741 if (!sources.
comp.empty()) {
1743 shared_src->type = ShaderSourceHeader::Type::COMPUTE;
1747 shared_src->type = sources.
geom.empty() ?
1748 ShaderSourceHeader::Type::GRAPHICS :
1749 ShaderSourceHeader::Type::GRAPHICS_WITH_GEOMETRY_STAGE;
1752 start_semaphore_->increment();
1754 state_ = COMPILATION_REQUESTED;
1758bool GLCompilerWorker::block_until_ready()
1760 BLI_assert(
ELEM(state_, COMPILATION_REQUESTED, COMPILATION_READY));
1761 if (state_ == COMPILATION_READY) {
1765 auto delete_cached_binary = [&]() {
1769 char str_start[] =
"SOURCE_HASH:";
1770 char *shared_mem =
reinterpret_cast<char *
>(shared_mem_->get_data());
1772 std::string path = GL_shader_cache_dir_get() +
SEP_STR +
1773 std::string(shared_mem +
sizeof(str_start) - 1);
1780 while (!end_semaphore_->try_decrement(1000)) {
1782 delete_cached_binary();
1787 state_ = COMPILATION_READY;
1791bool GLCompilerWorker::is_lost()
1794 float max_timeout_seconds = 30.0f;
1795 return !subprocess_.is_running() ||
1796 (state_ == COMPILATION_REQUESTED &&
1800bool GLCompilerWorker::load_program_binary(GLint program)
1802 if (!block_until_ready()) {
1806 ShaderBinaryHeader *binary =
static_cast<ShaderBinaryHeader *
>(shared_mem_->get_data());
1808 state_ = COMPILATION_FINISHED;
1810 if (binary->size > 0) {
1812 glProgramBinary(program, binary->format, binary->data, binary->size);
1820void GLCompilerWorker::release()
1831GLSubprocessShaderCompiler::~GLSubprocessShaderCompiler()
1834 destruct_compilation_worker();
1836 for (GLCompilerWorker *worker : workers_) {
1841GLCompilerWorker *GLSubprocessShaderCompiler::get_compiler_worker()
1843 auto new_worker = [&]() {
1844 GLCompilerWorker *
result =
new GLCompilerWorker();
1845 std::lock_guard
lock(workers_mutex_);
1850 static thread_local GLCompilerWorker *worker = new_worker();
1852 if (worker->is_lost()) {
1853 std::cerr <<
"ERROR: Compilation subprocess lost\n";
1855 std::lock_guard
lock(workers_mutex_);
1856 workers_.remove_first_occurrence_and_reorder(worker);
1859 worker = new_worker();
1867 const_cast<ShaderCreateInfo *
>(&info)->
finalize();
1871 size_t required_size = sources.
size();
1872 bool do_async_compilation = required_size <=
sizeof(ShaderSourceHeader::sources);
1873 if (!do_async_compilation) {
1876 return compile(info,
false);
1879 GLCompilerWorker *worker = get_compiler_worker();
1880 worker->compile(sources);
1888 if (!worker->load_program_binary(shader->program_cache_.lookup(
constants.
values)->program_id) ||
1901 return compile(info,
false);
1909 static std::mutex mutex;
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();
1920 auto program_release = [&]() {
1922 GLShader::GLProgram *program = program_get();
1923 glDeleteProgram(program->program_id);
1924 program->program_id = 0;
1927 GLSourcesBaked sources;
1929 std::lock_guard
lock(mutex);
1931 if (program_get()) {
1937 shader->is_codegen_only_ =
true;
1938 shader->program_get(&specialization.
constants);
1939 shader->is_codegen_only_ =
false;
1942 size_t required_size = sources.
size();
1943 bool do_async_compilation = required_size <=
sizeof(ShaderSourceHeader::sources);
1944 if (!do_async_compilation) {
1952 GLCompilerWorker *worker = get_compiler_worker();
1953 worker->compile(sources);
1954 worker->block_until_ready();
1956 std::lock_guard
lock(mutex);
1958 if (!worker->load_program_binary(program_get()->program_id)) {
File and directory operations.
Platform independent time functions.
#define GPU_DEBUG_SHADER_SPECIALIZATION_GROUP
#define UNUSED_FUNCTION(x)
#define BLI_assert_unreachable()
constexpr IndexRange()=default
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * c_str() const
constexpr bool is_empty() const
static bool layered_rendering_support
static bool framebuffer_fetch_support
static void shader_free(GLuint shader_id)
static bool vertex_shader_layer_support
static bool native_barycentric_support
static void program_free(GLuint program_id)
static bool vertex_shader_viewport_index_support
static bool derivative_control_support
virtual void specialize_shader(const ShaderSpecialization &specialization) override
GLSourcesBaked get_sources()
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override
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
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
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)
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
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
std::string constants_declare(const shader::SpecializationConstants &constants_state) const
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)
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)
#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)
void object_label(GLenum type, GLuint object, const char *name)
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)
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="")
static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type)
bool assign_if_different(T &old_value, T &&new_value)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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
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)
static void print_resource(std::ostream &os, const ShaderCreateInfo::Resource &res, const ShaderCreateInfo &)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void GPU_debug_group_end()
static Type UNUSED_FUNCTION to_component_type(const Type &type)
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)
double BLI_time_now_seconds()
bool GPU_stencil_export_support()
void GPU_debug_group_begin(const char *name)
const gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(gpu::Shader *sh)
gpu::shader::SpecializationConstants constants
std::optional< StringRefNull > source_ref
SpecializationConstant::Value value
PrimitiveOut primitive_out
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< Resource, 0 > geometry_resources_
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_
bool early_fragment_test_
Vector< VertIn, 0 > vertex_inputs_
Vector< StageInterfaceInfoHandle, 0 > geometry_out_interfaces_
Vector< StageInterfaceInfoHandle, 0 > vertex_out_interfaces_
Vector< Resource, 0 > pass_resources_
Vector< PushConst, 0 > push_constants_
StringRefNull geometry_source_
GeometryStageLayout geometry_layout_
Vector< SubpassIn, 0 > subpass_inputs_
Vector< Resource, 0 > batch_resources_
Vector< SharedVariable, 0 > shared_variables_
ComputeStageLayout compute_layout_
Vector< FragOut, 0 > fragment_outputs_
BuiltinBits builtins_combined() const
Vector< SpecializationConstant::Value, 8 > values
Vector< gpu::shader::Type, 8 > types
StringRefNull instance_name
void reserve(int new_size)