shader_language.h 15 KB

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