shader_compiler_gles3.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*************************************************************************/
  2. /* shader_compiler_gles3.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_gles3.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #define SL ShaderLanguage
  34. static String _mktab(int p_level) {
  35. String tb;
  36. for (int i = 0; i < p_level; i++) {
  37. tb += "\t";
  38. }
  39. return tb;
  40. }
  41. static String _typestr(SL::DataType p_type) {
  42. return ShaderLanguage::get_datatype_name(p_type);
  43. }
  44. static int _get_datatype_size(SL::DataType p_type) {
  45. switch (p_type) {
  46. case SL::TYPE_VOID: return 0;
  47. case SL::TYPE_BOOL: return 4;
  48. case SL::TYPE_BVEC2: return 8;
  49. case SL::TYPE_BVEC3: return 12;
  50. case SL::TYPE_BVEC4: return 16;
  51. case SL::TYPE_INT: return 4;
  52. case SL::TYPE_IVEC2: return 8;
  53. case SL::TYPE_IVEC3: return 12;
  54. case SL::TYPE_IVEC4: return 16;
  55. case SL::TYPE_UINT: return 4;
  56. case SL::TYPE_UVEC2: return 8;
  57. case SL::TYPE_UVEC3: return 12;
  58. case SL::TYPE_UVEC4: return 16;
  59. case SL::TYPE_FLOAT: return 4;
  60. case SL::TYPE_VEC2: return 8;
  61. case SL::TYPE_VEC3: return 12;
  62. case SL::TYPE_VEC4: return 16;
  63. case SL::TYPE_MAT2:
  64. return 32; //4 * 4 + 4 * 4
  65. case SL::TYPE_MAT3:
  66. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  67. case SL::TYPE_MAT4: return 64;
  68. case SL::TYPE_SAMPLER2D: return 16;
  69. case SL::TYPE_ISAMPLER2D: return 16;
  70. case SL::TYPE_USAMPLER2D: return 16;
  71. case SL::TYPE_SAMPLER2DARRAY: return 16;
  72. case SL::TYPE_ISAMPLER2DARRAY: return 16;
  73. case SL::TYPE_USAMPLER2DARRAY: return 16;
  74. case SL::TYPE_SAMPLER3D: return 16;
  75. case SL::TYPE_ISAMPLER3D: return 16;
  76. case SL::TYPE_USAMPLER3D: return 16;
  77. case SL::TYPE_SAMPLERCUBE: return 16;
  78. case SL::TYPE_SAMPLEREXT: return 16;
  79. }
  80. ERR_FAIL_V(0);
  81. }
  82. static int _get_datatype_alignment(SL::DataType p_type) {
  83. switch (p_type) {
  84. case SL::TYPE_VOID: return 0;
  85. case SL::TYPE_BOOL: return 4;
  86. case SL::TYPE_BVEC2: return 8;
  87. case SL::TYPE_BVEC3: return 16;
  88. case SL::TYPE_BVEC4: return 16;
  89. case SL::TYPE_INT: return 4;
  90. case SL::TYPE_IVEC2: return 8;
  91. case SL::TYPE_IVEC3: return 16;
  92. case SL::TYPE_IVEC4: return 16;
  93. case SL::TYPE_UINT: return 4;
  94. case SL::TYPE_UVEC2: return 8;
  95. case SL::TYPE_UVEC3: return 16;
  96. case SL::TYPE_UVEC4: return 16;
  97. case SL::TYPE_FLOAT: return 4;
  98. case SL::TYPE_VEC2: return 8;
  99. case SL::TYPE_VEC3: return 16;
  100. case SL::TYPE_VEC4: return 16;
  101. case SL::TYPE_MAT2: return 16;
  102. case SL::TYPE_MAT3: return 16;
  103. case SL::TYPE_MAT4: return 16;
  104. case SL::TYPE_SAMPLER2D: return 16;
  105. case SL::TYPE_ISAMPLER2D: return 16;
  106. case SL::TYPE_USAMPLER2D: return 16;
  107. case SL::TYPE_SAMPLER2DARRAY: return 16;
  108. case SL::TYPE_ISAMPLER2DARRAY: return 16;
  109. case SL::TYPE_USAMPLER2DARRAY: return 16;
  110. case SL::TYPE_SAMPLER3D: return 16;
  111. case SL::TYPE_ISAMPLER3D: return 16;
  112. case SL::TYPE_USAMPLER3D: return 16;
  113. case SL::TYPE_SAMPLERCUBE: return 16;
  114. case SL::TYPE_SAMPLEREXT: return 16;
  115. }
  116. ERR_FAIL_V(0);
  117. }
  118. static String _interpstr(SL::DataInterpolation p_interp) {
  119. switch (p_interp) {
  120. case SL::INTERPOLATION_FLAT: return "flat ";
  121. case SL::INTERPOLATION_SMOOTH: return "";
  122. }
  123. return "";
  124. }
  125. static String _prestr(SL::DataPrecision p_pres) {
  126. switch (p_pres) {
  127. case SL::PRECISION_LOWP: return "lowp ";
  128. case SL::PRECISION_MEDIUMP: return "mediump ";
  129. case SL::PRECISION_HIGHP: return "highp ";
  130. case SL::PRECISION_DEFAULT: return "";
  131. }
  132. return "";
  133. }
  134. static String _qualstr(SL::ArgumentQualifier p_qual) {
  135. switch (p_qual) {
  136. case SL::ARGUMENT_QUALIFIER_IN: return "";
  137. case SL::ARGUMENT_QUALIFIER_OUT: return "out ";
  138. case SL::ARGUMENT_QUALIFIER_INOUT: return "inout ";
  139. }
  140. return "";
  141. }
  142. static String _opstr(SL::Operator p_op) {
  143. return SL::get_operator_text(p_op);
  144. }
  145. static String _mkid(const String &p_id) {
  146. String id = "m_" + p_id.replace("__", "_dus_");
  147. return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
  148. }
  149. static String f2sp0(float p_float) {
  150. String num = rtoss(p_float);
  151. if (num.find(".") == -1 && num.find("e") == -1) {
  152. num += ".0";
  153. }
  154. return num;
  155. }
  156. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  157. switch (p_type) {
  158. case SL::TYPE_BOOL: return p_values[0].boolean ? "true" : "false";
  159. case SL::TYPE_BVEC2:
  160. case SL::TYPE_BVEC3:
  161. case SL::TYPE_BVEC4: {
  162. String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
  163. for (int i = 0; i < p_values.size(); i++) {
  164. if (i > 0)
  165. text += ",";
  166. text += p_values[i].boolean ? "true" : "false";
  167. }
  168. text += ")";
  169. return text;
  170. }
  171. case SL::TYPE_INT: return itos(p_values[0].sint);
  172. case SL::TYPE_IVEC2:
  173. case SL::TYPE_IVEC3:
  174. case SL::TYPE_IVEC4: {
  175. String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
  176. for (int i = 0; i < p_values.size(); i++) {
  177. if (i > 0)
  178. text += ",";
  179. text += itos(p_values[i].sint);
  180. }
  181. text += ")";
  182. return text;
  183. } break;
  184. case SL::TYPE_UINT: return itos(p_values[0].uint) + "u";
  185. case SL::TYPE_UVEC2:
  186. case SL::TYPE_UVEC3:
  187. case SL::TYPE_UVEC4: {
  188. String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
  189. for (int i = 0; i < p_values.size(); i++) {
  190. if (i > 0)
  191. text += ",";
  192. text += itos(p_values[i].uint) + "u";
  193. }
  194. text += ")";
  195. return text;
  196. } break;
  197. case SL::TYPE_FLOAT: return f2sp0(p_values[0].real);
  198. case SL::TYPE_VEC2:
  199. case SL::TYPE_VEC3:
  200. case SL::TYPE_VEC4: {
  201. String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
  202. for (int i = 0; i < p_values.size(); i++) {
  203. if (i > 0)
  204. text += ",";
  205. text += f2sp0(p_values[i].real);
  206. }
  207. text += ")";
  208. return text;
  209. } break;
  210. case SL::TYPE_MAT2:
  211. case SL::TYPE_MAT3:
  212. case SL::TYPE_MAT4: {
  213. String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
  214. for (int i = 0; i < p_values.size(); i++) {
  215. if (i > 0)
  216. text += ",";
  217. text += f2sp0(p_values[i].real);
  218. }
  219. text += ")";
  220. return text;
  221. } break;
  222. default: ERR_FAIL_V(String());
  223. }
  224. }
  225. void ShaderCompilerGLES3::_dump_function_deps(SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added) {
  226. int fidx = -1;
  227. for (int i = 0; i < p_node->functions.size(); i++) {
  228. if (p_node->functions[i].name == p_for_func) {
  229. fidx = i;
  230. break;
  231. }
  232. }
  233. ERR_FAIL_COND(fidx == -1);
  234. for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) {
  235. if (added.has(E->get())) {
  236. continue; //was added already
  237. }
  238. _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added);
  239. SL::FunctionNode *fnode = NULL;
  240. for (int i = 0; i < p_node->functions.size(); i++) {
  241. if (p_node->functions[i].name == E->get()) {
  242. fnode = p_node->functions[i].function;
  243. break;
  244. }
  245. }
  246. ERR_FAIL_COND(!fnode);
  247. r_to_add += "\n";
  248. String header;
  249. header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "(";
  250. for (int i = 0; i < fnode->arguments.size(); i++) {
  251. if (i > 0)
  252. header += ", ";
  253. header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
  254. }
  255. header += ")\n";
  256. r_to_add += header;
  257. r_to_add += p_func_code[E->get()];
  258. added.insert(E->get());
  259. }
  260. }
  261. String ShaderCompilerGLES3::_dump_node_code(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) {
  262. String code;
  263. switch (p_node->type) {
  264. case SL::Node::TYPE_SHADER: {
  265. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  266. for (int i = 0; i < pnode->render_modes.size(); i++) {
  267. if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
  268. r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]].utf8());
  269. used_rmode_defines.insert(pnode->render_modes[i]);
  270. }
  271. if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
  272. *p_actions.render_mode_flags[pnode->render_modes[i]] = true;
  273. }
  274. if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
  275. Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
  276. *p.first = p.second;
  277. }
  278. }
  279. int max_texture_uniforms = 0;
  280. int max_uniforms = 0;
  281. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  282. if (SL::is_sampler_type(E->get().type))
  283. max_texture_uniforms++;
  284. else
  285. max_uniforms++;
  286. }
  287. r_gen_code.texture_uniforms.resize(max_texture_uniforms);
  288. r_gen_code.texture_hints.resize(max_texture_uniforms);
  289. r_gen_code.texture_types.resize(max_texture_uniforms);
  290. Vector<int> uniform_sizes;
  291. Vector<int> uniform_alignments;
  292. Vector<StringName> uniform_defines;
  293. uniform_sizes.resize(max_uniforms);
  294. uniform_alignments.resize(max_uniforms);
  295. uniform_defines.resize(max_uniforms);
  296. bool uses_uniforms = false;
  297. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  298. String ucode;
  299. if (SL::is_sampler_type(E->get().type)) {
  300. ucode = "uniform ";
  301. }
  302. ucode += _prestr(E->get().precision);
  303. ucode += _typestr(E->get().type);
  304. ucode += " " + _mkid(E->key());
  305. ucode += ";\n";
  306. if (SL::is_sampler_type(E->get().type)) {
  307. r_gen_code.vertex_global += ucode;
  308. r_gen_code.fragment_global += ucode;
  309. r_gen_code.texture_uniforms.write[E->get().texture_order] = _mkid(E->key());
  310. r_gen_code.texture_hints.write[E->get().texture_order] = E->get().hint;
  311. r_gen_code.texture_types.write[E->get().texture_order] = E->get().type;
  312. } else {
  313. if (!uses_uniforms) {
  314. r_gen_code.defines.push_back(String("#define USE_MATERIAL\n").ascii());
  315. uses_uniforms = true;
  316. }
  317. uniform_defines.write[E->get().order] = ucode;
  318. uniform_sizes.write[E->get().order] = _get_datatype_size(E->get().type);
  319. uniform_alignments.write[E->get().order] = _get_datatype_alignment(E->get().type);
  320. }
  321. p_actions.uniforms->insert(E->key(), E->get());
  322. }
  323. for (int i = 0; i < max_uniforms; i++) {
  324. r_gen_code.uniforms += uniform_defines[i];
  325. }
  326. // add up
  327. int offset = 0;
  328. for (int i = 0; i < uniform_sizes.size(); i++) {
  329. int align = offset % uniform_alignments[i];
  330. if (align != 0) {
  331. offset += uniform_alignments[i] - align;
  332. }
  333. r_gen_code.uniform_offsets.push_back(offset);
  334. offset += uniform_sizes[i];
  335. }
  336. r_gen_code.uniform_total_size = offset;
  337. if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
  338. r_gen_code.uniform_total_size += r_gen_code.uniform_total_size % 16;
  339. }
  340. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  341. String vcode;
  342. String interp_mode = _interpstr(E->get().interpolation);
  343. vcode += _prestr(E->get().precision);
  344. vcode += _typestr(E->get().type);
  345. vcode += " " + _mkid(E->key());
  346. if (E->get().array_size > 0) {
  347. vcode += "[";
  348. vcode += itos(E->get().array_size);
  349. vcode += "]";
  350. }
  351. vcode += ";\n";
  352. r_gen_code.vertex_global += interp_mode + "out " + vcode;
  353. r_gen_code.fragment_global += interp_mode + "in " + vcode;
  354. }
  355. for (int i = 0; i < pnode->vconstants.size(); i++) {
  356. String gcode;
  357. gcode += "const ";
  358. gcode += _prestr(pnode->vconstants[i].precision);
  359. gcode += _typestr(pnode->vconstants[i].type);
  360. gcode += " " + _mkid(String(pnode->vconstants[i].name));
  361. gcode += "=";
  362. gcode += _dump_node_code(pnode->vconstants[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  363. gcode += ";\n";
  364. r_gen_code.vertex_global += gcode;
  365. r_gen_code.fragment_global += gcode;
  366. }
  367. Map<StringName, String> function_code;
  368. //code for functions
  369. for (int i = 0; i < pnode->functions.size(); i++) {
  370. SL::FunctionNode *fnode = pnode->functions[i].function;
  371. current_func_name = fnode->name;
  372. function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  373. }
  374. //place functions in actual code
  375. Set<StringName> added_vtx;
  376. Set<StringName> added_fragment; //share for light
  377. for (int i = 0; i < pnode->functions.size(); i++) {
  378. SL::FunctionNode *fnode = pnode->functions[i].function;
  379. current_func_name = fnode->name;
  380. if (fnode->name == vertex_name) {
  381. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx);
  382. r_gen_code.vertex = function_code[vertex_name];
  383. }
  384. if (fnode->name == fragment_name) {
  385. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  386. r_gen_code.fragment = function_code[fragment_name];
  387. }
  388. if (fnode->name == light_name) {
  389. _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment);
  390. r_gen_code.light = function_code[light_name];
  391. }
  392. }
  393. //code+=dump_node_code(pnode->body,p_level);
  394. } break;
  395. case SL::Node::TYPE_FUNCTION: {
  396. } break;
  397. case SL::Node::TYPE_BLOCK: {
  398. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  399. //variables
  400. if (!bnode->single_statement) {
  401. code += _mktab(p_level - 1) + "{\n";
  402. }
  403. for (int i = 0; i < bnode->statements.size(); i++) {
  404. String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  405. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) {
  406. code += scode; //use directly
  407. } else {
  408. code += _mktab(p_level) + scode + ";\n";
  409. }
  410. }
  411. if (!bnode->single_statement) {
  412. code += _mktab(p_level - 1) + "}\n";
  413. }
  414. } break;
  415. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  416. SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
  417. String declaration;
  418. if (vdnode->is_const) {
  419. declaration += "const ";
  420. }
  421. declaration += _prestr(vdnode->precision);
  422. declaration += _typestr(vdnode->datatype);
  423. for (int i = 0; i < vdnode->declarations.size(); i++) {
  424. if (i > 0) {
  425. declaration += ",";
  426. } else {
  427. declaration += " ";
  428. }
  429. declaration += _mkid(vdnode->declarations[i].name);
  430. if (vdnode->declarations[i].initializer) {
  431. declaration += "=";
  432. declaration += _dump_node_code(vdnode->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  433. }
  434. }
  435. code += declaration;
  436. } break;
  437. case SL::Node::TYPE_VARIABLE: {
  438. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  439. if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
  440. *p_actions.write_flag_pointers[vnode->name] = true;
  441. }
  442. if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
  443. String define = p_default_actions.usage_defines[vnode->name];
  444. if (define.begins_with("@")) {
  445. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  446. }
  447. r_gen_code.defines.push_back(define.utf8());
  448. used_name_defines.insert(vnode->name);
  449. }
  450. if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
  451. *p_actions.usage_flag_pointers[vnode->name] = true;
  452. used_flag_pointers.insert(vnode->name);
  453. }
  454. if (p_default_actions.renames.has(vnode->name))
  455. code = p_default_actions.renames[vnode->name];
  456. else
  457. code = _mkid(vnode->name);
  458. if (vnode->name == time_name) {
  459. if (current_func_name == vertex_name) {
  460. r_gen_code.uses_vertex_time = true;
  461. }
  462. if (current_func_name == fragment_name || current_func_name == light_name) {
  463. r_gen_code.uses_fragment_time = true;
  464. }
  465. }
  466. } break;
  467. case SL::Node::TYPE_ARRAY_DECLARATION: {
  468. SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
  469. String declaration;
  470. if (adnode->is_const) {
  471. declaration += "const ";
  472. }
  473. declaration += _prestr(adnode->precision);
  474. declaration += _typestr(adnode->datatype);
  475. for (int i = 0; i < adnode->declarations.size(); i++) {
  476. if (i > 0) {
  477. declaration += ",";
  478. } else {
  479. declaration += " ";
  480. }
  481. declaration += _mkid(adnode->declarations[i].name);
  482. declaration += "[";
  483. declaration += itos(adnode->declarations[i].size);
  484. declaration += "]";
  485. int sz = adnode->declarations[i].initializer.size();
  486. if (sz > 0) {
  487. declaration += "=";
  488. declaration += _typestr(adnode->datatype);
  489. declaration += "[";
  490. declaration += itos(sz);
  491. declaration += "]";
  492. declaration += "(";
  493. for (int j = 0; j < sz; j++) {
  494. declaration += _dump_node_code(adnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  495. if (j != sz - 1) {
  496. declaration += ", ";
  497. }
  498. }
  499. declaration += ")";
  500. }
  501. }
  502. code += declaration;
  503. } break;
  504. case SL::Node::TYPE_ARRAY: {
  505. SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
  506. if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
  507. *p_actions.write_flag_pointers[anode->name] = true;
  508. }
  509. if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
  510. String define = p_default_actions.usage_defines[anode->name];
  511. if (define.begins_with("@")) {
  512. define = p_default_actions.usage_defines[define.substr(1, define.length())];
  513. }
  514. r_gen_code.defines.push_back(define.utf8());
  515. used_name_defines.insert(anode->name);
  516. }
  517. if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
  518. *p_actions.usage_flag_pointers[anode->name] = true;
  519. used_flag_pointers.insert(anode->name);
  520. }
  521. if (p_default_actions.renames.has(anode->name))
  522. code = p_default_actions.renames[anode->name];
  523. else
  524. code = _mkid(anode->name);
  525. if (anode->call_expression != NULL) {
  526. code += ".";
  527. code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
  528. }
  529. if (anode->index_expression != NULL) {
  530. code += "[";
  531. code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  532. code += "]";
  533. }
  534. if (anode->name == time_name) {
  535. if (current_func_name == vertex_name) {
  536. r_gen_code.uses_vertex_time = true;
  537. }
  538. if (current_func_name == fragment_name || current_func_name == light_name) {
  539. r_gen_code.uses_fragment_time = true;
  540. }
  541. }
  542. } break;
  543. case SL::Node::TYPE_CONSTANT: {
  544. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  545. return get_constant_text(cnode->datatype, cnode->values);
  546. } break;
  547. case SL::Node::TYPE_OPERATOR: {
  548. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  549. switch (onode->op) {
  550. case SL::OP_ASSIGN:
  551. case SL::OP_ASSIGN_ADD:
  552. case SL::OP_ASSIGN_SUB:
  553. case SL::OP_ASSIGN_MUL:
  554. case SL::OP_ASSIGN_DIV:
  555. case SL::OP_ASSIGN_SHIFT_LEFT:
  556. case SL::OP_ASSIGN_SHIFT_RIGHT:
  557. case SL::OP_ASSIGN_MOD:
  558. case SL::OP_ASSIGN_BIT_AND:
  559. case SL::OP_ASSIGN_BIT_OR:
  560. case SL::OP_ASSIGN_BIT_XOR:
  561. 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);
  562. break;
  563. case SL::OP_BIT_INVERT:
  564. case SL::OP_NEGATE:
  565. case SL::OP_NOT:
  566. case SL::OP_DECREMENT:
  567. case SL::OP_INCREMENT:
  568. code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  569. break;
  570. case SL::OP_POST_DECREMENT:
  571. case SL::OP_POST_INCREMENT:
  572. code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
  573. break;
  574. case SL::OP_CALL:
  575. case SL::OP_CONSTRUCT: {
  576. ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String());
  577. SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0];
  578. if (onode->op == SL::OP_CONSTRUCT) {
  579. code += String(vnode->name);
  580. } else {
  581. if (internal_functions.has(vnode->name)) {
  582. code += vnode->name;
  583. } else if (p_default_actions.renames.has(vnode->name)) {
  584. code += p_default_actions.renames[vnode->name];
  585. } else {
  586. code += _mkid(vnode->name);
  587. }
  588. }
  589. code += "(";
  590. for (int i = 1; i < onode->arguments.size(); i++) {
  591. if (i > 1)
  592. code += ", ";
  593. code += _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  594. }
  595. code += ")";
  596. } break;
  597. case SL::OP_INDEX: {
  598. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  599. code += "[";
  600. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  601. code += "]";
  602. } break;
  603. case SL::OP_SELECT_IF: {
  604. code += "(";
  605. code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  606. code += "?";
  607. code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  608. code += ":";
  609. code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  610. code += ")";
  611. } break;
  612. default: {
  613. if (p_use_scope) {
  614. code += "(";
  615. }
  616. 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);
  617. if (p_use_scope) {
  618. code += ")";
  619. }
  620. break;
  621. }
  622. }
  623. } break;
  624. case SL::Node::TYPE_CONTROL_FLOW: {
  625. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  626. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  627. code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  628. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  629. if (cfnode->blocks.size() == 2) {
  630. code += _mktab(p_level) + "else\n";
  631. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  632. }
  633. } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
  634. code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  635. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  636. } else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
  637. code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
  638. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  639. } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
  640. code += _mktab(p_level) + "default:\n";
  641. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  642. } else if (cfnode->flow_op == SL::FLOW_OP_DO) {
  643. code += _mktab(p_level) + "do";
  644. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  645. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
  646. } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
  647. code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
  648. code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  649. } else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
  650. String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  651. String middle = _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  652. String right = _dump_node_code(cfnode->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
  653. code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
  654. code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
  655. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  656. if (cfnode->expressions.size()) {
  657. code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
  658. } else {
  659. code = "return;";
  660. }
  661. } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
  662. if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
  663. *p_actions.usage_flag_pointers["DISCARD"] = true;
  664. used_flag_pointers.insert("DISCARD");
  665. }
  666. code = "discard;";
  667. } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
  668. code = "continue;";
  669. } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
  670. code = "break;";
  671. }
  672. } break;
  673. case SL::Node::TYPE_MEMBER: {
  674. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  675. code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
  676. } break;
  677. }
  678. return code;
  679. }
  680. Error ShaderCompilerGLES3::compile(VS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
  681. Error err = parser.compile(p_code, ShaderTypes::get_singleton()->get_functions(p_mode), ShaderTypes::get_singleton()->get_modes(p_mode), ShaderTypes::get_singleton()->get_types());
  682. if (err != OK) {
  683. Vector<String> shader = p_code.split("\n");
  684. for (int i = 0; i < shader.size(); i++) {
  685. print_line(itos(i + 1) + " " + shader[i]);
  686. }
  687. _err_print_error(NULL, p_path.utf8().get_data(), parser.get_error_line(), parser.get_error_text().utf8().get_data(), ERR_HANDLER_SHADER);
  688. return err;
  689. }
  690. r_gen_code.defines.clear();
  691. r_gen_code.vertex = String();
  692. r_gen_code.vertex_global = String();
  693. r_gen_code.fragment = String();
  694. r_gen_code.fragment_global = String();
  695. r_gen_code.light = String();
  696. r_gen_code.uses_fragment_time = false;
  697. r_gen_code.uses_vertex_time = false;
  698. used_name_defines.clear();
  699. used_rmode_defines.clear();
  700. used_flag_pointers.clear();
  701. _dump_node_code(parser.get_shader(), 1, r_gen_code, *p_actions, actions[p_mode], false);
  702. if (r_gen_code.uniform_total_size) { //uniforms used?
  703. int md = sizeof(float) * 4;
  704. if (r_gen_code.uniform_total_size % md) {
  705. r_gen_code.uniform_total_size += md - (r_gen_code.uniform_total_size % md);
  706. }
  707. r_gen_code.uniform_total_size += md; //pad just in case
  708. }
  709. return OK;
  710. }
  711. ShaderCompilerGLES3::ShaderCompilerGLES3() {
  712. /** CANVAS ITEM SHADER **/
  713. actions[VS::SHADER_CANVAS_ITEM].renames["VERTEX"] = "outvec.xy";
  714. actions[VS::SHADER_CANVAS_ITEM].renames["UV"] = "uv";
  715. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "point_size";
  716. actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix";
  717. actions[VS::SHADER_CANVAS_ITEM].renames["PROJECTION_MATRIX"] = "projection_matrix";
  718. actions[VS::SHADER_CANVAS_ITEM].renames["EXTRA_MATRIX"] = "extra_matrix";
  719. actions[VS::SHADER_CANVAS_ITEM].renames["TIME"] = "time";
  720. actions[VS::SHADER_CANVAS_ITEM].renames["AT_LIGHT_PASS"] = "at_light_pass";
  721. actions[VS::SHADER_CANVAS_ITEM].renames["INSTANCE_CUSTOM"] = "instance_custom";
  722. actions[VS::SHADER_CANVAS_ITEM].renames["COLOR"] = "color";
  723. actions[VS::SHADER_CANVAS_ITEM].renames["MODULATE"] = "final_modulate_alias";
  724. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL"] = "normal";
  725. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP"] = "normal_map";
  726. actions[VS::SHADER_CANVAS_ITEM].renames["NORMALMAP_DEPTH"] = "normal_depth";
  727. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE"] = "color_texture";
  728. actions[VS::SHADER_CANVAS_ITEM].renames["TEXTURE_PIXEL_SIZE"] = "color_texpixel_size";
  729. actions[VS::SHADER_CANVAS_ITEM].renames["NORMAL_TEXTURE"] = "normal_texture";
  730. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_UV"] = "screen_uv";
  731. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_TEXTURE"] = "screen_texture";
  732. actions[VS::SHADER_CANVAS_ITEM].renames["SCREEN_PIXEL_SIZE"] = "screen_pixel_size";
  733. actions[VS::SHADER_CANVAS_ITEM].renames["FRAGCOORD"] = "gl_FragCoord";
  734. actions[VS::SHADER_CANVAS_ITEM].renames["POINT_COORD"] = "gl_PointCoord";
  735. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_VEC"] = "light_vec";
  736. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_HEIGHT"] = "light_height";
  737. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_COLOR"] = "light_color";
  738. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv";
  739. actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light";
  740. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color";
  741. actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_VEC"] = "shadow_vec";
  742. actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n";
  743. actions[VS::SHADER_CANVAS_ITEM].usage_defines["MODULATE"] = "#define MODULATE_USED\n";
  744. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  745. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  746. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_PIXEL_SIZE"] = "@SCREEN_UV";
  747. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n";
  748. actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
  749. actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  750. actions[VS::SHADER_CANVAS_ITEM].usage_defines["SHADOW_VEC"] = "#define SHADOW_VEC_USED\n";
  751. actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  752. /** SPATIAL SHADER **/
  753. actions[VS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
  754. actions[VS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix";
  755. actions[VS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix";
  756. actions[VS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix";
  757. actions[VS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  758. actions[VS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview";
  759. actions[VS::SHADER_SPATIAL].renames["VERTEX"] = "vertex.xyz";
  760. actions[VS::SHADER_SPATIAL].renames["NORMAL"] = "normal";
  761. actions[VS::SHADER_SPATIAL].renames["TANGENT"] = "tangent";
  762. actions[VS::SHADER_SPATIAL].renames["BINORMAL"] = "binormal";
  763. actions[VS::SHADER_SPATIAL].renames["POSITION"] = "position";
  764. actions[VS::SHADER_SPATIAL].renames["UV"] = "uv_interp";
  765. actions[VS::SHADER_SPATIAL].renames["UV2"] = "uv2_interp";
  766. actions[VS::SHADER_SPATIAL].renames["COLOR"] = "color_interp";
  767. actions[VS::SHADER_SPATIAL].renames["POINT_SIZE"] = "point_size";
  768. actions[VS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "gl_InstanceID";
  769. //builtins
  770. actions[VS::SHADER_SPATIAL].renames["TIME"] = "time";
  771. actions[VS::SHADER_SPATIAL].renames["VIEWPORT_SIZE"] = "viewport_size";
  772. actions[VS::SHADER_SPATIAL].renames["FRAGCOORD"] = "gl_FragCoord";
  773. actions[VS::SHADER_SPATIAL].renames["FRONT_FACING"] = "gl_FrontFacing";
  774. actions[VS::SHADER_SPATIAL].renames["NORMALMAP"] = "normalmap";
  775. actions[VS::SHADER_SPATIAL].renames["NORMALMAP_DEPTH"] = "normaldepth";
  776. actions[VS::SHADER_SPATIAL].renames["ALBEDO"] = "albedo";
  777. actions[VS::SHADER_SPATIAL].renames["ALPHA"] = "alpha";
  778. actions[VS::SHADER_SPATIAL].renames["METALLIC"] = "metallic";
  779. actions[VS::SHADER_SPATIAL].renames["SPECULAR"] = "specular";
  780. actions[VS::SHADER_SPATIAL].renames["ROUGHNESS"] = "roughness";
  781. actions[VS::SHADER_SPATIAL].renames["RIM"] = "rim";
  782. actions[VS::SHADER_SPATIAL].renames["RIM_TINT"] = "rim_tint";
  783. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT"] = "clearcoat";
  784. actions[VS::SHADER_SPATIAL].renames["CLEARCOAT_GLOSS"] = "clearcoat_gloss";
  785. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY"] = "anisotropy";
  786. actions[VS::SHADER_SPATIAL].renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  787. actions[VS::SHADER_SPATIAL].renames["SSS_STRENGTH"] = "sss_strength";
  788. actions[VS::SHADER_SPATIAL].renames["TRANSMISSION"] = "transmission";
  789. actions[VS::SHADER_SPATIAL].renames["AO"] = "ao";
  790. actions[VS::SHADER_SPATIAL].renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  791. actions[VS::SHADER_SPATIAL].renames["EMISSION"] = "emission";
  792. actions[VS::SHADER_SPATIAL].renames["POINT_COORD"] = "gl_PointCoord";
  793. actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
  794. actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
  795. actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
  796. actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer";
  797. actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
  798. actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
  799. actions[VS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  800. //for light
  801. actions[VS::SHADER_SPATIAL].renames["VIEW"] = "view";
  802. actions[VS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color";
  803. actions[VS::SHADER_SPATIAL].renames["LIGHT"] = "light";
  804. actions[VS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation";
  805. actions[VS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light";
  806. actions[VS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light";
  807. actions[VS::SHADER_SPATIAL].usage_defines["TANGENT"] = "#define ENABLE_TANGENT_INTERP\n";
  808. actions[VS::SHADER_SPATIAL].usage_defines["BINORMAL"] = "@TANGENT";
  809. actions[VS::SHADER_SPATIAL].usage_defines["RIM"] = "#define LIGHT_USE_RIM\n";
  810. actions[VS::SHADER_SPATIAL].usage_defines["RIM_TINT"] = "@RIM";
  811. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT"] = "#define LIGHT_USE_CLEARCOAT\n";
  812. actions[VS::SHADER_SPATIAL].usage_defines["CLEARCOAT_GLOSS"] = "@CLEARCOAT";
  813. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY"] = "#define LIGHT_USE_ANISOTROPY\n";
  814. actions[VS::SHADER_SPATIAL].usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  815. actions[VS::SHADER_SPATIAL].usage_defines["AO"] = "#define ENABLE_AO\n";
  816. actions[VS::SHADER_SPATIAL].usage_defines["AO_LIGHT_AFFECT"] = "#define ENABLE_AO\n";
  817. actions[VS::SHADER_SPATIAL].usage_defines["UV"] = "#define ENABLE_UV_INTERP\n";
  818. actions[VS::SHADER_SPATIAL].usage_defines["UV2"] = "#define ENABLE_UV2_INTERP\n";
  819. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP"] = "#define ENABLE_NORMALMAP\n";
  820. actions[VS::SHADER_SPATIAL].usage_defines["NORMALMAP_DEPTH"] = "@NORMALMAP";
  821. actions[VS::SHADER_SPATIAL].usage_defines["COLOR"] = "#define ENABLE_COLOR_INTERP\n";
  822. actions[VS::SHADER_SPATIAL].usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  823. actions[VS::SHADER_SPATIAL].usage_defines["ALPHA_SCISSOR"] = "#define ALPHA_SCISSOR_USED\n";
  824. actions[VS::SHADER_SPATIAL].usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  825. actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  826. actions[VS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
  827. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  828. actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  829. actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  830. actions[VS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  831. actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  832. actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  833. actions[VS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  834. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  835. actions[VS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  836. bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley");
  837. if (!force_lambert) {
  838. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  839. }
  840. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n";
  841. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  842. actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  843. bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx");
  844. if (!force_blinn) {
  845. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  846. } else {
  847. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n";
  848. }
  849. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n";
  850. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n";
  851. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  852. actions[VS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  853. actions[VS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  854. actions[VS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  855. actions[VS::SHADER_SPATIAL].render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  856. /* PARTICLES SHADER */
  857. actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color";
  858. actions[VS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz";
  859. actions[VS::SHADER_PARTICLES].renames["MASS"] = "mass";
  860. actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active";
  861. actions[VS::SHADER_PARTICLES].renames["RESTART"] = "restart";
  862. actions[VS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom";
  863. actions[VS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform";
  864. actions[VS::SHADER_PARTICLES].renames["TIME"] = "time";
  865. actions[VS::SHADER_PARTICLES].renames["LIFETIME"] = "lifetime";
  866. actions[VS::SHADER_PARTICLES].renames["DELTA"] = "local_delta";
  867. actions[VS::SHADER_PARTICLES].renames["NUMBER"] = "particle_number";
  868. actions[VS::SHADER_PARTICLES].renames["INDEX"] = "index";
  869. actions[VS::SHADER_PARTICLES].renames["GRAVITY"] = "current_gravity";
  870. actions[VS::SHADER_PARTICLES].renames["EMISSION_TRANSFORM"] = "emission_transform";
  871. actions[VS::SHADER_PARTICLES].renames["RANDOM_SEED"] = "random_seed";
  872. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  873. actions[VS::SHADER_PARTICLES].render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  874. actions[VS::SHADER_PARTICLES].render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  875. vertex_name = "vertex";
  876. fragment_name = "fragment";
  877. light_name = "light";
  878. time_name = "TIME";
  879. List<String> func_list;
  880. ShaderLanguage::get_builtin_funcs(&func_list);
  881. for (List<String>::Element *E = func_list.front(); E; E = E->next()) {
  882. internal_functions.insert(E->get());
  883. }
  884. }