shader_compiler.cpp 56 KB

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