shader_language.h 9.6 KB

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