diff options
Diffstat (limited to 'servers/rendering/shader_language.h')
-rw-r--r-- | servers/rendering/shader_language.h | 219 |
1 files changed, 95 insertions, 124 deletions
diff --git a/servers/rendering/shader_language.h b/servers/rendering/shader_language.h index 9124bee633..020a5e3e6f 100644 --- a/servers/rendering/shader_language.h +++ b/servers/rendering/shader_language.h @@ -40,7 +40,6 @@ #include "core/variant.h" class ShaderLanguage { - public: enum TokenType { TK_EMPTY, @@ -79,6 +78,7 @@ public: TK_TYPE_ISAMPLER3D, TK_TYPE_USAMPLER3D, TK_TYPE_SAMPLERCUBE, + TK_TYPE_SAMPLERCUBEARRAY, TK_INTERPOLATION_FLAT, TK_INTERPOLATION_SMOOTH, TK_CONST, @@ -143,6 +143,8 @@ public: TK_SEMICOLON, TK_PERIOD, TK_UNIFORM, + TK_INSTANCE, + TK_GLOBAL, TK_VARYING, TK_ARG_IN, TK_ARG_OUT, @@ -162,6 +164,7 @@ public: TK_HINT_BLACK_ALBEDO_TEXTURE, TK_HINT_COLOR, TK_HINT_RANGE, + TK_HINT_INSTANCE_INDEX, TK_FILTER_NEAREST, TK_FILTER_LINEAR, TK_FILTER_NEAREST_MIPMAP, @@ -215,7 +218,9 @@ public: TYPE_ISAMPLER3D, TYPE_USAMPLER3D, TYPE_SAMPLERCUBE, + TYPE_SAMPLERCUBEARRAY, TYPE_STRUCT, + TYPE_MAX }; enum DataPrecision { @@ -317,8 +322,12 @@ public: REPEAT_DEFAULT, }; + enum { + MAX_INSTANCE_UNIFORM_INDICES = 16 + }; + struct Node { - Node *next; + Node *next = nullptr; enum Type { TYPE_SHADER, @@ -342,7 +351,6 @@ public: virtual String get_datatype_name() const { return ""; } Node(Type t) : - next(NULL), type(t) {} virtual ~Node() {} }; @@ -358,41 +366,35 @@ public: Node *nodes; struct OperatorNode : public Node { - DataType return_cache; - DataPrecision return_precision_cache; - Operator op; + DataType return_cache = TYPE_VOID; + DataPrecision return_precision_cache = PRECISION_DEFAULT; + Operator op = OP_EQUAL; StringName struct_name; Vector<Node *> arguments; virtual DataType get_datatype() const { return return_cache; } virtual String get_datatype_name() const { return String(struct_name); } OperatorNode() : - Node(TYPE_OPERATOR), - return_cache(TYPE_VOID), - return_precision_cache(PRECISION_DEFAULT), - op(OP_EQUAL), - struct_name("") {} + Node(TYPE_OPERATOR) {} }; struct VariableNode : public Node { - DataType datatype_cache; + DataType datatype_cache = TYPE_VOID; StringName name; StringName struct_name; virtual DataType get_datatype() const { return datatype_cache; } virtual String get_datatype_name() const { return String(struct_name); } - bool is_const; + bool is_const = false; VariableNode() : - Node(TYPE_VARIABLE), - datatype_cache(TYPE_VOID), - is_const(false) {} + Node(TYPE_VARIABLE) {} }; struct VariableDeclarationNode : public Node { - DataPrecision precision; - DataType datatype; + DataPrecision precision = PRECISION_DEFAULT; + DataType datatype = TYPE_VOID; String struct_name; - bool is_const; + bool is_const = false; struct Declaration { StringName name; @@ -403,47 +405,38 @@ public: virtual DataType get_datatype() const { return datatype; } VariableDeclarationNode() : - Node(TYPE_VARIABLE_DECLARATION), - precision(PRECISION_DEFAULT), - datatype(TYPE_VOID), - is_const(false) {} + Node(TYPE_VARIABLE_DECLARATION) {} }; struct ArrayNode : public Node { - DataType datatype_cache; + DataType datatype_cache = TYPE_VOID; StringName struct_name; StringName name; - Node *index_expression; - Node *call_expression; - bool is_const; + Node *index_expression = nullptr; + Node *call_expression = nullptr; + bool is_const = false; virtual DataType get_datatype() const { return datatype_cache; } virtual String get_datatype_name() const { return String(struct_name); } ArrayNode() : - Node(TYPE_ARRAY), - datatype_cache(TYPE_VOID), - index_expression(NULL), - call_expression(NULL), - is_const(false) {} + Node(TYPE_ARRAY) {} }; struct ArrayConstructNode : public Node { - DataType datatype; + DataType datatype = TYPE_VOID; String struct_name; Vector<Node *> initializer; ArrayConstructNode() : - Node(TYPE_ARRAY_CONSTRUCT), - datatype(TYPE_VOID) { - } + Node(TYPE_ARRAY_CONSTRUCT) {} }; struct ArrayDeclarationNode : public Node { - DataPrecision precision; - DataType datatype; + DataPrecision precision = PRECISION_DEFAULT; + DataType datatype = TYPE_VOID; String struct_name; - bool is_const; + bool is_const = false; struct Declaration { StringName name; @@ -455,14 +448,11 @@ public: virtual DataType get_datatype() const { return datatype; } ArrayDeclarationNode() : - Node(TYPE_ARRAY_DECLARATION), - precision(PRECISION_DEFAULT), - datatype(TYPE_VOID), - is_const(false) {} + Node(TYPE_ARRAY_DECLARATION) {} }; struct ConstantNode : public Node { - DataType datatype; + DataType datatype = TYPE_VOID; union Value { bool boolean; @@ -475,15 +465,14 @@ public: virtual DataType get_datatype() const { return datatype; } ConstantNode() : - Node(TYPE_CONSTANT), - datatype(TYPE_VOID) {} + Node(TYPE_CONSTANT) {} }; struct FunctionNode; struct BlockNode : public Node { - FunctionNode *parent_function; - BlockNode *parent_block; + FunctionNode *parent_function = nullptr; + BlockNode *parent_block = nullptr; enum BlockType { BLOCK_TYPE_STANDART, @@ -493,8 +482,8 @@ public: BLOCK_TYPE_DEFAULT, }; - int block_type; - SubClassTag block_tag; + int block_type = BLOCK_TYPE_STANDART; + SubClassTag block_tag = SubClassTag::TAG_GLOBAL; struct Variable { DataType type; @@ -507,63 +496,48 @@ public: Map<StringName, Variable> variables; List<Node *> statements; - bool single_statement; + bool single_statement = false; BlockNode() : - Node(TYPE_BLOCK), - parent_function(NULL), - parent_block(NULL), - block_type(BLOCK_TYPE_STANDART), - block_tag(SubClassTag::TAG_GLOBAL), - single_statement(false) {} + Node(TYPE_BLOCK) {} }; struct ControlFlowNode : public Node { - FlowOperation flow_op; + FlowOperation flow_op = FLOW_OP_IF; Vector<Node *> expressions; Vector<BlockNode *> blocks; ControlFlowNode() : - Node(TYPE_CONTROL_FLOW), - flow_op(FLOW_OP_IF) {} + Node(TYPE_CONTROL_FLOW) {} }; struct MemberNode : public Node { - DataType basetype; - bool basetype_const; + DataType basetype = TYPE_VOID; + bool basetype_const = false; StringName base_struct_name; DataPrecision precision; - DataType datatype; - int array_size; + DataType datatype = TYPE_VOID; + int array_size = 0; StringName struct_name; StringName name; - Node *owner; - Node *index_expression; - bool has_swizzling_duplicates; + Node *owner = nullptr; + Node *index_expression = nullptr; + bool has_swizzling_duplicates = false; virtual DataType get_datatype() const { return datatype; } virtual String get_datatype_name() const { return String(struct_name); } MemberNode() : - Node(TYPE_MEMBER), - basetype(TYPE_VOID), - basetype_const(false), - datatype(TYPE_VOID), - array_size(0), - owner(NULL), - index_expression(NULL), - has_swizzling_duplicates(false) {} + Node(TYPE_MEMBER) {} }; struct StructNode : public Node { - List<MemberNode *> members; StructNode() : Node(TYPE_STRUCT) {} }; struct FunctionNode : public Node { - struct Argument { ArgumentQualifier qualifier; StringName name; @@ -581,24 +555,20 @@ public: }; StringName name; - DataType return_type; + DataType return_type = TYPE_VOID; StringName return_struct_name; - DataPrecision return_precision; + DataPrecision return_precision = PRECISION_DEFAULT; Vector<Argument> arguments; - BlockNode *body; - bool can_discard; + BlockNode *body = nullptr; + bool can_discard = false; FunctionNode() : - Node(TYPE_FUNCTION), - return_type(TYPE_VOID), - return_precision(PRECISION_DEFAULT), - body(NULL), - can_discard(false) {} + Node(TYPE_FUNCTION) {} }; struct ShaderNode : public Node { - struct Constant { + StringName name; DataType type; StringName type_str; DataPrecision precision; @@ -618,16 +588,12 @@ public: }; struct Varying { - DataType type; - DataInterpolation interpolation; - DataPrecision precision; - int array_size; + DataType type = TYPE_VOID; + DataInterpolation interpolation = INTERPOLATION_FLAT; + DataPrecision precision = PRECISION_DEFAULT; + int array_size = 0; - Varying() : - type(TYPE_VOID), - interpolation(INTERPOLATION_FLAT), - precision(PRECISION_DEFAULT), - array_size(0) {} + Varying() {} }; struct Uniform { @@ -650,24 +616,25 @@ public: HINT_MAX }; - int order; - int texture_order; - DataType type; - DataPrecision precision; + enum Scope { + SCOPE_LOCAL, + SCOPE_INSTANCE, + SCOPE_GLOBAL, + }; + + int order = 0; + int texture_order = 0; + DataType type = TYPE_VOID; + DataPrecision precision = PRECISION_DEFAULT; Vector<ConstantNode::Value> default_value; - Hint hint; - TextureFilter filter; - TextureRepeat repeat; + Scope scope = SCOPE_LOCAL; + Hint hint = HINT_NONE; + TextureFilter filter = FILTER_DEFAULT; + TextureRepeat repeat = REPEAT_DEFAULT; float hint_range[3]; + int instance_index = 0; - Uniform() : - order(0), - texture_order(0), - type(TYPE_VOID), - precision(PRECISION_DEFAULT), - hint(HINT_NONE), - filter(FILTER_DEFAULT), - repeat(REPEAT_DEFAULT) { + Uniform() { hint_range[0] = 0.0f; hint_range[1] = 1.0f; hint_range[2] = 0.001f; @@ -681,6 +648,7 @@ public: Vector<StringName> render_modes; Vector<Function> functions; + Vector<Constant> vconstants; Vector<Struct> vstructs; ShaderNode() : @@ -733,7 +701,7 @@ public: static bool is_token_nonvoid_datatype(TokenType p_type); static bool is_token_operator(TokenType p_type); - static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = NULL); + static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr); static DataType get_scalar_type(DataType p_type); static int get_cardinality(DataType p_type); static bool is_scalar_type(DataType p_type); @@ -746,12 +714,10 @@ public: static void get_builtin_funcs(List<String> *r_keywords); struct BuiltInInfo { - DataType type; - bool constant; + DataType type = TYPE_VOID; + bool constant = false; - BuiltInInfo() : - type(TYPE_VOID), - constant(false) {} + BuiltInInfo() {} BuiltInInfo(DataType p_type, bool p_constant = false) : type(p_type), @@ -764,6 +730,8 @@ public: }; static bool has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name); + typedef DataType (*GlobalVariableGetTypeFunc)(const StringName &p_name); + private: struct KeyWord { TokenType token; @@ -772,6 +740,8 @@ private: static const KeyWord keyword_list[]; + GlobalVariableGetTypeFunc global_var_get_type_func; + bool error_set; String error_str; int error_line; @@ -800,8 +770,9 @@ private: } void _set_error(const String &p_str) { - if (error_set) + if (error_set) { return; + } error_line = tk_line; error_set = true; @@ -825,10 +796,10 @@ private: IDENTIFIER_CONSTANT, }; - bool _find_identifier(const BlockNode *p_block, bool p_allow_reassign, 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, StringName *r_struct_name = NULL); + bool _find_identifier(const BlockNode *p_block, bool p_allow_reassign, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type = nullptr, IdentifierType *r_type = nullptr, bool *r_is_const = nullptr, int *r_array_size = nullptr, StringName *r_struct_name = nullptr); 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); + bool _validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message = nullptr); + bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr); struct BuiltinFuncDef { enum { MAX_ARGS = 5 }; @@ -861,7 +832,7 @@ private: bool _compare_datatypes_in_nodes(Node *a, Node *b) const; bool _validate_function_call(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str); - bool _parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg = NULL); + bool _parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg = nullptr); bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat); bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin); @@ -884,8 +855,8 @@ public: void clear(); 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<ScriptCodeCompletionOption> *r_options, String &r_call_hint); + Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func); + Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint); String get_error_text(); int get_error_line(); |