2
0

shader_language.h 18 KB

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