shader_compiler_gles3.cpp 36 KB

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