2
0

shader_compiler.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /**************************************************************************/
  2. /* shader_compiler.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "servers/rendering/rendering_server_globals.h"
  34. #include "servers/rendering/shader_types.h"
  35. #define SL ShaderLanguage
  36. static String _mktab(int p_level) {
  37. return String("\t").repeat(p_level);
  38. }
  39. static String _typestr(SL::DataType p_type) {
  40. String type = ShaderLanguage::get_datatype_name(p_type);
  41. if (!RS::get_singleton()->is_low_end() && ShaderLanguage::is_sampler_type(p_type)) {
  42. type = type.replace("sampler", "texture"); //we use textures instead of samplers in Vulkan GLSL
  43. }
  44. return type;
  45. }
  46. static int _get_datatype_alignment(SL::DataType p_type) {
  47. switch (p_type) {
  48. case SL::TYPE_VOID:
  49. return 0;
  50. case SL::TYPE_BOOL:
  51. return 4;
  52. case SL::TYPE_BVEC2:
  53. return 8;
  54. case SL::TYPE_BVEC3:
  55. return 16;
  56. case SL::TYPE_BVEC4:
  57. return 16;
  58. case SL::TYPE_INT:
  59. return 4;
  60. case SL::TYPE_IVEC2:
  61. return 8;
  62. case SL::TYPE_IVEC3:
  63. return 16;
  64. case SL::TYPE_IVEC4:
  65. return 16;
  66. case SL::TYPE_UINT:
  67. return 4;
  68. case SL::TYPE_UVEC2:
  69. return 8;
  70. case SL::TYPE_UVEC3:
  71. return 16;
  72. case SL::TYPE_UVEC4:
  73. return 16;
  74. case SL::TYPE_FLOAT:
  75. return 4;
  76. case SL::TYPE_VEC2:
  77. return 8;
  78. case SL::TYPE_VEC3:
  79. return 16;
  80. case SL::TYPE_VEC4:
  81. return 16;
  82. case SL::TYPE_MAT2:
  83. return 16;
  84. case SL::TYPE_MAT3:
  85. return 16;
  86. case SL::TYPE_MAT4:
  87. return 16;
  88. case SL::TYPE_SAMPLER2D:
  89. return 16;
  90. case SL::TYPE_ISAMPLER2D:
  91. return 16;
  92. case SL::TYPE_USAMPLER2D:
  93. return 16;
  94. case SL::TYPE_SAMPLER2DARRAY:
  95. return 16;
  96. case SL::TYPE_ISAMPLER2DARRAY:
  97. return 16;
  98. case SL::TYPE_USAMPLER2DARRAY:
  99. return 16;
  100. case SL::TYPE_SAMPLER3D:
  101. return 16;
  102. case SL::TYPE_ISAMPLER3D:
  103. return 16;
  104. case SL::TYPE_USAMPLER3D:
  105. return 16;
  106. case SL::TYPE_SAMPLERCUBE:
  107. return 16;
  108. case SL::TYPE_SAMPLERCUBEARRAY:
  109. return 16;
  110. case SL::TYPE_STRUCT:
  111. return 0;
  112. case SL::TYPE_MAX: {
  113. ERR_FAIL_V(0);
  114. }
  115. }
  116. ERR_FAIL_V(0);
  117. }
  118. static String _interpstr(SL::DataInterpolation p_interp) {
  119. switch (p_interp) {
  120. case SL::INTERPOLATION_FLAT:
  121. return "flat ";
  122. case SL::INTERPOLATION_SMOOTH:
  123. return "";
  124. case SL::INTERPOLATION_DEFAULT:
  125. return "";
  126. }
  127. return "";
  128. }
  129. static String _prestr(SL::DataPrecision p_pres, bool p_force_highp = false) {
  130. switch (p_pres) {
  131. case SL::PRECISION_LOWP:
  132. return "lowp ";
  133. case SL::PRECISION_MEDIUMP:
  134. return "mediump ";
  135. case SL::PRECISION_HIGHP:
  136. return "highp ";
  137. case SL::PRECISION_DEFAULT:
  138. return p_force_highp ? "highp " : "";
  139. }
  140. return "";
  141. }
  142. static String _constr(bool p_is_const) {
  143. if (p_is_const) {
  144. return "const ";
  145. }
  146. return "";
  147. }
  148. static String _qualstr(SL::ArgumentQualifier p_qual) {
  149. switch (p_qual) {
  150. case SL::ARGUMENT_QUALIFIER_IN:
  151. return "";
  152. case SL::ARGUMENT_QUALIFIER_OUT:
  153. return "out ";
  154. case SL::ARGUMENT_QUALIFIER_INOUT:
  155. return "inout ";
  156. }
  157. return "";
  158. }
  159. static String _opstr(SL::Operator p_op) {
  160. return SL::get_operator_text(p_op);
  161. }
  162. static String _mkid(const String &p_id) {
  163. String id = "m_" + p_id.replace("__", "_dus_");
  164. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  165. }
  166. static String f2sp0(float p_float) {
  167. String num = rtos(p_float);
  168. if (!num.contains(".") && !num.contains("e")) {
  169. num += ".0";
  170. }
  171. return num;
  172. }
  173. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  174. switch (p_type) {
  175. case SL::TYPE_BOOL:
  176. return p_values[0].boolean ? "true" : "false";
  177. case SL::TYPE_BVEC2:
  178. case SL::TYPE_BVEC3:
  179. case SL::TYPE_BVEC4: {
  180. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  181. for (int i = 0; i < p_values.size(); i++) {
  182. if (i > 0) {
  183. text += ",";
  184. }
  185. text += p_values[i].boolean ? "true" : "false";
  186. }
  187. text += ")";
  188. return text;
  189. }
  190. case SL::TYPE_INT:
  191. return itos(p_values[0].sint);
  192. case SL::TYPE_IVEC2:
  193. case SL::TYPE_IVEC3:
  194. case SL::TYPE_IVEC4: {
  195. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  196. for (int i = 0; i < p_values.size(); i++) {
  197. if (i > 0) {
  198. text += ",";
  199. }
  200. text += itos(p_values[i].sint);
  201. }
  202. text += ")";
  203. return text;
  204. } break;
  205. case SL::TYPE_UINT:
  206. return itos(p_values[0].uint) + "u";
  207. case SL::TYPE_UVEC2:
  208. case SL::TYPE_UVEC3:
  209. case SL::TYPE_UVEC4: {
  210. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  211. for (int i = 0; i < p_values.size(); i++) {
  212. if (i > 0) {
  213. text += ",";
  214. }
  215. text += itos(p_values[i].uint) + "u";
  216. }
  217. text += ")";
  218. return text;
  219. } break;
  220. case SL::TYPE_FLOAT:
  221. return f2sp0(p_values[0].real);
  222. case SL::TYPE_VEC2:
  223. case SL::TYPE_VEC3:
  224. case SL::TYPE_VEC4: {
  225. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  226. for (int i = 0; i < p_values.size(); i++) {
  227. if (i > 0) {
  228. text += ",";
  229. }
  230. text += f2sp0(p_values[i].real);
  231. }
  232. text += ")";
  233. return text;
  234. } break;
  235. case SL::TYPE_MAT2:
  236. case SL::TYPE_MAT3:
  237. case SL::TYPE_MAT4: {
  238. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  239. for (int i = 0; i < p_values.size(); i++) {
  240. if (i > 0) {
  241. text += ",";
  242. }
  243. text += f2sp0(p_values[i].real);
  244. }
  245. text += ")";
  246. return text;
  247. } break;
  248. default:
  249. ERR_FAIL_V(String());
  250. }
  251. }
  252. String ShaderCompiler::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) {
  253. if (p_filter == ShaderLanguage::FILTER_DEFAULT) {
  254. ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String());
  255. p_filter = actions.default_filter;
  256. }
  257. if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) {
  258. ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String());
  259. p_repeat = actions.default_repeat;
  260. }
  261. return actions.sampler_array_name + "[" + itos(p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)) + "]";
  262. }
  263. void ShaderCompiler::_dump_function_deps(const SL::ShaderNode *p_node, const StringName &p_for_func, const HashMap<StringName, String> &p_func_code, String &r_to_add, HashSet<StringName> &added) {
  264. int fidx = -1;
  265. for (int i = 0; i < p_node->functions.size(); i++) {
  266. if (p_node->functions[i].name == p_for_func) {
  267. fidx = i;
  268. break;
  269. }
  270. }
  271. ERR_FAIL_COND(fidx == -1);
  272. Vector<StringName> uses_functions;
  273. for (const StringName &E : p_node->functions[fidx].uses_function) {
  274. uses_functions.push_back(E);
  275. }
  276. uses_functions.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  277. for (int k = 0; k < uses_functions.size(); k++) {
  278. if (added.has(uses_functions[k])) {
  279. continue; //was added already
  280. }
  281. _dump_function_deps(p_node, uses_functions[k], p_func_code, r_to_add, added);
  282. SL::FunctionNode *fnode = nullptr;
  283. for (int i = 0; i < p_node->functions.size(); i++) {
  284. if (p_node->functions[i].name == uses_functions[k]) {
  285. fnode = p_node->functions[i].function;
  286. break;
  287. }
  288. }
  289. ERR_FAIL_COND(!fnode);
  290. r_to_add += "\n";
  291. String header;
  292. if (fnode->return_type == SL::TYPE_STRUCT) {
  293. header = _mkid(fnode->return_struct_name);
  294. } else {
  295. header = _typestr(fnode->return_type);
  296. }
  297. if (fnode->return_array_size > 0) {
  298. header += "[";
  299. header += itos(fnode->return_array_size);
  300. header += "]";
  301. }
  302. header += " ";
  303. header += _mkid(fnode->name);
  304. header += "(";
  305. for (int i = 0; i < fnode->arguments.size(); i++) {
  306. if (i > 0) {
  307. header += ", ";
  308. }
  309. header += _constr(fnode->arguments[i].is_const);
  310. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  311. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  312. } else {
  313. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  314. }
  315. if (fnode->arguments[i].array_size > 0) {
  316. header += "[";
  317. header += itos(fnode->arguments[i].array_size);
  318. header += "]";
  319. }
  320. }
  321. header += ")\n";
  322. r_to_add += header;
  323. r_to_add += p_func_code[uses_functions[k]];
  324. added.insert(uses_functions[k]);
  325. }
  326. }
  327. static String _get_global_shader_uniform_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
  328. switch (p_type) {
  329. case ShaderLanguage::TYPE_BOOL: {
  330. return "bool(floatBitsToUint(" + p_buffer + "[" + p_index + "].x))";
  331. }
  332. case ShaderLanguage::TYPE_BVEC2: {
  333. return "bvec2(floatBitsToUint(" + p_buffer + "[" + p_index + "].xy))";
  334. }
  335. case ShaderLanguage::TYPE_BVEC3: {
  336. return "bvec3(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz))";
  337. }
  338. case ShaderLanguage::TYPE_BVEC4: {
  339. return "bvec4(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw))";
  340. }
  341. case ShaderLanguage::TYPE_INT: {
  342. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
  343. }
  344. case ShaderLanguage::TYPE_IVEC2: {
  345. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
  346. }
  347. case ShaderLanguage::TYPE_IVEC3: {
  348. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
  349. }
  350. case ShaderLanguage::TYPE_IVEC4: {
  351. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
  352. }
  353. case ShaderLanguage::TYPE_UINT: {
  354. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)";
  355. }
  356. case ShaderLanguage::TYPE_UVEC2: {
  357. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)";
  358. }
  359. case ShaderLanguage::TYPE_UVEC3: {
  360. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)";
  361. }
  362. case ShaderLanguage::TYPE_UVEC4: {
  363. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)";
  364. }
  365. case ShaderLanguage::TYPE_FLOAT: {
  366. return "(" + p_buffer + "[" + p_index + "].x)";
  367. }
  368. case ShaderLanguage::TYPE_VEC2: {
  369. return "(" + p_buffer + "[" + p_index + "].xy)";
  370. }
  371. case ShaderLanguage::TYPE_VEC3: {
  372. return "(" + p_buffer + "[" + p_index + "].xyz)";
  373. }
  374. case ShaderLanguage::TYPE_VEC4: {
  375. return "(" + p_buffer + "[" + p_index + "].xyzw)";
  376. }
  377. case ShaderLanguage::TYPE_MAT2: {
  378. return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)";
  379. }
  380. case ShaderLanguage::TYPE_MAT3: {
  381. return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)";
  382. }
  383. case ShaderLanguage::TYPE_MAT4: {
  384. 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)";
  385. }
  386. default: {
  387. ERR_FAIL_V("void");
  388. }
  389. }
  390. }
  391. String ShaderCompiler::_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) {
  392. String code;
  393. switch (p_node->type) {
  394. case SL::Node::NODE_TYPE_SHADER: {
  395. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  396. for (int i = 0; i < pnode->render_modes.size(); i++) {
  397. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  398. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
  399. used_rmode_defines.insert(pnode->render_modes[i]);
  400. }
  401. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  402. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  403. }
  404. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  405. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  406. *p.first = p.second;
  407. }
  408. }
  409. // structs
  410. for (int i = 0; i < pnode->vstructs.size(); i++) {
  411. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  412. String struct_code;
  413. struct_code += "struct ";
  414. struct_code += _mkid(pnode->vstructs[i].name);
  415. struct_code += " ";
  416. struct_code += "{\n";
  417. for (int j = 0; j < st->members.size(); j++) {
  418. SL::MemberNode *m = st->members[j];
  419. if (m->datatype == SL::TYPE_STRUCT) {
  420. struct_code += _mkid(m->struct_name);
  421. } else {
  422. struct_code += _prestr(m->precision);
  423. struct_code += _typestr(m->datatype);
  424. }
  425. struct_code += " ";
  426. struct_code += m->name;
  427. if (m->array_size > 0) {
  428. struct_code += "[";
  429. struct_code += itos(m->array_size);
  430. struct_code += "]";
  431. }
  432. struct_code += ";\n";
  433. }
  434. struct_code += "}";
  435. struct_code += ";\n";
  436. for (int j = 0; j < STAGE_MAX; j++) {
  437. r_gen_code.stage_globals[j] += struct_code;
  438. }
  439. }
  440. int max_texture_uniforms = 0;
  441. int max_uniforms = 0;
  442. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  443. if (SL::is_sampler_type(E.value.type)) {
  444. if (E.value.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  445. E.value.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  446. E.value.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  447. continue; // Don't create uniforms in the generated code for these.
  448. }
  449. max_texture_uniforms++;
  450. } else {
  451. if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  452. continue; // Instances are indexed directly, don't need index uniforms.
  453. }
  454. max_uniforms++;
  455. }
  456. }
  457. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  458. Vector<int> uniform_sizes;
  459. Vector<int> uniform_alignments;
  460. Vector<StringName> uniform_defines;
  461. uniform_sizes.resize(max_uniforms);
  462. uniform_alignments.resize(max_uniforms);
  463. uniform_defines.resize(max_uniforms);
  464. bool uses_uniforms = false;
  465. Vector<StringName> uniform_names;
  466. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  467. uniform_names.push_back(E.key);
  468. }
  469. uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  470. for (int k = 0; k < uniform_names.size(); k++) {
  471. StringName uniform_name = uniform_names[k];
  472. const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name];
  473. String ucode;
  474. if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  475. //insert, but don't generate any code.
  476. p_actions.uniforms->insert(uniform_name, uniform);
  477. continue; // Instances are indexed directly, don't need index uniforms.
  478. }
  479. if (uniform.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  480. uniform.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  481. uniform.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  482. continue; // Don't create uniforms in the generated code for these.
  483. }
  484. if (SL::is_sampler_type(uniform.type)) {
  485. // Texture layouts are different for OpenGL GLSL and Vulkan GLSL
  486. if (!RS::get_singleton()->is_low_end()) {
  487. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") ";
  488. }
  489. ucode += "uniform ";
  490. }
  491. bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  492. if (is_buffer_global) {
  493. //this is an integer to index the global table
  494. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  495. } else {
  496. ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type));
  497. ucode += _typestr(uniform.type);
  498. }
  499. ucode += " " + _mkid(uniform_name);
  500. if (uniform.array_size > 0) {
  501. ucode += "[";
  502. ucode += itos(uniform.array_size);
  503. ucode += "]";
  504. }
  505. ucode += ";\n";
  506. if (SL::is_sampler_type(uniform.type)) {
  507. for (int j = 0; j < STAGE_MAX; j++) {
  508. r_gen_code.stage_globals[j] += ucode;
  509. }
  510. GeneratedCode::Texture texture;
  511. texture.name = uniform_name;
  512. texture.hint = uniform.hint;
  513. texture.type = uniform.type;
  514. texture.use_color = uniform.use_color;
  515. texture.filter = uniform.filter;
  516. texture.repeat = uniform.repeat;
  517. texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  518. texture.array_size = uniform.array_size;
  519. if (texture.global) {
  520. r_gen_code.uses_global_textures = true;
  521. }
  522. r_gen_code.texture_uniforms.write[uniform.texture_order] = texture;
  523. } else {
  524. if (!uses_uniforms) {
  525. uses_uniforms = true;
  526. }
  527. uniform_defines.write[uniform.order] = ucode;
  528. if (is_buffer_global) {
  529. //globals are indices into the global table
  530. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(ShaderLanguage::TYPE_UINT);
  531. uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  532. } else {
  533. // The following code enforces a 16-byte alignment of uniform arrays.
  534. if (uniform.array_size > 0) {
  535. int size = ShaderLanguage::get_datatype_size(uniform.type) * uniform.array_size;
  536. int m = (16 * uniform.array_size);
  537. if ((size % m) != 0) {
  538. size += m - (size % m);
  539. }
  540. uniform_sizes.write[uniform.order] = size;
  541. uniform_alignments.write[uniform.order] = 16;
  542. } else {
  543. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(uniform.type);
  544. uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type);
  545. }
  546. }
  547. }
  548. p_actions.uniforms->insert(uniform_name, uniform);
  549. }
  550. for (int i = 0; i < max_uniforms; i++) {
  551. r_gen_code.uniforms += uniform_defines[i];
  552. }
  553. // add up
  554. int offset = 0;
  555. for (int i = 0; i < uniform_sizes.size(); i++) {
  556. int align = offset % uniform_alignments[i];
  557. if (align != 0) {
  558. offset += uniform_alignments[i] - align;
  559. }
  560. r_gen_code.uniform_offsets.push_back(offset);
  561. offset += uniform_sizes[i];
  562. }
  563. r_gen_code.uniform_total_size = offset;
  564. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  565. r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
  566. }
  567. uint32_t index = p_default_actions.base_varying_index;
  568. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  569. Vector<StringName> varying_names;
  570. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  571. varying_names.push_back(E.key);
  572. }
  573. varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  574. for (int k = 0; k < varying_names.size(); k++) {
  575. StringName varying_name = varying_names[k];
  576. const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name];
  577. if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  578. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying));
  579. fragment_varyings.insert(varying_name);
  580. continue;
  581. }
  582. if (varying.type < SL::TYPE_INT) {
  583. continue; // Ignore boolean types to prevent crashing (if varying is just declared).
  584. }
  585. String vcode;
  586. String interp_mode = _interpstr(varying.interpolation);
  587. vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type));
  588. vcode += _typestr(varying.type);
  589. vcode += " " + _mkid(varying_name);
  590. uint32_t inc = 1U;
  591. if (varying.array_size > 0) {
  592. inc = (uint32_t)varying.array_size;
  593. vcode += "[";
  594. vcode += itos(varying.array_size);
  595. vcode += "]";
  596. }
  597. switch (varying.type) {
  598. case SL::TYPE_MAT2:
  599. inc *= 2U;
  600. break;
  601. case SL::TYPE_MAT3:
  602. inc *= 3U;
  603. break;
  604. case SL::TYPE_MAT4:
  605. inc *= 4U;
  606. break;
  607. default:
  608. break;
  609. }
  610. vcode += ";\n";
  611. // GLSL ES 3.0 does not allow layout qualifiers for varyings
  612. if (!RS::get_singleton()->is_low_end()) {
  613. r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") ";
  614. r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") ";
  615. }
  616. r_gen_code.stage_globals[STAGE_VERTEX] += interp_mode + "out " + vcode;
  617. r_gen_code.stage_globals[STAGE_FRAGMENT] += interp_mode + "in " + vcode;
  618. index += inc;
  619. }
  620. if (var_frag_to_light.size() > 0) {
  621. String gcode = "\n\nstruct {\n";
  622. for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) {
  623. gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first);
  624. if (E.second.array_size > 0) {
  625. gcode += "[";
  626. gcode += itos(E.second.array_size);
  627. gcode += "]";
  628. }
  629. gcode += ";\n";
  630. }
  631. gcode += "} frag_to_light;\n";
  632. r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode;
  633. }
  634. for (int i = 0; i < pnode->vconstants.size(); i++) {
  635. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  636. String gcode;
  637. gcode += _constr(true);
  638. gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
  639. if (cnode.type == SL::TYPE_STRUCT) {
  640. gcode += _mkid(cnode.type_str);
  641. } else {
  642. gcode += _typestr(cnode.type);
  643. }
  644. gcode += " " + _mkid(String(cnode.name));
  645. if (cnode.array_size > 0) {
  646. gcode += "[";
  647. gcode += itos(cnode.array_size);
  648. gcode += "]";
  649. }
  650. gcode += "=";
  651. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  652. gcode += ";\n";
  653. for (int j = 0; j < STAGE_MAX; j++) {
  654. r_gen_code.stage_globals[j] += gcode;
  655. }
  656. }
  657. HashMap<StringName, String> function_code;
  658. //code for functions
  659. for (int i = 0; i < pnode->functions.size(); i++) {
  660. SL::FunctionNode *fnode = pnode->functions[i].function;
  661. function = fnode;
  662. current_func_name = fnode->name;
  663. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  664. function = nullptr;
  665. }
  666. //place functions in actual code
  667. HashSet<StringName> added_funcs_per_stage[STAGE_MAX];
  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 (p_actions.entry_point_stages.has(fnode->name)) {
  673. Stage stage = p_actions.entry_point_stages[fnode->name];
  674. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]);
  675. r_gen_code.code[fnode->name] = function_code[fnode->name];
  676. }
  677. function = nullptr;
  678. }
  679. //code+=dump_node_code(pnode->body,p_level);
  680. } break;
  681. case SL::Node::NODE_TYPE_STRUCT: {
  682. } break;
  683. case SL::Node::NODE_TYPE_FUNCTION: {
  684. } break;
  685. case SL::Node::NODE_TYPE_BLOCK: {
  686. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  687. //variables
  688. if (!bnode->single_statement) {
  689. code += _mktab(p_level - 1) + "{\n";
  690. }
  691. for (int i = 0; i < bnode->statements.size(); i++) {
  692. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  693. if (bnode->statements[i]->type == SL::Node::NODE_TYPE_CONTROL_FLOW || bnode->single_statement) {
  694. code += scode; //use directly
  695. if (bnode->use_comma_between_statements && i + 1 < bnode->statements.size()) {
  696. code += ",";
  697. }
  698. } else {
  699. code += _mktab(p_level) + scode + ";\n";
  700. }
  701. }
  702. if (!bnode->single_statement) {
  703. code += _mktab(p_level - 1) + "}\n";
  704. }
  705. } break;
  706. case SL::Node::NODE_TYPE_VARIABLE_DECLARATION: {
  707. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  708. String declaration;
  709. declaration += _constr(vdnode->is_const);
  710. if (vdnode->datatype == SL::TYPE_STRUCT) {
  711. declaration += _mkid(vdnode->struct_name);
  712. } else {
  713. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  714. }
  715. declaration += " ";
  716. for (int i = 0; i < vdnode->declarations.size(); i++) {
  717. bool is_array = vdnode->declarations[i].size > 0;
  718. if (i > 0) {
  719. declaration += ",";
  720. }
  721. declaration += _mkid(vdnode->declarations[i].name);
  722. if (is_array) {
  723. declaration += "[";
  724. if (vdnode->declarations[i].size_expression != nullptr) {
  725. declaration += _dump_node_code(vdnode->declarations[i].size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  726. } else {
  727. declaration += itos(vdnode->declarations[i].size);
  728. }
  729. declaration += "]";
  730. }
  731. if (!is_array || vdnode->declarations[i].single_expression) {
  732. if (!vdnode->declarations[i].initializer.is_empty()) {
  733. declaration += "=";
  734. declaration += _dump_node_code(vdnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  735. }
  736. } else {
  737. int size = vdnode->declarations[i].initializer.size();
  738. if (size > 0) {
  739. declaration += "=";
  740. if (vdnode->datatype == SL::TYPE_STRUCT) {
  741. declaration += _mkid(vdnode->struct_name);
  742. } else {
  743. declaration += _typestr(vdnode->datatype);
  744. }
  745. declaration += "[";
  746. declaration += itos(size);
  747. declaration += "]";
  748. declaration += "(";
  749. for (int j = 0; j < size; j++) {
  750. if (j > 0) {
  751. declaration += ",";
  752. }
  753. declaration += _dump_node_code(vdnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  754. }
  755. declaration += ")";
  756. }
  757. }
  758. }
  759. code += declaration;
  760. } break;
  761. case SL::Node::NODE_TYPE_VARIABLE: {
  762. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  763. bool use_fragment_varying = false;
  764. if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  765. if (p_assigning) {
  766. if (shader->varyings.has(vnode->name)) {
  767. use_fragment_varying = true;
  768. }
  769. } else {
  770. if (fragment_varyings.has(vnode->name)) {
  771. use_fragment_varying = true;
  772. }
  773. }
  774. }
  775. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  776. *p_actions.write_flag_pointers[vnode->name] = true;
  777. }
  778. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  779. String define = p_default_actions.usage_defines[vnode->name];
  780. if (define.begins_with("@")) {
  781. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  782. }
  783. r_gen_code.defines.push_back(define);
  784. used_name_defines.insert(vnode->name);
  785. }
  786. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  787. *p_actions.usage_flag_pointers[vnode->name] = true;
  788. used_flag_pointers.insert(vnode->name);
  789. }
  790. if (p_default_actions.renames.has(vnode->name)) {
  791. code = p_default_actions.renames[vnode->name];
  792. } else {
  793. if (shader->uniforms.has(vnode->name)) {
  794. //its a uniform!
  795. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  796. if (u.texture_order >= 0) {
  797. StringName name = vnode->name;
  798. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
  799. name = "color_buffer";
  800. if (u.filter >= ShaderLanguage::FILTER_NEAREST_MIPMAP) {
  801. r_gen_code.uses_screen_texture_mipmaps = true;
  802. }
  803. r_gen_code.uses_screen_texture = true;
  804. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  805. name = "normal_roughness_buffer";
  806. r_gen_code.uses_normal_roughness_texture = true;
  807. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  808. name = "depth_buffer";
  809. r_gen_code.uses_depth_texture = true;
  810. } else {
  811. name = _mkid(vnode->name); //texture, use as is
  812. }
  813. code = name;
  814. } else {
  815. //a scalar or vector
  816. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  817. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  818. //global variable, this means the code points to an index to the global table
  819. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  820. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  821. //instance variable, index it as such
  822. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  823. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  824. } else {
  825. //regular uniform, index from UBO
  826. code = actions.base_uniform_string + _mkid(vnode->name);
  827. }
  828. }
  829. } else {
  830. if (use_fragment_varying) {
  831. code = "frag_to_light.";
  832. }
  833. code += _mkid(vnode->name); //its something else (local var most likely) use as is
  834. }
  835. }
  836. if (vnode->name == time_name) {
  837. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  838. r_gen_code.uses_vertex_time = true;
  839. }
  840. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  841. r_gen_code.uses_fragment_time = true;
  842. }
  843. }
  844. } break;
  845. case SL::Node::NODE_TYPE_ARRAY_CONSTRUCT: {
  846. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  847. int sz = acnode->initializer.size();
  848. if (acnode->datatype == SL::TYPE_STRUCT) {
  849. code += _mkid(acnode->struct_name);
  850. } else {
  851. code += _typestr(acnode->datatype);
  852. }
  853. code += "[";
  854. code += itos(acnode->initializer.size());
  855. code += "]";
  856. code += "(";
  857. for (int i = 0; i < sz; i++) {
  858. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  859. if (i != sz - 1) {
  860. code += ", ";
  861. }
  862. }
  863. code += ")";
  864. } break;
  865. case SL::Node::NODE_TYPE_ARRAY: {
  866. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  867. bool use_fragment_varying = false;
  868. if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  869. if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) {
  870. use_fragment_varying = true;
  871. } else {
  872. if (p_assigning) {
  873. if (shader->varyings.has(anode->name)) {
  874. use_fragment_varying = true;
  875. }
  876. } else {
  877. if (fragment_varyings.has(anode->name)) {
  878. use_fragment_varying = true;
  879. }
  880. }
  881. }
  882. }
  883. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  884. *p_actions.write_flag_pointers[anode->name] = true;
  885. }
  886. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  887. String define = p_default_actions.usage_defines[anode->name];
  888. if (define.begins_with("@")) {
  889. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  890. }
  891. r_gen_code.defines.push_back(define);
  892. used_name_defines.insert(anode->name);
  893. }
  894. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  895. *p_actions.usage_flag_pointers[anode->name] = true;
  896. used_flag_pointers.insert(anode->name);
  897. }
  898. if (p_default_actions.renames.has(anode->name)) {
  899. code = p_default_actions.renames[anode->name];
  900. } else {
  901. if (shader->uniforms.has(anode->name)) {
  902. //its a uniform!
  903. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name];
  904. if (u.texture_order >= 0) {
  905. code = _mkid(anode->name); //texture, use as is
  906. } else {
  907. //a scalar or vector
  908. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  909. code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is
  910. //global variable, this means the code points to an index to the global table
  911. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  912. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  913. //instance variable, index it as such
  914. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  915. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  916. } else {
  917. //regular uniform, index from UBO
  918. code = actions.base_uniform_string + _mkid(anode->name);
  919. }
  920. }
  921. } else {
  922. if (use_fragment_varying) {
  923. code = "frag_to_light.";
  924. }
  925. code += _mkid(anode->name);
  926. }
  927. }
  928. if (anode->call_expression != nullptr) {
  929. code += ".";
  930. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  931. } else if (anode->index_expression != nullptr) {
  932. code += "[";
  933. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  934. code += "]";
  935. } else if (anode->assign_expression != nullptr) {
  936. code += "=";
  937. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  938. }
  939. if (anode->name == time_name) {
  940. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  941. r_gen_code.uses_vertex_time = true;
  942. }
  943. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  944. r_gen_code.uses_fragment_time = true;
  945. }
  946. }
  947. } break;
  948. case SL::Node::NODE_TYPE_CONSTANT: {
  949. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  950. if (cnode->array_size == 0) {
  951. return get_constant_text(cnode->datatype, cnode->values);
  952. } else {
  953. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  954. code += _mkid(cnode->struct_name);
  955. } else {
  956. code += _typestr(cnode->datatype);
  957. }
  958. code += "[";
  959. code += itos(cnode->array_size);
  960. code += "]";
  961. code += "(";
  962. for (int i = 0; i < cnode->array_size; i++) {
  963. if (i > 0) {
  964. code += ",";
  965. } else {
  966. code += "";
  967. }
  968. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  969. }
  970. code += ")";
  971. }
  972. } break;
  973. case SL::Node::NODE_TYPE_OPERATOR: {
  974. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  975. switch (onode->op) {
  976. case SL::OP_ASSIGN:
  977. case SL::OP_ASSIGN_ADD:
  978. case SL::OP_ASSIGN_SUB:
  979. case SL::OP_ASSIGN_MUL:
  980. case SL::OP_ASSIGN_DIV:
  981. case SL::OP_ASSIGN_SHIFT_LEFT:
  982. case SL::OP_ASSIGN_SHIFT_RIGHT:
  983. case SL::OP_ASSIGN_MOD:
  984. case SL::OP_ASSIGN_BIT_AND:
  985. case SL::OP_ASSIGN_BIT_OR:
  986. case SL::OP_ASSIGN_BIT_XOR:
  987. 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);
  988. break;
  989. case SL::OP_BIT_INVERT:
  990. case SL::OP_NEGATE:
  991. case SL::OP_NOT:
  992. case SL::OP_DECREMENT:
  993. case SL::OP_INCREMENT:
  994. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  995. break;
  996. case SL::OP_POST_DECREMENT:
  997. case SL::OP_POST_INCREMENT:
  998. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  999. break;
  1000. case SL::OP_CALL:
  1001. case SL::OP_STRUCT:
  1002. case SL::OP_CONSTRUCT: {
  1003. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::NODE_TYPE_VARIABLE, String());
  1004. const SL::VariableNode *vnode = static_cast<const SL::VariableNode *>(onode->arguments[0]);
  1005. const SL::FunctionNode *func = nullptr;
  1006. const bool is_internal_func = internal_functions.has(vnode->name);
  1007. if (!is_internal_func) {
  1008. for (int i = 0; i < shader->functions.size(); i++) {
  1009. if (shader->functions[i].name == vnode->name) {
  1010. func = shader->functions[i].function;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. bool is_texture_func = false;
  1016. bool is_screen_texture = false;
  1017. bool texture_func_no_uv = false;
  1018. bool texture_func_returns_data = false;
  1019. if (onode->op == SL::OP_STRUCT) {
  1020. code += _mkid(vnode->name);
  1021. } else if (onode->op == SL::OP_CONSTRUCT) {
  1022. code += String(vnode->name);
  1023. } else {
  1024. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  1025. *p_actions.usage_flag_pointers[vnode->name] = true;
  1026. used_flag_pointers.insert(vnode->name);
  1027. }
  1028. if (is_internal_func) {
  1029. code += vnode->name;
  1030. is_texture_func = texture_functions.has(vnode->name);
  1031. texture_func_no_uv = (vnode->name == "textureSize" || vnode->name == "textureQueryLevels");
  1032. texture_func_returns_data = texture_func_no_uv || vnode->name == "textureQueryLod";
  1033. } else if (p_default_actions.renames.has(vnode->name)) {
  1034. code += p_default_actions.renames[vnode->name];
  1035. } else {
  1036. code += _mkid(vnode->name);
  1037. }
  1038. }
  1039. code += "(";
  1040. // if color backbuffer, depth backbuffer or normal roughness texture is used,
  1041. // we will add logic to automatically switch between
  1042. // sampler2D and sampler2D array and vec2 UV and vec3 UV.
  1043. bool multiview_uv_needed = false;
  1044. for (int i = 1; i < onode->arguments.size(); i++) {
  1045. if (i > 1) {
  1046. code += ", ";
  1047. }
  1048. bool is_out_qualifier = false;
  1049. if (is_internal_func) {
  1050. is_out_qualifier = SL::is_builtin_func_out_parameter(vnode->name, i - 1);
  1051. } else if (func != nullptr) {
  1052. const SL::ArgumentQualifier qualifier = func->arguments[i - 1].qualifier;
  1053. is_out_qualifier = qualifier == SL::ARGUMENT_QUALIFIER_OUT || qualifier == SL::ARGUMENT_QUALIFIER_INOUT;
  1054. }
  1055. if (is_out_qualifier) {
  1056. StringName name;
  1057. bool found = false;
  1058. {
  1059. const SL::Node *node = onode->arguments[i];
  1060. bool done = false;
  1061. do {
  1062. switch (node->type) {
  1063. case SL::Node::NODE_TYPE_VARIABLE: {
  1064. name = static_cast<const SL::VariableNode *>(node)->name;
  1065. done = true;
  1066. found = true;
  1067. } break;
  1068. case SL::Node::NODE_TYPE_MEMBER: {
  1069. node = static_cast<const SL::MemberNode *>(node)->owner;
  1070. } break;
  1071. default: {
  1072. done = true;
  1073. } break;
  1074. }
  1075. } while (!done);
  1076. }
  1077. if (found && p_actions.write_flag_pointers.has(name)) {
  1078. *p_actions.write_flag_pointers[name] = true;
  1079. }
  1080. }
  1081. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1082. if (is_texture_func && i == 1) {
  1083. // If we're doing a texture lookup we need to check our texture argument
  1084. StringName texture_uniform;
  1085. bool correct_texture_uniform = false;
  1086. switch (onode->arguments[i]->type) {
  1087. case SL::Node::NODE_TYPE_VARIABLE: {
  1088. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  1089. texture_uniform = varnode->name;
  1090. correct_texture_uniform = true;
  1091. } break;
  1092. case SL::Node::NODE_TYPE_ARRAY: {
  1093. const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]);
  1094. texture_uniform = anode->name;
  1095. correct_texture_uniform = true;
  1096. } break;
  1097. default:
  1098. break;
  1099. }
  1100. if (correct_texture_uniform && !RS::get_singleton()->is_low_end()) {
  1101. // Need to map from texture to sampler in order to sample when using Vulkan GLSL.
  1102. String sampler_name;
  1103. bool is_depth_texture = false;
  1104. bool is_normal_roughness_texture = false;
  1105. if (actions.custom_samplers.has(texture_uniform)) {
  1106. sampler_name = actions.custom_samplers[texture_uniform];
  1107. } else {
  1108. if (shader->uniforms.has(texture_uniform)) {
  1109. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform];
  1110. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
  1111. is_screen_texture = true;
  1112. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  1113. is_depth_texture = true;
  1114. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  1115. is_normal_roughness_texture = true;
  1116. }
  1117. sampler_name = _get_sampler_name(u.filter, u.repeat);
  1118. } else {
  1119. bool found = false;
  1120. for (int j = 0; j < function->arguments.size(); j++) {
  1121. if (function->arguments[j].name == texture_uniform) {
  1122. if (function->arguments[j].tex_builtin_check) {
  1123. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  1124. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  1125. found = true;
  1126. break;
  1127. }
  1128. if (function->arguments[j].tex_argument_check) {
  1129. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1130. found = true;
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. if (!found) {
  1136. //function was most likely unused, so use anything (compiler will remove it anyway)
  1137. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1138. }
  1139. }
  1140. }
  1141. String data_type_name = "";
  1142. if (actions.check_multiview_samplers && (is_screen_texture || is_depth_texture || is_normal_roughness_texture)) {
  1143. data_type_name = "multiviewSampler";
  1144. multiview_uv_needed = true;
  1145. } else {
  1146. data_type_name = ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype());
  1147. }
  1148. code += data_type_name + "(" + node_code + ", " + sampler_name + ")";
  1149. } else if (actions.check_multiview_samplers && correct_texture_uniform && RS::get_singleton()->is_low_end()) {
  1150. // Texture function on low end hardware (i.e. OpenGL).
  1151. // We just need to know if the texture supports multiview.
  1152. if (shader->uniforms.has(texture_uniform)) {
  1153. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform];
  1154. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
  1155. multiview_uv_needed = true;
  1156. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  1157. multiview_uv_needed = true;
  1158. } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  1159. multiview_uv_needed = true;
  1160. }
  1161. }
  1162. code += node_code;
  1163. } else {
  1164. code += node_code;
  1165. }
  1166. } else if (multiview_uv_needed && !texture_func_no_uv && i == 2) {
  1167. // UV coordinate after using color, depth or normal roughness texture.
  1168. node_code = "multiview_uv(" + node_code + ".xy)";
  1169. code += node_code;
  1170. } else {
  1171. code += node_code;
  1172. }
  1173. }
  1174. code += ")";
  1175. if (is_screen_texture && !texture_func_returns_data && actions.apply_luminance_multiplier) {
  1176. code = "(" + code + " * vec4(vec3(sc_luminance_multiplier), 1.0))";
  1177. }
  1178. } break;
  1179. case SL::OP_INDEX: {
  1180. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1181. code += "[";
  1182. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1183. code += "]";
  1184. } break;
  1185. case SL::OP_SELECT_IF: {
  1186. code += "(";
  1187. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1188. code += "?";
  1189. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1190. code += ":";
  1191. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1192. code += ")";
  1193. } break;
  1194. case SL::OP_EMPTY: {
  1195. // Semicolon (or empty statement) - ignored.
  1196. } break;
  1197. default: {
  1198. if (p_use_scope) {
  1199. code += "(";
  1200. }
  1201. 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);
  1202. if (p_use_scope) {
  1203. code += ")";
  1204. }
  1205. break;
  1206. }
  1207. }
  1208. } break;
  1209. case SL::Node::NODE_TYPE_CONTROL_FLOW: {
  1210. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1211. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1212. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1213. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1214. if (cfnode->blocks.size() == 2) {
  1215. code += _mktab(p_level) + "else\n";
  1216. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1217. }
  1218. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1219. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1220. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1221. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1222. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1223. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1224. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1225. code += _mktab(p_level) + "default:\n";
  1226. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1227. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1228. code += _mktab(p_level) + "do";
  1229. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1230. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1231. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1232. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1233. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1234. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1235. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1236. String middle = _dump_node_code(cfnode->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1237. String right = _dump_node_code(cfnode->blocks[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1238. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1239. code += _dump_node_code(cfnode->blocks[3], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1240. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1241. if (cfnode->expressions.size()) {
  1242. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1243. } else {
  1244. code = "return;";
  1245. }
  1246. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1247. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1248. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1249. used_flag_pointers.insert("DISCARD");
  1250. }
  1251. code = "discard;";
  1252. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1253. code = "continue;";
  1254. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1255. code = "break;";
  1256. }
  1257. } break;
  1258. case SL::Node::NODE_TYPE_MEMBER: {
  1259. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1260. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1261. if (mnode->index_expression != nullptr) {
  1262. code += "[";
  1263. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1264. code += "]";
  1265. } else if (mnode->assign_expression != nullptr) {
  1266. code += "=";
  1267. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1268. } else if (mnode->call_expression != nullptr) {
  1269. code += ".";
  1270. code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  1271. }
  1272. } break;
  1273. }
  1274. return code;
  1275. }
  1276. ShaderLanguage::DataType ShaderCompiler::_get_global_shader_uniform_type(const StringName &p_name) {
  1277. RS::GlobalShaderParameterType gvt = RSG::material_storage->global_shader_parameter_get_type(p_name);
  1278. return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt);
  1279. }
  1280. Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1281. SL::ShaderCompileInfo info;
  1282. info.functions = ShaderTypes::get_singleton()->get_functions(p_mode);
  1283. info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode);
  1284. info.shader_types = ShaderTypes::get_singleton()->get_types();
  1285. info.global_shader_uniform_type_func = _get_global_shader_uniform_type;
  1286. Error err = parser.compile(p_code, info);
  1287. if (err != OK) {
  1288. Vector<ShaderLanguage::FilePosition> include_positions = parser.get_include_positions();
  1289. String current;
  1290. HashMap<String, Vector<String>> includes;
  1291. includes[""] = Vector<String>();
  1292. Vector<String> include_stack;
  1293. Vector<String> shader_lines = p_code.split("\n");
  1294. // Reconstruct the files.
  1295. for (int i = 0; i < shader_lines.size(); i++) {
  1296. String l = shader_lines[i];
  1297. if (l.begins_with("@@>")) {
  1298. String inc_path = l.replace_first("@@>", "");
  1299. l = "#include \"" + inc_path + "\"";
  1300. includes[current].append("#include \"" + inc_path + "\""); // Restore the include directive
  1301. include_stack.push_back(current);
  1302. current = inc_path;
  1303. includes[inc_path] = Vector<String>();
  1304. } else if (l.begins_with("@@<")) {
  1305. if (include_stack.size()) {
  1306. current = include_stack[include_stack.size() - 1];
  1307. include_stack.resize(include_stack.size() - 1);
  1308. }
  1309. } else {
  1310. includes[current].push_back(l);
  1311. }
  1312. }
  1313. // Print the files.
  1314. for (const KeyValue<String, Vector<String>> &E : includes) {
  1315. if (E.key.is_empty()) {
  1316. if (p_path == "") {
  1317. print_line("--Main Shader--");
  1318. } else {
  1319. print_line("--" + p_path + "--");
  1320. }
  1321. } else {
  1322. print_line("--" + E.key + "--");
  1323. }
  1324. int err_line = -1;
  1325. for (int i = 0; i < include_positions.size(); i++) {
  1326. if (include_positions[i].file == E.key) {
  1327. err_line = include_positions[i].line;
  1328. }
  1329. }
  1330. const Vector<String> &V = E.value;
  1331. for (int i = 0; i < V.size(); i++) {
  1332. if (i == err_line - 1) {
  1333. // Mark the error line to be visible without having to look at
  1334. // the trace at the end.
  1335. print_line(vformat("E%4d-> %s", i + 1, V[i]));
  1336. } else {
  1337. print_line(vformat("%5d | %s", i + 1, V[i]));
  1338. }
  1339. }
  1340. }
  1341. String file;
  1342. int line;
  1343. if (include_positions.size() > 1) {
  1344. file = include_positions[include_positions.size() - 1].file;
  1345. line = include_positions[include_positions.size() - 1].line;
  1346. } else {
  1347. file = p_path;
  1348. line = parser.get_error_line();
  1349. }
  1350. _err_print_error(nullptr, file.utf8().get_data(), line, parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER);
  1351. return err;
  1352. }
  1353. r_gen_code.defines.clear();
  1354. r_gen_code.code.clear();
  1355. for (int i = 0; i < STAGE_MAX; i++) {
  1356. r_gen_code.stage_globals[i] = String();
  1357. }
  1358. r_gen_code.uses_fragment_time = false;
  1359. r_gen_code.uses_vertex_time = false;
  1360. r_gen_code.uses_global_textures = false;
  1361. r_gen_code.uses_screen_texture_mipmaps = false;
  1362. r_gen_code.uses_screen_texture = false;
  1363. r_gen_code.uses_depth_texture = false;
  1364. r_gen_code.uses_normal_roughness_texture = false;
  1365. used_name_defines.clear();
  1366. used_rmode_defines.clear();
  1367. used_flag_pointers.clear();
  1368. fragment_varyings.clear();
  1369. shader = parser.get_shader();
  1370. function = nullptr;
  1371. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1372. return OK;
  1373. }
  1374. void ShaderCompiler::initialize(DefaultIdentifierActions p_actions) {
  1375. actions = p_actions;
  1376. time_name = "TIME";
  1377. List<String> func_list;
  1378. ShaderLanguage::get_builtin_funcs(&func_list);
  1379. for (const String &E : func_list) {
  1380. internal_functions.insert(E);
  1381. }
  1382. texture_functions.insert("texture");
  1383. texture_functions.insert("textureProj");
  1384. texture_functions.insert("textureLod");
  1385. texture_functions.insert("textureProjLod");
  1386. texture_functions.insert("textureGrad");
  1387. texture_functions.insert("textureProjGrad");
  1388. texture_functions.insert("textureGather");
  1389. texture_functions.insert("textureSize");
  1390. texture_functions.insert("textureQueryLod");
  1391. texture_functions.insert("textureQueryLevels");
  1392. texture_functions.insert("texelFetch");
  1393. }
  1394. ShaderCompiler::ShaderCompiler() {
  1395. }