19#include BLI_SYSTEM_PID_H
37# define pclose _pclose
68 async_compilation_ = is_batch_compilation;
72 specialization_constant_names_.
append(constant.name.c_str());
85 case Interpolation::SMOOTH:
87 case Interpolation::FLAT:
89 case Interpolation::NO_PERSPECTIVE:
90 return "noperspective";
154 case Type::VEC3_101010I2:
201 case Type::VEC3_101010I2:
272 return "r11f_g11f_b10f";
283 case PrimitiveIn::POINTS:
285 case PrimitiveIn::LINES:
287 case PrimitiveIn::LINES_ADJACENCY:
288 return "lines_adjacency";
289 case PrimitiveIn::TRIANGLES:
291 case PrimitiveIn::TRIANGLES_ADJACENCY:
292 return "triangles_adjacency";
301 case PrimitiveOut::POINTS:
303 case PrimitiveOut::LINE_STRIP:
305 case PrimitiveOut::TRIANGLE_STRIP:
306 return "triangle_strip";
315 case DepthWrite::ANY:
317 case DepthWrite::GREATER:
318 return "depth_greater";
319 case DepthWrite::LESS:
322 return "depth_unchanged";
331 case ImageType::INT_BUFFER:
332 case ImageType::INT_1D:
333 case ImageType::INT_1D_ARRAY:
334 case ImageType::INT_2D:
335 case ImageType::INT_2D_ARRAY:
336 case ImageType::INT_3D:
337 case ImageType::INT_CUBE:
338 case ImageType::INT_CUBE_ARRAY:
339 case ImageType::INT_2D_ATOMIC:
340 case ImageType::INT_2D_ARRAY_ATOMIC:
341 case ImageType::INT_3D_ATOMIC:
344 case ImageType::UINT_BUFFER:
345 case ImageType::UINT_1D:
346 case ImageType::UINT_1D_ARRAY:
347 case ImageType::UINT_2D:
348 case ImageType::UINT_2D_ARRAY:
349 case ImageType::UINT_3D:
350 case ImageType::UINT_CUBE:
351 case ImageType::UINT_CUBE_ARRAY:
352 case ImageType::UINT_2D_ATOMIC:
353 case ImageType::UINT_2D_ARRAY_ATOMIC:
354 case ImageType::UINT_3D_ATOMIC:
361 if (bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
369 case ImageType::FLOAT_BUFFER:
370 case ImageType::INT_BUFFER:
371 case ImageType::UINT_BUFFER:
374 case ImageType::FLOAT_1D:
375 case ImageType::FLOAT_1D_ARRAY:
376 case ImageType::INT_1D:
377 case ImageType::INT_1D_ARRAY:
378 case ImageType::UINT_1D:
379 case ImageType::UINT_1D_ARRAY:
382 case ImageType::FLOAT_2D:
383 case ImageType::FLOAT_2D_ARRAY:
384 case ImageType::INT_2D:
385 case ImageType::INT_2D_ARRAY:
386 case ImageType::INT_2D_ATOMIC:
387 case ImageType::INT_2D_ARRAY_ATOMIC:
388 case ImageType::UINT_2D:
389 case ImageType::UINT_2D_ARRAY:
390 case ImageType::UINT_2D_ATOMIC:
391 case ImageType::UINT_2D_ARRAY_ATOMIC:
392 case ImageType::SHADOW_2D:
393 case ImageType::SHADOW_2D_ARRAY:
394 case ImageType::DEPTH_2D:
395 case ImageType::DEPTH_2D_ARRAY:
398 case ImageType::FLOAT_3D:
399 case ImageType::INT_3D:
400 case ImageType::UINT_3D:
401 case ImageType::INT_3D_ATOMIC:
402 case ImageType::UINT_3D_ATOMIC:
405 case ImageType::FLOAT_CUBE:
406 case ImageType::FLOAT_CUBE_ARRAY:
407 case ImageType::INT_CUBE:
408 case ImageType::INT_CUBE_ARRAY:
409 case ImageType::UINT_CUBE:
410 case ImageType::UINT_CUBE_ARRAY:
411 case ImageType::SHADOW_CUBE:
412 case ImageType::SHADOW_CUBE_ARRAY:
413 case ImageType::DEPTH_CUBE:
414 case ImageType::DEPTH_CUBE_ARRAY:
422 case ImageType::FLOAT_1D_ARRAY:
423 case ImageType::FLOAT_2D_ARRAY:
424 case ImageType::FLOAT_CUBE_ARRAY:
425 case ImageType::INT_1D_ARRAY:
426 case ImageType::INT_2D_ARRAY:
427 case ImageType::INT_CUBE_ARRAY:
428 case ImageType::UINT_1D_ARRAY:
429 case ImageType::UINT_2D_ARRAY:
430 case ImageType::UINT_2D_ARRAY_ATOMIC:
431 case ImageType::UINT_CUBE_ARRAY:
432 case ImageType::SHADOW_2D_ARRAY:
433 case ImageType::SHADOW_CUBE_ARRAY:
434 case ImageType::DEPTH_2D_ARRAY:
435 case ImageType::DEPTH_CUBE_ARRAY:
443 case ImageType::SHADOW_2D:
444 case ImageType::SHADOW_2D_ARRAY:
445 case ImageType::SHADOW_CUBE:
446 case ImageType::SHADOW_CUBE_ARRAY:
457 if (
bool(qualifiers & Qualifier::NO_RESTRICT) ==
false) {
460 if (
bool(qualifiers & Qualifier::READ) ==
false) {
463 if (
bool(qualifiers & Qualifier::WRITE) ==
false) {
471 bool auto_resource_location)
473 if (auto_resource_location && res.
bind_type == ShaderCreateInfo::Resource::BindType::SAMPLER) {
479 os <<
"layout(binding = " << res.
slot;
480 if (res.
bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
483 else if (res.
bind_type == ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER) {
486 else if (res.
bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
491 else if (res.
bind_type == ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER) {
492 os <<
"layout(std140) ";
499 case ShaderCreateInfo::Resource::BindType::SAMPLER:
504 case ShaderCreateInfo::Resource::BindType::IMAGE:
510 case ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER:
511 array_offset = res.
uniformbuf.name.find_first_of(
"[");
512 name_no_array = (array_offset == -1) ? res.
uniformbuf.name :
514 os <<
"uniform " << name_no_array <<
" { " << res.
uniformbuf.type_name <<
" _"
517 case ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER:
518 array_offset = res.
storagebuf.name.find_first_of(
"[");
519 name_no_array = (array_offset == -1) ? res.
storagebuf.name :
535 case ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER:
536 array_offset = res.
uniformbuf.name.find_first_of(
"[");
537 name_no_array = (array_offset == -1) ? res.
uniformbuf.name :
539 os <<
"#define " << name_no_array <<
" (_" << name_no_array <<
")\n";
541 case ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER:
542 array_offset = res.
storagebuf.name.find_first_of(
"[");
543 name_no_array = (array_offset == -1) ? res.
storagebuf.name :
545 os <<
"#define " << name_no_array <<
" (_" << name_no_array <<
")\n";
563 os << prefix <<
" " << iface.
name <<
"{" << std::endl;
565 os <<
" " <<
to_string(inout.interp) <<
" " <<
to_string(inout.type) <<
" " << inout.name
574 std::stringstream ss;
579 ss <<
"\n/* Pass Resources. */\n";
586 ss <<
"\n/* Batch Resources. */\n";
593 ss <<
"\n/* Geometry Resources. */\n";
600 ss <<
"\n/* Push Constants. */\n";
602 ss <<
"uniform " <<
to_string(uniform.type) <<
" " << uniform.name;
603 if (uniform.array_size > 0) {
604 ss <<
"[" << uniform.array_size <<
"]";
611 ss <<
"#define " << uniform.name <<
"_ " << uniform.name <<
"\n";
612 ss <<
"#define " << uniform.name <<
" (" << uniform.name <<
"_)\n";
621 std::stringstream ss;
623 ss <<
"/* Specialization Constants. */\n";
625 const StringRefNull name = specialization_constant_names_[constant_index];
629 switch (constant_type) {
631 ss <<
"const int " << name <<
"=" << std::to_string(value.i) <<
";\n";
634 ss <<
"const uint " << name <<
"=" << std::to_string(value.u) <<
"u;\n";
637 ss <<
"const bool " << name <<
"=" << (value.u ?
"true" :
"false") <<
";\n";
641 ss <<
"const float " << name <<
"= uintBitsToFloat(" << std::to_string(value.u) <<
"u);\n";
653 std::stringstream ss;
656 ss <<
"void main_function_();\n";
658 ss <<
"void main() {\n";
660 ss <<
" main_function_();\n";
664 ss <<
"#define main main_function_\n";
671 std::stringstream ss;
672 std::string post_main;
674 ss <<
"\n/* Inputs. */\n";
680 ss <<
"layout(location = " << attr.index <<
") ";
682 ss <<
"in " <<
to_string(attr.type) <<
" " << attr.name <<
";\n";
684 ss <<
"\n/* Interfaces. */\n";
689 ss <<
"out int gpu_Layer;\n";
693 ss <<
"out int gpu_ViewportIndex;\n";
695 if (
bool(info.
builtins_ & BuiltinBits::BARYCENTRIC_COORD)) {
699 else if (epoxy_has_gl_extension(
"GL_AMD_shader_explicit_vertex_parameter")) {
701 ss <<
"flat out vec4 gpu_pos_flat;\n";
702 ss <<
"out vec4 gpu_pos;\n";
704 post_main +=
" gpu_pos = gpu_pos_flat = gl_Position;\n";
709 if (post_main.empty() ==
false) {
710 std::string pre_main;
718 std::stringstream ss;
719 std::string pre_main, post_main;
721 ss <<
"\n/* Interfaces. */\n";
729 ss <<
"#define gpu_Layer gl_Layer\n";
733 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
735 if (
bool(info.
builtins_ & BuiltinBits::BARYCENTRIC_COORD)) {
737 ss <<
"flat in vec4 gpu_pos[3];\n";
738 ss <<
"smooth in vec3 gpu_BaryCoord;\n";
739 ss <<
"noperspective in vec3 gpu_BaryCoordNoPersp;\n";
741 else if (epoxy_has_gl_extension(
"GL_AMD_shader_explicit_vertex_parameter")) {
744 ss <<
"\n/* Stable Barycentric Coordinates. */\n";
745 ss <<
"flat in vec4 gpu_pos_flat;\n";
746 ss <<
"__explicitInterpAMD in vec4 gpu_pos;\n";
748 ss <<
"vec3 gpu_BaryCoord;\n";
749 ss <<
"vec3 gpu_BaryCoordNoPersp;\n";
751 ss <<
"vec2 stable_bary_(vec2 in_bary) {\n";
752 ss <<
" vec3 bary = vec3(in_bary, 1.0 - in_bary.x - in_bary.y);\n";
753 ss <<
" if (interpolateAtVertexAMD(gpu_pos, 0) == gpu_pos_flat) { return bary.zxy; }\n";
754 ss <<
" if (interpolateAtVertexAMD(gpu_pos, 2) == gpu_pos_flat) { return bary.yzx; }\n";
755 ss <<
" return bary.xyz;\n";
759 pre_main +=
" gpu_BaryCoord = stable_bary_(gl_BaryCoordSmoothAMD);\n";
760 pre_main +=
" gpu_BaryCoordNoPersp = stable_bary_(gl_BaryCoordNoPerspAMD);\n";
764 ss <<
"layout(early_fragment_tests) in;\n";
768 ss <<
"\n/* Sub-pass Inputs. */\n";
772 ss <<
"layout(location = " << std::to_string(input.index) <<
") inout "
773 <<
to_string(input.type) <<
" " << input.name <<
";\n";
776 std::string image_name =
"gpu_subpass_img_";
777 image_name += std::to_string(input.index);
780 ss <<
to_string(input.type) <<
" " << input.name <<
";\n";
784 bool is_layered_fb = bool(info.
builtins_ & BuiltinBits::LAYER);
787 ImageType image_type = ImageType::FLOAT_BUFFER;
790 image_type = is_layered_fb ? ImageType::FLOAT_2D_ARRAY : ImageType::FLOAT_2D;
793 image_type = is_layered_fb ? ImageType::INT_2D_ARRAY : ImageType::INT_2D;
796 image_type = is_layered_fb ? ImageType::UINT_2D_ARRAY : ImageType::UINT_2D;
805 Resource res(Resource::BindType::SAMPLER, input.index);
806 res.sampler.type = image_type;
808 res.sampler.name = image_name;
811 char swizzle[] =
"xyzw";
814 std::string texel_co = (is_layered_fb) ?
"ivec3(gl_FragCoord.xy, gpu_Layer)" :
815 "ivec2(gl_FragCoord.xy)";
817 std::stringstream ss_pre;
819 ss_pre <<
" " << input.name <<
" = texelFetch(" << image_name <<
", " << texel_co <<
", 0)."
822 pre_main += ss_pre.str();
825 ss <<
"\n/* Outputs. */\n";
827 ss <<
"layout(location = " << output.index;
828 switch (output.blend) {
829 case DualBlend::SRC_0:
832 case DualBlend::SRC_1:
839 ss <<
"out " <<
to_string(output.type) <<
" " << output.name <<
";\n";
843 if (!pre_main.empty() || !post_main.empty()) {
854 std::stringstream ss;
855 ss <<
"\n/* Geometry Layout. */\n";
857 if (invocations != -1) {
858 ss <<
", invocations = " << invocations;
863 <<
", max_vertices = " << max_verts <<
") out;\n";
871 for (
auto *iface : ifaces) {
872 if (iface->instance_name == name) {
881 std::stringstream ss;
883 ss <<
"\n/* Interfaces. */\n";
886 iface->instance_name) !=
nullptr;
887 const char *suffix = (has_matching_output_iface) ?
"_in[]" :
"[]";
893 iface->instance_name) !=
nullptr;
894 const char *suffix = (has_matching_input_iface) ?
"_out" :
"";
903 std::stringstream ss;
904 ss <<
"\n/* Compute Layout. */\n";
923std::string GLShader::workaround_geometry_shader_source_create(
926 std::stringstream ss;
929 bool(info.
builtins_ & BuiltinBits::LAYER);
931 bool(info.
builtins_ & BuiltinBits::VIEWPORT_INDEX);
933 bool(info.
builtins_ & BuiltinBits::BARYCENTRIC_COORD);
941 info_modified.
geometry_layout(PrimitiveIn::TRIANGLES, PrimitiveOut::TRIANGLE_STRIP, 3);
945 if (do_layer_workaround) {
946 ss <<
"in int gpu_Layer[];\n";
948 if (do_viewport_workaround) {
949 ss <<
"in int gpu_ViewportIndex[];\n";
951 if (do_barycentric_workaround) {
952 ss <<
"flat out vec4 gpu_pos[3];\n";
953 ss <<
"smooth out vec3 gpu_BaryCoord;\n";
954 ss <<
"noperspective out vec3 gpu_BaryCoordNoPersp;\n";
958 ss <<
"void main()\n";
960 if (do_layer_workaround) {
961 ss <<
" gl_Layer = gpu_Layer[0];\n";
963 if (do_viewport_workaround) {
964 ss <<
" gl_ViewportIndex = gpu_ViewportIndex[0];\n";
966 if (do_barycentric_workaround) {
967 ss <<
" gpu_pos[0] = gl_in[0].gl_Position;\n";
968 ss <<
" gpu_pos[1] = gl_in[1].gl_Position;\n";
969 ss <<
" gpu_pos[2] = gl_in[2].gl_Position;\n";
973 for (
auto &inout : iface->inouts) {
974 ss <<
" " << iface->instance_name <<
"_out." << inout.name;
975 ss <<
" = " << iface->instance_name <<
"_in[" << i <<
"]." << inout.name <<
";\n";
978 if (do_barycentric_workaround) {
979 ss <<
" gpu_BaryCoordNoPersp = gpu_BaryCoord =";
980 ss <<
" vec3(" <<
int(i == 0) <<
", " <<
int(i == 1) <<
", " <<
int(i == 2) <<
");\n";
982 ss <<
" gl_Position = gl_in[" << i <<
"].gl_Position;\n";
983 ss <<
" EmitVertex();\n";
1013 static std::string patch;
1014 if (!patch.empty()) {
1015 return patch.c_str();
1018 std::stringstream ss;
1020 if (epoxy_gl_version() >= 43) {
1021 ss <<
"#version 430\n";
1024 ss <<
"#version 330\n";
1030 ss <<
"#extension GL_ARB_shader_draw_parameters : enable\n";
1031 ss <<
"#define GPU_ARB_shader_draw_parameters\n";
1032 ss <<
"#define gpu_BaseInstance gl_BaseInstanceARB\n";
1035 ss <<
"#extension GL_ARB_shader_viewport_layer_array: enable\n";
1036 ss <<
"#define gpu_Layer gl_Layer\n";
1037 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
1040 ss <<
"#extension GL_AMD_shader_explicit_vertex_parameter: enable\n";
1043 ss <<
"#extension GL_EXT_shader_framebuffer_fetch: enable\n";
1046 ss <<
"#extension GL_ARB_shader_stencil_export: enable\n";
1047 ss <<
"#define GPU_ARB_shader_stencil_export\n";
1052 ss <<
"uniform int gpu_BaseInstance;\n";
1056 ss <<
"#define gpu_InstanceIndex (gl_InstanceID + gpu_BaseInstance)\n";
1057 ss <<
"#define gpu_EmitVertex EmitVertex\n";
1060 ss <<
"#define gpu_Array(_type) _type[]\n";
1070 return patch.c_str();
1076 static std::string patch;
1077 if (!patch.empty()) {
1078 return patch.c_str();
1081 std::stringstream ss;
1083 ss <<
"#version 430\n";
1084 ss <<
"#extension GL_ARB_compute_shader :enable\n";
1087 ss <<
"#define gpu_Array(_type) _type[]\n";
1092 return patch.c_str();
1095const char *GLShader::glsl_patch_get(GLenum gl_stage)
1097 if (gl_stage == GL_COMPUTE_SHADER) {
1103GLuint GLShader::create_shader_stage(GLenum gl_stage,
1108 std::string constants_source;
1111 if (has_specialization_constants) {
1115 sources = recreated_sources;
1123 if (async_compilation_) {
1133 case GL_VERTEX_SHADER:
1134 source_type =
"VertShader";
1136 case GL_GEOMETRY_SHADER:
1137 source_type =
"GeomShader";
1139 case GL_FRAGMENT_SHADER:
1140 source_type =
"FragShader";
1142 case GL_COMPUTE_SHADER:
1143 source_type =
"ComputeShader";
1147 debug_source +=
"\n\n----------" + source_type +
"----------\n\n";
1148 for (
const char *source : sources) {
1149 debug_source.append(source);
1153 if (async_compilation_) {
1158 GLuint shader = glCreateShader(gl_stage);
1160 fprintf(stderr,
"GLShader: Error: Could not create shader object.\n");
1164 glShaderSource(shader, sources.
size(), sources.
data(),
nullptr);
1165 glCompileShader(shader);
1168 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
1170 char log[5000] =
"";
1171 glGetShaderInfoLog(shader,
sizeof(
log),
nullptr,
log);
1172 if (
log[0] !=
'\0') {
1175 case GL_VERTEX_SHADER:
1176 this->
print_log(sources,
log,
"VertShader", !status, &parser);
1178 case GL_GEOMETRY_SHADER:
1179 this->
print_log(sources,
log,
"GeomShader", !status, &parser);
1181 case GL_FRAGMENT_SHADER:
1182 this->
print_log(sources,
log,
"FragShader", !status, &parser);
1184 case GL_COMPUTE_SHADER:
1185 this->
print_log(sources,
log,
"ComputeShader", !status, &parser);
1191 glDeleteShader(shader);
1192 compilation_failed_ =
true;
1200void GLShader::update_program_and_sources(
GLSources &stage_sources,
1204 if (store_sources && stage_sources.
is_empty()) {
1205 stage_sources = sources;
1213 update_program_and_sources(vertex_sources_, sources);
1214 program_active_->vert_shader = this->create_shader_stage(
1215 GL_VERTEX_SHADER, sources, vertex_sources_);
1220 update_program_and_sources(geometry_sources_, sources);
1221 program_active_->geom_shader = this->create_shader_stage(
1222 GL_GEOMETRY_SHADER, sources, geometry_sources_);
1227 update_program_and_sources(fragment_sources_, sources);
1228 program_active_->frag_shader = this->create_shader_stage(
1229 GL_FRAGMENT_SHADER, sources, fragment_sources_);
1234 update_program_and_sources(compute_sources_, sources);
1235 program_active_->compute_shader = this->create_shader_stage(
1236 GL_COMPUTE_SHADER, sources, compute_sources_);
1241 if (compilation_failed_) {
1245 if (info && do_geometry_shader_injection(info)) {
1246 std::string source = workaround_geometry_shader_source_create(*info);
1248 sources.
append(
"version");
1249 sources.
append(
"/* Specialization Constants. */\n");
1250 sources.
append(source.c_str());
1254 if (async_compilation_) {
1264 if (!check_link_status()) {
1269 async_compilation_ =
false;
1271 GLuint program_id = program_get();
1290 GLuint program_id = program_get();
1291 glUseProgram(program_id);
1312 glTransformFeedbackVaryings(
1313 program_get(), name_list.
size(), name_list.
data(), GL_INTERLEAVED_ATTRIBS);
1314 transform_feedback_type_ = geom_type;
1325 if (buf->vbo_id_ == 0) {
1331 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buf->vbo_id_);
1333 switch (transform_feedback_type_) {
1335 glBeginTransformFeedback(GL_POINTS);
1338 glBeginTransformFeedback(GL_LINES);
1341 glBeginTransformFeedback(GL_TRIANGLES);
1351 glEndTransformFeedback();
1364 glUniform1fv(location, array_size, data);
1367 glUniform2fv(location, array_size, data);
1370 glUniform3fv(location, array_size, data);
1373 glUniform4fv(location, array_size, data);
1376 glUniformMatrix3fv(location, array_size, 0, data);
1379 glUniformMatrix4fv(location, array_size, 0, data);
1391 glUniform1iv(location, array_size, data);
1394 glUniform2iv(location, array_size, data);
1397 glUniform3iv(location, array_size, data);
1400 glUniform4iv(location, array_size, data);
1417 return program_active_->program_id;
1442 for (
const char *other_source : other) {
1445 if (other_source[0] ==
'\0') {
1459 for (
const GLSource &source : *
this) {
1460 if (source.source_ref) {
1461 result.append(source.source_ref);
1464 result.append(source.source.c_str());
1473 for (
const GLSource &source : *
this) {
1474 if (source.source_ref) {
1475 result.append(source.source_ref);
1478 result.append(source.source);
1487 result +=
comp.empty() ? 0 :
comp.size() +
sizeof(
'\0');
1488 result +=
vert.empty() ? 0 :
vert.size() +
sizeof(
'\0');
1489 result +=
geom.empty() ? 0 :
geom.size() +
sizeof(
'\0');
1490 result +=
frag.empty() ? 0 :
frag.size() +
sizeof(
'\0');
1500GLShader::GLProgram::~GLProgram()
1503 glDeleteShader(vert_shader);
1504 glDeleteShader(geom_shader);
1505 glDeleteShader(frag_shader);
1506 glDeleteShader(compute_shader);
1507 glDeleteProgram(program_id);
1510void GLShader::program_link()
1513 if (program_active_->program_id == 0) {
1514 program_active_->program_id = glCreateProgram();
1518 if (async_compilation_) {
1522 GLuint program_id = program_active_->program_id;
1524 if (program_active_->vert_shader) {
1525 glAttachShader(program_id, program_active_->vert_shader);
1527 if (program_active_->geom_shader) {
1528 glAttachShader(program_id, program_active_->geom_shader);
1530 if (program_active_->frag_shader) {
1531 glAttachShader(program_id, program_active_->frag_shader);
1533 if (program_active_->compute_shader) {
1534 glAttachShader(program_id, program_active_->compute_shader);
1536 glLinkProgram(program_id);
1539bool GLShader::check_link_status()
1541 GLuint program_id = program_active_->program_id;
1543 glGetProgramiv(program_id, GL_LINK_STATUS, &status);
1546 glGetProgramInfoLog(program_id,
sizeof(
log),
nullptr,
log);
1552 return bool(status);
1555void GLShader::init_program()
1557 if (program_active_) {
1561 program_active_ = &program_cache_.lookup_or_add_default(
constants.
values);
1562 if (!program_active_->program_id) {
1563 program_active_->program_id = glCreateProgram();
1568GLuint GLShader::program_get()
1573 BLI_assert(program_active_ && program_active_->program_id);
1574 return program_active_->program_id;
1579 BLI_assert(program_active_ && program_active_->program_id);
1580 return program_active_->program_id;
1583 program_active_ = &program_cache_.lookup_or_add_default(
constants.
values);
1584 if (!program_active_->program_id) {
1587 program_active_->vert_shader = create_shader_stage(
1588 GL_VERTEX_SHADER, no_sources, vertex_sources_);
1590 if (!geometry_sources_.
is_empty()) {
1591 program_active_->geom_shader = create_shader_stage(
1592 GL_GEOMETRY_SHADER, no_sources, geometry_sources_);
1594 if (!fragment_sources_.
is_empty()) {
1595 program_active_->frag_shader = create_shader_stage(
1596 GL_FRAGMENT_SHADER, no_sources, fragment_sources_);
1598 if (!compute_sources_.
is_empty()) {
1599 program_active_->compute_shader = create_shader_stage(
1600 GL_COMPUTE_SHADER, no_sources, compute_sources_);
1607 return program_active_->program_id;
1614 result.vert = vertex_sources_.
to_string();
1615 result.geom = geometry_sources_.
to_string();
1616 result.frag = fragment_sources_.
to_string();
1622#if BLI_SUBPROCESS_SUPPORT
1628GLCompilerWorker::GLCompilerWorker()
1630 static size_t pipe_id = 0;
1633 std::string name =
"BLENDER_SHADER_COMPILER_" + std::to_string(getpid()) +
"_" +
1634 std::to_string(pipe_id);
1636 shared_mem_ = std::make_unique<SharedMemory>(
1637 name, compilation_subprocess_shared_memory_size,
true);
1638 start_semaphore_ = std::make_unique<SharedSemaphore>(name +
"_START",
false);
1639 end_semaphore_ = std::make_unique<SharedSemaphore>(name +
"_END",
false);
1640 close_semaphore_ = std::make_unique<SharedSemaphore>(name +
"_CLOSE",
false);
1642 subprocess_.create({
"--compilation-subprocess", name.c_str()});
1645GLCompilerWorker::~GLCompilerWorker()
1647 close_semaphore_->increment();
1649 start_semaphore_->increment();
1656 ShaderSourceHeader *shared_src =
reinterpret_cast<ShaderSourceHeader *
>(shared_mem_->get_data());
1657 char *next_src = shared_src->sources;
1659 auto add_src = [&](
const std::string &src) {
1661 const size_t src_size = src.size() + 1;
1662 memcpy(next_src, src.c_str(), src_size);
1663 next_src += src_size;
1667 add_src(sources.
comp);
1668 add_src(sources.
vert);
1669 add_src(sources.
geom);
1670 add_src(sources.
frag);
1672 BLI_assert(
size_t(next_src) <=
size_t(shared_src) + compilation_subprocess_shared_memory_size);
1674 if (!sources.
comp.empty()) {
1676 shared_src->type = ShaderSourceHeader::Type::COMPUTE;
1680 shared_src->type = sources.
geom.empty() ?
1681 ShaderSourceHeader::Type::GRAPHICS :
1682 ShaderSourceHeader::Type::GRAPHICS_WITH_GEOMETRY_STAGE;
1685 start_semaphore_->increment();
1687 state_ = COMPILATION_REQUESTED;
1691bool GLCompilerWorker::is_ready()
1693 BLI_assert(
ELEM(state_, COMPILATION_REQUESTED, COMPILATION_READY));
1694 if (state_ == COMPILATION_READY) {
1698 if (end_semaphore_->try_decrement()) {
1699 state_ = COMPILATION_READY;
1702 return state_ == COMPILATION_READY;
1705bool GLCompilerWorker::is_lost()
1708 float max_timeout_seconds = 30.0f;
1709 return !subprocess_.is_running() ||
1713bool GLCompilerWorker::load_program_binary(GLint program)
1715 BLI_assert(
ELEM(state_, COMPILATION_REQUESTED, COMPILATION_READY));
1716 if (state_ == COMPILATION_REQUESTED) {
1717 end_semaphore_->decrement();
1718 state_ = COMPILATION_READY;
1721 ShaderBinaryHeader *binary = (ShaderBinaryHeader *)shared_mem_->get_data();
1723 state_ = COMPILATION_FINISHED;
1725 if (binary->size > 0) {
1726 glProgramBinary(program, binary->format, binary->data, binary->size);
1733void GLCompilerWorker::release()
1744GLShaderCompiler::~GLShaderCompiler()
1748 for (GLCompilerWorker *worker : workers_) {
1753GLCompilerWorker *GLShaderCompiler::get_compiler_worker(
const GLSourcesBaked &sources)
1755 GLCompilerWorker *result =
nullptr;
1756 for (GLCompilerWorker *compiler : workers_) {
1757 if (compiler->state_ == GLCompilerWorker::AVAILABLE) {
1763 result =
new GLCompilerWorker();
1764 workers_.append(result);
1767 result->compile(sources);
1772bool GLShaderCompiler::worker_is_lost(GLCompilerWorker *&worker)
1774 if (worker->is_lost()) {
1775 std::cerr <<
"ERROR: Compilation subprocess lost\n";
1776 workers_.remove_first_occurrence_and_reorder(worker);
1781 return worker ==
nullptr;
1788 std::scoped_lock
lock(mutex_);
1790 batches.add(handle, {});
1791 Batch &
batch = batches.lookup(handle);
1793 batch.is_ready =
false;
1797 batch.items.append({});
1798 CompilationWork &item =
batch.items.last();
1800 item.shader =
static_cast<GLShader *
>(compile(*info,
true));
1803 size_t required_size = item.sources.
size();
1804 item.do_async_compilation = required_size <=
sizeof(ShaderSourceHeader::sources);
1805 if (item.do_async_compilation) {
1806 item.worker = get_compiler_worker(item.sources);
1818 std::scoped_lock
lock(mutex_);
1821 Batch &
batch = batches.lookup(handle);
1822 if (
batch.is_ready) {
1826 batch.is_ready =
true;
1827 for (CompilationWork &item :
batch.items) {
1828 if (item.is_ready) {
1832 if (!item.do_async_compilation) {
1834 item.shader =
static_cast<GLShader *
>(compile(*item.info,
false));
1835 item.is_ready =
true;
1841 item.worker = get_compiler_worker(item.sources);
1843 else if (item.worker->is_ready()) {
1845 if (!item.worker->load_program_binary(item.shader->program_active_->program_id) ||
1846 !item.shader->post_finalize(item.info))
1850 item.shader =
nullptr;
1851 item.do_async_compilation =
false;
1854 item.is_ready =
true;
1856 item.worker->release();
1857 item.worker =
nullptr;
1859 else if (worker_is_lost(item.worker)) {
1862 item.shader =
nullptr;
1863 item.do_async_compilation =
false;
1866 if (!item.is_ready) {
1867 batch.is_ready =
false;
1871 return batch.is_ready;
1876 while (!batch_is_ready(handle)) {
1879 std::scoped_lock
lock(mutex_);
1882 Batch
batch = batches.pop(handle);
1884 for (CompilationWork &item :
batch.items) {
1885 result.
append(item.shader);
1896 std::scoped_lock
lock(mutex_);
1900 specialization_queue.append({handle, specializations});
1905GLShader::GLProgram *GLShaderCompiler::SpecializationWork::program_get()
1908 const ShaderInput *input = shader->interface->constant_get(constant.name.c_str());
1909 BLI_assert_msg(input !=
nullptr,
"The specialization constant doesn't exists");
1910 shader->constants.values[input->location].u = constant.value.u;
1912 shader->constants.is_dirty =
true;
1913 if (shader->program_cache_.contains(shader->constants.values)) {
1914 return &shader->program_cache_.lookup(shader->constants.values);
1919void GLShaderCompiler::prepare_next_specialization_batch()
1921 BLI_assert(current_specialization_batch.is_ready && !specialization_queue.is_empty());
1923 SpecializationRequest &
next = specialization_queue.first();
1924 SpecializationBatch &
batch = current_specialization_batch;
1926 batch.is_ready =
false;
1931 for (
auto &specialization :
next.specializations) {
1934 SpecializationWork &item = items.
last();
1936 item.
constants = specialization.constants;
1938 if (item.program_get()) {
1945 sh->async_compilation_ =
true;
1947 sh->async_compilation_ =
false;
1951 size_t required_size = item.sources.
size();
1952 item.do_async_compilation = required_size <=
sizeof(ShaderSourceHeader::sources);
1955 specialization_queue.remove(0);
1960 std::scoped_lock
lock(mutex_);
1962 SpecializationBatch &
batch = current_specialization_batch;
1964 if (handle <
batch.handle || (handle ==
batch.handle &&
batch.is_ready)) {
1969 if (
batch.is_ready) {
1970 prepare_next_specialization_batch();
1973 bool is_ready =
true;
1974 for (SpecializationWork &item :
batch.items) {
1975 if (item.is_ready) {
1979 if (!item.do_async_compilation) {
1980 GLShader::GLProgram *program = item.program_get();
1981 glDeleteProgram(program->program_id);
1982 program->program_id = 0;
1983 item.shader->constants.is_dirty =
true;
1984 item.is_ready =
true;
1988 if (item.worker ==
nullptr) {
1990 item.worker = get_compiler_worker(item.sources);
1992 else if (item.worker->is_ready()) {
1994 if (item.worker->load_program_binary(item.program_get()->program_id)) {
1995 item.is_ready =
true;
1999 item.do_async_compilation =
false;
2001 item.worker->release();
2002 item.worker =
nullptr;
2004 else if (worker_is_lost(item.worker)) {
2006 item.do_async_compilation =
false;
2009 if (!item.is_ready) {
2015 batch.is_ready =
true;
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
Platform independent time functions.
void BLI_time_sleep_ms(int ms)
double BLI_time_now_seconds(void)
bool GPU_use_parallel_compilation()
bool GPU_stencil_export_support()
@ GPU_SHADER_TFB_TRIANGLES
int64_t SpecializationBatchHandle
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr T * data() const
constexpr const T * data() const
constexpr int64_t size() const
constexpr bool is_empty() const
void append(const T &value)
const T & last(const int64_t n=0) const
void reserve(const int64_t min_capacity)
static bool layered_rendering_support
static bool framebuffer_fetch_support
static bool shader_draw_parameters_support
static bool explicit_location_support
static float derivative_signs[2]
static bool native_barycentric_support
std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
void fragment_shader_from_glsl(MutableSpan< const char * > sources) override
void uniform_float(int location, int comp_len, int array_size, const float *data) override
void transform_feedback_names_set(Span< const char * > name_list, eGPUShaderTFBType geom_type) override
bool post_finalize(const shader::ShaderCreateInfo *info=nullptr)
GLShader(const char *name)
GLSourcesBaked get_sources()
void compute_shader_from_glsl(MutableSpan< const char * > sources) override
std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override
std::string constants_declare() const
int program_handle_get() const override
void init(const shader::ShaderCreateInfo &info, bool is_batch_compilation) override
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
bool transform_feedback_enable(VertBuf *buf) override
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
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
void geometry_shader_from_glsl(MutableSpan< const char * > sources) override
void vertex_shader_from_glsl(MutableSpan< const char * > sources) override
void transform_feedback_disable() override
Vector< const char * > sources_get() const
std::string to_string() const
GLSources & operator=(Span< const char * > other)
virtual Vector< Shader * > batch_finalize(BatchHandle &handle)=0
virtual bool specialization_batch_is_ready(SpecializationBatchHandle &handle)
virtual bool batch_is_ready(BatchHandle handle)=0
virtual BatchHandle batch_compile(Span< const shader::ShaderCreateInfo * > &infos)=0
virtual SpecializationBatchHandle precompile_specializations(Span< ShaderSpecialization >)
struct blender::gpu::Shader::Constants constants
void print_log(Span< const char * > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
additional_info("compositor_sum_float_shared") .push_constant(Type additional_info("compositor_sum_float_shared") .push_constant(Type GPU_RGBA32F
DOF_TILES_FLATTEN_GROUP_SIZE coc_tx GPU_R11F_G11F_B10F
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
out_radiance out_gbuf_normal out_gbuf_closure2 GPU_RG16
SHADOW_TILEMAP_RES tiles_buf[] statistics_buf render_view_buf[SHADOW_VIEW_MAX] GPU_R32UI
RAYTRACE_GROUP_SIZE additional_info("eevee_shared", "eevee_gbuffer_data", "eevee_global_ubo", "eevee_sampling_data", "eevee_utility_texture", "eevee_hiz_data", "draw_view") .specialization_constant(Type RAYTRACE_GROUP_SIZE in_sh_0_tx in_sh_2_tx screen_normal_tx GPU_RGBA8
static const char * glsl_patch_compute_get()
static void print_resource_alias(std::ostream &os, const ShaderCreateInfo::Resource &res)
char datatoc_glsl_shader_defines_glsl[]
static const char * glsl_patch_default_get()
#define DEBUG_LOG_SHADER_SRC_ON_ERROR
#define SOURCES_INDEX_SPECIALIZATION_CONSTANTS
#define SOURCES_INDEX_VERSION
ccl_device_inline float2 interp(const float2 a, const float2 b, float t)
ccl_device_inline float3 log(float3 v)
void object_label(GLenum type, GLuint object, const char *name)
StringRefNull gpu_shader_dependency_get_filename_from_source_string(const StringRefNull source_string)
Find the name of the file from which the given string was generated.
BLI_INLINE int to_component_count(const Type &type)
static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type)
const char * to_string(ShaderStage stage)
static Context * unwrap(GPUContext *ctx)
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="")
static std::ostream & print_qualifier(std::ostream &os, const Qualifier &qualifiers)
static void print_resource(std::ostream &os, const ShaderCreateInfo::Resource &res)
static Type to_component_type(const Type &type)
static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
static constexpr GPUSamplerState default_sampler()
int max_parallel_compilations
Vector< gpu::shader::Type > types
PrimitiveOut primitive_out
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< StageInterfaceInfo * > vertex_out_interfaces_
Vector< SubpassIn > subpass_inputs_
Vector< Resource > geometry_resources_
Self & geometry_layout(PrimitiveIn prim_in, PrimitiveOut prim_out, int max_vertices, int invocations=-1)
Vector< VertIn > vertex_inputs_
bool early_fragment_test_
bool auto_resource_location_
Vector< Resource > batch_resources_
StringRefNull geometry_source_
Vector< StageInterfaceInfo * > geometry_out_interfaces_
Vector< Resource > pass_resources_
bool legacy_resource_location_
GeometryStageLayout geometry_layout_
ComputeStageLayout compute_layout_
Vector< SpecializationConstant > specialization_constants_
Vector< PushConst > push_constants_
Vector< FragOut > fragment_outputs_
StringRefNull instance_name