Ensure implicit conversions for scalar constants work in shaders, closes #26239

This commit is contained in:
Juan Linietsky 2019-02-26 23:20:39 -03:00
parent df7541d2f9
commit ffb9f342a5

View file

@ -2074,7 +2074,9 @@ bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p
bool fail = false;
for (int i = 0; i < argcount; i++) {
if (args[i] != builtin_func_defs[idx].args[i]) {
if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), builtin_func_defs[idx].args[i])) {
//all good, but needs implicit conversion later
} else if (args[i] != builtin_func_defs[idx].args[i]) {
fail = true;
break;
}
@ -2119,6 +2121,24 @@ bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p
outarg_idx++;
}
//implicitly convert values if possible
for (int i = 0; i < argcount; i++) {
if (get_scalar_type(args[i]) != args[i] || args[i] == builtin_func_defs[idx].args[i] || p_func->arguments[i + 1]->type != Node::TYPE_CONSTANT) {
//can't do implicit conversion here
continue;
}
//this is an implicit conversion
ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
ConstantNode *conversion = alloc_node<ConstantNode>();
conversion->datatype = builtin_func_defs[idx].args[i];
conversion->values.resize(1);
convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
p_func->arguments.write[i + 1] = conversion;
}
if (r_ret_type)
*r_ret_type = builtin_func_defs[idx].rettype;
@ -2184,13 +2204,35 @@ bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p
for (int j = 0; j < args.size(); j++) {
if (args[j] != pfunc->arguments[j].type) {
if (get_scalar_type(args[j]) == args[j] && p_func->arguments[j + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[j + 1]), pfunc->arguments[j].type)) {
//all good, but it needs implicit conversion later
} else if (args[j] != pfunc->arguments[j].type) {
fail = true;
break;
}
}
if (!fail) {
//implicitly convert values if possible
for (int k = 0; k < args.size(); k++) {
if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::TYPE_CONSTANT) {
//can't do implicit conversion here
continue;
}
//this is an implicit conversion
ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
ConstantNode *conversion = alloc_node<ConstantNode>();
conversion->datatype = pfunc->arguments[k].type;
conversion->values.resize(1);
convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
p_func->arguments.write[k + 1] = conversion;
}
if (r_ret_type)
*r_ret_type = pfunc->return_type;
return true;