shader_compiler_rd.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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, bool p_force_highp = false) {
  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 p_force_highp ? "highp " : "";
  213. }
  214. return "";
  215. }
  216. static String _constr(bool p_is_const) {
  217. if (p_is_const) {
  218. return "const ";
  219. }
  220. return "";
  221. }
  222. static String _qualstr(SL::ArgumentQualifier p_qual) {
  223. switch (p_qual) {
  224. case SL::ARGUMENT_QUALIFIER_IN:
  225. return "";
  226. case SL::ARGUMENT_QUALIFIER_OUT:
  227. return "out ";
  228. case SL::ARGUMENT_QUALIFIER_INOUT:
  229. return "inout ";
  230. }
  231. return "";
  232. }
  233. static String _opstr(SL::Operator p_op) {
  234. return SL::get_operator_text(p_op);
  235. }
  236. static String _mkid(const String &p_id) {
  237. String id = "m_" + p_id.replace("__", "_dus_");
  238. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  239. }
  240. static String f2sp0(float p_float) {
  241. String num = rtoss(p_float);
  242. if (num.find(".") == -1 && num.find("e") == -1) {
  243. num += ".0";
  244. }
  245. return num;
  246. }
  247. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  248. switch (p_type) {
  249. case SL::TYPE_BOOL:
  250. return p_values[0].boolean ? "true" : "false";
  251. case SL::TYPE_BVEC2:
  252. case SL::TYPE_BVEC3:
  253. case SL::TYPE_BVEC4: {
  254. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  255. for (int i = 0; i < p_values.size(); i++) {
  256. if (i > 0) {
  257. text += ",";
  258. }
  259. text += p_values[i].boolean ? "true" : "false";
  260. }
  261. text += ")";
  262. return text;
  263. }
  264. case SL::TYPE_INT:
  265. return itos(p_values[0].sint);
  266. case SL::TYPE_IVEC2:
  267. case SL::TYPE_IVEC3:
  268. case SL::TYPE_IVEC4: {
  269. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  270. for (int i = 0; i < p_values.size(); i++) {
  271. if (i > 0) {
  272. text += ",";
  273. }
  274. text += itos(p_values[i].sint);
  275. }
  276. text += ")";
  277. return text;
  278. } break;
  279. case SL::TYPE_UINT:
  280. return itos(p_values[0].uint) + "u";
  281. case SL::TYPE_UVEC2:
  282. case SL::TYPE_UVEC3:
  283. case SL::TYPE_UVEC4: {
  284. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  285. for (int i = 0; i < p_values.size(); i++) {
  286. if (i > 0) {
  287. text += ",";
  288. }
  289. text += itos(p_values[i].uint) + "u";
  290. }
  291. text += ")";
  292. return text;
  293. } break;
  294. case SL::TYPE_FLOAT:
  295. return f2sp0(p_values[0].real);
  296. case SL::TYPE_VEC2:
  297. case SL::TYPE_VEC3:
  298. case SL::TYPE_VEC4: {
  299. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  300. for (int i = 0; i < p_values.size(); i++) {
  301. if (i > 0) {
  302. text += ",";
  303. }
  304. text += f2sp0(p_values[i].real);
  305. }
  306. text += ")";
  307. return text;
  308. } break;
  309. case SL::TYPE_MAT2:
  310. case SL::TYPE_MAT3:
  311. case SL::TYPE_MAT4: {
  312. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  313. for (int i = 0; i < p_values.size(); i++) {
  314. if (i > 0) {
  315. text += ",";
  316. }
  317. text += f2sp0(p_values[i].real);
  318. }
  319. text += ")";
  320. return text;
  321. } break;
  322. default:
  323. ERR_FAIL_V(String());
  324. }
  325. }
  326. String ShaderCompilerRD::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) {
  327. if (p_filter == ShaderLanguage::FILTER_DEFAULT) {
  328. ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String());
  329. p_filter = actions.default_filter;
  330. }
  331. if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) {
  332. ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String());
  333. p_repeat = actions.default_repeat;
  334. }
  335. return actions.sampler_array_name + "[" + itos(p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)) + "]";
  336. }
  337. 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) {
  338. int fidx = -1;
  339. for (int i = 0; i < p_node->functions.size(); i++) {
  340. if (p_node->functions[i].name == p_for_func) {
  341. fidx = i;
  342. break;
  343. }
  344. }
  345. ERR_FAIL_COND(fidx == -1);
  346. Vector<StringName> uses_functions;
  347. for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) {
  348. uses_functions.push_back(E->get());
  349. }
  350. uses_functions.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  351. for (int k = 0; k < uses_functions.size(); k++) {
  352. if (added.has(uses_functions[k])) {
  353. continue; //was added already
  354. }
  355. _dump_function_deps(p_node, uses_functions[k], p_func_code, r_to_add, added);
  356. SL::FunctionNode *fnode = nullptr;
  357. for (int i = 0; i < p_node->functions.size(); i++) {
  358. if (p_node->functions[i].name == uses_functions[k]) {
  359. fnode = p_node->functions[i].function;
  360. break;
  361. }
  362. }
  363. ERR_FAIL_COND(!fnode);
  364. r_to_add += "\n";
  365. String header;
  366. if (fnode->return_type == SL::TYPE_STRUCT) {
  367. header = _mkid(fnode->return_struct_name);
  368. } else {
  369. header = _typestr(fnode->return_type);
  370. }
  371. if (fnode->return_array_size > 0) {
  372. header += "[";
  373. header += itos(fnode->return_array_size);
  374. header += "]";
  375. }
  376. header += " ";
  377. header += _mkid(fnode->name);
  378. header += "(";
  379. for (int i = 0; i < fnode->arguments.size(); i++) {
  380. if (i > 0) {
  381. header += ", ";
  382. }
  383. header += _constr(fnode->arguments[i].is_const);
  384. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  385. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  386. } else {
  387. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  388. }
  389. if (fnode->arguments[i].array_size > 0) {
  390. header += "[";
  391. header += itos(fnode->arguments[i].array_size);
  392. header += "]";
  393. }
  394. }
  395. header += ")\n";
  396. r_to_add += header;
  397. r_to_add += p_func_code[uses_functions[k]];
  398. added.insert(uses_functions[k]);
  399. }
  400. }
  401. static String _get_global_variable_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
  402. switch (p_type) {
  403. case ShaderLanguage::TYPE_BOOL: {
  404. return "(" + p_buffer + "[" + p_index + "].x != 0.0)";
  405. }
  406. case ShaderLanguage::TYPE_BVEC2: {
  407. return "(notEqual(" + p_buffer + "[" + p_index + "].xy, vec2(0.0)))";
  408. }
  409. case ShaderLanguage::TYPE_BVEC3: {
  410. return "(notEqual(" + p_buffer + "[" + p_index + "].xyz, vec3(0.0)))";
  411. }
  412. case ShaderLanguage::TYPE_BVEC4: {
  413. return "(notEqual(" + p_buffer + "[" + p_index + "].xyzw, vec4(0.0)))";
  414. }
  415. case ShaderLanguage::TYPE_INT: {
  416. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
  417. }
  418. case ShaderLanguage::TYPE_IVEC2: {
  419. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
  420. }
  421. case ShaderLanguage::TYPE_IVEC3: {
  422. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
  423. }
  424. case ShaderLanguage::TYPE_IVEC4: {
  425. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
  426. }
  427. case ShaderLanguage::TYPE_UINT: {
  428. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)";
  429. }
  430. case ShaderLanguage::TYPE_UVEC2: {
  431. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)";
  432. }
  433. case ShaderLanguage::TYPE_UVEC3: {
  434. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)";
  435. }
  436. case ShaderLanguage::TYPE_UVEC4: {
  437. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)";
  438. }
  439. case ShaderLanguage::TYPE_FLOAT: {
  440. return "(" + p_buffer + "[" + p_index + "].x)";
  441. }
  442. case ShaderLanguage::TYPE_VEC2: {
  443. return "(" + p_buffer + "[" + p_index + "].xy)";
  444. }
  445. case ShaderLanguage::TYPE_VEC3: {
  446. return "(" + p_buffer + "[" + p_index + "].xyz)";
  447. }
  448. case ShaderLanguage::TYPE_VEC4: {
  449. return "(" + p_buffer + "[" + p_index + "].xyzw)";
  450. }
  451. case ShaderLanguage::TYPE_MAT2: {
  452. return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)";
  453. }
  454. case ShaderLanguage::TYPE_MAT3: {
  455. return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)";
  456. }
  457. case ShaderLanguage::TYPE_MAT4: {
  458. 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)";
  459. }
  460. default: {
  461. ERR_FAIL_V("void");
  462. }
  463. }
  464. }
  465. 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) {
  466. String code;
  467. switch (p_node->type) {
  468. case SL::Node::TYPE_SHADER: {
  469. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  470. for (int i = 0; i < pnode->render_modes.size(); i++) {
  471. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  472. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
  473. used_rmode_defines.insert(pnode->render_modes[i]);
  474. }
  475. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  476. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  477. }
  478. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  479. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  480. *p.first = p.second;
  481. }
  482. }
  483. // structs
  484. for (int i = 0; i < pnode->vstructs.size(); i++) {
  485. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  486. String struct_code;
  487. struct_code += "struct ";
  488. struct_code += _mkid(pnode->vstructs[i].name);
  489. struct_code += " ";
  490. struct_code += "{\n";
  491. for (int j = 0; j < st->members.size(); j++) {
  492. SL::MemberNode *m = st->members[j];
  493. if (m->datatype == SL::TYPE_STRUCT) {
  494. struct_code += _mkid(m->struct_name);
  495. } else {
  496. struct_code += _prestr(m->precision);
  497. struct_code += _typestr(m->datatype);
  498. }
  499. struct_code += " ";
  500. struct_code += m->name;
  501. if (m->array_size > 0) {
  502. struct_code += "[";
  503. struct_code += itos(m->array_size);
  504. struct_code += "]";
  505. }
  506. struct_code += ";\n";
  507. }
  508. struct_code += "}";
  509. struct_code += ";\n";
  510. for (int j = 0; j < STAGE_MAX; j++) {
  511. r_gen_code.stage_globals[j] += struct_code;
  512. }
  513. }
  514. int max_texture_uniforms = 0;
  515. int max_uniforms = 0;
  516. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  517. if (SL::is_sampler_type(E.value.type)) {
  518. max_texture_uniforms++;
  519. } else {
  520. if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  521. continue; // Instances are indexed directly, don't need index uniforms.
  522. }
  523. max_uniforms++;
  524. }
  525. }
  526. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  527. Vector<int> uniform_sizes;
  528. Vector<int> uniform_alignments;
  529. Vector<StringName> uniform_defines;
  530. uniform_sizes.resize(max_uniforms);
  531. uniform_alignments.resize(max_uniforms);
  532. uniform_defines.resize(max_uniforms);
  533. bool uses_uniforms = false;
  534. Vector<StringName> uniform_names;
  535. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  536. uniform_names.push_back(E.key);
  537. }
  538. uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  539. for (int k = 0; k < uniform_names.size(); k++) {
  540. StringName uniform_name = uniform_names[k];
  541. const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name];
  542. String ucode;
  543. if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  544. //insert, but don't generate any code.
  545. p_actions.uniforms->insert(uniform_name, uniform);
  546. continue; // Instances are indexed directly, don't need index uniforms.
  547. }
  548. if (SL::is_sampler_type(uniform.type)) {
  549. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") uniform ";
  550. }
  551. bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  552. if (is_buffer_global) {
  553. //this is an integer to index the global table
  554. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  555. } else {
  556. ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type));
  557. ucode += _typestr(uniform.type);
  558. }
  559. ucode += " " + _mkid(uniform_name);
  560. if (uniform.array_size > 0) {
  561. ucode += "[";
  562. ucode += itos(uniform.array_size);
  563. ucode += "]";
  564. }
  565. ucode += ";\n";
  566. if (SL::is_sampler_type(uniform.type)) {
  567. for (int j = 0; j < STAGE_MAX; j++) {
  568. r_gen_code.stage_globals[j] += ucode;
  569. }
  570. GeneratedCode::Texture texture;
  571. texture.name = uniform_name;
  572. texture.hint = uniform.hint;
  573. texture.type = uniform.type;
  574. texture.filter = uniform.filter;
  575. texture.repeat = uniform.repeat;
  576. texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  577. texture.array_size = uniform.array_size;
  578. if (texture.global) {
  579. r_gen_code.uses_global_textures = true;
  580. }
  581. r_gen_code.texture_uniforms.write[uniform.texture_order] = texture;
  582. } else {
  583. if (!uses_uniforms) {
  584. uses_uniforms = true;
  585. }
  586. uniform_defines.write[uniform.order] = ucode;
  587. if (is_buffer_global) {
  588. //globals are indices into the global table
  589. uniform_sizes.write[uniform.order] = _get_datatype_size(ShaderLanguage::TYPE_UINT);
  590. uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  591. } else {
  592. if (uniform.array_size > 0) {
  593. int size = _get_datatype_size(uniform.type) * uniform.array_size;
  594. int m = (16 * uniform.array_size);
  595. if ((size % m) != 0) {
  596. size += m - (size % m);
  597. }
  598. uniform_sizes.write[uniform.order] = size;
  599. } else {
  600. uniform_sizes.write[uniform.order] = _get_datatype_size(uniform.type);
  601. }
  602. uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type);
  603. }
  604. }
  605. p_actions.uniforms->insert(uniform_name, uniform);
  606. }
  607. for (int i = 0; i < max_uniforms; i++) {
  608. r_gen_code.uniforms += uniform_defines[i];
  609. }
  610. #if 1
  611. // add up
  612. int offset = 0;
  613. for (int i = 0; i < uniform_sizes.size(); i++) {
  614. int align = offset % uniform_alignments[i];
  615. if (align != 0) {
  616. offset += uniform_alignments[i] - align;
  617. }
  618. r_gen_code.uniform_offsets.push_back(offset);
  619. offset += uniform_sizes[i];
  620. }
  621. r_gen_code.uniform_total_size = offset;
  622. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  623. r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
  624. }
  625. #else
  626. // add up
  627. for (int i = 0; i < uniform_sizes.size(); i++) {
  628. if (i > 0) {
  629. int align = uniform_sizes[i - 1] % uniform_alignments[i];
  630. if (align != 0) {
  631. uniform_sizes[i - 1] += uniform_alignments[i] - align;
  632. }
  633. uniform_sizes[i] = uniform_sizes[i] + uniform_sizes[i - 1];
  634. }
  635. }
  636. //offset
  637. r_gen_code.uniform_offsets.resize(uniform_sizes.size());
  638. for (int i = 0; i < uniform_sizes.size(); i++) {
  639. if (i > 0)
  640. r_gen_code.uniform_offsets[i] = uniform_sizes[i - 1];
  641. else
  642. r_gen_code.uniform_offsets[i] = 0;
  643. }
  644. /*
  645. for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) {
  646. if (SL::is_sampler_type(E->get().type)) {
  647. continue;
  648. }
  649. }
  650. */
  651. if (uniform_sizes.size()) {
  652. r_gen_code.uniform_total_size = uniform_sizes[uniform_sizes.size() - 1];
  653. } else {
  654. r_gen_code.uniform_total_size = 0;
  655. }
  656. #endif
  657. uint32_t index = p_default_actions.base_varying_index;
  658. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  659. Vector<StringName> varying_names;
  660. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  661. varying_names.push_back(E.key);
  662. }
  663. varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  664. for (int k = 0; k < varying_names.size(); k++) {
  665. StringName varying_name = varying_names[k];
  666. const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name];
  667. if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  668. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying));
  669. fragment_varyings.insert(varying_name);
  670. continue;
  671. }
  672. String vcode;
  673. String interp_mode = _interpstr(varying.interpolation);
  674. vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type));
  675. vcode += _typestr(varying.type);
  676. vcode += " " + _mkid(varying_name);
  677. if (varying.array_size > 0) {
  678. vcode += "[";
  679. vcode += itos(varying.array_size);
  680. vcode += "]";
  681. }
  682. vcode += ";\n";
  683. r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  684. r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") " + interp_mode + "in " + vcode;
  685. index++;
  686. }
  687. if (var_frag_to_light.size() > 0) {
  688. String gcode = "\n\nstruct {\n";
  689. for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) {
  690. gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first);
  691. if (E.second.array_size > 0) {
  692. gcode += "[";
  693. gcode += itos(E.second.array_size);
  694. gcode += "]";
  695. }
  696. gcode += ";\n";
  697. }
  698. gcode += "} frag_to_light;\n";
  699. r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode;
  700. }
  701. for (int i = 0; i < pnode->vconstants.size(); i++) {
  702. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  703. String gcode;
  704. gcode += _constr(true);
  705. gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
  706. if (cnode.type == SL::TYPE_STRUCT) {
  707. gcode += _mkid(cnode.type_str);
  708. } else {
  709. gcode += _typestr(cnode.type);
  710. }
  711. gcode += " " + _mkid(String(cnode.name));
  712. if (cnode.array_size > 0) {
  713. gcode += "[";
  714. gcode += itos(cnode.array_size);
  715. gcode += "]";
  716. }
  717. gcode += "=";
  718. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  719. gcode += ";\n";
  720. for (int j = 0; j < STAGE_MAX; j++) {
  721. r_gen_code.stage_globals[j] += gcode;
  722. }
  723. }
  724. Map<StringName, String> function_code;
  725. //code for functions
  726. for (int i = 0; i < pnode->functions.size(); i++) {
  727. SL::FunctionNode *fnode = pnode->functions[i].function;
  728. function = fnode;
  729. current_func_name = fnode->name;
  730. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  731. function = nullptr;
  732. }
  733. //place functions in actual code
  734. Set<StringName> added_funcs_per_stage[STAGE_MAX];
  735. for (int i = 0; i < pnode->functions.size(); i++) {
  736. SL::FunctionNode *fnode = pnode->functions[i].function;
  737. function = fnode;
  738. current_func_name = fnode->name;
  739. if (p_actions.entry_point_stages.has(fnode->name)) {
  740. Stage stage = p_actions.entry_point_stages[fnode->name];
  741. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]);
  742. r_gen_code.code[fnode->name] = function_code[fnode->name];
  743. }
  744. function = nullptr;
  745. }
  746. //code+=dump_node_code(pnode->body,p_level);
  747. } break;
  748. case SL::Node::TYPE_STRUCT: {
  749. } break;
  750. case SL::Node::TYPE_FUNCTION: {
  751. } break;
  752. case SL::Node::TYPE_BLOCK: {
  753. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  754. //variables
  755. if (!bnode->single_statement) {
  756. code += _mktab(p_level - 1) + "{\n";
  757. }
  758. for (int i = 0; i < bnode->statements.size(); i++) {
  759. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  760. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  761. code += scode; //use directly
  762. } else {
  763. code += _mktab(p_level) + scode + ";\n";
  764. }
  765. }
  766. if (!bnode->single_statement) {
  767. code += _mktab(p_level - 1) + "}\n";
  768. }
  769. } break;
  770. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  771. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  772. String declaration;
  773. declaration += _constr(vdnode->is_const);
  774. if (vdnode->datatype == SL::TYPE_STRUCT) {
  775. declaration += _mkid(vdnode->struct_name);
  776. } else {
  777. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  778. }
  779. for (int i = 0; i < vdnode->declarations.size(); i++) {
  780. if (i > 0) {
  781. declaration += ",";
  782. } else {
  783. declaration += " ";
  784. }
  785. declaration += _mkid(vdnode->declarations[i].name);
  786. if (vdnode->declarations[i].initializer) {
  787. declaration += "=";
  788. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  789. }
  790. }
  791. code += declaration;
  792. } break;
  793. case SL::Node::TYPE_VARIABLE: {
  794. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  795. bool use_fragment_varying = false;
  796. if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  797. if (p_assigning) {
  798. if (shader->varyings.has(vnode->name)) {
  799. use_fragment_varying = true;
  800. }
  801. } else {
  802. if (fragment_varyings.has(vnode->name)) {
  803. use_fragment_varying = true;
  804. }
  805. }
  806. }
  807. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  808. *p_actions.write_flag_pointers[vnode->name] = true;
  809. }
  810. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  811. String define = p_default_actions.usage_defines[vnode->name];
  812. if (define.begins_with("@")) {
  813. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  814. }
  815. r_gen_code.defines.push_back(define);
  816. used_name_defines.insert(vnode->name);
  817. }
  818. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  819. *p_actions.usage_flag_pointers[vnode->name] = true;
  820. used_flag_pointers.insert(vnode->name);
  821. }
  822. if (p_default_actions.renames.has(vnode->name)) {
  823. code = p_default_actions.renames[vnode->name];
  824. } else {
  825. if (shader->uniforms.has(vnode->name)) {
  826. //its a uniform!
  827. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  828. if (u.texture_order >= 0) {
  829. code = _mkid(vnode->name); //texture, use as is
  830. } else {
  831. //a scalar or vector
  832. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  833. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  834. //global variable, this means the code points to an index to the global table
  835. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  836. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  837. //instance variable, index it as such
  838. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  839. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  840. } else {
  841. //regular uniform, index from UBO
  842. code = actions.base_uniform_string + _mkid(vnode->name);
  843. }
  844. }
  845. } else {
  846. if (use_fragment_varying) {
  847. code = "frag_to_light.";
  848. }
  849. code += _mkid(vnode->name); //its something else (local var most likely) use as is
  850. }
  851. }
  852. if (vnode->name == time_name) {
  853. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  854. r_gen_code.uses_vertex_time = true;
  855. }
  856. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  857. r_gen_code.uses_fragment_time = true;
  858. }
  859. }
  860. } break;
  861. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  862. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  863. int sz = acnode->initializer.size();
  864. if (acnode->datatype == SL::TYPE_STRUCT) {
  865. code += _mkid(acnode->struct_name);
  866. } else {
  867. code += _typestr(acnode->datatype);
  868. }
  869. code += "[";
  870. code += itos(acnode->initializer.size());
  871. code += "]";
  872. code += "(";
  873. for (int i = 0; i < sz; i++) {
  874. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  875. if (i != sz - 1) {
  876. code += ", ";
  877. }
  878. }
  879. code += ")";
  880. } break;
  881. case SL::Node::TYPE_ARRAY_DECLARATION: {
  882. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  883. String declaration;
  884. declaration += _constr(adnode->is_const);
  885. if (adnode->datatype == SL::TYPE_STRUCT) {
  886. declaration += _mkid(adnode->struct_name);
  887. } else {
  888. declaration += _prestr(adnode->precision) + _typestr(adnode->datatype);
  889. }
  890. for (int i = 0; i < adnode->declarations.size(); i++) {
  891. if (i > 0) {
  892. declaration += ",";
  893. } else {
  894. declaration += " ";
  895. }
  896. declaration += _mkid(adnode->declarations[i].name);
  897. declaration += "[";
  898. if (adnode->size_expression != nullptr) {
  899. declaration += _dump_node_code(adnode->size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  900. } else {
  901. declaration += itos(adnode->declarations[i].size);
  902. }
  903. declaration += "]";
  904. if (adnode->declarations[i].single_expression) {
  905. declaration += "=";
  906. declaration += _dump_node_code(adnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  907. } else {
  908. int sz = adnode->declarations[i].initializer.size();
  909. if (sz > 0) {
  910. declaration += "=";
  911. if (adnode->datatype == SL::TYPE_STRUCT) {
  912. declaration += _mkid(adnode->struct_name);
  913. } else {
  914. declaration += _typestr(adnode->datatype);
  915. }
  916. declaration += "[";
  917. declaration += itos(sz);
  918. declaration += "]";
  919. declaration += "(";
  920. for (int j = 0; j < sz; j++) {
  921. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  922. if (j != sz - 1) {
  923. declaration += ", ";
  924. }
  925. }
  926. declaration += ")";
  927. }
  928. }
  929. }
  930. code += declaration;
  931. } break;
  932. case SL::Node::TYPE_ARRAY: {
  933. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  934. bool use_fragment_varying = false;
  935. if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  936. if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) {
  937. use_fragment_varying = true;
  938. } else {
  939. if (p_assigning) {
  940. if (shader->varyings.has(anode->name)) {
  941. use_fragment_varying = true;
  942. }
  943. } else {
  944. if (fragment_varyings.has(anode->name)) {
  945. use_fragment_varying = true;
  946. }
  947. }
  948. }
  949. }
  950. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  951. *p_actions.write_flag_pointers[anode->name] = true;
  952. }
  953. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  954. String define = p_default_actions.usage_defines[anode->name];
  955. if (define.begins_with("@")) {
  956. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  957. }
  958. r_gen_code.defines.push_back(define);
  959. used_name_defines.insert(anode->name);
  960. }
  961. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  962. *p_actions.usage_flag_pointers[anode->name] = true;
  963. used_flag_pointers.insert(anode->name);
  964. }
  965. if (p_default_actions.renames.has(anode->name)) {
  966. code = p_default_actions.renames[anode->name];
  967. } else {
  968. if (shader->uniforms.has(anode->name)) {
  969. //its a uniform!
  970. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name];
  971. if (u.texture_order >= 0) {
  972. code = _mkid(anode->name); //texture, use as is
  973. } else {
  974. //a scalar or vector
  975. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  976. code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is
  977. //global variable, this means the code points to an index to the global table
  978. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  979. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  980. //instance variable, index it as such
  981. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  982. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  983. } else {
  984. //regular uniform, index from UBO
  985. code = actions.base_uniform_string + _mkid(anode->name);
  986. }
  987. }
  988. } else {
  989. if (use_fragment_varying) {
  990. code = "frag_to_light.";
  991. }
  992. code += _mkid(anode->name);
  993. }
  994. }
  995. if (anode->call_expression != nullptr) {
  996. code += ".";
  997. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  998. } else if (anode->index_expression != nullptr) {
  999. code += "[";
  1000. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1001. code += "]";
  1002. } else if (anode->assign_expression != nullptr) {
  1003. code += "=";
  1004. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1005. }
  1006. if (anode->name == time_name) {
  1007. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  1008. r_gen_code.uses_vertex_time = true;
  1009. }
  1010. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  1011. r_gen_code.uses_fragment_time = true;
  1012. }
  1013. }
  1014. } break;
  1015. case SL::Node::TYPE_CONSTANT: {
  1016. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  1017. if (cnode->array_size == 0) {
  1018. return get_constant_text(cnode->datatype, cnode->values);
  1019. } else {
  1020. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  1021. code += _mkid(cnode->struct_name);
  1022. } else {
  1023. code += _typestr(cnode->datatype);
  1024. }
  1025. code += "[";
  1026. code += itos(cnode->array_size);
  1027. code += "]";
  1028. code += "(";
  1029. for (int i = 0; i < cnode->array_size; i++) {
  1030. if (i > 0) {
  1031. code += ",";
  1032. } else {
  1033. code += "";
  1034. }
  1035. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1036. }
  1037. code += ")";
  1038. }
  1039. } break;
  1040. case SL::Node::TYPE_OPERATOR: {
  1041. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  1042. switch (onode->op) {
  1043. case SL::OP_ASSIGN:
  1044. case SL::OP_ASSIGN_ADD:
  1045. case SL::OP_ASSIGN_SUB:
  1046. case SL::OP_ASSIGN_MUL:
  1047. case SL::OP_ASSIGN_DIV:
  1048. case SL::OP_ASSIGN_SHIFT_LEFT:
  1049. case SL::OP_ASSIGN_SHIFT_RIGHT:
  1050. case SL::OP_ASSIGN_MOD:
  1051. case SL::OP_ASSIGN_BIT_AND:
  1052. case SL::OP_ASSIGN_BIT_OR:
  1053. case SL::OP_ASSIGN_BIT_XOR:
  1054. 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);
  1055. break;
  1056. case SL::OP_BIT_INVERT:
  1057. case SL::OP_NEGATE:
  1058. case SL::OP_NOT:
  1059. case SL::OP_DECREMENT:
  1060. case SL::OP_INCREMENT:
  1061. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1062. break;
  1063. case SL::OP_POST_DECREMENT:
  1064. case SL::OP_POST_INCREMENT:
  1065. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  1066. break;
  1067. case SL::OP_CALL:
  1068. case SL::OP_STRUCT:
  1069. case SL::OP_CONSTRUCT: {
  1070. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  1071. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  1072. bool is_texture_func = false;
  1073. bool is_screen_texture = false;
  1074. if (onode->op == SL::OP_STRUCT) {
  1075. code += _mkid(vnode->name);
  1076. } else if (onode->op == SL::OP_CONSTRUCT) {
  1077. code += String(vnode->name);
  1078. } else {
  1079. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  1080. *p_actions.usage_flag_pointers[vnode->name] = true;
  1081. used_flag_pointers.insert(vnode->name);
  1082. }
  1083. if (internal_functions.has(vnode->name)) {
  1084. code += vnode->name;
  1085. is_texture_func = texture_functions.has(vnode->name);
  1086. } else if (p_default_actions.renames.has(vnode->name)) {
  1087. code += p_default_actions.renames[vnode->name];
  1088. } else {
  1089. code += _mkid(vnode->name);
  1090. }
  1091. }
  1092. code += "(";
  1093. for (int i = 1; i < onode->arguments.size(); i++) {
  1094. if (i > 1) {
  1095. code += ", ";
  1096. }
  1097. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1098. if (is_texture_func && i == 1) {
  1099. //need to map from texture to sampler in order to sample
  1100. StringName texture_uniform;
  1101. bool correct_texture_uniform = false;
  1102. switch (onode->arguments[i]->type) {
  1103. case SL::Node::TYPE_VARIABLE: {
  1104. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  1105. texture_uniform = varnode->name;
  1106. correct_texture_uniform = true;
  1107. } break;
  1108. case SL::Node::TYPE_ARRAY: {
  1109. const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]);
  1110. texture_uniform = anode->name;
  1111. correct_texture_uniform = true;
  1112. } break;
  1113. default:
  1114. break;
  1115. }
  1116. if (correct_texture_uniform) {
  1117. is_screen_texture = (texture_uniform == "SCREEN_TEXTURE");
  1118. String sampler_name;
  1119. if (actions.custom_samplers.has(texture_uniform)) {
  1120. sampler_name = actions.custom_samplers[texture_uniform];
  1121. } else {
  1122. if (shader->uniforms.has(texture_uniform)) {
  1123. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  1124. } else {
  1125. bool found = false;
  1126. for (int j = 0; j < function->arguments.size(); j++) {
  1127. if (function->arguments[j].name == texture_uniform) {
  1128. if (function->arguments[j].tex_builtin_check) {
  1129. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  1130. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  1131. found = true;
  1132. break;
  1133. }
  1134. if (function->arguments[j].tex_argument_check) {
  1135. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1136. found = true;
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. if (!found) {
  1142. //function was most likely unused, so use anything (compiler will remove it anyway)
  1143. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1144. }
  1145. }
  1146. }
  1147. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  1148. } else {
  1149. code += node_code;
  1150. }
  1151. } else {
  1152. code += node_code;
  1153. }
  1154. }
  1155. code += ")";
  1156. if (is_screen_texture && actions.apply_luminance_multiplier) {
  1157. code = "(" + code + " * vec4(vec3(sc_luminance_multiplier), 1.0))";
  1158. }
  1159. } break;
  1160. case SL::OP_INDEX: {
  1161. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1162. code += "[";
  1163. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1164. code += "]";
  1165. } break;
  1166. case SL::OP_SELECT_IF: {
  1167. code += "(";
  1168. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1169. code += "?";
  1170. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1171. code += ":";
  1172. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1173. code += ")";
  1174. } break;
  1175. default: {
  1176. if (p_use_scope) {
  1177. code += "(";
  1178. }
  1179. 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);
  1180. if (p_use_scope) {
  1181. code += ")";
  1182. }
  1183. break;
  1184. }
  1185. }
  1186. } break;
  1187. case SL::Node::TYPE_CONTROL_FLOW: {
  1188. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1189. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1190. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1191. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1192. if (cfnode->blocks.size() == 2) {
  1193. code += _mktab(p_level) + "else\n";
  1194. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1195. }
  1196. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1197. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1198. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1199. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1200. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1201. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1202. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1203. code += _mktab(p_level) + "default:\n";
  1204. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1205. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1206. code += _mktab(p_level) + "do";
  1207. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1208. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1209. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1210. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1211. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1212. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1213. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1214. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1215. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1216. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1217. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1218. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1219. if (cfnode->expressions.size()) {
  1220. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1221. } else {
  1222. code = "return;";
  1223. }
  1224. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1225. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1226. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1227. used_flag_pointers.insert("DISCARD");
  1228. }
  1229. code = "discard;";
  1230. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1231. code = "continue;";
  1232. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1233. code = "break;";
  1234. }
  1235. } break;
  1236. case SL::Node::TYPE_MEMBER: {
  1237. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1238. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1239. if (mnode->index_expression != nullptr) {
  1240. code += "[";
  1241. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1242. code += "]";
  1243. } else if (mnode->assign_expression != nullptr) {
  1244. code += "=";
  1245. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1246. } else if (mnode->call_expression != nullptr) {
  1247. code += ".";
  1248. code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  1249. }
  1250. } break;
  1251. }
  1252. return code;
  1253. }
  1254. ShaderLanguage::DataType ShaderCompilerRD::_get_variable_type(const StringName &p_type) {
  1255. RS::GlobalVariableType gvt = ((RendererStorageRD *)(RendererStorage::base_singleton))->global_variable_get_type_internal(p_type);
  1256. return RS::global_variable_type_get_shader_datatype(gvt);
  1257. }
  1258. Error ShaderCompilerRD::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1259. Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_variable_type);
  1260. if (err != OK) {
  1261. Vector<String> shader = p_code.split("\n");
  1262. for (int i = 0; i < shader.size(); i++) {
  1263. if (i + 1 == parser.get_error_line()) {
  1264. // Mark the error line to be visible without having to look at
  1265. // the trace at the end.
  1266. print_line(vformat("E%4d-> %s", i + 1, shader[i]));
  1267. } else {
  1268. print_line(vformat("%5d | %s", i + 1, shader[i]));
  1269. }
  1270. }
  1271. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER);
  1272. return err;
  1273. }
  1274. r_gen_code.defines.clear();
  1275. r_gen_code.code.clear();
  1276. for (int i = 0; i < STAGE_MAX; i++) {
  1277. r_gen_code.stage_globals[i] = String();
  1278. }
  1279. r_gen_code.uses_fragment_time = false;
  1280. r_gen_code.uses_vertex_time = false;
  1281. r_gen_code.uses_global_textures = false;
  1282. used_name_defines.clear();
  1283. used_rmode_defines.clear();
  1284. used_flag_pointers.clear();
  1285. fragment_varyings.clear();
  1286. shader = parser.get_shader();
  1287. function = nullptr;
  1288. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1289. return OK;
  1290. }
  1291. void ShaderCompilerRD::initialize(DefaultIdentifierActions p_actions) {
  1292. actions = p_actions;
  1293. time_name = "TIME";
  1294. List<String> func_list;
  1295. ShaderLanguage::get_builtin_funcs(&func_list);
  1296. for (const String &E : func_list) {
  1297. internal_functions.insert(E);
  1298. }
  1299. texture_functions.insert("texture");
  1300. texture_functions.insert("textureProj");
  1301. texture_functions.insert("textureLod");
  1302. texture_functions.insert("textureProjLod");
  1303. texture_functions.insert("textureGrad");
  1304. texture_functions.insert("textureGather");
  1305. texture_functions.insert("textureSize");
  1306. texture_functions.insert("texelFetch");
  1307. }
  1308. ShaderCompilerRD::ShaderCompilerRD() {
  1309. #if 0
  1310. /** SPATIAL SHADER **/
  1311. actions[RS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  1312. actions[RS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  1313. actions[RS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  1314. actions[RS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  1315. actions[RS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1316. actions[RS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  1317. actions[RS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  1318. actions[RS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  1319. actions[RS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  1320. actions[RS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  1321. actions[RS::SHADER_SPATIAL].renames["POSITION"] = "position";
  1322. actions[RS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  1323. actions[RS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  1324. actions[RS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  1325. actions[RS::SHADER_SPATIAL].renames["POINT_SIZE"] = "gl_PointSize";
  1326. actions[RS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  1327. //builtins
  1328. actions[RS::SHADER_SPATIAL].renames["TIME"] = "time";
  1329. actions[RS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  1330. actions[RS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  1331. actions[RS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  1332. actions[RS::SHADER_SPATIAL].renames["NORMAL_MAP"] = "normal_map";
  1333. actions[RS::SHADER_SPATIAL].renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
  1334. actions[RS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  1335. actions[RS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  1336. actions[RS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  1337. actions[RS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  1338. actions[RS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  1339. actions[RS::SHADER_SPATIAL].renames["RIM"] = "rim";
  1340. actions[RS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  1341. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  1342. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  1343. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  1344. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1345. actions[RS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  1346. actions[RS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  1347. actions[RS::SHADER_SPATIAL].renames["AO"] = "ao";
  1348. actions[RS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1349. actions[RS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  1350. actions[RS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  1351. actions[RS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1352. actions[RS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  1353. actions[RS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  1354. actions[RS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  1355. actions[RS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  1356. actions[RS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  1357. actions[RS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1358. //for light
  1359. actions[RS::SHADER_SPATIAL].renames["VIEW"] = "view";
  1360. actions[RS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  1361. actions[RS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  1362. actions[RS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  1363. actions[RS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1364. actions[RS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  1365. actions[RS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  1366. actions[RS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  1367. actions[RS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  1368. actions[RS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  1369. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  1370. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  1371. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  1372. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1373. actions[RS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  1374. actions[RS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  1375. actions[RS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  1376. actions[RS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  1377. actions[RS::SHADER_SPATIAL].usage_defines["NORMAL_MAP"] = "#define ENABLE_NORMAL_MAP\n";
  1378. actions[RS::SHADER_SPATIAL].usage_defines["NORMAL_MAP_DEPTH"] = "@NORMAL_MAP";
  1379. actions[RS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  1380. actions[RS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1381. actions[RS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  1382. actions[RS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1383. actions[RS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1384. actions[RS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  1385. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1386. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1387. actions[RS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1388. actions[RS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1389. actions[RS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1390. actions[RS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1391. actions[RS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1392. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1393. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1394. bool force_lambert = GLOBAL_GET("rendering/shading/overrides/force_lambert_over_burley");
  1395. if (!force_lambert) {
  1396. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1397. }
  1398. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1399. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1400. bool force_blinn = GLOBAL_GET("rendering/shading/overrides/force_blinn_over_ggx");
  1401. if (!force_blinn) {
  1402. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1403. } else {
  1404. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  1405. }
  1406. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  1407. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  1408. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1409. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1410. actions[RS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1411. actions[RS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1412. actions[RS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1413. /* PARTICLES SHADER */
  1414. actions[RS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  1415. actions[RS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  1416. actions[RS::SHADER_PARTICLES].renames["MASS"] = "mass";
  1417. actions[RS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  1418. actions[RS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  1419. actions[RS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  1420. actions[RS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  1421. actions[RS::SHADER_PARTICLES].renames["TIME"] = "time";
  1422. actions[RS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  1423. actions[RS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  1424. actions[RS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  1425. actions[RS::SHADER_PARTICLES].renames["INDEX"] = "index";
  1426. actions[RS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  1427. actions[RS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  1428. actions[RS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  1429. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1430. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1431. actions[RS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1432. #endif
  1433. }