shader_compiler_gles3.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /**************************************************************************/
  2. /* shader_compiler_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_compiler_gles3.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #define SL ShaderLanguage
  34. static String _mktab(int p_level) {
  35. String tb;
  36. for (int i = 0; i < p_level; i++) {
  37. tb += "\t";
  38. }
  39. return tb;
  40. }
  41. static String _typestr(SL::DataType p_type) {
  42. return ShaderLanguage::get_datatype_name(p_type);
  43. }
  44. static int _get_datatype_size(SL::DataType p_type) {
  45. switch (p_type) {
  46. case SL::TYPE_VOID:
  47. return 0;
  48. case SL::TYPE_BOOL:
  49. return 4;
  50. case SL::TYPE_BVEC2:
  51. return 8;
  52. case SL::TYPE_BVEC3:
  53. return 12;
  54. case SL::TYPE_BVEC4:
  55. return 16;
  56. case SL::TYPE_INT:
  57. return 4;
  58. case SL::TYPE_IVEC2:
  59. return 8;
  60. case SL::TYPE_IVEC3:
  61. return 12;
  62. case SL::TYPE_IVEC4:
  63. return 16;
  64. case SL::TYPE_UINT:
  65. return 4;
  66. case SL::TYPE_UVEC2:
  67. return 8;
  68. case SL::TYPE_UVEC3:
  69. return 12;
  70. case SL::TYPE_UVEC4:
  71. return 16;
  72. case SL::TYPE_FLOAT:
  73. return 4;
  74. case SL::TYPE_VEC2:
  75. return 8;
  76. case SL::TYPE_VEC3:
  77. return 12;
  78. case SL::TYPE_VEC4:
  79. return 16;
  80. case SL::TYPE_MAT2:
  81. return 32; //4 * 4 + 4 * 4
  82. case SL::TYPE_MAT3:
  83. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  84. case SL::TYPE_MAT4:
  85. return 64;
  86. case SL::TYPE_SAMPLER2D:
  87. return 16;
  88. case SL::TYPE_ISAMPLER2D:
  89. return 16;
  90. case SL::TYPE_USAMPLER2D:
  91. return 16;
  92. case SL::TYPE_SAMPLER2DARRAY:
  93. return 16;
  94. case SL::TYPE_ISAMPLER2DARRAY:
  95. return 16;
  96. case SL::TYPE_USAMPLER2DARRAY:
  97. return 16;
  98. case SL::TYPE_SAMPLER3D:
  99. return 16;
  100. case SL::TYPE_ISAMPLER3D:
  101. return 16;
  102. case SL::TYPE_USAMPLER3D:
  103. return 16;
  104. case SL::TYPE_SAMPLERCUBE:
  105. return 16;
  106. case SL::TYPE_SAMPLEREXT:
  107. return 16;
  108. case SL::TYPE_STRUCT:
  109. return 0;
  110. default: {
  111. }
  112. }
  113. ERR_FAIL_V(0);
  114. }
  115. static int _get_datatype_alignment(SL::DataType p_type) {
  116. switch (p_type) {
  117. case SL::TYPE_VOID:
  118. return 0;
  119. case SL::TYPE_BOOL:
  120. return 4;
  121. case SL::TYPE_BVEC2:
  122. return 8;
  123. case SL::TYPE_BVEC3:
  124. return 16;
  125. case SL::TYPE_BVEC4:
  126. return 16;
  127. case SL::TYPE_INT:
  128. return 4;
  129. case SL::TYPE_IVEC2:
  130. return 8;
  131. case SL::TYPE_IVEC3:
  132. return 16;
  133. case SL::TYPE_IVEC4:
  134. return 16;
  135. case SL::TYPE_UINT:
  136. return 4;
  137. case SL::TYPE_UVEC2:
  138. return 8;
  139. case SL::TYPE_UVEC3:
  140. return 16;
  141. case SL::TYPE_UVEC4:
  142. return 16;
  143. case SL::TYPE_FLOAT:
  144. return 4;
  145. case SL::TYPE_VEC2:
  146. return 8;
  147. case SL::TYPE_VEC3:
  148. return 16;
  149. case SL::TYPE_VEC4:
  150. return 16;
  151. case SL::TYPE_MAT2:
  152. return 16;
  153. case SL::TYPE_MAT3:
  154. return 16;
  155. case SL::TYPE_MAT4:
  156. return 16;
  157. case SL::TYPE_SAMPLER2D:
  158. return 16;
  159. case SL::TYPE_ISAMPLER2D:
  160. return 16;
  161. case SL::TYPE_USAMPLER2D:
  162. return 16;
  163. case SL::TYPE_SAMPLER2DARRAY:
  164. return 16;
  165. case SL::TYPE_ISAMPLER2DARRAY:
  166. return 16;
  167. case SL::TYPE_USAMPLER2DARRAY:
  168. return 16;
  169. case SL::TYPE_SAMPLER3D:
  170. return 16;
  171. case SL::TYPE_ISAMPLER3D:
  172. return 16;
  173. case SL::TYPE_USAMPLER3D:
  174. return 16;
  175. case SL::TYPE_SAMPLERCUBE:
  176. return 16;
  177. case SL::TYPE_SAMPLEREXT:
  178. return 16;
  179. case SL::TYPE_STRUCT:
  180. return 0;
  181. default: {
  182. }
  183. }
  184. ERR_FAIL_V(0);
  185. }
  186. static String _interpstr(SL::DataInterpolation p_interp) {
  187. switch (p_interp) {
  188. case SL::INTERPOLATION_FLAT:
  189. return "flat ";
  190. case SL::INTERPOLATION_SMOOTH:
  191. return "";
  192. }
  193. return "";
  194. }
  195. static String _prestr(SL::DataPrecision p_pres) {
  196. switch (p_pres) {
  197. case SL::PRECISION_LOWP:
  198. return "lowp ";
  199. case SL::PRECISION_MEDIUMP:
  200. return "mediump ";
  201. case SL::PRECISION_HIGHP:
  202. return "highp ";
  203. case SL::PRECISION_DEFAULT:
  204. return "";
  205. }
  206. return "";
  207. }
  208. static String _constr(bool p_is_const) {
  209. if (p_is_const) {
  210. return "const ";
  211. }
  212. return "";
  213. }
  214. static String _qualstr(SL::ArgumentQualifier p_qual) {
  215. switch (p_qual) {
  216. case SL::ARGUMENT_QUALIFIER_IN:
  217. return "";
  218. case SL::ARGUMENT_QUALIFIER_OUT:
  219. return "out ";
  220. case SL::ARGUMENT_QUALIFIER_INOUT:
  221. return "inout ";
  222. }
  223. return "";
  224. }
  225. static String _opstr(SL::Operator p_op) {
  226. return SL::get_operator_text(p_op);
  227. }
  228. static String _mkid(const String &p_id) {
  229. String id = "m_" + p_id.replace("__", "_dus_");
  230. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  231. }
  232. static String f2sp0(float p_float) {
  233. String num = rtoss(p_float);
  234. if (num.find(".") == -1 && num.find("e") == -1) {
  235. num += ".0";
  236. }
  237. return num;
  238. }
  239. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  240. switch (p_type) {
  241. case SL::TYPE_BOOL:
  242. return p_values[0].boolean ? "true" : "false";
  243. case SL::TYPE_BVEC2:
  244. case SL::TYPE_BVEC3:
  245. case SL::TYPE_BVEC4: {
  246. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  247. for (int i = 0; i < p_values.size(); i++) {
  248. if (i > 0) {
  249. text += ",";
  250. }
  251. text += p_values[i].boolean ? "true" : "false";
  252. }
  253. text += ")";
  254. return text;
  255. }
  256. case SL::TYPE_INT:
  257. return itos(p_values[0].sint);
  258. case SL::TYPE_IVEC2:
  259. case SL::TYPE_IVEC3:
  260. case SL::TYPE_IVEC4: {
  261. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  262. for (int i = 0; i < p_values.size(); i++) {
  263. if (i > 0) {
  264. text += ",";
  265. }
  266. text += itos(p_values[i].sint);
  267. }
  268. text += ")";
  269. return text;
  270. } break;
  271. case SL::TYPE_UINT:
  272. return itos(p_values[0].uint) + "u";
  273. case SL::TYPE_UVEC2:
  274. case SL::TYPE_UVEC3:
  275. case SL::TYPE_UVEC4: {
  276. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  277. for (int i = 0; i < p_values.size(); i++) {
  278. if (i > 0) {
  279. text += ",";
  280. }
  281. text += itos(p_values[i].uint) + "u";
  282. }
  283. text += ")";
  284. return text;
  285. } break;
  286. case SL::TYPE_FLOAT:
  287. return f2sp0(p_values[0].real);
  288. case SL::TYPE_VEC2:
  289. case SL::TYPE_VEC3:
  290. case SL::TYPE_VEC4: {
  291. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  292. for (int i = 0; i < p_values.size(); i++) {
  293. if (i > 0) {
  294. text += ",";
  295. }
  296. text += f2sp0(p_values[i].real);
  297. }
  298. text += ")";
  299. return text;
  300. } break;
  301. case SL::TYPE_MAT2:
  302. case SL::TYPE_MAT3:
  303. case SL::TYPE_MAT4: {
  304. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  305. for (int i = 0; i < p_values.size(); i++) {
  306. if (i > 0) {
  307. text += ",";
  308. }
  309. text += f2sp0(p_values[i].real);
  310. }
  311. text += ")";
  312. return text;
  313. } break;
  314. default:
  315. ERR_FAIL_V(String());
  316. }
  317. }
  318. void ShaderCompilerGLES3::_dump_function_deps(const SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added) {
  319. int fidx = -1;
  320. for (int i = 0; i < p_node->functions.size(); i++) {
  321. if (p_node->functions[i].name == p_for_func) {
  322. fidx = i;
  323. break;
  324. }
  325. }
  326. ERR_FAIL_COND(fidx == -1);
  327. for (int ufidx = 0; ufidx < p_node->functions[fidx].uses_function.size(); ufidx++) {
  328. StringName function_name = p_node->functions[fidx].uses_function[ufidx];
  329. if (added.has(function_name)) {
  330. continue; //was added already
  331. }
  332. _dump_function_deps(p_node, function_name, p_func_code, r_to_add, added);
  333. SL::FunctionNode *fnode = nullptr;
  334. for (int i = 0; i < p_node->functions.size(); i++) {
  335. if (p_node->functions[i].name == function_name) {
  336. fnode = p_node->functions[i].function;
  337. break;
  338. }
  339. }
  340. ERR_FAIL_COND(!fnode);
  341. r_to_add += "\n";
  342. String header;
  343. if (fnode->return_type == SL::TYPE_STRUCT) {
  344. header = _mkid(fnode->return_struct_name) + " " + _mkid(fnode->name) + "(";
  345. } else {
  346. header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "(";
  347. }
  348. for (int i = 0; i < fnode->arguments.size(); i++) {
  349. if (i > 0) {
  350. header += ", ";
  351. }
  352. header += _constr(fnode->arguments[i].is_const);
  353. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  354. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  355. } else {
  356. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  357. }
  358. }
  359. header += ")\n";
  360. r_to_add += header;
  361. r_to_add += p_func_code[function_name];
  362. added.insert(function_name);
  363. }
  364. }
  365. String ShaderCompilerGLES3::_dump_node_code(const SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_use_scope) {
  366. String code;
  367. switch (p_node->type) {
  368. case SL::Node::TYPE_SHADER: {
  369. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  370. for (int i = 0; i < pnode->render_modes.size(); i++) {
  371. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  372. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]].utf8());
  373. used_rmode_defines.insert(pnode->render_modes[i]);
  374. }
  375. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  376. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  377. }
  378. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  379. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  380. *p.first = p.second;
  381. }
  382. }
  383. // structs
  384. for (int i = 0; i < pnode->vstructs.size(); i++) {
  385. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  386. String struct_code;
  387. struct_code += "struct ";
  388. struct_code += _mkid(pnode->vstructs[i].name);
  389. struct_code += " ";
  390. struct_code += "{\n";
  391. for (int j = 0; j < st->members.size(); j++) {
  392. SL::MemberNode *m = st->members[j];
  393. if (m->datatype == SL::TYPE_STRUCT) {
  394. struct_code += _mkid(m->struct_name);
  395. } else {
  396. struct_code += _prestr(m->precision);
  397. struct_code += _typestr(m->datatype);
  398. }
  399. struct_code += " ";
  400. struct_code += m->name;
  401. if (m->array_size > 0) {
  402. struct_code += "[";
  403. struct_code += itos(m->array_size);
  404. struct_code += "]";
  405. }
  406. struct_code += ";\n";
  407. }
  408. struct_code += "}";
  409. struct_code += ";\n";
  410. r_gen_code.vertex_global += struct_code;
  411. r_gen_code.fragment_global += struct_code;
  412. }
  413. int max_texture_uniforms = 0;
  414. int max_uniforms = 0;
  415. for (OrderedHashMap<StringName, SL::ShaderNode::Uniform>::Element E = pnode->uniforms.front(); E; E = E.next()) {
  416. if (SL::is_sampler_type(E.get().type)) {
  417. max_texture_uniforms++;
  418. } else {
  419. max_uniforms++;
  420. }
  421. }
  422. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  423. r_gen_code.texture_hints.resize(max_texture_uniforms);
  424. r_gen_code.texture_types.resize(max_texture_uniforms);
  425. Vector<int> uniform_sizes;
  426. Vector<int> uniform_alignments;
  427. Vector<StringName> uniform_defines;
  428. uniform_sizes.resize(max_uniforms);
  429. uniform_alignments.resize(max_uniforms);
  430. uniform_defines.resize(max_uniforms);
  431. bool uses_uniforms = false;
  432. for (OrderedHashMap<StringName, SL::ShaderNode::Uniform>::Element E = pnode->uniforms.front(); E; E = E.next()) {
  433. String ucode;
  434. if (SL::is_sampler_type(E.get().type)) {
  435. ucode = "uniform ";
  436. }
  437. ucode += _prestr(E.get().precision);
  438. ucode += _typestr(E.get().type);
  439. ucode += " " + _mkid(E.key());
  440. ucode += ";\n";
  441. if (SL::is_sampler_type(E.get().type)) {
  442. r_gen_code.vertex_global += ucode;
  443. r_gen_code.fragment_global += ucode;
  444. r_gen_code.texture_uniforms.write[E.get().texture_order] = _mkid(E.key());
  445. r_gen_code.texture_hints.write[E.get().texture_order] = E.get().hint;
  446. r_gen_code.texture_types.write[E.get().texture_order] = E.get().type;
  447. } else {
  448. if (!uses_uniforms) {
  449. r_gen_code.defines.push_back(String("#define USE_MATERIAL\n").ascii());
  450. uses_uniforms = true;
  451. }
  452. uniform_defines.write[E.get().order] = ucode;
  453. uniform_sizes.write[E.get().order] = _get_datatype_size(E.get().type);
  454. uniform_alignments.write[E.get().order] = _get_datatype_alignment(E.get().type);
  455. }
  456. p_actions.uniforms->insert(E.key(), E.get());
  457. }
  458. for (int i = 0; i < max_uniforms; i++) {
  459. r_gen_code.uniforms += uniform_defines[i];
  460. }
  461. // add up
  462. int offset = 0;
  463. for (int i = 0; i < uniform_sizes.size(); i++) {
  464. int align = offset % uniform_alignments[i];
  465. if (align != 0) {
  466. offset += uniform_alignments[i] - align;
  467. }
  468. r_gen_code.uniform_offsets.push_back(offset);
  469. offset += uniform_sizes[i];
  470. }
  471. r_gen_code.uniform_total_size = offset;
  472. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  473. r_gen_code.uniform_total_size += r_gen_code.uniform_total_size % 16;
  474. }
  475. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  476. for (OrderedHashMap<StringName, SL::ShaderNode::Varying>::Element E = pnode->varyings.front(); E; E = E.next()) {
  477. if (E.get().stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || E.get().stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  478. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(E.key(), E.get()));
  479. fragment_varyings.insert(E.key());
  480. continue;
  481. }
  482. String vcode;
  483. String interp_mode = _interpstr(E.get().interpolation);
  484. vcode += _prestr(E.get().precision);
  485. vcode += _typestr(E.get().type);
  486. vcode += " " + _mkid(E.key());
  487. if (E.get().array_size > 0) {
  488. vcode += "[";
  489. vcode += itos(E.get().array_size);
  490. vcode += "]";
  491. }
  492. vcode += ";\n";
  493. r_gen_code.vertex_global += interp_mode + "out " + vcode;
  494. r_gen_code.fragment_global += interp_mode + "in " + vcode;
  495. }
  496. if (var_frag_to_light.size() > 0) {
  497. String gcode = "\n\nstruct {\n";
  498. for (List<Pair<StringName, SL::ShaderNode::Varying>>::Element *E = var_frag_to_light.front(); E; E = E->next()) {
  499. gcode += "\t" + _prestr(E->get().second.precision) + _typestr(E->get().second.type) + " " + _mkid(E->get().first);
  500. if (E->get().second.array_size > 0) {
  501. gcode += "[";
  502. gcode += itos(E->get().second.array_size);
  503. gcode += "]";
  504. }
  505. gcode += ";\n";
  506. }
  507. gcode += "} frag_to_light;\n";
  508. r_gen_code.fragment_global += gcode;
  509. }
  510. for (int i = 0; i < pnode->vconstants.size(); i++) {
  511. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  512. String gcode;
  513. gcode += _constr(true);
  514. if (pnode->vconstants[i].type == SL::TYPE_STRUCT) {
  515. gcode += _mkid(cnode.type_str);
  516. } else {
  517. gcode += _prestr(cnode.precision);
  518. gcode += _typestr(cnode.type);
  519. }
  520. gcode += " " + _mkid(String(cnode.name));
  521. if (cnode.array_size > 0) {
  522. gcode += "[";
  523. gcode += itos(cnode.array_size);
  524. gcode += "]";
  525. }
  526. gcode += "=";
  527. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  528. gcode += ";\n";
  529. r_gen_code.vertex_global += gcode;
  530. r_gen_code.fragment_global += gcode;
  531. }
  532. Map<StringName, String> function_code;
  533. //code for functions
  534. for (int i = 0; i < pnode->functions.size(); i++) {
  535. SL::FunctionNode *fnode = pnode->functions[i].function;
  536. function = fnode;
  537. current_func_name = fnode->name;
  538. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  539. function = nullptr;
  540. }
  541. //place functions in actual code
  542. Set<StringName> added_vtx;
  543. Set<StringName> added_fragment; //share for light
  544. for (int i = 0; i < pnode->functions.size(); i++) {
  545. SL::FunctionNode *fnode = pnode->functions[i].function;
  546. function = fnode;
  547. current_func_name = fnode->name;
  548. if (fnode->name == vertex_name) {
  549. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  550. r_gen_code.vertex = function_code[vertex_name];
  551. }
  552. if (fnode->name == fragment_name) {
  553. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  554. r_gen_code.fragment = function_code[fragment_name];
  555. }
  556. if (fnode->name == light_name) {
  557. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  558. r_gen_code.light = function_code[light_name];
  559. }
  560. function = nullptr;
  561. }
  562. //code+=dump_node_code(pnode->body,p_level);
  563. } break;
  564. case SL::Node::TYPE_STRUCT: {
  565. } break;
  566. case SL::Node::TYPE_FUNCTION: {
  567. } break;
  568. case SL::Node::TYPE_BLOCK: {
  569. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  570. //variables
  571. if (!bnode->single_statement) {
  572. code += _mktab(p_level - 1) + "{\n";
  573. }
  574. for (int i = 0; i < bnode->statements.size(); i++) {
  575. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  576. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  577. code += scode; //use directly
  578. } else {
  579. code += _mktab(p_level) + scode + ";\n";
  580. }
  581. }
  582. if (!bnode->single_statement) {
  583. code += _mktab(p_level - 1) + "}\n";
  584. }
  585. } break;
  586. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  587. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  588. String declaration;
  589. declaration += _constr(vdnode->is_const);
  590. if (vdnode->datatype == SL::TYPE_STRUCT) {
  591. declaration += _mkid(vdnode->struct_name);
  592. } else {
  593. declaration += _prestr(vdnode->precision);
  594. declaration += _typestr(vdnode->datatype);
  595. }
  596. for (int i = 0; i < vdnode->declarations.size(); i++) {
  597. if (i > 0) {
  598. declaration += ",";
  599. } else {
  600. declaration += " ";
  601. }
  602. declaration += _mkid(vdnode->declarations[i].name);
  603. if (vdnode->declarations[i].initializer) {
  604. declaration += "=";
  605. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  606. }
  607. }
  608. code += declaration;
  609. } break;
  610. case SL::Node::TYPE_VARIABLE: {
  611. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  612. bool use_fragment_varying = false;
  613. if (!vnode->is_local && current_func_name != vertex_name) {
  614. if (p_assigning) {
  615. if (shader->varyings.has(vnode->name)) {
  616. use_fragment_varying = true;
  617. }
  618. } else {
  619. if (fragment_varyings.has(vnode->name)) {
  620. use_fragment_varying = true;
  621. }
  622. }
  623. }
  624. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  625. *p_actions.write_flag_pointers[vnode->name] = true;
  626. }
  627. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  628. String define = p_default_actions.usage_defines[vnode->name];
  629. if (define.begins_with("@")) {
  630. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  631. }
  632. r_gen_code.defines.push_back(define.utf8());
  633. used_name_defines.insert(vnode->name);
  634. }
  635. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  636. *p_actions.usage_flag_pointers[vnode->name] = true;
  637. used_flag_pointers.insert(vnode->name);
  638. }
  639. if (p_default_actions.renames.has(vnode->name)) {
  640. code = p_default_actions.renames[vnode->name];
  641. } else if (use_fragment_varying) {
  642. code = "frag_to_light." + _mkid(vnode->name);
  643. } else {
  644. code = _mkid(vnode->name);
  645. }
  646. if (vnode->name == time_name) {
  647. if (current_func_name == vertex_name) {
  648. r_gen_code.uses_vertex_time = true;
  649. }
  650. if (current_func_name == fragment_name || current_func_name == light_name) {
  651. r_gen_code.uses_fragment_time = true;
  652. }
  653. }
  654. } break;
  655. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  656. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  657. int sz = acnode->initializer.size();
  658. if (acnode->datatype == SL::TYPE_STRUCT) {
  659. code += _mkid(acnode->struct_name);
  660. } else {
  661. code += _typestr(acnode->datatype);
  662. }
  663. code += "[";
  664. code += itos(acnode->initializer.size());
  665. code += "]";
  666. code += "(";
  667. for (int i = 0; i < sz; i++) {
  668. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  669. if (i != sz - 1) {
  670. code += ", ";
  671. }
  672. }
  673. code += ")";
  674. } break;
  675. case SL::Node::TYPE_ARRAY_DECLARATION: {
  676. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  677. String declaration;
  678. if (adnode->is_const) {
  679. declaration += "const ";
  680. }
  681. if (adnode->datatype == SL::TYPE_STRUCT) {
  682. declaration += _mkid(adnode->struct_name);
  683. } else {
  684. declaration += _prestr(adnode->precision);
  685. declaration += _typestr(adnode->datatype);
  686. }
  687. for (int i = 0; i < adnode->declarations.size(); i++) {
  688. if (i > 0) {
  689. declaration += ",";
  690. } else {
  691. declaration += " ";
  692. }
  693. declaration += _mkid(adnode->declarations[i].name);
  694. declaration += "[";
  695. declaration += itos(adnode->declarations[i].size);
  696. declaration += "]";
  697. int sz = adnode->declarations[i].initializer.size();
  698. if (sz > 0) {
  699. declaration += "=";
  700. if (adnode->datatype == SL::TYPE_STRUCT) {
  701. declaration += _mkid(adnode->struct_name);
  702. } else {
  703. declaration += _typestr(adnode->datatype);
  704. }
  705. declaration += "[";
  706. declaration += itos(sz);
  707. declaration += "]";
  708. declaration += "(";
  709. for (int j = 0; j < sz; j++) {
  710. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  711. if (j != sz - 1) {
  712. declaration += ", ";
  713. }
  714. }
  715. declaration += ")";
  716. }
  717. }
  718. code += declaration;
  719. } break;
  720. case SL::Node::TYPE_ARRAY: {
  721. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  722. bool use_fragment_varying = false;
  723. if (!anode->is_local && current_func_name != vertex_name) {
  724. if (anode->assign_expression != nullptr) {
  725. use_fragment_varying = true;
  726. } else {
  727. if (p_assigning) {
  728. if (shader->varyings.has(anode->name)) {
  729. use_fragment_varying = true;
  730. }
  731. } else {
  732. if (fragment_varyings.has(anode->name)) {
  733. use_fragment_varying = true;
  734. }
  735. }
  736. }
  737. }
  738. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  739. *p_actions.write_flag_pointers[anode->name] = true;
  740. }
  741. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  742. String define = p_default_actions.usage_defines[anode->name];
  743. if (define.begins_with("@")) {
  744. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  745. }
  746. r_gen_code.defines.push_back(define.utf8());
  747. used_name_defines.insert(anode->name);
  748. }
  749. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  750. *p_actions.usage_flag_pointers[anode->name] = true;
  751. used_flag_pointers.insert(anode->name);
  752. }
  753. if (p_default_actions.renames.has(anode->name)) {
  754. code = p_default_actions.renames[anode->name];
  755. } else if (use_fragment_varying) {
  756. code = "frag_to_light." + _mkid(anode->name);
  757. } else {
  758. code = _mkid(anode->name);
  759. }
  760. if (anode->call_expression != nullptr) {
  761. code += ".";
  762. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  763. } else if (anode->index_expression != nullptr) {
  764. code += "[";
  765. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  766. code += "]";
  767. } else if (anode->assign_expression != nullptr) {
  768. code += "=";
  769. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  770. }
  771. if (anode->name == time_name) {
  772. if (current_func_name == vertex_name) {
  773. r_gen_code.uses_vertex_time = true;
  774. }
  775. if (current_func_name == fragment_name || current_func_name == light_name) {
  776. r_gen_code.uses_fragment_time = true;
  777. }
  778. }
  779. } break;
  780. case SL::Node::TYPE_CONSTANT: {
  781. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  782. if (cnode->array_size == 0) {
  783. return get_constant_text(cnode->datatype, cnode->values);
  784. } else {
  785. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  786. code += _mkid(cnode->struct_name);
  787. } else {
  788. code += _typestr(cnode->datatype);
  789. }
  790. code += "[";
  791. code += itos(cnode->array_size);
  792. code += "]";
  793. code += "(";
  794. for (int i = 0; i < cnode->array_size; i++) {
  795. if (i > 0) {
  796. code += ",";
  797. } else {
  798. code += "";
  799. }
  800. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  801. }
  802. code += ")";
  803. }
  804. } break;
  805. case SL::Node::TYPE_OPERATOR: {
  806. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  807. switch (onode->op) {
  808. case SL::OP_ASSIGN:
  809. case SL::OP_ASSIGN_ADD:
  810. case SL::OP_ASSIGN_SUB:
  811. case SL::OP_ASSIGN_MUL:
  812. case SL::OP_ASSIGN_DIV:
  813. case SL::OP_ASSIGN_SHIFT_LEFT:
  814. case SL::OP_ASSIGN_SHIFT_RIGHT:
  815. case SL::OP_ASSIGN_MOD:
  816. case SL::OP_ASSIGN_BIT_AND:
  817. case SL::OP_ASSIGN_BIT_OR:
  818. case SL::OP_ASSIGN_BIT_XOR:
  819. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  820. break;
  821. case SL::OP_BIT_INVERT:
  822. case SL::OP_NEGATE:
  823. case SL::OP_NOT:
  824. case SL::OP_DECREMENT:
  825. case SL::OP_INCREMENT:
  826. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  827. break;
  828. case SL::OP_POST_DECREMENT:
  829. case SL::OP_POST_INCREMENT:
  830. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  831. break;
  832. case SL::OP_CALL:
  833. case SL::OP_STRUCT:
  834. case SL::OP_CONSTRUCT: {
  835. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  836. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  837. if (onode->op == SL::OP_STRUCT) {
  838. code += _mkid(vnode->name);
  839. } else if (onode->op == SL::OP_CONSTRUCT) {
  840. code += String(vnode->name);
  841. } else {
  842. if (internal_functions.has(vnode->name)) {
  843. code += vnode->name;
  844. } else if (p_default_actions.renames.has(vnode->name)) {
  845. code += p_default_actions.renames[vnode->name];
  846. } else {
  847. code += _mkid(vnode->name);
  848. }
  849. }
  850. code += "(";
  851. for (int i = 1; i < onode->arguments.size(); i++) {
  852. if (i > 1) {
  853. code += ", ";
  854. }
  855. code += _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  856. }
  857. code += ")";
  858. } break;
  859. case SL::OP_INDEX: {
  860. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  861. code += "[";
  862. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  863. code += "]";
  864. } break;
  865. case SL::OP_SELECT_IF: {
  866. code += "(";
  867. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  868. code += "?";
  869. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  870. code += ":";
  871. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  872. code += ")";
  873. } break;
  874. default: {
  875. if (p_use_scope) {
  876. code += "(";
  877. }
  878. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  879. if (p_use_scope) {
  880. code += ")";
  881. }
  882. break;
  883. }
  884. }
  885. } break;
  886. case SL::Node::TYPE_CONTROL_FLOW: {
  887. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  888. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  889. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  890. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  891. if (cfnode->blocks.size() == 2) {
  892. code += _mktab(p_level) + "else\n";
  893. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  894. }
  895. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  896. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  897. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  898. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  899. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  900. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  901. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  902. code += _mktab(p_level) + "default:\n";
  903. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  904. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  905. code += _mktab(p_level) + "do";
  906. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  907. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  908. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  909. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  910. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  911. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  912. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  913. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  914. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  915. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  916. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  917. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  918. if (cfnode->expressions.size()) {
  919. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  920. } else {
  921. code = "return;";
  922. }
  923. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  924. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  925. *p_actions.usage_flag_pointers["DISCARD"] = true;
  926. used_flag_pointers.insert("DISCARD");
  927. }
  928. code = "discard;";
  929. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  930. code = "continue;";
  931. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  932. code = "break;";
  933. }
  934. } break;
  935. case SL::Node::TYPE_MEMBER: {
  936. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  937. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  938. if (mnode->index_expression != nullptr) {
  939. code += "[";
  940. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  941. code += "]";
  942. } else if (mnode->assign_expression != nullptr) {
  943. code += "=";
  944. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  945. }
  946. } break;
  947. }
  948. return code;
  949. }
  950. Error ShaderCompilerGLES3::compile(VS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  951. Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), ShaderTypes::get_singleton()->get_types());
  952. if (err != OK) {
  953. Vector<String> shader = p_code.split("\n");
  954. for (int i = 0; i < shader.size(); i++) {
  955. if (i + 1 == parser.get_error_line()) {
  956. // Mark the error line to be visible without having to look at
  957. // the trace at the end.
  958. print_line(vformat("E%4d-> %s", i + 1, shader[i]));
  959. } else {
  960. print_line(vformat("%5d | %s", i + 1, shader[i]));
  961. }
  962. }
  963. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  964. return err;
  965. }
  966. r_gen_code.defines.clear();
  967. r_gen_code.vertex = String();
  968. r_gen_code.vertex_global = String();
  969. r_gen_code.fragment = String();
  970. r_gen_code.fragment_global = String();
  971. r_gen_code.light = String();
  972. r_gen_code.uses_fragment_time = false;
  973. r_gen_code.uses_vertex_time = false;
  974. used_name_defines.clear();
  975. used_rmode_defines.clear();
  976. used_flag_pointers.clear();
  977. fragment_varyings.clear();
  978. shader = parser.get_shader();
  979. function = nullptr;
  980. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions[p_mode], false);
  981. if (r_gen_code.uniform_total_size) { //uniforms used?
  982. int md = sizeof(float) * 4;
  983. if (r_gen_code.uniform_total_size % md) {
  984. r_gen_code.uniform_total_size += md - (r_gen_code.uniform_total_size % md);
  985. }
  986. r_gen_code.uniform_total_size += md; //pad just in case
  987. }
  988. return OK;
  989. }
  990. ShaderCompilerGLES3::ShaderCompilerGLES3() {
  991. /** CANVAS ITEM SHADER **/
  992. actions[VS::SHADER_CANVAS_ITEM].renames["VERTEX"] = "outvec.xy";
  993. actions[VS::SHADER_CANVAS_ITEM].renames["UV"] = "uv";
  994. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "point_size";
  995. actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix";
  996. actions[VS::SHADER_CANVAS_ITEM].renames["PROJECTION_MATRIX"] = "projection_matrix";
  997. actions[VS::SHADER_CANVAS_ITEM].renames["EXTRA_MATRIX"] = "extra_matrix";
  998. actions[VS::SHADER_CANVAS_ITEM].renames["TIME"] = "time";
  999. actions[VS::SHADER_CANVAS_ITEM].renames["AT_LIGHT_PASS"] = "at_light_pass";
  1000. actions[VS::SHADER_CANVAS_ITEM].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1001. actions[VS::SHADER_CANVAS_ITEM].renames["COLOR"] = "color";
  1002. actions[VS::SHADER_CANVAS_ITEM].renames["MODULATE"] = "final_modulate_alias";
  1003. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL"] = "normal";
  1004. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP"] = "normal_map";
  1005. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP_DEPTH"] = "normal_depth";
  1006. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE"] = "color_texture";
  1007. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE_PIXEL_SIZE"] = "color_texpixel_size";
  1008. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL_TEXTURE"] = "normal_texture";
  1009. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_UV"] = "screen_uv";
  1010. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_TEXTURE"] = "screen_texture";
  1011. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_PIXEL_SIZE"] = "screen_pixel_size";
  1012. actions[VS::SHADER_CANVAS_ITEM].renames["FRAGCOORD"] = "gl_FragCoord";
  1013. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_COORD"] = "gl_PointCoord";
  1014. actions[VS::SHADER_CANVAS_ITEM].renames["INSTANCE_ID"] = "gl_InstanceID";
  1015. actions[VS::SHADER_CANVAS_ITEM].renames["VERTEX_ID"] = "gl_VertexID";
  1016. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_VEC"] = "light_vec";
  1017. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_HEIGHT"] = "light_height";
  1018. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_COLOR"] = "light_color";
  1019. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv";
  1020. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light";
  1021. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color";
  1022. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_VEC"] = "shadow_vec";
  1023. actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n";
  1024. actions[VS::SHADER_CANVAS_ITEM].usage_defines["MODULATE"] = "#define MODULATE_USED\n";
  1025. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1026. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1027. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_PIXEL_SIZE"] = "@SCREEN_UV";
  1028. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n";
  1029. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
  1030. actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1031. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SHADOW_VEC"] = "#define SHADOW_VEC_USED\n";
  1032. actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1033. /** SPATIAL SHADER **/
  1034. actions[VS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  1035. actions[VS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  1036. actions[VS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  1037. actions[VS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  1038. actions[VS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1039. actions[VS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  1040. actions[VS::SHADER_SPATIAL].renames["VIEW_INDEX"] = "view_index";
  1041. actions[VS::SHADER_SPATIAL].renames["VIEW_MONO_LEFT"] = "0";
  1042. actions[VS::SHADER_SPATIAL].renames["VIEW_RIGHT"] = "1";
  1043. actions[VS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  1044. actions[VS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  1045. actions[VS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  1046. actions[VS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  1047. actions[VS::SHADER_SPATIAL].renames["POSITION"] = "position";
  1048. actions[VS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  1049. actions[VS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  1050. actions[VS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  1051. actions[VS::SHADER_SPATIAL].renames["POINT_SIZE"] = "point_size";
  1052. actions[VS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  1053. actions[VS::SHADER_SPATIAL].renames["VERTEX_ID"] = "gl_VertexID";
  1054. //builtins
  1055. actions[VS::SHADER_SPATIAL].renames["TIME"] = "time";
  1056. actions[VS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  1057. actions[VS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  1058. actions[VS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  1059. actions[VS::SHADER_SPATIAL].renames["NORMALMAP"] = "normalmap";
  1060. actions[VS::SHADER_SPATIAL].renames["NORMALMAP_DEPTH"] = "normaldepth";
  1061. actions[VS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  1062. actions[VS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  1063. actions[VS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  1064. actions[VS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  1065. actions[VS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  1066. actions[VS::SHADER_SPATIAL].renames["RIM"] = "rim";
  1067. actions[VS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  1068. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  1069. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  1070. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  1071. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1072. actions[VS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  1073. actions[VS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  1074. actions[VS::SHADER_SPATIAL].renames["AO"] = "ao";
  1075. actions[VS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1076. actions[VS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  1077. actions[VS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  1078. actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1079. actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  1080. actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  1081. actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  1082. actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  1083. actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  1084. actions[VS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1085. actions[VS::SHADER_SPATIAL].renames["NODE_POSITION_WORLD"] = "world_transform[3].xyz";
  1086. actions[VS::SHADER_SPATIAL].renames["CAMERA_POSITION_WORLD"] = "camera_matrix[3].xyz";
  1087. actions[VS::SHADER_SPATIAL].renames["CAMERA_DIRECTION_WORLD"] = "camera_inverse_matrix[3].xyz";
  1088. actions[VS::SHADER_SPATIAL].renames["NODE_POSITION_VIEW"] = "(camera_inverse_matrix * world_transform)[3].xyz";
  1089. //for light
  1090. actions[VS::SHADER_SPATIAL].renames["VIEW"] = "view";
  1091. actions[VS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  1092. actions[VS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  1093. actions[VS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  1094. actions[VS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1095. actions[VS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  1096. actions[VS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  1097. actions[VS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  1098. actions[VS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  1099. actions[VS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  1100. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  1101. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  1102. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  1103. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1104. actions[VS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  1105. actions[VS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  1106. actions[VS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  1107. actions[VS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  1108. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP"] = "#define ENABLE_NORMALMAP\n";
  1109. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP_DEPTH"] = "@NORMALMAP";
  1110. actions[VS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  1111. actions[VS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1112. actions[VS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  1113. actions[VS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1114. actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1115. actions[VS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  1116. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1117. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1118. actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1119. actions[VS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1120. actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1121. actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1122. actions[VS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1123. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1124. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1125. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  1126. if (!force_lambert) {
  1127. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1128. }
  1129. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  1130. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1131. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1132. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  1133. if (!force_blinn) {
  1134. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1135. } else {
  1136. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  1137. }
  1138. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  1139. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  1140. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1141. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1142. actions[VS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1143. actions[VS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1144. actions[VS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1145. /* PARTICLES SHADER */
  1146. actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  1147. actions[VS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  1148. actions[VS::SHADER_PARTICLES].renames["MASS"] = "mass";
  1149. actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  1150. actions[VS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  1151. actions[VS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  1152. actions[VS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  1153. actions[VS::SHADER_PARTICLES].renames["TIME"] = "time";
  1154. actions[VS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  1155. actions[VS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  1156. actions[VS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  1157. actions[VS::SHADER_PARTICLES].renames["INDEX"] = "index";
  1158. actions[VS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  1159. actions[VS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  1160. actions[VS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  1161. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1162. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1163. actions[VS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1164. vertex_name = "vertex";
  1165. fragment_name = "fragment";
  1166. light_name = "light";
  1167. time_name = "TIME";
  1168. List<String> func_list;
  1169. ShaderLanguage::get_builtin_funcs(&func_list);
  1170. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  1171. internal_functions.insert(E->get());
  1172. }
  1173. }