shader_compiler.cpp 50 KB

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