shader_language.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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_INDENTIFIER,
  43. TK_TRUE,
  44. TK_FALSE,
  45. TK_REAL_CONSTANT,
  46. TK_TYPE_VOID,
  47. TK_TYPE_BOOL,
  48. TK_TYPE_FLOAT,
  49. TK_TYPE_VEC2,
  50. TK_TYPE_VEC3,
  51. TK_TYPE_VEC4,
  52. TK_TYPE_MAT2,
  53. TK_TYPE_MAT3,
  54. TK_TYPE_MAT4,
  55. TK_TYPE_TEXTURE,
  56. TK_TYPE_CUBEMAP,
  57. TK_TYPE_COLOR,
  58. TK_OP_EQUAL,
  59. TK_OP_NOT_EQUAL,
  60. TK_OP_LESS,
  61. TK_OP_LESS_EQUAL,
  62. TK_OP_GREATER,
  63. TK_OP_GREATER_EQUAL,
  64. TK_OP_AND,
  65. TK_OP_OR,
  66. TK_OP_NOT,
  67. TK_OP_ADD,
  68. TK_OP_SUB,
  69. TK_OP_MUL,
  70. TK_OP_DIV,
  71. TK_OP_NEG,
  72. TK_OP_ASSIGN,
  73. TK_OP_ASSIGN_ADD,
  74. TK_OP_ASSIGN_SUB,
  75. TK_OP_ASSIGN_MUL,
  76. TK_OP_ASSIGN_DIV,
  77. TK_CF_IF,
  78. TK_CF_ELSE,
  79. TK_CF_RETURN,
  80. TK_BRACKET_OPEN,
  81. TK_BRACKET_CLOSE,
  82. TK_CURLY_BRACKET_OPEN,
  83. TK_CURLY_BRACKET_CLOSE,
  84. TK_PARENTHESIS_OPEN,
  85. TK_PARENTHESIS_CLOSE,
  86. TK_COMMA,
  87. TK_SEMICOLON,
  88. TK_PERIOD,
  89. TK_UNIFORM,
  90. TK_ERROR,
  91. TK_MAX
  92. };
  93. /* COMPILER */
  94. enum ShaderType {
  95. SHADER_MATERIAL_VERTEX,
  96. SHADER_MATERIAL_FRAGMENT,
  97. SHADER_MATERIAL_LIGHT,
  98. SHADER_CANVAS_ITEM_VERTEX,
  99. SHADER_CANVAS_ITEM_FRAGMENT,
  100. SHADER_CANVAS_ITEM_LIGHT,
  101. SHADER_POST_PROCESS,
  102. };
  103. enum DataType {
  104. TYPE_VOID,
  105. TYPE_BOOL,
  106. TYPE_FLOAT,
  107. TYPE_VEC2,
  108. TYPE_VEC3,
  109. TYPE_VEC4,
  110. TYPE_MAT2,
  111. TYPE_MAT3,
  112. TYPE_MAT4,
  113. TYPE_TEXTURE,
  114. TYPE_CUBEMAP,
  115. };
  116. enum Operator {
  117. OP_ASSIGN,
  118. OP_ADD,
  119. OP_SUB,
  120. OP_MUL,
  121. OP_DIV,
  122. OP_ASSIGN_ADD,
  123. OP_ASSIGN_SUB,
  124. OP_ASSIGN_MUL,
  125. OP_ASSIGN_DIV,
  126. OP_NEG,
  127. OP_NOT,
  128. OP_CMP_EQ,
  129. OP_CMP_NEQ,
  130. OP_CMP_LEQ,
  131. OP_CMP_GEQ,
  132. OP_CMP_LESS,
  133. OP_CMP_GREATER,
  134. OP_CMP_OR,
  135. OP_CMP_AND,
  136. OP_CALL,
  137. OP_CONSTRUCT,
  138. OP_MAX
  139. };
  140. enum FlowOperation {
  141. FLOW_OP_IF,
  142. FLOW_OP_RETURN,
  143. //FLOW_OP_FOR,
  144. //FLOW_OP_WHILE,
  145. //FLOW_OP_DO,
  146. //FLOW_OP_BREAK,
  147. //FLOW_OP_CONTINUE,
  148. };
  149. struct Node {
  150. enum Type {
  151. TYPE_PROGRAM,
  152. TYPE_FUNCTION,
  153. TYPE_BLOCK,
  154. TYPE_VARIABLE,
  155. TYPE_CONSTANT,
  156. TYPE_OPERATOR,
  157. TYPE_CONTROL_FLOW,
  158. TYPE_MEMBER
  159. };
  160. Node *parent;
  161. Type type;
  162. virtual DataType get_datatype() const { return TYPE_VOID; }
  163. virtual ~Node() {}
  164. };
  165. struct OperatorNode : public Node {
  166. DataType return_cache;
  167. Operator op;
  168. Vector<Node *> arguments;
  169. virtual DataType get_datatype() const { return return_cache; }
  170. OperatorNode() {
  171. type = TYPE_OPERATOR;
  172. return_cache = TYPE_VOID;
  173. }
  174. };
  175. struct VariableNode : public Node {
  176. bool uniform;
  177. DataType datatype_cache;
  178. StringName name;
  179. virtual DataType get_datatype() const { return datatype_cache; }
  180. VariableNode() {
  181. type = TYPE_VARIABLE;
  182. datatype_cache = TYPE_VOID;
  183. uniform = false;
  184. }
  185. };
  186. struct ConstantNode : public Node {
  187. DataType datatype;
  188. Variant value;
  189. virtual DataType get_datatype() const { return datatype; }
  190. ConstantNode() { type = TYPE_CONSTANT; }
  191. };
  192. struct BlockNode : public Node {
  193. Map<StringName, DataType> variables;
  194. List<Node *> statements;
  195. BlockNode() { type = TYPE_BLOCK; }
  196. };
  197. struct ControlFlowNode : public Node {
  198. FlowOperation flow_op;
  199. Vector<Node *> statements;
  200. ControlFlowNode() {
  201. type = TYPE_CONTROL_FLOW;
  202. flow_op = FLOW_OP_IF;
  203. }
  204. };
  205. struct MemberNode : public Node {
  206. DataType basetype;
  207. DataType datatype;
  208. StringName name;
  209. Node *owner;
  210. virtual DataType get_datatype() const { return datatype; }
  211. MemberNode() { type = TYPE_MEMBER; }
  212. };
  213. struct FunctionNode : public Node {
  214. struct Argument {
  215. StringName name;
  216. DataType type;
  217. };
  218. StringName name;
  219. DataType return_type;
  220. Vector<Argument> arguments;
  221. BlockNode *body;
  222. FunctionNode() { type = TYPE_FUNCTION; }
  223. };
  224. struct Uniform {
  225. int order;
  226. DataType type;
  227. Variant default_value;
  228. };
  229. struct ProgramNode : public Node {
  230. struct Function {
  231. StringName name;
  232. FunctionNode *function;
  233. };
  234. Map<StringName, DataType> builtin_variables;
  235. Map<StringName, Uniform> uniforms;
  236. Vector<Function> functions;
  237. BlockNode *body;
  238. ProgramNode() { type = TYPE_PROGRAM; }
  239. };
  240. struct Expression {
  241. bool is_op;
  242. union {
  243. TokenType op;
  244. Node *node;
  245. };
  246. };
  247. typedef Error (*CompileFunc)(void *, ProgramNode *);
  248. struct VarInfo {
  249. StringName name;
  250. DataType type;
  251. };
  252. private:
  253. static const char *token_names[TK_MAX];
  254. struct Token {
  255. TokenType type;
  256. StringName text;
  257. uint16_t line, col;
  258. Token(TokenType p_type = TK_EMPTY, const String &p_text = String()) {
  259. type = p_type;
  260. text = p_text;
  261. line = 0;
  262. col = 0;
  263. }
  264. };
  265. static Token read_token(const CharType *p_text, int p_len, int &r_line, int &r_chars);
  266. static Error tokenize(const String &p_text, Vector<Token> *p_tokens, String *r_error, int *r_err_line, int *r_err_column);
  267. class Parser {
  268. Vector<Token> tokens;
  269. int pos;
  270. String error;
  271. public:
  272. void set_error(const String &p_error) { error = p_error; }
  273. void get_error(String *r_error, int *r_line, int *r_column) {
  274. *r_error = error;
  275. *r_line = get_next_token(0).line;
  276. *r_column = get_next_token(0).col;
  277. }
  278. Token get_next_token(int ofs = 0) const {
  279. int idx = pos + ofs;
  280. if (idx < 0 || idx >= tokens.size()) return Token(TK_ERROR);
  281. return tokens[idx];
  282. }
  283. TokenType get_next_token_type(int ofs = 0) const {
  284. int idx = pos + ofs;
  285. if (idx < 0 || idx >= tokens.size()) return TK_ERROR;
  286. return tokens[idx].type;
  287. }
  288. void advance(int p_amount = 1) { pos += p_amount; }
  289. bool is_at_end() const { return pos >= tokens.size(); }
  290. ProgramNode *program;
  291. template <class T>
  292. T *create_node(Node *p_parent) {
  293. T *n = memnew(T);
  294. nodegc.push_back(n);
  295. n->parent = p_parent;
  296. return n;
  297. }
  298. List<Node *> nodegc;
  299. Parser(const Vector<Token> &p_tokens) {
  300. tokens = p_tokens;
  301. pos = 0;
  302. }
  303. };
  304. struct IntrinsicFuncDef {
  305. enum { MAX_ARGS = 5 };
  306. const char *name;
  307. DataType rettype;
  308. const DataType args[MAX_ARGS];
  309. };
  310. static const IntrinsicFuncDef intrinsic_func_defs[];
  311. struct OperatorDef {
  312. enum { MAX_ARGS = 2 };
  313. Operator op;
  314. DataType rettype;
  315. const DataType args[MAX_ARGS];
  316. };
  317. static const OperatorDef operator_defs[];
  318. struct BuiltinsDef {
  319. const char *name;
  320. DataType type;
  321. };
  322. static const BuiltinsDef vertex_builtins_defs[];
  323. static const BuiltinsDef fragment_builtins_defs[];
  324. static const BuiltinsDef light_builtins_defs[];
  325. static const BuiltinsDef ci_vertex_builtins_defs[];
  326. static const BuiltinsDef ci_fragment_builtins_defs[];
  327. static const BuiltinsDef ci_light_builtins_defs[];
  328. static const BuiltinsDef postprocess_fragment_builtins_defs[];
  329. static DataType get_token_datatype(TokenType p_type);
  330. static String get_datatype_name(DataType p_type);
  331. static bool is_token_datatype(TokenType p_type);
  332. static bool is_token_nonvoid_datatype(TokenType p_type);
  333. static bool test_existing_identifier(Node *p_node, const StringName p_identifier, bool p_func = true, bool p_var = true, bool p_builtin = true);
  334. static bool parser_is_at_function(Parser &parser);
  335. static DataType compute_node_type(Node *p_node);
  336. static Node *validate_function_call(Parser &parser, OperatorNode *p_func);
  337. static Node *validate_operator(Parser &parser, OperatorNode *p_func);
  338. static bool is_token_operator(TokenType p_type);
  339. static Operator get_token_operator(TokenType p_type);
  340. static Error parse_expression(Parser &parser, Node *p_parent, Node **r_expr);
  341. static Error parse_variable_declaration(Parser &parser, BlockNode *p_block);
  342. static Error parse_function(Parser &parser, BlockNode *p_block);
  343. static Error parse_flow_if(Parser &parser, Node *p_parent, Node **r_statement);
  344. static Error parse_flow_return(Parser &parser, Node *p_parent, Node **r_statement);
  345. static Error parse_statement(Parser &parser, Node *p_parent, Node **r_statement);
  346. static Error parse_block(Parser &parser, BlockNode *p_block);
  347. static Error parse(const Vector<Token> &p_tokens, ShaderType p_type, CompileFunc p_compile_func, void *p_userdata, String *r_error, int *r_err_line, int *r_err_column);
  348. ;
  349. public:
  350. static void get_keyword_list(ShaderType p_type, List<String> *p_keywords);
  351. static Error compile(const String &p_code, ShaderType p_type, CompileFunc p_compile_func, void *p_userdata, String *r_error, int *r_err_line, int *r_err_column);
  352. static String lex_debug(const String &p_code);
  353. };
  354. #endif // SHADER_LANGUAGE_H