shader_language.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /**************************************************************************/
  2. /* shader_language.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef SHADER_LANGUAGE_H
  31. #define SHADER_LANGUAGE_H
  32. #include "core/object/script_language.h"
  33. #include "core/string/string_name.h"
  34. #include "core/string/ustring.h"
  35. #include "core/templates/list.h"
  36. #include "core/templates/rb_map.h"
  37. #include "core/typedefs.h"
  38. #include "core/variant/variant.h"
  39. #include "scene/resources/shader_include.h"
  40. #ifdef DEBUG_ENABLED
  41. #include "shader_warnings.h"
  42. #endif // DEBUG_ENABLED
  43. class ShaderLanguage {
  44. public:
  45. struct TkPos {
  46. int char_idx;
  47. int tk_line;
  48. };
  49. enum TokenType {
  50. TK_EMPTY,
  51. TK_IDENTIFIER,
  52. TK_TRUE,
  53. TK_FALSE,
  54. TK_FLOAT_CONSTANT,
  55. TK_INT_CONSTANT,
  56. TK_UINT_CONSTANT,
  57. TK_TYPE_VOID,
  58. TK_TYPE_BOOL,
  59. TK_TYPE_BVEC2,
  60. TK_TYPE_BVEC3,
  61. TK_TYPE_BVEC4,
  62. TK_TYPE_INT,
  63. TK_TYPE_IVEC2,
  64. TK_TYPE_IVEC3,
  65. TK_TYPE_IVEC4,
  66. TK_TYPE_UINT,
  67. TK_TYPE_UVEC2,
  68. TK_TYPE_UVEC3,
  69. TK_TYPE_UVEC4,
  70. TK_TYPE_FLOAT,
  71. TK_TYPE_VEC2,
  72. TK_TYPE_VEC3,
  73. TK_TYPE_VEC4,
  74. TK_TYPE_MAT2,
  75. TK_TYPE_MAT3,
  76. TK_TYPE_MAT4,
  77. TK_TYPE_SAMPLER2D,
  78. TK_TYPE_ISAMPLER2D,
  79. TK_TYPE_USAMPLER2D,
  80. TK_TYPE_SAMPLER2DARRAY,
  81. TK_TYPE_ISAMPLER2DARRAY,
  82. TK_TYPE_USAMPLER2DARRAY,
  83. TK_TYPE_SAMPLER3D,
  84. TK_TYPE_ISAMPLER3D,
  85. TK_TYPE_USAMPLER3D,
  86. TK_TYPE_SAMPLERCUBE,
  87. TK_TYPE_SAMPLERCUBEARRAY,
  88. TK_INTERPOLATION_FLAT,
  89. TK_INTERPOLATION_SMOOTH,
  90. TK_CONST,
  91. TK_STRUCT,
  92. TK_PRECISION_LOW,
  93. TK_PRECISION_MID,
  94. TK_PRECISION_HIGH,
  95. TK_OP_EQUAL,
  96. TK_OP_NOT_EQUAL,
  97. TK_OP_LESS,
  98. TK_OP_LESS_EQUAL,
  99. TK_OP_GREATER,
  100. TK_OP_GREATER_EQUAL,
  101. TK_OP_AND,
  102. TK_OP_OR,
  103. TK_OP_NOT,
  104. TK_OP_ADD,
  105. TK_OP_SUB,
  106. TK_OP_MUL,
  107. TK_OP_DIV,
  108. TK_OP_MOD,
  109. TK_OP_SHIFT_LEFT,
  110. TK_OP_SHIFT_RIGHT,
  111. TK_OP_ASSIGN,
  112. TK_OP_ASSIGN_ADD,
  113. TK_OP_ASSIGN_SUB,
  114. TK_OP_ASSIGN_MUL,
  115. TK_OP_ASSIGN_DIV,
  116. TK_OP_ASSIGN_MOD,
  117. TK_OP_ASSIGN_SHIFT_LEFT,
  118. TK_OP_ASSIGN_SHIFT_RIGHT,
  119. TK_OP_ASSIGN_BIT_AND,
  120. TK_OP_ASSIGN_BIT_OR,
  121. TK_OP_ASSIGN_BIT_XOR,
  122. TK_OP_BIT_AND,
  123. TK_OP_BIT_OR,
  124. TK_OP_BIT_XOR,
  125. TK_OP_BIT_INVERT,
  126. TK_OP_INCREMENT,
  127. TK_OP_DECREMENT,
  128. TK_CF_IF,
  129. TK_CF_ELSE,
  130. TK_CF_FOR,
  131. TK_CF_WHILE,
  132. TK_CF_DO,
  133. TK_CF_SWITCH,
  134. TK_CF_CASE,
  135. TK_CF_DEFAULT,
  136. TK_CF_BREAK,
  137. TK_CF_CONTINUE,
  138. TK_CF_RETURN,
  139. TK_CF_DISCARD,
  140. TK_BRACKET_OPEN,
  141. TK_BRACKET_CLOSE,
  142. TK_CURLY_BRACKET_OPEN,
  143. TK_CURLY_BRACKET_CLOSE,
  144. TK_PARENTHESIS_OPEN,
  145. TK_PARENTHESIS_CLOSE,
  146. TK_QUESTION,
  147. TK_COMMA,
  148. TK_COLON,
  149. TK_SEMICOLON,
  150. TK_PERIOD,
  151. TK_UNIFORM,
  152. TK_UNIFORM_GROUP,
  153. TK_INSTANCE,
  154. TK_GLOBAL,
  155. TK_VARYING,
  156. TK_ARG_IN,
  157. TK_ARG_OUT,
  158. TK_ARG_INOUT,
  159. TK_RENDER_MODE,
  160. TK_HINT_DEFAULT_WHITE_TEXTURE,
  161. TK_HINT_DEFAULT_BLACK_TEXTURE,
  162. TK_HINT_DEFAULT_TRANSPARENT_TEXTURE,
  163. TK_HINT_NORMAL_TEXTURE,
  164. TK_HINT_ROUGHNESS_NORMAL_TEXTURE,
  165. TK_HINT_ROUGHNESS_R,
  166. TK_HINT_ROUGHNESS_G,
  167. TK_HINT_ROUGHNESS_B,
  168. TK_HINT_ROUGHNESS_A,
  169. TK_HINT_ROUGHNESS_GRAY,
  170. TK_HINT_ANISOTROPY_TEXTURE,
  171. TK_HINT_SOURCE_COLOR,
  172. TK_HINT_RANGE,
  173. TK_HINT_INSTANCE_INDEX,
  174. TK_HINT_SCREEN_TEXTURE,
  175. TK_HINT_NORMAL_ROUGHNESS_TEXTURE,
  176. TK_HINT_DEPTH_TEXTURE,
  177. TK_FILTER_NEAREST,
  178. TK_FILTER_LINEAR,
  179. TK_FILTER_NEAREST_MIPMAP,
  180. TK_FILTER_LINEAR_MIPMAP,
  181. TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC,
  182. TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  183. TK_REPEAT_ENABLE,
  184. TK_REPEAT_DISABLE,
  185. TK_SHADER_TYPE,
  186. TK_CURSOR,
  187. TK_ERROR,
  188. TK_EOF,
  189. TK_MAX
  190. };
  191. /* COMPILER */
  192. // lame work around to Apple defining this as a macro in 10.12 SDK
  193. #ifdef TYPE_BOOL
  194. #undef TYPE_BOOL
  195. #endif
  196. enum DataType {
  197. TYPE_VOID,
  198. TYPE_BOOL,
  199. TYPE_BVEC2,
  200. TYPE_BVEC3,
  201. TYPE_BVEC4,
  202. TYPE_INT,
  203. TYPE_IVEC2,
  204. TYPE_IVEC3,
  205. TYPE_IVEC4,
  206. TYPE_UINT,
  207. TYPE_UVEC2,
  208. TYPE_UVEC3,
  209. TYPE_UVEC4,
  210. TYPE_FLOAT,
  211. TYPE_VEC2,
  212. TYPE_VEC3,
  213. TYPE_VEC4,
  214. TYPE_MAT2,
  215. TYPE_MAT3,
  216. TYPE_MAT4,
  217. TYPE_SAMPLER2D,
  218. TYPE_ISAMPLER2D,
  219. TYPE_USAMPLER2D,
  220. TYPE_SAMPLER2DARRAY,
  221. TYPE_ISAMPLER2DARRAY,
  222. TYPE_USAMPLER2DARRAY,
  223. TYPE_SAMPLER3D,
  224. TYPE_ISAMPLER3D,
  225. TYPE_USAMPLER3D,
  226. TYPE_SAMPLERCUBE,
  227. TYPE_SAMPLERCUBEARRAY,
  228. TYPE_STRUCT,
  229. TYPE_MAX
  230. };
  231. enum DataPrecision {
  232. PRECISION_LOWP,
  233. PRECISION_MEDIUMP,
  234. PRECISION_HIGHP,
  235. PRECISION_DEFAULT,
  236. };
  237. enum DataInterpolation {
  238. INTERPOLATION_FLAT,
  239. INTERPOLATION_SMOOTH,
  240. INTERPOLATION_DEFAULT,
  241. };
  242. enum Operator {
  243. OP_EQUAL,
  244. OP_NOT_EQUAL,
  245. OP_LESS,
  246. OP_LESS_EQUAL,
  247. OP_GREATER,
  248. OP_GREATER_EQUAL,
  249. OP_AND,
  250. OP_OR,
  251. OP_NOT,
  252. OP_NEGATE,
  253. OP_ADD,
  254. OP_SUB,
  255. OP_MUL,
  256. OP_DIV,
  257. OP_MOD,
  258. OP_SHIFT_LEFT,
  259. OP_SHIFT_RIGHT,
  260. OP_ASSIGN,
  261. OP_ASSIGN_ADD,
  262. OP_ASSIGN_SUB,
  263. OP_ASSIGN_MUL,
  264. OP_ASSIGN_DIV,
  265. OP_ASSIGN_MOD,
  266. OP_ASSIGN_SHIFT_LEFT,
  267. OP_ASSIGN_SHIFT_RIGHT,
  268. OP_ASSIGN_BIT_AND,
  269. OP_ASSIGN_BIT_OR,
  270. OP_ASSIGN_BIT_XOR,
  271. OP_BIT_AND,
  272. OP_BIT_OR,
  273. OP_BIT_XOR,
  274. OP_BIT_INVERT,
  275. OP_INCREMENT,
  276. OP_DECREMENT,
  277. OP_SELECT_IF,
  278. OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
  279. OP_POST_INCREMENT,
  280. OP_POST_DECREMENT,
  281. OP_CALL,
  282. OP_CONSTRUCT,
  283. OP_STRUCT,
  284. OP_INDEX,
  285. OP_EMPTY,
  286. OP_MAX
  287. };
  288. enum FlowOperation {
  289. FLOW_OP_IF,
  290. FLOW_OP_RETURN,
  291. FLOW_OP_FOR,
  292. FLOW_OP_WHILE,
  293. FLOW_OP_DO,
  294. FLOW_OP_BREAK,
  295. FLOW_OP_SWITCH,
  296. FLOW_OP_CASE,
  297. FLOW_OP_DEFAULT,
  298. FLOW_OP_CONTINUE,
  299. FLOW_OP_DISCARD
  300. };
  301. enum ArgumentQualifier {
  302. ARGUMENT_QUALIFIER_IN,
  303. ARGUMENT_QUALIFIER_OUT,
  304. ARGUMENT_QUALIFIER_INOUT,
  305. };
  306. enum SubClassTag {
  307. TAG_GLOBAL,
  308. TAG_ARRAY,
  309. };
  310. enum TextureFilter {
  311. FILTER_NEAREST,
  312. FILTER_LINEAR,
  313. FILTER_NEAREST_MIPMAP,
  314. FILTER_LINEAR_MIPMAP,
  315. FILTER_NEAREST_MIPMAP_ANISOTROPIC,
  316. FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  317. FILTER_DEFAULT,
  318. };
  319. enum TextureRepeat {
  320. REPEAT_DISABLE,
  321. REPEAT_ENABLE,
  322. REPEAT_DEFAULT,
  323. };
  324. enum {
  325. MAX_INSTANCE_UNIFORM_INDICES = 16
  326. };
  327. struct VaryingFunctionNames {
  328. StringName fragment;
  329. StringName vertex;
  330. StringName light;
  331. VaryingFunctionNames() {
  332. fragment = "fragment";
  333. vertex = "vertex";
  334. light = "light";
  335. }
  336. };
  337. struct Node {
  338. Node *next = nullptr;
  339. enum Type {
  340. NODE_TYPE_SHADER,
  341. NODE_TYPE_FUNCTION,
  342. NODE_TYPE_BLOCK,
  343. NODE_TYPE_VARIABLE,
  344. NODE_TYPE_VARIABLE_DECLARATION,
  345. NODE_TYPE_CONSTANT,
  346. NODE_TYPE_OPERATOR,
  347. NODE_TYPE_CONTROL_FLOW,
  348. NODE_TYPE_MEMBER,
  349. NODE_TYPE_ARRAY,
  350. NODE_TYPE_ARRAY_CONSTRUCT,
  351. NODE_TYPE_STRUCT,
  352. };
  353. Type type;
  354. virtual DataType get_datatype() const { return TYPE_VOID; }
  355. virtual String get_datatype_name() const { return ""; }
  356. virtual int get_array_size() const { return 0; }
  357. virtual bool is_indexed() const { return false; }
  358. Node(Type t) :
  359. type(t) {}
  360. virtual ~Node() {}
  361. };
  362. template <class T>
  363. T *alloc_node() {
  364. T *node = memnew(T);
  365. node->next = nodes;
  366. nodes = node;
  367. return node;
  368. }
  369. Node *nodes = nullptr;
  370. struct OperatorNode : public Node {
  371. DataType return_cache = TYPE_VOID;
  372. DataPrecision return_precision_cache = PRECISION_DEFAULT;
  373. int return_array_size = 0;
  374. Operator op = OP_EQUAL;
  375. StringName struct_name;
  376. Vector<Node *> arguments;
  377. virtual DataType get_datatype() const override { return return_cache; }
  378. virtual String get_datatype_name() const override { return String(struct_name); }
  379. virtual int get_array_size() const override { return return_array_size; }
  380. virtual bool is_indexed() const override { return op == OP_INDEX; }
  381. OperatorNode() :
  382. Node(NODE_TYPE_OPERATOR) {}
  383. };
  384. struct VariableNode : public Node {
  385. DataType datatype_cache = TYPE_VOID;
  386. StringName name;
  387. StringName struct_name;
  388. bool is_const = false;
  389. bool is_local = false;
  390. virtual DataType get_datatype() const override { return datatype_cache; }
  391. virtual String get_datatype_name() const override { return String(struct_name); }
  392. VariableNode() :
  393. Node(NODE_TYPE_VARIABLE) {}
  394. };
  395. struct VariableDeclarationNode : public Node {
  396. DataPrecision precision = PRECISION_DEFAULT;
  397. DataType datatype = TYPE_VOID;
  398. String struct_name;
  399. bool is_const = false;
  400. struct Declaration {
  401. StringName name;
  402. uint32_t size = 0U;
  403. Node *size_expression = nullptr;
  404. Vector<Node *> initializer;
  405. bool single_expression = false;
  406. };
  407. Vector<Declaration> declarations;
  408. virtual DataType get_datatype() const override { return datatype; }
  409. VariableDeclarationNode() :
  410. Node(NODE_TYPE_VARIABLE_DECLARATION) {}
  411. };
  412. struct ArrayNode : public Node {
  413. DataType datatype_cache = TYPE_VOID;
  414. StringName struct_name;
  415. StringName name;
  416. Node *index_expression = nullptr;
  417. Node *call_expression = nullptr;
  418. Node *assign_expression = nullptr;
  419. bool is_const = false;
  420. int array_size = 0;
  421. bool is_local = false;
  422. virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype_cache; }
  423. virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
  424. virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
  425. virtual bool is_indexed() const override { return index_expression != nullptr; }
  426. ArrayNode() :
  427. Node(NODE_TYPE_ARRAY) {}
  428. };
  429. struct ArrayConstructNode : public Node {
  430. DataType datatype = TYPE_VOID;
  431. String struct_name;
  432. Vector<Node *> initializer;
  433. virtual DataType get_datatype() const override { return datatype; }
  434. virtual String get_datatype_name() const override { return struct_name; }
  435. virtual int get_array_size() const override { return initializer.size(); }
  436. ArrayConstructNode() :
  437. Node(NODE_TYPE_ARRAY_CONSTRUCT) {}
  438. };
  439. struct ConstantNode : public Node {
  440. DataType datatype = TYPE_VOID;
  441. String struct_name = "";
  442. int array_size = 0;
  443. union Value {
  444. bool boolean = false;
  445. float real;
  446. int32_t sint;
  447. uint32_t uint;
  448. };
  449. Vector<Value> values;
  450. Vector<VariableDeclarationNode::Declaration> array_declarations;
  451. virtual DataType get_datatype() const override { return datatype; }
  452. virtual String get_datatype_name() const override { return struct_name; }
  453. virtual int get_array_size() const override { return array_size; }
  454. ConstantNode() :
  455. Node(NODE_TYPE_CONSTANT) {}
  456. };
  457. struct FunctionNode;
  458. struct BlockNode : public Node {
  459. FunctionNode *parent_function = nullptr;
  460. BlockNode *parent_block = nullptr;
  461. enum BlockType {
  462. BLOCK_TYPE_STANDARD,
  463. BLOCK_TYPE_FOR_INIT,
  464. BLOCK_TYPE_FOR_CONDITION,
  465. BLOCK_TYPE_FOR_EXPRESSION,
  466. BLOCK_TYPE_SWITCH,
  467. BLOCK_TYPE_CASE,
  468. BLOCK_TYPE_DEFAULT,
  469. };
  470. int block_type = BLOCK_TYPE_STANDARD;
  471. SubClassTag block_tag = SubClassTag::TAG_GLOBAL;
  472. struct Variable {
  473. DataType type;
  474. StringName struct_name;
  475. DataPrecision precision;
  476. int line; //for completion
  477. int array_size;
  478. bool is_const;
  479. ConstantNode::Value value;
  480. };
  481. HashMap<StringName, Variable> variables;
  482. List<Node *> statements;
  483. bool single_statement = false;
  484. bool use_comma_between_statements = false;
  485. BlockNode() :
  486. Node(NODE_TYPE_BLOCK) {}
  487. };
  488. struct ControlFlowNode : public Node {
  489. FlowOperation flow_op = FLOW_OP_IF;
  490. Vector<Node *> expressions;
  491. Vector<BlockNode *> blocks;
  492. ControlFlowNode() :
  493. Node(NODE_TYPE_CONTROL_FLOW) {}
  494. };
  495. struct MemberNode : public Node {
  496. DataType basetype = TYPE_VOID;
  497. bool basetype_const = false;
  498. StringName base_struct_name;
  499. DataPrecision precision = PRECISION_DEFAULT;
  500. DataType datatype = TYPE_VOID;
  501. int array_size = 0;
  502. StringName struct_name;
  503. StringName name;
  504. Node *owner = nullptr;
  505. Node *index_expression = nullptr;
  506. Node *assign_expression = nullptr;
  507. Node *call_expression = nullptr;
  508. bool has_swizzling_duplicates = false;
  509. virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype; }
  510. virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
  511. virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
  512. virtual bool is_indexed() const override { return index_expression != nullptr || call_expression != nullptr; }
  513. MemberNode() :
  514. Node(NODE_TYPE_MEMBER) {}
  515. };
  516. struct StructNode : public Node {
  517. List<MemberNode *> members;
  518. StructNode() :
  519. Node(NODE_TYPE_STRUCT) {}
  520. };
  521. struct FunctionNode : public Node {
  522. struct Argument {
  523. ArgumentQualifier qualifier;
  524. StringName name;
  525. DataType type;
  526. StringName type_str;
  527. DataPrecision precision;
  528. //for passing textures as arguments
  529. bool tex_argument_check;
  530. TextureFilter tex_argument_filter;
  531. TextureRepeat tex_argument_repeat;
  532. bool tex_builtin_check;
  533. StringName tex_builtin;
  534. bool is_const;
  535. int array_size;
  536. HashMap<StringName, HashSet<int>> tex_argument_connect;
  537. };
  538. StringName name;
  539. DataType return_type = TYPE_VOID;
  540. StringName return_struct_name;
  541. DataPrecision return_precision = PRECISION_DEFAULT;
  542. int return_array_size = 0;
  543. Vector<Argument> arguments;
  544. BlockNode *body = nullptr;
  545. bool can_discard = false;
  546. virtual DataType get_datatype() const override { return return_type; }
  547. virtual String get_datatype_name() const override { return String(return_struct_name); }
  548. virtual int get_array_size() const override { return return_array_size; }
  549. FunctionNode() :
  550. Node(NODE_TYPE_FUNCTION) {}
  551. };
  552. struct ShaderNode : public Node {
  553. struct Constant {
  554. StringName name;
  555. DataType type;
  556. StringName type_str;
  557. DataPrecision precision;
  558. ConstantNode *initializer = nullptr;
  559. int array_size;
  560. };
  561. struct Function {
  562. StringName name;
  563. FunctionNode *function = nullptr;
  564. HashSet<StringName> uses_function;
  565. bool callable;
  566. };
  567. struct Struct {
  568. StringName name;
  569. StructNode *shader_struct = nullptr;
  570. };
  571. struct Varying {
  572. enum Stage {
  573. STAGE_UNKNOWN,
  574. STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT_LIGHT, emits warning if it's not used
  575. STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits warning if it's not used
  576. STAGE_VERTEX_TO_FRAGMENT_LIGHT,
  577. STAGE_FRAGMENT_TO_LIGHT,
  578. };
  579. Stage stage = STAGE_UNKNOWN;
  580. DataType type = TYPE_VOID;
  581. DataInterpolation interpolation = INTERPOLATION_FLAT;
  582. DataPrecision precision = PRECISION_DEFAULT;
  583. int array_size = 0;
  584. TkPos tkpos;
  585. Varying() {}
  586. };
  587. struct Uniform {
  588. enum Hint {
  589. HINT_NONE,
  590. HINT_RANGE,
  591. HINT_SOURCE_COLOR,
  592. HINT_NORMAL,
  593. HINT_ROUGHNESS_NORMAL,
  594. HINT_ROUGHNESS_R,
  595. HINT_ROUGHNESS_G,
  596. HINT_ROUGHNESS_B,
  597. HINT_ROUGHNESS_A,
  598. HINT_ROUGHNESS_GRAY,
  599. HINT_DEFAULT_BLACK,
  600. HINT_DEFAULT_WHITE,
  601. HINT_DEFAULT_TRANSPARENT,
  602. HINT_ANISOTROPY,
  603. HINT_SCREEN_TEXTURE,
  604. HINT_NORMAL_ROUGHNESS_TEXTURE,
  605. HINT_DEPTH_TEXTURE,
  606. HINT_MAX
  607. };
  608. enum Scope {
  609. SCOPE_LOCAL,
  610. SCOPE_INSTANCE,
  611. SCOPE_GLOBAL,
  612. };
  613. int order = 0;
  614. int texture_order = 0;
  615. int texture_binding = 0;
  616. DataType type = TYPE_VOID;
  617. DataPrecision precision = PRECISION_DEFAULT;
  618. int array_size = 0;
  619. Vector<ConstantNode::Value> default_value;
  620. Scope scope = SCOPE_LOCAL;
  621. Hint hint = HINT_NONE;
  622. bool use_color = false;
  623. TextureFilter filter = FILTER_DEFAULT;
  624. TextureRepeat repeat = REPEAT_DEFAULT;
  625. float hint_range[3];
  626. int instance_index = 0;
  627. String group;
  628. String subgroup;
  629. Uniform() {
  630. hint_range[0] = 0.0f;
  631. hint_range[1] = 1.0f;
  632. hint_range[2] = 0.001f;
  633. }
  634. };
  635. HashMap<StringName, Constant> constants;
  636. HashMap<StringName, Varying> varyings;
  637. HashMap<StringName, Uniform> uniforms;
  638. HashMap<StringName, Struct> structs;
  639. HashMap<StringName, Function> functions;
  640. Vector<StringName> render_modes;
  641. Vector<Function> vfunctions;
  642. Vector<Constant> vconstants;
  643. Vector<Struct> vstructs;
  644. ShaderNode() :
  645. Node(NODE_TYPE_SHADER) {}
  646. };
  647. struct UniformOrderComparator {
  648. _FORCE_INLINE_ bool operator()(const Pair<StringName, int> &A, const Pair<StringName, int> &B) const {
  649. return A.second < B.second;
  650. }
  651. };
  652. struct Expression {
  653. bool is_op;
  654. union {
  655. Operator op;
  656. Node *node = nullptr;
  657. };
  658. };
  659. struct VarInfo {
  660. StringName name;
  661. DataType type;
  662. };
  663. enum CompletionType {
  664. COMPLETION_NONE,
  665. COMPLETION_SHADER_TYPE,
  666. COMPLETION_RENDER_MODE,
  667. COMPLETION_MAIN_FUNCTION,
  668. COMPLETION_IDENTIFIER,
  669. COMPLETION_FUNCTION_CALL,
  670. COMPLETION_CALL_ARGUMENTS,
  671. COMPLETION_INDEX,
  672. COMPLETION_STRUCT,
  673. COMPLETION_HINT,
  674. };
  675. struct Token {
  676. TokenType type;
  677. StringName text;
  678. double constant;
  679. uint16_t line;
  680. bool is_integer_constant() const {
  681. return type == TK_INT_CONSTANT || type == TK_UINT_CONSTANT;
  682. }
  683. };
  684. static String get_operator_text(Operator p_op);
  685. static String get_token_text(Token p_token);
  686. static bool is_token_datatype(TokenType p_type);
  687. static bool is_token_variable_datatype(TokenType p_type);
  688. static DataType get_token_datatype(TokenType p_type);
  689. static bool is_token_interpolation(TokenType p_type);
  690. static DataInterpolation get_token_interpolation(TokenType p_type);
  691. static bool is_token_precision(TokenType p_type);
  692. static bool is_token_arg_qual(TokenType p_type);
  693. static DataPrecision get_token_precision(TokenType p_type);
  694. static String get_precision_name(DataPrecision p_type);
  695. static String get_interpolation_name(DataInterpolation p_interpolation);
  696. static String get_datatype_name(DataType p_type);
  697. static String get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint);
  698. static String get_texture_filter_name(TextureFilter p_filter);
  699. static String get_texture_repeat_name(TextureRepeat p_repeat);
  700. static bool is_token_nonvoid_datatype(TokenType p_type);
  701. static bool is_token_operator(TokenType p_type);
  702. static bool is_token_operator_assign(TokenType p_type);
  703. static bool is_token_hint(TokenType p_type);
  704. static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
  705. static DataType get_scalar_type(DataType p_type);
  706. static int get_cardinality(DataType p_type);
  707. static bool is_scalar_type(DataType p_type);
  708. static bool is_float_type(DataType p_type);
  709. static bool is_sampler_type(DataType p_type);
  710. static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE);
  711. static PropertyInfo uniform_to_property_info(const ShaderNode::Uniform &p_uniform);
  712. static uint32_t get_datatype_size(DataType p_type);
  713. static void get_keyword_list(List<String> *r_keywords);
  714. static bool is_control_flow_keyword(String p_keyword);
  715. static void get_builtin_funcs(List<String> *r_keywords);
  716. struct BuiltInInfo {
  717. DataType type = TYPE_VOID;
  718. bool constant = false;
  719. BuiltInInfo() {}
  720. BuiltInInfo(DataType p_type, bool p_constant = false) :
  721. type(p_type),
  722. constant(p_constant) {}
  723. };
  724. struct StageFunctionInfo {
  725. struct Argument {
  726. StringName name;
  727. DataType type;
  728. Argument(const StringName &p_name = StringName(), DataType p_type = TYPE_VOID) {
  729. name = p_name;
  730. type = p_type;
  731. }
  732. };
  733. Vector<Argument> arguments;
  734. DataType return_type = TYPE_VOID;
  735. };
  736. struct ModeInfo {
  737. StringName name;
  738. Vector<StringName> options;
  739. ModeInfo() {}
  740. ModeInfo(const StringName &p_name) :
  741. name(p_name) {
  742. }
  743. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2) :
  744. name(p_name) {
  745. options.push_back(p_arg1);
  746. options.push_back(p_arg2);
  747. }
  748. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3) :
  749. name(p_name) {
  750. options.push_back(p_arg1);
  751. options.push_back(p_arg2);
  752. options.push_back(p_arg3);
  753. }
  754. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4) :
  755. name(p_name) {
  756. options.push_back(p_arg1);
  757. options.push_back(p_arg2);
  758. options.push_back(p_arg3);
  759. options.push_back(p_arg4);
  760. }
  761. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5) :
  762. name(p_name) {
  763. options.push_back(p_arg1);
  764. options.push_back(p_arg2);
  765. options.push_back(p_arg3);
  766. options.push_back(p_arg4);
  767. options.push_back(p_arg5);
  768. }
  769. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5, const StringName &p_arg6) :
  770. name(p_name) {
  771. options.push_back(p_arg1);
  772. options.push_back(p_arg2);
  773. options.push_back(p_arg3);
  774. options.push_back(p_arg4);
  775. options.push_back(p_arg5);
  776. options.push_back(p_arg6);
  777. }
  778. };
  779. struct FunctionInfo {
  780. HashMap<StringName, BuiltInInfo> built_ins;
  781. HashMap<StringName, StageFunctionInfo> stage_functions;
  782. bool can_discard = false;
  783. bool main_function = false;
  784. };
  785. static bool has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
  786. typedef DataType (*GlobalShaderUniformGetTypeFunc)(const StringName &p_name);
  787. struct FilePosition {
  788. String file;
  789. int line = 0;
  790. };
  791. private:
  792. struct KeyWord {
  793. TokenType token;
  794. const char *text;
  795. uint32_t flags;
  796. const Vector<String> excluded_shader_types;
  797. const Vector<String> functions;
  798. };
  799. static const KeyWord keyword_list[];
  800. GlobalShaderUniformGetTypeFunc global_shader_uniform_get_type_func = nullptr;
  801. bool error_set = false;
  802. String error_str;
  803. int error_line = 0;
  804. Vector<FilePosition> include_positions;
  805. HashSet<String> include_markers_handled;
  806. #ifdef DEBUG_ENABLED
  807. struct Usage {
  808. int decl_line;
  809. bool used = false;
  810. Usage(int p_decl_line = -1) {
  811. decl_line = p_decl_line;
  812. }
  813. };
  814. HashMap<StringName, Usage> used_constants;
  815. HashMap<StringName, Usage> used_varyings;
  816. HashMap<StringName, Usage> used_uniforms;
  817. HashMap<StringName, Usage> used_functions;
  818. HashMap<StringName, Usage> used_structs;
  819. HashMap<ShaderWarning::Code, HashMap<StringName, Usage> *> warnings_check_map;
  820. HashMap<StringName, HashMap<StringName, Usage>> used_local_vars;
  821. HashMap<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> warnings_check_map2;
  822. List<ShaderWarning> warnings;
  823. bool check_warnings = false;
  824. uint32_t warning_flags = 0;
  825. void _add_line_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  826. warnings.push_back(ShaderWarning(p_code, tk_line, p_subject, p_extra_args));
  827. }
  828. void _add_global_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  829. warnings.push_back(ShaderWarning(p_code, -1, p_subject, p_extra_args));
  830. }
  831. void _add_warning(ShaderWarning::Code p_code, int p_line, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  832. warnings.push_back(ShaderWarning(p_code, p_line, p_subject, p_extra_args));
  833. }
  834. void _check_warning_accums();
  835. #endif // DEBUG_ENABLED
  836. String code;
  837. int char_idx = 0;
  838. int tk_line = 0;
  839. StringName shader_type_identifier;
  840. StringName current_function;
  841. bool is_const_decl = false;
  842. StringName last_name;
  843. bool is_shader_inc = false;
  844. String current_uniform_group_name;
  845. String current_uniform_subgroup_name;
  846. VaryingFunctionNames varying_function_names;
  847. TkPos _get_tkpos() {
  848. TkPos tkp;
  849. tkp.char_idx = char_idx;
  850. tkp.tk_line = tk_line;
  851. return tkp;
  852. }
  853. void _set_tkpos(TkPos p_pos) {
  854. char_idx = p_pos.char_idx;
  855. tk_line = p_pos.tk_line;
  856. }
  857. void _set_error(const String &p_str) {
  858. if (error_set) {
  859. return;
  860. }
  861. error_line = tk_line;
  862. error_set = true;
  863. error_str = p_str;
  864. include_positions.write[include_positions.size() - 1].line = tk_line;
  865. }
  866. void _set_expected_error(const String &p_what) {
  867. _set_error(vformat(RTR("Expected a '%s'."), p_what));
  868. }
  869. void _set_expected_error(const String &p_first, const String p_second) {
  870. _set_error(vformat(RTR("Expected a '%s' or '%s'."), p_first, p_second));
  871. }
  872. void _set_expected_after_error(const String &p_what, const String &p_after) {
  873. _set_error(vformat(RTR("Expected a '%s' after '%s'."), p_what, p_after));
  874. }
  875. void _set_redefinition_error(const String &p_what) {
  876. _set_error(vformat(RTR("Redefinition of '%s'."), p_what));
  877. }
  878. void _set_parsing_error() {
  879. _set_error("Parser bug.");
  880. }
  881. static const char *token_names[TK_MAX];
  882. Token _make_token(TokenType p_type, const StringName &p_text = StringName());
  883. Token _get_token();
  884. bool _lookup_next(Token &r_tk);
  885. ShaderNode *shader = nullptr;
  886. enum IdentifierType {
  887. IDENTIFIER_FUNCTION,
  888. IDENTIFIER_UNIFORM,
  889. IDENTIFIER_VARYING,
  890. IDENTIFIER_FUNCTION_ARGUMENT,
  891. IDENTIFIER_LOCAL_VAR,
  892. IDENTIFIER_BUILTIN_VAR,
  893. IDENTIFIER_CONSTANT,
  894. IDENTIFIER_MAX,
  895. };
  896. IdentifierType last_type = IDENTIFIER_MAX;
  897. bool _find_identifier(const BlockNode *p_block, bool p_allow_reassign, const FunctionInfo &p_function_info, const StringName &p_identifier, DataType *r_data_type = nullptr, IdentifierType *r_type = nullptr, bool *r_is_const = nullptr, int *r_array_size = nullptr, StringName *r_struct_name = nullptr, ConstantNode::Value *r_constant_value = nullptr);
  898. #ifdef DEBUG_ENABLED
  899. void _parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function);
  900. #endif // DEBUG_ENABLED
  901. bool _is_operator_assign(Operator p_op) const;
  902. bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
  903. bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr);
  904. struct BuiltinFuncDef {
  905. enum { MAX_ARGS = 5 };
  906. const char *name;
  907. DataType rettype;
  908. const DataType args[MAX_ARGS];
  909. const char *args_names[MAX_ARGS];
  910. SubClassTag tag;
  911. bool high_end;
  912. };
  913. struct BuiltinFuncOutArgs { //arguments used as out in built in functions
  914. enum { MAX_ARGS = 2 };
  915. const char *name;
  916. const int arguments[MAX_ARGS];
  917. };
  918. struct BuiltinFuncConstArgs {
  919. const char *name;
  920. int arg;
  921. int min;
  922. int max;
  923. };
  924. CompletionType completion_type;
  925. ShaderNode::Uniform::Hint current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  926. TextureFilter current_uniform_filter = FILTER_DEFAULT;
  927. TextureRepeat current_uniform_repeat = REPEAT_DEFAULT;
  928. bool current_uniform_instance_index_defined = false;
  929. int completion_line = 0;
  930. BlockNode *completion_block = nullptr;
  931. DataType completion_base;
  932. bool completion_base_array = false;
  933. SubClassTag completion_class;
  934. StringName completion_function;
  935. StringName completion_struct;
  936. int completion_argument = 0;
  937. #ifdef DEBUG_ENABLED
  938. uint32_t keyword_completion_context;
  939. #endif // DEBUG_ENABLED
  940. const HashMap<StringName, FunctionInfo> *stages = nullptr;
  941. bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
  942. static const BuiltinFuncDef builtin_func_defs[];
  943. static const BuiltinFuncOutArgs builtin_func_out_args[];
  944. static const BuiltinFuncConstArgs builtin_func_const_args[];
  945. static bool is_const_suffix_lut_initialized;
  946. Error _validate_precision(DataType p_type, DataPrecision p_precision);
  947. bool _compare_datatypes(DataType p_datatype_a, String p_datatype_name_a, int p_array_size_a, DataType p_datatype_b, String p_datatype_name_b, int p_array_size_b);
  948. bool _compare_datatypes_in_nodes(Node *a, Node *b);
  949. bool _validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str, bool *r_is_custom_function = nullptr);
  950. bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
  951. bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
  952. bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin);
  953. bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
  954. bool _check_node_constness(const Node *p_node) const;
  955. Node *_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  956. Error _parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_forbid_unknown_size, Node **r_size_expression, int *r_array_size, bool *r_unknown_size);
  957. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info);
  958. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size);
  959. ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
  960. Node *_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  961. Error _parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one = false, bool p_can_break = false, bool p_can_continue = false);
  962. String _get_shader_type_list(const HashSet<String> &p_shader_types) const;
  963. String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
  964. Error _parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types);
  965. Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
  966. Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
  967. public:
  968. #ifdef DEBUG_ENABLED
  969. List<ShaderWarning>::Element *get_warnings_ptr();
  970. void enable_warning_checking(bool p_enabled);
  971. bool is_warning_checking_enabled() const;
  972. void set_warning_flags(uint32_t p_flags);
  973. uint32_t get_warning_flags() const;
  974. #endif // DEBUG_ENABLED
  975. //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  976. void clear();
  977. static String get_shader_type(const String &p_code);
  978. static bool is_builtin_func_out_parameter(const String &p_name, int p_param);
  979. struct ShaderCompileInfo {
  980. HashMap<StringName, FunctionInfo> functions;
  981. Vector<ModeInfo> render_modes;
  982. VaryingFunctionNames varying_function_names = VaryingFunctionNames();
  983. HashSet<String> shader_types;
  984. GlobalShaderUniformGetTypeFunc global_shader_uniform_type_func = nullptr;
  985. bool is_include = false;
  986. };
  987. Error compile(const String &p_code, const ShaderCompileInfo &p_info);
  988. Error complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint);
  989. String get_error_text();
  990. Vector<FilePosition> get_include_positions();
  991. int get_error_line();
  992. ShaderNode *get_shader();
  993. String token_debug(const String &p_code);
  994. ShaderLanguage();
  995. ~ShaderLanguage();
  996. };
  997. #endif // SHADER_LANGUAGE_H