shader_language.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 Node {
  318. Node *next = nullptr;
  319. enum Type {
  320. TYPE_SHADER,
  321. TYPE_FUNCTION,
  322. TYPE_BLOCK,
  323. TYPE_VARIABLE,
  324. TYPE_VARIABLE_DECLARATION,
  325. TYPE_CONSTANT,
  326. TYPE_OPERATOR,
  327. TYPE_CONTROL_FLOW,
  328. TYPE_MEMBER,
  329. TYPE_ARRAY,
  330. TYPE_ARRAY_DECLARATION,
  331. TYPE_ARRAY_CONSTRUCT,
  332. TYPE_STRUCT,
  333. };
  334. Type type;
  335. virtual DataType get_datatype() const { return TYPE_VOID; }
  336. virtual String get_datatype_name() const { return ""; }
  337. Node(Type t) :
  338. type(t) {}
  339. virtual ~Node() {}
  340. };
  341. template <class T>
  342. T *alloc_node() {
  343. T *node = memnew(T);
  344. node->next = nodes;
  345. nodes = node;
  346. return node;
  347. }
  348. Node *nodes;
  349. struct OperatorNode : public Node {
  350. DataType return_cache = TYPE_VOID;
  351. DataPrecision return_precision_cache = PRECISION_DEFAULT;
  352. Operator op = OP_EQUAL;
  353. StringName struct_name;
  354. Vector<Node *> arguments;
  355. virtual DataType get_datatype() const { return return_cache; }
  356. virtual String get_datatype_name() const { return String(struct_name); }
  357. OperatorNode() :
  358. Node(TYPE_OPERATOR) {}
  359. };
  360. struct VariableNode : public Node {
  361. DataType datatype_cache = TYPE_VOID;
  362. StringName name;
  363. StringName struct_name;
  364. virtual DataType get_datatype() const { return datatype_cache; }
  365. virtual String get_datatype_name() const { return String(struct_name); }
  366. bool is_const = false;
  367. VariableNode() :
  368. Node(TYPE_VARIABLE) {}
  369. };
  370. struct VariableDeclarationNode : public Node {
  371. DataPrecision precision = PRECISION_DEFAULT;
  372. DataType datatype = TYPE_VOID;
  373. String struct_name;
  374. bool is_const = false;
  375. struct Declaration {
  376. StringName name;
  377. Node *initializer;
  378. };
  379. Vector<Declaration> declarations;
  380. virtual DataType get_datatype() const { return datatype; }
  381. VariableDeclarationNode() :
  382. Node(TYPE_VARIABLE_DECLARATION) {}
  383. };
  384. struct ArrayNode : public Node {
  385. DataType datatype_cache = TYPE_VOID;
  386. StringName struct_name;
  387. StringName name;
  388. Node *index_expression = nullptr;
  389. Node *call_expression = nullptr;
  390. Node *assign_expression = nullptr;
  391. bool is_const = false;
  392. virtual DataType get_datatype() const { return datatype_cache; }
  393. virtual String get_datatype_name() const { return String(struct_name); }
  394. ArrayNode() :
  395. Node(TYPE_ARRAY) {}
  396. };
  397. struct ArrayConstructNode : public Node {
  398. DataType datatype = TYPE_VOID;
  399. String struct_name;
  400. Vector<Node *> initializer;
  401. ArrayConstructNode() :
  402. Node(TYPE_ARRAY_CONSTRUCT) {}
  403. };
  404. struct ArrayDeclarationNode : public Node {
  405. DataPrecision precision = PRECISION_DEFAULT;
  406. DataType datatype = TYPE_VOID;
  407. String struct_name;
  408. bool is_const = false;
  409. Node *size_expression = nullptr;
  410. struct Declaration {
  411. StringName name;
  412. uint32_t size;
  413. Vector<Node *> initializer;
  414. };
  415. Vector<Declaration> declarations;
  416. virtual DataType get_datatype() const { return datatype; }
  417. ArrayDeclarationNode() :
  418. Node(TYPE_ARRAY_DECLARATION) {}
  419. };
  420. struct ConstantNode : public Node {
  421. DataType datatype = TYPE_VOID;
  422. String struct_name = "";
  423. int array_size = 0;
  424. union Value {
  425. bool boolean;
  426. float real;
  427. int32_t sint;
  428. uint32_t uint;
  429. };
  430. Vector<Value> values;
  431. Vector<ArrayDeclarationNode::Declaration> array_declarations;
  432. virtual DataType get_datatype() const { return datatype; }
  433. virtual String get_datatype_name() const { return struct_name; }
  434. ConstantNode() :
  435. Node(TYPE_CONSTANT) {}
  436. };
  437. struct FunctionNode;
  438. struct BlockNode : public Node {
  439. FunctionNode *parent_function = nullptr;
  440. BlockNode *parent_block = nullptr;
  441. enum BlockType {
  442. BLOCK_TYPE_STANDART,
  443. BLOCK_TYPE_FOR,
  444. BLOCK_TYPE_SWITCH,
  445. BLOCK_TYPE_CASE,
  446. BLOCK_TYPE_DEFAULT,
  447. };
  448. int block_type = BLOCK_TYPE_STANDART;
  449. SubClassTag block_tag = SubClassTag::TAG_GLOBAL;
  450. struct Variable {
  451. DataType type;
  452. StringName struct_name;
  453. DataPrecision precision;
  454. int line; //for completion
  455. int array_size;
  456. bool is_const;
  457. ConstantNode::Value value;
  458. };
  459. Map<StringName, Variable> variables;
  460. List<Node *> statements;
  461. bool single_statement = false;
  462. BlockNode() :
  463. Node(TYPE_BLOCK) {}
  464. };
  465. struct ControlFlowNode : public Node {
  466. FlowOperation flow_op = FLOW_OP_IF;
  467. Vector<Node *> expressions;
  468. Vector<BlockNode *> blocks;
  469. ControlFlowNode() :
  470. Node(TYPE_CONTROL_FLOW) {}
  471. };
  472. struct MemberNode : public Node {
  473. DataType basetype = TYPE_VOID;
  474. bool basetype_const = false;
  475. StringName base_struct_name;
  476. DataPrecision precision = PRECISION_DEFAULT;
  477. DataType datatype = TYPE_VOID;
  478. int array_size = 0;
  479. StringName struct_name;
  480. StringName name;
  481. Node *owner = nullptr;
  482. Node *index_expression = nullptr;
  483. Node *assign_expression = nullptr;
  484. bool has_swizzling_duplicates = false;
  485. virtual DataType get_datatype() const { return datatype; }
  486. virtual String get_datatype_name() const { return String(struct_name); }
  487. MemberNode() :
  488. Node(TYPE_MEMBER) {}
  489. };
  490. struct StructNode : public Node {
  491. List<MemberNode *> members;
  492. StructNode() :
  493. Node(TYPE_STRUCT) {}
  494. };
  495. struct FunctionNode : public Node {
  496. struct Argument {
  497. ArgumentQualifier qualifier;
  498. StringName name;
  499. DataType type;
  500. StringName type_str;
  501. DataPrecision precision;
  502. //for passing textures as arguments
  503. bool tex_argument_check;
  504. TextureFilter tex_argument_filter;
  505. TextureRepeat tex_argument_repeat;
  506. bool tex_builtin_check;
  507. StringName tex_builtin;
  508. bool is_const;
  509. Map<StringName, Set<int>> tex_argument_connect;
  510. };
  511. StringName name;
  512. DataType return_type = TYPE_VOID;
  513. StringName return_struct_name;
  514. DataPrecision return_precision = PRECISION_DEFAULT;
  515. Vector<Argument> arguments;
  516. BlockNode *body = nullptr;
  517. bool can_discard = false;
  518. FunctionNode() :
  519. Node(TYPE_FUNCTION) {}
  520. };
  521. struct ShaderNode : public Node {
  522. struct Constant {
  523. StringName name;
  524. DataType type;
  525. StringName type_str;
  526. DataPrecision precision;
  527. ConstantNode *initializer;
  528. int array_size;
  529. };
  530. struct Function {
  531. StringName name;
  532. FunctionNode *function;
  533. Set<StringName> uses_function;
  534. bool callable;
  535. };
  536. struct Struct {
  537. StringName name;
  538. StructNode *shader_struct;
  539. };
  540. struct Varying {
  541. enum Stage {
  542. STAGE_UNKNOWN,
  543. STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT or STAGE_VERTEX_TO_LIGHT, emits error if they are not used
  544. STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits error if it's not used
  545. STAGE_VERTEX_TO_FRAGMENT,
  546. STAGE_VERTEX_TO_LIGHT,
  547. STAGE_FRAGMENT_TO_LIGHT,
  548. };
  549. Stage stage = STAGE_UNKNOWN;
  550. DataType type = TYPE_VOID;
  551. DataInterpolation interpolation = INTERPOLATION_FLAT;
  552. DataPrecision precision = PRECISION_DEFAULT;
  553. int array_size = 0;
  554. TkPos tkpos;
  555. Varying() {}
  556. };
  557. struct Uniform {
  558. enum Hint {
  559. HINT_NONE,
  560. HINT_COLOR,
  561. HINT_RANGE,
  562. HINT_ALBEDO,
  563. HINT_BLACK_ALBEDO,
  564. HINT_NORMAL,
  565. HINT_ROUGHNESS_NORMAL,
  566. HINT_ROUGHNESS_R,
  567. HINT_ROUGHNESS_G,
  568. HINT_ROUGHNESS_B,
  569. HINT_ROUGHNESS_A,
  570. HINT_ROUGHNESS_GRAY,
  571. HINT_BLACK,
  572. HINT_WHITE,
  573. HINT_ANISO,
  574. HINT_MAX
  575. };
  576. enum Scope {
  577. SCOPE_LOCAL,
  578. SCOPE_INSTANCE,
  579. SCOPE_GLOBAL,
  580. };
  581. int order = 0;
  582. int texture_order = 0;
  583. DataType type = TYPE_VOID;
  584. DataPrecision precision = PRECISION_DEFAULT;
  585. Vector<ConstantNode::Value> default_value;
  586. Scope scope = SCOPE_LOCAL;
  587. Hint hint = HINT_NONE;
  588. TextureFilter filter = FILTER_DEFAULT;
  589. TextureRepeat repeat = REPEAT_DEFAULT;
  590. float hint_range[3];
  591. int instance_index = 0;
  592. Uniform() {
  593. hint_range[0] = 0.0f;
  594. hint_range[1] = 1.0f;
  595. hint_range[2] = 0.001f;
  596. }
  597. };
  598. Map<StringName, Constant> constants;
  599. Map<StringName, Varying> varyings;
  600. Map<StringName, Uniform> uniforms;
  601. Map<StringName, Struct> structs;
  602. Vector<StringName> render_modes;
  603. Vector<Function> functions;
  604. Vector<Constant> vconstants;
  605. Vector<Struct> vstructs;
  606. ShaderNode() :
  607. Node(TYPE_SHADER) {}
  608. };
  609. struct Expression {
  610. bool is_op;
  611. union {
  612. Operator op;
  613. Node *node;
  614. };
  615. };
  616. struct VarInfo {
  617. StringName name;
  618. DataType type;
  619. };
  620. enum CompletionType {
  621. COMPLETION_NONE,
  622. COMPLETION_RENDER_MODE,
  623. COMPLETION_MAIN_FUNCTION,
  624. COMPLETION_IDENTIFIER,
  625. COMPLETION_FUNCTION_CALL,
  626. COMPLETION_CALL_ARGUMENTS,
  627. COMPLETION_INDEX,
  628. COMPLETION_STRUCT,
  629. };
  630. struct Token {
  631. TokenType type;
  632. StringName text;
  633. double constant;
  634. uint16_t line;
  635. };
  636. static String get_operator_text(Operator p_op);
  637. static String get_token_text(Token p_token);
  638. static bool is_token_datatype(TokenType p_type);
  639. static bool is_token_variable_datatype(TokenType p_type);
  640. static DataType get_token_datatype(TokenType p_type);
  641. static bool is_token_interpolation(TokenType p_type);
  642. static DataInterpolation get_token_interpolation(TokenType p_type);
  643. static bool is_token_precision(TokenType p_type);
  644. static DataPrecision get_token_precision(TokenType p_type);
  645. static String get_precision_name(DataPrecision p_type);
  646. static String get_datatype_name(DataType p_type);
  647. static bool is_token_nonvoid_datatype(TokenType p_type);
  648. static bool is_token_operator(TokenType p_type);
  649. static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
  650. static DataType get_scalar_type(DataType p_type);
  651. static int get_cardinality(DataType p_type);
  652. static bool is_scalar_type(DataType p_type);
  653. static bool is_sampler_type(DataType p_type);
  654. 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);
  655. static PropertyInfo uniform_to_property_info(const ShaderNode::Uniform &p_uniform);
  656. static uint32_t get_type_size(DataType p_type);
  657. static void get_keyword_list(List<String> *r_keywords);
  658. static void get_builtin_funcs(List<String> *r_keywords);
  659. struct BuiltInInfo {
  660. DataType type = TYPE_VOID;
  661. bool constant = false;
  662. BuiltInInfo() {}
  663. BuiltInInfo(DataType p_type, bool p_constant = false) :
  664. type(p_type),
  665. constant(p_constant) {}
  666. };
  667. struct StageFunctionInfo {
  668. struct Argument {
  669. StringName name;
  670. DataType type;
  671. Argument(const StringName &p_name = StringName(), DataType p_type = TYPE_VOID) {
  672. name = p_name;
  673. type = p_type;
  674. }
  675. };
  676. Vector<Argument> arguments;
  677. DataType return_type = TYPE_VOID;
  678. };
  679. struct FunctionInfo {
  680. Map<StringName, BuiltInInfo> built_ins;
  681. Map<StringName, StageFunctionInfo> stage_functions;
  682. bool can_discard;
  683. };
  684. static bool has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
  685. typedef DataType (*GlobalVariableGetTypeFunc)(const StringName &p_name);
  686. private:
  687. struct KeyWord {
  688. TokenType token;
  689. const char *text;
  690. };
  691. static const KeyWord keyword_list[];
  692. GlobalVariableGetTypeFunc global_var_get_type_func;
  693. bool error_set;
  694. String error_str;
  695. int error_line;
  696. String code;
  697. int char_idx;
  698. int tk_line;
  699. StringName current_function;
  700. bool last_const = false;
  701. TkPos _get_tkpos() {
  702. TkPos tkp;
  703. tkp.char_idx = char_idx;
  704. tkp.tk_line = tk_line;
  705. return tkp;
  706. }
  707. void _set_tkpos(TkPos p_pos) {
  708. char_idx = p_pos.char_idx;
  709. tk_line = p_pos.tk_line;
  710. }
  711. void _set_error(const String &p_str) {
  712. if (error_set) {
  713. return;
  714. }
  715. error_line = tk_line;
  716. error_set = true;
  717. error_str = p_str;
  718. }
  719. static const char *token_names[TK_MAX];
  720. Token _make_token(TokenType p_type, const StringName &p_text = StringName());
  721. Token _get_token();
  722. ShaderNode *shader;
  723. enum IdentifierType {
  724. IDENTIFIER_FUNCTION,
  725. IDENTIFIER_UNIFORM,
  726. IDENTIFIER_VARYING,
  727. IDENTIFIER_FUNCTION_ARGUMENT,
  728. IDENTIFIER_LOCAL_VAR,
  729. IDENTIFIER_BUILTIN_VAR,
  730. IDENTIFIER_CONSTANT,
  731. };
  732. 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);
  733. bool _is_operator_assign(Operator p_op) const;
  734. bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
  735. bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr);
  736. struct BuiltinFuncDef {
  737. enum { MAX_ARGS = 5 };
  738. const char *name;
  739. DataType rettype;
  740. const DataType args[MAX_ARGS];
  741. SubClassTag tag;
  742. bool high_end;
  743. };
  744. struct BuiltinFuncOutArgs { //arguments used as out in built in functions
  745. const char *name;
  746. int argument;
  747. };
  748. CompletionType completion_type;
  749. int completion_line;
  750. BlockNode *completion_block;
  751. DataType completion_base;
  752. SubClassTag completion_class;
  753. StringName completion_function;
  754. StringName completion_struct;
  755. int completion_argument;
  756. const Map<StringName, FunctionInfo> *stages = nullptr;
  757. bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
  758. static const BuiltinFuncDef builtin_func_defs[];
  759. static const BuiltinFuncOutArgs builtin_func_out_args[];
  760. Error _validate_datatype(DataType p_type);
  761. bool _compare_datatypes_in_nodes(Node *a, Node *b) const;
  762. bool _validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str);
  763. bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
  764. bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
  765. bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin);
  766. bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
  767. bool _validate_varying_using(ShaderNode::Varying &p_varying, String *r_message);
  768. Node *_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  769. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size);
  770. ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
  771. Node *_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  772. 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);
  773. String _get_shader_type_list(const Set<String> &p_shader_types) const;
  774. String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
  775. Error _parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
  776. Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
  777. Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
  778. public:
  779. //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  780. void clear();
  781. static String get_shader_type(const String &p_code);
  782. Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func);
  783. Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint);
  784. String get_error_text();
  785. int get_error_line();
  786. ShaderNode *get_shader();
  787. String token_debug(const String &p_code);
  788. ShaderLanguage();
  789. ~ShaderLanguage();
  790. };
  791. #endif // SHADER_LANGUAGE_H