shader_language.h 23 KB

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