shader_gles3.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. #include "shader_gles3.h"
  31. #ifdef GLES3_ENABLED
  32. #include "core/io/compression.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "drivers/gles3/rasterizer_gles3.h"
  36. #include "drivers/gles3/storage/config.h"
  37. static String _mkid(const String &p_id) {
  38. String id = "m_" + p_id.replace("__", "_dus_");
  39. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  40. }
  41. void ShaderGLES3::_add_stage(const char *p_code, StageType p_stage_type) {
  42. Vector<String> lines = String(p_code).split("\n");
  43. String text;
  44. for (int i = 0; i < lines.size(); i++) {
  45. const String &l = lines[i];
  46. bool push_chunk = false;
  47. StageTemplate::Chunk chunk;
  48. if (l.begins_with("#GLOBALS")) {
  49. switch (p_stage_type) {
  50. case STAGE_TYPE_VERTEX:
  51. chunk.type = StageTemplate::Chunk::TYPE_VERTEX_GLOBALS;
  52. break;
  53. case STAGE_TYPE_FRAGMENT:
  54. chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS;
  55. break;
  56. default: {
  57. }
  58. }
  59. push_chunk = true;
  60. } else if (l.begins_with("#MATERIAL_UNIFORMS")) {
  61. chunk.type = StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS;
  62. push_chunk = true;
  63. } else if (l.begins_with("#CODE")) {
  64. chunk.type = StageTemplate::Chunk::TYPE_CODE;
  65. push_chunk = true;
  66. chunk.code = l.replace_first("#CODE", String()).replace(":", "").strip_edges().to_upper();
  67. } else {
  68. text += l + "\n";
  69. }
  70. if (push_chunk) {
  71. if (text != String()) {
  72. StageTemplate::Chunk text_chunk;
  73. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  74. text_chunk.text = text.utf8();
  75. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  76. text = String();
  77. }
  78. stage_templates[p_stage_type].chunks.push_back(chunk);
  79. }
  80. if (text != String()) {
  81. StageTemplate::Chunk text_chunk;
  82. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  83. text_chunk.text = text.utf8();
  84. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  85. text = String();
  86. }
  87. }
  88. }
  89. 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) {
  90. name = p_name;
  91. if (p_vertex_code) {
  92. _add_stage(p_vertex_code, STAGE_TYPE_VERTEX);
  93. }
  94. if (p_fragment_code) {
  95. _add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT);
  96. }
  97. uniform_names = p_uniform_names;
  98. uniform_count = p_uniform_count;
  99. ubo_pairs = p_ubos;
  100. ubo_count = p_ubo_count;
  101. texunit_pairs = p_tex_units;
  102. texunit_pair_count = p_texture_count;
  103. specializations = p_specializations;
  104. specialization_count = p_specialization_count;
  105. specialization_default_mask = 0;
  106. for (int i = 0; i < specialization_count; i++) {
  107. if (specializations[i].default_value) {
  108. specialization_default_mask |= (uint64_t(1) << uint64_t(i));
  109. }
  110. }
  111. variant_defines = p_variants;
  112. variant_count = p_variant_count;
  113. feedbacks = p_feedback;
  114. feedback_count = p_feedback_count;
  115. StringBuilder tohash;
  116. /*
  117. tohash.append("[SpirvCacheKey]");
  118. tohash.append(RenderingDevice::get_singleton()->shader_get_spirv_cache_key());
  119. tohash.append("[BinaryCacheKey]");
  120. tohash.append(RenderingDevice::get_singleton()->shader_get_binary_cache_key());
  121. */
  122. tohash.append("[Vertex]");
  123. tohash.append(p_vertex_code ? p_vertex_code : "");
  124. tohash.append("[Fragment]");
  125. tohash.append(p_fragment_code ? p_fragment_code : "");
  126. tohash.append("[gl_implementation]");
  127. const String &vendor = String::utf8((const char *)glGetString(GL_VENDOR));
  128. tohash.append(vendor.is_empty() ? "unknown" : vendor);
  129. const String &renderer = String::utf8((const char *)glGetString(GL_RENDERER));
  130. tohash.append(renderer.is_empty() ? "unknown" : renderer);
  131. const String &version = String::utf8((const char *)glGetString(GL_VERSION));
  132. tohash.append(version.is_empty() ? "unknown" : version);
  133. base_sha256 = tohash.as_string().sha256_text();
  134. }
  135. RID ShaderGLES3::version_create() {
  136. //initialize() was never called
  137. ERR_FAIL_COND_V(variant_count == 0, RID());
  138. Version version;
  139. return version_owner.make_rid(version);
  140. }
  141. void ShaderGLES3::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization) {
  142. if (RasterizerGLES3::is_gles_over_gl()) {
  143. builder.append("#version 330\n");
  144. builder.append("#define USE_GLES_OVER_GL\n");
  145. } else {
  146. builder.append("#version 300 es\n");
  147. }
  148. if (GLES3::Config::get_singleton()->polyfill_half2float) {
  149. builder.append("#define USE_HALF2FLOAT\n");
  150. }
  151. for (int i = 0; i < specialization_count; i++) {
  152. if (p_specialization & (uint64_t(1) << uint64_t(i))) {
  153. builder.append("#define " + String(specializations[i].name) + "\n");
  154. }
  155. }
  156. if (p_version->uniforms.size()) {
  157. builder.append("#define MATERIAL_UNIFORMS_USED\n");
  158. }
  159. for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
  160. builder.append(String("#define ") + String(E.key) + "_CODE_USED\n");
  161. }
  162. builder.append("\n"); //make sure defines begin at newline
  163. builder.append(general_defines.get_data());
  164. builder.append(variant_defines[p_variant]);
  165. builder.append("\n");
  166. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  167. builder.append(p_version->custom_defines[j].get_data());
  168. }
  169. builder.append("\n"); //make sure defines begin at newline
  170. // Optional support for external textures.
  171. if (GLES3::Config::get_singleton()->external_texture_supported) {
  172. builder.append("#extension GL_OES_EGL_image_external : enable\n");
  173. builder.append("#extension GL_OES_EGL_image_external_essl3 : enable\n");
  174. } else {
  175. builder.append("#define samplerExternalOES sampler2D\n");
  176. }
  177. // Insert multiview extension loading, because it needs to appear before
  178. // any non-preprocessor code (like the "precision highp..." lines below).
  179. builder.append("#ifdef USE_MULTIVIEW\n");
  180. builder.append("#if defined(GL_OVR_multiview2)\n");
  181. builder.append("#extension GL_OVR_multiview2 : require\n");
  182. builder.append("#elif defined(GL_OVR_multiview)\n");
  183. builder.append("#extension GL_OVR_multiview : require\n");
  184. builder.append("#endif\n");
  185. if (p_stage_type == StageType::STAGE_TYPE_VERTEX) {
  186. builder.append("layout(num_views=2) in;\n");
  187. }
  188. builder.append("#define ViewIndex gl_ViewID_OVR\n");
  189. builder.append("#define MAX_VIEWS 2\n");
  190. builder.append("#else\n");
  191. builder.append("#define ViewIndex uint(0)\n");
  192. builder.append("#define MAX_VIEWS 1\n");
  193. builder.append("#endif\n");
  194. // Default to highp precision unless specified otherwise.
  195. builder.append("precision highp float;\n");
  196. builder.append("precision highp int;\n");
  197. if (!RasterizerGLES3::is_gles_over_gl()) {
  198. builder.append("precision highp sampler2D;\n");
  199. builder.append("precision highp samplerCube;\n");
  200. builder.append("precision highp sampler2DArray;\n");
  201. builder.append("precision highp sampler3D;\n");
  202. }
  203. const StageTemplate &stage_template = stage_templates[p_stage_type];
  204. for (uint32_t i = 0; i < stage_template.chunks.size(); i++) {
  205. const StageTemplate::Chunk &chunk = stage_template.chunks[i];
  206. switch (chunk.type) {
  207. case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: {
  208. builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  209. } break;
  210. case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: {
  211. builder.append(p_version->vertex_globals.get_data()); // vertex globals
  212. } break;
  213. case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
  214. builder.append(p_version->fragment_globals.get_data()); // fragment globals
  215. } break;
  216. case StageTemplate::Chunk::TYPE_CODE: {
  217. if (p_version->code_sections.has(chunk.code)) {
  218. builder.append(p_version->code_sections[chunk.code].get_data());
  219. }
  220. } break;
  221. case StageTemplate::Chunk::TYPE_TEXT: {
  222. builder.append(chunk.text.get_data());
  223. } break;
  224. }
  225. }
  226. }
  227. static void _display_error_with_code(const String &p_error, const String &p_code) {
  228. int line = 1;
  229. Vector<String> lines = p_code.split("\n");
  230. for (int j = 0; j < lines.size(); j++) {
  231. print_line(itos(line) + ": " + lines[j]);
  232. line++;
  233. }
  234. ERR_PRINT(p_error);
  235. }
  236. void ShaderGLES3::_get_uniform_locations(Version::Specialization &spec, Version *p_version) {
  237. glUseProgram(spec.id);
  238. spec.uniform_location.resize(uniform_count);
  239. for (int i = 0; i < uniform_count; i++) {
  240. spec.uniform_location[i] = glGetUniformLocation(spec.id, uniform_names[i]);
  241. }
  242. for (int i = 0; i < texunit_pair_count; i++) {
  243. GLint loc = glGetUniformLocation(spec.id, texunit_pairs[i].name);
  244. if (loc >= 0) {
  245. if (texunit_pairs[i].index < 0) {
  246. glUniform1i(loc, max_image_units + texunit_pairs[i].index);
  247. } else {
  248. glUniform1i(loc, texunit_pairs[i].index);
  249. }
  250. }
  251. }
  252. for (int i = 0; i < ubo_count; i++) {
  253. GLint loc = glGetUniformBlockIndex(spec.id, ubo_pairs[i].name);
  254. if (loc >= 0) {
  255. glUniformBlockBinding(spec.id, loc, ubo_pairs[i].index);
  256. }
  257. }
  258. // textures
  259. int texture_index = 0;
  260. for (uint32_t i = 0; i < p_version->texture_uniforms.size(); i++) {
  261. String native_uniform_name = _mkid(p_version->texture_uniforms[i].name);
  262. GLint location = glGetUniformLocation(spec.id, (native_uniform_name).ascii().get_data());
  263. Vector<int32_t> texture_uniform_bindings;
  264. int texture_count = p_version->texture_uniforms[i].array_size;
  265. for (int j = 0; j < texture_count; j++) {
  266. texture_uniform_bindings.append(texture_index + base_texture_index);
  267. texture_index++;
  268. }
  269. glUniform1iv(location, texture_uniform_bindings.size(), texture_uniform_bindings.ptr());
  270. }
  271. glUseProgram(0);
  272. }
  273. void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_t p_variant, Version *p_version, uint64_t p_specialization) {
  274. spec.id = glCreateProgram();
  275. spec.ok = false;
  276. GLint status;
  277. //vertex stage
  278. {
  279. StringBuilder builder;
  280. _build_variant_code(builder, p_variant, p_version, STAGE_TYPE_VERTEX, p_specialization);
  281. spec.vert_id = glCreateShader(GL_VERTEX_SHADER);
  282. String builder_string = builder.as_string();
  283. CharString cs = builder_string.utf8();
  284. const char *cstr = cs.ptr();
  285. glShaderSource(spec.vert_id, 1, &cstr, nullptr);
  286. glCompileShader(spec.vert_id);
  287. glGetShaderiv(spec.vert_id, GL_COMPILE_STATUS, &status);
  288. if (status == GL_FALSE) {
  289. GLsizei iloglen;
  290. glGetShaderiv(spec.vert_id, GL_INFO_LOG_LENGTH, &iloglen);
  291. if (iloglen < 0) {
  292. glDeleteShader(spec.vert_id);
  293. glDeleteProgram(spec.id);
  294. spec.id = 0;
  295. ERR_PRINT("No OpenGL vertex shader compiler log.");
  296. } else {
  297. if (iloglen == 0) {
  298. iloglen = 4096; // buggy driver (Adreno 220+)
  299. }
  300. char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
  301. memset(ilogmem, 0, iloglen + 1);
  302. glGetShaderInfoLog(spec.vert_id, iloglen, &iloglen, ilogmem);
  303. String err_string = name + ": Vertex shader compilation failed:\n";
  304. err_string += ilogmem;
  305. _display_error_with_code(err_string, builder_string);
  306. Memory::free_static(ilogmem);
  307. glDeleteShader(spec.vert_id);
  308. glDeleteProgram(spec.id);
  309. spec.id = 0;
  310. }
  311. ERR_FAIL();
  312. }
  313. }
  314. //fragment stage
  315. {
  316. StringBuilder builder;
  317. _build_variant_code(builder, p_variant, p_version, STAGE_TYPE_FRAGMENT, p_specialization);
  318. spec.frag_id = glCreateShader(GL_FRAGMENT_SHADER);
  319. String builder_string = builder.as_string();
  320. CharString cs = builder_string.utf8();
  321. const char *cstr = cs.ptr();
  322. glShaderSource(spec.frag_id, 1, &cstr, nullptr);
  323. glCompileShader(spec.frag_id);
  324. glGetShaderiv(spec.frag_id, GL_COMPILE_STATUS, &status);
  325. if (status == GL_FALSE) {
  326. GLsizei iloglen;
  327. glGetShaderiv(spec.frag_id, GL_INFO_LOG_LENGTH, &iloglen);
  328. if (iloglen < 0) {
  329. glDeleteShader(spec.frag_id);
  330. glDeleteProgram(spec.id);
  331. spec.id = 0;
  332. ERR_PRINT("No OpenGL fragment shader compiler log.");
  333. } else {
  334. if (iloglen == 0) {
  335. iloglen = 4096; // buggy driver (Adreno 220+)
  336. }
  337. char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
  338. memset(ilogmem, 0, iloglen + 1);
  339. glGetShaderInfoLog(spec.frag_id, iloglen, &iloglen, ilogmem);
  340. String err_string = name + ": Fragment shader compilation failed:\n";
  341. err_string += ilogmem;
  342. _display_error_with_code(err_string, builder_string);
  343. Memory::free_static(ilogmem);
  344. glDeleteShader(spec.frag_id);
  345. glDeleteProgram(spec.id);
  346. spec.id = 0;
  347. }
  348. ERR_FAIL();
  349. }
  350. }
  351. glAttachShader(spec.id, spec.frag_id);
  352. glAttachShader(spec.id, spec.vert_id);
  353. // If feedback exists, set it up.
  354. if (feedback_count) {
  355. Vector<const char *> feedback;
  356. for (int i = 0; i < feedback_count; i++) {
  357. if (feedbacks[i].specialization == 0 || (feedbacks[i].specialization & p_specialization)) {
  358. // Specialization for this feedback is enabled
  359. feedback.push_back(feedbacks[i].name);
  360. }
  361. }
  362. if (feedback.size()) {
  363. glTransformFeedbackVaryings(spec.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
  364. }
  365. }
  366. glLinkProgram(spec.id);
  367. glGetProgramiv(spec.id, GL_LINK_STATUS, &status);
  368. if (status == GL_FALSE) {
  369. GLsizei iloglen;
  370. glGetProgramiv(spec.id, GL_INFO_LOG_LENGTH, &iloglen);
  371. if (iloglen < 0) {
  372. glDeleteShader(spec.frag_id);
  373. glDeleteShader(spec.vert_id);
  374. glDeleteProgram(spec.id);
  375. spec.id = 0;
  376. ERR_PRINT("No OpenGL program link log. Something is wrong.");
  377. ERR_FAIL();
  378. }
  379. if (iloglen == 0) {
  380. iloglen = 4096; // buggy driver (Adreno 220+)
  381. }
  382. char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
  383. ilogmem[iloglen] = '\0';
  384. glGetProgramInfoLog(spec.id, iloglen, &iloglen, ilogmem);
  385. String err_string = name + ": Program linking failed:\n";
  386. err_string += ilogmem;
  387. _display_error_with_code(err_string, String());
  388. Memory::free_static(ilogmem);
  389. glDeleteShader(spec.frag_id);
  390. glDeleteShader(spec.vert_id);
  391. glDeleteProgram(spec.id);
  392. spec.id = 0;
  393. ERR_FAIL();
  394. }
  395. _get_uniform_locations(spec, p_version);
  396. spec.ok = true;
  397. }
  398. RS::ShaderNativeSourceCode ShaderGLES3::version_get_native_source_code(RID p_version) {
  399. Version *version = version_owner.get_or_null(p_version);
  400. RS::ShaderNativeSourceCode source_code;
  401. ERR_FAIL_NULL_V(version, source_code);
  402. source_code.versions.resize(variant_count);
  403. for (int i = 0; i < source_code.versions.size(); i++) {
  404. //vertex stage
  405. {
  406. StringBuilder builder;
  407. _build_variant_code(builder, i, version, STAGE_TYPE_VERTEX, specialization_default_mask);
  408. RS::ShaderNativeSourceCode::Version::Stage stage;
  409. stage.name = "vertex";
  410. stage.code = builder.as_string();
  411. source_code.versions.write[i].stages.push_back(stage);
  412. }
  413. //fragment stage
  414. {
  415. StringBuilder builder;
  416. _build_variant_code(builder, i, version, STAGE_TYPE_FRAGMENT, specialization_default_mask);
  417. RS::ShaderNativeSourceCode::Version::Stage stage;
  418. stage.name = "fragment";
  419. stage.code = builder.as_string();
  420. source_code.versions.write[i].stages.push_back(stage);
  421. }
  422. }
  423. return source_code;
  424. }
  425. String ShaderGLES3::_version_get_sha1(Version *p_version) const {
  426. StringBuilder hash_build;
  427. hash_build.append("[uniforms]");
  428. hash_build.append(p_version->uniforms.get_data());
  429. hash_build.append("[vertex_globals]");
  430. hash_build.append(p_version->vertex_globals.get_data());
  431. hash_build.append("[fragment_globals]");
  432. hash_build.append(p_version->fragment_globals.get_data());
  433. Vector<StringName> code_sections;
  434. for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
  435. code_sections.push_back(E.key);
  436. }
  437. code_sections.sort_custom<StringName::AlphCompare>();
  438. for (int i = 0; i < code_sections.size(); i++) {
  439. hash_build.append(String("[code:") + String(code_sections[i]) + "]");
  440. hash_build.append(p_version->code_sections[code_sections[i]].get_data());
  441. }
  442. for (int i = 0; i < p_version->custom_defines.size(); i++) {
  443. hash_build.append("[custom_defines:" + itos(i) + "]");
  444. hash_build.append(p_version->custom_defines[i].get_data());
  445. }
  446. if (RasterizerGLES3::is_gles_over_gl()) {
  447. hash_build.append("[gl]");
  448. } else {
  449. hash_build.append("[gles]");
  450. }
  451. return hash_build.as_string().sha1_text();
  452. }
  453. #ifndef WEB_ENABLED // not supported in webgl
  454. static const char *shader_file_header = "GLSC";
  455. static const uint32_t cache_file_version = 3;
  456. #endif
  457. bool ShaderGLES3::_load_from_cache(Version *p_version) {
  458. #ifdef WEB_ENABLED // not supported in webgl
  459. return false;
  460. #else
  461. #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
  462. if (RasterizerGLES3::is_gles_over_gl() && (glProgramBinary == nullptr)) { // ARB_get_program_binary extension not available.
  463. return false;
  464. }
  465. #endif
  466. String sha1 = _version_get_sha1(p_version);
  467. String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
  468. Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ);
  469. if (f.is_null()) {
  470. return false;
  471. }
  472. char header[5] = {};
  473. f->get_buffer((uint8_t *)header, 4);
  474. ERR_FAIL_COND_V(header != String(shader_file_header), false);
  475. uint32_t file_version = f->get_32();
  476. if (file_version != cache_file_version) {
  477. return false; // wrong version
  478. }
  479. int cache_variant_count = static_cast<int>(f->get_32());
  480. 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
  481. LocalVector<OAHashMap<uint64_t, Version::Specialization>> variants;
  482. for (int i = 0; i < cache_variant_count; i++) {
  483. uint32_t cache_specialization_count = f->get_32();
  484. OAHashMap<uint64_t, Version::Specialization> variant;
  485. for (uint32_t j = 0; j < cache_specialization_count; j++) {
  486. uint64_t specialization_key = f->get_64();
  487. uint32_t variant_size = f->get_32();
  488. if (variant_size == 0) {
  489. continue;
  490. }
  491. uint32_t variant_format = f->get_32();
  492. Vector<uint8_t> variant_bytes;
  493. variant_bytes.resize(variant_size);
  494. uint32_t br = f->get_buffer(variant_bytes.ptrw(), variant_size);
  495. ERR_FAIL_COND_V(br != variant_size, false);
  496. Version::Specialization specialization;
  497. specialization.id = glCreateProgram();
  498. if (feedback_count) {
  499. Vector<const char *> feedback;
  500. for (int feedback_index = 0; feedback_index < feedback_count; feedback_index++) {
  501. if (feedbacks[feedback_index].specialization == 0 || (feedbacks[feedback_index].specialization & specialization_key)) {
  502. // Specialization for this feedback is enabled.
  503. feedback.push_back(feedbacks[feedback_index].name);
  504. }
  505. }
  506. if (!feedback.is_empty()) {
  507. glTransformFeedbackVaryings(specialization.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
  508. }
  509. }
  510. glProgramBinary(specialization.id, variant_format, variant_bytes.ptr(), variant_bytes.size());
  511. GLint link_status = 0;
  512. glGetProgramiv(specialization.id, GL_LINK_STATUS, &link_status);
  513. if (link_status != GL_TRUE) {
  514. WARN_PRINT_ONCE("Failed to load cached shader, recompiling.");
  515. return false;
  516. }
  517. _get_uniform_locations(specialization, p_version);
  518. specialization.ok = true;
  519. variant.insert(specialization_key, specialization);
  520. }
  521. variants.push_back(variant);
  522. }
  523. p_version->variants = variants;
  524. return true;
  525. #endif // WEB_ENABLED
  526. }
  527. void ShaderGLES3::_save_to_cache(Version *p_version) {
  528. #ifdef WEB_ENABLED // not supported in webgl
  529. return;
  530. #else
  531. ERR_FAIL_COND(!shader_cache_dir_valid);
  532. #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
  533. if (RasterizerGLES3::is_gles_over_gl() && (glGetProgramBinary == nullptr)) { // ARB_get_program_binary extension not available.
  534. return;
  535. }
  536. #endif
  537. String sha1 = _version_get_sha1(p_version);
  538. String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
  539. Error error;
  540. Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE, &error);
  541. ERR_FAIL_COND(f.is_null());
  542. f->store_buffer((const uint8_t *)shader_file_header, 4);
  543. f->store_32(cache_file_version);
  544. f->store_32(variant_count);
  545. for (int i = 0; i < variant_count; i++) {
  546. int cache_specialization_count = p_version->variants[i].get_num_elements();
  547. f->store_32(cache_specialization_count);
  548. for (OAHashMap<uint64_t, ShaderGLES3::Version::Specialization>::Iterator it = p_version->variants[i].iter(); it.valid; it = p_version->variants[i].next_iter(it)) {
  549. const uint64_t specialization_key = *it.key;
  550. f->store_64(specialization_key);
  551. const Version::Specialization *specialization = it.value;
  552. if (specialization == nullptr) {
  553. f->store_32(0);
  554. continue;
  555. }
  556. GLint program_size = 0;
  557. glGetProgramiv(specialization->id, GL_PROGRAM_BINARY_LENGTH, &program_size);
  558. if (program_size == 0) {
  559. f->store_32(0);
  560. continue;
  561. }
  562. PackedByteArray compiled_program;
  563. compiled_program.resize(program_size);
  564. GLenum binary_format = 0;
  565. glGetProgramBinary(specialization->id, program_size, nullptr, &binary_format, compiled_program.ptrw());
  566. if (program_size != compiled_program.size()) {
  567. f->store_32(0);
  568. continue;
  569. }
  570. f->store_32(program_size);
  571. f->store_32(binary_format);
  572. f->store_buffer(compiled_program.ptr(), compiled_program.size());
  573. }
  574. }
  575. #endif // WEB_ENABLED
  576. }
  577. void ShaderGLES3::_clear_version(Version *p_version) {
  578. // Variants not compiled yet, just return
  579. if (p_version->variants.size() == 0) {
  580. return;
  581. }
  582. for (int i = 0; i < variant_count; i++) {
  583. for (OAHashMap<uint64_t, Version::Specialization>::Iterator it = p_version->variants[i].iter(); it.valid; it = p_version->variants[i].next_iter(it)) {
  584. if (it.value->id != 0) {
  585. glDeleteShader(it.value->vert_id);
  586. glDeleteShader(it.value->frag_id);
  587. glDeleteProgram(it.value->id);
  588. }
  589. }
  590. }
  591. p_version->variants.clear();
  592. }
  593. void ShaderGLES3::_initialize_version(Version *p_version) {
  594. ERR_FAIL_COND(p_version->variants.size() > 0);
  595. bool use_cache = shader_cache_dir_valid && !(feedback_count > 0 && GLES3::Config::get_singleton()->disable_transform_feedback_shader_cache);
  596. if (use_cache && _load_from_cache(p_version)) {
  597. return;
  598. }
  599. p_version->variants.reserve(variant_count);
  600. for (int i = 0; i < variant_count; i++) {
  601. OAHashMap<uint64_t, Version::Specialization> variant;
  602. p_version->variants.push_back(variant);
  603. Version::Specialization spec;
  604. _compile_specialization(spec, i, p_version, specialization_default_mask);
  605. p_version->variants[i].insert(specialization_default_mask, spec);
  606. }
  607. if (use_cache) {
  608. _save_to_cache(p_version);
  609. }
  610. }
  611. 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) {
  612. Version *version = version_owner.get_or_null(p_version);
  613. ERR_FAIL_NULL(version);
  614. _clear_version(version); //clear if existing
  615. version->vertex_globals = p_vertex_globals.utf8();
  616. version->fragment_globals = p_fragment_globals.utf8();
  617. version->uniforms = p_uniforms.utf8();
  618. version->code_sections.clear();
  619. version->texture_uniforms = p_texture_uniforms;
  620. for (const KeyValue<String, String> &E : p_code) {
  621. version->code_sections[StringName(E.key.to_upper())] = E.value.utf8();
  622. }
  623. version->custom_defines.clear();
  624. for (int i = 0; i < p_custom_defines.size(); i++) {
  625. version->custom_defines.push_back(p_custom_defines[i].utf8());
  626. }
  627. if (p_initialize) {
  628. _initialize_version(version);
  629. }
  630. }
  631. bool ShaderGLES3::version_is_valid(RID p_version) {
  632. Version *version = version_owner.get_or_null(p_version);
  633. return version != nullptr;
  634. }
  635. bool ShaderGLES3::version_free(RID p_version) {
  636. if (version_owner.owns(p_version)) {
  637. Version *version = version_owner.get_or_null(p_version);
  638. _clear_version(version);
  639. version_owner.free(p_version);
  640. } else {
  641. return false;
  642. }
  643. return true;
  644. }
  645. bool ShaderGLES3::shader_cache_cleanup_on_start = false;
  646. ShaderGLES3::ShaderGLES3() {
  647. }
  648. void ShaderGLES3::initialize(const String &p_general_defines, int p_base_texture_index) {
  649. general_defines = p_general_defines.utf8();
  650. base_texture_index = p_base_texture_index;
  651. _init();
  652. if (shader_cache_dir != String()) {
  653. StringBuilder hash_build;
  654. hash_build.append("[base_hash]");
  655. hash_build.append(base_sha256);
  656. hash_build.append("[general_defines]");
  657. hash_build.append(general_defines.get_data());
  658. for (int i = 0; i < variant_count; i++) {
  659. hash_build.append("[variant_defines:" + itos(i) + "]");
  660. hash_build.append(variant_defines[i]);
  661. }
  662. base_sha256 = hash_build.as_string().sha256_text();
  663. Ref<DirAccess> d = DirAccess::open(shader_cache_dir);
  664. ERR_FAIL_COND(d.is_null());
  665. if (d->change_dir(name) != OK) {
  666. Error err = d->make_dir(name);
  667. ERR_FAIL_COND(err != OK);
  668. d->change_dir(name);
  669. }
  670. //erase other versions?
  671. if (shader_cache_cleanup_on_start) {
  672. }
  673. //
  674. if (d->change_dir(base_sha256) != OK) {
  675. Error err = d->make_dir(base_sha256);
  676. ERR_FAIL_COND(err != OK);
  677. }
  678. shader_cache_dir_valid = true;
  679. print_verbose("Shader '" + name + "' SHA256: " + base_sha256);
  680. }
  681. GLES3::Config *config = GLES3::Config::get_singleton();
  682. ERR_FAIL_NULL(config);
  683. max_image_units = config->max_texture_image_units;
  684. }
  685. void ShaderGLES3::set_shader_cache_dir(const String &p_dir) {
  686. shader_cache_dir = p_dir;
  687. }
  688. void ShaderGLES3::set_shader_cache_save_compressed(bool p_enable) {
  689. shader_cache_save_compressed = p_enable;
  690. }
  691. void ShaderGLES3::set_shader_cache_save_compressed_zstd(bool p_enable) {
  692. shader_cache_save_compressed_zstd = p_enable;
  693. }
  694. void ShaderGLES3::set_shader_cache_save_debug(bool p_enable) {
  695. shader_cache_save_debug = p_enable;
  696. }
  697. String ShaderGLES3::shader_cache_dir;
  698. bool ShaderGLES3::shader_cache_save_compressed = true;
  699. bool ShaderGLES3::shader_cache_save_compressed_zstd = true;
  700. bool ShaderGLES3::shader_cache_save_debug = true;
  701. ShaderGLES3::~ShaderGLES3() {
  702. List<RID> remaining;
  703. version_owner.get_owned_list(&remaining);
  704. if (remaining.size()) {
  705. ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
  706. while (remaining.size()) {
  707. version_free(remaining.front()->get());
  708. remaining.pop_front();
  709. }
  710. }
  711. }
  712. #endif