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