shader_gles3.cpp 27 KB

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