From 537316098f6292909ca9ae68b054280198b3d8d7 Mon Sep 17 00:00:00 2001 From: Yuri Rubinsky Date: Sun, 16 Jun 2024 10:16:52 +0300 Subject: [PATCH] Fix bugs in visual shader varyings --- .../plugins/visual_shader_editor_plugin.cpp | 9 +++- scene/resources/visual_shader.cpp | 53 ++++++++++--------- scene/resources/visual_shader.h | 9 ++-- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 290e0a32398..8d027bccef2 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -806,6 +806,11 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id, bool parameter_ref->update_parameter_type(); } + Ref varying = vsnode; + if (varying.is_valid()) { + varying->set_shader_rid(visual_shader->get_rid()); + } + Ref parameter = vsnode; HBoxContainer *hb = nullptr; @@ -3859,13 +3864,13 @@ void VisualShaderEditor::_remove_varying(const String &p_name) { } void VisualShaderEditor::_update_varyings() { - VisualShaderNodeVarying::clear_varyings(); + VisualShaderNodeVarying::clear_varyings(visual_shader->get_rid()); for (int i = 0; i < visual_shader->get_varyings_count(); i++) { const VisualShader::Varying *var = visual_shader->get_varying_by_index(i); if (var != nullptr) { - VisualShaderNodeVarying::add_varying(var->name, var->mode, var->type); + VisualShaderNodeVarying::add_varying(visual_shader->get_rid(), var->name, var->mode, var->type); } } } diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index a54f1d376cf..bedac4e9339 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -1861,7 +1861,7 @@ void VisualShader::_get_property_list(List *p_list) const { } for (const KeyValue &E : varyings) { - p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", PNAME("varyings"), E.key))); + p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", PNAME("varyings"), E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); } for (int i = 0; i < TYPE_MAX; i++) { @@ -2620,7 +2620,7 @@ void VisualShader::_update_shader() const { default: break; } - global_code += E.key + ";\n"; + global_code += vformat("var_%s;\n", E.key); } global_code += "\n"; @@ -2689,7 +2689,7 @@ void VisualShader::_update_shader() const { default: break; } - varying_code += vformat(" %s = %s;\n", E.key, code2); + varying_code += vformat(" var_%s = %s;\n", E.key, code2); } is_empty_func = false; } @@ -5048,18 +5048,18 @@ VisualShaderNodeGlobalExpression::VisualShaderNodeGlobalExpression() { ////////////// Varying -List varyings; +RBMap> varyings; -void VisualShaderNodeVarying::add_varying(const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type) { // static - varyings.push_back({ p_name, p_mode, p_type }); +void VisualShaderNodeVarying::add_varying(RID p_shader_rid, const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type) { // static + varyings[p_shader_rid].push_back({ p_name, p_mode, p_type }); } -void VisualShaderNodeVarying::clear_varyings() { // static - varyings.clear(); +void VisualShaderNodeVarying::clear_varyings(RID p_shader_rid) { // static + varyings[p_shader_rid].clear(); } -bool VisualShaderNodeVarying::has_varying(const String &p_name) { // static - for (const VisualShaderNodeVarying::Varying &E : varyings) { +bool VisualShaderNodeVarying::has_varying(RID p_shader_rid, const String &p_name) { // static + for (const VisualShaderNodeVarying::Varying &E : varyings[p_shader_rid]) { if (E.name == p_name) { return true; } @@ -5067,19 +5067,23 @@ bool VisualShaderNodeVarying::has_varying(const String &p_name) { // static return false; } +void VisualShaderNodeVarying::set_shader_rid(const RID &p_shader_rid) { + shader_rid = p_shader_rid; +} + int VisualShaderNodeVarying::get_varyings_count() const { - return varyings.size(); + return varyings[shader_rid].size(); } String VisualShaderNodeVarying::get_varying_name_by_index(int p_idx) const { - if (p_idx >= 0 && p_idx < varyings.size()) { - return varyings.get(p_idx).name; + if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) { + return varyings[shader_rid].get(p_idx).name; } return ""; } VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_name(const String &p_name) const { - for (const VisualShaderNodeVarying::Varying &varying : varyings) { + for (const VisualShaderNodeVarying::Varying &varying : varyings[shader_rid]) { if (varying.name == p_name) { return varying.type; } @@ -5088,14 +5092,14 @@ VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_name(cons } VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_index(int p_idx) const { - if (p_idx >= 0 && p_idx < varyings.size()) { - return varyings.get(p_idx).type; + if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) { + return varyings[shader_rid].get(p_idx).type; } return VisualShader::VARYING_TYPE_FLOAT; } VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_name(const String &p_name) const { - for (const VisualShaderNodeVarying::Varying &varying : varyings) { + for (const VisualShaderNodeVarying::Varying &varying : varyings[shader_rid]) { if (varying.name == p_name) { return varying.mode; } @@ -5104,15 +5108,15 @@ VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_name(cons } VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_index(int p_idx) const { - if (p_idx >= 0 && p_idx < varyings.size()) { - return varyings.get(p_idx).mode; + if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) { + return varyings[shader_rid].get(p_idx).mode; } return VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT; } VisualShaderNodeVarying::PortType VisualShaderNodeVarying::get_port_type_by_index(int p_idx) const { - if (p_idx >= 0 && p_idx < varyings.size()) { - return get_port_type(varyings.get(p_idx).type, 0); + if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) { + return get_port_type(varyings[shader_rid].get(p_idx).type, 0); } return PORT_TYPE_SCALAR; } @@ -5239,7 +5243,7 @@ String VisualShaderNodeVaryingSetter::generate_code(Shader::Mode p_mode, VisualS if (varying_name == "[None]") { return code; } - code += vformat(" %s = %s;\n", varying_name, p_input_vars[0]); + code += vformat(" var_%s = %s;\n", varying_name, p_input_vars[0]); return code; } @@ -5284,7 +5288,7 @@ String VisualShaderNodeVaryingGetter::generate_code(Shader::Mode p_mode, VisualS String from = varying_name; String from2; - if (varying_name == "[None]") { + if (varying_name == "[None]" || p_for_preview) { switch (varying_type) { case VisualShader::VARYING_TYPE_FLOAT: from = "0.0"; @@ -5313,8 +5317,9 @@ String VisualShaderNodeVaryingGetter::generate_code(Shader::Mode p_mode, VisualS default: break; } + return vformat(" %s = %s;\n", p_output_vars[0], from); } - return vformat(" %s = %s;\n", p_output_vars[0], from); + return vformat(" %s = var_%s;\n", p_output_vars[0], from); } VisualShaderNodeVaryingGetter::VisualShaderNodeVaryingGetter() { diff --git a/scene/resources/visual_shader.h b/scene/resources/visual_shader.h index 18cdc8342b2..9cd8f86d0fe 100644 --- a/scene/resources/visual_shader.h +++ b/scene/resources/visual_shader.h @@ -910,13 +910,16 @@ public: }; protected: + RID shader_rid; VisualShader::VaryingType varying_type = VisualShader::VARYING_TYPE_FLOAT; String varying_name = "[None]"; public: // internal - static void add_varying(const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type); - static void clear_varyings(); - static bool has_varying(const String &p_name); + static void add_varying(RID p_shader_rid, const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type); + static void clear_varyings(RID p_shader_rid); + static bool has_varying(RID p_shader_rid, const String &p_name); + + void set_shader_rid(const RID &p_shader); int get_varyings_count() const; String get_varying_name_by_index(int p_idx) const;