shader_compiler.cpp 56 KB

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