shader_compiler_rd.cpp 57 KB

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