shader_gles3.cpp 20 KB

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