shader_compiler.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. max_texture_uniforms++;
  447. } else {
  448. if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  449. continue; // Instances are indexed directly, don't need index uniforms.
  450. }
  451. max_uniforms++;
  452. }
  453. }
  454. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  455. Vector<int> uniform_sizes;
  456. Vector<int> uniform_alignments;
  457. Vector<StringName> uniform_defines;
  458. uniform_sizes.resize(max_uniforms);
  459. uniform_alignments.resize(max_uniforms);
  460. uniform_defines.resize(max_uniforms);
  461. bool uses_uniforms = false;
  462. Vector<StringName> uniform_names;
  463. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  464. uniform_names.push_back(E.key);
  465. }
  466. uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  467. for (int k = 0; k < uniform_names.size(); k++) {
  468. StringName uniform_name = uniform_names[k];
  469. const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name];
  470. String ucode;
  471. if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
  472. //insert, but don't generate any code.
  473. p_actions.uniforms->insert(uniform_name, uniform);
  474. continue; // Instances are indexed directly, don't need index uniforms.
  475. }
  476. if (SL::is_sampler_type(uniform.type)) {
  477. // Texture layouts are different for OpenGL GLSL and Vulkan GLSL
  478. if (!RS::get_singleton()->is_low_end()) {
  479. ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") ";
  480. }
  481. ucode += "uniform ";
  482. }
  483. bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
  484. if (is_buffer_global) {
  485. //this is an integer to index the global table
  486. ucode += _typestr(ShaderLanguage::TYPE_UINT);
  487. } else {
  488. ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type));
  489. ucode += _typestr(uniform.type);
  490. }
  491. ucode += " " + _mkid(uniform_name);
  492. if (uniform.array_size > 0) {
  493. ucode += "[";
  494. ucode += itos(uniform.array_size);
  495. ucode += "]";
  496. }
  497. ucode += ";\n";
  498. if (SL::is_sampler_type(uniform.type)) {
  499. for (int j = 0; j < STAGE_MAX; j++) {
  500. r_gen_code.stage_globals[j] += ucode;
  501. }
  502. GeneratedCode::Texture texture;
  503. texture.name = uniform_name;
  504. texture.hint = uniform.hint;
  505. texture.type = uniform.type;
  506. texture.use_color = uniform.use_color;
  507. texture.filter = uniform.filter;
  508. texture.repeat = uniform.repeat;
  509. texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
  510. texture.array_size = uniform.array_size;
  511. if (texture.global) {
  512. r_gen_code.uses_global_textures = true;
  513. }
  514. r_gen_code.texture_uniforms.write[uniform.texture_order] = texture;
  515. } else {
  516. if (!uses_uniforms) {
  517. uses_uniforms = true;
  518. }
  519. uniform_defines.write[uniform.order] = ucode;
  520. if (is_buffer_global) {
  521. //globals are indices into the global table
  522. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(ShaderLanguage::TYPE_UINT);
  523. uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
  524. } else {
  525. // The following code enforces a 16-byte alignment of uniform arrays.
  526. if (uniform.array_size > 0) {
  527. int size = ShaderLanguage::get_datatype_size(uniform.type) * uniform.array_size;
  528. int m = (16 * uniform.array_size);
  529. if ((size % m) != 0) {
  530. size += m - (size % m);
  531. }
  532. uniform_sizes.write[uniform.order] = size;
  533. uniform_alignments.write[uniform.order] = 16;
  534. } else {
  535. uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(uniform.type);
  536. uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type);
  537. }
  538. }
  539. }
  540. p_actions.uniforms->insert(uniform_name, uniform);
  541. }
  542. for (int i = 0; i < max_uniforms; i++) {
  543. r_gen_code.uniforms += uniform_defines[i];
  544. }
  545. // add up
  546. int offset = 0;
  547. for (int i = 0; i < uniform_sizes.size(); i++) {
  548. int align = offset % uniform_alignments[i];
  549. if (align != 0) {
  550. offset += uniform_alignments[i] - align;
  551. }
  552. r_gen_code.uniform_offsets.push_back(offset);
  553. offset += uniform_sizes[i];
  554. }
  555. r_gen_code.uniform_total_size = offset;
  556. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  557. r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
  558. }
  559. uint32_t index = p_default_actions.base_varying_index;
  560. List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
  561. Vector<StringName> varying_names;
  562. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  563. varying_names.push_back(E.key);
  564. }
  565. varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
  566. for (int k = 0; k < varying_names.size(); k++) {
  567. StringName varying_name = varying_names[k];
  568. const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name];
  569. if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
  570. var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying));
  571. fragment_varyings.insert(varying_name);
  572. continue;
  573. }
  574. String vcode;
  575. String interp_mode = _interpstr(varying.interpolation);
  576. vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type));
  577. vcode += _typestr(varying.type);
  578. vcode += " " + _mkid(varying_name);
  579. uint32_t inc = 1U;
  580. if (varying.array_size > 0) {
  581. inc = (uint32_t)varying.array_size;
  582. vcode += "[";
  583. vcode += itos(varying.array_size);
  584. vcode += "]";
  585. }
  586. switch (varying.type) {
  587. case SL::TYPE_MAT2:
  588. inc *= 2U;
  589. break;
  590. case SL::TYPE_MAT3:
  591. inc *= 3U;
  592. break;
  593. case SL::TYPE_MAT4:
  594. inc *= 4U;
  595. break;
  596. default:
  597. break;
  598. }
  599. vcode += ";\n";
  600. // GLSL ES 3.0 does not allow layout qualifiers for varyings
  601. if (!RS::get_singleton()->is_low_end()) {
  602. r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") ";
  603. r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") ";
  604. }
  605. r_gen_code.stage_globals[STAGE_VERTEX] += interp_mode + "out " + vcode;
  606. r_gen_code.stage_globals[STAGE_FRAGMENT] += interp_mode + "in " + vcode;
  607. index += inc;
  608. }
  609. if (var_frag_to_light.size() > 0) {
  610. String gcode = "\n\nstruct {\n";
  611. for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) {
  612. gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first);
  613. if (E.second.array_size > 0) {
  614. gcode += "[";
  615. gcode += itos(E.second.array_size);
  616. gcode += "]";
  617. }
  618. gcode += ";\n";
  619. }
  620. gcode += "} frag_to_light;\n";
  621. r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode;
  622. }
  623. for (int i = 0; i < pnode->vconstants.size(); i++) {
  624. const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
  625. String gcode;
  626. gcode += _constr(true);
  627. gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
  628. if (cnode.type == SL::TYPE_STRUCT) {
  629. gcode += _mkid(cnode.type_str);
  630. } else {
  631. gcode += _typestr(cnode.type);
  632. }
  633. gcode += " " + _mkid(String(cnode.name));
  634. if (cnode.array_size > 0) {
  635. gcode += "[";
  636. gcode += itos(cnode.array_size);
  637. gcode += "]";
  638. }
  639. gcode += "=";
  640. gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  641. gcode += ";\n";
  642. for (int j = 0; j < STAGE_MAX; j++) {
  643. r_gen_code.stage_globals[j] += gcode;
  644. }
  645. }
  646. HashMap<StringName, String> function_code;
  647. //code for functions
  648. for (int i = 0; i < pnode->functions.size(); i++) {
  649. SL::FunctionNode *fnode = pnode->functions[i].function;
  650. function = fnode;
  651. current_func_name = fnode->name;
  652. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  653. function = nullptr;
  654. }
  655. //place functions in actual code
  656. HashSet<StringName> added_funcs_per_stage[STAGE_MAX];
  657. for (int i = 0; i < pnode->functions.size(); i++) {
  658. SL::FunctionNode *fnode = pnode->functions[i].function;
  659. function = fnode;
  660. current_func_name = fnode->name;
  661. if (p_actions.entry_point_stages.has(fnode->name)) {
  662. Stage stage = p_actions.entry_point_stages[fnode->name];
  663. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]);
  664. r_gen_code.code[fnode->name] = function_code[fnode->name];
  665. }
  666. function = nullptr;
  667. }
  668. //code+=dump_node_code(pnode->body,p_level);
  669. } break;
  670. case SL::Node::TYPE_STRUCT: {
  671. } break;
  672. case SL::Node::TYPE_FUNCTION: {
  673. } break;
  674. case SL::Node::TYPE_BLOCK: {
  675. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  676. //variables
  677. if (!bnode->single_statement) {
  678. code += _mktab(p_level - 1) + "{\n";
  679. }
  680. for (int i = 0; i < bnode->statements.size(); i++) {
  681. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  682. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  683. code += scode; //use directly
  684. if (bnode->use_comma_between_statements && i + 1 < bnode->statements.size()) {
  685. code += ",";
  686. }
  687. } else {
  688. code += _mktab(p_level) + scode + ";\n";
  689. }
  690. }
  691. if (!bnode->single_statement) {
  692. code += _mktab(p_level - 1) + "}\n";
  693. }
  694. } break;
  695. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  696. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  697. String declaration;
  698. declaration += _constr(vdnode->is_const);
  699. if (vdnode->datatype == SL::TYPE_STRUCT) {
  700. declaration += _mkid(vdnode->struct_name);
  701. } else {
  702. declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
  703. }
  704. declaration += " ";
  705. for (int i = 0; i < vdnode->declarations.size(); i++) {
  706. bool is_array = vdnode->declarations[i].size > 0;
  707. if (i > 0) {
  708. declaration += ",";
  709. }
  710. declaration += _mkid(vdnode->declarations[i].name);
  711. if (is_array) {
  712. declaration += "[";
  713. if (vdnode->declarations[i].size_expression != nullptr) {
  714. declaration += _dump_node_code(vdnode->declarations[i].size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  715. } else {
  716. declaration += itos(vdnode->declarations[i].size);
  717. }
  718. declaration += "]";
  719. }
  720. if (!is_array || vdnode->declarations[i].single_expression) {
  721. if (!vdnode->declarations[i].initializer.is_empty()) {
  722. declaration += "=";
  723. declaration += _dump_node_code(vdnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  724. }
  725. } else {
  726. int size = vdnode->declarations[i].initializer.size();
  727. if (size > 0) {
  728. declaration += "=";
  729. if (vdnode->datatype == SL::TYPE_STRUCT) {
  730. declaration += _mkid(vdnode->struct_name);
  731. } else {
  732. declaration += _typestr(vdnode->datatype);
  733. }
  734. declaration += "[";
  735. declaration += itos(size);
  736. declaration += "]";
  737. declaration += "(";
  738. for (int j = 0; j < size; j++) {
  739. if (j > 0) {
  740. declaration += ",";
  741. }
  742. declaration += _dump_node_code(vdnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  743. }
  744. declaration += ")";
  745. }
  746. }
  747. }
  748. code += declaration;
  749. } break;
  750. case SL::Node::TYPE_VARIABLE: {
  751. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  752. bool use_fragment_varying = false;
  753. if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  754. if (p_assigning) {
  755. if (shader->varyings.has(vnode->name)) {
  756. use_fragment_varying = true;
  757. }
  758. } else {
  759. if (fragment_varyings.has(vnode->name)) {
  760. use_fragment_varying = true;
  761. }
  762. }
  763. }
  764. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  765. *p_actions.write_flag_pointers[vnode->name] = true;
  766. }
  767. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  768. String define = p_default_actions.usage_defines[vnode->name];
  769. if (define.begins_with("@")) {
  770. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  771. }
  772. r_gen_code.defines.push_back(define);
  773. used_name_defines.insert(vnode->name);
  774. }
  775. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  776. *p_actions.usage_flag_pointers[vnode->name] = true;
  777. used_flag_pointers.insert(vnode->name);
  778. }
  779. if (p_default_actions.renames.has(vnode->name)) {
  780. code = p_default_actions.renames[vnode->name];
  781. } else {
  782. if (shader->uniforms.has(vnode->name)) {
  783. //its a uniform!
  784. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
  785. if (u.texture_order >= 0) {
  786. code = _mkid(vnode->name); //texture, use as is
  787. } else {
  788. //a scalar or vector
  789. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  790. code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
  791. //global variable, this means the code points to an index to the global table
  792. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  793. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  794. //instance variable, index it as such
  795. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  796. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  797. } else {
  798. //regular uniform, index from UBO
  799. code = actions.base_uniform_string + _mkid(vnode->name);
  800. }
  801. }
  802. } else {
  803. if (use_fragment_varying) {
  804. code = "frag_to_light.";
  805. }
  806. code += _mkid(vnode->name); //its something else (local var most likely) use as is
  807. }
  808. }
  809. if (vnode->name == time_name) {
  810. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  811. r_gen_code.uses_vertex_time = true;
  812. }
  813. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  814. r_gen_code.uses_fragment_time = true;
  815. }
  816. }
  817. } break;
  818. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  819. SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
  820. int sz = acnode->initializer.size();
  821. if (acnode->datatype == SL::TYPE_STRUCT) {
  822. code += _mkid(acnode->struct_name);
  823. } else {
  824. code += _typestr(acnode->datatype);
  825. }
  826. code += "[";
  827. code += itos(acnode->initializer.size());
  828. code += "]";
  829. code += "(";
  830. for (int i = 0; i < sz; i++) {
  831. code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  832. if (i != sz - 1) {
  833. code += ", ";
  834. }
  835. }
  836. code += ")";
  837. } break;
  838. case SL::Node::TYPE_ARRAY: {
  839. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  840. bool use_fragment_varying = false;
  841. if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
  842. if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) {
  843. use_fragment_varying = true;
  844. } else {
  845. if (p_assigning) {
  846. if (shader->varyings.has(anode->name)) {
  847. use_fragment_varying = true;
  848. }
  849. } else {
  850. if (fragment_varyings.has(anode->name)) {
  851. use_fragment_varying = true;
  852. }
  853. }
  854. }
  855. }
  856. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  857. *p_actions.write_flag_pointers[anode->name] = true;
  858. }
  859. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  860. String define = p_default_actions.usage_defines[anode->name];
  861. if (define.begins_with("@")) {
  862. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  863. }
  864. r_gen_code.defines.push_back(define);
  865. used_name_defines.insert(anode->name);
  866. }
  867. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  868. *p_actions.usage_flag_pointers[anode->name] = true;
  869. used_flag_pointers.insert(anode->name);
  870. }
  871. if (p_default_actions.renames.has(anode->name)) {
  872. code = p_default_actions.renames[anode->name];
  873. } else {
  874. if (shader->uniforms.has(anode->name)) {
  875. //its a uniform!
  876. const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name];
  877. if (u.texture_order >= 0) {
  878. code = _mkid(anode->name); //texture, use as is
  879. } else {
  880. //a scalar or vector
  881. if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  882. code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is
  883. //global variable, this means the code points to an index to the global table
  884. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  885. } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  886. //instance variable, index it as such
  887. code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")";
  888. code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
  889. } else {
  890. //regular uniform, index from UBO
  891. code = actions.base_uniform_string + _mkid(anode->name);
  892. }
  893. }
  894. } else {
  895. if (use_fragment_varying) {
  896. code = "frag_to_light.";
  897. }
  898. code += _mkid(anode->name);
  899. }
  900. }
  901. if (anode->call_expression != nullptr) {
  902. code += ".";
  903. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  904. } else if (anode->index_expression != nullptr) {
  905. code += "[";
  906. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  907. code += "]";
  908. } else if (anode->assign_expression != nullptr) {
  909. code += "=";
  910. code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  911. }
  912. if (anode->name == time_name) {
  913. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
  914. r_gen_code.uses_vertex_time = true;
  915. }
  916. if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
  917. r_gen_code.uses_fragment_time = true;
  918. }
  919. }
  920. } break;
  921. case SL::Node::TYPE_CONSTANT: {
  922. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  923. if (cnode->array_size == 0) {
  924. return get_constant_text(cnode->datatype, cnode->values);
  925. } else {
  926. if (cnode->get_datatype() == SL::TYPE_STRUCT) {
  927. code += _mkid(cnode->struct_name);
  928. } else {
  929. code += _typestr(cnode->datatype);
  930. }
  931. code += "[";
  932. code += itos(cnode->array_size);
  933. code += "]";
  934. code += "(";
  935. for (int i = 0; i < cnode->array_size; i++) {
  936. if (i > 0) {
  937. code += ",";
  938. } else {
  939. code += "";
  940. }
  941. code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  942. }
  943. code += ")";
  944. }
  945. } break;
  946. case SL::Node::TYPE_OPERATOR: {
  947. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  948. switch (onode->op) {
  949. case SL::OP_ASSIGN:
  950. case SL::OP_ASSIGN_ADD:
  951. case SL::OP_ASSIGN_SUB:
  952. case SL::OP_ASSIGN_MUL:
  953. case SL::OP_ASSIGN_DIV:
  954. case SL::OP_ASSIGN_SHIFT_LEFT:
  955. case SL::OP_ASSIGN_SHIFT_RIGHT:
  956. case SL::OP_ASSIGN_MOD:
  957. case SL::OP_ASSIGN_BIT_AND:
  958. case SL::OP_ASSIGN_BIT_OR:
  959. case SL::OP_ASSIGN_BIT_XOR:
  960. 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);
  961. break;
  962. case SL::OP_BIT_INVERT:
  963. case SL::OP_NEGATE:
  964. case SL::OP_NOT:
  965. case SL::OP_DECREMENT:
  966. case SL::OP_INCREMENT:
  967. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  968. break;
  969. case SL::OP_POST_DECREMENT:
  970. case SL::OP_POST_INCREMENT:
  971. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  972. break;
  973. case SL::OP_CALL:
  974. case SL::OP_STRUCT:
  975. case SL::OP_CONSTRUCT: {
  976. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  977. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  978. bool is_texture_func = false;
  979. bool is_screen_texture = false;
  980. if (onode->op == SL::OP_STRUCT) {
  981. code += _mkid(vnode->name);
  982. } else if (onode->op == SL::OP_CONSTRUCT) {
  983. code += String(vnode->name);
  984. } else {
  985. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  986. *p_actions.usage_flag_pointers[vnode->name] = true;
  987. used_flag_pointers.insert(vnode->name);
  988. }
  989. if (internal_functions.has(vnode->name)) {
  990. code += vnode->name;
  991. is_texture_func = texture_functions.has(vnode->name);
  992. } else if (p_default_actions.renames.has(vnode->name)) {
  993. code += p_default_actions.renames[vnode->name];
  994. } else {
  995. code += _mkid(vnode->name);
  996. }
  997. }
  998. code += "(";
  999. for (int i = 1; i < onode->arguments.size(); i++) {
  1000. if (i > 1) {
  1001. code += ", ";
  1002. }
  1003. String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1004. if (!RS::get_singleton()->is_low_end() && is_texture_func && i == 1) {
  1005. //need to map from texture to sampler in order to sample when using Vulkan GLSL
  1006. StringName texture_uniform;
  1007. bool correct_texture_uniform = false;
  1008. switch (onode->arguments[i]->type) {
  1009. case SL::Node::TYPE_VARIABLE: {
  1010. const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
  1011. texture_uniform = varnode->name;
  1012. correct_texture_uniform = true;
  1013. } break;
  1014. case SL::Node::TYPE_ARRAY: {
  1015. const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]);
  1016. texture_uniform = anode->name;
  1017. correct_texture_uniform = true;
  1018. } break;
  1019. default:
  1020. break;
  1021. }
  1022. if (correct_texture_uniform) {
  1023. is_screen_texture = (texture_uniform == "SCREEN_TEXTURE");
  1024. String sampler_name;
  1025. if (actions.custom_samplers.has(texture_uniform)) {
  1026. sampler_name = actions.custom_samplers[texture_uniform];
  1027. } else {
  1028. if (shader->uniforms.has(texture_uniform)) {
  1029. sampler_name = _get_sampler_name(shader->uniforms[texture_uniform].filter, shader->uniforms[texture_uniform].repeat);
  1030. } else {
  1031. bool found = false;
  1032. for (int j = 0; j < function->arguments.size(); j++) {
  1033. if (function->arguments[j].name == texture_uniform) {
  1034. if (function->arguments[j].tex_builtin_check) {
  1035. ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
  1036. sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
  1037. found = true;
  1038. break;
  1039. }
  1040. if (function->arguments[j].tex_argument_check) {
  1041. sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
  1042. found = true;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. if (!found) {
  1048. //function was most likely unused, so use anything (compiler will remove it anyway)
  1049. sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
  1050. }
  1051. }
  1052. }
  1053. code += ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()) + "(" + node_code + ", " + sampler_name + ")";
  1054. } else {
  1055. code += node_code;
  1056. }
  1057. } else {
  1058. code += node_code;
  1059. }
  1060. }
  1061. code += ")";
  1062. if (is_screen_texture && actions.apply_luminance_multiplier) {
  1063. code = "(" + code + " * vec4(vec3(sc_luminance_multiplier), 1.0))";
  1064. }
  1065. } break;
  1066. case SL::OP_INDEX: {
  1067. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1068. code += "[";
  1069. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1070. code += "]";
  1071. } break;
  1072. case SL::OP_SELECT_IF: {
  1073. code += "(";
  1074. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1075. code += "?";
  1076. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1077. code += ":";
  1078. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1079. code += ")";
  1080. } break;
  1081. case SL::OP_EMPTY: {
  1082. // Semicolon (or empty statement) - ignored.
  1083. } break;
  1084. default: {
  1085. if (p_use_scope) {
  1086. code += "(";
  1087. }
  1088. 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);
  1089. if (p_use_scope) {
  1090. code += ")";
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. } break;
  1096. case SL::Node::TYPE_CONTROL_FLOW: {
  1097. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  1098. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  1099. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1100. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1101. if (cfnode->blocks.size() == 2) {
  1102. code += _mktab(p_level) + "else\n";
  1103. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1104. }
  1105. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  1106. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1107. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1108. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  1109. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  1110. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1111. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  1112. code += _mktab(p_level) + "default:\n";
  1113. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1114. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  1115. code += _mktab(p_level) + "do";
  1116. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1117. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  1118. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  1119. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  1120. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1121. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  1122. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1123. String middle = _dump_node_code(cfnode->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1124. String right = _dump_node_code(cfnode->blocks[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1125. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  1126. code += _dump_node_code(cfnode->blocks[3], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  1127. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  1128. if (cfnode->expressions.size()) {
  1129. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  1130. } else {
  1131. code = "return;";
  1132. }
  1133. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  1134. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  1135. *p_actions.usage_flag_pointers["DISCARD"] = true;
  1136. used_flag_pointers.insert("DISCARD");
  1137. }
  1138. code = "discard;";
  1139. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  1140. code = "continue;";
  1141. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  1142. code = "break;";
  1143. }
  1144. } break;
  1145. case SL::Node::TYPE_MEMBER: {
  1146. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  1147. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  1148. if (mnode->index_expression != nullptr) {
  1149. code += "[";
  1150. code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  1151. code += "]";
  1152. } else if (mnode->assign_expression != nullptr) {
  1153. code += "=";
  1154. code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
  1155. } else if (mnode->call_expression != nullptr) {
  1156. code += ".";
  1157. code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  1158. }
  1159. } break;
  1160. }
  1161. return code;
  1162. }
  1163. ShaderLanguage::DataType ShaderCompiler::_get_variable_type(const StringName &p_type) {
  1164. RS::GlobalShaderUniformType gvt = RS::get_singleton()->global_shader_uniform_get_type(p_type);
  1165. return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt);
  1166. }
  1167. Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  1168. SL::ShaderCompileInfo info;
  1169. info.functions = ShaderTypes::get_singleton()->get_functions(p_mode);
  1170. info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode);
  1171. info.shader_types = ShaderTypes::get_singleton()->get_types();
  1172. info.global_shader_uniform_type_func = _get_variable_type;
  1173. Error err = parser.compile(p_code, info);
  1174. if (err != OK) {
  1175. Vector<ShaderLanguage::FilePosition> include_positions = parser.get_include_positions();
  1176. String current;
  1177. HashMap<String, Vector<String>> includes;
  1178. includes[""] = Vector<String>();
  1179. Vector<String> include_stack;
  1180. Vector<String> shader = p_code.split("\n");
  1181. // Reconstruct the files.
  1182. for (int i = 0; i < shader.size(); i++) {
  1183. String l = shader[i];
  1184. if (l.begins_with("@@>")) {
  1185. String inc_path = l.replace_first("@@>", "");
  1186. l = "#include \"" + inc_path + "\"";
  1187. includes[current].append("#include \"" + inc_path + "\""); // Restore the include directive
  1188. include_stack.push_back(current);
  1189. current = inc_path;
  1190. includes[inc_path] = Vector<String>();
  1191. } else if (l.begins_with("@@<")) {
  1192. if (include_stack.size()) {
  1193. current = include_stack[include_stack.size() - 1];
  1194. include_stack.resize(include_stack.size() - 1);
  1195. }
  1196. } else {
  1197. includes[current].push_back(l);
  1198. }
  1199. }
  1200. // Print the files.
  1201. for (const KeyValue<String, Vector<String>> &E : includes) {
  1202. if (E.key.is_empty()) {
  1203. if (p_path == "") {
  1204. print_line("--Main Shader--");
  1205. } else {
  1206. print_line("--" + p_path + "--");
  1207. }
  1208. } else {
  1209. print_line("--" + E.key + "--");
  1210. }
  1211. int err_line = -1;
  1212. for (int i = 0; i < include_positions.size(); i++) {
  1213. if (include_positions[i].file == E.key) {
  1214. err_line = include_positions[i].line;
  1215. }
  1216. }
  1217. const Vector<String> &V = E.value;
  1218. for (int i = 0; i < V.size(); i++) {
  1219. if (i == err_line - 1) {
  1220. // Mark the error line to be visible without having to look at
  1221. // the trace at the end.
  1222. print_line(vformat("E%4d-> %s", i + 1, V[i]));
  1223. } else {
  1224. print_line(vformat("%5d | %s", i + 1, V[i]));
  1225. }
  1226. }
  1227. }
  1228. String file;
  1229. int line;
  1230. if (include_positions.size() > 1) {
  1231. file = include_positions[include_positions.size() - 1].file;
  1232. line = include_positions[include_positions.size() - 1].line;
  1233. } else {
  1234. file = p_path;
  1235. line = parser.get_error_line();
  1236. }
  1237. _err_print_error(nullptr, file.utf8().get_data(), line, parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER);
  1238. return err;
  1239. }
  1240. r_gen_code.defines.clear();
  1241. r_gen_code.code.clear();
  1242. for (int i = 0; i < STAGE_MAX; i++) {
  1243. r_gen_code.stage_globals[i] = String();
  1244. }
  1245. r_gen_code.uses_fragment_time = false;
  1246. r_gen_code.uses_vertex_time = false;
  1247. r_gen_code.uses_global_textures = false;
  1248. used_name_defines.clear();
  1249. used_rmode_defines.clear();
  1250. used_flag_pointers.clear();
  1251. fragment_varyings.clear();
  1252. shader = parser.get_shader();
  1253. function = nullptr;
  1254. _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
  1255. return OK;
  1256. }
  1257. void ShaderCompiler::initialize(DefaultIdentifierActions p_actions) {
  1258. actions = p_actions;
  1259. time_name = "TIME";
  1260. List<String> func_list;
  1261. ShaderLanguage::get_builtin_funcs(&func_list);
  1262. for (const String &E : func_list) {
  1263. internal_functions.insert(E);
  1264. }
  1265. texture_functions.insert("texture");
  1266. texture_functions.insert("textureProj");
  1267. texture_functions.insert("textureLod");
  1268. texture_functions.insert("textureProjLod");
  1269. texture_functions.insert("textureGrad");
  1270. texture_functions.insert("textureGather");
  1271. texture_functions.insert("textureSize");
  1272. texture_functions.insert("textureQueryLod");
  1273. texture_functions.insert("textureQueryLevels");
  1274. texture_functions.insert("texelFetch");
  1275. }
  1276. ShaderCompiler::ShaderCompiler() {
  1277. }