shader_compiler_rd.cpp 52 KB

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