shader_compiler_rd.cpp 55 KB

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