shader_compiler.cpp 56 KB

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