shader_compiler_rd.cpp 42 KB

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