shader_language.h 22 KB

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