shader_compiler_rd.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_compiler_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "renderer_storage_rd.h"
  34. #include "servers/rendering_server.h"
  35. #define SL ShaderLanguage
  36. static String _mktab(int p_level) {
  37. String tb;
  38. for (int i = 0; i < p_level; i++) {
  39. tb += "\t";
  40. }
  41. return tb;
  42. }
  43. static String _typestr(SL::DataType p_type) {
  44. String type = ShaderLanguage::get_datatype_name(p_type);
  45. if (ShaderLanguage::is_sampler_type(p_type)) {
  46. type = type.replace("sampler", "texture"); //we use textures instead of samplers
  47. }
  48. return type;
  49. }
  50. static int _get_datatype_size(SL::DataType p_type) {
  51. switch (p_type) {
  52. case SL::TYPE_VOID:
  53. return 0;
  54. case SL::TYPE_BOOL:
  55. return 4;
  56. case SL::TYPE_BVEC2:
  57. return 8;
  58. case SL::TYPE_BVEC3:
  59. return 12;
  60. case SL::TYPE_BVEC4:
  61. return 16;
  62. case SL::TYPE_INT:
  63. return 4;
  64. case SL::TYPE_IVEC2:
  65. return 8;
  66. case SL::TYPE_IVEC3:
  67. return 12;
  68. case SL::TYPE_IVEC4:
  69. return 16;
  70. case SL::TYPE_UINT:
  71. return 4;
  72. case SL::TYPE_UVEC2:
  73. return 8;
  74. case SL::TYPE_UVEC3:
  75. return 12;
  76. case SL::TYPE_UVEC4:
  77. return 16;
  78. case SL::TYPE_FLOAT:
  79. return 4;
  80. case SL::TYPE_VEC2:
  81. return 8;
  82. case SL::TYPE_VEC3:
  83. return 12;
  84. case SL::TYPE_VEC4:
  85. return 16;
  86. case SL::TYPE_MAT2:
  87. return 32; //4 * 4 + 4 * 4
  88. case SL::TYPE_MAT3:
  89. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  90. case SL::TYPE_MAT4:
  91. return 64;
  92. case SL::TYPE_SAMPLER2D:
  93. return 16;
  94. case SL::TYPE_ISAMPLER2D:
  95. return 16;
  96. case SL::TYPE_USAMPLER2D:
  97. return 16;
  98. case SL::TYPE_SAMPLER2DARRAY:
  99. return 16;
  100. case SL::TYPE_ISAMPLER2DARRAY:
  101. return 16;
  102. case SL::TYPE_USAMPLER2DARRAY:
  103. return 16;
  104. case SL::TYPE_SAMPLER3D:
  105. return 16;
  106. case SL::TYPE_ISAMPLER3D:
  107. return 16;
  108. case SL::TYPE_USAMPLER3D:
  109. return 16;
  110. case SL::TYPE_SAMPLERCUBE:
  111. return 16;
  112. case SL::TYPE_SAMPLERCUBEARRAY:
  113. return 16;
  114. case SL::TYPE_STRUCT:
  115. return 0;
  116. case SL::TYPE_MAX: {
  117. ERR_FAIL_V(0);
  118. };
  119. }
  120. ERR_FAIL_V(0);
  121. }
  122. static int _get_datatype_alignment(SL::DataType p_type) {
  123. switch (p_type) {
  124. case SL::TYPE_VOID:
  125. return 0;
  126. case SL::TYPE_BOOL:
  127. return 4;
  128. case SL::TYPE_BVEC2:
  129. return 8;
  130. case SL::TYPE_BVEC3:
  131. return 16;
  132. case SL::TYPE_BVEC4:
  133. return 16;
  134. case SL::TYPE_INT:
  135. return 4;
  136. case SL::TYPE_IVEC2:
  137. return 8;
  138. case SL::TYPE_IVEC3:
  139. return 16;
  140. case SL::TYPE_IVEC4:
  141. return 16;
  142. case SL::TYPE_UINT:
  143. return 4;
  144. case SL::TYPE_UVEC2:
  145. return 8;
  146. case SL::TYPE_UVEC3:
  147. return 16;
  148. case SL::TYPE_UVEC4:
  149. return 16;
  150. case SL::TYPE_FLOAT:
  151. return 4;
  152. case SL::TYPE_VEC2:
  153. return 8;
  154. case SL::TYPE_VEC3:
  155. return 16;
  156. case SL::TYPE_VEC4:
  157. return 16;
  158. case SL::TYPE_MAT2:
  159. return 16;
  160. case SL::TYPE_MAT3:
  161. return 16;
  162. case SL::TYPE_MAT4:
  163. return 16;
  164. case SL::TYPE_SAMPLER2D:
  165. return 16;
  166. case SL::TYPE_ISAMPLER2D:
  167. return 16;
  168. case SL::TYPE_USAMPLER2D:
  169. return 16;
  170. case SL::TYPE_SAMPLER2DARRAY:
  171. return 16;
  172. case SL::TYPE_ISAMPLER2DARRAY:
  173. return 16;
  174. case SL::TYPE_USAMPLER2DARRAY:
  175. return 16;
  176. case SL::TYPE_SAMPLER3D:
  177. return 16;
  178. case SL::TYPE_ISAMPLER3D:
  179. return 16;
  180. case SL::TYPE_USAMPLER3D:
  181. return 16;
  182. case SL::TYPE_SAMPLERCUBE:
  183. return 16;
  184. case SL::TYPE_SAMPLERCUBEARRAY:
  185. return 16;
  186. case SL::TYPE_STRUCT:
  187. return 0;
  188. case SL::TYPE_MAX: {
  189. ERR_FAIL_V(0);
  190. }
  191. }
  192. ERR_FAIL_V(0);
  193. }
  194. static String _interpstr(SL::DataInterpolation p_interp) {
  195. switch (p_interp) {
  196. case SL::INTERPOLATION_FLAT:
  197. return "flat ";
  198. case SL::INTERPOLATION_SMOOTH:
  199. return "";
  200. }
  201. return "";
  202. }
  203. static String _prestr(SL::DataPrecision p_pres) {
  204. switch (p_pres) {
  205. case SL::PRECISION_LOWP:
  206. return "lowp ";
  207. case SL::PRECISION_MEDIUMP:
  208. return "mediump ";
  209. case SL::PRECISION_HIGHP:
  210. return "highp ";
  211. case SL::PRECISION_DEFAULT:
  212. return "";
  213. }
  214. return "";
  215. }
  216. static String _qualstr(SL::ArgumentQualifier p_qual) {
  217. switch (p_qual) {
  218. case SL::ARGUMENT_QUALIFIER_IN:
  219. return "";
  220. case SL::ARGUMENT_QUALIFIER_OUT:
  221. return "out ";
  222. case SL::ARGUMENT_QUALIFIER_INOUT:
  223. return "inout ";
  224. }
  225. return "";
  226. }
  227. static String _opstr(SL::Operator p_op) {
  228. return SL::get_operator_text(p_op);
  229. }
  230. static String _mkid(const String &p_id) {
  231. String id = "m_" + p_id.replace("__", "_dus_");
  232. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  233. }
  234. static String f2sp0(float p_float) {
  235. String num = rtoss(p_float);
  236. if (num.find(".") == -1 && num.find("e") == -1) {
  237. num += ".0";
  238. }
  239. return num;
  240. }
  241. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  242. switch (p_type) {
  243. case SL::TYPE_BOOL:
  244. return p_values[0].boolean ? "true" : "false";
  245. case SL::TYPE_BVEC2:
  246. case SL::TYPE_BVEC3:
  247. case SL::TYPE_BVEC4: {
  248. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  249. for (int i = 0; i < p_values.size(); i++) {
  250. if (i > 0) {
  251. text += ",";
  252. }
  253. text += p_values[i].boolean ? "true" : "false";
  254. }
  255. text += ")";
  256. return text;
  257. }
  258. case SL::TYPE_INT:
  259. return itos(p_values[0].sint);
  260. case SL::TYPE_IVEC2:
  261. case SL::TYPE_IVEC3:
  262. case SL::TYPE_IVEC4: {
  263. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  264. for (int i = 0; i < p_values.size(); i++) {
  265. if (i > 0) {
  266. text += ",";
  267. }
  268. text += itos(p_values[i].sint);
  269. }
  270. text += ")";
  271. return text;
  272. } break;
  273. case SL::TYPE_UINT:
  274. return itos(p_values[0].uint) + "u";
  275. case SL::TYPE_UVEC2:
  276. case SL::TYPE_UVEC3:
  277. case SL::TYPE_UVEC4: {
  278. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  279. for (int i = 0; i < p_values.size(); i++) {
  280. if (i > 0) {
  281. text += ",";
  282. }
  283. text += itos(p_values[i].uint) + "u";
  284. }
  285. text += ")";
  286. return text;
  287. } break;
  288. case SL::TYPE_FLOAT:
  289. return f2sp0(p_values[0].real);
  290. case SL::TYPE_VEC2:
  291. case SL::TYPE_VEC3:
  292. case SL::TYPE_VEC4: {
  293. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  294. for (int i = 0; i < p_values.size(); i++) {
  295. if (i > 0) {
  296. text += ",";
  297. }
  298. text += f2sp0(p_values[i].real);
  299. }
  300. text += ")";
  301. return text;
  302. } break;
  303. case SL::TYPE_MAT2:
  304. case SL::TYPE_MAT3:
  305. case SL::TYPE_MAT4: {
  306. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  307. for (int i = 0; i < p_values.size(); i++) {
  308. if (i > 0) {
  309. text += ",";
  310. }
  311. text += f2sp0(p_values[i].real);
  312. }
  313. text += ")";
  314. return text;
  315. } break;
  316. default:
  317. ERR_FAIL_V(String());
  318. }
  319. }
  320. String ShaderCompilerRD::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) {
  321. if (p_filter == ShaderLanguage::FILTER_DEFAULT) {
  322. ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String());
  323. p_filter = actions.default_filter;
  324. }
  325. if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) {
  326. ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String());
  327. p_repeat = actions.default_repeat;
  328. }
  329. return actions.sampler_array_name + "[" + itos(p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)) + "]";
  330. }
  331. 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) {
  332. int fidx = -1;
  333. for (int i = 0; i < p_node->functions.size(); i++) {
  334. if (p_node->functions[i].name == p_for_func) {
  335. fidx = i;
  336. break;
  337. }
  338. }
  339. ERR_FAIL_COND(fidx == -1);
  340. for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) {
  341. if (added.has(E->get())) {
  342. continue; //was added already
  343. }
  344. _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added);
  345. SL::FunctionNode *fnode = nullptr;
  346. for (int i = 0; i < p_node->functions.size(); i++) {
  347. if (p_node->functions[i].name == E->get()) {
  348. fnode = p_node->functions[i].function;
  349. break;
  350. }
  351. }
  352. ERR_FAIL_COND(!fnode);
  353. r_to_add += "\n";
  354. String header;
  355. if (fnode->return_type == SL::TYPE_STRUCT) {
  356. header = _mkid(fnode->return_struct_name) + " " + _mkid(fnode->name) + "(";
  357. } else {
  358. header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "(";
  359. }
  360. for (int i = 0; i < fnode->arguments.size(); i++) {
  361. if (i > 0) {
  362. header += ", ";
  363. }
  364. if (fnode->arguments[i].is_const) {
  365. header += "const ";
  366. }
  367. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  368. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  369. } else {
  370. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  371. }
  372. }
  373. header += ")\n";
  374. r_to_add += header;
  375. r_to_add += p_func_code[E->get()];
  376. added.insert(E->get());
  377. }
  378. }
  379. static String _get_global_variable_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
  380. switch (p_type) {
  381. case ShaderLanguage::TYPE_BOOL: {
  382. return "(" + p_buffer + "[" + p_index + "].x != 0.0)";
  383. }
  384. case ShaderLanguage::TYPE_BVEC2: {
  385. return "(notEqual(" + p_buffer + "[" + p_index + "].xy, vec2(0.0)))";
  386. }
  387. case ShaderLanguage::TYPE_BVEC3: {
  388. return "(notEqual(" + p_buffer + "[" + p_index + "].xyz, vec3(0.0)))";
  389. }
  390. case ShaderLanguage::TYPE_BVEC4: {
  391. return "(notEqual(" + p_buffer + "[" + p_index + "].xyzw, vec4(0.0)))";
  392. }
  393. case ShaderLanguage::TYPE_INT: {
  394. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
  395. }
  396. case ShaderLanguage::TYPE_IVEC2: {
  397. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
  398. }
  399. case ShaderLanguage::TYPE_IVEC3: {
  400. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
  401. }
  402. case ShaderLanguage::TYPE_IVEC4: {
  403. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
  404. }
  405. case ShaderLanguage::TYPE_UINT: {
  406. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)";
  407. }
  408. case ShaderLanguage::TYPE_UVEC2: {
  409. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)";
  410. }
  411. case ShaderLanguage::TYPE_UVEC3: {
  412. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)";
  413. }
  414. case ShaderLanguage::TYPE_UVEC4: {
  415. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)";
  416. }
  417. case ShaderLanguage::TYPE_FLOAT: {
  418. return "(" + p_buffer + "[" + p_index + "].x)";
  419. }
  420. case ShaderLanguage::TYPE_VEC2: {
  421. return "(" + p_buffer + "[" + p_index + "].xy)";
  422. }
  423. case ShaderLanguage::TYPE_VEC3: {
  424. return "(" + p_buffer + "[" + p_index + "].xyz)";
  425. }
  426. case ShaderLanguage::TYPE_VEC4: {
  427. return "(" + p_buffer + "[" + p_index + "].xyzw)";
  428. }
  429. case ShaderLanguage::TYPE_MAT2: {
  430. return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)";
  431. }
  432. case ShaderLanguage::TYPE_MAT3: {
  433. return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)";
  434. }
  435. case ShaderLanguage::TYPE_MAT4: {
  436. return "mat4(" + p_buffer + "[" + p_index + "].xyzw," + p_buffer + "[" + p_index + "+1].xyzw," + p_buffer + "[" + p_index + "+2].xyzw," + p_buffer + "[" + p_index + "+3].xyzw)";
  437. }
  438. default: {
  439. ERR_FAIL_V("void");
  440. }
  441. }
  442. }
  443. 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, bool p_use_scope) {
  444. String code;
  445. switch (p_node->type) {
  446. case SL::Node::TYPE_SHADER: {
  447. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  448. for (int i = 0; i < pnode->render_modes.size(); i++) {
  449. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  450. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
  451. used_rmode_defines.insert(pnode->render_modes[i]);
  452. }
  453. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  454. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  455. }
  456. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  457. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  458. *p.first = p.second;
  459. }
  460. }
  461. // structs
  462. for (int i = 0; i < pnode->vstructs.size(); i++) {
  463. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  464. String struct_code;
  465. struct_code += "struct ";
  466. struct_code += _mkid(pnode->vstructs[i].name);
  467. struct_code += " ";
  468. struct_code += "{\n";
  469. for (int j = 0; j < st->members.size(); j++) {
  470. SL::MemberNode *m = st->members[j];
  471. if (m->datatype == SL::TYPE_STRUCT) {
  472. struct_code += _mkid(m->struct_name);
  473. } else {
  474. struct_code += _prestr(m->precision);
  475. struct_code += _typestr(m->datatype);
  476. }
  477. struct_code += " ";
  478. struct_code += m->name;
  479. if (m->array_size > 0) {
  480. struct_code += "[";
  481. struct_code += itos(m->array_size);
  482. struct_code += "]";
  483. }
  484. struct_code += ";\n";
  485. }
  486. struct_code += "}";
  487. struct_code += ";\n";
  488. r_gen_code.vertex_global += struct_code;
  489. r_gen_code.fragment_global += struct_code;
  490. r_gen_code.compute_global += struct_code;
  491. }
  492. int max_texture_uniforms = 0;
  493. int max_uniforms = 0;
  494. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  495. if (SL::is_sampler_type(E->get().type)) {
  496. max_texture_uniforms++;
  497. } else {
  498. if (E->get().scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  499. continue; //instances are indexed directly, dont need index uniforms
  500. }
  501. max_uniforms++;
  502. }
  503. }
  504. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  505. Vector<int> uniform_sizes;
  506. Vector<int> uniform_alignments;
  507. Vector<StringName> uniform_defines;
  508. uniform_sizes.resize(max_uniforms);
  509. uniform_alignments.resize(max_uniforms);
  510. uniform_defines.resize(max_uniforms);
  511. bool uses_uniforms = false;
  512. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  513. String ucode;
  514. if (E->get().scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  515. //insert, but don't generate any code.
  516. p_actions.uniforms->insert(E->key(), E->get());
  517. continue; //instances are indexed directly, dont need index uniforms
  518. }
  519. if (SL::is_sampler_type(E->get().type)) {
  520. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + E->get().texture_order) + ") uniform ";
  521. }
  522. bool is_buffer_global = !SL::is_sampler_type(E->get().type) && E->get().scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  523. if (is_buffer_global) {
  524. //this is an integer to index the global table
  525. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  526. } else {
  527. ucode += _prestr(E->get().precision);
  528. ucode += _typestr(E->get().type);
  529. }
  530. ucode += " " + _mkid(E->key());
  531. ucode += ";\n";
  532. if (SL::is_sampler_type(E->get().type)) {
  533. r_gen_code.vertex_global += ucode;
  534. r_gen_code.fragment_global += ucode;
  535. r_gen_code.compute_global += ucode;
  536. GeneratedCode::Texture texture;
  537. texture.name = E->key();
  538. texture.hint = E->get().hint;
  539. texture.type = E->get().type;
  540. texture.filter = E->get().filter;
  541. texture.repeat = E->get().repeat;
  542. texture.global = E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  543. if (texture.global) {
  544. r_gen_code.uses_global_textures = true;
  545. }
  546. r_gen_code.texture_uniforms.write[E->get().texture_order] = texture;
  547. } else {
  548. if (!uses_uniforms) {
  549. r_gen_code.defines.push_back(String("#define USE_MATERIAL_UNIFORMS\n"));
  550. uses_uniforms = true;
  551. }
  552. uniform_defines.write[E->get().order] = ucode;
  553. if (is_buffer_global) {
  554. //globals are indices into the global table
  555. uniform_sizes.write[E->get().order] = _get_datatype_size(ShaderLanguage::TYPE_UINT);
  556. uniform_alignments.write[E->get().order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  557. } else {
  558. uniform_sizes.write[E->get().order] = _get_datatype_size(E->get().type);
  559. uniform_alignments.write[E->get().order] = _get_datatype_alignment(E->get().type);
  560. }
  561. }
  562. p_actions.uniforms->insert(E->key(), E->get());
  563. }
  564. for (int i = 0; i < max_uniforms; i++) {
  565. r_gen_code.uniforms += uniform_defines[i];
  566. }
  567. #if 1
  568. // add up
  569. int offset = 0;
  570. for (int i = 0; i < uniform_sizes.size(); i++) {
  571. int align = offset % uniform_alignments[i];
  572. if (align != 0) {
  573. offset += uniform_alignments[i] - align;
  574. }
  575. r_gen_code.uniform_offsets.push_back(offset);
  576. offset += uniform_sizes[i];
  577. }
  578. r_gen_code.uniform_total_size = offset;
  579. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  580. r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
  581. }
  582. #else
  583. // add up
  584. for (int i = 0; i < uniform_sizes.size(); i++) {
  585. if (i > 0) {
  586. int align = uniform_sizes[i - 1] % uniform_alignments[i];
  587. if (align != 0) {
  588. uniform_sizes[i - 1] += uniform_alignments[i] - align;
  589. }
  590. uniform_sizes[i] = uniform_sizes[i] + uniform_sizes[i - 1];
  591. }
  592. }
  593. //offset
  594. r_gen_code.uniform_offsets.resize(uniform_sizes.size());
  595. for (int i = 0; i < uniform_sizes.size(); i++) {
  596. if (i > 0)
  597. r_gen_code.uniform_offsets[i] = uniform_sizes[i - 1];
  598. else
  599. r_gen_code.uniform_offsets[i] = 0;
  600. }
  601. /*
  602. for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) {
  603. if (SL::is_sampler_type(E->get().type)) {
  604. continue;
  605. }
  606. }
  607. */
  608. if (uniform_sizes.size()) {
  609. r_gen_code.uniform_total_size = uniform_sizes[uniform_sizes.size() - 1];
  610. } else {
  611. r_gen_code.uniform_total_size = 0;
  612. }
  613. #endif
  614. uint32_t index = p_default_actions.base_varying_index;
  615. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  616. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  617. if (E->get().stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || E->get().stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  618. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(E->key(), E->get()));
  619. fragment_varyings.insert(E->key());
  620. continue;
  621. }
  622. String vcode;
  623. String interp_mode = _interpstr(E->get().interpolation);
  624. vcode += _prestr(E->get().precision);
  625. vcode += _typestr(E->get().type);
  626. vcode += " " + _mkid(E->key());
  627. if (E->get().array_size > 0) {
  628. vcode += "[";
  629. vcode += itos(E->get().array_size);
  630. vcode += "]";
  631. }
  632. vcode += ";\n";
  633. r_gen_code.vertex_global += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  634. r_gen_code.fragment_global += "layout(location=" + itos(index) + ") " + interp_mode + "in " + vcode;
  635. r_gen_code.compute_global += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  636. index++;
  637. }
  638. if (var_frag_to_light.size() > 0) {
  639. String gcode = "\n\nstruct {\n";
  640. for (List<Pair<StringName, SL::ShaderNode::Varying>>::Element *E = var_frag_to_light.front(); E; E = E->next()) {
  641. gcode += "\t" + _prestr(E->get().second.precision) + _typestr(E->get().second.type) + " " + _mkid(E->get().first);
  642. if (E->get().second.array_size > 0) {
  643. gcode += "[";
  644. gcode += itos(E->get().second.array_size);
  645. gcode += "]";
  646. }
  647. gcode += ";\n";
  648. }
  649. gcode += "} frag_to_light;\n";
  650. r_gen_code.fragment_global += gcode;
  651. }
  652. for (int i = 0; i < pnode->vconstants.size(); i++) {
  653. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  654. String gcode;
  655. gcode += "const ";
  656. gcode += _prestr(cnode.precision);
  657. if (cnode.type == SL::TYPE_STRUCT) {
  658. gcode += _mkid(cnode.type_str);
  659. } else {
  660. gcode += _typestr(cnode.type);
  661. }
  662. gcode += " " + _mkid(String(cnode.name));
  663. if (cnode.array_size > 0) {
  664. gcode += "[";
  665. gcode += itos(cnode.array_size);
  666. gcode += "]";
  667. }
  668. gcode += "=";
  669. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  670. gcode += ";\n";
  671. r_gen_code.vertex_global += gcode;
  672. r_gen_code.fragment_global += gcode;
  673. r_gen_code.compute_global += gcode;
  674. }
  675. Map<StringName, String> function_code;
  676. //code for functions
  677. for (int i = 0; i < pnode->functions.size(); i++) {
  678. SL::FunctionNode *fnode = pnode->functions[i].function;
  679. function = fnode;
  680. current_func_name = fnode->name;
  681. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  682. function = nullptr;
  683. }
  684. //place functions in actual code
  685. Set<StringName> added_vtx;
  686. Set<StringName> added_fragment; //share for light
  687. Set<StringName> added_compute; //share for light
  688. for (int i = 0; i < pnode->functions.size(); i++) {
  689. SL::FunctionNode *fnode = pnode->functions[i].function;
  690. function = fnode;
  691. current_func_name = fnode->name;
  692. if (fnode->name == vertex_name) {
  693. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  694. r_gen_code.vertex = function_code[vertex_name];
  695. }
  696. if (fnode->name == fragment_name) {
  697. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  698. r_gen_code.fragment = function_code[fragment_name];
  699. }
  700. if (fnode->name == light_name) {
  701. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  702. r_gen_code.light = function_code[light_name];
  703. }
  704. if (fnode->name == compute_name) {
  705. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.compute_global, added_compute);
  706. r_gen_code.compute = function_code[compute_name];
  707. }
  708. function = nullptr;
  709. }
  710. //code+=dump_node_code(pnode->body,p_level);
  711. } break;
  712. case SL::Node::TYPE_STRUCT: {
  713. } break;
  714. case SL::Node::TYPE_FUNCTION: {
  715. } break;
  716. case SL::Node::TYPE_BLOCK: {
  717. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  718. //variables
  719. if (!bnode->single_statement) {
  720. code += _mktab(p_level - 1) + "{\n";
  721. }
  722. for (int i = 0; i < bnode->statements.size(); i++) {
  723. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  724. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  725. code += scode; //use directly
  726. } else {
  727. code += _mktab(p_level) + scode + ";\n";
  728. }
  729. }
  730. if (!bnode->single_statement) {
  731. code += _mktab(p_level - 1) + "}\n";
  732. }
  733. } break;
  734. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  735. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  736. String declaration;
  737. if (vdnode->is_const) {
  738. declaration += "const ";
  739. }
  740. if (vdnode->datatype == SL::TYPE_STRUCT) {
  741. declaration += _mkid(vdnode->struct_name);
  742. } else {
  743. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  744. }
  745. for (int i = 0; i < vdnode->declarations.size(); i++) {
  746. if (i > 0) {
  747. declaration += ",";
  748. } else {
  749. declaration += " ";
  750. }
  751. declaration += _mkid(vdnode->declarations[i].name);
  752. if (vdnode->declarations[i].initializer) {
  753. declaration += "=";
  754. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  755. }
  756. }
  757. code += declaration;
  758. } break;
  759. case SL::Node::TYPE_VARIABLE: {
  760. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  761. bool use_fragment_varying = false;
  762. if (current_func_name != vertex_name) {
  763. if (p_assigning) {
  764. if (shader->varyings.has(vnode->name)) {
  765. use_fragment_varying = true;
  766. }
  767. } else {
  768. if (fragment_varyings.has(vnode->name)) {
  769. use_fragment_varying = true;
  770. }
  771. }
  772. }
  773. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  774. *p_actions.write_flag_pointers[vnode->name] = true;
  775. }
  776. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  777. String define = p_default_actions.usage_defines[vnode->name];
  778. if (define.begins_with("@")) {
  779. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  780. }
  781. r_gen_code.defines.push_back(define);
  782. used_name_defines.insert(vnode->name);
  783. }
  784. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  785. *p_actions.usage_flag_pointers[vnode->name] = true;
  786. used_flag_pointers.insert(vnode->name);
  787. }
  788. if (p_default_actions.renames.has(vnode->name)) {
  789. code = p_default_actions.renames[vnode->name];
  790. } else {
  791. if (shader->uniforms.has(vnode->name)) {
  792. //its a uniform!
  793. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  794. if (u.texture_order >= 0) {
  795. code = _mkid(vnode->name); //texture, use as is
  796. } else {
  797. //a scalar or vector
  798. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  799. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  800. //global variable, this means the code points to an index to the global table
  801. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  802. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  803. //instance variable, index it as such
  804. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  805. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  806. } else {
  807. //regular uniform, index from UBO
  808. code = actions.base_uniform_string + _mkid(vnode->name);
  809. }
  810. }
  811. } else {
  812. if (use_fragment_varying) {
  813. code = "frag_to_light.";
  814. }
  815. code += _mkid(vnode->name); //its something else (local var most likely) use as is
  816. }
  817. }
  818. if (vnode->name == time_name) {
  819. if (current_func_name == vertex_name) {
  820. r_gen_code.uses_vertex_time = true;
  821. }
  822. if (current_func_name == fragment_name || current_func_name == light_name) {
  823. r_gen_code.uses_fragment_time = true;
  824. }
  825. }
  826. } break;
  827. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  828. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  829. int sz = acnode->initializer.size();
  830. if (acnode->datatype == SL::TYPE_STRUCT) {
  831. code += _mkid(acnode->struct_name);
  832. } else {
  833. code += _typestr(acnode->datatype);
  834. }
  835. code += "[";
  836. code += itos(acnode->initializer.size());
  837. code += "]";
  838. code += "(";
  839. for (int i = 0; i < sz; i++) {
  840. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  841. if (i != sz - 1) {
  842. code += ", ";
  843. }
  844. }
  845. code += ")";
  846. } break;
  847. case SL::Node::TYPE_ARRAY_DECLARATION: {
  848. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  849. String declaration;
  850. if (adnode->is_const) {
  851. declaration += "const ";
  852. }
  853. if (adnode->datatype == SL::TYPE_STRUCT) {
  854. declaration += _mkid(adnode->struct_name);
  855. } else {
  856. declaration += _prestr(adnode->precision) + _typestr(adnode->datatype);
  857. }
  858. for (int i = 0; i < adnode->declarations.size(); i++) {
  859. if (i > 0) {
  860. declaration += ",";
  861. } else {
  862. declaration += " ";
  863. }
  864. declaration += _mkid(adnode->declarations[i].name);
  865. declaration += "[";
  866. if (adnode->size_expression != nullptr) {
  867. declaration += _dump_node_code(adnode->size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  868. } else {
  869. declaration += itos(adnode->declarations[i].size);
  870. }
  871. declaration += "]";
  872. int sz = adnode->declarations[i].initializer.size();
  873. if (sz > 0) {
  874. declaration += "=";
  875. if (adnode->datatype == SL::TYPE_STRUCT) {
  876. declaration += _mkid(adnode->struct_name);
  877. } else {
  878. declaration += _typestr(adnode->datatype);
  879. }
  880. declaration += "[";
  881. declaration += itos(sz);
  882. declaration += "]";
  883. declaration += "(";
  884. for (int j = 0; j < sz; j++) {
  885. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  886. if (j != sz - 1) {
  887. declaration += ", ";
  888. }
  889. }
  890. declaration += ")";
  891. }
  892. }
  893. code += declaration;
  894. } break;
  895. case SL::Node::TYPE_ARRAY: {
  896. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  897. bool use_fragment_varying = false;
  898. if (current_func_name != vertex_name) {
  899. if (anode->assign_expression != nullptr) {
  900. use_fragment_varying = true;
  901. } else {
  902. if (p_assigning) {
  903. if (shader->varyings.has(anode->name)) {
  904. use_fragment_varying = true;
  905. }
  906. } else {
  907. if (fragment_varyings.has(anode->name)) {
  908. use_fragment_varying = true;
  909. }
  910. }
  911. }
  912. }
  913. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  914. *p_actions.write_flag_pointers[anode->name] = true;
  915. }
  916. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  917. String define = p_default_actions.usage_defines[anode->name];
  918. if (define.begins_with("@")) {
  919. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  920. }
  921. r_gen_code.defines.push_back(define);
  922. used_name_defines.insert(anode->name);
  923. }
  924. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  925. *p_actions.usage_flag_pointers[anode->name] = true;
  926. used_flag_pointers.insert(anode->name);
  927. }
  928. if (p_default_actions.renames.has(anode->name)) {
  929. code = p_default_actions.renames[anode->name];
  930. } else {
  931. if (use_fragment_varying) {
  932. code = "frag_to_light.";
  933. }
  934. code += _mkid(anode->name);
  935. }
  936. if (anode->call_expression != nullptr) {
  937. code += ".";
  938. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  939. } else if (anode->index_expression != nullptr) {
  940. code += "[";
  941. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  942. code += "]";
  943. } else if (anode->assign_expression != nullptr) {
  944. code += "=";
  945. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  946. }
  947. if (anode->name == time_name) {
  948. if (current_func_name == vertex_name) {
  949. r_gen_code.uses_vertex_time = true;
  950. }
  951. if (current_func_name == fragment_name || current_func_name == light_name) {
  952. r_gen_code.uses_fragment_time = true;
  953. }
  954. }
  955. } break;
  956. case SL::Node::TYPE_CONSTANT: {
  957. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  958. if (cnode->array_size == 0) {
  959. return get_constant_text(cnode->datatype, cnode->values);
  960. } else {
  961. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  962. code += _mkid(cnode->struct_name);
  963. } else {
  964. code += _typestr(cnode->datatype);
  965. }
  966. code += "[";
  967. code += itos(cnode->array_size);
  968. code += "]";
  969. code += "(";
  970. for (int i = 0; i < cnode->array_size; i++) {
  971. if (i > 0) {
  972. code += ",";
  973. } else {
  974. code += "";
  975. }
  976. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  977. }
  978. code += ")";
  979. }
  980. } break;
  981. case SL::Node::TYPE_OPERATOR: {
  982. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  983. switch (onode->op) {
  984. case SL::OP_ASSIGN:
  985. case SL::OP_ASSIGN_ADD:
  986. case SL::OP_ASSIGN_SUB:
  987. case SL::OP_ASSIGN_MUL:
  988. case SL::OP_ASSIGN_DIV:
  989. case SL::OP_ASSIGN_SHIFT_LEFT:
  990. case SL::OP_ASSIGN_SHIFT_RIGHT:
  991. case SL::OP_ASSIGN_MOD:
  992. case SL::OP_ASSIGN_BIT_AND:
  993. case SL::OP_ASSIGN_BIT_OR:
  994. case SL::OP_ASSIGN_BIT_XOR:
  995. 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);
  996. break;
  997. case SL::OP_BIT_INVERT:
  998. case SL::OP_NEGATE:
  999. case SL::OP_NOT:
  1000. case SL::OP_DECREMENT:
  1001. case SL::OP_INCREMENT:
  1002. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1003. break;
  1004. case SL::OP_POST_DECREMENT:
  1005. case SL::OP_POST_INCREMENT:
  1006. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  1007. break;
  1008. case SL::OP_CALL:
  1009. case SL::OP_STRUCT:
  1010. case SL::OP_CONSTRUCT: {
  1011. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  1012. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  1013. bool is_texture_func = false;
  1014. if (onode->op == SL::OP_STRUCT) {
  1015. code += _mkid(vnode->name);
  1016. } else if (onode->op == SL::OP_CONSTRUCT) {
  1017. code += String(vnode->name);
  1018. } else {
  1019. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  1020. *p_actions.usage_flag_pointers[vnode->name] = true;
  1021. used_flag_pointers.insert(vnode->name);
  1022. }
  1023. if (internal_functions.has(vnode->name)) {
  1024. code += vnode->name;
  1025. is_texture_func = texture_functions.has(vnode->name);
  1026. } else if (p_default_actions.renames.has(vnode->name)) {
  1027. code += p_default_actions.renames[vnode->name];
  1028. } else {
  1029. code += _mkid(vnode->name);
  1030. }
  1031. }
  1032. code += "(";
  1033. for (int i = 1; i < onode->arguments.size(); i++) {
  1034. if (i > 1) {
  1035. code += ", ";
  1036. }
  1037. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1038. if (is_texture_func && i == 1 && onode->arguments[i]->type == SL::Node::TYPE_VARIABLE) {
  1039. //need to map from texture to sampler in order to sample
  1040. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  1041. StringName texture_uniform = varnode->name;
  1042. String sampler_name;
  1043. if (actions.custom_samplers.has(texture_uniform)) {
  1044. sampler_name = actions.custom_samplers[texture_uniform];
  1045. } else {
  1046. if (shader->uniforms.has(texture_uniform)) {
  1047. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  1048. } else {
  1049. bool found = false;
  1050. for (int j = 0; j < function->arguments.size(); j++) {
  1051. if (function->arguments[j].name == texture_uniform) {
  1052. if (function->arguments[j].tex_builtin_check) {
  1053. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  1054. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  1055. found = true;
  1056. break;
  1057. }
  1058. if (function->arguments[j].tex_argument_check) {
  1059. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1060. found = true;
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. if (!found) {
  1066. //function was most likely unused, so use anything (compiler will remove it anyway)
  1067. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1068. }
  1069. }
  1070. }
  1071. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  1072. } else {
  1073. code += node_code;
  1074. }
  1075. }
  1076. code += ")";
  1077. } break;
  1078. case SL::OP_INDEX: {
  1079. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1080. code += "[";
  1081. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1082. code += "]";
  1083. } break;
  1084. case SL::OP_SELECT_IF: {
  1085. code += "(";
  1086. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1087. code += "?";
  1088. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1089. code += ":";
  1090. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1091. code += ")";
  1092. } break;
  1093. default: {
  1094. if (p_use_scope) {
  1095. code += "(";
  1096. }
  1097. 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);
  1098. if (p_use_scope) {
  1099. code += ")";
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. } break;
  1105. case SL::Node::TYPE_CONTROL_FLOW: {
  1106. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1107. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1108. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1109. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1110. if (cfnode->blocks.size() == 2) {
  1111. code += _mktab(p_level) + "else\n";
  1112. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1113. }
  1114. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1115. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1116. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1117. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1118. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1119. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1120. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1121. code += _mktab(p_level) + "default:\n";
  1122. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1123. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1124. code += _mktab(p_level) + "do";
  1125. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1126. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1127. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1128. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1129. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1130. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1131. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1132. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1133. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1134. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1135. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1136. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1137. if (cfnode->expressions.size()) {
  1138. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1139. } else {
  1140. code = "return;";
  1141. }
  1142. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1143. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1144. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1145. used_flag_pointers.insert("DISCARD");
  1146. }
  1147. code = "discard;";
  1148. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1149. code = "continue;";
  1150. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1151. code = "break;";
  1152. }
  1153. } break;
  1154. case SL::Node::TYPE_MEMBER: {
  1155. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1156. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1157. if (mnode->index_expression != nullptr) {
  1158. code += "[";
  1159. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1160. code += "]";
  1161. } else if (mnode->assign_expression != nullptr) {
  1162. code += "=";
  1163. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1164. }
  1165. } break;
  1166. }
  1167. return code;
  1168. }
  1169. ShaderLanguage::DataType ShaderCompilerRD::_get_variable_type(const StringName &p_type) {
  1170. RS::GlobalVariableType gvt = ((RendererStorageRD *)(RendererStorage::base_singleton))->global_variable_get_type_internal(p_type);
  1171. return RS::global_variable_type_get_shader_datatype(gvt);
  1172. }
  1173. Error ShaderCompilerRD::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1174. 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(), _get_variable_type);
  1175. if (err != OK) {
  1176. Vector<String> shader = p_code.split("\n");
  1177. for (int i = 0; i < shader.size(); i++) {
  1178. print_line(itos(i + 1) + " " + shader[i]);
  1179. }
  1180. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  1181. return err;
  1182. }
  1183. r_gen_code.defines.clear();
  1184. r_gen_code.vertex = String();
  1185. r_gen_code.vertex_global = String();
  1186. r_gen_code.fragment = String();
  1187. r_gen_code.fragment_global = String();
  1188. r_gen_code.compute = String();
  1189. r_gen_code.compute_global = String();
  1190. r_gen_code.light = String();
  1191. r_gen_code.uses_fragment_time = false;
  1192. r_gen_code.uses_vertex_time = false;
  1193. r_gen_code.uses_global_textures = false;
  1194. used_name_defines.clear();
  1195. used_rmode_defines.clear();
  1196. used_flag_pointers.clear();
  1197. fragment_varyings.clear();
  1198. shader = parser.get_shader();
  1199. function = nullptr;
  1200. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1201. return OK;
  1202. }
  1203. void ShaderCompilerRD::initialize(DefaultIdentifierActions p_actions) {
  1204. actions = p_actions;
  1205. vertex_name = "vertex";
  1206. fragment_name = "fragment";
  1207. compute_name = "compute";
  1208. light_name = "light";
  1209. time_name = "TIME";
  1210. List<String> func_list;
  1211. ShaderLanguage::get_builtin_funcs(&func_list);
  1212. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  1213. internal_functions.insert(E->get());
  1214. }
  1215. texture_functions.insert("texture");
  1216. texture_functions.insert("textureProj");
  1217. texture_functions.insert("textureLod");
  1218. texture_functions.insert("textureProjLod");
  1219. texture_functions.insert("textureGrad");
  1220. texture_functions.insert("textureSize");
  1221. texture_functions.insert("texelFetch");
  1222. }
  1223. ShaderCompilerRD::ShaderCompilerRD() {
  1224. #if 0
  1225. /** SPATIAL SHADER **/
  1226. actions[RS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  1227. actions[RS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  1228. actions[RS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  1229. actions[RS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  1230. actions[RS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1231. actions[RS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  1232. actions[RS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  1233. actions[RS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  1234. actions[RS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  1235. actions[RS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  1236. actions[RS::SHADER_SPATIAL].renames["POSITION"] = "position";
  1237. actions[RS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  1238. actions[RS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  1239. actions[RS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  1240. actions[RS::SHADER_SPATIAL].renames["POINT_SIZE"] = "gl_PointSize";
  1241. actions[RS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  1242. //builtins
  1243. actions[RS::SHADER_SPATIAL].renames["TIME"] = "time";
  1244. actions[RS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  1245. actions[RS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  1246. actions[RS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  1247. actions[RS::SHADER_SPATIAL].renames["NORMAL_MAP"] = "normal_map";
  1248. actions[RS::SHADER_SPATIAL].renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
  1249. actions[RS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  1250. actions[RS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  1251. actions[RS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  1252. actions[RS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  1253. actions[RS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  1254. actions[RS::SHADER_SPATIAL].renames["RIM"] = "rim";
  1255. actions[RS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  1256. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  1257. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  1258. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  1259. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1260. actions[RS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  1261. actions[RS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  1262. actions[RS::SHADER_SPATIAL].renames["AO"] = "ao";
  1263. actions[RS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1264. actions[RS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  1265. actions[RS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  1266. actions[RS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1267. actions[RS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  1268. actions[RS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  1269. actions[RS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  1270. actions[RS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  1271. actions[RS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  1272. actions[RS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1273. //for light
  1274. actions[RS::SHADER_SPATIAL].renames["VIEW"] = "view";
  1275. actions[RS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  1276. actions[RS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  1277. actions[RS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  1278. actions[RS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1279. actions[RS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  1280. actions[RS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  1281. actions[RS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  1282. actions[RS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  1283. actions[RS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  1284. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  1285. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  1286. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  1287. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1288. actions[RS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  1289. actions[RS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  1290. actions[RS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  1291. actions[RS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  1292. actions[RS::SHADER_SPATIAL].usage_defines["NORMAL_MAP"] = "#define ENABLE_NORMAL_MAP\n";
  1293. actions[RS::SHADER_SPATIAL].usage_defines["NORMAL_MAP_DEPTH"] = "@NORMAL_MAP";
  1294. actions[RS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  1295. actions[RS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1296. actions[RS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  1297. actions[RS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1298. actions[RS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1299. actions[RS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  1300. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1301. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1302. actions[RS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1303. actions[RS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1304. actions[RS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1305. actions[RS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1306. actions[RS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1307. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1308. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1309. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  1310. if (!force_lambert) {
  1311. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1312. }
  1313. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  1314. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1315. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1316. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  1317. if (!force_blinn) {
  1318. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1319. } else {
  1320. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  1321. }
  1322. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  1323. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  1324. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1325. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1326. actions[RS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1327. actions[RS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1328. actions[RS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1329. /* PARTICLES SHADER */
  1330. actions[RS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  1331. actions[RS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  1332. actions[RS::SHADER_PARTICLES].renames["MASS"] = "mass";
  1333. actions[RS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  1334. actions[RS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  1335. actions[RS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  1336. actions[RS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  1337. actions[RS::SHADER_PARTICLES].renames["TIME"] = "time";
  1338. actions[RS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  1339. actions[RS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  1340. actions[RS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  1341. actions[RS::SHADER_PARTICLES].renames["INDEX"] = "index";
  1342. actions[RS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  1343. actions[RS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  1344. actions[RS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  1345. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1346. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1347. actions[RS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1348. #endif
  1349. }