shader_language.h 22 KB

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