shader_compiler.cpp 55 KB

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