shader_compiler_gles3.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*************************************************************************/
  2. /* shader_compiler_gles3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_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: return 0;
  47. case SL::TYPE_BOOL: return 4;
  48. case SL::TYPE_BVEC2: return 8;
  49. case SL::TYPE_BVEC3: return 12;
  50. case SL::TYPE_BVEC4: return 16;
  51. case SL::TYPE_INT: return 4;
  52. case SL::TYPE_IVEC2: return 8;
  53. case SL::TYPE_IVEC3: return 12;
  54. case SL::TYPE_IVEC4: return 16;
  55. case SL::TYPE_UINT: return 4;
  56. case SL::TYPE_UVEC2: return 8;
  57. case SL::TYPE_UVEC3: return 12;
  58. case SL::TYPE_UVEC4: return 16;
  59. case SL::TYPE_FLOAT: return 4;
  60. case SL::TYPE_VEC2: return 8;
  61. case SL::TYPE_VEC3: return 12;
  62. case SL::TYPE_VEC4: return 16;
  63. case SL::TYPE_MAT2:
  64. return 32; //4 * 4 + 4 * 4
  65. case SL::TYPE_MAT3:
  66. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  67. case SL::TYPE_MAT4: return 64;
  68. case SL::TYPE_SAMPLER2D: return 16;
  69. case SL::TYPE_ISAMPLER2D: return 16;
  70. case SL::TYPE_USAMPLER2D: return 16;
  71. case SL::TYPE_SAMPLER2DARRAY: return 16;
  72. case SL::TYPE_ISAMPLER2DARRAY: return 16;
  73. case SL::TYPE_USAMPLER2DARRAY: return 16;
  74. case SL::TYPE_SAMPLER3D: return 16;
  75. case SL::TYPE_ISAMPLER3D: return 16;
  76. case SL::TYPE_USAMPLER3D: return 16;
  77. case SL::TYPE_SAMPLERCUBE: return 16;
  78. }
  79. ERR_FAIL_V(0);
  80. }
  81. static int _get_datatype_alignment(SL::DataType p_type) {
  82. switch (p_type) {
  83. case SL::TYPE_VOID: return 0;
  84. case SL::TYPE_BOOL: return 4;
  85. case SL::TYPE_BVEC2: return 8;
  86. case SL::TYPE_BVEC3: return 16;
  87. case SL::TYPE_BVEC4: return 16;
  88. case SL::TYPE_INT: return 4;
  89. case SL::TYPE_IVEC2: return 8;
  90. case SL::TYPE_IVEC3: return 16;
  91. case SL::TYPE_IVEC4: return 16;
  92. case SL::TYPE_UINT: return 4;
  93. case SL::TYPE_UVEC2: return 8;
  94. case SL::TYPE_UVEC3: return 16;
  95. case SL::TYPE_UVEC4: return 16;
  96. case SL::TYPE_FLOAT: return 4;
  97. case SL::TYPE_VEC2: return 8;
  98. case SL::TYPE_VEC3: return 16;
  99. case SL::TYPE_VEC4: return 16;
  100. case SL::TYPE_MAT2: return 16;
  101. case SL::TYPE_MAT3: return 16;
  102. case SL::TYPE_MAT4: return 16;
  103. case SL::TYPE_SAMPLER2D: return 16;
  104. case SL::TYPE_ISAMPLER2D: return 16;
  105. case SL::TYPE_USAMPLER2D: return 16;
  106. case SL::TYPE_SAMPLER2DARRAY: return 16;
  107. case SL::TYPE_ISAMPLER2DARRAY: return 16;
  108. case SL::TYPE_USAMPLER2DARRAY: return 16;
  109. case SL::TYPE_SAMPLER3D: return 16;
  110. case SL::TYPE_ISAMPLER3D: return 16;
  111. case SL::TYPE_USAMPLER3D: return 16;
  112. case SL::TYPE_SAMPLERCUBE: return 16;
  113. }
  114. ERR_FAIL_V(0);
  115. }
  116. static String _interpstr(SL::DataInterpolation p_interp) {
  117. switch (p_interp) {
  118. case SL::INTERPOLATION_FLAT: return "flat ";
  119. case SL::INTERPOLATION_SMOOTH: return "";
  120. }
  121. return "";
  122. }
  123. static String _prestr(SL::DataPrecision p_pres) {
  124. switch (p_pres) {
  125. case SL::PRECISION_LOWP: return "lowp ";
  126. case SL::PRECISION_MEDIUMP: return "mediump ";
  127. case SL::PRECISION_HIGHP: return "highp ";
  128. case SL::PRECISION_DEFAULT: return "";
  129. }
  130. return "";
  131. }
  132. static String _qualstr(SL::ArgumentQualifier p_qual) {
  133. switch (p_qual) {
  134. case SL::ARGUMENT_QUALIFIER_IN: return "";
  135. case SL::ARGUMENT_QUALIFIER_OUT: return "out ";
  136. case SL::ARGUMENT_QUALIFIER_INOUT: return "inout ";
  137. }
  138. return "";
  139. }
  140. static String _opstr(SL::Operator p_op) {
  141. return SL::get_operator_text(p_op);
  142. }
  143. static String _mkid(const String &p_id) {
  144. String id = "m_" + p_id;
  145. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  146. }
  147. static String f2sp0(float p_float) {
  148. String num = rtoss(p_float);
  149. if (num.find(".") == -1 && num.find("e") == -1) {
  150. num += ".0";
  151. }
  152. return num;
  153. }
  154. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  155. switch (p_type) {
  156. case SL::TYPE_BOOL: return p_values[0].boolean ? "true" : "false";
  157. case SL::TYPE_BVEC2:
  158. case SL::TYPE_BVEC3:
  159. case SL::TYPE_BVEC4: {
  160. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  161. for (int i = 0; i < p_values.size(); i++) {
  162. if (i > 0)
  163. text += ",";
  164. text += p_values[i].boolean ? "true" : "false";
  165. }
  166. text += ")";
  167. return text;
  168. }
  169. case SL::TYPE_INT: return itos(p_values[0].sint);
  170. case SL::TYPE_IVEC2:
  171. case SL::TYPE_IVEC3:
  172. case SL::TYPE_IVEC4: {
  173. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  174. for (int i = 0; i < p_values.size(); i++) {
  175. if (i > 0)
  176. text += ",";
  177. text += itos(p_values[i].sint);
  178. }
  179. text += ")";
  180. return text;
  181. } break;
  182. case SL::TYPE_UINT: return itos(p_values[0].uint) + "u";
  183. case SL::TYPE_UVEC2:
  184. case SL::TYPE_UVEC3:
  185. case SL::TYPE_UVEC4: {
  186. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  187. for (int i = 0; i < p_values.size(); i++) {
  188. if (i > 0)
  189. text += ",";
  190. text += itos(p_values[i].uint) + "u";
  191. }
  192. text += ")";
  193. return text;
  194. } break;
  195. case SL::TYPE_FLOAT: return f2sp0(p_values[0].real);
  196. case SL::TYPE_VEC2:
  197. case SL::TYPE_VEC3:
  198. case SL::TYPE_VEC4: {
  199. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  200. for (int i = 0; i < p_values.size(); i++) {
  201. if (i > 0)
  202. text += ",";
  203. text += f2sp0(p_values[i].real);
  204. }
  205. text += ")";
  206. return text;
  207. } break;
  208. case SL::TYPE_MAT2:
  209. case SL::TYPE_MAT3:
  210. case SL::TYPE_MAT4: {
  211. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  212. for (int i = 0; i < p_values.size(); i++) {
  213. if (i > 0)
  214. text += ",";
  215. text += f2sp0(p_values[i].real);
  216. }
  217. text += ")";
  218. return text;
  219. } break;
  220. default: ERR_FAIL_V(String());
  221. }
  222. }
  223. void ShaderCompilerGLES3::_dump_function_deps(SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added) {
  224. int fidx = -1;
  225. for (int i = 0; i < p_node->functions.size(); i++) {
  226. if (p_node->functions[i].name == p_for_func) {
  227. fidx = i;
  228. break;
  229. }
  230. }
  231. ERR_FAIL_COND(fidx == -1);
  232. for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) {
  233. if (added.has(E->get())) {
  234. continue; //was added already
  235. }
  236. _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added);
  237. SL::FunctionNode *fnode = NULL;
  238. for (int i = 0; i < p_node->functions.size(); i++) {
  239. if (p_node->functions[i].name == E->get()) {
  240. fnode = p_node->functions[i].function;
  241. break;
  242. }
  243. }
  244. ERR_FAIL_COND(!fnode);
  245. r_to_add += "\n";
  246. String header;
  247. header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "(";
  248. for (int i = 0; i < fnode->arguments.size(); i++) {
  249. if (i > 0)
  250. header += ", ";
  251. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  252. }
  253. header += ")\n";
  254. r_to_add += header;
  255. r_to_add += p_func_code[E->get()];
  256. added.insert(E->get());
  257. }
  258. }
  259. String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning) {
  260. String code;
  261. switch (p_node->type) {
  262. case SL::Node::TYPE_SHADER: {
  263. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  264. for (int i = 0; i < pnode->render_modes.size(); i++) {
  265. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  266. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]].utf8());
  267. used_rmode_defines.insert(pnode->render_modes[i]);
  268. }
  269. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  270. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  271. }
  272. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  273. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  274. *p.first = p.second;
  275. }
  276. }
  277. int max_texture_uniforms = 0;
  278. int max_uniforms = 0;
  279. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  280. if (SL::is_sampler_type(E->get().type))
  281. max_texture_uniforms++;
  282. else
  283. max_uniforms++;
  284. }
  285. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  286. r_gen_code.texture_hints.resize(max_texture_uniforms);
  287. r_gen_code.texture_types.resize(max_texture_uniforms);
  288. Vector<int> uniform_sizes;
  289. Vector<int> uniform_alignments;
  290. Vector<StringName> uniform_defines;
  291. uniform_sizes.resize(max_uniforms);
  292. uniform_alignments.resize(max_uniforms);
  293. uniform_defines.resize(max_uniforms);
  294. bool uses_uniforms = false;
  295. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  296. String ucode;
  297. if (SL::is_sampler_type(E->get().type)) {
  298. ucode = "uniform ";
  299. }
  300. ucode += _prestr(E->get().precision);
  301. ucode += _typestr(E->get().type);
  302. ucode += " " + _mkid(E->key());
  303. ucode += ";\n";
  304. if (SL::is_sampler_type(E->get().type)) {
  305. r_gen_code.vertex_global += ucode;
  306. r_gen_code.fragment_global += ucode;
  307. r_gen_code.texture_uniforms.write[E->get().texture_order] = _mkid(E->key());
  308. r_gen_code.texture_hints.write[E->get().texture_order] = E->get().hint;
  309. r_gen_code.texture_types.write[E->get().texture_order] = E->get().type;
  310. } else {
  311. if (!uses_uniforms) {
  312. r_gen_code.defines.push_back(String("#define USE_MATERIAL\n").ascii());
  313. uses_uniforms = true;
  314. }
  315. uniform_defines.write[E->get().order] = ucode;
  316. uniform_sizes.write[E->get().order] = _get_datatype_size(E->get().type);
  317. uniform_alignments.write[E->get().order] = _get_datatype_alignment(E->get().type);
  318. }
  319. p_actions.uniforms->insert(E->key(), E->get());
  320. }
  321. for (int i = 0; i < max_uniforms; i++) {
  322. r_gen_code.uniforms += uniform_defines[i];
  323. }
  324. // add up
  325. int offset = 0;
  326. for (int i = 0; i < uniform_sizes.size(); i++) {
  327. int align = offset % uniform_alignments[i];
  328. if (align != 0) {
  329. offset += uniform_alignments[i] - align;
  330. }
  331. r_gen_code.uniform_offsets.push_back(offset);
  332. offset += uniform_sizes[i];
  333. }
  334. r_gen_code.uniform_total_size = offset;
  335. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  336. r_gen_code.uniform_total_size += r_gen_code.uniform_total_size % 16;
  337. }
  338. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  339. String vcode;
  340. String interp_mode = _interpstr(E->get().interpolation);
  341. vcode += _prestr(E->get().precision);
  342. vcode += _typestr(E->get().type);
  343. vcode += " " + _mkid(E->key());
  344. if (E->get().array_size > 0) {
  345. vcode += "[";
  346. vcode += itos(E->get().array_size);
  347. vcode += "]";
  348. }
  349. vcode += ";\n";
  350. r_gen_code.vertex_global += interp_mode + "out " + vcode;
  351. r_gen_code.fragment_global += interp_mode + "in " + vcode;
  352. }
  353. for (Map<StringName, SL::ShaderNode::Constant>::Element *E = pnode->constants.front(); E; E = E->next()) {
  354. String gcode;
  355. gcode += "const ";
  356. gcode += _prestr(E->get().precision);
  357. gcode += _typestr(E->get().type);
  358. gcode += " " + _mkid(E->key());
  359. gcode += "=";
  360. gcode += _dump_node_code(E->get().initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  361. gcode += ";\n";
  362. r_gen_code.vertex_global += gcode;
  363. r_gen_code.fragment_global += gcode;
  364. }
  365. Map<StringName, String> function_code;
  366. //code for functions
  367. for (int i = 0; i < pnode->functions.size(); i++) {
  368. SL::FunctionNode *fnode = pnode->functions[i].function;
  369. current_func_name = fnode->name;
  370. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  371. }
  372. //place functions in actual code
  373. Set<StringName> added_vtx;
  374. Set<StringName> added_fragment; //share for light
  375. for (int i = 0; i < pnode->functions.size(); i++) {
  376. SL::FunctionNode *fnode = pnode->functions[i].function;
  377. current_func_name = fnode->name;
  378. if (fnode->name == vertex_name) {
  379. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  380. r_gen_code.vertex = function_code[vertex_name];
  381. }
  382. if (fnode->name == fragment_name) {
  383. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  384. r_gen_code.fragment = function_code[fragment_name];
  385. }
  386. if (fnode->name == light_name) {
  387. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  388. r_gen_code.light = function_code[light_name];
  389. }
  390. }
  391. //code+=dump_node_code(pnode->body,p_level);
  392. } break;
  393. case SL::Node::TYPE_FUNCTION: {
  394. } break;
  395. case SL::Node::TYPE_BLOCK: {
  396. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  397. //variables
  398. if (!bnode->single_statement) {
  399. code += _mktab(p_level - 1) + "{\n";
  400. }
  401. for (int i = 0; i < bnode->statements.size(); i++) {
  402. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  403. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  404. code += scode; //use directly
  405. } else {
  406. code += _mktab(p_level) + scode + ";\n";
  407. }
  408. }
  409. if (!bnode->single_statement) {
  410. code += _mktab(p_level - 1) + "}\n";
  411. }
  412. } break;
  413. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  414. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  415. String declaration;
  416. if (vdnode->is_const) {
  417. declaration += "const ";
  418. }
  419. declaration += _prestr(vdnode->precision);
  420. declaration += _typestr(vdnode->datatype);
  421. for (int i = 0; i < vdnode->declarations.size(); i++) {
  422. if (i > 0) {
  423. declaration += ",";
  424. } else {
  425. declaration += " ";
  426. }
  427. declaration += _mkid(vdnode->declarations[i].name);
  428. if (vdnode->declarations[i].initializer) {
  429. declaration += "=";
  430. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  431. }
  432. }
  433. code += declaration;
  434. } break;
  435. case SL::Node::TYPE_VARIABLE: {
  436. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  437. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  438. *p_actions.write_flag_pointers[vnode->name] = true;
  439. }
  440. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  441. String define = p_default_actions.usage_defines[vnode->name];
  442. if (define.begins_with("@")) {
  443. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  444. }
  445. r_gen_code.defines.push_back(define.utf8());
  446. used_name_defines.insert(vnode->name);
  447. }
  448. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  449. *p_actions.usage_flag_pointers[vnode->name] = true;
  450. used_flag_pointers.insert(vnode->name);
  451. }
  452. if (p_default_actions.renames.has(vnode->name))
  453. code = p_default_actions.renames[vnode->name];
  454. else
  455. code = _mkid(vnode->name);
  456. if (vnode->name == time_name) {
  457. if (current_func_name == vertex_name) {
  458. r_gen_code.uses_vertex_time = true;
  459. }
  460. if (current_func_name == fragment_name || current_func_name == light_name) {
  461. r_gen_code.uses_fragment_time = true;
  462. }
  463. }
  464. } break;
  465. case SL::Node::TYPE_ARRAY_DECLARATION: {
  466. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  467. String declaration;
  468. if (adnode->is_const) {
  469. declaration += "const ";
  470. }
  471. declaration += _prestr(adnode->precision);
  472. declaration += _typestr(adnode->datatype);
  473. for (int i = 0; i < adnode->declarations.size(); i++) {
  474. if (i > 0) {
  475. declaration += ",";
  476. } else {
  477. declaration += " ";
  478. }
  479. declaration += _mkid(adnode->declarations[i].name);
  480. declaration += "[";
  481. declaration += itos(adnode->declarations[i].size);
  482. declaration += "]";
  483. int sz = adnode->declarations[i].initializer.size();
  484. if (sz > 0) {
  485. declaration += "=";
  486. declaration += _typestr(adnode->datatype);
  487. declaration += "[";
  488. declaration += itos(sz);
  489. declaration += "]";
  490. declaration += "(";
  491. for (int j = 0; j < sz; j++) {
  492. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  493. if (j != sz - 1) {
  494. declaration += ", ";
  495. }
  496. }
  497. declaration += ")";
  498. }
  499. }
  500. code += declaration;
  501. } break;
  502. case SL::Node::TYPE_ARRAY: {
  503. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  504. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  505. *p_actions.write_flag_pointers[anode->name] = true;
  506. }
  507. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  508. String define = p_default_actions.usage_defines[anode->name];
  509. if (define.begins_with("@")) {
  510. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  511. }
  512. r_gen_code.defines.push_back(define.utf8());
  513. used_name_defines.insert(anode->name);
  514. }
  515. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  516. *p_actions.usage_flag_pointers[anode->name] = true;
  517. used_flag_pointers.insert(anode->name);
  518. }
  519. if (p_default_actions.renames.has(anode->name))
  520. code = p_default_actions.renames[anode->name];
  521. else
  522. code = _mkid(anode->name);
  523. if (anode->call_expression != NULL) {
  524. code += ".";
  525. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  526. }
  527. if (anode->index_expression != NULL) {
  528. code += "[";
  529. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  530. code += "]";
  531. }
  532. if (anode->name == time_name) {
  533. if (current_func_name == vertex_name) {
  534. r_gen_code.uses_vertex_time = true;
  535. }
  536. if (current_func_name == fragment_name || current_func_name == light_name) {
  537. r_gen_code.uses_fragment_time = true;
  538. }
  539. }
  540. } break;
  541. case SL::Node::TYPE_CONSTANT: {
  542. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  543. return get_constant_text(cnode->datatype, cnode->values);
  544. } break;
  545. case SL::Node::TYPE_OPERATOR: {
  546. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  547. switch (onode->op) {
  548. case SL::OP_ASSIGN:
  549. case SL::OP_ASSIGN_ADD:
  550. case SL::OP_ASSIGN_SUB:
  551. case SL::OP_ASSIGN_MUL:
  552. case SL::OP_ASSIGN_DIV:
  553. case SL::OP_ASSIGN_SHIFT_LEFT:
  554. case SL::OP_ASSIGN_SHIFT_RIGHT:
  555. case SL::OP_ASSIGN_MOD:
  556. case SL::OP_ASSIGN_BIT_AND:
  557. case SL::OP_ASSIGN_BIT_OR:
  558. case SL::OP_ASSIGN_BIT_XOR:
  559. 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);
  560. break;
  561. case SL::OP_BIT_INVERT:
  562. case SL::OP_NEGATE:
  563. case SL::OP_NOT:
  564. case SL::OP_DECREMENT:
  565. case SL::OP_INCREMENT:
  566. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  567. break;
  568. case SL::OP_POST_DECREMENT:
  569. case SL::OP_POST_INCREMENT:
  570. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  571. break;
  572. case SL::OP_CALL:
  573. case SL::OP_CONSTRUCT: {
  574. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  575. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  576. if (onode->op == SL::OP_CONSTRUCT) {
  577. code += String(vnode->name);
  578. } else {
  579. if (internal_functions.has(vnode->name)) {
  580. code += vnode->name;
  581. } else if (p_default_actions.renames.has(vnode->name)) {
  582. code += p_default_actions.renames[vnode->name];
  583. } else {
  584. code += _mkid(vnode->name);
  585. }
  586. }
  587. code += "(";
  588. for (int i = 1; i < onode->arguments.size(); i++) {
  589. if (i > 1)
  590. code += ", ";
  591. code += _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  592. }
  593. code += ")";
  594. } break;
  595. case SL::OP_INDEX: {
  596. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  597. code += "[";
  598. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  599. code += "]";
  600. } break;
  601. case SL::OP_SELECT_IF: {
  602. code += "(";
  603. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  604. code += "?";
  605. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  606. code += ":";
  607. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  608. code += ")";
  609. } break;
  610. default: {
  611. 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) + ")";
  612. break;
  613. }
  614. }
  615. } break;
  616. case SL::Node::TYPE_CONTROL_FLOW: {
  617. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  618. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  619. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  620. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  621. if (cfnode->blocks.size() == 2) {
  622. code += _mktab(p_level) + "else\n";
  623. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  624. }
  625. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  626. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  627. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  628. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  629. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  630. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  631. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  632. code += _mktab(p_level) + "default:\n";
  633. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  634. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  635. code += _mktab(p_level) + "do";
  636. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  637. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  638. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  639. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  640. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  641. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  642. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  643. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  644. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  645. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  646. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  647. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  648. if (cfnode->expressions.size()) {
  649. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  650. } else {
  651. code = "return;";
  652. }
  653. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  654. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  655. *p_actions.usage_flag_pointers["DISCARD"] = true;
  656. used_flag_pointers.insert("DISCARD");
  657. }
  658. code = "discard;";
  659. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  660. code = "continue;";
  661. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  662. code = "break;";
  663. }
  664. } break;
  665. case SL::Node::TYPE_MEMBER: {
  666. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  667. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  668. } break;
  669. }
  670. return code;
  671. }
  672. Error ShaderCompilerGLES3::compile(VS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  673. 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());
  674. if (err != OK) {
  675. Vector<String> shader = p_code.split("\n");
  676. for (int i = 0; i < shader.size(); i++) {
  677. print_line(itos(i + 1) + " " + shader[i]);
  678. }
  679. _err_print_error(NULL, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  680. return err;
  681. }
  682. r_gen_code.defines.clear();
  683. r_gen_code.vertex = String();
  684. r_gen_code.vertex_global = String();
  685. r_gen_code.fragment = String();
  686. r_gen_code.fragment_global = String();
  687. r_gen_code.light = String();
  688. r_gen_code.uses_fragment_time = false;
  689. r_gen_code.uses_vertex_time = false;
  690. used_name_defines.clear();
  691. used_rmode_defines.clear();
  692. used_flag_pointers.clear();
  693. _dump_node_code(parser.get_shader(), 1, r_gen_code, *p_actions, actions[p_mode], false);
  694. if (r_gen_code.uniform_total_size) { //uniforms used?
  695. int md = sizeof(float) * 4;
  696. if (r_gen_code.uniform_total_size % md) {
  697. r_gen_code.uniform_total_size += md - (r_gen_code.uniform_total_size % md);
  698. }
  699. r_gen_code.uniform_total_size += md; //pad just in case
  700. }
  701. return OK;
  702. }
  703. ShaderCompilerGLES3::ShaderCompilerGLES3() {
  704. /** CANVAS ITEM SHADER **/
  705. actions[VS::SHADER_CANVAS_ITEM].renames["VERTEX"] = "outvec.xy";
  706. actions[VS::SHADER_CANVAS_ITEM].renames["UV"] = "uv";
  707. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "point_size";
  708. actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix";
  709. actions[VS::SHADER_CANVAS_ITEM].renames["PROJECTION_MATRIX"] = "projection_matrix";
  710. actions[VS::SHADER_CANVAS_ITEM].renames["EXTRA_MATRIX"] = "extra_matrix";
  711. actions[VS::SHADER_CANVAS_ITEM].renames["TIME"] = "time";
  712. actions[VS::SHADER_CANVAS_ITEM].renames["AT_LIGHT_PASS"] = "at_light_pass";
  713. actions[VS::SHADER_CANVAS_ITEM].renames["INSTANCE_CUSTOM"] = "instance_custom";
  714. actions[VS::SHADER_CANVAS_ITEM].renames["COLOR"] = "color";
  715. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL"] = "normal";
  716. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP"] = "normal_map";
  717. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP_DEPTH"] = "normal_depth";
  718. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE"] = "color_texture";
  719. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE_PIXEL_SIZE"] = "color_texpixel_size";
  720. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL_TEXTURE"] = "normal_texture";
  721. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_UV"] = "screen_uv";
  722. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_TEXTURE"] = "screen_texture";
  723. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_PIXEL_SIZE"] = "screen_pixel_size";
  724. actions[VS::SHADER_CANVAS_ITEM].renames["FRAGCOORD"] = "gl_FragCoord";
  725. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_COORD"] = "gl_PointCoord";
  726. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_VEC"] = "light_vec";
  727. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_HEIGHT"] = "light_height";
  728. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_COLOR"] = "light_color";
  729. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv";
  730. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light";
  731. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color";
  732. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_VEC"] = "shadow_vec";
  733. actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n";
  734. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  735. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  736. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_PIXEL_SIZE"] = "@SCREEN_UV";
  737. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n";
  738. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
  739. actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  740. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SHADOW_VEC"] = "#define SHADOW_VEC_USED\n";
  741. actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  742. /** SPATIAL SHADER **/
  743. actions[VS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  744. actions[VS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  745. actions[VS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  746. actions[VS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  747. actions[VS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  748. actions[VS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  749. actions[VS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  750. actions[VS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  751. actions[VS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  752. actions[VS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  753. actions[VS::SHADER_SPATIAL].renames["POSITION"] = "position";
  754. actions[VS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  755. actions[VS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  756. actions[VS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  757. actions[VS::SHADER_SPATIAL].renames["POINT_SIZE"] = "point_size";
  758. actions[VS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  759. //builtins
  760. actions[VS::SHADER_SPATIAL].renames["TIME"] = "time";
  761. actions[VS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  762. actions[VS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  763. actions[VS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  764. actions[VS::SHADER_SPATIAL].renames["NORMALMAP"] = "normalmap";
  765. actions[VS::SHADER_SPATIAL].renames["NORMALMAP_DEPTH"] = "normaldepth";
  766. actions[VS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  767. actions[VS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  768. actions[VS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  769. actions[VS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  770. actions[VS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  771. actions[VS::SHADER_SPATIAL].renames["RIM"] = "rim";
  772. actions[VS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  773. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  774. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  775. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  776. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  777. actions[VS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  778. actions[VS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  779. actions[VS::SHADER_SPATIAL].renames["AO"] = "ao";
  780. actions[VS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  781. actions[VS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  782. actions[VS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  783. actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  784. actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  785. actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  786. actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  787. actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  788. actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  789. actions[VS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  790. //for light
  791. actions[VS::SHADER_SPATIAL].renames["VIEW"] = "view";
  792. actions[VS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  793. actions[VS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  794. actions[VS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  795. actions[VS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  796. actions[VS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  797. actions[VS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  798. actions[VS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  799. actions[VS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  800. actions[VS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  801. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  802. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  803. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  804. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  805. actions[VS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  806. actions[VS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  807. actions[VS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  808. actions[VS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  809. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP"] = "#define ENABLE_NORMALMAP\n";
  810. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP_DEPTH"] = "@NORMALMAP";
  811. actions[VS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  812. actions[VS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  813. actions[VS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  814. actions[VS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  815. actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  816. actions[VS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  817. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  818. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  819. actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  820. actions[VS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  821. actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  822. actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  823. actions[VS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  824. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  825. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  826. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  827. if (!force_lambert) {
  828. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  829. }
  830. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  831. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  832. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  833. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  834. if (!force_blinn) {
  835. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  836. } else {
  837. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  838. }
  839. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  840. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  841. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  842. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  843. actions[VS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  844. actions[VS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  845. actions[VS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  846. /* PARTICLES SHADER */
  847. actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  848. actions[VS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  849. actions[VS::SHADER_PARTICLES].renames["MASS"] = "mass";
  850. actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  851. actions[VS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  852. actions[VS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  853. actions[VS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  854. actions[VS::SHADER_PARTICLES].renames["TIME"] = "time";
  855. actions[VS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  856. actions[VS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  857. actions[VS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  858. actions[VS::SHADER_PARTICLES].renames["INDEX"] = "index";
  859. actions[VS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  860. actions[VS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  861. actions[VS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  862. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  863. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  864. actions[VS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  865. vertex_name = "vertex";
  866. fragment_name = "fragment";
  867. light_name = "light";
  868. time_name = "TIME";
  869. List<String> func_list;
  870. ShaderLanguage::get_builtin_funcs(&func_list);
  871. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  872. internal_functions.insert(E->get());
  873. }
  874. }