| 1 | /**************************************************************************/ |
| 2 | /* shader_gles3.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "shader_gles3.h" |
| 32 | |
| 33 | #ifdef GLES3_ENABLED |
| 34 | |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | |
| 38 | #include "drivers/gles3/rasterizer_gles3.h" |
| 39 | #include "drivers/gles3/storage/config.h" |
| 40 | |
| 41 | static String _mkid(const String &p_id) { |
| 42 | String id = "m_" + p_id.replace("__", "_dus_"); |
| 43 | return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl |
| 44 | } |
| 45 | |
| 46 | void ShaderGLES3::_add_stage(const char *p_code, StageType p_stage_type) { |
| 47 | Vector<String> lines = String::utf8(p_code).split("\n"); |
| 48 | |
| 49 | String text; |
| 50 | |
| 51 | for (int i = 0; i < lines.size(); i++) { |
| 52 | const String &l = lines[i]; |
| 53 | bool push_chunk = false; |
| 54 | |
| 55 | StageTemplate::Chunk chunk; |
| 56 | |
| 57 | if (l.begins_with("#GLOBALS")) { |
| 58 | switch (p_stage_type) { |
| 59 | case STAGE_TYPE_VERTEX: |
| 60 | chunk.type = StageTemplate::Chunk::TYPE_VERTEX_GLOBALS; |
| 61 | break; |
| 62 | case STAGE_TYPE_FRAGMENT: |
| 63 | chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS; |
| 64 | break; |
| 65 | default: { |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | push_chunk = true; |
| 70 | } else if (l.begins_with("#MATERIAL_UNIFORMS")) { |
| 71 | chunk.type = StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS; |
| 72 | push_chunk = true; |
| 73 | } else if (l.begins_with("#CODE")) { |
| 74 | chunk.type = StageTemplate::Chunk::TYPE_CODE; |
| 75 | push_chunk = true; |
| 76 | chunk.code = l.replace_first("#CODE", String()).remove_char(':').strip_edges().to_upper(); |
| 77 | } else { |
| 78 | text += l + "\n"; |
| 79 | } |
| 80 | |
| 81 | if (push_chunk) { |
| 82 | if (text != String()) { |
| 83 | StageTemplate::Chunk text_chunk; |
| 84 | text_chunk.type = StageTemplate::Chunk::TYPE_TEXT; |
| 85 | text_chunk.text = text.utf8(); |
| 86 | stage_templates[p_stage_type].chunks.push_back(text_chunk); |
| 87 | text = String(); |
| 88 | } |
| 89 | stage_templates[p_stage_type].chunks.push_back(chunk); |
| 90 | } |
| 91 | |
| 92 | if (text != String()) { |
| 93 | StageTemplate::Chunk text_chunk; |
| 94 | text_chunk.type = StageTemplate::Chunk::TYPE_TEXT; |
| 95 | text_chunk.text = text.utf8(); |
| 96 | stage_templates[p_stage_type].chunks.push_back(text_chunk); |
| 97 | text = String(); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void ShaderGLES3::_setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_name, int p_uniform_count, const char **p_uniform_names, int p_ubo_count, const UBOPair *p_ubos, int p_feedback_count, const Feedback *p_feedback, int p_texture_count, const TexUnitPair *p_tex_units, int p_specialization_count, const Specialization *p_specializations, int p_variant_count, const char **p_variants) { |
| 103 | name = p_name; |
| 104 | |
| 105 | if (p_vertex_code) { |
| 106 | _add_stage(p_vertex_code, STAGE_TYPE_VERTEX); |
| 107 | } |
| 108 | if (p_fragment_code) { |
| 109 | _add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT); |
| 110 | } |
| 111 | |
| 112 | uniform_names = p_uniform_names; |
| 113 | uniform_count = p_uniform_count; |
| 114 | ubo_pairs = p_ubos; |
| 115 | ubo_count = p_ubo_count; |
| 116 | texunit_pairs = p_tex_units; |
| 117 | texunit_pair_count = p_texture_count; |
| 118 | specializations = p_specializations; |
| 119 | specialization_count = p_specialization_count; |
| 120 | specialization_default_mask = 0; |
| 121 | for (int i = 0; i < specialization_count; i++) { |
| 122 | if (specializations[i].default_value) { |
| 123 | specialization_default_mask |= (uint64_t(1) << uint64_t(i)); |
| 124 | } |
| 125 | } |
| 126 | variant_defines = p_variants; |
| 127 | variant_count = p_variant_count; |
| 128 | feedbacks = p_feedback; |
| 129 | feedback_count = p_feedback_count; |
| 130 | |
| 131 | StringBuilder tohash; |
| 132 | tohash.append("[Vertex]"); |
| 133 | tohash.append(p_vertex_code ? String::utf8(p_vertex_code) : ""); |
| 134 | tohash.append("[Fragment]"); |
| 135 | tohash.append(p_fragment_code ? String::utf8(p_fragment_code) : ""); |
| 136 | |
| 137 | tohash.append("[gl_implementation]"); |
| 138 | const String &vendor = String::utf8((const char *)glGetString(GL_VENDOR)); |
| 139 | tohash.append(vendor.is_empty() ? "unknown" : vendor); |
| 140 | const String &renderer = String::utf8((const char *)glGetString(GL_RENDERER)); |
| 141 | tohash.append(renderer.is_empty() ? "unknown" : renderer); |
| 142 | const String &version = String::utf8((const char *)glGetString(GL_VERSION)); |
| 143 | tohash.append(version.is_empty() ? "unknown" : version); |
| 144 | |
| 145 | base_sha256 = tohash.as_string().sha256_text(); |
| 146 | } |
| 147 | |
| 148 | RID ShaderGLES3::version_create() { |
| 149 | //initialize() was never called |
| 150 | ERR_FAIL_COND_V(variant_count == 0, RID()); |
| 151 | |
| 152 | Version version; |
| 153 | return version_owner.make_rid(version); |
| 154 | } |
| 155 | |
| 156 | void ShaderGLES3::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization) { |
| 157 | if (RasterizerGLES3::is_gles_over_gl()) { |
| 158 | builder.append("#version 330\n"); |
| 159 | builder.append("#define USE_GLES_OVER_GL\n"); |
| 160 | } else { |
| 161 | builder.append("#version 300 es\n"); |
| 162 | } |
| 163 | |
| 164 | if (GLES3::Config::get_singleton()->polyfill_half2float) { |
| 165 | builder.append("#define USE_HALF2FLOAT\n"); |
| 166 | } |
| 167 | |
| 168 | for (int i = 0; i < specialization_count; i++) { |
| 169 | if (p_specialization & (uint64_t(1) << uint64_t(i))) { |
| 170 | builder.append("#define " + String(specializations[i].name) + "\n"); |
| 171 | } |
| 172 | } |
| 173 | if (p_version->uniforms.size()) { |
| 174 | builder.append("#define MATERIAL_UNIFORMS_USED\n"); |
| 175 | } |
| 176 | for (const KeyValue<StringName, CharString> &E : p_version->code_sections) { |
| 177 | builder.append(String("#define ") + String(E.key) + "_CODE_USED\n"); |
| 178 | } |
| 179 | |
| 180 | builder.append("\n"); //make sure defines begin at newline |
| 181 | builder.append(general_defines.get_data()); |
| 182 | builder.append(variant_defines[p_variant]); |
| 183 | builder.append("\n"); |
| 184 | for (int j = 0; j < p_version->custom_defines.size(); j++) { |
| 185 | builder.append(p_version->custom_defines[j].get_data()); |
| 186 | } |
| 187 | builder.append("\n"); //make sure defines begin at newline |
| 188 | |
| 189 | // Optional support for external textures. |
| 190 | if (GLES3::Config::get_singleton()->external_texture_supported) { |
| 191 | builder.append("#extension GL_OES_EGL_image_external : enable\n"); |
| 192 | builder.append("#extension GL_OES_EGL_image_external_essl3 : enable\n"); |
| 193 | } else { |
| 194 | builder.append("#define samplerExternalOES sampler2D\n"); |
| 195 | } |
| 196 | |
| 197 | // Insert multiview extension loading, because it needs to appear before |
| 198 | // any non-preprocessor code (like the "precision highp..." lines below). |
| 199 | builder.append("#ifdef USE_MULTIVIEW\n"); |
| 200 | builder.append("#if defined(GL_OVR_multiview2)\n"); |
| 201 | builder.append("#extension GL_OVR_multiview2 : require\n"); |
| 202 | builder.append("#elif defined(GL_OVR_multiview)\n"); |
| 203 | builder.append("#extension GL_OVR_multiview : require\n"); |
| 204 | builder.append("#endif\n"); |
| 205 | if (p_stage_type == StageType::STAGE_TYPE_VERTEX) { |
| 206 | builder.append("layout(num_views=2) in;\n"); |
| 207 | } |
| 208 | builder.append("#define ViewIndex gl_ViewID_OVR\n"); |
| 209 | builder.append("#define MAX_VIEWS 2\n"); |
| 210 | builder.append("#else\n"); |
| 211 | builder.append("#define ViewIndex uint(0)\n"); |
| 212 | builder.append("#define MAX_VIEWS 1\n"); |
| 213 | builder.append("#endif\n"); |
| 214 | |
| 215 | // Default to highp precision unless specified otherwise. |
| 216 | builder.append("precision highp float;\n"); |
| 217 | builder.append("precision highp int;\n"); |
| 218 | if (!RasterizerGLES3::is_gles_over_gl()) { |
| 219 | builder.append("precision highp sampler2D;\n"); |
| 220 | builder.append("precision highp samplerCube;\n"); |
| 221 | builder.append("precision highp sampler2DArray;\n"); |
| 222 | builder.append("precision highp sampler3D;\n"); |
| 223 | } |
| 224 | |
| 225 | const StageTemplate &stage_template = stage_templates[p_stage_type]; |
| 226 | for (uint32_t i = 0; i < stage_template.chunks.size(); i++) { |
| 227 | const StageTemplate::Chunk &chunk = stage_template.chunks[i]; |
| 228 | switch (chunk.type) { |
| 229 | case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: { |
| 230 | builder.append(String::utf8(p_version->uniforms.get_data())); //uniforms (same for vertex and fragment) |
| 231 | } break; |
| 232 | case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: { |
| 233 | builder.append(String::utf8(p_version->vertex_globals.get_data())); // vertex globals |
| 234 | } break; |
| 235 | case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: { |
| 236 | builder.append(String::utf8(p_version->fragment_globals.get_data())); // fragment globals |
| 237 | } break; |
| 238 | case StageTemplate::Chunk::TYPE_CODE: { |
| 239 | if (p_version->code_sections.has(chunk.code)) { |
| 240 | builder.append(String::utf8(p_version->code_sections[chunk.code].get_data())); |
| 241 | } |
| 242 | } break; |
| 243 | case StageTemplate::Chunk::TYPE_TEXT: { |
| 244 | builder.append(String::utf8(chunk.text.get_data())); |
| 245 | } break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static void _display_error_with_code(const String &p_error, const String &p_code) { |
| 251 | int line = 1; |
| 252 | Vector<String> lines = p_code.split("\n"); |
| 253 | |
| 254 | for (int j = 0; j < lines.size(); j++) { |
| 255 | print_line(itos(line) + ": " + lines[j]); |
| 256 | line++; |
| 257 | } |
| 258 | |
| 259 | ERR_PRINT(p_error); |
| 260 | } |
| 261 | |
| 262 | void ShaderGLES3::_get_uniform_locations(Version::Specialization &spec, Version *p_version) { |
| 263 | glUseProgram(spec.id); |
| 264 | |
| 265 | spec.uniform_location.resize(uniform_count); |
| 266 | for (int i = 0; i < uniform_count; i++) { |
| 267 | spec.uniform_location[i] = glGetUniformLocation(spec.id, uniform_names[i]); |
| 268 | } |
| 269 | |
| 270 | for (int i = 0; i < texunit_pair_count; i++) { |
| 271 | GLint loc = glGetUniformLocation(spec.id, texunit_pairs[i].name); |
| 272 | if (loc >= 0) { |
| 273 | if (texunit_pairs[i].index < 0) { |
| 274 | glUniform1i(loc, max_image_units + texunit_pairs[i].index); |
| 275 | } else { |
| 276 | glUniform1i(loc, texunit_pairs[i].index); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | for (int i = 0; i < ubo_count; i++) { |
| 282 | GLint loc = glGetUniformBlockIndex(spec.id, ubo_pairs[i].name); |
| 283 | if (loc >= 0) { |
| 284 | glUniformBlockBinding(spec.id, loc, ubo_pairs[i].index); |
| 285 | } |
| 286 | } |
| 287 | // textures |
| 288 | int texture_index = 0; |
| 289 | for (uint32_t i = 0; i < p_version->texture_uniforms.size(); i++) { |
| 290 | String native_uniform_name = _mkid(p_version->texture_uniforms[i].name); |
| 291 | GLint location = glGetUniformLocation(spec.id, (native_uniform_name).ascii().get_data()); |
| 292 | Vector<int32_t> texture_uniform_bindings; |
| 293 | int texture_count = p_version->texture_uniforms[i].array_size; |
| 294 | for (int j = 0; j < texture_count; j++) { |
| 295 | texture_uniform_bindings.append(texture_index + base_texture_index); |
| 296 | texture_index++; |
| 297 | } |
| 298 | glUniform1iv(location, texture_uniform_bindings.size(), texture_uniform_bindings.ptr()); |
| 299 | } |
| 300 | |
| 301 | glUseProgram(0); |
| 302 | } |
| 303 | |
| 304 | void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_t p_variant, Version *p_version, uint64_t p_specialization) { |
| 305 | spec.id = glCreateProgram(); |
| 306 | spec.ok = false; |
| 307 | GLint status; |
| 308 | |
| 309 | //vertex stage |
| 310 | { |
| 311 | StringBuilder builder; |
| 312 | _build_variant_code(builder, p_variant, p_version, STAGE_TYPE_VERTEX, p_specialization); |
| 313 | |
| 314 | spec.vert_id = glCreateShader(GL_VERTEX_SHADER); |
| 315 | String builder_string = builder.as_string(); |
| 316 | CharString cs = builder_string.utf8(); |
| 317 | const char *cstr = cs.ptr(); |
| 318 | GLint cstr_len = cs.length(); |
| 319 | glShaderSource(spec.vert_id, 1, &cstr, &cstr_len); |
| 320 | glCompileShader(spec.vert_id); |
| 321 | |
| 322 | glGetShaderiv(spec.vert_id, GL_COMPILE_STATUS, &status); |
| 323 | if (status == GL_FALSE) { |
| 324 | GLsizei iloglen; |
| 325 | glGetShaderiv(spec.vert_id, GL_INFO_LOG_LENGTH, &iloglen); |
| 326 | |
| 327 | if (iloglen < 0) { |
| 328 | glDeleteShader(spec.vert_id); |
| 329 | glDeleteProgram(spec.id); |
| 330 | spec.id = 0; |
| 331 | |
| 332 | ERR_PRINT("No OpenGL vertex shader compiler log."); |
| 333 | } else { |
| 334 | if (iloglen == 0) { |
| 335 | iloglen = 4096; // buggy driver (Adreno 220+) |
| 336 | } |
| 337 | |
| 338 | char *ilogmem = (char *)Memory::alloc_static_zeroed(iloglen + 1); |
| 339 | glGetShaderInfoLog(spec.vert_id, iloglen, &iloglen, ilogmem); |
| 340 | |
| 341 | String err_string = name + ": Vertex shader compilation failed:\n"; |
| 342 | |
| 343 | err_string += ilogmem; |
| 344 | |
| 345 | _display_error_with_code(err_string, builder_string); |
| 346 | |
| 347 | Memory::free_static(ilogmem); |
| 348 | glDeleteShader(spec.vert_id); |
| 349 | glDeleteProgram(spec.id); |
| 350 | spec.id = 0; |
| 351 | } |
| 352 | |
| 353 | ERR_FAIL(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | //fragment stage |
| 358 | { |
| 359 | StringBuilder builder; |
| 360 | _build_variant_code(builder, p_variant, p_version, STAGE_TYPE_FRAGMENT, p_specialization); |
| 361 | |
| 362 | spec.frag_id = glCreateShader(GL_FRAGMENT_SHADER); |
| 363 | String builder_string = builder.as_string(); |
| 364 | CharString cs = builder_string.utf8(); |
| 365 | const char *cstr = cs.ptr(); |
| 366 | GLint cstr_len = cs.length(); |
| 367 | glShaderSource(spec.frag_id, 1, &cstr, &cstr_len); |
| 368 | glCompileShader(spec.frag_id); |
| 369 | |
| 370 | glGetShaderiv(spec.frag_id, GL_COMPILE_STATUS, &status); |
| 371 | if (status == GL_FALSE) { |
| 372 | GLsizei iloglen; |
| 373 | glGetShaderiv(spec.frag_id, GL_INFO_LOG_LENGTH, &iloglen); |
| 374 | |
| 375 | if (iloglen < 0) { |
| 376 | glDeleteShader(spec.frag_id); |
| 377 | glDeleteProgram(spec.id); |
| 378 | spec.id = 0; |
| 379 | |
| 380 | ERR_PRINT("No OpenGL fragment shader compiler log."); |
| 381 | } else { |
| 382 | if (iloglen == 0) { |
| 383 | iloglen = 4096; // buggy driver (Adreno 220+) |
| 384 | } |
| 385 | |
| 386 | char *ilogmem = (char *)Memory::alloc_static_zeroed(iloglen + 1); |
| 387 | glGetShaderInfoLog(spec.frag_id, iloglen, &iloglen, ilogmem); |
| 388 | |
| 389 | String err_string = name + ": Fragment shader compilation failed:\n"; |
| 390 | |
| 391 | err_string += ilogmem; |
| 392 | |
| 393 | _display_error_with_code(err_string, builder_string); |
| 394 | |
| 395 | Memory::free_static(ilogmem); |
| 396 | glDeleteShader(spec.frag_id); |
| 397 | glDeleteProgram(spec.id); |
| 398 | spec.id = 0; |
| 399 | } |
| 400 | |
| 401 | ERR_FAIL(); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | glAttachShader(spec.id, spec.frag_id); |
| 406 | glAttachShader(spec.id, spec.vert_id); |
| 407 | |
| 408 | // If feedback exists, set it up. |
| 409 | |
| 410 | if (feedback_count) { |
| 411 | Vector<const char *> feedback; |
| 412 | for (int i = 0; i < feedback_count; i++) { |
| 413 | if (feedbacks[i].specialization == 0 || (feedbacks[i].specialization & p_specialization)) { |
| 414 | // Specialization for this feedback is enabled |
| 415 | feedback.push_back(feedbacks[i].name); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (feedback.size()) { |
| 420 | glTransformFeedbackVaryings(spec.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | glLinkProgram(spec.id); |
| 425 | |
| 426 | glGetProgramiv(spec.id, GL_LINK_STATUS, &status); |
| 427 | if (status == GL_FALSE) { |
| 428 | GLsizei iloglen; |
| 429 | glGetProgramiv(spec.id, GL_INFO_LOG_LENGTH, &iloglen); |
| 430 | |
| 431 | if (iloglen < 0) { |
| 432 | glDeleteShader(spec.frag_id); |
| 433 | glDeleteShader(spec.vert_id); |
| 434 | glDeleteProgram(spec.id); |
| 435 | spec.id = 0; |
| 436 | |
| 437 | ERR_PRINT("No OpenGL program link log. Something is wrong."); |
| 438 | ERR_FAIL(); |
| 439 | } |
| 440 | |
| 441 | if (iloglen == 0) { |
| 442 | iloglen = 4096; // buggy driver (Adreno 220+) |
| 443 | } |
| 444 | |
| 445 | char *ilogmem = (char *)Memory::alloc_static(iloglen + 1); |
| 446 | ilogmem[iloglen] = '\0'; |
| 447 | glGetProgramInfoLog(spec.id, iloglen, &iloglen, ilogmem); |
| 448 | |
| 449 | String err_string = name + ": Program linking failed:\n"; |
| 450 | |
| 451 | err_string += ilogmem; |
| 452 | |
| 453 | _display_error_with_code(err_string, String()); |
| 454 | |
| 455 | Memory::free_static(ilogmem); |
| 456 | glDeleteShader(spec.frag_id); |
| 457 | glDeleteShader(spec.vert_id); |
| 458 | glDeleteProgram(spec.id); |
| 459 | spec.id = 0; |
| 460 | |
| 461 | ERR_FAIL(); |
| 462 | } |
| 463 | |
| 464 | _get_uniform_locations(spec, p_version); |
| 465 | |
| 466 | spec.ok = true; |
| 467 | } |
| 468 | |
| 469 | RS::ShaderNativeSourceCode ShaderGLES3::version_get_native_source_code(RID p_version) { |
| 470 | Version *version = version_owner.get_or_null(p_version); |
| 471 | RS::ShaderNativeSourceCode source_code; |
| 472 | ERR_FAIL_NULL_V(version, source_code); |
| 473 | |
| 474 | source_code.versions.resize(variant_count); |
| 475 | |
| 476 | for (int i = 0; i < source_code.versions.size(); i++) { |
| 477 | //vertex stage |
| 478 | |
| 479 | { |
| 480 | StringBuilder builder; |
| 481 | _build_variant_code(builder, i, version, STAGE_TYPE_VERTEX, specialization_default_mask); |
| 482 | |
| 483 | RS::ShaderNativeSourceCode::Version::Stage stage; |
| 484 | stage.name = "vertex"; |
| 485 | stage.code = builder.as_string(); |
| 486 | |
| 487 | source_code.versions.write[i].stages.push_back(stage); |
| 488 | } |
| 489 | |
| 490 | //fragment stage |
| 491 | { |
| 492 | StringBuilder builder; |
| 493 | _build_variant_code(builder, i, version, STAGE_TYPE_FRAGMENT, specialization_default_mask); |
| 494 | |
| 495 | RS::ShaderNativeSourceCode::Version::Stage stage; |
| 496 | stage.name = "fragment"; |
| 497 | stage.code = builder.as_string(); |
| 498 | |
| 499 | source_code.versions.write[i].stages.push_back(stage); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | return source_code; |
| 504 | } |
| 505 | |
| 506 | String ShaderGLES3::_version_get_sha1(Version *p_version) const { |
| 507 | StringBuilder hash_build; |
| 508 | |
| 509 | hash_build.append("[uniforms]"); |
| 510 | hash_build.append(p_version->uniforms.get_data()); |
| 511 | hash_build.append("[vertex_globals]"); |
| 512 | hash_build.append(p_version->vertex_globals.get_data()); |
| 513 | hash_build.append("[fragment_globals]"); |
| 514 | hash_build.append(p_version->fragment_globals.get_data()); |
| 515 | |
| 516 | Vector<StringName> code_sections; |
| 517 | for (const KeyValue<StringName, CharString> &E : p_version->code_sections) { |
| 518 | code_sections.push_back(E.key); |
| 519 | } |
| 520 | code_sections.sort_custom<StringName::AlphCompare>(); |
| 521 | |
| 522 | for (int i = 0; i < code_sections.size(); i++) { |
| 523 | hash_build.append(String("[code:") + String(code_sections[i]) + "]"); |
| 524 | hash_build.append(p_version->code_sections[code_sections[i]].get_data()); |
| 525 | } |
| 526 | for (int i = 0; i < p_version->custom_defines.size(); i++) { |
| 527 | hash_build.append("[custom_defines:" + itos(i) + "]"); |
| 528 | hash_build.append(p_version->custom_defines[i].get_data()); |
| 529 | } |
| 530 | if (RasterizerGLES3::is_gles_over_gl()) { |
| 531 | hash_build.append("[gl]"); |
| 532 | } else { |
| 533 | hash_build.append("[gles]"); |
| 534 | } |
| 535 | |
| 536 | return hash_build.as_string().sha1_text(); |
| 537 | } |
| 538 | |
| 539 | #ifndef WEB_ENABLED // not supported in webgl |
| 540 | static const char *shader_file_header = "GLSC"; |
| 541 | static const uint32_t cache_file_version = 3; |
| 542 | #endif |
| 543 | |
| 544 | bool ShaderGLES3::_load_from_cache(Version *p_version) { |
| 545 | #ifdef WEB_ENABLED // not supported in webgl |
| 546 | return false; |
| 547 | #else |
| 548 | #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED) |
| 549 | if (RasterizerGLES3::is_gles_over_gl() && (glProgramBinary == nullptr)) { // ARB_get_program_binary extension not available. |
| 550 | return false; |
| 551 | } |
| 552 | #endif |
| 553 | String sha1 = _version_get_sha1(p_version); |
| 554 | String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache"; |
| 555 | |
| 556 | Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ); |
| 557 | if (f.is_null()) { |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | char header[5] = {}; |
| 562 | f->get_buffer((uint8_t *)header, 4); |
| 563 | ERR_FAIL_COND_V(header != String(shader_file_header), false); |
| 564 | |
| 565 | uint32_t file_version = f->get_32(); |
| 566 | if (file_version != cache_file_version) { |
| 567 | return false; // wrong version |
| 568 | } |
| 569 | |
| 570 | int cache_variant_count = static_cast<int>(f->get_32()); |
| 571 | ERR_FAIL_COND_V_MSG(cache_variant_count != variant_count, false, "shader cache variant count mismatch, expected " + itos(variant_count) + " got " + itos(cache_variant_count)); //should not happen but check |
| 572 | |
| 573 | LocalVector<AHashMap<uint64_t, Version::Specialization>> variants; |
| 574 | for (int i = 0; i < cache_variant_count; i++) { |
| 575 | uint32_t cache_specialization_count = f->get_32(); |
| 576 | AHashMap<uint64_t, Version::Specialization> variant; |
| 577 | for (uint32_t j = 0; j < cache_specialization_count; j++) { |
| 578 | uint64_t specialization_key = f->get_64(); |
| 579 | uint32_t variant_size = f->get_32(); |
| 580 | if (variant_size == 0) { |
| 581 | continue; |
| 582 | } |
| 583 | uint32_t variant_format = f->get_32(); |
| 584 | Vector<uint8_t> variant_bytes; |
| 585 | variant_bytes.resize(variant_size); |
| 586 | |
| 587 | uint32_t br = f->get_buffer(variant_bytes.ptrw(), variant_size); |
| 588 | |
| 589 | ERR_FAIL_COND_V(br != variant_size, false); |
| 590 | |
| 591 | Version::Specialization specialization; |
| 592 | |
| 593 | specialization.id = glCreateProgram(); |
| 594 | if (feedback_count) { |
| 595 | Vector<const char *> feedback; |
| 596 | for (int feedback_index = 0; feedback_index < feedback_count; feedback_index++) { |
| 597 | if (feedbacks[feedback_index].specialization == 0 || (feedbacks[feedback_index].specialization & specialization_key)) { |
| 598 | // Specialization for this feedback is enabled. |
| 599 | feedback.push_back(feedbacks[feedback_index].name); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!feedback.is_empty()) { |
| 604 | glTransformFeedbackVaryings(specialization.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS); |
| 605 | } |
| 606 | } |
| 607 | glProgramBinary(specialization.id, variant_format, variant_bytes.ptr(), variant_bytes.size()); |
| 608 | |
| 609 | GLint link_status = 0; |
| 610 | glGetProgramiv(specialization.id, GL_LINK_STATUS, &link_status); |
| 611 | if (link_status != GL_TRUE) { |
| 612 | WARN_PRINT_ONCE("Failed to load cached shader, recompiling."); |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | _get_uniform_locations(specialization, p_version); |
| 617 | |
| 618 | specialization.ok = true; |
| 619 | |
| 620 | variant.insert(specialization_key, specialization); |
| 621 | } |
| 622 | variants.push_back(variant); |
| 623 | } |
| 624 | p_version->variants = variants; |
| 625 | |
| 626 | return true; |
| 627 | #endif // WEB_ENABLED |
| 628 | } |
| 629 | |
| 630 | void ShaderGLES3::_save_to_cache(Version *p_version) { |
| 631 | #ifdef WEB_ENABLED // not supported in webgl |
| 632 | return; |
| 633 | #else |
| 634 | ERR_FAIL_COND(!shader_cache_dir_valid); |
| 635 | #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED) |
| 636 | if (RasterizerGLES3::is_gles_over_gl() && (glGetProgramBinary == nullptr)) { // ARB_get_program_binary extension not available. |
| 637 | return; |
| 638 | } |
| 639 | #endif |
| 640 | String sha1 = _version_get_sha1(p_version); |
| 641 | String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache"; |
| 642 | |
| 643 | Error error; |
| 644 | Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE, &error); |
| 645 | ERR_FAIL_COND(f.is_null()); |
| 646 | f->store_buffer((const uint8_t *)shader_file_header, 4); |
| 647 | f->store_32(cache_file_version); |
| 648 | f->store_32(variant_count); |
| 649 | |
| 650 | for (int i = 0; i < variant_count; i++) { |
| 651 | int cache_specialization_count = p_version->variants[i].size(); |
| 652 | f->store_32(cache_specialization_count); |
| 653 | |
| 654 | for (KeyValue<uint64_t, ShaderGLES3::Version::Specialization> &kv : p_version->variants[i]) { |
| 655 | const uint64_t specialization_key = kv.key; |
| 656 | f->store_64(specialization_key); |
| 657 | |
| 658 | const Version::Specialization *specialization = &kv.value; |
| 659 | GLint program_size = 0; |
| 660 | glGetProgramiv(specialization->id, GL_PROGRAM_BINARY_LENGTH, &program_size); |
| 661 | if (program_size == 0) { |
| 662 | f->store_32(0); |
| 663 | continue; |
| 664 | } |
| 665 | PackedByteArray compiled_program; |
| 666 | compiled_program.resize(program_size); |
| 667 | GLenum binary_format = 0; |
| 668 | glGetProgramBinary(specialization->id, program_size, nullptr, &binary_format, compiled_program.ptrw()); |
| 669 | if (program_size != compiled_program.size()) { |
| 670 | f->store_32(0); |
| 671 | continue; |
| 672 | } |
| 673 | f->store_32(program_size); |
| 674 | f->store_32(binary_format); |
| 675 | f->store_buffer(compiled_program.ptr(), compiled_program.size()); |
| 676 | } |
| 677 | } |
| 678 | #endif // WEB_ENABLED |
| 679 | } |
| 680 | |
| 681 | void ShaderGLES3::_clear_version(Version *p_version) { |
| 682 | // Variants not compiled yet, just return |
| 683 | if (p_version->variants.is_empty()) { |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | for (int i = 0; i < variant_count; i++) { |
| 688 | for (KeyValue<uint64_t, Version::Specialization> &kv : p_version->variants[i]) { |
| 689 | if (kv.value.id != 0) { |
| 690 | glDeleteShader(kv.value.vert_id); |
| 691 | glDeleteShader(kv.value.frag_id); |
| 692 | glDeleteProgram(kv.value.id); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | p_version->variants.clear(); |
| 698 | } |
| 699 | |
| 700 | void ShaderGLES3::_initialize_version(Version *p_version) { |
| 701 | ERR_FAIL_COND(p_version->variants.size() > 0); |
| 702 | bool use_cache = shader_cache_dir_valid && !(feedback_count > 0 && GLES3::Config::get_singleton()->disable_transform_feedback_shader_cache); |
| 703 | if (use_cache && _load_from_cache(p_version)) { |
| 704 | return; |
| 705 | } |
| 706 | p_version->variants.reserve(variant_count); |
| 707 | for (int i = 0; i < variant_count; i++) { |
| 708 | AHashMap<uint64_t, Version::Specialization> variant; |
| 709 | p_version->variants.push_back(variant); |
| 710 | Version::Specialization spec; |
| 711 | _compile_specialization(spec, i, p_version, specialization_default_mask); |
| 712 | p_version->variants[i].insert(specialization_default_mask, spec); |
| 713 | } |
| 714 | if (use_cache) { |
| 715 | _save_to_cache(p_version); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | void ShaderGLES3::version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines, const LocalVector<ShaderGLES3::TextureUniformData> &p_texture_uniforms, bool p_initialize) { |
| 720 | Version *version = version_owner.get_or_null(p_version); |
| 721 | ERR_FAIL_NULL(version); |
| 722 | |
| 723 | _clear_version(version); //clear if existing |
| 724 | |
| 725 | version->vertex_globals = p_vertex_globals.utf8(); |
| 726 | version->fragment_globals = p_fragment_globals.utf8(); |
| 727 | version->uniforms = p_uniforms.utf8(); |
| 728 | version->code_sections.clear(); |
| 729 | version->texture_uniforms = p_texture_uniforms; |
| 730 | for (const KeyValue<String, String> &E : p_code) { |
| 731 | version->code_sections[StringName(E.key.to_upper())] = E.value.utf8(); |
| 732 | } |
| 733 | |
| 734 | version->custom_defines.clear(); |
| 735 | for (int i = 0; i < p_custom_defines.size(); i++) { |
| 736 | version->custom_defines.push_back(p_custom_defines[i].utf8()); |
| 737 | } |
| 738 | |
| 739 | if (p_initialize) { |
| 740 | _initialize_version(version); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | bool ShaderGLES3::version_is_valid(RID p_version) { |
| 745 | Version *version = version_owner.get_or_null(p_version); |
| 746 | return version != nullptr; |
| 747 | } |
| 748 | |
| 749 | bool ShaderGLES3::version_free(RID p_version) { |
| 750 | if (version_owner.owns(p_version)) { |
| 751 | Version *version = version_owner.get_or_null(p_version); |
| 752 | _clear_version(version); |
| 753 | version_owner.free(p_version); |
| 754 | } else { |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | bool ShaderGLES3::shader_cache_cleanup_on_start = false; |
| 762 | |
| 763 | ShaderGLES3::ShaderGLES3() { |
| 764 | } |
| 765 | |
| 766 | void ShaderGLES3::initialize(const String &p_general_defines, int p_base_texture_index) { |
| 767 | general_defines = p_general_defines.utf8(); |
| 768 | base_texture_index = p_base_texture_index; |
| 769 | |
| 770 | _init(); |
| 771 | |
| 772 | if (shader_cache_dir != String()) { |
| 773 | StringBuilder hash_build; |
| 774 | |
| 775 | hash_build.append("[base_hash]"); |
| 776 | hash_build.append(base_sha256); |
| 777 | hash_build.append("[general_defines]"); |
| 778 | hash_build.append(general_defines.get_data()); |
| 779 | for (int i = 0; i < variant_count; i++) { |
| 780 | hash_build.append("[variant_defines:" + itos(i) + "]"); |
| 781 | hash_build.append(variant_defines[i]); |
| 782 | } |
| 783 | |
| 784 | base_sha256 = hash_build.as_string().sha256_text(); |
| 785 | |
| 786 | Ref<DirAccess> d = DirAccess::open(shader_cache_dir); |
| 787 | ERR_FAIL_COND(d.is_null()); |
| 788 | if (d->change_dir(name) != OK) { |
| 789 | Error err = d->make_dir(name); |
| 790 | ERR_FAIL_COND(err != OK); |
| 791 | d->change_dir(name); |
| 792 | } |
| 793 | |
| 794 | //erase other versions? |
| 795 | if (shader_cache_cleanup_on_start) { |
| 796 | } |
| 797 | // |
| 798 | if (d->change_dir(base_sha256) != OK) { |
| 799 | Error err = d->make_dir(base_sha256); |
| 800 | ERR_FAIL_COND(err != OK); |
| 801 | } |
| 802 | shader_cache_dir_valid = true; |
| 803 | |
| 804 | print_verbose("Shader '" + name + "' SHA256: " + base_sha256); |
| 805 | } |
| 806 | |
| 807 | GLES3::Config *config = GLES3::Config::get_singleton(); |
| 808 | ERR_FAIL_NULL(config); |
| 809 | max_image_units = config->max_texture_image_units; |
| 810 | } |
| 811 | |
| 812 | void ShaderGLES3::set_shader_cache_dir(const String &p_dir) { |
| 813 | shader_cache_dir = p_dir; |
| 814 | } |
| 815 | |
| 816 | void ShaderGLES3::set_shader_cache_save_compressed(bool p_enable) { |
| 817 | shader_cache_save_compressed = p_enable; |
| 818 | } |
| 819 | |
| 820 | void ShaderGLES3::set_shader_cache_save_compressed_zstd(bool p_enable) { |
| 821 | shader_cache_save_compressed_zstd = p_enable; |
| 822 | } |
| 823 | |
| 824 | void ShaderGLES3::set_shader_cache_save_debug(bool p_enable) { |
| 825 | shader_cache_save_debug = p_enable; |
| 826 | } |
| 827 | |
| 828 | String ShaderGLES3::shader_cache_dir; |
| 829 | bool ShaderGLES3::shader_cache_save_compressed = true; |
| 830 | bool ShaderGLES3::shader_cache_save_compressed_zstd = true; |
| 831 | bool ShaderGLES3::shader_cache_save_debug = true; |
| 832 | |
| 833 | ShaderGLES3::~ShaderGLES3() { |
| 834 | LocalVector<RID> remaining = version_owner.get_owned_list(); |
| 835 | if (remaining.size()) { |
| 836 | ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed"); |
| 837 | for (RID &rid : remaining) { |
| 838 | version_free(rid); |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | #endif |