shader_gles3.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*************************************************************************/
  2. /* shader_gles3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "core/print_string.h"
  32. //#define DEBUG_OPENGL
  33. #ifdef DEBUG_OPENGL
  34. #define DEBUG_TEST_ERROR(m_section) \
  35. { \
  36. uint32_t err = glGetError(); \
  37. if (err) { \
  38. print_line("OpenGL Error #" + itos(err) + " at: " + m_section); \
  39. } \
  40. }
  41. #else
  42. #define DEBUG_TEST_ERROR(m_section)
  43. #endif
  44. ShaderGLES3 *ShaderGLES3::active = NULL;
  45. //#define DEBUG_SHADER
  46. #ifdef DEBUG_SHADER
  47. #define DEBUG_PRINT(m_text) print_line(m_text);
  48. #else
  49. #define DEBUG_PRINT(m_text)
  50. #endif
  51. void ShaderGLES3::bind_uniforms() {
  52. if (!uniforms_dirty) {
  53. return;
  54. };
  55. // upload default uniforms
  56. const Map<uint32_t, Variant>::Element *E = uniform_defaults.front();
  57. while (E) {
  58. int idx = E->key();
  59. int location = version->uniform_location[idx];
  60. if (location < 0) {
  61. E = E->next();
  62. continue;
  63. }
  64. const Variant &v = E->value();
  65. _set_uniform_variant(location, v);
  66. //print_line("uniform "+itos(location)+" value "+v+ " type "+Variant::get_type_name(v.get_type()));
  67. E = E->next();
  68. };
  69. const Map<uint32_t, CameraMatrix>::Element *C = uniform_cameras.front();
  70. while (C) {
  71. int location = version->uniform_location[C->key()];
  72. if (location < 0) {
  73. C = C->next();
  74. continue;
  75. }
  76. glUniformMatrix4fv(location, 1, false, &(C->get().matrix[0][0]));
  77. C = C->next();
  78. };
  79. uniforms_dirty = false;
  80. }
  81. GLint ShaderGLES3::get_uniform_location(int p_index) const {
  82. ERR_FAIL_COND_V(!version, -1);
  83. return version->uniform_location[p_index];
  84. }
  85. bool ShaderGLES3::bind() {
  86. if (active != this || !version || new_conditional_version.key != conditional_version.key) {
  87. conditional_version = new_conditional_version;
  88. version = get_current_version();
  89. } else {
  90. return false;
  91. }
  92. ERR_FAIL_COND_V(!version, false);
  93. if (!version->ok) { //broken, unable to bind (do not throw error, you saw it before already when it failed compilation).
  94. glUseProgram(0);
  95. return false;
  96. }
  97. glUseProgram(version->id);
  98. DEBUG_TEST_ERROR("Use Program");
  99. active = this;
  100. uniforms_dirty = true;
  101. return true;
  102. }
  103. void ShaderGLES3::unbind() {
  104. version = NULL;
  105. glUseProgram(0);
  106. uniforms_dirty = true;
  107. active = NULL;
  108. }
  109. static void _display_error_with_code(const String &p_error, const Vector<const char *> &p_code) {
  110. int line = 1;
  111. String total_code;
  112. for (int i = 0; i < p_code.size(); i++) {
  113. total_code += String(p_code[i]);
  114. }
  115. Vector<String> lines = String(total_code).split("\n");
  116. for (int j = 0; j < lines.size(); j++) {
  117. print_line(itos(line) + ": " + lines[j]);
  118. line++;
  119. }
  120. ERR_PRINTS(p_error);
  121. }
  122. ShaderGLES3::Version *ShaderGLES3::get_current_version() {
  123. Version *_v = version_map.getptr(conditional_version);
  124. if (_v) {
  125. if (conditional_version.code_version != 0) {
  126. CustomCode *cc = custom_code_map.getptr(conditional_version.code_version);
  127. ERR_FAIL_COND_V(!cc, _v);
  128. if (cc->version == _v->code_version)
  129. return _v;
  130. } else {
  131. return _v;
  132. }
  133. }
  134. if (!_v)
  135. version_map[conditional_version] = Version();
  136. Version &v = version_map[conditional_version];
  137. if (!_v) {
  138. v.uniform_location = memnew_arr(GLint, uniform_count);
  139. } else {
  140. if (v.ok) {
  141. //bye bye shaders
  142. glDeleteShader(v.vert_id);
  143. glDeleteShader(v.frag_id);
  144. glDeleteProgram(v.id);
  145. v.id = 0;
  146. }
  147. }
  148. v.ok = false;
  149. /* SETUP CONDITIONALS */
  150. Vector<const char *> strings;
  151. #ifdef GLES_OVER_GL
  152. strings.push_back("#version 330\n");
  153. strings.push_back("#define GLES_OVER_GL\n");
  154. #else
  155. strings.push_back("#version 300 es\n");
  156. #endif
  157. for (int i = 0; i < custom_defines.size(); i++) {
  158. strings.push_back(custom_defines[i].get_data());
  159. }
  160. for (int j = 0; j < conditional_count; j++) {
  161. bool enable = ((1 << j) & conditional_version.version);
  162. strings.push_back(enable ? conditional_defines[j] : "");
  163. if (enable) {
  164. DEBUG_PRINT(conditional_defines[j]);
  165. }
  166. }
  167. //keep them around during the function
  168. CharString code_string;
  169. CharString code_string2;
  170. CharString code_globals;
  171. CharString material_string;
  172. CustomCode *cc = NULL;
  173. if (conditional_version.code_version > 0) {
  174. //do custom code related stuff
  175. ERR_FAIL_COND_V(!custom_code_map.has(conditional_version.code_version), NULL);
  176. cc = &custom_code_map[conditional_version.code_version];
  177. v.code_version = cc->version;
  178. }
  179. /* CREATE PROGRAM */
  180. v.id = glCreateProgram();
  181. ERR_FAIL_COND_V(v.id == 0, NULL);
  182. /* VERTEX SHADER */
  183. if (cc) {
  184. for (int i = 0; i < cc->custom_defines.size(); i++) {
  185. strings.push_back(cc->custom_defines[i].get_data());
  186. DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i]));
  187. }
  188. }
  189. int strings_base_size = strings.size();
  190. //vertex precision is high
  191. strings.push_back("precision highp float;\n");
  192. strings.push_back("precision highp int;\n");
  193. #ifndef GLES_OVER_GL
  194. strings.push_back("precision highp sampler2D;\n");
  195. strings.push_back("precision highp samplerCube;\n");
  196. strings.push_back("precision highp sampler2DArray;\n");
  197. #endif
  198. strings.push_back(vertex_code0.get_data());
  199. if (cc) {
  200. material_string = cc->uniforms.ascii();
  201. strings.push_back(material_string.get_data());
  202. }
  203. strings.push_back(vertex_code1.get_data());
  204. if (cc) {
  205. code_globals = cc->vertex_globals.ascii();
  206. strings.push_back(code_globals.get_data());
  207. }
  208. strings.push_back(vertex_code2.get_data());
  209. if (cc) {
  210. code_string = cc->vertex.ascii();
  211. strings.push_back(code_string.get_data());
  212. }
  213. strings.push_back(vertex_code3.get_data());
  214. #ifdef DEBUG_SHADER
  215. DEBUG_PRINT("\nVertex Code:\n\n" + String(code_string.get_data()));
  216. for (int i = 0; i < strings.size(); i++) {
  217. //print_line("vert strings "+itos(i)+":"+String(strings[i]));
  218. }
  219. #endif
  220. v.vert_id = glCreateShader(GL_VERTEX_SHADER);
  221. glShaderSource(v.vert_id, strings.size(), &strings[0], NULL);
  222. glCompileShader(v.vert_id);
  223. GLint status;
  224. glGetShaderiv(v.vert_id, GL_COMPILE_STATUS, &status);
  225. if (status == GL_FALSE) {
  226. // error compiling
  227. GLsizei iloglen;
  228. glGetShaderiv(v.vert_id, GL_INFO_LOG_LENGTH, &iloglen);
  229. if (iloglen < 0) {
  230. glDeleteShader(v.vert_id);
  231. glDeleteProgram(v.id);
  232. v.id = 0;
  233. ERR_PRINT("Vertex shader compilation failed with empty log");
  234. } else {
  235. if (iloglen == 0) {
  236. iloglen = 4096; //buggy driver (Adreno 220+....)
  237. }
  238. char *ilogmem = (char *)memalloc(iloglen + 1);
  239. ilogmem[iloglen] = 0;
  240. glGetShaderInfoLog(v.vert_id, iloglen, &iloglen, ilogmem);
  241. String err_string = get_shader_name() + ": Vertex Program Compilation Failed:\n";
  242. err_string += ilogmem;
  243. _display_error_with_code(err_string, strings);
  244. memfree(ilogmem);
  245. glDeleteShader(v.vert_id);
  246. glDeleteProgram(v.id);
  247. v.id = 0;
  248. }
  249. ERR_FAIL_V(NULL);
  250. }
  251. //_display_error_with_code("pepo", strings);
  252. /* FRAGMENT SHADER */
  253. strings.resize(strings_base_size);
  254. //fragment precision is medium
  255. strings.push_back("precision highp float;\n");
  256. strings.push_back("precision highp int;\n");
  257. #ifndef GLES_OVER_GL
  258. strings.push_back("precision highp sampler2D;\n");
  259. strings.push_back("precision highp samplerCube;\n");
  260. strings.push_back("precision highp sampler2DArray;\n");
  261. #endif
  262. strings.push_back(fragment_code0.get_data());
  263. if (cc) {
  264. material_string = cc->uniforms.ascii();
  265. strings.push_back(material_string.get_data());
  266. }
  267. strings.push_back(fragment_code1.get_data());
  268. if (cc) {
  269. code_globals = cc->fragment_globals.ascii();
  270. strings.push_back(code_globals.get_data());
  271. }
  272. strings.push_back(fragment_code2.get_data());
  273. if (cc) {
  274. code_string = cc->light.ascii();
  275. strings.push_back(code_string.get_data());
  276. }
  277. strings.push_back(fragment_code3.get_data());
  278. if (cc) {
  279. code_string2 = cc->fragment.ascii();
  280. strings.push_back(code_string2.get_data());
  281. }
  282. strings.push_back(fragment_code4.get_data());
  283. #ifdef DEBUG_SHADER
  284. DEBUG_PRINT("\nFragment Globals:\n\n" + String(code_globals.get_data()));
  285. DEBUG_PRINT("\nFragment Code:\n\n" + String(code_string2.get_data()));
  286. for (int i = 0; i < strings.size(); i++) {
  287. //print_line("frag strings "+itos(i)+":"+String(strings[i]));
  288. }
  289. #endif
  290. v.frag_id = glCreateShader(GL_FRAGMENT_SHADER);
  291. glShaderSource(v.frag_id, strings.size(), &strings[0], NULL);
  292. glCompileShader(v.frag_id);
  293. glGetShaderiv(v.frag_id, GL_COMPILE_STATUS, &status);
  294. if (status == GL_FALSE) {
  295. // error compiling
  296. GLsizei iloglen;
  297. glGetShaderiv(v.frag_id, GL_INFO_LOG_LENGTH, &iloglen);
  298. if (iloglen < 0) {
  299. glDeleteShader(v.frag_id);
  300. glDeleteShader(v.vert_id);
  301. glDeleteProgram(v.id);
  302. v.id = 0;
  303. ERR_PRINT("Fragment shader compilation failed with empty log");
  304. } else {
  305. if (iloglen == 0) {
  306. iloglen = 4096; //buggy driver (Adreno 220+....)
  307. }
  308. char *ilogmem = (char *)memalloc(iloglen + 1);
  309. ilogmem[iloglen] = 0;
  310. glGetShaderInfoLog(v.frag_id, iloglen, &iloglen, ilogmem);
  311. String err_string = get_shader_name() + ": Fragment Program Compilation Failed:\n";
  312. err_string += ilogmem;
  313. _display_error_with_code(err_string, strings);
  314. ERR_PRINT(err_string.ascii().get_data());
  315. memfree(ilogmem);
  316. glDeleteShader(v.frag_id);
  317. glDeleteShader(v.vert_id);
  318. glDeleteProgram(v.id);
  319. v.id = 0;
  320. }
  321. ERR_FAIL_V(NULL);
  322. }
  323. glAttachShader(v.id, v.frag_id);
  324. glAttachShader(v.id, v.vert_id);
  325. // bind attributes before linking
  326. for (int i = 0; i < attribute_pair_count; i++) {
  327. glBindAttribLocation(v.id, attribute_pairs[i].index, attribute_pairs[i].name);
  328. }
  329. //if feedback exists, set it up
  330. if (feedback_count) {
  331. Vector<const char *> feedback;
  332. for (int i = 0; i < feedback_count; i++) {
  333. if (feedbacks[i].conditional == -1 || (1 << feedbacks[i].conditional) & conditional_version.version) {
  334. //conditional for this feedback is enabled
  335. feedback.push_back(feedbacks[i].name);
  336. }
  337. }
  338. if (feedback.size()) {
  339. glTransformFeedbackVaryings(v.id, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
  340. }
  341. }
  342. glLinkProgram(v.id);
  343. glGetProgramiv(v.id, GL_LINK_STATUS, &status);
  344. if (status == GL_FALSE) {
  345. // error linking
  346. GLsizei iloglen;
  347. glGetProgramiv(v.id, GL_INFO_LOG_LENGTH, &iloglen);
  348. if (iloglen < 0) {
  349. glDeleteShader(v.frag_id);
  350. glDeleteShader(v.vert_id);
  351. glDeleteProgram(v.id);
  352. v.id = 0;
  353. ERR_FAIL_COND_V(iloglen <= 0, NULL);
  354. }
  355. if (iloglen == 0) {
  356. iloglen = 4096; //buggy driver (Adreno 220+....)
  357. }
  358. char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
  359. ilogmem[iloglen] = 0;
  360. glGetProgramInfoLog(v.id, iloglen, &iloglen, ilogmem);
  361. String err_string = get_shader_name() + ": Program LINK FAILED:\n";
  362. err_string += ilogmem;
  363. _display_error_with_code(err_string, strings);
  364. ERR_PRINT(err_string.ascii().get_data());
  365. Memory::free_static(ilogmem);
  366. glDeleteShader(v.frag_id);
  367. glDeleteShader(v.vert_id);
  368. glDeleteProgram(v.id);
  369. v.id = 0;
  370. ERR_FAIL_V(NULL);
  371. }
  372. /* UNIFORMS */
  373. glUseProgram(v.id);
  374. //print_line("uniforms: ");
  375. for (int j = 0; j < uniform_count; j++) {
  376. v.uniform_location[j] = glGetUniformLocation(v.id, uniform_names[j]);
  377. //print_line("uniform "+String(uniform_names[j])+" location "+itos(v.uniform_location[j]));
  378. }
  379. // set texture uniforms
  380. for (int i = 0; i < texunit_pair_count; i++) {
  381. GLint loc = glGetUniformLocation(v.id, texunit_pairs[i].name);
  382. if (loc >= 0) {
  383. if (texunit_pairs[i].index < 0) {
  384. glUniform1i(loc, max_image_units + texunit_pairs[i].index); //negative, goes down
  385. } else {
  386. glUniform1i(loc, texunit_pairs[i].index);
  387. }
  388. }
  389. }
  390. // assign uniform block bind points
  391. for (int i = 0; i < ubo_count; i++) {
  392. GLint loc = glGetUniformBlockIndex(v.id, ubo_pairs[i].name);
  393. if (loc >= 0)
  394. glUniformBlockBinding(v.id, loc, ubo_pairs[i].index);
  395. }
  396. if (cc) {
  397. v.texture_uniform_locations.resize(cc->texture_uniforms.size());
  398. for (int i = 0; i < cc->texture_uniforms.size(); i++) {
  399. v.texture_uniform_locations.write[i] = glGetUniformLocation(v.id, String(cc->texture_uniforms[i]).ascii().get_data());
  400. glUniform1i(v.texture_uniform_locations[i], i + base_material_tex_index);
  401. }
  402. }
  403. glUseProgram(0);
  404. v.ok = true;
  405. if (cc) {
  406. cc->versions.insert(conditional_version.version);
  407. }
  408. return &v;
  409. }
  410. GLint ShaderGLES3::get_uniform_location(const String &p_name) const {
  411. ERR_FAIL_COND_V(!version, -1);
  412. return glGetUniformLocation(version->id, p_name.ascii().get_data());
  413. }
  414. void ShaderGLES3::setup(const char **p_conditional_defines, int p_conditional_count, const char **p_uniform_names, int p_uniform_count, const AttributePair *p_attribute_pairs, int p_attribute_count, const TexUnitPair *p_texunit_pairs, int p_texunit_pair_count, const UBOPair *p_ubo_pairs, int p_ubo_pair_count, const Feedback *p_feedback, int p_feedback_count, const char *p_vertex_code, const char *p_fragment_code, int p_vertex_code_start, int p_fragment_code_start) {
  415. ERR_FAIL_COND(version);
  416. conditional_version.key = 0;
  417. new_conditional_version.key = 0;
  418. uniform_count = p_uniform_count;
  419. conditional_count = p_conditional_count;
  420. conditional_defines = p_conditional_defines;
  421. uniform_names = p_uniform_names;
  422. vertex_code = p_vertex_code;
  423. fragment_code = p_fragment_code;
  424. texunit_pairs = p_texunit_pairs;
  425. texunit_pair_count = p_texunit_pair_count;
  426. vertex_code_start = p_vertex_code_start;
  427. fragment_code_start = p_fragment_code_start;
  428. attribute_pairs = p_attribute_pairs;
  429. attribute_pair_count = p_attribute_count;
  430. ubo_pairs = p_ubo_pairs;
  431. ubo_count = p_ubo_pair_count;
  432. feedbacks = p_feedback;
  433. feedback_count = p_feedback_count;
  434. //split vertex and shader code (thank you, shader compiler programmers from you know what company).
  435. {
  436. String globals_tag = "\nVERTEX_SHADER_GLOBALS";
  437. String material_tag = "\nMATERIAL_UNIFORMS";
  438. String code_tag = "\nVERTEX_SHADER_CODE";
  439. String code = vertex_code;
  440. int cpos = code.find(material_tag);
  441. if (cpos == -1) {
  442. vertex_code0 = code.ascii();
  443. } else {
  444. vertex_code0 = code.substr(0, cpos).ascii();
  445. code = code.substr(cpos + material_tag.length(), code.length());
  446. cpos = code.find(globals_tag);
  447. if (cpos == -1) {
  448. vertex_code1 = code.ascii();
  449. } else {
  450. vertex_code1 = code.substr(0, cpos).ascii();
  451. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  452. cpos = code2.find(code_tag);
  453. if (cpos == -1) {
  454. vertex_code2 = code2.ascii();
  455. } else {
  456. vertex_code2 = code2.substr(0, cpos).ascii();
  457. vertex_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii();
  458. }
  459. }
  460. }
  461. }
  462. {
  463. String globals_tag = "\nFRAGMENT_SHADER_GLOBALS";
  464. String material_tag = "\nMATERIAL_UNIFORMS";
  465. String code_tag = "\nFRAGMENT_SHADER_CODE";
  466. String light_code_tag = "\nLIGHT_SHADER_CODE";
  467. String code = fragment_code;
  468. int cpos = code.find(material_tag);
  469. if (cpos == -1) {
  470. fragment_code0 = code.ascii();
  471. } else {
  472. fragment_code0 = code.substr(0, cpos).ascii();
  473. //print_line("CODE0:\n"+String(fragment_code0.get_data()));
  474. code = code.substr(cpos + material_tag.length(), code.length());
  475. cpos = code.find(globals_tag);
  476. if (cpos == -1) {
  477. fragment_code1 = code.ascii();
  478. } else {
  479. fragment_code1 = code.substr(0, cpos).ascii();
  480. //print_line("CODE1:\n"+String(fragment_code1.get_data()));
  481. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  482. cpos = code2.find(light_code_tag);
  483. if (cpos == -1) {
  484. fragment_code2 = code2.ascii();
  485. } else {
  486. fragment_code2 = code2.substr(0, cpos).ascii();
  487. //print_line("CODE2:\n"+String(fragment_code2.get_data()));
  488. String code3 = code2.substr(cpos + light_code_tag.length(), code2.length());
  489. cpos = code3.find(code_tag);
  490. if (cpos == -1) {
  491. fragment_code3 = code3.ascii();
  492. } else {
  493. fragment_code3 = code3.substr(0, cpos).ascii();
  494. //print_line("CODE3:\n"+String(fragment_code3.get_data()));
  495. fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii();
  496. //print_line("CODE4:\n"+String(fragment_code4.get_data()));
  497. }
  498. }
  499. }
  500. }
  501. }
  502. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_image_units);
  503. }
  504. void ShaderGLES3::finish() {
  505. const VersionKey *V = NULL;
  506. while ((V = version_map.next(V))) {
  507. Version &v = version_map[*V];
  508. glDeleteShader(v.vert_id);
  509. glDeleteShader(v.frag_id);
  510. glDeleteProgram(v.id);
  511. memdelete_arr(v.uniform_location);
  512. }
  513. }
  514. void ShaderGLES3::clear_caches() {
  515. const VersionKey *V = NULL;
  516. while ((V = version_map.next(V))) {
  517. Version &v = version_map[*V];
  518. glDeleteShader(v.vert_id);
  519. glDeleteShader(v.frag_id);
  520. glDeleteProgram(v.id);
  521. memdelete_arr(v.uniform_location);
  522. }
  523. version_map.clear();
  524. custom_code_map.clear();
  525. version = NULL;
  526. last_custom_code = 1;
  527. uniforms_dirty = true;
  528. }
  529. uint32_t ShaderGLES3::create_custom_shader() {
  530. custom_code_map[last_custom_code] = CustomCode();
  531. custom_code_map[last_custom_code].version = 1;
  532. return last_custom_code++;
  533. }
  534. void ShaderGLES3::set_custom_shader_code(uint32_t p_code_id, const String &p_vertex, const String &p_vertex_globals, const String &p_fragment, const String &p_light, const String &p_fragment_globals, const String &p_uniforms, const Vector<StringName> &p_texture_uniforms, const Vector<CharString> &p_custom_defines) {
  535. ERR_FAIL_COND(!custom_code_map.has(p_code_id));
  536. CustomCode *cc = &custom_code_map[p_code_id];
  537. cc->vertex = p_vertex;
  538. cc->vertex_globals = p_vertex_globals;
  539. cc->fragment = p_fragment;
  540. cc->fragment_globals = p_fragment_globals;
  541. cc->light = p_light;
  542. cc->texture_uniforms = p_texture_uniforms;
  543. cc->uniforms = p_uniforms;
  544. cc->custom_defines = p_custom_defines;
  545. cc->version++;
  546. }
  547. void ShaderGLES3::set_custom_shader(uint32_t p_code_id) {
  548. new_conditional_version.code_version = p_code_id;
  549. }
  550. void ShaderGLES3::free_custom_shader(uint32_t p_code_id) {
  551. ERR_FAIL_COND(!custom_code_map.has(p_code_id));
  552. if (conditional_version.code_version == p_code_id) {
  553. conditional_version.code_version = 0; //do not keep using a version that is going away
  554. unbind();
  555. }
  556. VersionKey key;
  557. key.code_version = p_code_id;
  558. for (Set<uint32_t>::Element *E = custom_code_map[p_code_id].versions.front(); E; E = E->next()) {
  559. key.version = E->get();
  560. ERR_CONTINUE(!version_map.has(key));
  561. Version &v = version_map[key];
  562. glDeleteShader(v.vert_id);
  563. glDeleteShader(v.frag_id);
  564. glDeleteProgram(v.id);
  565. memdelete_arr(v.uniform_location);
  566. v.id = 0;
  567. version_map.erase(key);
  568. }
  569. custom_code_map.erase(p_code_id);
  570. }
  571. void ShaderGLES3::set_base_material_tex_index(int p_idx) {
  572. base_material_tex_index = p_idx;
  573. }
  574. ShaderGLES3::ShaderGLES3() {
  575. version = NULL;
  576. last_custom_code = 1;
  577. uniforms_dirty = true;
  578. base_material_tex_index = 0;
  579. }
  580. ShaderGLES3::~ShaderGLES3() {
  581. finish();
  582. }