Skip to content

Commit e1e3f74

Browse files
committed
Merge pull request #111277 from Chaosus/shader_fix_ternary_structs
Fix ternary expression for structs in shaders
2 parents 90cab74 + fe1447d commit e1e3f74

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

servers/rendering/shader_language.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,10 +1555,11 @@ bool ShaderLanguage::_find_identifier(const BlockNode *p_block, bool p_allow_rea
15551555
return false;
15561556
}
15571557

1558-
bool ShaderLanguage::_validate_operator(const BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size) {
1558+
bool ShaderLanguage::_validate_operator(const BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size, StringName *r_ret_struct_name) {
15591559
bool valid = false;
15601560
DataType ret_type = TYPE_VOID;
15611561
int ret_size = 0;
1562+
String ret_struct_name;
15621563

15631564
switch (p_op->op) {
15641565
case OP_EQUAL:
@@ -2003,7 +2004,23 @@ bool ShaderLanguage::_validate_operator(const BlockNode *p_block, const Function
20032004
DataType nb = p_op->arguments[1]->get_datatype();
20042005
DataType nc = p_op->arguments[2]->get_datatype();
20052006

2006-
valid = na == TYPE_BOOL && (nb == nc) && !is_sampler_type(nb);
2007+
bool is_same = false;
2008+
if (nb == nc) {
2009+
is_same = true;
2010+
2011+
if (nb == TYPE_STRUCT) {
2012+
String tb = p_op->arguments[1]->get_datatype_name();
2013+
String tc = p_op->arguments[2]->get_datatype_name();
2014+
2015+
if (tb != tc) {
2016+
break;
2017+
}
2018+
2019+
ret_struct_name = tb;
2020+
}
2021+
}
2022+
2023+
valid = na == TYPE_BOOL && is_same && !is_sampler_type(nb);
20072024
ret_type = nb;
20082025
ret_size = sa;
20092026
} break;
@@ -2018,6 +2035,9 @@ bool ShaderLanguage::_validate_operator(const BlockNode *p_block, const Function
20182035
if (r_ret_size) {
20192036
*r_ret_size = ret_size;
20202037
}
2038+
if (r_ret_struct_name) {
2039+
*r_ret_struct_name = ret_struct_name;
2040+
}
20212041

20222042
if (valid && (!p_block || p_block->use_op_eval)) {
20232043
// Need to be placed here and not in the `_reduce_expression` because otherwise expressions like `1 + 2 / 2` will not work correctly.
@@ -7699,7 +7719,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
76997719

77007720
expression.write[next_op - 1].is_op = false;
77017721
expression.write[next_op - 1].node = op;
7702-
if (!_validate_operator(p_block, p_function_info, op, &op->return_cache, &op->return_array_size)) {
7722+
if (!_validate_operator(p_block, p_function_info, op, &op->return_cache, &op->return_array_size, &op->struct_name)) {
77037723
if (error_set) {
77047724
return nullptr;
77057725
}
@@ -7709,7 +7729,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
77097729
if (i > 0) {
77107730
at += ", ";
77117731
}
7712-
at += get_datatype_name(op->arguments[i]->get_datatype());
7732+
DataType dt = op->arguments[i]->get_datatype();
7733+
if (dt == TYPE_STRUCT) {
7734+
at += op->arguments[i]->get_datatype_name();
7735+
} else {
7736+
at += get_datatype_name(dt);
7737+
}
77137738
if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
77147739
at += "[";
77157740
at += itos(op->arguments[i]->get_array_size());

servers/rendering/shader_language.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ class ShaderLanguage {
11321132
#endif // DEBUG_ENABLED
11331133
bool _is_operator_assign(Operator p_op) const;
11341134
bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
1135-
bool _validate_operator(const BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr);
1135+
bool _validate_operator(const BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr, StringName *r_ret_struct_name = nullptr);
11361136

11371137
Vector<Scalar> _get_node_values(const BlockNode *p_block, const FunctionInfo &p_function_info, Node *p_node);
11381138
bool _eval_operator(const BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_op);

0 commit comments

Comments
 (0)