shader_gles3.cpp 23 KB

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