shader_compiler.cpp 55 KB

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