shader_compiler_rd.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*************************************************************************/
  2. /* shader_compiler_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_storage_rd.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 (ShaderLanguage::is_sampler_type(p_type)) {
  46. type = type.replace("sampler", "texture"); //we use textures instead of samplers
  47. }
  48. return type;
  49. }
  50. static int _get_datatype_size(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 12;
  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 12;
  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 12;
  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 12;
  84. case SL::TYPE_VEC4:
  85. return 16;
  86. case SL::TYPE_MAT2:
  87. return 32; //4 * 4 + 4 * 4
  88. case SL::TYPE_MAT3:
  89. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  90. case SL::TYPE_MAT4:
  91. return 64;
  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_STRUCT:
  113. return 0;
  114. case SL::TYPE_MAX: {
  115. ERR_FAIL_V(0);
  116. };
  117. }
  118. ERR_FAIL_V(0);
  119. }
  120. static int _get_datatype_alignment(SL::DataType p_type) {
  121. switch (p_type) {
  122. case SL::TYPE_VOID:
  123. return 0;
  124. case SL::TYPE_BOOL:
  125. return 4;
  126. case SL::TYPE_BVEC2:
  127. return 8;
  128. case SL::TYPE_BVEC3:
  129. return 16;
  130. case SL::TYPE_BVEC4:
  131. return 16;
  132. case SL::TYPE_INT:
  133. return 4;
  134. case SL::TYPE_IVEC2:
  135. return 8;
  136. case SL::TYPE_IVEC3:
  137. return 16;
  138. case SL::TYPE_IVEC4:
  139. return 16;
  140. case SL::TYPE_UINT:
  141. return 4;
  142. case SL::TYPE_UVEC2:
  143. return 8;
  144. case SL::TYPE_UVEC3:
  145. return 16;
  146. case SL::TYPE_UVEC4:
  147. return 16;
  148. case SL::TYPE_FLOAT:
  149. return 4;
  150. case SL::TYPE_VEC2:
  151. return 8;
  152. case SL::TYPE_VEC3:
  153. return 16;
  154. case SL::TYPE_VEC4:
  155. return 16;
  156. case SL::TYPE_MAT2:
  157. return 16;
  158. case SL::TYPE_MAT3:
  159. return 16;
  160. case SL::TYPE_MAT4:
  161. return 16;
  162. case SL::TYPE_SAMPLER2D:
  163. return 16;
  164. case SL::TYPE_ISAMPLER2D:
  165. return 16;
  166. case SL::TYPE_USAMPLER2D:
  167. return 16;
  168. case SL::TYPE_SAMPLER2DARRAY:
  169. return 16;
  170. case SL::TYPE_ISAMPLER2DARRAY:
  171. return 16;
  172. case SL::TYPE_USAMPLER2DARRAY:
  173. return 16;
  174. case SL::TYPE_SAMPLER3D:
  175. return 16;
  176. case SL::TYPE_ISAMPLER3D:
  177. return 16;
  178. case SL::TYPE_USAMPLER3D:
  179. return 16;
  180. case SL::TYPE_SAMPLERCUBE:
  181. return 16;
  182. case SL::TYPE_STRUCT:
  183. return 0;
  184. case SL::TYPE_MAX: {
  185. ERR_FAIL_V(0);
  186. }
  187. }
  188. ERR_FAIL_V(0);
  189. }
  190. static String _interpstr(SL::DataInterpolation p_interp) {
  191. switch (p_interp) {
  192. case SL::INTERPOLATION_FLAT:
  193. return "flat ";
  194. case SL::INTERPOLATION_SMOOTH:
  195. return "";
  196. }
  197. return "";
  198. }
  199. static String _prestr(SL::DataPrecision p_pres) {
  200. switch (p_pres) {
  201. case SL::PRECISION_LOWP:
  202. return "lowp ";
  203. case SL::PRECISION_MEDIUMP:
  204. return "mediump ";
  205. case SL::PRECISION_HIGHP:
  206. return "highp ";
  207. case SL::PRECISION_DEFAULT:
  208. return "";
  209. }
  210. return "";
  211. }
  212. static String _qualstr(SL::ArgumentQualifier p_qual) {
  213. switch (p_qual) {
  214. case SL::ARGUMENT_QUALIFIER_IN:
  215. return "";
  216. case SL::ARGUMENT_QUALIFIER_OUT:
  217. return "out ";
  218. case SL::ARGUMENT_QUALIFIER_INOUT:
  219. return "inout ";
  220. }
  221. return "";
  222. }
  223. static String _opstr(SL::Operator p_op) {
  224. return SL::get_operator_text(p_op);
  225. }
  226. static String _mkid(const String &p_id) {
  227. String id = "m_" + p_id.replace("__", "_dus_");
  228. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  229. }
  230. static String f2sp0(float p_float) {
  231. String num = rtoss(p_float);
  232. if (num.find(".") == -1 && num.find("e") == -1) {
  233. num += ".0";
  234. }
  235. return num;
  236. }
  237. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  238. switch (p_type) {
  239. case SL::TYPE_BOOL:
  240. return p_values[0].boolean ? "true" : "false";
  241. case SL::TYPE_BVEC2:
  242. case SL::TYPE_BVEC3:
  243. case SL::TYPE_BVEC4: {
  244. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  245. for (int i = 0; i < p_values.size(); i++) {
  246. if (i > 0)
  247. text += ",";
  248. text += p_values[i].boolean ? "true" : "false";
  249. }
  250. text += ")";
  251. return text;
  252. }
  253. case SL::TYPE_INT:
  254. return itos(p_values[0].sint);
  255. case SL::TYPE_IVEC2:
  256. case SL::TYPE_IVEC3:
  257. case SL::TYPE_IVEC4: {
  258. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  259. for (int i = 0; i < p_values.size(); i++) {
  260. if (i > 0)
  261. text += ",";
  262. text += itos(p_values[i].sint);
  263. }
  264. text += ")";
  265. return text;
  266. } break;
  267. case SL::TYPE_UINT:
  268. return itos(p_values[0].uint) + "u";
  269. case SL::TYPE_UVEC2:
  270. case SL::TYPE_UVEC3:
  271. case SL::TYPE_UVEC4: {
  272. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  273. for (int i = 0; i < p_values.size(); i++) {
  274. if (i > 0)
  275. text += ",";
  276. text += itos(p_values[i].uint) + "u";
  277. }
  278. text += ")";
  279. return text;
  280. } break;
  281. case SL::TYPE_FLOAT:
  282. return f2sp0(p_values[0].real);
  283. case SL::TYPE_VEC2:
  284. case SL::TYPE_VEC3:
  285. case SL::TYPE_VEC4: {
  286. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  287. for (int i = 0; i < p_values.size(); i++) {
  288. if (i > 0)
  289. text += ",";
  290. text += f2sp0(p_values[i].real);
  291. }
  292. text += ")";
  293. return text;
  294. } break;
  295. case SL::TYPE_MAT2:
  296. case SL::TYPE_MAT3:
  297. case SL::TYPE_MAT4: {
  298. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  299. for (int i = 0; i < p_values.size(); i++) {
  300. if (i > 0)
  301. text += ",";
  302. text += f2sp0(p_values[i].real);
  303. }
  304. text += ")";
  305. return text;
  306. } break;
  307. default:
  308. ERR_FAIL_V(String());
  309. }
  310. }
  311. String ShaderCompilerRD::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) {
  312. if (p_filter == ShaderLanguage::FILTER_DEFAULT) {
  313. ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String());
  314. p_filter = actions.default_filter;
  315. }
  316. if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) {
  317. ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String());
  318. p_repeat = actions.default_repeat;
  319. }
  320. return actions.sampler_array_name + "[" + itos(p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)) + "]";
  321. }
  322. void ShaderCompilerRD::_dump_function_deps(const SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added) {
  323. int fidx = -1;
  324. for (int i = 0; i < p_node->functions.size(); i++) {
  325. if (p_node->functions[i].name == p_for_func) {
  326. fidx = i;
  327. break;
  328. }
  329. }
  330. ERR_FAIL_COND(fidx == -1);
  331. for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) {
  332. if (added.has(E->get())) {
  333. continue; //was added already
  334. }
  335. _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added);
  336. SL::FunctionNode *fnode = nullptr;
  337. for (int i = 0; i < p_node->functions.size(); i++) {
  338. if (p_node->functions[i].name == E->get()) {
  339. fnode = p_node->functions[i].function;
  340. break;
  341. }
  342. }
  343. ERR_FAIL_COND(!fnode);
  344. r_to_add += "\n";
  345. String header;
  346. if (fnode->return_type == SL::TYPE_STRUCT) {
  347. header = _mkid(fnode->return_struct_name) + " " + _mkid(fnode->name) + "(";
  348. } else {
  349. header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "(";
  350. }
  351. for (int i = 0; i < fnode->arguments.size(); i++) {
  352. if (i > 0)
  353. header += ", ";
  354. if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
  355. header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
  356. } else {
  357. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  358. }
  359. }
  360. header += ")\n";
  361. r_to_add += header;
  362. r_to_add += p_func_code[E->get()];
  363. added.insert(E->get());
  364. }
  365. }
  366. static String _get_global_variable_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
  367. switch (p_type) {
  368. case ShaderLanguage::TYPE_BOOL: {
  369. return "(" + p_buffer + "[" + p_index + "].x != 0.0)";
  370. }
  371. case ShaderLanguage::TYPE_BVEC2: {
  372. return "(" + p_buffer + "[" + p_index + "].xy != vec2(0.0))";
  373. }
  374. case ShaderLanguage::TYPE_BVEC3: {
  375. return "(" + p_buffer + "[" + p_index + "].xyz != vec3(0.0))";
  376. }
  377. case ShaderLanguage::TYPE_BVEC4: {
  378. return "(" + p_buffer + "[" + p_index + "].xyzw != vec4(0.0))";
  379. }
  380. case ShaderLanguage::TYPE_INT: {
  381. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
  382. }
  383. case ShaderLanguage::TYPE_IVEC2: {
  384. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
  385. }
  386. case ShaderLanguage::TYPE_IVEC3: {
  387. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
  388. }
  389. case ShaderLanguage::TYPE_IVEC4: {
  390. return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
  391. }
  392. case ShaderLanguage::TYPE_UINT: {
  393. return "floatBitsToUInt(" + p_buffer + "[" + p_index + "].x)";
  394. }
  395. case ShaderLanguage::TYPE_UVEC2: {
  396. return "floatBitsToUInt(" + p_buffer + "[" + p_index + "].xy)";
  397. }
  398. case ShaderLanguage::TYPE_UVEC3: {
  399. return "floatBitsToUInt(" + p_buffer + "[" + p_index + "].xyz)";
  400. }
  401. case ShaderLanguage::TYPE_UVEC4: {
  402. return "floatBitsToUInt(" + p_buffer + "[" + p_index + "].xyzw)";
  403. }
  404. case ShaderLanguage::TYPE_FLOAT: {
  405. return "(" + p_buffer + "[" + p_index + "].x)";
  406. }
  407. case ShaderLanguage::TYPE_VEC2: {
  408. return "(" + p_buffer + "[" + p_index + "].xy)";
  409. }
  410. case ShaderLanguage::TYPE_VEC3: {
  411. return "(" + p_buffer + "[" + p_index + "].xyz)";
  412. }
  413. case ShaderLanguage::TYPE_VEC4: {
  414. return "(" + p_buffer + "[" + p_index + "].xyzw)";
  415. }
  416. case ShaderLanguage::TYPE_MAT2: {
  417. return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)";
  418. }
  419. case ShaderLanguage::TYPE_MAT3: {
  420. return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)";
  421. }
  422. case ShaderLanguage::TYPE_MAT4: {
  423. 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)";
  424. }
  425. default: {
  426. ERR_FAIL_V("void");
  427. }
  428. }
  429. }
  430. String ShaderCompilerRD::_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) {
  431. String code;
  432. switch (p_node->type) {
  433. case SL::Node::TYPE_SHADER: {
  434. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  435. for (int i = 0; i < pnode->render_modes.size(); i++) {
  436. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  437. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
  438. used_rmode_defines.insert(pnode->render_modes[i]);
  439. }
  440. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  441. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  442. }
  443. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  444. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  445. *p.first = p.second;
  446. }
  447. }
  448. // structs
  449. for (int i = 0; i < pnode->vstructs.size(); i++) {
  450. SL::StructNode *st = pnode->vstructs[i].shader_struct;
  451. String struct_code;
  452. struct_code += "struct ";
  453. struct_code += _mkid(pnode->vstructs[i].name);
  454. struct_code += " ";
  455. struct_code += "{\n";
  456. for (int j = 0; j < st->members.size(); j++) {
  457. SL::MemberNode *m = st->members[j];
  458. if (m->datatype == SL::TYPE_STRUCT) {
  459. struct_code += _mkid(m->struct_name);
  460. } else {
  461. struct_code += _prestr(m->precision);
  462. struct_code += _typestr(m->datatype);
  463. }
  464. struct_code += " ";
  465. struct_code += m->name;
  466. if (m->array_size > 0) {
  467. struct_code += "[";
  468. struct_code += itos(m->array_size);
  469. struct_code += "]";
  470. }
  471. struct_code += ";\n";
  472. }
  473. struct_code += "}";
  474. struct_code += ";\n";
  475. r_gen_code.vertex_global += struct_code;
  476. r_gen_code.fragment_global += struct_code;
  477. }
  478. int max_texture_uniforms = 0;
  479. int max_uniforms = 0;
  480. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  481. if (SL::is_sampler_type(E->get().type)) {
  482. max_texture_uniforms++;
  483. } else {
  484. if (E->get().scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  485. continue; //instances are indexed directly, dont need index uniforms
  486. }
  487. max_uniforms++;
  488. }
  489. }
  490. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  491. Vector<int> uniform_sizes;
  492. Vector<int> uniform_alignments;
  493. Vector<StringName> uniform_defines;
  494. uniform_sizes.resize(max_uniforms);
  495. uniform_alignments.resize(max_uniforms);
  496. uniform_defines.resize(max_uniforms);
  497. bool uses_uniforms = false;
  498. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  499. String ucode;
  500. if (E->get().scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  501. //insert, but don't generate any code.
  502. p_actions.uniforms->insert(E->key(), E->get());
  503. continue; //instances are indexed directly, dont need index uniforms
  504. }
  505. if (SL::is_sampler_type(E->get().type)) {
  506. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + E->get().texture_order) + ") uniform ";
  507. }
  508. bool is_buffer_global = !SL::is_sampler_type(E->get().type) && E->get().scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  509. if (is_buffer_global) {
  510. //this is an integer to index the global table
  511. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  512. } else {
  513. ucode += _prestr(E->get().precision);
  514. ucode += _typestr(E->get().type);
  515. }
  516. ucode += " " + _mkid(E->key());
  517. ucode += ";\n";
  518. if (SL::is_sampler_type(E->get().type)) {
  519. r_gen_code.vertex_global += ucode;
  520. r_gen_code.fragment_global += ucode;
  521. GeneratedCode::Texture texture;
  522. texture.name = E->key();
  523. texture.hint = E->get().hint;
  524. texture.type = E->get().type;
  525. texture.filter = E->get().filter;
  526. texture.repeat = E->get().repeat;
  527. texture.global = E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  528. if (texture.global) {
  529. r_gen_code.uses_global_textures = true;
  530. }
  531. r_gen_code.texture_uniforms.write[E->get().texture_order] = texture;
  532. } else {
  533. if (!uses_uniforms) {
  534. r_gen_code.defines.push_back(String("#define USE_MATERIAL_UNIFORMS\n"));
  535. uses_uniforms = true;
  536. }
  537. uniform_defines.write[E->get().order] = ucode;
  538. if (is_buffer_global) {
  539. //globals are indices into the global table
  540. uniform_sizes.write[E->get().order] = _get_datatype_size(ShaderLanguage::TYPE_UINT);
  541. uniform_alignments.write[E->get().order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  542. } else {
  543. uniform_sizes.write[E->get().order] = _get_datatype_size(E->get().type);
  544. uniform_alignments.write[E->get().order] = _get_datatype_alignment(E->get().type);
  545. }
  546. }
  547. p_actions.uniforms->insert(E->key(), E->get());
  548. }
  549. for (int i = 0; i < max_uniforms; i++) {
  550. r_gen_code.uniforms += uniform_defines[i];
  551. }
  552. #if 1
  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. #else
  568. // add up
  569. for (int i = 0; i < uniform_sizes.size(); i++) {
  570. if (i > 0) {
  571. int align = uniform_sizes[i - 1] % uniform_alignments[i];
  572. if (align != 0) {
  573. uniform_sizes[i - 1] += uniform_alignments[i] - align;
  574. }
  575. uniform_sizes[i] = uniform_sizes[i] + uniform_sizes[i - 1];
  576. }
  577. }
  578. //offset
  579. r_gen_code.uniform_offsets.resize(uniform_sizes.size());
  580. for (int i = 0; i < uniform_sizes.size(); i++) {
  581. if (i > 0)
  582. r_gen_code.uniform_offsets[i] = uniform_sizes[i - 1];
  583. else
  584. r_gen_code.uniform_offsets[i] = 0;
  585. }
  586. /*
  587. for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) {
  588. if (SL::is_sampler_type(E->get().type)) {
  589. continue;
  590. }
  591. }
  592. */
  593. if (uniform_sizes.size()) {
  594. r_gen_code.uniform_total_size = uniform_sizes[uniform_sizes.size() - 1];
  595. } else {
  596. r_gen_code.uniform_total_size = 0;
  597. }
  598. #endif
  599. uint32_t index = p_default_actions.base_varying_index;
  600. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  601. String vcode;
  602. String interp_mode = _interpstr(E->get().interpolation);
  603. vcode += _prestr(E->get().precision);
  604. vcode += _typestr(E->get().type);
  605. vcode += " " + _mkid(E->key());
  606. if (E->get().array_size > 0) {
  607. vcode += "[";
  608. vcode += itos(E->get().array_size);
  609. vcode += "]";
  610. }
  611. vcode += ";\n";
  612. r_gen_code.vertex_global += "layout(location=" + itos(index) + ") " + interp_mode + "out " + vcode;
  613. r_gen_code.fragment_global += "layout(location=" + itos(index) + ") " + interp_mode + "in " + vcode;
  614. index++;
  615. }
  616. for (int i = 0; i < pnode->vconstants.size(); i++) {
  617. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  618. String gcode;
  619. gcode += "const ";
  620. gcode += _prestr(cnode.precision);
  621. if (cnode.type == SL::TYPE_STRUCT) {
  622. gcode += _mkid(cnode.type_str);
  623. } else {
  624. gcode += _typestr(cnode.type);
  625. }
  626. gcode += " " + _mkid(String(cnode.name));
  627. gcode += "=";
  628. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  629. gcode += ";\n";
  630. r_gen_code.vertex_global += gcode;
  631. r_gen_code.fragment_global += gcode;
  632. }
  633. Map<StringName, String> function_code;
  634. //code for functions
  635. for (int i = 0; i < pnode->functions.size(); i++) {
  636. SL::FunctionNode *fnode = pnode->functions[i].function;
  637. function = fnode;
  638. current_func_name = fnode->name;
  639. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  640. function = nullptr;
  641. }
  642. //place functions in actual code
  643. Set<StringName> added_vtx;
  644. Set<StringName> added_fragment; //share for light
  645. for (int i = 0; i < pnode->functions.size(); i++) {
  646. SL::FunctionNode *fnode = pnode->functions[i].function;
  647. function = fnode;
  648. current_func_name = fnode->name;
  649. if (fnode->name == vertex_name) {
  650. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  651. r_gen_code.vertex = function_code[vertex_name];
  652. }
  653. if (fnode->name == fragment_name) {
  654. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  655. r_gen_code.fragment = function_code[fragment_name];
  656. }
  657. if (fnode->name == light_name) {
  658. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  659. r_gen_code.light = function_code[light_name];
  660. }
  661. function = nullptr;
  662. }
  663. //code+=dump_node_code(pnode->body,p_level);
  664. } break;
  665. case SL::Node::TYPE_STRUCT: {
  666. } break;
  667. case SL::Node::TYPE_FUNCTION: {
  668. } break;
  669. case SL::Node::TYPE_BLOCK: {
  670. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  671. //variables
  672. if (!bnode->single_statement) {
  673. code += _mktab(p_level - 1) + "{\n";
  674. }
  675. for (int i = 0; i < bnode->statements.size(); i++) {
  676. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  677. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  678. code += scode; //use directly
  679. } else {
  680. code += _mktab(p_level) + scode + ";\n";
  681. }
  682. }
  683. if (!bnode->single_statement) {
  684. code += _mktab(p_level - 1) + "}\n";
  685. }
  686. } break;
  687. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  688. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  689. String declaration;
  690. if (vdnode->is_const) {
  691. declaration += "const ";
  692. }
  693. if (vdnode->datatype == SL::TYPE_STRUCT) {
  694. declaration += _mkid(vdnode->struct_name);
  695. } else {
  696. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  697. }
  698. for (int i = 0; i < vdnode->declarations.size(); i++) {
  699. if (i > 0) {
  700. declaration += ",";
  701. } else {
  702. declaration += " ";
  703. }
  704. declaration += _mkid(vdnode->declarations[i].name);
  705. if (vdnode->declarations[i].initializer) {
  706. declaration += "=";
  707. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  708. }
  709. }
  710. code += declaration;
  711. } break;
  712. case SL::Node::TYPE_VARIABLE: {
  713. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  714. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  715. *p_actions.write_flag_pointers[vnode->name] = true;
  716. }
  717. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  718. String define = p_default_actions.usage_defines[vnode->name];
  719. if (define.begins_with("@")) {
  720. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  721. }
  722. r_gen_code.defines.push_back(define);
  723. used_name_defines.insert(vnode->name);
  724. }
  725. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  726. *p_actions.usage_flag_pointers[vnode->name] = true;
  727. used_flag_pointers.insert(vnode->name);
  728. }
  729. if (p_default_actions.renames.has(vnode->name))
  730. code = p_default_actions.renames[vnode->name];
  731. else {
  732. if (shader->uniforms.has(vnode->name)) {
  733. //its a uniform!
  734. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  735. if (u.texture_order >= 0) {
  736. code = _mkid(vnode->name); //texture, use as is
  737. } else {
  738. //a scalar or vector
  739. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  740. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  741. //global variable, this means the code points to an index to the global table
  742. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  743. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  744. //instance variable, index it as such
  745. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  746. code = _get_global_variable_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  747. } else {
  748. //regular uniform, index from UBO
  749. code = actions.base_uniform_string + _mkid(vnode->name);
  750. }
  751. }
  752. } else {
  753. code = _mkid(vnode->name); //its something else (local var most likely) use as is
  754. }
  755. }
  756. if (vnode->name == time_name) {
  757. if (current_func_name == vertex_name) {
  758. r_gen_code.uses_vertex_time = true;
  759. }
  760. if (current_func_name == fragment_name || current_func_name == light_name) {
  761. r_gen_code.uses_fragment_time = true;
  762. }
  763. }
  764. } break;
  765. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  766. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  767. int sz = acnode->initializer.size();
  768. if (acnode->datatype == SL::TYPE_STRUCT) {
  769. code += _mkid(acnode->struct_name);
  770. } else {
  771. code += _typestr(acnode->datatype);
  772. }
  773. code += "[";
  774. code += itos(acnode->initializer.size());
  775. code += "]";
  776. code += "(";
  777. for (int i = 0; i < sz; i++) {
  778. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  779. if (i != sz - 1) {
  780. code += ", ";
  781. }
  782. }
  783. code += ")";
  784. } break;
  785. case SL::Node::TYPE_ARRAY_DECLARATION: {
  786. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  787. String declaration;
  788. if (adnode->is_const) {
  789. declaration += "const ";
  790. }
  791. if (adnode->datatype == SL::TYPE_STRUCT) {
  792. declaration += _mkid(adnode->struct_name);
  793. } else {
  794. declaration = _prestr(adnode->precision) + _typestr(adnode->datatype);
  795. }
  796. for (int i = 0; i < adnode->declarations.size(); i++) {
  797. if (i > 0) {
  798. declaration += ",";
  799. } else {
  800. declaration += " ";
  801. }
  802. declaration += _mkid(adnode->declarations[i].name);
  803. declaration += "[";
  804. declaration += itos(adnode->declarations[i].size);
  805. declaration += "]";
  806. int sz = adnode->declarations[i].initializer.size();
  807. if (sz > 0) {
  808. declaration += "=";
  809. if (adnode->datatype == SL::TYPE_STRUCT) {
  810. declaration += _mkid(adnode->struct_name);
  811. } else {
  812. declaration += _typestr(adnode->datatype);
  813. }
  814. declaration += "[";
  815. declaration += itos(sz);
  816. declaration += "]";
  817. declaration += "(";
  818. for (int j = 0; j < sz; j++) {
  819. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  820. if (j != sz - 1) {
  821. declaration += ", ";
  822. }
  823. }
  824. declaration += ")";
  825. }
  826. }
  827. code += declaration;
  828. } break;
  829. case SL::Node::TYPE_ARRAY: {
  830. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  831. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  832. *p_actions.write_flag_pointers[anode->name] = true;
  833. }
  834. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  835. String define = p_default_actions.usage_defines[anode->name];
  836. if (define.begins_with("@")) {
  837. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  838. }
  839. r_gen_code.defines.push_back(define);
  840. used_name_defines.insert(anode->name);
  841. }
  842. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  843. *p_actions.usage_flag_pointers[anode->name] = true;
  844. used_flag_pointers.insert(anode->name);
  845. }
  846. if (p_default_actions.renames.has(anode->name))
  847. code = p_default_actions.renames[anode->name];
  848. else
  849. code = _mkid(anode->name);
  850. if (anode->call_expression != nullptr) {
  851. code += ".";
  852. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  853. }
  854. if (anode->index_expression != nullptr) {
  855. code += "[";
  856. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  857. code += "]";
  858. }
  859. if (anode->name == time_name) {
  860. if (current_func_name == vertex_name) {
  861. r_gen_code.uses_vertex_time = true;
  862. }
  863. if (current_func_name == fragment_name || current_func_name == light_name) {
  864. r_gen_code.uses_fragment_time = true;
  865. }
  866. }
  867. } break;
  868. case SL::Node::TYPE_CONSTANT: {
  869. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  870. return get_constant_text(cnode->datatype, cnode->values);
  871. } break;
  872. case SL::Node::TYPE_OPERATOR: {
  873. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  874. switch (onode->op) {
  875. case SL::OP_ASSIGN:
  876. case SL::OP_ASSIGN_ADD:
  877. case SL::OP_ASSIGN_SUB:
  878. case SL::OP_ASSIGN_MUL:
  879. case SL::OP_ASSIGN_DIV:
  880. case SL::OP_ASSIGN_SHIFT_LEFT:
  881. case SL::OP_ASSIGN_SHIFT_RIGHT:
  882. case SL::OP_ASSIGN_MOD:
  883. case SL::OP_ASSIGN_BIT_AND:
  884. case SL::OP_ASSIGN_BIT_OR:
  885. case SL::OP_ASSIGN_BIT_XOR:
  886. 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);
  887. break;
  888. case SL::OP_BIT_INVERT:
  889. case SL::OP_NEGATE:
  890. case SL::OP_NOT:
  891. case SL::OP_DECREMENT:
  892. case SL::OP_INCREMENT:
  893. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  894. break;
  895. case SL::OP_POST_DECREMENT:
  896. case SL::OP_POST_INCREMENT:
  897. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  898. break;
  899. case SL::OP_CALL:
  900. case SL::OP_STRUCT:
  901. case SL::OP_CONSTRUCT: {
  902. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  903. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  904. bool is_texture_func = false;
  905. if (onode->op == SL::OP_STRUCT) {
  906. code += _mkid(vnode->name);
  907. } else if (onode->op == SL::OP_CONSTRUCT) {
  908. code += String(vnode->name);
  909. } else {
  910. if (internal_functions.has(vnode->name)) {
  911. code += vnode->name;
  912. is_texture_func = texture_functions.has(vnode->name);
  913. } else if (p_default_actions.renames.has(vnode->name)) {
  914. code += p_default_actions.renames[vnode->name];
  915. } else {
  916. code += _mkid(vnode->name);
  917. }
  918. }
  919. code += "(";
  920. for (int i = 1; i < onode->arguments.size(); i++) {
  921. if (i > 1)
  922. code += ", ";
  923. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  924. if (is_texture_func && i == 1 && onode->arguments[i]->type == SL::Node::TYPE_VARIABLE) {
  925. //need to map from texture to sampler in order to sample
  926. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  927. StringName texture_uniform = varnode->name;
  928. String sampler_name;
  929. if (actions.custom_samplers.has(texture_uniform)) {
  930. sampler_name = actions.custom_samplers[texture_uniform];
  931. } else {
  932. if (shader->uniforms.has(texture_uniform)) {
  933. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  934. } else {
  935. bool found = false;
  936. for (int j = 0; j < function->arguments.size(); j++) {
  937. if (function->arguments[j].name == texture_uniform) {
  938. if (function->arguments[j].tex_builtin_check) {
  939. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  940. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  941. found = true;
  942. break;
  943. }
  944. if (function->arguments[j].tex_argument_check) {
  945. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  946. found = true;
  947. break;
  948. }
  949. }
  950. }
  951. if (!found) {
  952. //function was most likely unused, so use anything (compiler will remove it anyway)
  953. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  954. }
  955. }
  956. }
  957. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  958. } else {
  959. code += node_code;
  960. }
  961. }
  962. code += ")";
  963. } break;
  964. case SL::OP_INDEX: {
  965. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  966. code += "[";
  967. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  968. code += "]";
  969. } break;
  970. case SL::OP_SELECT_IF: {
  971. code += "(";
  972. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  973. code += "?";
  974. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  975. code += ":";
  976. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  977. code += ")";
  978. } break;
  979. default: {
  980. 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) + ")";
  981. break;
  982. }
  983. }
  984. } break;
  985. case SL::Node::TYPE_CONTROL_FLOW: {
  986. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  987. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  988. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  989. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  990. if (cfnode->blocks.size() == 2) {
  991. code += _mktab(p_level) + "else\n";
  992. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  993. }
  994. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  995. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  996. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  997. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  998. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  999. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1000. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1001. code += _mktab(p_level) + "default:\n";
  1002. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1003. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1004. code += _mktab(p_level) + "do";
  1005. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1006. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1007. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1008. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1009. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1010. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1011. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1012. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1013. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1014. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1015. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1016. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1017. if (cfnode->expressions.size()) {
  1018. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1019. } else {
  1020. code = "return;";
  1021. }
  1022. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1023. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1024. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1025. used_flag_pointers.insert("DISCARD");
  1026. }
  1027. code = "discard;";
  1028. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1029. code = "continue;";
  1030. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1031. code = "break;";
  1032. }
  1033. } break;
  1034. case SL::Node::TYPE_MEMBER: {
  1035. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1036. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1037. if (mnode->index_expression != nullptr) {
  1038. code += "[";
  1039. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1040. code += "]";
  1041. }
  1042. } break;
  1043. }
  1044. return code;
  1045. }
  1046. ShaderLanguage::DataType ShaderCompilerRD::_get_variable_type(const StringName &p_type) {
  1047. RS::GlobalVariableType gvt = ((RasterizerStorageRD *)(RasterizerStorage::base_singleton))->global_variable_get_type_internal(p_type);
  1048. return RS::global_variable_type_get_shader_datatype(gvt);
  1049. }
  1050. Error ShaderCompilerRD::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1051. Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), ShaderTypes::get_singleton()->get_types(), _get_variable_type);
  1052. if (err != OK) {
  1053. Vector<String> shader = p_code.split("\n");
  1054. for (int i = 0; i < shader.size(); i++) {
  1055. print_line(itos(i + 1) + " " + shader[i]);
  1056. }
  1057. _err_print_error(nullptr, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  1058. return err;
  1059. }
  1060. r_gen_code.defines.clear();
  1061. r_gen_code.vertex = String();
  1062. r_gen_code.vertex_global = String();
  1063. r_gen_code.fragment = String();
  1064. r_gen_code.fragment_global = String();
  1065. r_gen_code.light = String();
  1066. r_gen_code.uses_fragment_time = false;
  1067. r_gen_code.uses_vertex_time = false;
  1068. r_gen_code.uses_global_textures = false;
  1069. used_name_defines.clear();
  1070. used_rmode_defines.clear();
  1071. used_flag_pointers.clear();
  1072. shader = parser.get_shader();
  1073. function = nullptr;
  1074. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1075. return OK;
  1076. }
  1077. void ShaderCompilerRD::initialize(DefaultIdentifierActions p_actions) {
  1078. actions = p_actions;
  1079. vertex_name = "vertex";
  1080. fragment_name = "fragment";
  1081. light_name = "light";
  1082. time_name = "TIME";
  1083. List<String> func_list;
  1084. ShaderLanguage::get_builtin_funcs(&func_list);
  1085. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  1086. internal_functions.insert(E->get());
  1087. }
  1088. texture_functions.insert("texture");
  1089. texture_functions.insert("textureProj");
  1090. texture_functions.insert("textureLod");
  1091. texture_functions.insert("textureProjLod");
  1092. texture_functions.insert("textureGrad");
  1093. }
  1094. ShaderCompilerRD::ShaderCompilerRD() {
  1095. #if 0
  1096. /** SPATIAL SHADER **/
  1097. actions[RS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  1098. actions[RS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  1099. actions[RS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  1100. actions[RS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  1101. actions[RS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1102. actions[RS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  1103. actions[RS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  1104. actions[RS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  1105. actions[RS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  1106. actions[RS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  1107. actions[RS::SHADER_SPATIAL].renames["POSITION"] = "position";
  1108. actions[RS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  1109. actions[RS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  1110. actions[RS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  1111. actions[RS::SHADER_SPATIAL].renames["POINT_SIZE"] = "gl_PointSize";
  1112. actions[RS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  1113. //builtins
  1114. actions[RS::SHADER_SPATIAL].renames["TIME"] = "time";
  1115. actions[RS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  1116. actions[RS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  1117. actions[RS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  1118. actions[RS::SHADER_SPATIAL].renames["NORMALMAP"] = "normalmap";
  1119. actions[RS::SHADER_SPATIAL].renames["NORMALMAP_DEPTH"] = "normaldepth";
  1120. actions[RS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  1121. actions[RS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  1122. actions[RS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  1123. actions[RS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  1124. actions[RS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  1125. actions[RS::SHADER_SPATIAL].renames["RIM"] = "rim";
  1126. actions[RS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  1127. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  1128. actions[RS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  1129. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  1130. actions[RS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1131. actions[RS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  1132. actions[RS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  1133. actions[RS::SHADER_SPATIAL].renames["AO"] = "ao";
  1134. actions[RS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1135. actions[RS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  1136. actions[RS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  1137. actions[RS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  1138. actions[RS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  1139. actions[RS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  1140. actions[RS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  1141. actions[RS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  1142. actions[RS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  1143. actions[RS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1144. //for light
  1145. actions[RS::SHADER_SPATIAL].renames["VIEW"] = "view";
  1146. actions[RS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  1147. actions[RS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  1148. actions[RS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  1149. actions[RS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1150. actions[RS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  1151. actions[RS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  1152. actions[RS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  1153. actions[RS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  1154. actions[RS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  1155. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  1156. actions[RS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  1157. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  1158. actions[RS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1159. actions[RS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  1160. actions[RS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  1161. actions[RS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  1162. actions[RS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  1163. actions[RS::SHADER_SPATIAL].usage_defines["NORMALMAP"] = "#define ENABLE_NORMALMAP\n";
  1164. actions[RS::SHADER_SPATIAL].usage_defines["NORMALMAP_DEPTH"] = "@NORMALMAP";
  1165. actions[RS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  1166. actions[RS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1167. actions[RS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  1168. actions[RS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1169. actions[RS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1170. actions[RS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  1171. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1172. actions[RS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1173. actions[RS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1174. actions[RS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1175. actions[RS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1176. actions[RS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1177. actions[RS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1178. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1179. actions[RS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1180. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  1181. if (!force_lambert) {
  1182. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1183. }
  1184. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  1185. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1186. actions[RS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1187. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  1188. if (!force_blinn) {
  1189. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1190. } else {
  1191. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  1192. }
  1193. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  1194. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  1195. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1196. actions[RS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1197. actions[RS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1198. actions[RS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1199. actions[RS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1200. /* PARTICLES SHADER */
  1201. actions[RS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  1202. actions[RS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  1203. actions[RS::SHADER_PARTICLES].renames["MASS"] = "mass";
  1204. actions[RS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  1205. actions[RS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  1206. actions[RS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  1207. actions[RS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  1208. actions[RS::SHADER_PARTICLES].renames["TIME"] = "time";
  1209. actions[RS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  1210. actions[RS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  1211. actions[RS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  1212. actions[RS::SHADER_PARTICLES].renames["INDEX"] = "index";
  1213. actions[RS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  1214. actions[RS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  1215. actions[RS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  1216. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1217. actions[RS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1218. actions[RS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1219. #endif
  1220. }