shader_compiler_rd.cpp 43 KB

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