shader_compiler_rd.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. /*************************************************************************/
  2. /* shader_compiler_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_compiler_rd.h"
  31. #include "core/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. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  616. String vcode;
  617. String interp_mode = _interpstr(E->get().interpolation);
  618. vcode += _prestr(E->get().precision);
  619. vcode += _typestr(E->get().type);
  620. vcode += " " + _mkid(E->key());
  621. if (E->get().array_size > 0) {
  622. vcode += "[";
  623. vcode += itos(E->get().array_size);
  624. vcode += "]";
  625. }
  626. vcode += ";\n";
  627. r_gen_code.vertex_global += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  628. r_gen_code.fragment_global += "layout(location=" + itos(index) + ") " + interp_mode + "in " + vcode;
  629. r_gen_code.compute_global += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  630. index++;
  631. }
  632. for (int i = 0; i < pnode->vconstants.size(); i++) {
  633. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  634. String gcode;
  635. gcode += "const ";
  636. gcode += _prestr(cnode.precision);
  637. if (cnode.type == SL::TYPE_STRUCT) {
  638. gcode += _mkid(cnode.type_str);
  639. } else {
  640. gcode += _typestr(cnode.type);
  641. }
  642. gcode += " " + _mkid(String(cnode.name));
  643. if (cnode.array_size > 0) {
  644. gcode += "[";
  645. gcode += itos(cnode.array_size);
  646. gcode += "]";
  647. }
  648. gcode += "=";
  649. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  650. gcode += ";\n";
  651. r_gen_code.vertex_global += gcode;
  652. r_gen_code.fragment_global += gcode;
  653. r_gen_code.compute_global += gcode;
  654. }
  655. Map<StringName, String> function_code;
  656. //code for functions
  657. for (int i = 0; i < pnode->functions.size(); i++) {
  658. SL::FunctionNode *fnode = pnode->functions[i].function;
  659. function = fnode;
  660. current_func_name = fnode->name;
  661. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  662. function = nullptr;
  663. }
  664. //place functions in actual code
  665. Set<StringName> added_vtx;
  666. Set<StringName> added_fragment; //share for light
  667. Set<StringName> added_compute; //share for light
  668. for (int i = 0; i < pnode->functions.size(); i++) {
  669. SL::FunctionNode *fnode = pnode->functions[i].function;
  670. function = fnode;
  671. current_func_name = fnode->name;
  672. if (fnode->name == vertex_name) {
  673. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  674. r_gen_code.vertex = function_code[vertex_name];
  675. }
  676. if (fnode->name == fragment_name) {
  677. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  678. r_gen_code.fragment = function_code[fragment_name];
  679. }
  680. if (fnode->name == light_name) {
  681. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  682. r_gen_code.light = function_code[light_name];
  683. }
  684. if (fnode->name == compute_name) {
  685. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.compute_global, added_compute);
  686. r_gen_code.compute = function_code[compute_name];
  687. }
  688. function = nullptr;
  689. }
  690. //code+=dump_node_code(pnode->body,p_level);
  691. } break;
  692. case SL::Node::TYPE_STRUCT: {
  693. } break;
  694. case SL::Node::TYPE_FUNCTION: {
  695. } break;
  696. case SL::Node::TYPE_BLOCK: {
  697. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  698. //variables
  699. if (!bnode->single_statement) {
  700. code += _mktab(p_level - 1) + "{\n";
  701. }
  702. for (int i = 0; i < bnode->statements.size(); i++) {
  703. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  704. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  705. code += scode; //use directly
  706. } else {
  707. code += _mktab(p_level) + scode + ";\n";
  708. }
  709. }
  710. if (!bnode->single_statement) {
  711. code += _mktab(p_level - 1) + "}\n";
  712. }
  713. } break;
  714. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  715. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  716. String declaration;
  717. if (vdnode->is_const) {
  718. declaration += "const ";
  719. }
  720. if (vdnode->datatype == SL::TYPE_STRUCT) {
  721. declaration += _mkid(vdnode->struct_name);
  722. } else {
  723. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  724. }
  725. for (int i = 0; i < vdnode->declarations.size(); i++) {
  726. if (i > 0) {
  727. declaration += ",";
  728. } else {
  729. declaration += " ";
  730. }
  731. declaration += _mkid(vdnode->declarations[i].name);
  732. if (vdnode->declarations[i].initializer) {
  733. declaration += "=";
  734. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  735. }
  736. }
  737. code += declaration;
  738. } break;
  739. case SL::Node::TYPE_VARIABLE: {
  740. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  741. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  742. *p_actions.write_flag_pointers[vnode->name] = true;
  743. }
  744. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  745. String define = p_default_actions.usage_defines[vnode->name];
  746. if (define.begins_with("@")) {
  747. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  748. }
  749. r_gen_code.defines.push_back(define);
  750. used_name_defines.insert(vnode->name);
  751. }
  752. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  753. *p_actions.usage_flag_pointers[vnode->name] = true;
  754. used_flag_pointers.insert(vnode->name);
  755. }
  756. if (p_default_actions.renames.has(vnode->name)) {
  757. code = p_default_actions.renames[vnode->name];
  758. } else {
  759. if (shader->uniforms.has(vnode->name)) {
  760. //its a uniform!
  761. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  762. if (u.texture_order >= 0) {
  763. code = _mkid(vnode->name); //texture, use as is
  764. } else {
  765. //a scalar or vector
  766. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  767. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  768. //global variable, this means the code points to an index to the global table
  769. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  770. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  771. //instance variable, index it as such
  772. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  773. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  774. } else {
  775. //regular uniform, index from UBO
  776. code = actions.base_uniform_string + _mkid(vnode->name);
  777. }
  778. }
  779. } else {
  780. code = _mkid(vnode->name); //its something else (local var most likely) use as is
  781. }
  782. }
  783. if (vnode->name == time_name) {
  784. if (current_func_name == vertex_name) {
  785. r_gen_code.uses_vertex_time = true;
  786. }
  787. if (current_func_name == fragment_name || current_func_name == light_name) {
  788. r_gen_code.uses_fragment_time = true;
  789. }
  790. }
  791. } break;
  792. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  793. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  794. int sz = acnode->initializer.size();
  795. if (acnode->datatype == SL::TYPE_STRUCT) {
  796. code += _mkid(acnode->struct_name);
  797. } else {
  798. code += _typestr(acnode->datatype);
  799. }
  800. code += "[";
  801. code += itos(acnode->initializer.size());
  802. code += "]";
  803. code += "(";
  804. for (int i = 0; i < sz; i++) {
  805. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  806. if (i != sz - 1) {
  807. code += ", ";
  808. }
  809. }
  810. code += ")";
  811. } break;
  812. case SL::Node::TYPE_ARRAY_DECLARATION: {
  813. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  814. String declaration;
  815. if (adnode->is_const) {
  816. declaration += "const ";
  817. }
  818. if (adnode->datatype == SL::TYPE_STRUCT) {
  819. declaration += _mkid(adnode->struct_name);
  820. } else {
  821. declaration = _prestr(adnode->precision) + _typestr(adnode->datatype);
  822. }
  823. for (int i = 0; i < adnode->declarations.size(); i++) {
  824. if (i > 0) {
  825. declaration += ",";
  826. } else {
  827. declaration += " ";
  828. }
  829. declaration += _mkid(adnode->declarations[i].name);
  830. declaration += "[";
  831. declaration += itos(adnode->declarations[i].size);
  832. declaration += "]";
  833. int sz = adnode->declarations[i].initializer.size();
  834. if (sz > 0) {
  835. declaration += "=";
  836. if (adnode->datatype == SL::TYPE_STRUCT) {
  837. declaration += _mkid(adnode->struct_name);
  838. } else {
  839. declaration += _typestr(adnode->datatype);
  840. }
  841. declaration += "[";
  842. declaration += itos(sz);
  843. declaration += "]";
  844. declaration += "(";
  845. for (int j = 0; j < sz; j++) {
  846. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  847. if (j != sz - 1) {
  848. declaration += ", ";
  849. }
  850. }
  851. declaration += ")";
  852. }
  853. }
  854. code += declaration;
  855. } break;
  856. case SL::Node::TYPE_ARRAY: {
  857. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  858. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  859. *p_actions.write_flag_pointers[anode->name] = true;
  860. }
  861. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  862. String define = p_default_actions.usage_defines[anode->name];
  863. if (define.begins_with("@")) {
  864. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  865. }
  866. r_gen_code.defines.push_back(define);
  867. used_name_defines.insert(anode->name);
  868. }
  869. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  870. *p_actions.usage_flag_pointers[anode->name] = true;
  871. used_flag_pointers.insert(anode->name);
  872. }
  873. if (p_default_actions.renames.has(anode->name)) {
  874. code = p_default_actions.renames[anode->name];
  875. } else {
  876. code = _mkid(anode->name);
  877. }
  878. if (anode->call_expression != nullptr) {
  879. code += ".";
  880. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  881. }
  882. if (anode->index_expression != nullptr) {
  883. code += "[";
  884. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  885. code += "]";
  886. }
  887. if (anode->name == time_name) {
  888. if (current_func_name == vertex_name) {
  889. r_gen_code.uses_vertex_time = true;
  890. }
  891. if (current_func_name == fragment_name || current_func_name == light_name) {
  892. r_gen_code.uses_fragment_time = true;
  893. }
  894. }
  895. } break;
  896. case SL::Node::TYPE_CONSTANT: {
  897. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  898. if (cnode->array_size == 0) {
  899. return get_constant_text(cnode->datatype, cnode->values);
  900. } else {
  901. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  902. code += _mkid(cnode->struct_name);
  903. } else {
  904. code += _typestr(cnode->datatype);
  905. }
  906. code += "[";
  907. code += itos(cnode->array_size);
  908. code += "]";
  909. code += "(";
  910. for (int i = 0; i < cnode->array_size; i++) {
  911. if (i > 0) {
  912. code += ",";
  913. } else {
  914. code += "";
  915. }
  916. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  917. }
  918. code += ")";
  919. }
  920. } break;
  921. case SL::Node::TYPE_OPERATOR: {
  922. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  923. switch (onode->op) {
  924. case SL::OP_ASSIGN:
  925. case SL::OP_ASSIGN_ADD:
  926. case SL::OP_ASSIGN_SUB:
  927. case SL::OP_ASSIGN_MUL:
  928. case SL::OP_ASSIGN_DIV:
  929. case SL::OP_ASSIGN_SHIFT_LEFT:
  930. case SL::OP_ASSIGN_SHIFT_RIGHT:
  931. case SL::OP_ASSIGN_MOD:
  932. case SL::OP_ASSIGN_BIT_AND:
  933. case SL::OP_ASSIGN_BIT_OR:
  934. case SL::OP_ASSIGN_BIT_XOR:
  935. 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);
  936. break;
  937. case SL::OP_BIT_INVERT:
  938. case SL::OP_NEGATE:
  939. case SL::OP_NOT:
  940. case SL::OP_DECREMENT:
  941. case SL::OP_INCREMENT:
  942. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  943. break;
  944. case SL::OP_POST_DECREMENT:
  945. case SL::OP_POST_INCREMENT:
  946. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  947. break;
  948. case SL::OP_CALL:
  949. case SL::OP_STRUCT:
  950. case SL::OP_CONSTRUCT: {
  951. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  952. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  953. bool is_texture_func = false;
  954. if (onode->op == SL::OP_STRUCT) {
  955. code += _mkid(vnode->name);
  956. } else if (onode->op == SL::OP_CONSTRUCT) {
  957. code += String(vnode->name);
  958. } else {
  959. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  960. *p_actions.usage_flag_pointers[vnode->name] = true;
  961. used_flag_pointers.insert(vnode->name);
  962. }
  963. if (internal_functions.has(vnode->name)) {
  964. code += vnode->name;
  965. is_texture_func = texture_functions.has(vnode->name);
  966. } else if (p_default_actions.renames.has(vnode->name)) {
  967. code += p_default_actions.renames[vnode->name];
  968. } else {
  969. code += _mkid(vnode->name);
  970. }
  971. }
  972. code += "(";
  973. for (int i = 1; i < onode->arguments.size(); i++) {
  974. if (i > 1) {
  975. code += ", ";
  976. }
  977. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  978. if (is_texture_func && i == 1 && onode->arguments[i]->type == SL::Node::TYPE_VARIABLE) {
  979. //need to map from texture to sampler in order to sample
  980. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  981. StringName texture_uniform = varnode->name;
  982. String sampler_name;
  983. if (actions.custom_samplers.has(texture_uniform)) {
  984. sampler_name = actions.custom_samplers[texture_uniform];
  985. } else {
  986. if (shader->uniforms.has(texture_uniform)) {
  987. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  988. } else {
  989. bool found = false;
  990. for (int j = 0; j < function->arguments.size(); j++) {
  991. if (function->arguments[j].name == texture_uniform) {
  992. if (function->arguments[j].tex_builtin_check) {
  993. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  994. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  995. found = true;
  996. break;
  997. }
  998. if (function->arguments[j].tex_argument_check) {
  999. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1000. found = true;
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. if (!found) {
  1006. //function was most likely unused, so use anything (compiler will remove it anyway)
  1007. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1008. }
  1009. }
  1010. }
  1011. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  1012. } else {
  1013. code += node_code;
  1014. }
  1015. }
  1016. code += ")";
  1017. } break;
  1018. case SL::OP_INDEX: {
  1019. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1020. code += "[";
  1021. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1022. code += "]";
  1023. } break;
  1024. case SL::OP_SELECT_IF: {
  1025. code += "(";
  1026. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1027. code += "?";
  1028. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1029. code += ":";
  1030. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1031. code += ")";
  1032. } break;
  1033. default: {
  1034. if (p_use_scope) {
  1035. code += "(";
  1036. }
  1037. 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);
  1038. if (p_use_scope) {
  1039. code += ")";
  1040. }
  1041. break;
  1042. }
  1043. }
  1044. } break;
  1045. case SL::Node::TYPE_CONTROL_FLOW: {
  1046. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1047. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1048. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1049. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1050. if (cfnode->blocks.size() == 2) {
  1051. code += _mktab(p_level) + "else\n";
  1052. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1053. }
  1054. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1055. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1056. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1057. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1058. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1059. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1060. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1061. code += _mktab(p_level) + "default:\n";
  1062. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1063. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1064. code += _mktab(p_level) + "do";
  1065. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1066. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1067. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1068. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1069. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1070. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1071. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1072. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1073. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1074. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1075. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1076. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1077. if (cfnode->expressions.size()) {
  1078. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1079. } else {
  1080. code = "return;";
  1081. }
  1082. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1083. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1084. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1085. used_flag_pointers.insert("DISCARD");
  1086. }
  1087. code = "discard;";
  1088. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1089. code = "continue;";
  1090. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1091. code = "break;";
  1092. }
  1093. } break;
  1094. case SL::Node::TYPE_MEMBER: {
  1095. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1096. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1097. if (mnode->index_expression != nullptr) {
  1098. code += "[";
  1099. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1100. code += "]";
  1101. }
  1102. } break;
  1103. }
  1104. return code;
  1105. }
  1106. ShaderLanguage::DataType ShaderCompilerRD::_get_variable_type(const StringName &p_type) {
  1107. RS::GlobalVariableType gvt = ((RendererStorageRD *)(RendererStorage::base_singleton))->global_variable_get_type_internal(p_type);
  1108. return RS::global_variable_type_get_shader_datatype(gvt);
  1109. }
  1110. Error ShaderCompilerRD::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1111. 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);
  1112. if (err != OK) {
  1113. Vector<String> shader = p_code.split("\n");
  1114. for (int i = 0; i < shader.size(); i++) {
  1115. print_line(itos(i + 1) + " " + shader[i]);
  1116. }
  1117. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  1118. return err;
  1119. }
  1120. r_gen_code.defines.clear();
  1121. r_gen_code.vertex = String();
  1122. r_gen_code.vertex_global = String();
  1123. r_gen_code.fragment = String();
  1124. r_gen_code.fragment_global = String();
  1125. r_gen_code.compute = String();
  1126. r_gen_code.compute_global = String();
  1127. r_gen_code.light = String();
  1128. r_gen_code.uses_fragment_time = false;
  1129. r_gen_code.uses_vertex_time = false;
  1130. r_gen_code.uses_global_textures = false;
  1131. used_name_defines.clear();
  1132. used_rmode_defines.clear();
  1133. used_flag_pointers.clear();
  1134. shader = parser.get_shader();
  1135. function = nullptr;
  1136. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1137. return OK;
  1138. }
  1139. void ShaderCompilerRD::initialize(DefaultIdentifierActions p_actions) {
  1140. actions = p_actions;
  1141. vertex_name = "vertex";
  1142. fragment_name = "fragment";
  1143. compute_name = "compute";
  1144. light_name = "light";
  1145. time_name = "TIME";
  1146. List<String> func_list;
  1147. ShaderLanguage::get_builtin_funcs(&func_list);
  1148. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  1149. internal_functions.insert(E->get());
  1150. }
  1151. texture_functions.insert("texture");
  1152. texture_functions.insert("textureProj");
  1153. texture_functions.insert("textureLod");
  1154. texture_functions.insert("textureProjLod");
  1155. texture_functions.insert("textureGrad");
  1156. texture_functions.insert("textureSize");
  1157. texture_functions.insert("texelFetch");
  1158. }
  1159. ShaderCompilerRD::ShaderCompilerRD() {
  1160. #if 0
  1161. /** SPATIAL SHADER **/
  1162. actions[RS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  1163. actions[RS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  1164. actions[RS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  1165. actions[RS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  1166. actions[RS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1167. actions[RS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  1168. actions[RS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  1169. actions[RS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  1170. actions[RS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  1171. actions[RS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  1172. actions[RS::SHADER_SPATIAL].renames["POSITION"] = "position";
  1173. actions[RS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  1174. actions[RS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  1175. actions[RS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  1176. actions[RS::SHADER_SPATIAL].renames["POINT_SIZE"] = "gl_PointSize";
  1177. actions[RS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  1178. //builtins
  1179. actions[RS::SHADER_SPATIAL].renames["TIME"] = "time";
  1180. actions[RS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  1181. actions[RS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  1182. actions[RS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  1183. actions[RS::SHADER_SPATIAL].renames["NORMALMAP"] = "normalmap";
  1184. actions[RS::SHADER_SPATIAL].renames["NORMALMAP_DEPTH"] = "normaldepth";
  1185. actions[RS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  1186. actions[RS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  1187. actions[RS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  1188. actions[RS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  1189. actions[RS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  1190. actions[RS::SHADER_SPATIAL].renames["RIM"] = "rim";
  1191. actions[RS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  1192. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  1193. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  1194. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  1195. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1196. actions[RS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  1197. actions[RS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  1198. actions[RS::SHADER_SPATIAL].renames["AO"] = "ao";
  1199. actions[RS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1200. actions[RS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  1201. actions[RS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  1202. actions[RS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1203. actions[RS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  1204. actions[RS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  1205. actions[RS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  1206. actions[RS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  1207. actions[RS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  1208. actions[RS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1209. //for light
  1210. actions[RS::SHADER_SPATIAL].renames["VIEW"] = "view";
  1211. actions[RS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  1212. actions[RS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  1213. actions[RS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  1214. actions[RS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1215. actions[RS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  1216. actions[RS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  1217. actions[RS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  1218. actions[RS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  1219. actions[RS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  1220. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  1221. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  1222. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  1223. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1224. actions[RS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  1225. actions[RS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  1226. actions[RS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  1227. actions[RS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  1228. actions[RS::SHADER_SPATIAL].usage_defines["NORMALMAP"] = "#define ENABLE_NORMALMAP\n";
  1229. actions[RS::SHADER_SPATIAL].usage_defines["NORMALMAP_DEPTH"] = "@NORMALMAP";
  1230. actions[RS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  1231. actions[RS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1232. actions[RS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  1233. actions[RS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1234. actions[RS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1235. actions[RS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  1236. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1237. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1238. actions[RS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1239. actions[RS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1240. actions[RS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1241. actions[RS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1242. actions[RS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1243. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1244. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1245. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  1246. if (!force_lambert) {
  1247. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1248. }
  1249. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  1250. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1251. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1252. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  1253. if (!force_blinn) {
  1254. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1255. } else {
  1256. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  1257. }
  1258. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  1259. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  1260. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1261. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1262. actions[RS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1263. actions[RS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1264. actions[RS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1265. /* PARTICLES SHADER */
  1266. actions[RS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  1267. actions[RS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  1268. actions[RS::SHADER_PARTICLES].renames["MASS"] = "mass";
  1269. actions[RS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  1270. actions[RS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  1271. actions[RS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  1272. actions[RS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  1273. actions[RS::SHADER_PARTICLES].renames["TIME"] = "time";
  1274. actions[RS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  1275. actions[RS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  1276. actions[RS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  1277. actions[RS::SHADER_PARTICLES].renames["INDEX"] = "index";
  1278. actions[RS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  1279. actions[RS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  1280. actions[RS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  1281. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1282. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1283. actions[RS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1284. #endif
  1285. }