shader_language.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*************************************************************************/
  2. /* shader_language.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SHADER_LANGUAGE_H
  30. #define SHADER_LANGUAGE_H
  31. #include "typedefs.h"
  32. #include "ustring.h"
  33. #include "list.h"
  34. #include "string_db.h"
  35. #include "map.h"
  36. #include "variant.h"
  37. class ShaderLanguage {
  38. public:
  39. enum TokenType {
  40. TK_EMPTY,
  41. TK_INDENTIFIER,
  42. TK_TRUE,
  43. TK_FALSE,
  44. TK_REAL_CONSTANT,
  45. TK_TYPE_VOID,
  46. TK_TYPE_BOOL,
  47. TK_TYPE_FLOAT,
  48. TK_TYPE_VEC2,
  49. TK_TYPE_VEC3,
  50. TK_TYPE_VEC4,
  51. TK_TYPE_MAT2,
  52. TK_TYPE_MAT3,
  53. TK_TYPE_MAT4,
  54. TK_TYPE_TEXTURE,
  55. TK_TYPE_CUBEMAP,
  56. TK_TYPE_COLOR,
  57. TK_OP_EQUAL,
  58. TK_OP_NOT_EQUAL,
  59. TK_OP_LESS,
  60. TK_OP_LESS_EQUAL,
  61. TK_OP_GREATER,
  62. TK_OP_GREATER_EQUAL,
  63. TK_OP_AND,
  64. TK_OP_OR,
  65. TK_OP_NOT,
  66. TK_OP_ADD,
  67. TK_OP_SUB,
  68. TK_OP_MUL,
  69. TK_OP_DIV,
  70. TK_OP_NEG,
  71. TK_OP_ASSIGN,
  72. TK_OP_ASSIGN_ADD,
  73. TK_OP_ASSIGN_SUB,
  74. TK_OP_ASSIGN_MUL,
  75. TK_OP_ASSIGN_DIV,
  76. TK_CF_IF,
  77. TK_CF_ELSE,
  78. TK_CF_RETURN,
  79. TK_BRACKET_OPEN,
  80. TK_BRACKET_CLOSE,
  81. TK_CURLY_BRACKET_OPEN,
  82. TK_CURLY_BRACKET_CLOSE,
  83. TK_PARENTHESIS_OPEN,
  84. TK_PARENTHESIS_CLOSE,
  85. TK_COMMA,
  86. TK_SEMICOLON,
  87. TK_PERIOD,
  88. TK_UNIFORM,
  89. TK_ERROR,
  90. TK_MAX
  91. };
  92. /* COMPILER */
  93. enum ShaderType {
  94. SHADER_MATERIAL_VERTEX,
  95. SHADER_MATERIAL_FRAGMENT,
  96. SHADER_MATERIAL_LIGHT,
  97. SHADER_CANVAS_ITEM_VERTEX,
  98. SHADER_CANVAS_ITEM_FRAGMENT,
  99. SHADER_CANVAS_ITEM_LIGHT,
  100. SHADER_POST_PROCESS,
  101. };
  102. enum DataType {
  103. TYPE_VOID,
  104. TYPE_BOOL,
  105. TYPE_FLOAT,
  106. TYPE_VEC2,
  107. TYPE_VEC3,
  108. TYPE_VEC4,
  109. TYPE_MAT2,
  110. TYPE_MAT3,
  111. TYPE_MAT4,
  112. TYPE_TEXTURE,
  113. TYPE_CUBEMAP,
  114. };
  115. enum Operator {
  116. OP_ASSIGN,
  117. OP_ADD,
  118. OP_SUB,
  119. OP_MUL,
  120. OP_DIV,
  121. OP_ASSIGN_ADD,
  122. OP_ASSIGN_SUB,
  123. OP_ASSIGN_MUL,
  124. OP_ASSIGN_DIV,
  125. OP_NEG,
  126. OP_NOT,
  127. OP_CMP_EQ,
  128. OP_CMP_NEQ,
  129. OP_CMP_LEQ,
  130. OP_CMP_GEQ,
  131. OP_CMP_LESS,
  132. OP_CMP_GREATER,
  133. OP_CMP_OR,
  134. OP_CMP_AND,
  135. OP_CALL,
  136. OP_CONSTRUCT,
  137. OP_MAX
  138. };
  139. enum FlowOperation {
  140. FLOW_OP_IF,
  141. FLOW_OP_RETURN,
  142. //FLOW_OP_FOR,
  143. //FLOW_OP_WHILE,
  144. //FLOW_OP_DO,
  145. //FLOW_OP_BREAK,
  146. //FLOW_OP_CONTINUE,
  147. };
  148. struct Node {
  149. enum Type {
  150. TYPE_PROGRAM,
  151. TYPE_FUNCTION,
  152. TYPE_BLOCK,
  153. TYPE_VARIABLE,
  154. TYPE_CONSTANT,
  155. TYPE_OPERATOR,
  156. TYPE_CONTROL_FLOW,
  157. TYPE_MEMBER
  158. };
  159. Node * parent;
  160. Type type;
  161. virtual DataType get_datatype() const { return TYPE_VOID; }
  162. virtual ~Node() {}
  163. };
  164. struct OperatorNode : public Node {
  165. DataType return_cache;
  166. Operator op;
  167. Vector<Node*> arguments;
  168. virtual DataType get_datatype() const { return return_cache; }
  169. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  170. };
  171. struct VariableNode : public Node {
  172. bool uniform;
  173. DataType datatype_cache;
  174. StringName name;
  175. virtual DataType get_datatype() const { return datatype_cache; }
  176. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; uniform=false; }
  177. };
  178. struct ConstantNode : public Node {
  179. DataType datatype;
  180. Variant value;
  181. virtual DataType get_datatype() const { return datatype; }
  182. ConstantNode() { type=TYPE_CONSTANT; }
  183. };
  184. struct BlockNode : public Node {
  185. Map<StringName,DataType> variables;
  186. List<Node*> statements;
  187. BlockNode() { type=TYPE_BLOCK; }
  188. };
  189. struct ControlFlowNode : public Node {
  190. FlowOperation flow_op;
  191. Vector<Node*> statements;
  192. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  193. };
  194. struct MemberNode : public Node {
  195. DataType basetype;
  196. DataType datatype;
  197. StringName name;
  198. Node* owner;
  199. virtual DataType get_datatype() const { return datatype; }
  200. MemberNode() { type=TYPE_MEMBER; }
  201. };
  202. struct FunctionNode : public Node {
  203. struct Argument {
  204. StringName name;
  205. DataType type;
  206. };
  207. StringName name;
  208. DataType return_type;
  209. Vector<Argument> arguments;
  210. BlockNode *body;
  211. FunctionNode() { type=TYPE_FUNCTION; }
  212. };
  213. struct Uniform {
  214. int order;
  215. DataType type;
  216. Variant default_value;
  217. };
  218. struct ProgramNode : public Node {
  219. struct Function {
  220. StringName name;
  221. FunctionNode*function;
  222. };
  223. Map<StringName,DataType> builtin_variables;
  224. Map<StringName,Uniform> uniforms;
  225. Vector<Function> functions;
  226. BlockNode *body;
  227. ProgramNode() { type=TYPE_PROGRAM; }
  228. };
  229. struct Expression {
  230. bool is_op;
  231. union {
  232. TokenType op;
  233. Node *node;
  234. };
  235. };
  236. typedef Error (*CompileFunc)(void*,ProgramNode*);
  237. struct VarInfo {
  238. StringName name;
  239. DataType type;
  240. };
  241. private:
  242. static const char * token_names[TK_MAX];
  243. struct Token {
  244. TokenType type;
  245. StringName text;
  246. uint16_t line,col;
  247. Token(TokenType p_type=TK_EMPTY,const String& p_text=String()) { type=p_type; text=p_text; line=0; col=0; }
  248. };
  249. static Token read_token(const CharType* p_text,int p_len,int &r_line,int &r_chars);
  250. static Error tokenize(const String& p_text,Vector<Token> *p_tokens,String *r_error,int *r_err_line,int *r_err_column);
  251. class Parser {
  252. Vector<Token> tokens;
  253. int pos;
  254. String error;
  255. public:
  256. void set_error(const String& p_error) { error=p_error; }
  257. void get_error(String *r_error, int *r_line, int *r_column) {
  258. *r_error=error;
  259. *r_line=get_token(pos).line;
  260. *r_column=get_token(pos).col;
  261. }
  262. Token get_token(int ofs=0) const { int idx=pos+ofs; if (idx<0 || idx>=tokens.size()) return Token(TK_ERROR); return tokens[idx]; }
  263. TokenType get_token_type(int ofs=0) const { int idx=pos+ofs; if (idx<0 || idx>=tokens.size()) return TK_ERROR; return tokens[idx].type; }
  264. void advance(int p_amount=1) { pos+=p_amount; }
  265. bool is_at_end() const { return pos>=tokens.size(); }
  266. ProgramNode *program;
  267. template<class T>
  268. T* create_node(Node *p_parent) { T*n=memnew( T ); nodegc.push_back(n); n->parent=p_parent; return n; }
  269. List<Node*> nodegc;
  270. Parser(const Vector<Token>& p_tokens) { tokens=p_tokens; pos=0;}
  271. };
  272. struct IntrinsicFuncDef {
  273. enum { MAX_ARGS=5 };
  274. const char* name;
  275. DataType rettype;
  276. const DataType args[MAX_ARGS];
  277. };
  278. static const IntrinsicFuncDef intrinsic_func_defs[];
  279. struct OperatorDef {
  280. enum { MAX_ARGS=2 };
  281. Operator op;
  282. DataType rettype;
  283. const DataType args[MAX_ARGS];
  284. };
  285. static const OperatorDef operator_defs[];
  286. struct BuiltinsDef {
  287. const char* name;
  288. DataType type;
  289. };
  290. static const BuiltinsDef vertex_builtins_defs[];
  291. static const BuiltinsDef fragment_builtins_defs[];
  292. static const BuiltinsDef light_builtins_defs[];
  293. static const BuiltinsDef ci_vertex_builtins_defs[];
  294. static const BuiltinsDef ci_fragment_builtins_defs[];
  295. static const BuiltinsDef ci_light_builtins_defs[];
  296. static const BuiltinsDef postprocess_fragment_builtins_defs[];
  297. static DataType get_token_datatype(TokenType p_type);
  298. static String get_datatype_name(DataType p_type);
  299. static bool is_token_datatype(TokenType p_type);
  300. static bool is_token_nonvoid_datatype(TokenType p_type);
  301. static bool test_existing_identifier(Node *p_node,const StringName p_identifier,bool p_func=true,bool p_var=true,bool p_builtin=true);
  302. static bool parser_is_at_function(Parser& parser);
  303. static DataType compute_node_type(Node *p_node);
  304. static Node* validate_function_call(Parser&parser, OperatorNode *p_func);
  305. static Node* validate_operator(Parser& parser,OperatorNode *p_func);
  306. static bool is_token_operator(TokenType p_type);
  307. static Operator get_token_operator(TokenType p_type);
  308. static Error parse_expression(Parser& parser,Node *p_parent,Node **r_expr);
  309. static Error parse_variable_declaration(Parser& parser,BlockNode *p_block);
  310. static Error parse_function(Parser& parser,BlockNode *p_block);
  311. static Error parse_flow_if(Parser& parser,Node *p_parent,Node **r_statement);
  312. static Error parse_flow_return(Parser& parser,Node *p_parent,Node **r_statement);
  313. static Error parse_statement(Parser& parser,Node *p_parent,Node **r_statement);
  314. static Error parse_block(Parser& parser,BlockNode *p_block);
  315. 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);
  316. ;
  317. public:
  318. static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  319. 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);
  320. static String lex_debug(const String& p_code);
  321. };
  322. #endif // SHADER_LANGUAGE_H