diff options
Diffstat (limited to 'servers/visual/shader_language.h')
| -rw-r--r-- | servers/visual/shader_language.h | 242 |
1 files changed, 166 insertions, 76 deletions
diff --git a/servers/visual/shader_language.h b/servers/visual/shader_language.h index 08c4d06992..3a5630ef42 100644 --- a/servers/visual/shader_language.h +++ b/servers/visual/shader_language.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,8 @@ #include "core/list.h" #include "core/map.h" -#include "core/string_db.h" +#include "core/script_language.h" +#include "core/string_name.h" #include "core/typedefs.h" #include "core/ustring.h" #include "core/variant.h" @@ -42,7 +43,6 @@ class ShaderLanguage { public: enum TokenType { - TK_EMPTY, TK_IDENTIFIER, TK_TRUE, @@ -80,8 +80,8 @@ public: TK_TYPE_USAMPLER3D, TK_TYPE_SAMPLERCUBE, TK_INTERPOLATION_FLAT, - TK_INTERPOLATION_NO_PERSPECTIVE, TK_INTERPOLATION_SMOOTH, + TK_CONST, TK_PRECISION_LOW, TK_PRECISION_MID, TK_PRECISION_HIGH, @@ -125,6 +125,7 @@ public: TK_CF_DO, TK_CF_SWITCH, TK_CF_CASE, + TK_CF_DEFAULT, TK_CF_BREAK, TK_CF_CONTINUE, TK_CF_RETURN, @@ -210,7 +211,6 @@ public: enum DataInterpolation { INTERPOLATION_FLAT, - INTERPOLATION_NO_PERSPECTIVE, INTERPOLATION_SMOOTH, }; @@ -267,20 +267,19 @@ public: FLOW_OP_DO, FLOW_OP_BREAK, FLOW_OP_SWITCH, + FLOW_OP_CASE, + FLOW_OP_DEFAULT, FLOW_OP_CONTINUE, FLOW_OP_DISCARD - }; enum ArgumentQualifier { ARGUMENT_QUALIFIER_IN, ARGUMENT_QUALIFIER_OUT, ARGUMENT_QUALIFIER_INOUT, - }; struct Node { - Node *next; enum Type { @@ -292,13 +291,17 @@ public: TYPE_CONSTANT, TYPE_OPERATOR, TYPE_CONTROL_FLOW, - TYPE_MEMBER + TYPE_MEMBER, + TYPE_ARRAY, + TYPE_ARRAY_DECLARATION, }; Type type; virtual DataType get_datatype() const { return TYPE_VOID; } - + Node(Type t) : + next(NULL), + type(t) {} virtual ~Node() {} }; @@ -313,38 +316,37 @@ public: Node *nodes; struct OperatorNode : public Node { - DataType return_cache; DataPrecision return_precision_cache; Operator op; Vector<Node *> arguments; virtual DataType get_datatype() const { return return_cache; } - OperatorNode() { - type = TYPE_OPERATOR; - return_cache = TYPE_VOID; - return_precision_cache = PRECISION_DEFAULT; - } + OperatorNode() : + Node(TYPE_OPERATOR), + return_cache(TYPE_VOID), + return_precision_cache(PRECISION_DEFAULT), + op(OP_EQUAL) {} }; struct VariableNode : public Node { DataType datatype_cache; StringName name; virtual DataType get_datatype() const { return datatype_cache; } + bool is_const; - VariableNode() { - type = TYPE_VARIABLE; - datatype_cache = TYPE_VOID; - } + VariableNode() : + Node(TYPE_VARIABLE), + datatype_cache(TYPE_VOID), + is_const(false) {} }; struct VariableDeclarationNode : public Node { - DataPrecision precision; DataType datatype; + bool is_const; struct Declaration { - StringName name; Node *initializer; }; @@ -352,13 +354,52 @@ public: Vector<Declaration> declarations; virtual DataType get_datatype() const { return datatype; } - VariableDeclarationNode() { - type = TYPE_VARIABLE_DECLARATION; - } + VariableDeclarationNode() : + Node(TYPE_VARIABLE_DECLARATION), + precision(PRECISION_DEFAULT), + datatype(TYPE_VOID), + is_const(false) {} }; - struct ConstantNode : public Node { + struct ArrayNode : public Node { + DataType datatype_cache; + StringName name; + Node *index_expression; + Node *call_expression; + bool is_const; + + virtual DataType get_datatype() const { return datatype_cache; } + + ArrayNode() : + Node(TYPE_ARRAY), + datatype_cache(TYPE_VOID), + index_expression(NULL), + call_expression(NULL), + is_const(false) {} + }; + + struct ArrayDeclarationNode : public Node { + DataPrecision precision; + DataType datatype; + bool is_const; + + struct Declaration { + StringName name; + uint32_t size; + Vector<Node *> initializer; + }; + Vector<Declaration> declarations; + virtual DataType get_datatype() const { return datatype; } + + ArrayDeclarationNode() : + Node(TYPE_ARRAY_DECLARATION), + precision(PRECISION_DEFAULT), + datatype(TYPE_VOID), + is_const(false) {} + }; + + struct ConstantNode : public Node { DataType datatype; union Value { @@ -371,7 +412,9 @@ public: Vector<Value> values; virtual DataType get_datatype() const { return datatype; } - ConstantNode() { type = TYPE_CONSTANT; } + ConstantNode() : + Node(TYPE_CONSTANT), + datatype(TYPE_VOID) {} }; struct FunctionNode; @@ -380,48 +423,62 @@ public: FunctionNode *parent_function; BlockNode *parent_block; + enum BlockType { + BLOCK_TYPE_STANDART, + BLOCK_TYPE_SWITCH, + BLOCK_TYPE_CASE, + BLOCK_TYPE_DEFAULT, + }; + + int block_type; + struct Variable { DataType type; DataPrecision precision; int line; //for completion + int array_size; + bool is_const; }; Map<StringName, Variable> variables; List<Node *> statements; bool single_statement; - BlockNode() { - type = TYPE_BLOCK; - parent_block = NULL; - parent_function = NULL; - single_statement = false; - } + + BlockNode() : + Node(TYPE_BLOCK), + parent_function(NULL), + parent_block(NULL), + block_type(BLOCK_TYPE_STANDART), + single_statement(false) {} }; struct ControlFlowNode : public Node { - FlowOperation flow_op; Vector<Node *> expressions; Vector<BlockNode *> blocks; - ControlFlowNode() { - type = TYPE_CONTROL_FLOW; - flow_op = FLOW_OP_IF; - } + + ControlFlowNode() : + Node(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; } + + MemberNode() : + Node(TYPE_MEMBER), + basetype(TYPE_VOID), + datatype(TYPE_VOID), + owner(NULL) {} }; struct FunctionNode : public Node { - struct Argument { - ArgumentQualifier qualifier; StringName name; DataType type; @@ -435,16 +492,22 @@ public: BlockNode *body; bool can_discard; - FunctionNode() { - type = TYPE_FUNCTION; - return_type = TYPE_VOID; - return_precision = PRECISION_DEFAULT; - can_discard = false; - } + FunctionNode() : + Node(TYPE_FUNCTION), + return_type(TYPE_VOID), + return_precision(PRECISION_DEFAULT), + body(NULL), + can_discard(false) {} }; struct ShaderNode : public Node { + struct Constant { + DataType type; + DataPrecision precision; + ConstantNode *initializer; + }; + struct Function { StringName name; FunctionNode *function; @@ -455,7 +518,14 @@ public: struct Varying { DataType type; DataInterpolation interpolation; - DataPrecision precission; + DataPrecision precision; + int array_size; + + Varying() : + type(TYPE_VOID), + interpolation(INTERPOLATION_FLAT), + precision(PRECISION_DEFAULT), + array_size(0) {} }; struct Uniform { @@ -475,30 +545,35 @@ public: int order; int texture_order; DataType type; - DataPrecision precission; + DataPrecision precision; Vector<ConstantNode::Value> default_value; Hint hint; float hint_range[3]; - Uniform() { - hint = HINT_NONE; - hint_range[0] = 0; - hint_range[1] = 1; - hint_range[2] = 0.001; + Uniform() : + order(0), + texture_order(0), + type(TYPE_VOID), + precision(PRECISION_DEFAULT), + hint(HINT_NONE) { + hint_range[0] = 0.0f; + hint_range[1] = 1.0f; + hint_range[2] = 0.001f; } }; + Map<StringName, Constant> constants; Map<StringName, Varying> varyings; Map<StringName, Uniform> uniforms; Vector<StringName> render_modes; Vector<Function> functions; - ShaderNode() { type = TYPE_SHADER; } + ShaderNode() : + Node(TYPE_SHADER) {} }; struct Expression { - bool is_op; union { Operator op; @@ -507,7 +582,6 @@ public: }; struct VarInfo { - StringName name; DataType type; }; @@ -523,7 +597,6 @@ public: }; struct Token { - TokenType type; StringName text; double constant; @@ -534,11 +607,13 @@ public: static String get_token_text(Token p_token); static bool is_token_datatype(TokenType p_type); + static bool is_token_variable_datatype(TokenType p_type); static DataType get_token_datatype(TokenType p_type); static bool is_token_interpolation(TokenType p_type); static DataInterpolation get_token_interpolation(TokenType p_type); static bool is_token_precision(TokenType p_type); static DataPrecision get_token_precision(TokenType p_type); + static String get_precision_name(DataPrecision p_type); static String get_datatype_name(DataType p_type); static bool is_token_nonvoid_datatype(TokenType p_type); static bool is_token_operator(TokenType p_type); @@ -548,7 +623,7 @@ public: static int get_cardinality(DataType p_type); static bool is_scalar_type(DataType p_type); static bool is_sampler_type(DataType p_type); - static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type); + static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE); static void get_keyword_list(List<String> *r_keywords); static void get_builtin_funcs(List<String> *r_keywords); @@ -556,11 +631,14 @@ public: struct BuiltInInfo { DataType type; bool constant; - BuiltInInfo() {} - BuiltInInfo(DataType p_type, bool p_constant = false) { - type = p_type; - constant = p_constant; - } + + BuiltInInfo() : + type(TYPE_VOID), + constant(false) {} + + BuiltInInfo(DataType p_type, bool p_constant = false) : + type(p_type), + constant(p_constant) {} }; struct FunctionInfo { @@ -573,6 +651,7 @@ private: TokenType token; const char *text; }; + static const KeyWord keyword_list[]; bool error_set; @@ -625,46 +704,57 @@ private: IDENTIFIER_FUNCTION_ARGUMENT, IDENTIFIER_LOCAL_VAR, IDENTIFIER_BUILTIN_VAR, + IDENTIFIER_CONSTANT, }; - 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); - + 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, bool *r_is_const = NULL, int *r_array_size = NULL); bool _is_operator_assign(Operator p_op) const; bool _validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message = NULL); - bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = NULL); - struct BuiltinFuncDef { + enum SubClassTag { + TAG_GLOBAL, + TAG_ARRAY + }; + struct BuiltinFuncDef { enum { MAX_ARGS = 5 }; const char *name; DataType rettype; const DataType args[MAX_ARGS]; + SubClassTag tag; + }; + + struct BuiltinFuncOutArgs { //arguments used as out in built in functions + const char *name; + int argument; }; CompletionType completion_type; int completion_line; BlockNode *completion_block; DataType completion_base; + SubClassTag completion_class; StringName completion_function; int completion_argument; bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier); - static const BuiltinFuncDef builtin_func_defs[]; - bool _validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type); + static const BuiltinFuncOutArgs builtin_func_out_args[]; + bool _validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type); bool _parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg = NULL); Node *_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types); - ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node); - Node *_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types); + Node *_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types); 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); - Error _parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types); + Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op); + Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op); + public: //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords); @@ -672,7 +762,7 @@ public: static String get_shader_type(const String &p_code); Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types); - Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<String> *r_options, String &r_call_hint); + Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint); String get_error_text(); int get_error_line(); |