summaryrefslogtreecommitdiff
path: root/servers/rendering/shader_language.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'servers/rendering/shader_language.cpp')
-rw-r--r--servers/rendering/shader_language.cpp328
1 files changed, 224 insertions, 104 deletions
diff --git a/servers/rendering/shader_language.cpp b/servers/rendering/shader_language.cpp
index 300c38db22..93593effd4 100644
--- a/servers/rendering/shader_language.cpp
+++ b/servers/rendering/shader_language.cpp
@@ -194,6 +194,8 @@ const char *ShaderLanguage::token_names[TK_MAX] = {
"SEMICOLON",
"PERIOD",
"UNIFORM",
+ "INSTANCE",
+ "GLOBAL",
"VARYING",
"IN",
"OUT",
@@ -207,6 +209,7 @@ const char *ShaderLanguage::token_names[TK_MAX] = {
"HINT_BLACK_ALBEDO_TEXTURE",
"HINT_COLOR",
"HINT_RANGE",
+ "HINT_INSTANCE_INDEX",
"FILTER_NEAREST",
"FILTER_LINEAR",
"FILTER_NEAREST_MIPMAP",
@@ -300,6 +303,8 @@ const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
{ TK_CF_RETURN, "return" },
{ TK_CF_DISCARD, "discard" },
{ TK_UNIFORM, "uniform" },
+ { TK_INSTANCE, "instance" },
+ { TK_GLOBAL, "global" },
{ TK_VARYING, "varying" },
{ TK_ARG_IN, "in" },
{ TK_ARG_OUT, "out" },
@@ -319,6 +324,7 @@ const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
{ TK_HINT_BLACK_ALBEDO_TEXTURE, "hint_black_albedo" },
{ TK_HINT_COLOR, "hint_color" },
{ TK_HINT_RANGE, "hint_range" },
+ { TK_HINT_INSTANCE_INDEX, "instance_index" },
{ TK_FILTER_NEAREST, "filter_nearest" },
{ TK_FILTER_LINEAR, "filter_linear" },
{ TK_FILTER_NEAREST_MIPMAP, "filter_nearest_mipmap" },
@@ -328,7 +334,7 @@ const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
{ TK_REPEAT_ENABLE, "repeat_enable" },
{ TK_REPEAT_DISABLE, "repeat_disable" },
{ TK_SHADER_TYPE, "shader_type" },
- { TK_ERROR, NULL }
+ { TK_ERROR, nullptr }
};
ShaderLanguage::Token ShaderLanguage::_get_token() {
@@ -864,6 +870,7 @@ String ShaderLanguage::get_datatype_name(DataType p_type) {
case TYPE_USAMPLER3D: return "usampler3D";
case TYPE_SAMPLERCUBE: return "samplerCube";
case TYPE_STRUCT: return "struct";
+ case TYPE_MAX: return "invalid";
}
return "";
@@ -879,7 +886,7 @@ void ShaderLanguage::clear() {
current_function = StringName();
completion_type = COMPLETION_NONE;
- completion_block = NULL;
+ completion_block = nullptr;
completion_function = StringName();
completion_class = SubClassTag::TAG_GLOBAL;
completion_struct = StringName();
@@ -913,7 +920,7 @@ bool ShaderLanguage::_find_identifier(const BlockNode *p_block, bool p_allow_rea
return true;
}
- FunctionNode *function = NULL;
+ FunctionNode *function = nullptr;
while (p_block) {
@@ -2107,14 +2114,14 @@ const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
//array
{ "length", TYPE_INT, { TYPE_VOID }, TAG_ARRAY, true },
- { NULL, TYPE_VOID, { TYPE_VOID }, TAG_GLOBAL, false }
+ { nullptr, TYPE_VOID, { TYPE_VOID }, TAG_GLOBAL, false }
};
const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
//constructors
{ "modf", 1 },
- { NULL, 0 }
+ { nullptr, 0 }
};
bool ShaderLanguage::_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) {
@@ -2678,6 +2685,8 @@ Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::C
break;
case ShaderLanguage::TYPE_VOID:
break;
+ case ShaderLanguage::TYPE_MAX:
+ break;
}
return value;
}
@@ -2774,6 +2783,8 @@ PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform
case ShaderLanguage::TYPE_STRUCT: {
// FIXME: Implement this.
} break;
+ case ShaderLanguage::TYPE_MAX:
+ break;
}
return pi;
}
@@ -2822,6 +2833,8 @@ uint32_t ShaderLanguage::get_type_size(DataType p_type) {
case TYPE_STRUCT:
// FIXME: Implement.
return 0;
+ case ShaderLanguage::TYPE_MAX:
+ return 0;
}
return 0;
}
@@ -3150,7 +3163,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
while (true) {
- Node *expr = NULL;
+ Node *expr = nullptr;
TkPos prepos = _get_tkpos();
Token tk = _get_token();
TkPos pos = _get_tkpos();
@@ -3162,14 +3175,14 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
expr = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!expr)
- return NULL;
+ return nullptr;
tk = _get_token();
if (tk.type != TK_PARENTHESIS_CLOSE) {
_set_error("Expected ')' in expression");
- return NULL;
+ return nullptr;
}
} else if (tk.type == TK_REAL_CONSTANT) {
@@ -3214,7 +3227,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
//make sure void is not used in expression
_set_error("Void value not allowed in Expression");
- return NULL;
+ return nullptr;
} else if (is_token_nonvoid_datatype(tk.type)) {
//basic type constructor
@@ -3234,7 +3247,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_PARENTHESIS_OPEN) {
_set_error("Expected '(' after type name");
- return NULL;
+ return nullptr;
}
int carg = -1;
@@ -3250,11 +3263,11 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
if (!ok)
- return NULL;
+ return nullptr;
if (!_validate_function_call(p_block, p_builtin_types, func, &func->return_cache, &func->struct_name)) {
_set_error("No matching constructor found for: '" + String(funcname->name) + "'");
- return NULL;
+ return nullptr;
}
expr = _reduce_expression(p_block, func);
@@ -3265,7 +3278,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
StringName identifier;
- StructNode *pstruct = NULL;
+ StructNode *pstruct = nullptr;
bool struct_init = false;
_get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
@@ -3317,7 +3330,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
} else {
if (!is_token_variable_datatype(tk.type)) {
_set_error("Invalid data type for array");
- return NULL;
+ return nullptr;
}
type2 = get_token_datatype(tk.type);
}
@@ -3335,7 +3348,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
_set_error("Expected single integer constant > 0");
- return NULL;
+ return nullptr;
}
ConstantNode *cnode = (ConstantNode *)n;
@@ -3343,24 +3356,24 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
array_size2 = cnode->values[0].sint;
if (array_size2 <= 0) {
_set_error("Expected single integer constant > 0");
- return NULL;
+ return nullptr;
}
} else {
_set_error("Expected single integer constant > 0");
- return NULL;
+ return nullptr;
}
tk = _get_token();
if (tk.type != TK_BRACKET_CLOSE) {
_set_error("Expected ']'");
- return NULL;
+ return nullptr;
} else {
tk = _get_token();
}
}
} else {
_set_error("Expected '['");
- return NULL;
+ return nullptr;
}
if (type != type2 || struct_name != struct_name2 || array_size != array_size2) {
@@ -3383,7 +3396,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
error_str += itos(array_size);
error_str += "]'";
_set_error(error_str);
- return NULL;
+ return nullptr;
}
}
@@ -3396,12 +3409,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!n) {
- return NULL;
+ return nullptr;
}
if (type != n->get_datatype() || struct_name != n->get_datatype_name()) {
_set_error("Invalid assignment of '" + (n->get_datatype() == TYPE_STRUCT ? n->get_datatype_name() : get_datatype_name(n->get_datatype())) + "' to '" + (type == TYPE_STRUCT ? struct_name : get_datatype_name(type)) + "'");
- return NULL;
+ return nullptr;
}
tk = _get_token();
@@ -3419,30 +3432,30 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
_set_error("Expected '}' or ','");
else
_set_error("Expected ')' or ','");
- return NULL;
+ return nullptr;
}
}
if (an->initializer.size() != array_size) {
_set_error("Array size mismatch");
- return NULL;
+ return nullptr;
}
} else {
_set_error("Expected array initialization!");
- return NULL;
+ return nullptr;
}
nexpr = an;
} else {
nexpr = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!nexpr) {
- return NULL;
+ return nullptr;
}
Node *node = pstruct->members[i];
if (!_compare_datatypes_in_nodes(pstruct->members[i], nexpr)) {
String type_name = nexpr->get_datatype() == TYPE_STRUCT ? nexpr->get_datatype_name() : get_datatype_name(nexpr->get_datatype());
String type_name2 = node->get_datatype() == TYPE_STRUCT ? node->get_datatype_name() : get_datatype_name(node->get_datatype());
_set_error("Invalid assignment of '" + type_name + "' to '" + type_name2 + "'");
- return NULL;
+ return nullptr;
}
}
@@ -3450,7 +3463,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_COMMA) {
_set_error("Expected ','");
- return NULL;
+ return nullptr;
}
}
func->arguments.push_back(nexpr);
@@ -3458,7 +3471,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_PARENTHESIS_CLOSE) {
_set_error("Expected ')'");
- return NULL;
+ return nullptr;
}
expr = func;
@@ -3482,7 +3495,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
while (bnode) {
if (bnode->variables.has(name)) {
_set_error("Expected function name");
- return NULL;
+ return nullptr;
}
bnode = bnode->parent_block;
}
@@ -3514,11 +3527,11 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
if (!ok)
- return NULL;
+ return nullptr;
if (!_validate_function_call(p_block, p_builtin_types, func, &func->return_cache, &func->struct_name)) {
_set_error("No matching function found for: '" + String(funcname->name) + "'");
- return NULL;
+ return nullptr;
}
completion_class = TAG_GLOBAL; // reset sub-class
if (function_index >= 0) {
@@ -3529,7 +3542,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (call_function) {
//get current base function
- FunctionNode *base_function = NULL;
+ FunctionNode *base_function = nullptr;
{
BlockNode *b = p_block;
@@ -3544,7 +3557,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
}
- ERR_FAIL_COND_V(!base_function, NULL); //bug, wtf
+ ERR_FAIL_COND_V(!base_function, nullptr); //bug, wtf
for (int i = 0; i < call_function->arguments.size(); i++) {
int argidx = i + 1;
@@ -3556,7 +3569,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
error = true;
} else if (n->type == Node::TYPE_ARRAY) {
ArrayNode *an = static_cast<ArrayNode *>(n);
- if (an->call_expression != NULL) {
+ if (an->call_expression != nullptr) {
error = true;
}
} else if (n->type == Node::TYPE_VARIABLE) {
@@ -3584,7 +3597,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
if (error) {
_set_error(vformat("Constant value cannot be passed for '%s' parameter!", _get_qualifier_str(call_function->arguments[i].qualifier)));
- return NULL;
+ return nullptr;
}
}
if (is_sampler_type(call_function->arguments[i].type)) {
@@ -3599,12 +3612,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
ERR_CONTINUE(u->type != call_function->arguments[i].type); //this should have been validated previously
//propagate
if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat)) {
- return NULL;
+ return nullptr;
}
} else if (p_builtin_types.has(varname)) {
//a built-in
if (!_propagate_function_call_sampler_builtin_reference(name, i, varname)) {
- return NULL;
+ return nullptr;
}
} else {
//or this comes from an argument, but nothing else can be a sampler
@@ -3653,30 +3666,30 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
if (!found) {
_set_error("Unknown identifier in expression: " + String(identifier));
- return NULL;
+ return nullptr;
}
} else {
if (!_find_identifier(p_block, false, p_builtin_types, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
_set_error("Unknown identifier in expression: " + String(identifier));
- return NULL;
+ return nullptr;
}
if (ident_type == IDENTIFIER_FUNCTION) {
_set_error("Can't use function as identifier: " + String(identifier));
- return NULL;
+ return nullptr;
}
}
- Node *index_expression = NULL;
- Node *call_expression = NULL;
+ Node *index_expression = nullptr;
+ Node *call_expression = nullptr;
if (array_size > 0) {
tk = _get_token();
if (tk.type != TK_BRACKET_OPEN && tk.type != TK_PERIOD) {
_set_error("Expected '[' or '.'");
- return NULL;
+ return nullptr;
}
if (tk.type == TK_PERIOD) {
@@ -3685,17 +3698,17 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
call_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
p_block->block_tag = SubClassTag::TAG_GLOBAL;
if (!call_expression)
- return NULL;
+ return nullptr;
data_type = call_expression->get_datatype();
} else { // indexing
index_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!index_expression)
- return NULL;
+ return nullptr;
if (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT) {
_set_error("Only integer expressions are allowed for indexing");
- return NULL;
+ return nullptr;
}
if (index_expression->type == Node::TYPE_CONSTANT) {
@@ -3705,7 +3718,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
int value = cnode->values[0].sint;
if (value < 0 || value >= array_size) {
_set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
- return NULL;
+ return nullptr;
}
}
}
@@ -3714,7 +3727,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_BRACKET_CLOSE) {
_set_error("Expected ']'");
- return NULL;
+ return nullptr;
}
}
@@ -3750,18 +3763,18 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
case TK_OP_BIT_INVERT: e.op = OP_BIT_INVERT; break;
case TK_OP_INCREMENT: e.op = OP_INCREMENT; break;
case TK_OP_DECREMENT: e.op = OP_DECREMENT; break;
- default: ERR_FAIL_V(NULL);
+ default: ERR_FAIL_V(nullptr);
}
expression.push_back(e);
continue;
} else {
_set_error("Expected expression, found: " + get_token_text(tk));
- return NULL;
+ return nullptr;
//nothing
}
- ERR_FAIL_COND_V(!expr, NULL);
+ ERR_FAIL_COND_V(!expr, nullptr);
/* OK now see what's NEXT to the operator.. */
/* OK now see what's NEXT to the operator.. */
@@ -3791,7 +3804,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (identifier == StringName()) {
_set_error("Expected identifier as member");
- return NULL;
+ return nullptr;
}
String ident = identifier;
@@ -4042,12 +4055,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (mix_error) {
_set_error("Cannot combine symbols from different sets in expression ." + ident);
- return NULL;
+ return nullptr;
}
if (!ok) {
_set_error("Invalid member for " + (dt == TYPE_STRUCT ? st : get_datatype_name(dt)) + " expression: ." + ident);
- return NULL;
+ return nullptr;
}
MemberNode *mn = alloc_node<MemberNode>();
@@ -4066,16 +4079,16 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type == TK_PERIOD) {
_set_error("Nested array length() is not yet implemented");
- return NULL;
+ return nullptr;
} else if (tk.type == TK_BRACKET_OPEN) {
Node *index_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!index_expression)
- return NULL;
+ return nullptr;
if (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT) {
_set_error("Only integer expressions are allowed for indexing");
- return NULL;
+ return nullptr;
}
if (index_expression->type == Node::TYPE_CONSTANT) {
@@ -4085,7 +4098,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
int value = cnode->values[0].sint;
if (value < 0 || value >= array_size) {
_set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
- return NULL;
+ return nullptr;
}
}
}
@@ -4094,13 +4107,13 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_BRACKET_CLOSE) {
_set_error("Expected ']'");
- return NULL;
+ return nullptr;
}
mn->index_expression = index_expression;
} else {
_set_error("Expected '[' or '.'");
- return NULL;
+ return nullptr;
}
}
@@ -4120,11 +4133,11 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
Node *index = _parse_and_reduce_expression(p_block, p_builtin_types);
if (!index)
- return NULL;
+ return nullptr;
if (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT) {
_set_error("Only integer datatypes are allowed for indexing");
- return NULL;
+ return nullptr;
}
DataType member_type = TYPE_VOID;
@@ -4139,7 +4152,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
if (index_constant >= 2) {
_set_error("Index out of range (0-1)");
- return NULL;
+ return nullptr;
}
}
@@ -4162,7 +4175,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
if (index_constant >= 3) {
_set_error("Index out of range (0-2)");
- return NULL;
+ return nullptr;
}
}
@@ -4184,7 +4197,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
if (index_constant >= 4) {
_set_error("Index out of range (0-3)");
- return NULL;
+ return nullptr;
}
}
@@ -4199,7 +4212,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
break;
default: {
_set_error("Object of type '" + (expr->get_datatype() == TYPE_STRUCT ? expr->get_datatype_name() : get_datatype_name(expr->get_datatype())) + "' can't be indexed");
- return NULL;
+ return nullptr;
}
}
@@ -4213,7 +4226,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
tk = _get_token();
if (tk.type != TK_BRACKET_CLOSE) {
_set_error("Expected ']' after indexing expression");
- return NULL;
+ return nullptr;
}
} else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
@@ -4224,12 +4237,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (!_validate_operator(op, &op->return_cache)) {
_set_error("Invalid base type for increment/decrement operator");
- return NULL;
+ return nullptr;
}
if (!_validate_assign(expr, p_builtin_types)) {
_set_error("Invalid use of increment/decrement operator in constant expression.");
- return NULL;
+ return nullptr;
}
expr = op;
} else {
@@ -4287,7 +4300,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
case TK_COLON: o.op = OP_SELECT_ELSE; break;
default: {
_set_error("Invalid token for operator: " + get_token_text(tk));
- return NULL;
+ return nullptr;
}
}
@@ -4379,7 +4392,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
break;
default:
- ERR_FAIL_V(NULL); //unexpected operator
+ ERR_FAIL_V(nullptr); //unexpected operator
}
if (priority < min_priority) {
@@ -4392,7 +4405,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
}
- ERR_FAIL_COND_V(next_op == -1, NULL);
+ ERR_FAIL_COND_V(next_op == -1, nullptr);
// OK! create operator..
// OK! create operator..
@@ -4405,7 +4418,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (expr_pos == expression.size()) {
//can happen..
_set_error("Unexpected end of expression...");
- return NULL;
+ return nullptr;
}
}
@@ -4417,7 +4430,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_builtin_types)) {
_set_error("Can't use increment/decrement operator in constant expression.");
- return NULL;
+ return nullptr;
}
op->arguments.push_back(expression[i + 1].node);
@@ -4433,7 +4446,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
at += get_datatype_name(op->arguments[j]->get_datatype());
}
_set_error("Invalid arguments to unary operator '" + get_operator_text(op->op) + "' :" + at);
- return NULL;
+ return nullptr;
}
expression.remove(i + 1);
}
@@ -4442,12 +4455,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (next_op < 1 || next_op >= (expression.size() - 1)) {
_set_error("Parser bug...");
- ERR_FAIL_V(NULL);
+ ERR_FAIL_V(nullptr);
}
if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
_set_error("Missing matching ':' for select operator");
- return NULL;
+ return nullptr;
}
OperatorNode *op = alloc_node<OperatorNode>();
@@ -4467,7 +4480,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
at += get_datatype_name(op->arguments[i]->get_datatype());
}
_set_error("Invalid argument to ternary ?: operator: " + at);
- return NULL;
+ return nullptr;
}
for (int i = 0; i < 4; i++) {
@@ -4478,7 +4491,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (next_op < 1 || next_op >= (expression.size() - 1)) {
_set_error("Parser bug...");
- ERR_FAIL_V(NULL);
+ ERR_FAIL_V(nullptr);
}
OperatorNode *op = alloc_node<OperatorNode>();
@@ -4487,7 +4500,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (expression[next_op - 1].is_op) {
_set_error("Parser bug...");
- ERR_FAIL_V(NULL);
+ ERR_FAIL_V(nullptr);
}
if (_is_operator_assign(op->op)) {
@@ -4496,7 +4509,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
if (!_validate_assign(expression[next_op - 1].node, p_builtin_types, &assign_message)) {
_set_error(assign_message);
- return NULL;
+ return nullptr;
}
}
@@ -4528,7 +4541,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
}
}
_set_error("Invalid arguments to operator '" + get_operator_text(op->op) + "' :" + at);
- return NULL;
+ return nullptr;
}
expression.remove(next_op);
@@ -4658,7 +4671,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_
ShaderLanguage::Node *expr = _parse_expression(p_block, p_builtin_types);
if (!expr) //errored
- return NULL;
+ return nullptr;
expr = _reduce_expression(p_block, expr);
@@ -4741,7 +4754,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
tk = _get_token();
- Node *vardecl = NULL;
+ Node *vardecl = nullptr;
while (true) {
@@ -4752,7 +4765,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
StringName name = tk.text;
ShaderLanguage::IdentifierType itype;
- if (_find_identifier(p_block, true, p_builtin_types, name, (ShaderLanguage::DataType *)0, &itype)) {
+ if (_find_identifier(p_block, true, p_builtin_types, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
if (itype != IDENTIFIER_FUNCTION) {
_set_error("Redefinition of '" + String(name) + "'");
return ERR_PARSE_ERROR;
@@ -5025,7 +5038,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
VariableDeclarationNode::Declaration decl;
decl.name = name;
- decl.initializer = NULL;
+ decl.initializer = nullptr;
//variable created with assignment! must parse an expression
Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
@@ -5061,7 +5074,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
VariableDeclarationNode::Declaration decl;
decl.name = name;
- decl.initializer = NULL;
+ decl.initializer = nullptr;
node->declarations.push_back(decl);
}
@@ -5326,7 +5339,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, Bui
// while() {}
bool is_do = tk.type == TK_CF_DO;
- BlockNode *do_block = NULL;
+ BlockNode *do_block = nullptr;
if (is_do) {
do_block = alloc_node<BlockNode>();
@@ -5685,6 +5698,8 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
int texture_uniforms = 0;
int uniforms = 0;
+ int instance_index = 0;
+ ShaderNode::Uniform::Scope uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
while (tk.type != TK_EOF) {
@@ -5694,7 +5709,7 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
while (true) {
StringName mode;
- _get_completable_identifier(NULL, COMPLETION_RENDER_MODE, mode);
+ _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
if (mode == StringName()) {
_set_error("Expected identifier for render mode");
@@ -5853,6 +5868,27 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
shader->vstructs.push_back(st); // struct's order is important!
} break;
+ case TK_GLOBAL: {
+
+ tk = _get_token();
+ if (tk.type != TK_UNIFORM) {
+ _set_error("Expected 'uniform' after 'global'");
+ return ERR_PARSE_ERROR;
+ }
+ uniform_scope = ShaderNode::Uniform::SCOPE_GLOBAL;
+ };
+ [[fallthrough]];
+ case TK_INSTANCE: {
+ if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL) {
+ tk = _get_token();
+ if (tk.type != TK_UNIFORM) {
+ _set_error("Expected 'uniform' after 'instance'");
+ return ERR_PARSE_ERROR;
+ }
+ uniform_scope = ShaderNode::Uniform::SCOPE_INSTANCE;
+ }
+ };
+ [[fallthrough]];
case TK_UNIFORM:
case TK_VARYING: {
@@ -5898,7 +5934,7 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
name = tk.text;
- if (_find_identifier(NULL, false, Map<StringName, BuiltInInfo>(), name)) {
+ if (_find_identifier(nullptr, false, Map<StringName, BuiltInInfo>(), name)) {
_set_error("Redefinition of '" + String(name) + "'");
return ERR_PARSE_ERROR;
}
@@ -5910,25 +5946,50 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
if (uniform) {
+ if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
+ //validate global uniform
+ DataType gvtype = global_var_get_type_func(name);
+ if (gvtype == TYPE_MAX) {
+ _set_error("Global uniform '" + String(name) + "' does not exist. Create it in Project Settings.");
+ return ERR_PARSE_ERROR;
+ }
+
+ if (type != gvtype) {
+ _set_error("Global uniform '" + String(name) + "' must be of type '" + get_datatype_name(gvtype) + "'.");
+ return ERR_PARSE_ERROR;
+ }
+ }
ShaderNode::Uniform uniform2;
if (is_sampler_type(type)) {
+ if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
+ _set_error("Uniforms with 'instance' qualifiers can't be of sampler type.");
+ return ERR_PARSE_ERROR;
+ }
uniform2.texture_order = texture_uniforms++;
uniform2.order = -1;
if (_validate_datatype(type) != OK) {
return ERR_PARSE_ERROR;
}
} else {
+ if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL && (type == TYPE_MAT2 || type == TYPE_MAT3 || type == TYPE_MAT4)) {
+ _set_error("Uniforms with 'instance' qualifiers can't be of matrix type.");
+ return ERR_PARSE_ERROR;
+ }
+
uniform2.texture_order = -1;
uniform2.order = uniforms++;
}
uniform2.type = type;
+ uniform2.scope = uniform_scope;
uniform2.precision = precision;
//todo parse default value
tk = _get_token();
+ int custom_instance_index = -1;
+
if (tk.type == TK_COLON) {
//hint
do {
@@ -6039,7 +6100,45 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
if (tk.type != TK_PARENTHESIS_CLOSE) {
- _set_error("Expected ','");
+ _set_error("Expected ')'");
+ return ERR_PARSE_ERROR;
+ }
+ } else if (tk.type == TK_HINT_INSTANCE_INDEX) {
+
+ if (custom_instance_index != -1) {
+ _set_error("Can only specify 'instance_index' once.");
+ return ERR_PARSE_ERROR;
+ }
+
+ tk = _get_token();
+ if (tk.type != TK_PARENTHESIS_OPEN) {
+ _set_error("Expected '(' after 'instance_index'");
+ return ERR_PARSE_ERROR;
+ }
+
+ tk = _get_token();
+
+ if (tk.type == TK_OP_SUB) {
+ _set_error("The instance index can't be negative.");
+ return ERR_PARSE_ERROR;
+ }
+
+ if (tk.type != TK_INT_CONSTANT) {
+ _set_error("Expected integer constant");
+ return ERR_PARSE_ERROR;
+ }
+
+ custom_instance_index = tk.constant;
+
+ if (custom_instance_index >= MAX_INSTANCE_UNIFORM_INDICES) {
+ _set_error("Allowed instance uniform indices are 0-" + itos(MAX_INSTANCE_UNIFORM_INDICES - 1));
+ return ERR_PARSE_ERROR;
+ }
+
+ tk = _get_token();
+
+ if (tk.type != TK_PARENTHESIS_CLOSE) {
+ _set_error("Expected ')'");
return ERR_PARSE_ERROR;
}
} else if (tk.type == TK_FILTER_LINEAR) {
@@ -6072,9 +6171,23 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
} while (tk.type == TK_COMMA);
}
+ if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
+ if (custom_instance_index >= 0) {
+ uniform2.instance_index = custom_instance_index;
+ } else {
+ uniform2.instance_index = instance_index++;
+ if (instance_index > MAX_INSTANCE_UNIFORM_INDICES) {
+ _set_error("Too many 'instance' uniforms in shader, maximum supported is " + itos(MAX_INSTANCE_UNIFORM_INDICES));
+ return ERR_PARSE_ERROR;
+ }
+ }
+ }
+
+ //reset scope for next uniform
+
if (tk.type == TK_OP_ASSIGN) {
- Node *expr = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
+ Node *expr = _parse_and_reduce_expression(nullptr, Map<StringName, BuiltInInfo>());
if (!expr)
return ERR_PARSE_ERROR;
if (expr->type != Node::TYPE_CONSTANT) {
@@ -6094,6 +6207,8 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
shader->uniforms[name] = uniform2;
+ //reset scope for next uniform
+ uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
if (tk.type != TK_SEMICOLON) {
_set_error("Expected ';'");
@@ -6191,14 +6306,14 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
_set_tkpos(prev_pos);
- _get_completable_identifier(NULL, COMPLETION_MAIN_FUNCTION, name);
+ _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
if (name == StringName()) {
_set_error("Expected function name after datatype");
return ERR_PARSE_ERROR;
}
- if (_find_identifier(NULL, false, Map<StringName, BuiltInInfo>(), name)) {
+ if (_find_identifier(nullptr, false, Map<StringName, BuiltInInfo>(), name)) {
_set_error("Redefinition of '" + String(name) + "'");
return ERR_PARSE_ERROR;
}
@@ -6219,10 +6334,11 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
while (true) {
ShaderNode::Constant constant;
+ constant.name = name;
constant.type = is_struct ? TYPE_STRUCT : type;
constant.type_str = struct_name;
constant.precision = precision;
- constant.initializer = NULL;
+ constant.initializer = nullptr;
if (tk.type == TK_OP_ASSIGN) {
@@ -6232,7 +6348,7 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
//variable created with assignment! must parse an expression
- Node *expr = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
+ Node *expr = _parse_and_reduce_expression(nullptr, Map<StringName, BuiltInInfo>());
if (!expr)
return ERR_PARSE_ERROR;
if (expr->type == Node::TYPE_OPERATOR && ((OperatorNode *)expr)->op == OP_CALL) {
@@ -6258,6 +6374,8 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
shader->constants[name] = constant;
+ shader->vconstants.push_back(constant);
+
if (tk.type == TK_COMMA) {
tk = _get_token();
if (tk.type != TK_IDENTIFIER) {
@@ -6266,7 +6384,7 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
}
name = tk.text;
- if (_find_identifier(NULL, false, Map<StringName, BuiltInInfo>(), name)) {
+ if (_find_identifier(nullptr, false, Map<StringName, BuiltInInfo>(), name)) {
_set_error("Redefinition of '" + String(name) + "'");
return ERR_PARSE_ERROR;
}
@@ -6405,7 +6523,7 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
pname = tk.text;
ShaderLanguage::IdentifierType itype;
- if (_find_identifier(func_node->body, false, builtin_types, pname, (ShaderLanguage::DataType *)0, &itype)) {
+ if (_find_identifier(func_node->body, false, builtin_types, pname, (ShaderLanguage::DataType *)nullptr, &itype)) {
if (itype != IDENTIFIER_FUNCTION) {
_set_error("Redefinition of '" + String(pname) + "'");
return ERR_PARSE_ERROR;
@@ -6639,13 +6757,14 @@ String ShaderLanguage::get_shader_type(const String &p_code) {
return String();
}
-Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
+Error ShaderLanguage::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) {
clear();
code = p_code;
+ global_var_get_type_func = p_global_variable_type_func;
- nodes = NULL;
+ nodes = nullptr;
shader = alloc_node<ShaderNode>();
Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
@@ -6656,13 +6775,14 @@ Error ShaderLanguage::compile(const String &p_code, const Map<StringName, Functi
return OK;
}
-Error ShaderLanguage::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 ShaderLanguage::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) {
clear();
code = p_code;
- nodes = NULL;
+ nodes = nullptr;
+ global_var_get_type_func = p_global_variable_type_func;
shader = alloc_node<ShaderNode>();
_parse_shader(p_functions, p_render_modes, p_shader_types);
@@ -7001,7 +7121,7 @@ ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
ShaderLanguage::ShaderLanguage() {
- nodes = NULL;
+ nodes = nullptr;
completion_class = TAG_GLOBAL;
}