summaryrefslogtreecommitdiff
path: root/servers/visual/shader_language.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'servers/visual/shader_language.cpp')
-rw-r--r--servers/visual/shader_language.cpp107
1 files changed, 89 insertions, 18 deletions
diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp
index 25973aa295..1dd6699851 100644
--- a/servers/visual/shader_language.cpp
+++ b/servers/visual/shader_language.cpp
@@ -860,7 +860,7 @@ void ShaderLanguage::clear() {
}
}
-bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type, int *r_array_size) {
+bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type, bool *r_is_const, int *r_array_size) {
if (p_builtin_types.has(p_identifier)) {
@@ -882,6 +882,9 @@ bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<String
if (r_data_type) {
*r_data_type = p_block->variables[p_identifier].type;
}
+ if (r_is_const) {
+ *r_is_const = p_block->variables[p_identifier].is_const;
+ }
if (r_array_size) {
*r_array_size = p_block->variables[p_identifier].array_size;
}
@@ -958,6 +961,7 @@ bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<String
if (r_type) {
*r_type = IDENTIFIER_FUNCTION;
}
+ return true;
}
}
@@ -2735,7 +2739,7 @@ bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltI
return false;
}
- if (shader->constants.has(var->name)) {
+ if (shader->constants.has(var->name) || var->is_const) {
if (r_message)
*r_message = RTR("Constants cannot be modified.");
return false;
@@ -2745,6 +2749,15 @@ bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltI
return true;
}
} else if (p_node->type == Node::TYPE_ARRAY) {
+
+ ArrayNode *arr = static_cast<ArrayNode *>(p_node);
+
+ if (arr->is_const) {
+ if (r_message)
+ *r_message = RTR("Constants cannot be modified.");
+ return false;
+ }
+
return true;
}
@@ -2931,9 +2944,10 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
DataType data_type;
IdentifierType ident_type;
+ bool is_const = false;
int array_size = 0;
- if (!_find_identifier(p_block, p_builtin_types, identifier, &data_type, &ident_type, &array_size)) {
+ if (!_find_identifier(p_block, p_builtin_types, identifier, &data_type, &ident_type, &is_const, &array_size)) {
_set_error("Unknown identifier in expression: " + String(identifier));
return NULL;
}
@@ -2996,6 +3010,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
arrname->datatype_cache = data_type;
arrname->index_expression = index_expression;
arrname->call_expression = call_expression;
+ arrname->is_const = is_const;
expr = arrname;
} else {
@@ -3003,6 +3018,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
VariableNode *varname = alloc_node<VariableNode>();
varname->name = identifier;
varname->datatype_cache = data_type;
+ varname->is_const = is_const;
expr = varname;
}
}
@@ -3770,7 +3786,15 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
return OK;
- } else if (is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type)) {
+ } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type)) {
+
+ bool is_const = false;
+
+ if (tk.type == TK_CONST) {
+ is_const = true;
+ tk = _get_token();
+ }
+
DataPrecision precision = PRECISION_DEFAULT;
if (is_token_precision(tk.type)) {
precision = get_token_precision(tk.type);
@@ -3810,6 +3834,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
var.precision = precision;
var.line = tk_line;
var.array_size = 0;
+ var.is_const = is_const;
tk = _get_token();
@@ -3819,6 +3844,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
ArrayDeclarationNode *node = alloc_node<ArrayDeclarationNode>();
node->datatype = type;
node->precision = precision;
+ node->is_const = is_const;
vardecl = (Node *)node;
ArrayDeclarationNode::Declaration decl;
@@ -3836,14 +3862,13 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
return ERR_PARSE_ERROR;
}
+ decl.size = ((uint32_t)tk.constant);
tk = _get_token();
if (tk.type != TK_BRACKET_CLOSE) {
_set_error("Expected ']'");
return ERR_PARSE_ERROR;
}
-
- decl.size = ((uint32_t)tk.constant);
var.array_size = decl.size;
}
@@ -3996,6 +4021,10 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
_set_error("Expected array initialization");
return ERR_PARSE_ERROR;
}
+ if (is_const) {
+ _set_error("Expected initialization of constant");
+ return ERR_PARSE_ERROR;
+ }
}
node->declarations.push_back(decl);
@@ -4004,6 +4033,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
node->datatype = type;
node->precision = precision;
+ node->is_const = is_const;
vardecl = (Node *)node;
VariableDeclarationNode::Declaration decl;
@@ -4024,6 +4054,11 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
tk = _get_token();
node->declarations.push_back(decl);
} else {
+ if (is_const) {
+ _set_error("Expected initialization of constant");
+ return ERR_PARSE_ERROR;
+ }
+
VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
node->datatype = type;
node->precision = precision;
@@ -4097,16 +4132,40 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
} else {
_set_tkpos(pos); //rollback
}
- } else if (tk.type == TK_CF_WHILE) {
- //if () {}
+ } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
+ // do {} while()
+ // while() {}
+ bool is_do = tk.type == TK_CF_DO;
+
+ BlockNode *do_block = NULL;
+ if (is_do) {
+
+ do_block = alloc_node<BlockNode>();
+ do_block->parent_block = p_block;
+
+ Error err = _parse_block(do_block, p_builtin_types, true, true, true);
+ if (err)
+ return err;
+
+ tk = _get_token();
+ if (tk.type != TK_CF_WHILE) {
+ _set_error("Expected while after do");
+ return ERR_PARSE_ERROR;
+ }
+ }
tk = _get_token();
+
if (tk.type != TK_PARENTHESIS_OPEN) {
_set_error("Expected '(' after while");
return ERR_PARSE_ERROR;
}
ControlFlowNode *cf = alloc_node<ControlFlowNode>();
- cf->flow_op = FLOW_OP_WHILE;
+ if (is_do) {
+ cf->flow_op = FLOW_OP_DO;
+ } else {
+ cf->flow_op = FLOW_OP_WHILE;
+ }
Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!n)
return ERR_PARSE_ERROR;
@@ -4116,18 +4175,30 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
_set_error("Expected ')' after expression");
return ERR_PARSE_ERROR;
}
+ if (!is_do) {
+ BlockNode *block = alloc_node<BlockNode>();
+ block->parent_block = p_block;
+ cf->expressions.push_back(n);
+ cf->blocks.push_back(block);
+ p_block->statements.push_back(cf);
- BlockNode *block = alloc_node<BlockNode>();
- block->parent_block = p_block;
- cf->expressions.push_back(n);
- cf->blocks.push_back(block);
- p_block->statements.push_back(cf);
+ Error err = _parse_block(block, p_builtin_types, true, true, true);
+ if (err)
+ return err;
+ } else {
- Error err = _parse_block(block, p_builtin_types, true, true, true);
- if (err)
- return err;
+ cf->expressions.push_back(n);
+ cf->blocks.push_back(do_block);
+ p_block->statements.push_back(cf);
+
+ tk = _get_token();
+ if (tk.type != TK_SEMICOLON) {
+ _set_error("Expected ';'");
+ return ERR_PARSE_ERROR;
+ }
+ }
} else if (tk.type == TK_CF_FOR) {
- //if () {}
+ // for() {}
tk = _get_token();
if (tk.type != TK_PARENTHESIS_OPEN) {
_set_error("Expected '(' after for");