shader_compiler_rd.cpp 58 KB

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