Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shader failure when using non-const initializer on a constant #72494

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion servers/rendering/shader_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ void ShaderLanguage::clear() {
char_idx = 0;
error_set = false;
error_str = "";
last_const = false;
is_const_decl = false;
while (nodes) {
Node *n = nodes;
nodes = nodes->next;
Expand Down Expand Up @@ -3561,6 +3561,14 @@ bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const Functio
return false;
}

if (is_const_decl && arg->type == Node::TYPE_VARIABLE) {
const VariableNode *var = static_cast<const VariableNode *>(arg);
if (!var->is_const) {
_set_error(RTR("Expected constant expression."));
return false;
}
}

p_func->arguments.push_back(arg);

tk = _get_token();
Expand Down Expand Up @@ -5184,6 +5192,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
expr = func;

} else { //a function call
if (p_block == nullptr) { // Non-constructor function call in global space is forbidden.
if (is_const_decl) {
_set_error(RTR("Expected constant expression."));
}
return nullptr;
}

const StringName &name = identifier;

Expand Down Expand Up @@ -5460,6 +5474,10 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
_set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
return nullptr;
}
if (is_const_decl && !is_const) {
_set_error(RTR("Expected constant expression."));
return nullptr;
}
if (ident_type == IDENTIFIER_VARYING) {
TkPos prev_pos = _get_tkpos();
Token next_token = _get_token();
Expand Down Expand Up @@ -6977,6 +6995,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_fun
}
}
#endif // DEBUG_ENABLED
is_const_decl = is_const;

BlockNode::Variable var;
var.type = type;
Expand Down Expand Up @@ -7233,6 +7252,7 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_fun

vdnode->declarations.push_back(decl);
p_block->variables[name] = var;
is_const_decl = false;

if (!fixed_array_size) {
array_size = 0;
Expand Down Expand Up @@ -9101,6 +9121,7 @@ Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_f
constant.precision = precision;
constant.initializer = nullptr;
constant.array_size = array_size;
is_const_decl = true;

if (tk.type == TK_BRACKET_OPEN) {
Error error = _parse_array_size(nullptr, constants, false, nullptr, &constant.array_size, &unknown_size);
Expand Down Expand Up @@ -9360,6 +9381,7 @@ Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_f
unknown_size = false;

} else if (tk.type == TK_SEMICOLON) {
is_const_decl = false;
break;
} else {
_set_expected_error(",", ";");
Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/shader_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ class ShaderLanguage {

StringName shader_type_identifier;
StringName current_function;
bool last_const = false;
bool is_const_decl = false;
StringName last_name;
bool is_shader_inc = false;

Expand Down