shader_compiler.cpp 53 KB

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