shader_compiler.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /*************************************************************************/
  2. /* shader_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/shader_types.h"
  34. #include "servers/rendering_server.h"
  35. #define SL ShaderLanguage
  36. static String _mktab(int p_level) {
  37. String tb;
  38. for (int i = 0; i < p_level; i++) {
  39. tb += "\t";
  40. }
  41. return tb;
  42. }
  43. static String _typestr(SL::DataType p_type) {
  44. String type = ShaderLanguage::get_datatype_name(p_type);
  45. if (!RS::get_singleton()->is_low_end() && ShaderLanguage::is_sampler_type(p_type)) {
  46. type = type.replace("sampler", "texture"); //we use textures instead of samplers in Vulkan GLSL
  47. }
  48. return type;
  49. }
  50. static int _get_datatype_alignment(SL::DataType p_type) {
  51. switch (p_type) {
  52. case SL::TYPE_VOID:
  53. return 0;
  54. case SL::TYPE_BOOL:
  55. return 4;
  56. case SL::TYPE_BVEC2:
  57. return 8;
  58. case SL::TYPE_BVEC3:
  59. return 16;
  60. case SL::TYPE_BVEC4:
  61. return 16;
  62. case SL::TYPE_INT:
  63. return 4;
  64. case SL::TYPE_IVEC2:
  65. return 8;
  66. case SL::TYPE_IVEC3:
  67. return 16;
  68. case SL::TYPE_IVEC4:
  69. return 16;
  70. case SL::TYPE_UINT:
  71. return 4;
  72. case SL::TYPE_UVEC2:
  73. return 8;
  74. case SL::TYPE_UVEC3:
  75. return 16;
  76. case SL::TYPE_UVEC4:
  77. return 16;
  78. case SL::TYPE_FLOAT:
  79. return 4;
  80. case SL::TYPE_VEC2:
  81. return 8;
  82. case SL::TYPE_VEC3:
  83. return 16;
  84. case SL::TYPE_VEC4:
  85. return 16;
  86. case SL::TYPE_MAT2:
  87. return 16;
  88. case SL::TYPE_MAT3:
  89. return 16;
  90. case SL::TYPE_MAT4:
  91. return 16;
  92. case SL::TYPE_SAMPLER2D:
  93. return 16;
  94. case SL::TYPE_ISAMPLER2D:
  95. return 16;
  96. case SL::TYPE_USAMPLER2D:
  97. return 16;
  98. case SL::TYPE_SAMPLER2DARRAY:
  99. return 16;
  100. case SL::TYPE_ISAMPLER2DARRAY:
  101. return 16;
  102. case SL::TYPE_USAMPLER2DARRAY:
  103. return 16;
  104. case SL::TYPE_SAMPLER3D:
  105. return 16;
  106. case SL::TYPE_ISAMPLER3D:
  107. return 16;
  108. case SL::TYPE_USAMPLER3D:
  109. return 16;
  110. case SL::TYPE_SAMPLERCUBE:
  111. return 16;
  112. case SL::TYPE_SAMPLERCUBEARRAY:
  113. return 16;
  114. case SL::TYPE_STRUCT:
  115. return 0;
  116. case SL::TYPE_MAX: {
  117. ERR_FAIL_V(0);
  118. }
  119. }
  120. ERR_FAIL_V(0);
  121. }
  122. static String _interpstr(SL::DataInterpolation p_interp) {
  123. switch (p_interp) {
  124. case SL::INTERPOLATION_FLAT:
  125. return "flat ";
  126. case SL::INTERPOLATION_SMOOTH:
  127. return "";
  128. }
  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 = rtoss(p_float);
  170. if (!num.contains(".") && !num.contains("e")) {
  171. num += ".0";
  172. }
  173. return num;
  174. }
  175. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &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. return actions.sampler_array_name + "[" + itos(p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)) + "]";
  264. }
  265. 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, RBSet<StringName> &added) {
  266. int fidx = -1;
  267. for (int i = 0; i < p_node->functions.size(); i++) {
  268. if (p_node->functions[i].name == p_for_func) {
  269. fidx = i;
  270. break;
  271. }
  272. }
  273. ERR_FAIL_COND(fidx == -1);
  274. Vector<StringName> uses_functions;
  275. for (const StringName &E : p_node->functions[fidx].uses_function) {
  276. uses_functions.push_back(E);
  277. }
  278. uses_functions.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  279. for (int k = 0; k < uses_functions.size(); k++) {
  280. if (added.has(uses_functions[k])) {
  281. continue; //was added already
  282. }
  283. _dump_function_deps(p_node, uses_functions[k], p_func_code, r_to_add, added);
  284. SL::FunctionNode *fnode = nullptr;
  285. for (int i = 0; i < p_node->functions.size(); i++) {
  286. if (p_node->functions[i].name == uses_functions[k]) {
  287. fnode = p_node->functions[i].function;
  288. break;
  289. }
  290. }
  291. ERR_FAIL_COND(!fnode);
  292. r_to_add += "\n";
  293. String header;
  294. if (fnode->return_type == SL::TYPE_STRUCT) {
  295. header = _mkid(fnode->return_struct_name);
  296. } else {
  297. header = _typestr(fnode->return_type);
  298. }
  299. if (fnode->return_array_size > 0) {
  300. header += "[";
  301. header += itos(fnode->return_array_size);
  302. header += "]";
  303. }
  304. header += " ";
  305. header += _mkid(fnode->name);
  306. header += "(";
  307. for (int i = 0; i < fnode->arguments.size(); i++) {
  308. if (i > 0) {
  309. header += ", ";
  310. }
  311. header += _constr(fnode->arguments[i].is_const);
  312. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  313. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  314. } else {
  315. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  316. }
  317. if (fnode->arguments[i].array_size > 0) {
  318. header += "[";
  319. header += itos(fnode->arguments[i].array_size);
  320. header += "]";
  321. }
  322. }
  323. header += ")\n";
  324. r_to_add += header;
  325. r_to_add += p_func_code[uses_functions[k]];
  326. added.insert(uses_functions[k]);
  327. }
  328. }
  329. static String _get_global_variable_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
  330. switch (p_type) {
  331. case ShaderLanguage::TYPE_BOOL: {
  332. return "(" + p_buffer + "[" + p_index + "].x != 0.0)";
  333. }
  334. case ShaderLanguage::TYPE_BVEC2: {
  335. return "(notEqual(" + p_buffer + "[" + p_index + "].xy, vec2(0.0)))";
  336. }
  337. case ShaderLanguage::TYPE_BVEC3: {
  338. return "(notEqual(" + p_buffer + "[" + p_index + "].xyz, vec3(0.0)))";
  339. }
  340. case ShaderLanguage::TYPE_BVEC4: {
  341. return "(notEqual(" + p_buffer + "[" + p_index + "].xyzw, vec4(0.0)))";
  342. }
  343. case ShaderLanguage::TYPE_INT: {
  344. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
  345. }
  346. case ShaderLanguage::TYPE_IVEC2: {
  347. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
  348. }
  349. case ShaderLanguage::TYPE_IVEC3: {
  350. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
  351. }
  352. case ShaderLanguage::TYPE_IVEC4: {
  353. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
  354. }
  355. case ShaderLanguage::TYPE_UINT: {
  356. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)";
  357. }
  358. case ShaderLanguage::TYPE_UVEC2: {
  359. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)";
  360. }
  361. case ShaderLanguage::TYPE_UVEC3: {
  362. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)";
  363. }
  364. case ShaderLanguage::TYPE_UVEC4: {
  365. return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)";
  366. }
  367. case ShaderLanguage::TYPE_FLOAT: {
  368. return "(" + p_buffer + "[" + p_index + "].x)";
  369. }
  370. case ShaderLanguage::TYPE_VEC2: {
  371. return "(" + p_buffer + "[" + p_index + "].xy)";
  372. }
  373. case ShaderLanguage::TYPE_VEC3: {
  374. return "(" + p_buffer + "[" + p_index + "].xyz)";
  375. }
  376. case ShaderLanguage::TYPE_VEC4: {
  377. return "(" + p_buffer + "[" + p_index + "].xyzw)";
  378. }
  379. case ShaderLanguage::TYPE_MAT2: {
  380. return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)";
  381. }
  382. case ShaderLanguage::TYPE_MAT3: {
  383. return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)";
  384. }
  385. case ShaderLanguage::TYPE_MAT4: {
  386. 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)";
  387. }
  388. default: {
  389. ERR_FAIL_V("void");
  390. }
  391. }
  392. }
  393. 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) {
  394. String code;
  395. switch (p_node->type) {
  396. case SL::Node::TYPE_SHADER: {
  397. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  398. for (int i = 0; i < pnode->render_modes.size(); i++) {
  399. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  400. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
  401. used_rmode_defines.insert(pnode->render_modes[i]);
  402. }
  403. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  404. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  405. }
  406. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  407. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  408. *p.first = p.second;
  409. }
  410. }
  411. // structs
  412. for (int i = 0; i < pnode->vstructs.size(); i++) {
  413. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  414. String struct_code;
  415. struct_code += "struct ";
  416. struct_code += _mkid(pnode->vstructs[i].name);
  417. struct_code += " ";
  418. struct_code += "{\n";
  419. for (int j = 0; j < st->members.size(); j++) {
  420. SL::MemberNode *m = st->members[j];
  421. if (m->datatype == SL::TYPE_STRUCT) {
  422. struct_code += _mkid(m->struct_name);
  423. } else {
  424. struct_code += _prestr(m->precision);
  425. struct_code += _typestr(m->datatype);
  426. }
  427. struct_code += " ";
  428. struct_code += m->name;
  429. if (m->array_size > 0) {
  430. struct_code += "[";
  431. struct_code += itos(m->array_size);
  432. struct_code += "]";
  433. }
  434. struct_code += ";\n";
  435. }
  436. struct_code += "}";
  437. struct_code += ";\n";
  438. for (int j = 0; j < STAGE_MAX; j++) {
  439. r_gen_code.stage_globals[j] += struct_code;
  440. }
  441. }
  442. int max_texture_uniforms = 0;
  443. int max_uniforms = 0;
  444. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  445. if (SL::is_sampler_type(E.value.type)) {
  446. max_texture_uniforms++;
  447. } else {
  448. if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  449. continue; // Instances are indexed directly, don't need index uniforms.
  450. }
  451. max_uniforms++;
  452. }
  453. }
  454. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  455. Vector<int> uniform_sizes;
  456. Vector<int> uniform_alignments;
  457. Vector<StringName> uniform_defines;
  458. uniform_sizes.resize(max_uniforms);
  459. uniform_alignments.resize(max_uniforms);
  460. uniform_defines.resize(max_uniforms);
  461. bool uses_uniforms = false;
  462. Vector<StringName> uniform_names;
  463. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  464. uniform_names.push_back(E.key);
  465. }
  466. uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  467. for (int k = 0; k < uniform_names.size(); k++) {
  468. StringName uniform_name = uniform_names[k];
  469. const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name];
  470. String ucode;
  471. if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  472. //insert, but don't generate any code.
  473. p_actions.uniforms->insert(uniform_name, uniform);
  474. continue; // Instances are indexed directly, don't need index uniforms.
  475. }
  476. if (SL::is_sampler_type(uniform.type)) {
  477. // Texture layouts are different for OpenGL GLSL and Vulkan GLSL
  478. if (!RS::get_singleton()->is_low_end()) {
  479. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") ";
  480. }
  481. ucode += "uniform ";
  482. }
  483. bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  484. if (is_buffer_global) {
  485. //this is an integer to index the global table
  486. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  487. } else {
  488. ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type));
  489. ucode += _typestr(uniform.type);
  490. }
  491. ucode += " " + _mkid(uniform_name);
  492. if (uniform.array_size > 0) {
  493. ucode += "[";
  494. ucode += itos(uniform.array_size);
  495. ucode += "]";
  496. }
  497. ucode += ";\n";
  498. if (SL::is_sampler_type(uniform.type)) {
  499. for (int j = 0; j < STAGE_MAX; j++) {
  500. r_gen_code.stage_globals[j] += ucode;
  501. }
  502. GeneratedCode::Texture texture;
  503. texture.name = uniform_name;
  504. texture.hint = uniform.hint;
  505. texture.type = uniform.type;
  506. texture.filter = uniform.filter;
  507. texture.repeat = uniform.repeat;
  508. texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  509. texture.array_size = uniform.array_size;
  510. if (texture.global) {
  511. r_gen_code.uses_global_textures = true;
  512. }
  513. r_gen_code.texture_uniforms.write[uniform.texture_order] = texture;
  514. } else {
  515. if (!uses_uniforms) {
  516. uses_uniforms = true;
  517. }
  518. uniform_defines.write[uniform.order] = ucode;
  519. if (is_buffer_global) {
  520. //globals are indices into the global table
  521. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(ShaderLanguage::TYPE_UINT);
  522. uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  523. } else {
  524. // The following code enforces a 16-byte alignment of uniform arrays.
  525. if (uniform.array_size > 0) {
  526. int size = ShaderLanguage::get_datatype_size(uniform.type) * uniform.array_size;
  527. int m = (16 * uniform.array_size);
  528. if ((size % m) != 0) {
  529. size += m - (size % m);
  530. }
  531. uniform_sizes.write[uniform.order] = size;
  532. uniform_alignments.write[uniform.order] = 16;
  533. } else {
  534. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(uniform.type);
  535. uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type);
  536. }
  537. }
  538. }
  539. p_actions.uniforms->insert(uniform_name, uniform);
  540. }
  541. for (int i = 0; i < max_uniforms; i++) {
  542. r_gen_code.uniforms += uniform_defines[i];
  543. }
  544. // add up
  545. int offset = 0;
  546. for (int i = 0; i < uniform_sizes.size(); i++) {
  547. int align = offset % uniform_alignments[i];
  548. if (align != 0) {
  549. offset += uniform_alignments[i] - align;
  550. }
  551. r_gen_code.uniform_offsets.push_back(offset);
  552. offset += uniform_sizes[i];
  553. }
  554. r_gen_code.uniform_total_size = offset;
  555. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  556. r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
  557. }
  558. uint32_t index = p_default_actions.base_varying_index;
  559. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  560. Vector<StringName> varying_names;
  561. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  562. varying_names.push_back(E.key);
  563. }
  564. varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  565. for (int k = 0; k < varying_names.size(); k++) {
  566. StringName varying_name = varying_names[k];
  567. const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name];
  568. if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  569. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying));
  570. fragment_varyings.insert(varying_name);
  571. continue;
  572. }
  573. String vcode;
  574. String interp_mode = _interpstr(varying.interpolation);
  575. vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type));
  576. vcode += _typestr(varying.type);
  577. vcode += " " + _mkid(varying_name);
  578. uint32_t inc = 1U;
  579. if (varying.array_size > 0) {
  580. inc = (uint32_t)varying.array_size;
  581. vcode += "[";
  582. vcode += itos(varying.array_size);
  583. vcode += "]";
  584. }
  585. switch (varying.type) {
  586. case SL::TYPE_MAT2:
  587. inc *= 2U;
  588. break;
  589. case SL::TYPE_MAT3:
  590. inc *= 3U;
  591. break;
  592. case SL::TYPE_MAT4:
  593. inc *= 4U;
  594. break;
  595. default:
  596. break;
  597. }
  598. vcode += ";\n";
  599. // GLSL ES 3.0 does not allow layout qualifiers for varyings
  600. if (!RS::get_singleton()->is_low_end()) {
  601. r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") ";
  602. r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") ";
  603. }
  604. r_gen_code.stage_globals[STAGE_VERTEX] += interp_mode + "out " + vcode;
  605. r_gen_code.stage_globals[STAGE_FRAGMENT] += interp_mode + "in " + vcode;
  606. index += inc;
  607. }
  608. if (var_frag_to_light.size() > 0) {
  609. String gcode = "\n\nstruct {\n";
  610. for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) {
  611. gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first);
  612. if (E.second.array_size > 0) {
  613. gcode += "[";
  614. gcode += itos(E.second.array_size);
  615. gcode += "]";
  616. }
  617. gcode += ";\n";
  618. }
  619. gcode += "} frag_to_light;\n";
  620. r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode;
  621. }
  622. for (int i = 0; i < pnode->vconstants.size(); i++) {
  623. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  624. String gcode;
  625. gcode += _constr(true);
  626. gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
  627. if (cnode.type == SL::TYPE_STRUCT) {
  628. gcode += _mkid(cnode.type_str);
  629. } else {
  630. gcode += _typestr(cnode.type);
  631. }
  632. gcode += " " + _mkid(String(cnode.name));
  633. if (cnode.array_size > 0) {
  634. gcode += "[";
  635. gcode += itos(cnode.array_size);
  636. gcode += "]";
  637. }
  638. gcode += "=";
  639. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  640. gcode += ";\n";
  641. for (int j = 0; j < STAGE_MAX; j++) {
  642. r_gen_code.stage_globals[j] += gcode;
  643. }
  644. }
  645. HashMap<StringName, String> function_code;
  646. //code for functions
  647. for (int i = 0; i < pnode->functions.size(); i++) {
  648. SL::FunctionNode *fnode = pnode->functions[i].function;
  649. function = fnode;
  650. current_func_name = fnode->name;
  651. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  652. function = nullptr;
  653. }
  654. //place functions in actual code
  655. RBSet<StringName> added_funcs_per_stage[STAGE_MAX];
  656. for (int i = 0; i < pnode->functions.size(); i++) {
  657. SL::FunctionNode *fnode = pnode->functions[i].function;
  658. function = fnode;
  659. current_func_name = fnode->name;
  660. if (p_actions.entry_point_stages.has(fnode->name)) {
  661. Stage stage = p_actions.entry_point_stages[fnode->name];
  662. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]);
  663. r_gen_code.code[fnode->name] = function_code[fnode->name];
  664. }
  665. function = nullptr;
  666. }
  667. //code+=dump_node_code(pnode->body,p_level);
  668. } break;
  669. case SL::Node::TYPE_STRUCT: {
  670. } break;
  671. case SL::Node::TYPE_FUNCTION: {
  672. } break;
  673. case SL::Node::TYPE_BLOCK: {
  674. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  675. //variables
  676. if (!bnode->single_statement) {
  677. code += _mktab(p_level - 1) + "{\n";
  678. }
  679. for (int i = 0; i < bnode->statements.size(); i++) {
  680. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  681. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  682. code += scode; //use directly
  683. if (bnode->use_comma_between_statements && i + 1 < bnode->statements.size()) {
  684. code += ",";
  685. }
  686. } else {
  687. code += _mktab(p_level) + scode + ";\n";
  688. }
  689. }
  690. if (!bnode->single_statement) {
  691. code += _mktab(p_level - 1) + "}\n";
  692. }
  693. } break;
  694. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  695. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  696. String declaration;
  697. declaration += _constr(vdnode->is_const);
  698. if (vdnode->datatype == SL::TYPE_STRUCT) {
  699. declaration += _mkid(vdnode->struct_name);
  700. } else {
  701. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  702. }
  703. declaration += " ";
  704. for (int i = 0; i < vdnode->declarations.size(); i++) {
  705. bool is_array = vdnode->declarations[i].size > 0;
  706. if (i > 0) {
  707. declaration += ",";
  708. }
  709. declaration += _mkid(vdnode->declarations[i].name);
  710. if (is_array) {
  711. declaration += "[";
  712. if (vdnode->declarations[i].size_expression != nullptr) {
  713. declaration += _dump_node_code(vdnode->declarations[i].size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  714. } else {
  715. declaration += itos(vdnode->declarations[i].size);
  716. }
  717. declaration += "]";
  718. }
  719. if (!is_array || vdnode->declarations[i].single_expression) {
  720. if (!vdnode->declarations[i].initializer.is_empty()) {
  721. declaration += "=";
  722. declaration += _dump_node_code(vdnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  723. }
  724. } else {
  725. int size = vdnode->declarations[i].initializer.size();
  726. if (size > 0) {
  727. declaration += "=";
  728. if (vdnode->datatype == SL::TYPE_STRUCT) {
  729. declaration += _mkid(vdnode->struct_name);
  730. } else {
  731. declaration += _typestr(vdnode->datatype);
  732. }
  733. declaration += "[";
  734. declaration += itos(size);
  735. declaration += "]";
  736. declaration += "(";
  737. for (int j = 0; j < size; j++) {
  738. if (j > 0) {
  739. declaration += ",";
  740. }
  741. declaration += _dump_node_code(vdnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  742. }
  743. declaration += ")";
  744. }
  745. }
  746. }
  747. code += declaration;
  748. } break;
  749. case SL::Node::TYPE_VARIABLE: {
  750. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  751. bool use_fragment_varying = false;
  752. if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  753. if (p_assigning) {
  754. if (shader->varyings.has(vnode->name)) {
  755. use_fragment_varying = true;
  756. }
  757. } else {
  758. if (fragment_varyings.has(vnode->name)) {
  759. use_fragment_varying = true;
  760. }
  761. }
  762. }
  763. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  764. *p_actions.write_flag_pointers[vnode->name] = true;
  765. }
  766. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  767. String define = p_default_actions.usage_defines[vnode->name];
  768. if (define.begins_with("@")) {
  769. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  770. }
  771. r_gen_code.defines.push_back(define);
  772. used_name_defines.insert(vnode->name);
  773. }
  774. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  775. *p_actions.usage_flag_pointers[vnode->name] = true;
  776. used_flag_pointers.insert(vnode->name);
  777. }
  778. if (p_default_actions.renames.has(vnode->name)) {
  779. code = p_default_actions.renames[vnode->name];
  780. } else {
  781. if (shader->uniforms.has(vnode->name)) {
  782. //its a uniform!
  783. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  784. if (u.texture_order >= 0) {
  785. code = _mkid(vnode->name); //texture, use as is
  786. } else {
  787. //a scalar or vector
  788. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  789. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  790. //global variable, this means the code points to an index to the global table
  791. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  792. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  793. //instance variable, index it as such
  794. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  795. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  796. } else {
  797. //regular uniform, index from UBO
  798. code = actions.base_uniform_string + _mkid(vnode->name);
  799. }
  800. }
  801. } else {
  802. if (use_fragment_varying) {
  803. code = "frag_to_light.";
  804. }
  805. code += _mkid(vnode->name); //its something else (local var most likely) use as is
  806. }
  807. }
  808. if (vnode->name == time_name) {
  809. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  810. r_gen_code.uses_vertex_time = true;
  811. }
  812. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  813. r_gen_code.uses_fragment_time = true;
  814. }
  815. }
  816. } break;
  817. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  818. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  819. int sz = acnode->initializer.size();
  820. if (acnode->datatype == SL::TYPE_STRUCT) {
  821. code += _mkid(acnode->struct_name);
  822. } else {
  823. code += _typestr(acnode->datatype);
  824. }
  825. code += "[";
  826. code += itos(acnode->initializer.size());
  827. code += "]";
  828. code += "(";
  829. for (int i = 0; i < sz; i++) {
  830. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  831. if (i != sz - 1) {
  832. code += ", ";
  833. }
  834. }
  835. code += ")";
  836. } break;
  837. case SL::Node::TYPE_ARRAY: {
  838. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  839. bool use_fragment_varying = false;
  840. if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  841. if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) {
  842. use_fragment_varying = true;
  843. } else {
  844. if (p_assigning) {
  845. if (shader->varyings.has(anode->name)) {
  846. use_fragment_varying = true;
  847. }
  848. } else {
  849. if (fragment_varyings.has(anode->name)) {
  850. use_fragment_varying = true;
  851. }
  852. }
  853. }
  854. }
  855. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  856. *p_actions.write_flag_pointers[anode->name] = true;
  857. }
  858. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  859. String define = p_default_actions.usage_defines[anode->name];
  860. if (define.begins_with("@")) {
  861. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  862. }
  863. r_gen_code.defines.push_back(define);
  864. used_name_defines.insert(anode->name);
  865. }
  866. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  867. *p_actions.usage_flag_pointers[anode->name] = true;
  868. used_flag_pointers.insert(anode->name);
  869. }
  870. if (p_default_actions.renames.has(anode->name)) {
  871. code = p_default_actions.renames[anode->name];
  872. } else {
  873. if (shader->uniforms.has(anode->name)) {
  874. //its a uniform!
  875. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name];
  876. if (u.texture_order >= 0) {
  877. code = _mkid(anode->name); //texture, use as is
  878. } else {
  879. //a scalar or vector
  880. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  881. code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is
  882. //global variable, this means the code points to an index to the global table
  883. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  884. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  885. //instance variable, index it as such
  886. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  887. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  888. } else {
  889. //regular uniform, index from UBO
  890. code = actions.base_uniform_string + _mkid(anode->name);
  891. }
  892. }
  893. } else {
  894. if (use_fragment_varying) {
  895. code = "frag_to_light.";
  896. }
  897. code += _mkid(anode->name);
  898. }
  899. }
  900. if (anode->call_expression != nullptr) {
  901. code += ".";
  902. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  903. } else if (anode->index_expression != nullptr) {
  904. code += "[";
  905. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  906. code += "]";
  907. } else if (anode->assign_expression != nullptr) {
  908. code += "=";
  909. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  910. }
  911. if (anode->name == time_name) {
  912. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  913. r_gen_code.uses_vertex_time = true;
  914. }
  915. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  916. r_gen_code.uses_fragment_time = true;
  917. }
  918. }
  919. } break;
  920. case SL::Node::TYPE_CONSTANT: {
  921. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  922. if (cnode->array_size == 0) {
  923. return get_constant_text(cnode->datatype, cnode->values);
  924. } else {
  925. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  926. code += _mkid(cnode->struct_name);
  927. } else {
  928. code += _typestr(cnode->datatype);
  929. }
  930. code += "[";
  931. code += itos(cnode->array_size);
  932. code += "]";
  933. code += "(";
  934. for (int i = 0; i < cnode->array_size; i++) {
  935. if (i > 0) {
  936. code += ",";
  937. } else {
  938. code += "";
  939. }
  940. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  941. }
  942. code += ")";
  943. }
  944. } break;
  945. case SL::Node::TYPE_OPERATOR: {
  946. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  947. switch (onode->op) {
  948. case SL::OP_ASSIGN:
  949. case SL::OP_ASSIGN_ADD:
  950. case SL::OP_ASSIGN_SUB:
  951. case SL::OP_ASSIGN_MUL:
  952. case SL::OP_ASSIGN_DIV:
  953. case SL::OP_ASSIGN_SHIFT_LEFT:
  954. case SL::OP_ASSIGN_SHIFT_RIGHT:
  955. case SL::OP_ASSIGN_MOD:
  956. case SL::OP_ASSIGN_BIT_AND:
  957. case SL::OP_ASSIGN_BIT_OR:
  958. case SL::OP_ASSIGN_BIT_XOR:
  959. 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);
  960. break;
  961. case SL::OP_BIT_INVERT:
  962. case SL::OP_NEGATE:
  963. case SL::OP_NOT:
  964. case SL::OP_DECREMENT:
  965. case SL::OP_INCREMENT:
  966. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  967. break;
  968. case SL::OP_POST_DECREMENT:
  969. case SL::OP_POST_INCREMENT:
  970. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  971. break;
  972. case SL::OP_CALL:
  973. case SL::OP_STRUCT:
  974. case SL::OP_CONSTRUCT: {
  975. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  976. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  977. bool is_texture_func = false;
  978. bool is_screen_texture = false;
  979. if (onode->op == SL::OP_STRUCT) {
  980. code += _mkid(vnode->name);
  981. } else if (onode->op == SL::OP_CONSTRUCT) {
  982. code += String(vnode->name);
  983. } else {
  984. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  985. *p_actions.usage_flag_pointers[vnode->name] = true;
  986. used_flag_pointers.insert(vnode->name);
  987. }
  988. if (internal_functions.has(vnode->name)) {
  989. code += vnode->name;
  990. is_texture_func = texture_functions.has(vnode->name);
  991. } else if (p_default_actions.renames.has(vnode->name)) {
  992. code += p_default_actions.renames[vnode->name];
  993. } else {
  994. code += _mkid(vnode->name);
  995. }
  996. }
  997. code += "(";
  998. for (int i = 1; i < onode->arguments.size(); i++) {
  999. if (i > 1) {
  1000. code += ", ";
  1001. }
  1002. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1003. if (!RS::get_singleton()->is_low_end() && is_texture_func && i == 1) {
  1004. //need to map from texture to sampler in order to sample when using Vulkan GLSL
  1005. StringName texture_uniform;
  1006. bool correct_texture_uniform = false;
  1007. switch (onode->arguments[i]->type) {
  1008. case SL::Node::TYPE_VARIABLE: {
  1009. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  1010. texture_uniform = varnode->name;
  1011. correct_texture_uniform = true;
  1012. } break;
  1013. case SL::Node::TYPE_ARRAY: {
  1014. const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]);
  1015. texture_uniform = anode->name;
  1016. correct_texture_uniform = true;
  1017. } break;
  1018. default:
  1019. break;
  1020. }
  1021. if (correct_texture_uniform) {
  1022. is_screen_texture = (texture_uniform == "SCREEN_TEXTURE");
  1023. String sampler_name;
  1024. if (actions.custom_samplers.has(texture_uniform)) {
  1025. sampler_name = actions.custom_samplers[texture_uniform];
  1026. } else {
  1027. if (shader->uniforms.has(texture_uniform)) {
  1028. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  1029. } else {
  1030. bool found = false;
  1031. for (int j = 0; j < function->arguments.size(); j++) {
  1032. if (function->arguments[j].name == texture_uniform) {
  1033. if (function->arguments[j].tex_builtin_check) {
  1034. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  1035. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  1036. found = true;
  1037. break;
  1038. }
  1039. if (function->arguments[j].tex_argument_check) {
  1040. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1041. found = true;
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. if (!found) {
  1047. //function was most likely unused, so use anything (compiler will remove it anyway)
  1048. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1049. }
  1050. }
  1051. }
  1052. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  1053. } else {
  1054. code += node_code;
  1055. }
  1056. } else {
  1057. code += node_code;
  1058. }
  1059. }
  1060. code += ")";
  1061. if (is_screen_texture && actions.apply_luminance_multiplier) {
  1062. code = "(" + code + " * vec4(vec3(sc_luminance_multiplier), 1.0))";
  1063. }
  1064. } break;
  1065. case SL::OP_INDEX: {
  1066. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1067. code += "[";
  1068. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1069. code += "]";
  1070. } break;
  1071. case SL::OP_SELECT_IF: {
  1072. code += "(";
  1073. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1074. code += "?";
  1075. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1076. code += ":";
  1077. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1078. code += ")";
  1079. } break;
  1080. case SL::OP_EMPTY: {
  1081. // Semicolon (or empty statement) - ignored.
  1082. } break;
  1083. default: {
  1084. if (p_use_scope) {
  1085. code += "(";
  1086. }
  1087. 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);
  1088. if (p_use_scope) {
  1089. code += ")";
  1090. }
  1091. break;
  1092. }
  1093. }
  1094. } break;
  1095. case SL::Node::TYPE_CONTROL_FLOW: {
  1096. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1097. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1098. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1099. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1100. if (cfnode->blocks.size() == 2) {
  1101. code += _mktab(p_level) + "else\n";
  1102. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1103. }
  1104. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1105. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1106. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1107. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1108. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1109. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1110. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1111. code += _mktab(p_level) + "default:\n";
  1112. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1113. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1114. code += _mktab(p_level) + "do";
  1115. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1116. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1117. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1118. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1119. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1120. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1121. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1122. String middle = _dump_node_code(cfnode->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1123. String right = _dump_node_code(cfnode->blocks[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1124. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1125. code += _dump_node_code(cfnode->blocks[3], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1126. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1127. if (cfnode->expressions.size()) {
  1128. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1129. } else {
  1130. code = "return;";
  1131. }
  1132. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1133. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1134. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1135. used_flag_pointers.insert("DISCARD");
  1136. }
  1137. code = "discard;";
  1138. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1139. code = "continue;";
  1140. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1141. code = "break;";
  1142. }
  1143. } break;
  1144. case SL::Node::TYPE_MEMBER: {
  1145. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1146. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1147. if (mnode->index_expression != nullptr) {
  1148. code += "[";
  1149. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1150. code += "]";
  1151. } else if (mnode->assign_expression != nullptr) {
  1152. code += "=";
  1153. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1154. } else if (mnode->call_expression != nullptr) {
  1155. code += ".";
  1156. code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  1157. }
  1158. } break;
  1159. }
  1160. return code;
  1161. }
  1162. ShaderLanguage::DataType ShaderCompiler::_get_variable_type(const StringName &p_type) {
  1163. RS::GlobalVariableType gvt = RS::get_singleton()->global_variable_get_type(p_type);
  1164. return (ShaderLanguage::DataType)RS::global_variable_type_get_shader_datatype(gvt);
  1165. }
  1166. Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1167. SL::ShaderCompileInfo info;
  1168. info.functions = ShaderTypes::get_singleton()->get_functions(p_mode);
  1169. info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode);
  1170. info.shader_types = ShaderTypes::get_singleton()->get_types();
  1171. info.global_variable_type_func = _get_variable_type;
  1172. Error err = parser.compile(p_code, info);
  1173. if (err != OK) {
  1174. Vector<String> shader = p_code.split("\n");
  1175. for (int i = 0; i < shader.size(); i++) {
  1176. if (i + 1 == parser.get_error_line()) {
  1177. // Mark the error line to be visible without having to look at
  1178. // the trace at the end.
  1179. print_line(vformat("E%4d-> %s", i + 1, shader[i]));
  1180. } else {
  1181. print_line(vformat("%5d | %s", i + 1, shader[i]));
  1182. }
  1183. }
  1184. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER);
  1185. return err;
  1186. }
  1187. r_gen_code.defines.clear();
  1188. r_gen_code.code.clear();
  1189. for (int i = 0; i < STAGE_MAX; i++) {
  1190. r_gen_code.stage_globals[i] = String();
  1191. }
  1192. r_gen_code.uses_fragment_time = false;
  1193. r_gen_code.uses_vertex_time = false;
  1194. r_gen_code.uses_global_textures = false;
  1195. used_name_defines.clear();
  1196. used_rmode_defines.clear();
  1197. used_flag_pointers.clear();
  1198. fragment_varyings.clear();
  1199. shader = parser.get_shader();
  1200. function = nullptr;
  1201. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1202. return OK;
  1203. }
  1204. void ShaderCompiler::initialize(DefaultIdentifierActions p_actions) {
  1205. actions = p_actions;
  1206. time_name = "TIME";
  1207. List<String> func_list;
  1208. ShaderLanguage::get_builtin_funcs(&func_list);
  1209. for (const String &E : func_list) {
  1210. internal_functions.insert(E);
  1211. }
  1212. texture_functions.insert("texture");
  1213. texture_functions.insert("textureProj");
  1214. texture_functions.insert("textureLod");
  1215. texture_functions.insert("textureProjLod");
  1216. texture_functions.insert("textureGrad");
  1217. texture_functions.insert("textureGather");
  1218. texture_functions.insert("textureSize");
  1219. texture_functions.insert("texelFetch");
  1220. }
  1221. ShaderCompiler::ShaderCompiler() {
  1222. }