123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /*************************************************************************/
- /* shader_language.h */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #ifndef SHADER_LANGUAGE_H
- #define SHADER_LANGUAGE_H
- #include "typedefs.h"
- #include "ustring.h"
- #include "list.h"
- #include "string_db.h"
- #include "map.h"
- #include "variant.h"
- class ShaderLanguage {
- public:
- enum TokenType {
- TK_EMPTY,
- TK_INDENTIFIER,
- TK_TRUE,
- TK_FALSE,
- TK_REAL_CONSTANT,
- TK_TYPE_VOID,
- TK_TYPE_BOOL,
- TK_TYPE_FLOAT,
- TK_TYPE_VEC2,
- TK_TYPE_VEC3,
- TK_TYPE_VEC4,
- TK_TYPE_MAT2,
- TK_TYPE_MAT3,
- TK_TYPE_MAT4,
- TK_TYPE_TEXTURE,
- TK_TYPE_CUBEMAP,
- TK_TYPE_COLOR,
- TK_OP_EQUAL,
- TK_OP_NOT_EQUAL,
- TK_OP_LESS,
- TK_OP_LESS_EQUAL,
- TK_OP_GREATER,
- TK_OP_GREATER_EQUAL,
- TK_OP_AND,
- TK_OP_OR,
- TK_OP_NOT,
- TK_OP_ADD,
- TK_OP_SUB,
- TK_OP_MUL,
- TK_OP_DIV,
- TK_OP_NEG,
- TK_OP_ASSIGN,
- TK_OP_ASSIGN_ADD,
- TK_OP_ASSIGN_SUB,
- TK_OP_ASSIGN_MUL,
- TK_OP_ASSIGN_DIV,
- TK_CF_IF,
- TK_CF_ELSE,
- TK_CF_RETURN,
- TK_BRACKET_OPEN,
- TK_BRACKET_CLOSE,
- TK_CURLY_BRACKET_OPEN,
- TK_CURLY_BRACKET_CLOSE,
- TK_PARENTHESIS_OPEN,
- TK_PARENTHESIS_CLOSE,
- TK_COMMA,
- TK_SEMICOLON,
- TK_PERIOD,
- TK_UNIFORM,
- TK_ERROR,
- TK_MAX
- };
- /* COMPILER */
- enum ShaderType {
- SHADER_MATERIAL_VERTEX,
- SHADER_MATERIAL_FRAGMENT,
- SHADER_MATERIAL_LIGHT,
- SHADER_CANVAS_ITEM_VERTEX,
- SHADER_CANVAS_ITEM_FRAGMENT,
- SHADER_CANVAS_ITEM_LIGHT,
- SHADER_POST_PROCESS,
- };
- enum DataType {
- TYPE_VOID,
- TYPE_BOOL,
- TYPE_FLOAT,
- TYPE_VEC2,
- TYPE_VEC3,
- TYPE_VEC4,
- TYPE_MAT2,
- TYPE_MAT3,
- TYPE_MAT4,
- TYPE_TEXTURE,
- TYPE_CUBEMAP,
- };
- enum Operator {
- OP_ASSIGN,
- OP_ADD,
- OP_SUB,
- OP_MUL,
- OP_DIV,
- OP_ASSIGN_ADD,
- OP_ASSIGN_SUB,
- OP_ASSIGN_MUL,
- OP_ASSIGN_DIV,
- OP_NEG,
- OP_NOT,
- OP_CMP_EQ,
- OP_CMP_NEQ,
- OP_CMP_LEQ,
- OP_CMP_GEQ,
- OP_CMP_LESS,
- OP_CMP_GREATER,
- OP_CMP_OR,
- OP_CMP_AND,
- OP_CALL,
- OP_CONSTRUCT,
- OP_MAX
- };
- enum FlowOperation {
- FLOW_OP_IF,
- FLOW_OP_RETURN,
- //FLOW_OP_FOR,
- //FLOW_OP_WHILE,
- //FLOW_OP_DO,
- //FLOW_OP_BREAK,
- //FLOW_OP_CONTINUE,
- };
- struct Node {
- enum Type {
- TYPE_PROGRAM,
- TYPE_FUNCTION,
- TYPE_BLOCK,
- TYPE_VARIABLE,
- TYPE_CONSTANT,
- TYPE_OPERATOR,
- TYPE_CONTROL_FLOW,
- TYPE_MEMBER
- };
- Node * parent;
- Type type;
- virtual DataType get_datatype() const { return TYPE_VOID; }
- virtual ~Node() {}
- };
- struct OperatorNode : public Node {
- DataType return_cache;
- Operator op;
- Vector<Node*> arguments;
- virtual DataType get_datatype() const { return return_cache; }
- OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
- };
- struct VariableNode : public Node {
- bool uniform;
- DataType datatype_cache;
- StringName name;
- virtual DataType get_datatype() const { return datatype_cache; }
- VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; uniform=false; }
- };
- struct ConstantNode : public Node {
- DataType datatype;
- Variant value;
- virtual DataType get_datatype() const { return datatype; }
- ConstantNode() { type=TYPE_CONSTANT; }
- };
- struct BlockNode : public Node {
- Map<StringName,DataType> variables;
- List<Node*> statements;
- BlockNode() { type=TYPE_BLOCK; }
- };
- struct ControlFlowNode : public Node {
- FlowOperation flow_op;
- Vector<Node*> statements;
- ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
- };
- struct MemberNode : public Node {
- DataType basetype;
- DataType datatype;
- StringName name;
- Node* owner;
- virtual DataType get_datatype() const { return datatype; }
- MemberNode() { type=TYPE_MEMBER; }
- };
- struct FunctionNode : public Node {
- struct Argument {
- StringName name;
- DataType type;
- };
- StringName name;
- DataType return_type;
- Vector<Argument> arguments;
- BlockNode *body;
- FunctionNode() { type=TYPE_FUNCTION; }
- };
- struct Uniform {
- int order;
- DataType type;
- Variant default_value;
- };
- struct ProgramNode : public Node {
- struct Function {
- StringName name;
- FunctionNode*function;
- };
- Map<StringName,DataType> builtin_variables;
- Map<StringName,Uniform> uniforms;
- Vector<Function> functions;
- BlockNode *body;
- ProgramNode() { type=TYPE_PROGRAM; }
- };
- struct Expression {
- bool is_op;
- union {
- TokenType op;
- Node *node;
- };
- };
- typedef Error (*CompileFunc)(void*,ProgramNode*);
- struct VarInfo {
- StringName name;
- DataType type;
- };
- private:
- static const char * token_names[TK_MAX];
- struct Token {
- TokenType type;
- StringName text;
- uint16_t line,col;
- Token(TokenType p_type=TK_EMPTY,const String& p_text=String()) { type=p_type; text=p_text; line=0; col=0; }
- };
- static Token read_token(const CharType* p_text,int p_len,int &r_line,int &r_chars);
- static Error tokenize(const String& p_text,Vector<Token> *p_tokens,String *r_error,int *r_err_line,int *r_err_column);
- class Parser {
- Vector<Token> tokens;
- int pos;
- String error;
- public:
- void set_error(const String& p_error) { error=p_error; }
- void get_error(String *r_error, int *r_line, int *r_column) {
- *r_error=error;
- *r_line=get_token(pos).line;
- *r_column=get_token(pos).col;
- }
- Token get_token(int ofs=0) const { int idx=pos+ofs; if (idx<0 || idx>=tokens.size()) return Token(TK_ERROR); return tokens[idx]; }
- 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; }
- void advance(int p_amount=1) { pos+=p_amount; }
- bool is_at_end() const { return pos>=tokens.size(); }
- ProgramNode *program;
- template<class T>
- T* create_node(Node *p_parent) { T*n=memnew( T ); nodegc.push_back(n); n->parent=p_parent; return n; }
- List<Node*> nodegc;
- Parser(const Vector<Token>& p_tokens) { tokens=p_tokens; pos=0;}
- };
- struct IntrinsicFuncDef {
- enum { MAX_ARGS=5 };
- const char* name;
- DataType rettype;
- const DataType args[MAX_ARGS];
- };
- static const IntrinsicFuncDef intrinsic_func_defs[];
- struct OperatorDef {
- enum { MAX_ARGS=2 };
- Operator op;
- DataType rettype;
- const DataType args[MAX_ARGS];
- };
- static const OperatorDef operator_defs[];
- struct BuiltinsDef {
- const char* name;
- DataType type;
- };
- static const BuiltinsDef vertex_builtins_defs[];
- static const BuiltinsDef fragment_builtins_defs[];
- static const BuiltinsDef light_builtins_defs[];
- static const BuiltinsDef ci_vertex_builtins_defs[];
- static const BuiltinsDef ci_fragment_builtins_defs[];
- static const BuiltinsDef ci_light_builtins_defs[];
- static const BuiltinsDef postprocess_fragment_builtins_defs[];
- static DataType get_token_datatype(TokenType p_type);
- static String get_datatype_name(DataType p_type);
- static bool is_token_datatype(TokenType p_type);
- static bool is_token_nonvoid_datatype(TokenType p_type);
- static bool test_existing_identifier(Node *p_node,const StringName p_identifier,bool p_func=true,bool p_var=true,bool p_builtin=true);
- static bool parser_is_at_function(Parser& parser);
- static DataType compute_node_type(Node *p_node);
- static Node* validate_function_call(Parser&parser, OperatorNode *p_func);
- static Node* validate_operator(Parser& parser,OperatorNode *p_func);
- static bool is_token_operator(TokenType p_type);
- static Operator get_token_operator(TokenType p_type);
- static Error parse_expression(Parser& parser,Node *p_parent,Node **r_expr);
- static Error parse_variable_declaration(Parser& parser,BlockNode *p_block);
- static Error parse_function(Parser& parser,BlockNode *p_block);
- static Error parse_flow_if(Parser& parser,Node *p_parent,Node **r_statement);
- static Error parse_flow_return(Parser& parser,Node *p_parent,Node **r_statement);
- static Error parse_statement(Parser& parser,Node *p_parent,Node **r_statement);
- static Error parse_block(Parser& parser,BlockNode *p_block);
- 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);
- ;
- public:
- static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
- 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);
- static String lex_debug(const String& p_code);
- };
- #endif // SHADER_LANGUAGE_H
|