shader_language.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*************************************************************************/
  2. /* shader_language.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef SHADER_LANGUAGE_H
  31. #define SHADER_LANGUAGE_H
  32. #include "core/list.h"
  33. #include "core/map.h"
  34. #include "core/script_language.h"
  35. #include "core/string_name.h"
  36. #include "core/typedefs.h"
  37. #include "core/ustring.h"
  38. #include "core/variant.h"
  39. class ShaderLanguage {
  40. public:
  41. struct TkPos {
  42. int char_idx;
  43. int tk_line;
  44. };
  45. enum TokenType {
  46. TK_EMPTY,
  47. TK_IDENTIFIER,
  48. TK_TRUE,
  49. TK_FALSE,
  50. TK_REAL_CONSTANT,
  51. TK_INT_CONSTANT,
  52. TK_TYPE_VOID,
  53. TK_TYPE_BOOL,
  54. TK_TYPE_BVEC2,
  55. TK_TYPE_BVEC3,
  56. TK_TYPE_BVEC4,
  57. TK_TYPE_INT,
  58. TK_TYPE_IVEC2,
  59. TK_TYPE_IVEC3,
  60. TK_TYPE_IVEC4,
  61. TK_TYPE_UINT,
  62. TK_TYPE_UVEC2,
  63. TK_TYPE_UVEC3,
  64. TK_TYPE_UVEC4,
  65. TK_TYPE_FLOAT,
  66. TK_TYPE_VEC2,
  67. TK_TYPE_VEC3,
  68. TK_TYPE_VEC4,
  69. TK_TYPE_MAT2,
  70. TK_TYPE_MAT3,
  71. TK_TYPE_MAT4,
  72. TK_TYPE_SAMPLER2D,
  73. TK_TYPE_ISAMPLER2D,
  74. TK_TYPE_USAMPLER2D,
  75. TK_TYPE_SAMPLER2DARRAY,
  76. TK_TYPE_ISAMPLER2DARRAY,
  77. TK_TYPE_USAMPLER2DARRAY,
  78. TK_TYPE_SAMPLER3D,
  79. TK_TYPE_ISAMPLER3D,
  80. TK_TYPE_USAMPLER3D,
  81. TK_TYPE_SAMPLERCUBE,
  82. TK_TYPE_SAMPLEREXT,
  83. TK_INTERPOLATION_FLAT,
  84. TK_INTERPOLATION_SMOOTH,
  85. TK_CONST,
  86. TK_STRUCT,
  87. TK_PRECISION_LOW,
  88. TK_PRECISION_MID,
  89. TK_PRECISION_HIGH,
  90. TK_OP_EQUAL,
  91. TK_OP_NOT_EQUAL,
  92. TK_OP_LESS,
  93. TK_OP_LESS_EQUAL,
  94. TK_OP_GREATER,
  95. TK_OP_GREATER_EQUAL,
  96. TK_OP_AND,
  97. TK_OP_OR,
  98. TK_OP_NOT,
  99. TK_OP_ADD,
  100. TK_OP_SUB,
  101. TK_OP_MUL,
  102. TK_OP_DIV,
  103. TK_OP_MOD,
  104. TK_OP_SHIFT_LEFT,
  105. TK_OP_SHIFT_RIGHT,
  106. TK_OP_ASSIGN,
  107. TK_OP_ASSIGN_ADD,
  108. TK_OP_ASSIGN_SUB,
  109. TK_OP_ASSIGN_MUL,
  110. TK_OP_ASSIGN_DIV,
  111. TK_OP_ASSIGN_MOD,
  112. TK_OP_ASSIGN_SHIFT_LEFT,
  113. TK_OP_ASSIGN_SHIFT_RIGHT,
  114. TK_OP_ASSIGN_BIT_AND,
  115. TK_OP_ASSIGN_BIT_OR,
  116. TK_OP_ASSIGN_BIT_XOR,
  117. TK_OP_BIT_AND,
  118. TK_OP_BIT_OR,
  119. TK_OP_BIT_XOR,
  120. TK_OP_BIT_INVERT,
  121. TK_OP_INCREMENT,
  122. TK_OP_DECREMENT,
  123. TK_CF_IF,
  124. TK_CF_ELSE,
  125. TK_CF_FOR,
  126. TK_CF_WHILE,
  127. TK_CF_DO,
  128. TK_CF_SWITCH,
  129. TK_CF_CASE,
  130. TK_CF_DEFAULT,
  131. TK_CF_BREAK,
  132. TK_CF_CONTINUE,
  133. TK_CF_RETURN,
  134. TK_CF_DISCARD,
  135. TK_BRACKET_OPEN,
  136. TK_BRACKET_CLOSE,
  137. TK_CURLY_BRACKET_OPEN,
  138. TK_CURLY_BRACKET_CLOSE,
  139. TK_PARENTHESIS_OPEN,
  140. TK_PARENTHESIS_CLOSE,
  141. TK_QUESTION,
  142. TK_COMMA,
  143. TK_COLON,
  144. TK_SEMICOLON,
  145. TK_PERIOD,
  146. TK_UNIFORM,
  147. TK_VARYING,
  148. TK_ARG_IN,
  149. TK_ARG_OUT,
  150. TK_ARG_INOUT,
  151. TK_RENDER_MODE,
  152. TK_HINT_WHITE_TEXTURE,
  153. TK_HINT_BLACK_TEXTURE,
  154. TK_HINT_NORMAL_TEXTURE,
  155. TK_HINT_ANISO_TEXTURE,
  156. TK_HINT_ALBEDO_TEXTURE,
  157. TK_HINT_BLACK_ALBEDO_TEXTURE,
  158. TK_HINT_COLOR,
  159. TK_HINT_RANGE,
  160. TK_SHADER_TYPE,
  161. TK_CURSOR,
  162. TK_ERROR,
  163. TK_EOF,
  164. TK_MAX
  165. };
  166. /* COMPILER */
  167. // lame work around to Apple defining this as a macro in 10.12 SDK
  168. #ifdef TYPE_BOOL
  169. #undef TYPE_BOOL
  170. #endif
  171. enum DataType {
  172. TYPE_VOID,
  173. TYPE_BOOL,
  174. TYPE_BVEC2,
  175. TYPE_BVEC3,
  176. TYPE_BVEC4,
  177. TYPE_INT,
  178. TYPE_IVEC2,
  179. TYPE_IVEC3,
  180. TYPE_IVEC4,
  181. TYPE_UINT,
  182. TYPE_UVEC2,
  183. TYPE_UVEC3,
  184. TYPE_UVEC4,
  185. TYPE_FLOAT,
  186. TYPE_VEC2,
  187. TYPE_VEC3,
  188. TYPE_VEC4,
  189. TYPE_MAT2,
  190. TYPE_MAT3,
  191. TYPE_MAT4,
  192. TYPE_SAMPLER2D,
  193. TYPE_ISAMPLER2D,
  194. TYPE_USAMPLER2D,
  195. TYPE_SAMPLER2DARRAY,
  196. TYPE_ISAMPLER2DARRAY,
  197. TYPE_USAMPLER2DARRAY,
  198. TYPE_SAMPLER3D,
  199. TYPE_ISAMPLER3D,
  200. TYPE_USAMPLER3D,
  201. TYPE_SAMPLERCUBE,
  202. TYPE_SAMPLEREXT,
  203. TYPE_STRUCT,
  204. };
  205. enum DataPrecision {
  206. PRECISION_LOWP,
  207. PRECISION_MEDIUMP,
  208. PRECISION_HIGHP,
  209. PRECISION_DEFAULT,
  210. };
  211. enum DataInterpolation {
  212. INTERPOLATION_FLAT,
  213. INTERPOLATION_SMOOTH,
  214. };
  215. enum Operator {
  216. OP_EQUAL,
  217. OP_NOT_EQUAL,
  218. OP_LESS,
  219. OP_LESS_EQUAL,
  220. OP_GREATER,
  221. OP_GREATER_EQUAL,
  222. OP_AND,
  223. OP_OR,
  224. OP_NOT,
  225. OP_NEGATE,
  226. OP_ADD,
  227. OP_SUB,
  228. OP_MUL,
  229. OP_DIV,
  230. OP_MOD,
  231. OP_SHIFT_LEFT,
  232. OP_SHIFT_RIGHT,
  233. OP_ASSIGN,
  234. OP_ASSIGN_ADD,
  235. OP_ASSIGN_SUB,
  236. OP_ASSIGN_MUL,
  237. OP_ASSIGN_DIV,
  238. OP_ASSIGN_MOD,
  239. OP_ASSIGN_SHIFT_LEFT,
  240. OP_ASSIGN_SHIFT_RIGHT,
  241. OP_ASSIGN_BIT_AND,
  242. OP_ASSIGN_BIT_OR,
  243. OP_ASSIGN_BIT_XOR,
  244. OP_BIT_AND,
  245. OP_BIT_OR,
  246. OP_BIT_XOR,
  247. OP_BIT_INVERT,
  248. OP_INCREMENT,
  249. OP_DECREMENT,
  250. OP_SELECT_IF,
  251. OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
  252. OP_POST_INCREMENT,
  253. OP_POST_DECREMENT,
  254. OP_CALL,
  255. OP_CONSTRUCT,
  256. OP_STRUCT,
  257. OP_INDEX,
  258. OP_MAX
  259. };
  260. enum FlowOperation {
  261. FLOW_OP_IF,
  262. FLOW_OP_RETURN,
  263. FLOW_OP_FOR,
  264. FLOW_OP_WHILE,
  265. FLOW_OP_DO,
  266. FLOW_OP_BREAK,
  267. FLOW_OP_SWITCH,
  268. FLOW_OP_CASE,
  269. FLOW_OP_DEFAULT,
  270. FLOW_OP_CONTINUE,
  271. FLOW_OP_DISCARD
  272. };
  273. enum ArgumentQualifier {
  274. ARGUMENT_QUALIFIER_IN,
  275. ARGUMENT_QUALIFIER_OUT,
  276. ARGUMENT_QUALIFIER_INOUT,
  277. };
  278. enum SubClassTag {
  279. TAG_GLOBAL,
  280. TAG_ARRAY,
  281. };
  282. struct Node {
  283. Node *next;
  284. enum Type {
  285. TYPE_SHADER,
  286. TYPE_FUNCTION,
  287. TYPE_BLOCK,
  288. TYPE_VARIABLE,
  289. TYPE_VARIABLE_DECLARATION,
  290. TYPE_CONSTANT,
  291. TYPE_OPERATOR,
  292. TYPE_CONTROL_FLOW,
  293. TYPE_MEMBER,
  294. TYPE_ARRAY,
  295. TYPE_ARRAY_DECLARATION,
  296. TYPE_ARRAY_CONSTRUCT,
  297. TYPE_STRUCT,
  298. };
  299. Type type;
  300. virtual DataType get_datatype() const { return TYPE_VOID; }
  301. virtual String get_datatype_name() const { return ""; }
  302. Node(Type t) :
  303. next(nullptr),
  304. type(t) {}
  305. virtual ~Node() {}
  306. };
  307. template <class T>
  308. T *alloc_node() {
  309. T *node = memnew(T);
  310. node->next = nodes;
  311. nodes = node;
  312. return node;
  313. }
  314. Node *nodes;
  315. struct OperatorNode : public Node {
  316. DataType return_cache;
  317. DataPrecision return_precision_cache;
  318. Operator op;
  319. StringName struct_name;
  320. Vector<Node *> arguments;
  321. virtual DataType get_datatype() const { return return_cache; }
  322. virtual String get_datatype_name() const { return String(struct_name); }
  323. OperatorNode() :
  324. Node(TYPE_OPERATOR),
  325. return_cache(TYPE_VOID),
  326. return_precision_cache(PRECISION_DEFAULT),
  327. op(OP_EQUAL),
  328. struct_name("") {}
  329. };
  330. struct VariableNode : public Node {
  331. DataType datatype_cache;
  332. StringName name;
  333. StringName struct_name;
  334. virtual DataType get_datatype() const { return datatype_cache; }
  335. virtual String get_datatype_name() const { return String(struct_name); }
  336. bool is_const;
  337. bool is_local;
  338. VariableNode() :
  339. Node(TYPE_VARIABLE),
  340. datatype_cache(TYPE_VOID),
  341. is_const(false),
  342. is_local(false) {}
  343. };
  344. struct VariableDeclarationNode : public Node {
  345. DataPrecision precision;
  346. DataType datatype;
  347. String struct_name;
  348. bool is_const;
  349. struct Declaration {
  350. StringName name;
  351. Node *initializer;
  352. };
  353. Vector<Declaration> declarations;
  354. virtual DataType get_datatype() const { return datatype; }
  355. VariableDeclarationNode() :
  356. Node(TYPE_VARIABLE_DECLARATION),
  357. precision(PRECISION_DEFAULT),
  358. datatype(TYPE_VOID),
  359. is_const(false) {}
  360. };
  361. struct ArrayNode : public Node {
  362. DataType datatype_cache;
  363. StringName struct_name;
  364. StringName name;
  365. Node *index_expression;
  366. Node *call_expression;
  367. Node *assign_expression;
  368. bool is_const;
  369. bool is_local;
  370. virtual DataType get_datatype() const { return datatype_cache; }
  371. virtual String get_datatype_name() const { return String(struct_name); }
  372. ArrayNode() :
  373. Node(TYPE_ARRAY),
  374. datatype_cache(TYPE_VOID),
  375. index_expression(nullptr),
  376. call_expression(nullptr),
  377. assign_expression(nullptr),
  378. is_const(false),
  379. is_local(false) {}
  380. };
  381. struct ArrayConstructNode : public Node {
  382. DataType datatype;
  383. String struct_name;
  384. Vector<Node *> initializer;
  385. ArrayConstructNode() :
  386. Node(TYPE_ARRAY_CONSTRUCT),
  387. datatype(TYPE_VOID) {
  388. }
  389. };
  390. struct ArrayDeclarationNode : public Node {
  391. DataPrecision precision;
  392. DataType datatype;
  393. String struct_name;
  394. bool is_const;
  395. struct Declaration {
  396. StringName name;
  397. uint32_t size;
  398. Vector<Node *> initializer;
  399. };
  400. Vector<Declaration> declarations;
  401. virtual DataType get_datatype() const { return datatype; }
  402. ArrayDeclarationNode() :
  403. Node(TYPE_ARRAY_DECLARATION),
  404. precision(PRECISION_DEFAULT),
  405. datatype(TYPE_VOID),
  406. is_const(false) {}
  407. };
  408. struct ConstantNode : public Node {
  409. DataType datatype;
  410. String struct_name = "";
  411. int array_size = 0;
  412. union Value {
  413. bool boolean;
  414. float real;
  415. int32_t sint;
  416. uint32_t uint;
  417. };
  418. Vector<Value> values;
  419. Vector<ArrayDeclarationNode::Declaration> array_declarations;
  420. virtual DataType get_datatype() const { return datatype; }
  421. virtual String get_datatype_name() const { return struct_name; }
  422. ConstantNode() :
  423. Node(TYPE_CONSTANT),
  424. datatype(TYPE_VOID) {}
  425. };
  426. struct FunctionNode;
  427. struct BlockNode : public Node {
  428. FunctionNode *parent_function;
  429. BlockNode *parent_block;
  430. enum BlockType {
  431. BLOCK_TYPE_STANDART,
  432. BLOCK_TYPE_SWITCH,
  433. BLOCK_TYPE_CASE,
  434. BLOCK_TYPE_DEFAULT,
  435. };
  436. int block_type;
  437. SubClassTag block_tag;
  438. struct Variable {
  439. DataType type;
  440. StringName struct_name;
  441. DataPrecision precision;
  442. int line; //for completion
  443. int array_size;
  444. bool is_const;
  445. };
  446. Map<StringName, Variable> variables;
  447. List<Node *> statements;
  448. bool single_statement;
  449. BlockNode() :
  450. Node(TYPE_BLOCK),
  451. parent_function(nullptr),
  452. parent_block(nullptr),
  453. block_type(BLOCK_TYPE_STANDART),
  454. block_tag(SubClassTag::TAG_GLOBAL),
  455. single_statement(false) {}
  456. };
  457. struct ControlFlowNode : public Node {
  458. FlowOperation flow_op;
  459. Vector<Node *> expressions;
  460. Vector<BlockNode *> blocks;
  461. ControlFlowNode() :
  462. Node(TYPE_CONTROL_FLOW),
  463. flow_op(FLOW_OP_IF) {}
  464. };
  465. struct MemberNode : public Node {
  466. DataType basetype;
  467. StringName base_struct_name;
  468. DataPrecision precision;
  469. DataType datatype;
  470. int array_size;
  471. StringName struct_name;
  472. StringName name;
  473. Node *owner;
  474. Node *index_expression;
  475. Node *assign_expression;
  476. bool has_swizzling_duplicates;
  477. virtual DataType get_datatype() const { return datatype; }
  478. virtual String get_datatype_name() const { return String(struct_name); }
  479. MemberNode() :
  480. Node(TYPE_MEMBER),
  481. basetype(TYPE_VOID),
  482. datatype(TYPE_VOID),
  483. array_size(0),
  484. owner(nullptr),
  485. index_expression(nullptr),
  486. assign_expression(nullptr),
  487. has_swizzling_duplicates(false) {}
  488. };
  489. struct StructNode : public Node {
  490. List<MemberNode *> members;
  491. StructNode() :
  492. Node(TYPE_STRUCT) {}
  493. };
  494. struct FunctionNode : public Node {
  495. struct Argument {
  496. ArgumentQualifier qualifier;
  497. StringName name;
  498. DataType type;
  499. StringName type_str;
  500. DataPrecision precision;
  501. };
  502. StringName name;
  503. DataType return_type;
  504. StringName return_struct_name;
  505. DataPrecision return_precision;
  506. Vector<Argument> arguments;
  507. BlockNode *body;
  508. bool can_discard;
  509. FunctionNode() :
  510. Node(TYPE_FUNCTION),
  511. return_type(TYPE_VOID),
  512. return_precision(PRECISION_DEFAULT),
  513. body(nullptr),
  514. can_discard(false) {}
  515. };
  516. struct ShaderNode : public Node {
  517. struct Constant {
  518. StringName name;
  519. DataType type;
  520. StringName type_str;
  521. DataPrecision precision;
  522. ConstantNode *initializer;
  523. int array_size;
  524. };
  525. struct Function {
  526. StringName name;
  527. FunctionNode *function;
  528. Set<StringName> uses_function;
  529. bool callable;
  530. };
  531. struct Struct {
  532. StringName name;
  533. StructNode *shader_struct;
  534. };
  535. struct Varying {
  536. enum Stage {
  537. STAGE_UNKNOWN,
  538. STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT_LIGHT, emits warning if it's not used
  539. STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits warning if it's not used
  540. STAGE_VERTEX_TO_FRAGMENT_LIGHT,
  541. STAGE_FRAGMENT_TO_LIGHT,
  542. };
  543. Stage stage;
  544. DataType type;
  545. DataInterpolation interpolation;
  546. DataPrecision precision;
  547. int array_size;
  548. TkPos tkpos;
  549. Varying() :
  550. stage(STAGE_UNKNOWN),
  551. type(TYPE_VOID),
  552. interpolation(INTERPOLATION_FLAT),
  553. precision(PRECISION_DEFAULT),
  554. array_size(0) {}
  555. };
  556. struct Uniform {
  557. enum Hint {
  558. HINT_NONE,
  559. HINT_COLOR,
  560. HINT_RANGE,
  561. HINT_ALBEDO,
  562. HINT_BLACK_ALBEDO,
  563. HINT_NORMAL,
  564. HINT_BLACK,
  565. HINT_WHITE,
  566. HINT_ANISO,
  567. HINT_MAX
  568. };
  569. int order;
  570. int texture_order;
  571. DataType type;
  572. DataPrecision precision;
  573. Vector<ConstantNode::Value> default_value;
  574. Hint hint;
  575. float hint_range[3];
  576. Uniform() :
  577. order(0),
  578. texture_order(0),
  579. type(TYPE_VOID),
  580. precision(PRECISION_DEFAULT),
  581. hint(HINT_NONE) {
  582. hint_range[0] = 0.0f;
  583. hint_range[1] = 1.0f;
  584. hint_range[2] = 0.001f;
  585. }
  586. };
  587. Map<StringName, Constant> constants;
  588. Map<StringName, Varying> varyings;
  589. Map<StringName, Uniform> uniforms;
  590. Map<StringName, Struct> structs;
  591. Vector<StringName> render_modes;
  592. Vector<Function> functions;
  593. Vector<Constant> vconstants;
  594. Vector<Struct> vstructs;
  595. ShaderNode() :
  596. Node(TYPE_SHADER) {}
  597. };
  598. struct Expression {
  599. bool is_op;
  600. union {
  601. Operator op;
  602. Node *node;
  603. };
  604. };
  605. struct VarInfo {
  606. StringName name;
  607. DataType type;
  608. };
  609. enum CompletionType {
  610. COMPLETION_NONE,
  611. COMPLETION_RENDER_MODE,
  612. COMPLETION_MAIN_FUNCTION,
  613. COMPLETION_IDENTIFIER,
  614. COMPLETION_FUNCTION_CALL,
  615. COMPLETION_CALL_ARGUMENTS,
  616. COMPLETION_INDEX,
  617. COMPLETION_STRUCT,
  618. };
  619. struct Token {
  620. TokenType type;
  621. StringName text;
  622. double constant;
  623. uint16_t line;
  624. };
  625. static String get_operator_text(Operator p_op);
  626. static String get_token_text(Token p_token);
  627. static bool is_token_datatype(TokenType p_type);
  628. static bool is_token_variable_datatype(TokenType p_type);
  629. static DataType get_token_datatype(TokenType p_type);
  630. static bool is_token_interpolation(TokenType p_type);
  631. static DataInterpolation get_token_interpolation(TokenType p_type);
  632. static bool is_token_precision(TokenType p_type);
  633. static DataPrecision get_token_precision(TokenType p_type);
  634. static String get_precision_name(DataPrecision p_type);
  635. static String get_datatype_name(DataType p_type);
  636. static bool is_token_nonvoid_datatype(TokenType p_type);
  637. static bool is_token_operator(TokenType p_type);
  638. static bool is_token_operator_assign(TokenType p_type);
  639. static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
  640. static DataType get_scalar_type(DataType p_type);
  641. static int get_cardinality(DataType p_type);
  642. static bool is_scalar_type(DataType p_type);
  643. static bool is_sampler_type(DataType p_type);
  644. static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE);
  645. static void get_keyword_list(List<String> *r_keywords);
  646. static bool is_control_flow_keyword(String p_keyword);
  647. static void get_builtin_funcs(List<String> *r_keywords);
  648. struct BuiltInInfo {
  649. DataType type;
  650. bool constant;
  651. BuiltInInfo() :
  652. type(TYPE_VOID),
  653. constant(false) {}
  654. BuiltInInfo(DataType p_type, bool p_constant = false) :
  655. type(p_type),
  656. constant(p_constant) {}
  657. };
  658. struct FunctionInfo {
  659. Map<StringName, BuiltInInfo> built_ins;
  660. bool can_discard;
  661. };
  662. static bool has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
  663. private:
  664. struct KeyWord {
  665. TokenType token;
  666. const char *text;
  667. };
  668. static const KeyWord keyword_list[];
  669. bool error_set;
  670. String error_str;
  671. int error_line;
  672. String code;
  673. int char_idx;
  674. int tk_line;
  675. StringName current_function;
  676. bool last_const = false;
  677. struct VaryingUsage {
  678. ShaderNode::Varying *var;
  679. int line;
  680. };
  681. List<VaryingUsage> unknown_varying_usages;
  682. bool _check_varying_usages(int *r_error_line, String *r_error_message) const;
  683. TkPos _get_tkpos() {
  684. TkPos tkp;
  685. tkp.char_idx = char_idx;
  686. tkp.tk_line = tk_line;
  687. return tkp;
  688. }
  689. void _set_tkpos(TkPos p_pos) {
  690. char_idx = p_pos.char_idx;
  691. tk_line = p_pos.tk_line;
  692. }
  693. void _set_error(const String &p_str) {
  694. if (error_set) {
  695. return;
  696. }
  697. error_line = tk_line;
  698. error_set = true;
  699. error_str = p_str;
  700. }
  701. static const char *token_names[TK_MAX];
  702. Token _make_token(TokenType p_type, const StringName &p_text = StringName());
  703. Token _get_token();
  704. ShaderNode *shader;
  705. enum IdentifierType {
  706. IDENTIFIER_FUNCTION,
  707. IDENTIFIER_UNIFORM,
  708. IDENTIFIER_VARYING,
  709. IDENTIFIER_FUNCTION_ARGUMENT,
  710. IDENTIFIER_LOCAL_VAR,
  711. IDENTIFIER_BUILTIN_VAR,
  712. IDENTIFIER_CONSTANT,
  713. };
  714. bool _find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, 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);
  715. bool _is_operator_assign(Operator p_op) const;
  716. bool _validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message = nullptr);
  717. bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr);
  718. struct BuiltinFuncDef {
  719. enum { MAX_ARGS = 5 };
  720. const char *name;
  721. DataType rettype;
  722. const DataType args[MAX_ARGS];
  723. SubClassTag tag;
  724. bool high_end;
  725. };
  726. struct BuiltinFuncOutArgs { //arguments used as out in built in functions
  727. const char *name;
  728. int argument;
  729. };
  730. CompletionType completion_type;
  731. int completion_line;
  732. BlockNode *completion_block;
  733. DataType completion_base;
  734. SubClassTag completion_class;
  735. StringName completion_function;
  736. StringName completion_struct;
  737. int completion_argument;
  738. bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
  739. static const BuiltinFuncDef builtin_func_defs[];
  740. static const BuiltinFuncOutArgs builtin_func_out_args[];
  741. Error _validate_datatype(DataType p_type);
  742. bool _compare_datatypes_in_nodes(Node *a, Node *b) const;
  743. bool _validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str);
  744. bool _parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg = nullptr);
  745. bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
  746. bool _validate_varying_using(ShaderNode::Varying &p_varying, String *r_message);
  747. Node *_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types);
  748. Node *_parse_array_constructor(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, DataType p_type, const StringName &p_struct_name, int p_array_size);
  749. ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
  750. Node *_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types);
  751. Error _parse_block(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, bool p_just_one = false, bool p_can_break = false, bool p_can_continue = false);
  752. String _get_shader_type_list(const Set<String> &p_shader_types) const;
  753. String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
  754. Error _parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
  755. Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
  756. Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
  757. public:
  758. //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  759. void clear();
  760. static String get_shader_type(const String &p_code);
  761. Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
  762. Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint);
  763. String get_error_text();
  764. int get_error_line();
  765. ShaderNode *get_shader();
  766. String token_debug(const String &p_code);
  767. ShaderLanguage();
  768. ~ShaderLanguage();
  769. };
  770. #endif // SHADER_LANGUAGE_H