shader_gles3.cpp 21 KB

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