diff options
author | Yuri Roubinsky <chaosus89@gmail.com> | 2020-02-13 19:43:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-13 19:43:45 +0300 |
commit | 5c3944e7da9ca72077cb5348fadc86347904838c (patch) | |
tree | 7475369fbc64c5a9a06b0fed824eee94bbc69a74 | |
parent | ef51726ff3cd60e0caf9ce55c9c85cc47bfe0420 (diff) | |
parent | 01de6513ac12c10139817deeecb357c023b00710 (diff) |
Merge pull request #36177 from Chaosus/shader_fix_swizzling
Fix bugs in shader swizzling
-rw-r--r-- | servers/visual/shader_language.cpp | 121 | ||||
-rw-r--r-- | servers/visual/shader_language.h | 4 |
2 files changed, 123 insertions, 2 deletions
diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp index a5b028b15b..6166489302 100644 --- a/servers/visual/shader_language.cpp +++ b/servers/visual/shader_language.cpp @@ -2933,6 +2933,13 @@ bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltI } else if (p_node->type == Node::TYPE_MEMBER) { MemberNode *member = static_cast<MemberNode *>(p_node); + + if (member->has_swizzling_duplicates) { + if (r_message) + *r_message = RTR("Swizzling assignment contains duplicates."); + return false; + } + return _validate_assign(member->owner, p_builtin_types, r_message); } else if (p_node->type == Node::TYPE_VARIABLE) { @@ -3710,9 +3717,17 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons String ident = identifier; bool ok = true; + bool repeated = false; DataType member_type = TYPE_VOID; StringName member_struct_name = ""; int array_size = 0; + + Set<char> position_symbols; + Set<char> color_symbols; + Set<char> texture_symbols; + + bool mix_error = false; + switch (dt) { case TYPE_STRUCT: { ok = false; @@ -3758,8 +3773,39 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons switch (c[i]) { case 'r': case 'g': + if (position_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!color_symbols.has(c[i])) { + color_symbols.insert(c[i]); + } else { + repeated = true; + } + break; case 'x': case 'y': + if (color_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!position_symbols.has(c[i])) { + position_symbols.insert(c[i]); + } else { + repeated = true; + } + break; + case 's': + case 't': + if (color_symbols.size() > 0 || position_symbols.size() > 0) { + mix_error = true; + break; + } + if (!texture_symbols.has(c[i])) { + texture_symbols.insert(c[i]); + } else { + repeated = true; + } break; default: ok = false; @@ -3794,9 +3840,41 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons case 'r': case 'g': case 'b': + if (position_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!color_symbols.has(c[i])) { + color_symbols.insert(c[i]); + } else { + repeated = true; + } + break; case 'x': case 'y': case 'z': + if (color_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!position_symbols.has(c[i])) { + position_symbols.insert(c[i]); + } else { + repeated = true; + } + break; + case 's': + case 't': + case 'p': + if (color_symbols.size() > 0 || position_symbols.size() > 0) { + mix_error = true; + break; + } + if (!texture_symbols.has(c[i])) { + texture_symbols.insert(c[i]); + } else { + repeated = true; + } break; default: ok = false; @@ -3832,10 +3910,43 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons case 'g': case 'b': case 'a': + if (position_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!color_symbols.has(c[i])) { + color_symbols.insert(c[i]); + } else { + repeated = true; + } + break; case 'x': case 'y': case 'z': case 'w': + if (color_symbols.size() > 0 || texture_symbols.size() > 0) { + mix_error = true; + break; + } + if (!position_symbols.has(c[i])) { + position_symbols.insert(c[i]); + } else { + repeated = true; + } + break; + case 's': + case 't': + case 'p': + case 'q': + if (color_symbols.size() > 0 || position_symbols.size() > 0) { + mix_error = true; + break; + } + if (!texture_symbols.has(c[i])) { + texture_symbols.insert(c[i]); + } else { + repeated = true; + } break; default: ok = false; @@ -3850,8 +3961,12 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons } } - if (!ok) { + if (mix_error) { + _set_error("Cannot combine symbols from different sets in expression ." + ident); + return NULL; + } + if (!ok) { _set_error("Invalid member for " + (dt == TYPE_STRUCT ? st : get_datatype_name(dt)) + " expression: ." + ident); return NULL; } @@ -3865,6 +3980,8 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons mn->array_size = array_size; mn->name = ident; mn->owner = expr; + mn->has_swizzling_duplicates = repeated; + if (array_size > 0) { tk = _get_token(); @@ -6684,6 +6801,7 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct const char colv[4] = { 'r', 'g', 'b', 'a' }; const char coordv[4] = { 'x', 'y', 'z', 'w' }; + const char coordt[4] = { 's', 't', 'p', 'q' }; int limit = 0; @@ -6721,6 +6839,7 @@ Error ShaderLanguage::complete(const String &p_code, const Map<StringName, Funct for (int i = 0; i < limit; i++) { r_options->push_back(ScriptCodeCompletionOption(String::chr(colv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT)); r_options->push_back(ScriptCodeCompletionOption(String::chr(coordv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT)); + r_options->push_back(ScriptCodeCompletionOption(String::chr(coordt[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT)); } } break; diff --git a/servers/visual/shader_language.h b/servers/visual/shader_language.h index e56fb35a8b..5a69a84650 100644 --- a/servers/visual/shader_language.h +++ b/servers/visual/shader_language.h @@ -538,6 +538,7 @@ public: StringName name; Node *owner; Node *index_expression; + bool has_swizzling_duplicates; virtual DataType get_datatype() const { return datatype; } virtual String get_datatype_name() const { return String(struct_name); } @@ -549,7 +550,8 @@ public: datatype(TYPE_VOID), array_size(0), owner(NULL), - index_expression(NULL) {} + index_expression(NULL), + has_swizzling_duplicates(false) {} }; struct StructNode : public Node { |