From e26075c9a29fc4749de8bb5e97892c8976be6089 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 5 Sep 2023 22:29:42 +0300 Subject: [PATCH 01/11] Preparation for export shader nodes --- .../nodes/shader/materialx/material.cc | 12 ++++++----- .../blender/nodes/shader/materialx/material.h | 4 +--- .../shader/materialx/nodes/node_parser.cc | 20 ++++++++++--------- .../shader/materialx/nodes/node_parser.h | 13 ++++++++---- .../shader/materialx/nodes/output_material.cc | 12 ++++------- .../shader/materialx/nodes/output_material.h | 4 ++-- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/source/blender/nodes/shader/materialx/material.cc b/source/blender/nodes/shader/materialx/material.cc index 1e88124b3570..7c950dd6001b 100644 --- a/source/blender/nodes/shader/materialx/material.cc +++ b/source/blender/nodes/shader/materialx/material.cc @@ -17,9 +17,7 @@ namespace blender::nodes::materialx { CLG_LOGREF_DECLARE_GLOBAL(LOG_MATERIALX_SHADER, "materialx.shader"); -MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, - Material *material, - const std::string &socket_name) +MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material) { CLOG_INFO(LOG_MATERIALX_SHADER, 0, "Material: %s", material->id.name); @@ -27,10 +25,14 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, if (material->use_nodes) { material->nodetree->ensure_topology_cache(); bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL); - OutputMaterialNodeParser(doc.get(), depsgraph, material, output_node).compute(socket_name); + OutputMaterialNodeParser( + doc.get(), depsgraph, material, output_node, NodeParser::ShaderType::SurfaceMaterial) + .compute(); } else { - OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr).compute_default(); + OutputMaterialNodeParser( + doc.get(), depsgraph, material, nullptr, NodeParser::ShaderType::SurfaceMaterial) + .compute_default(); } CLOG_INFO(LOG_MATERIALX_SHADER, diff --git a/source/blender/nodes/shader/materialx/material.h b/source/blender/nodes/shader/materialx/material.h index 4c90febce6c9..b32f3ff2962a 100644 --- a/source/blender/nodes/shader/materialx/material.h +++ b/source/blender/nodes/shader/materialx/material.h @@ -15,8 +15,6 @@ namespace blender::nodes::materialx { extern struct CLG_LogRef *LOG_MATERIALX_SHADER; -MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, - Material *material, - const std::string &socket_name = "Surface"); +MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material); } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index 187590c7332f..0a223f22dedc 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -13,12 +13,14 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, const bNode *node, - const bNodeSocket *socket_out) + const bNodeSocket *socket_out, + ShaderType shader) : graph_(graph), depsgraph_(depsgraph), material_(material), node_(node), - socket_out_(socket_out) + socket_out_(socket_out), + shader_(shader) { } @@ -46,14 +48,14 @@ NodeItem NodeParser::get_input_default(int index) return get_input_default(node_->input_socket(index)); } -NodeItem NodeParser::get_input_link(const std::string &name) +NodeItem NodeParser::get_input_link(const std::string &name, ShaderType shader) { - return get_input_link(node_->input_by_identifier(name)); + return get_input_link(node_->input_by_identifier(name), shader); } -NodeItem NodeParser::get_input_link(int index) +NodeItem NodeParser::get_input_link(int index, ShaderType shader) { - return get_input_link(node_->input_socket(index)); + return get_input_link(node_->input_socket(index), shader); } NodeItem NodeParser::get_input_value(const std::string &name) @@ -99,7 +101,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket) return res; } -NodeItem NodeParser::get_input_link(const bNodeSocket &socket) +NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channel) { NodeItem res = empty(); @@ -128,7 +130,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket) /* Computing from_node with required NodeParser object */ #define CASE_NODE_TYPE(type, T) \ case type: \ - res = T(graph_, depsgraph_, material_, from_node, link->fromsock).compute_full(); \ + res = T(graph_, depsgraph_, material_, from_node, link->fromsock, channel).compute_full(); \ break; switch (from_node->typeinfo->type) { @@ -153,7 +155,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket) NodeItem NodeParser::get_input_value(const bNodeSocket &socket) { - NodeItem res = get_input_link(socket); + NodeItem res = get_input_link(socket, ShaderType::Value); if (!res) { res = get_input_default(socket); } diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index bb9986896d99..b64f852d2f14 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -13,19 +13,24 @@ namespace blender::nodes::materialx { class NodeParser { + public: + enum class ShaderType { Value = 0, BSDF, EDF, VDF, SurfaceShader, SurfaceMaterial }; + protected: MaterialX::GraphElement *graph_; const Depsgraph *depsgraph_; const Material *material_; const bNode *node_; const bNodeSocket *socket_out_; + ShaderType shader_; public: NodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, const bNode *node, - const bNodeSocket *socket_out); + const bNodeSocket *socket_out, + ShaderType shader); virtual ~NodeParser() = default; virtual NodeItem compute() = 0; @@ -34,8 +39,8 @@ class NodeParser { NodeItem create_node(const std::string &mx_category, const std::string &mx_type); NodeItem get_input_default(const std::string &name); NodeItem get_input_default(int index); - NodeItem get_input_link(const std::string &name); - NodeItem get_input_link(int index); + NodeItem get_input_link(const std::string &name, ShaderType shader = ShaderType::Value); + NodeItem get_input_link(int index, ShaderType shader = ShaderType::Value); NodeItem get_input_value(const std::string &name); NodeItem get_input_value(int index); NodeItem empty() const; @@ -43,7 +48,7 @@ class NodeParser { private: NodeItem get_input_default(const bNodeSocket &socket); - NodeItem get_input_link(const bNodeSocket &socket); + NodeItem get_input_link(const bNodeSocket &socket, ShaderType shader); NodeItem get_input_value(const bNodeSocket &socket); NodeItem compute_full(); }; diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.cc b/source/blender/nodes/shader/materialx/nodes/output_material.cc index 92fd81dbbe22..2f55348d6922 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.cc +++ b/source/blender/nodes/shader/materialx/nodes/output_material.cc @@ -8,21 +8,17 @@ namespace blender::nodes::materialx { OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, - const bNode *node) - : NodeParser(graph, depsgraph, material, node, nullptr) + const bNode *node, + ShaderType shader) + : NodeParser(graph, depsgraph, material, node, nullptr, shader) { } NodeItem OutputMaterialNodeParser::compute() -{ - return empty(); -} - -NodeItem OutputMaterialNodeParser::compute(const std::string &socket_name) { NodeItem surface = empty(); if (node_) { - surface = get_input_link(socket_name); + surface = get_input_link("Surface", ShaderType::SurfaceShader); } else { surface = create_node("standard_surface", "surfaceshader"); diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.h b/source/blender/nodes/shader/materialx/nodes/output_material.h index 986c926f33d4..189fa7233cde 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.h +++ b/source/blender/nodes/shader/materialx/nodes/output_material.h @@ -13,9 +13,9 @@ class OutputMaterialNodeParser : public NodeParser { OutputMaterialNodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, - const bNode *node); + const bNode *node, + ShaderType shader); NodeItem compute() override; - NodeItem compute(const std::string &socket_name); NodeItem compute_default(); }; -- 2.30.2 From c18534dbe4e94baf743112919c1808261504f645 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 14:49:00 +0300 Subject: [PATCH 02/11] Added shader_type to NodeParser --- .../nodes/shader/materialx/material.cc | 6 +-- .../nodes/shader/materialx/nodes/node_item.cc | 42 +++++++++++++++---- .../nodes/shader/materialx/nodes/node_item.h | 16 +++++-- .../shader/materialx/nodes/node_parser.cc | 19 +++++---- .../shader/materialx/nodes/node_parser.h | 14 +++---- .../shader/materialx/nodes/output_material.cc | 6 +-- .../shader/materialx/nodes/output_material.h | 2 +- 7 files changed, 69 insertions(+), 36 deletions(-) diff --git a/source/blender/nodes/shader/materialx/material.cc b/source/blender/nodes/shader/materialx/material.cc index 7c950dd6001b..e47937035eb0 100644 --- a/source/blender/nodes/shader/materialx/material.cc +++ b/source/blender/nodes/shader/materialx/material.cc @@ -25,13 +25,11 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater if (material->use_nodes) { material->nodetree->ensure_topology_cache(); bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL); - OutputMaterialNodeParser( - doc.get(), depsgraph, material, output_node, NodeParser::ShaderType::SurfaceMaterial) + OutputMaterialNodeParser(doc.get(), depsgraph, material, output_node, NodeItem::Type::Material) .compute(); } else { - OutputMaterialNodeParser( - doc.get(), depsgraph, material, nullptr, NodeParser::ShaderType::SurfaceMaterial) + OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr, NodeItem::Type::Material) .compute_default(); } diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index 1b1801e741cf..93b97f6bdd6d 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -71,7 +71,7 @@ bool NodeItem::operator==(const NodeItem &other) const NodeItem item1 = *this; NodeItem item2 = other; Type to_type = adjust_types(item1, item2); - if (to_type == Type::Empty) { + if (to_type == Type::None) { return false; } return item1.value->getValueString() == item2.value->getValueString(); @@ -449,7 +449,7 @@ NodeItem NodeItem::if_else(CompareOp op, auto item1 = if_val; auto item2 = else_val; Type to_type = adjust_types(item1, item2); - if (to_type == Type::Empty) { + if (to_type == Type::None) { return res; } @@ -499,7 +499,7 @@ NodeItem::Type NodeItem::type() const if (node) { return type(node->getType()); } - return Type::Empty; + return Type::None; } void NodeItem::set_input(const std::string &name, @@ -567,6 +567,9 @@ NodeItem::Type NodeItem::type(const std::string &type_str) if (type_str == "string") { return Type::String; } + if (type_str == "filename") { + return Type::Filename; + } if (type_str == "integer") { return Type::Integer; } @@ -588,7 +591,20 @@ NodeItem::Type NodeItem::type(const std::string &type_str) if (type_str == "color4") { return Type::Color4; } - return Type::Other; + if (type_str == "BSDF") { + return Type::BSDF; + } + if (type_str == "EDF") { + return Type::EDF; + } + if (type_str == "surfaceshader") { + return Type::SurfaceShader; + } + if (type_str == "material") { + return Type::Material; + } + BLI_assert_unreachable(); + return Type::None; } std::string NodeItem::type(Type type) @@ -596,6 +612,8 @@ std::string NodeItem::type(Type type) switch (type) { case Type::String: return "string"; + case Type::Filename: + return "filename"; case Type::Integer: return "integer"; case Type::Float: @@ -610,15 +628,23 @@ std::string NodeItem::type(Type type) return "color3"; case Type::Color4: return "color4"; + case Type::BSDF: + return "BSDF"; + case Type::EDF: + return "EDF"; + case Type::SurfaceShader: + return "surfaceshader"; + case Type::Material: + return "material"; default: - break; + BLI_assert_unreachable(); } return ""; } bool NodeItem::is_arithmetic(Type type) { - return type >= Type::Float; + return type >= Type::Float && type <= Type::Color4; } NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) @@ -629,7 +655,7 @@ NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) return t1; } if (!is_arithmetic(t1) || !is_arithmetic(t2)) { - return Type::Empty; + return Type::None; } if (t1 < t2) { item1 = item1.convert(t2); @@ -710,7 +736,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other, NodeItem item1 = *this; NodeItem item2 = other; Type to_type = adjust_types(item1, item2); - if (to_type == Type::Empty) { + if (to_type == Type::None) { return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.h b/source/blender/nodes/shader/materialx/nodes/node_item.h index 6e9e011ad2f2..b53cd940889c 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.h +++ b/source/blender/nodes/shader/materialx/nodes/node_item.h @@ -11,16 +11,26 @@ namespace blender::nodes::materialx { class NodeItem { public: enum class Type { - Empty = 0, - Other, /* For MaterialX types like: surfaceshader, bsdf, edf, ...*/ + None = 0, + + /* Value types */ String, + Filename, Integer, Float, Vector2, Vector3, Vector4, Color3, - Color4 + Color4, + + /* Shader types + * Note: There are only supported types */ + BSDF, + EDF, + SurfaceShader, + Material, + }; enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq }; diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index eb3aa88159a3..d46d3f1ba9cb 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -14,13 +14,13 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph, const Material *material, const bNode *node, const bNodeSocket *socket_out, - ShaderType shader) + NodeItem::Type shader_type) : graph_(graph), depsgraph_(depsgraph), material_(material), node_(node), socket_out_(socket_out), - shader_(shader) + shader_type_(shader_type) { } @@ -48,14 +48,14 @@ NodeItem NodeParser::get_input_default(int index) return get_input_default(node_->input_socket(index)); } -NodeItem NodeParser::get_input_link(const std::string &name, ShaderType shader) +NodeItem NodeParser::get_input_link(const std::string &name, NodeItem::Type shader_type) { - return get_input_link(node_->input_by_identifier(name), shader); + return get_input_link(node_->input_by_identifier(name), shader_type); } -NodeItem NodeParser::get_input_link(int index, ShaderType shader) +NodeItem NodeParser::get_input_link(int index, NodeItem::Type shader_type) { - return get_input_link(node_->input_socket(index), shader); + return get_input_link(node_->input_socket(index), shader_type); } NodeItem NodeParser::get_input_value(const std::string &name) @@ -101,7 +101,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket) return res; } -NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channel) +NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type shader_type) { NodeItem res = empty(); @@ -130,7 +130,8 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channe /* Computing from_node with required NodeParser object */ #define CASE_NODE_TYPE(type, T) \ case type: \ - res = T(graph_, depsgraph_, material_, from_node, link->fromsock, channel).compute_full(); \ + res = \ + T(graph_, depsgraph_, material_, from_node, link->fromsock, shader_type).compute_full(); \ break; switch (from_node->typeinfo->type) { @@ -163,7 +164,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channe NodeItem NodeParser::get_input_value(const bNodeSocket &socket) { - NodeItem res = get_input_link(socket, ShaderType::Value); + NodeItem res = get_input_link(socket, NodeItem::Type::None); if (!res) { res = get_input_default(socket); } diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index bef0cf7638ce..edd9f2e849b7 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -13,16 +13,13 @@ namespace blender::nodes::materialx { class NodeParser { - public: - enum class ShaderType { Value = 0, BSDF, EDF, VDF, SurfaceShader, SurfaceMaterial }; - protected: MaterialX::GraphElement *graph_; const Depsgraph *depsgraph_; const Material *material_; const bNode *node_; const bNodeSocket *socket_out_; - ShaderType shader_; + NodeItem::Type shader_type_; public: NodeParser(MaterialX::GraphElement *graph, @@ -30,7 +27,7 @@ class NodeParser { const Material *material, const bNode *node, const bNodeSocket *socket_out, - ShaderType shader); + NodeItem::Type shader_type); virtual ~NodeParser() = default; virtual NodeItem compute() = 0; @@ -40,8 +37,9 @@ class NodeParser { NodeItem create_node(const std::string &mx_category, const std::string &mx_type); NodeItem get_input_default(const std::string &name); NodeItem get_input_default(int index); - NodeItem get_input_link(const std::string &name, ShaderType shader = ShaderType::Value); - NodeItem get_input_link(int index, ShaderType shader = ShaderType::Value); + NodeItem get_input_link(const std::string &name, + NodeItem::Type shader_type = NodeItem::Type::None); + NodeItem get_input_link(int index, NodeItem::Type shader_type = NodeItem::Type::None); NodeItem get_input_value(const std::string &name); NodeItem get_input_value(int index); NodeItem empty() const; @@ -49,7 +47,7 @@ class NodeParser { private: NodeItem get_input_default(const bNodeSocket &socket); - NodeItem get_input_link(const bNodeSocket &socket, ShaderType shader); + NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type shader_type); NodeItem get_input_value(const bNodeSocket &socket); NodeItem compute_full(); }; diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.cc b/source/blender/nodes/shader/materialx/nodes/output_material.cc index 2f55348d6922..5703be395dc7 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.cc +++ b/source/blender/nodes/shader/materialx/nodes/output_material.cc @@ -9,8 +9,8 @@ OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *grap const Depsgraph *depsgraph, const Material *material, const bNode *node, - ShaderType shader) - : NodeParser(graph, depsgraph, material, node, nullptr, shader) + NodeItem::Type shader_type) + : NodeParser(graph, depsgraph, material, node, nullptr, shader_type) { } @@ -18,7 +18,7 @@ NodeItem OutputMaterialNodeParser::compute() { NodeItem surface = empty(); if (node_) { - surface = get_input_link("Surface", ShaderType::SurfaceShader); + surface = get_input_link("Surface", NodeItem::Type::SurfaceShader); } else { surface = create_node("standard_surface", "surfaceshader"); diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.h b/source/blender/nodes/shader/materialx/nodes/output_material.h index 189fa7233cde..cc078c6a1c04 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.h +++ b/source/blender/nodes/shader/materialx/nodes/output_material.h @@ -14,7 +14,7 @@ class OutputMaterialNodeParser : public NodeParser { const Depsgraph *depsgraph, const Material *material, const bNode *node, - ShaderType shader); + NodeItem::Type shader_type); NodeItem compute() override; NodeItem compute_default(); }; -- 2.30.2 From 89633acfc46b405eb2c75a361bb3698169566ba5 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 14:50:39 +0300 Subject: [PATCH 03/11] Simplified tex_checker.cc --- .../shader/materialx/nodes/tex_checker.cc | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc index 8325bb0e06ef..0276dd169d73 100644 --- a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc +++ b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc @@ -16,28 +16,13 @@ NodeItem TexCheckerNodeParser::compute() if (!vector) { vector = create_node("texcoord", "vector2"); } - vector = vector * scale; - - NodeItem separate = create_node("separate2", "multioutput"); - separate.set_input("in", vector, NodeItem::Type::Vector2); - separate.add_output("outx", NodeItem::Type::Float); - separate.add_output("outy", NodeItem::Type::Float); - - NodeItem modulo_x = create_node("modulo", "float"); - modulo_x.set_input("in1", separate, "outx"); - modulo_x.set_input("in2", value(2.0f)); - - NodeItem modulo_y = create_node("modulo", "float"); - modulo_y.set_input("in1", separate, "outy"); - modulo_y.set_input("in2", value(2.0f)); - - NodeItem ifequal = (modulo_x.floor() + modulo_y.floor()) - .if_else(NodeItem::CompareOp::Eq, value(1.0f), value(0.0f), value(1.0f)); - + vector = (vector * scale) % value(2.0f); + NodeItem mix = (vector.extract(0).floor() + vector.extract(1).floor()) + .if_else(NodeItem::CompareOp::Eq, value(1.0f), value(1.0f), value(0.0f)); NodeItem res = create_node("mix", "color3"); - res.set_input("bg", color1, NodeItem::Type::Color3); - res.set_input("fg", color2, NodeItem::Type::Color3); - res.set_input("mix", ifequal); + res.set_input("fg", color1, NodeItem::Type::Color3); + res.set_input("bg", color2, NodeItem::Type::Color3); + res.set_input("mix", mix); return res; } -- 2.30.2 From 8c1b9b8a9f2c53d7bd2ef0f4d7cda181688010fa Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 17:48:03 +0300 Subject: [PATCH 04/11] Added ShaderNodeParser class --- .../nodes/shader/materialx/material.cc | 6 +- .../shader/materialx/nodes/bsdf_principled.cc | 12 +- .../nodes/shader/materialx/nodes/node_item.cc | 160 +++++++++--------- .../nodes/shader/materialx/nodes/node_item.h | 5 +- .../shader/materialx/nodes/node_parser.cc | 131 +++++++++++--- .../shader/materialx/nodes/node_parser.h | 85 +++++++--- .../shader/materialx/nodes/output_material.cc | 41 ++++- .../shader/materialx/nodes/output_material.h | 13 +- 8 files changed, 310 insertions(+), 143 deletions(-) diff --git a/source/blender/nodes/shader/materialx/material.cc b/source/blender/nodes/shader/materialx/material.cc index e47937035eb0..a4d2f2264a7c 100644 --- a/source/blender/nodes/shader/materialx/material.cc +++ b/source/blender/nodes/shader/materialx/material.cc @@ -25,12 +25,10 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater if (material->use_nodes) { material->nodetree->ensure_topology_cache(); bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL); - OutputMaterialNodeParser(doc.get(), depsgraph, material, output_node, NodeItem::Type::Material) - .compute(); + OutputMaterialNodeParser(doc.get(), depsgraph, material, output_node).compute_full(); } else { - OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr, NodeItem::Type::Material) - .compute_default(); + OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr).compute_default(); } CLOG_INFO(LOG_MATERIALX_SHADER, diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc index 5abd13ddaab6..1514b4eb7af2 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc @@ -6,7 +6,17 @@ namespace blender::nodes::materialx { -NodeItem BSDFPrincipledNodeParser::compute() +NodeItem BSDFPrincipledNodeParser::compute_bsdf() +{ + return empty(); +} + +NodeItem BSDFPrincipledNodeParser::compute_edf() +{ + return empty(); +} + +NodeItem BSDFPrincipledNodeParser::compute_surface() { /* Getting required inputs * Note: if some inputs are not needed they won't be taken */ diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index 93b97f6bdd6d..ea41923c12da 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -12,6 +12,86 @@ namespace blender::nodes::materialx { NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {} +NodeItem::Type NodeItem::type(const std::string &type_str) +{ + if (type_str == "string") { + return Type::String; + } + if (type_str == "filename") { + return Type::Filename; + } + if (type_str == "integer") { + return Type::Integer; + } + if (type_str == "float") { + return Type::Float; + } + if (type_str == "vector2") { + return Type::Vector2; + } + if (type_str == "vector3") { + return Type::Vector3; + } + if (type_str == "vector4") { + return Type::Vector4; + } + if (type_str == "color3") { + return Type::Color3; + } + if (type_str == "color4") { + return Type::Color4; + } + if (type_str == "BSDF") { + return Type::BSDF; + } + if (type_str == "EDF") { + return Type::EDF; + } + if (type_str == "surfaceshader") { + return Type::SurfaceShader; + } + if (type_str == "material") { + return Type::Material; + } + BLI_assert_unreachable(); + return Type::None; +} + +std::string NodeItem::type(Type type) +{ + switch (type) { + case Type::String: + return "string"; + case Type::Filename: + return "filename"; + case Type::Integer: + return "integer"; + case Type::Float: + return "float"; + case Type::Vector2: + return "vector2"; + case Type::Vector3: + return "vector3"; + case Type::Vector4: + return "vector4"; + case Type::Color3: + return "color3"; + case Type::Color4: + return "color4"; + case Type::BSDF: + return "BSDF"; + case Type::EDF: + return "EDF"; + case Type::SurfaceShader: + return "surfaceshader"; + case Type::Material: + return "material"; + default: + BLI_assert_unreachable(); + } + return ""; +} + NodeItem::operator bool() const { return value || node; @@ -562,86 +642,6 @@ void NodeItem::add_output(const std::string &name, Type out_type) node->addOutput(name, type(out_type)); } -NodeItem::Type NodeItem::type(const std::string &type_str) -{ - if (type_str == "string") { - return Type::String; - } - if (type_str == "filename") { - return Type::Filename; - } - if (type_str == "integer") { - return Type::Integer; - } - if (type_str == "float") { - return Type::Float; - } - if (type_str == "vector2") { - return Type::Vector2; - } - if (type_str == "vector3") { - return Type::Vector3; - } - if (type_str == "vector4") { - return Type::Vector4; - } - if (type_str == "color3") { - return Type::Color3; - } - if (type_str == "color4") { - return Type::Color4; - } - if (type_str == "BSDF") { - return Type::BSDF; - } - if (type_str == "EDF") { - return Type::EDF; - } - if (type_str == "surfaceshader") { - return Type::SurfaceShader; - } - if (type_str == "material") { - return Type::Material; - } - BLI_assert_unreachable(); - return Type::None; -} - -std::string NodeItem::type(Type type) -{ - switch (type) { - case Type::String: - return "string"; - case Type::Filename: - return "filename"; - case Type::Integer: - return "integer"; - case Type::Float: - return "float"; - case Type::Vector2: - return "vector2"; - case Type::Vector3: - return "vector3"; - case Type::Vector4: - return "vector4"; - case Type::Color3: - return "color3"; - case Type::Color4: - return "color4"; - case Type::BSDF: - return "BSDF"; - case Type::EDF: - return "EDF"; - case Type::SurfaceShader: - return "surfaceshader"; - case Type::Material: - return "material"; - default: - BLI_assert_unreachable(); - } - return ""; -} - bool NodeItem::is_arithmetic(Type type) { return type >= Type::Float && type <= Type::Color4; diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.h b/source/blender/nodes/shader/materialx/nodes/node_item.h index b53cd940889c..0e5abb0c32e2 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.h +++ b/source/blender/nodes/shader/materialx/nodes/node_item.h @@ -45,6 +45,9 @@ class NodeItem { NodeItem(MaterialX::GraphElement *graph); ~NodeItem() = default; + static Type type(const std::string &type_str); + static std::string type(Type type); + /* Operators */ operator bool() const; NodeItem operator+(const NodeItem &other) const; @@ -106,8 +109,6 @@ class NodeItem { void add_output(const std::string &in_name, Type out_type); private: - static Type type(const std::string &type_str); - static std::string type(Type type); static bool is_arithmetic(Type type); static Type adjust_types(NodeItem &item1, NodeItem &item2); diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index d46d3f1ba9cb..26dde9dab79f 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -13,22 +13,30 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, const bNode *node, - const bNodeSocket *socket_out, - NodeItem::Type shader_type) + const bNodeSocket *socket_out) : graph_(graph), depsgraph_(depsgraph), material_(material), node_(node), - socket_out_(socket_out), - shader_type_(shader_type) + socket_out_(socket_out) { } -std::string NodeParser::node_name(const bNode *node, const bNodeSocket *socket_out) +NodeItem NodeParser::compute_full() { - return MaterialX::createValidName(node->output_sockets().size() <= 1 ? - std::string(node->name) : - std::string(node->name) + "_" + socket_out->name); + CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d]", node_->name, node_->typeinfo->type); + NodeItem res = compute(); + if (res.node) { + res.node->setName(node_name()); + } + return res; +} + +std::string NodeParser::node_name() +{ + return MaterialX::createValidName(node_->output_sockets().size() <= 1 ? + std::string(node_->name) : + std::string(node_->name) + "_" + socket_out_->name); } NodeItem NodeParser::create_node(const std::string &mx_category, const std::string &mx_type) @@ -48,14 +56,14 @@ NodeItem NodeParser::get_input_default(int index) return get_input_default(node_->input_socket(index)); } -NodeItem NodeParser::get_input_link(const std::string &name, NodeItem::Type shader_type) +NodeItem NodeParser::get_input_link(const std::string &name) { - return get_input_link(node_->input_by_identifier(name), shader_type); + return get_input_link(node_->input_by_identifier(name)); } -NodeItem NodeParser::get_input_link(int index, NodeItem::Type shader_type) +NodeItem NodeParser::get_input_link(int index) { - return get_input_link(node_->input_socket(index), shader_type); + return get_input_link(node_->input_socket(index)); } NodeItem NodeParser::get_input_value(const std::string &name) @@ -101,7 +109,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket) return res; } -NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type shader_type) +NodeItem NodeParser::get_input_link(const bNodeSocket &socket) { NodeItem res = empty(); @@ -122,7 +130,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type sh } /* Checking if node was already computed */ - res.node = graph_->getNode(node_name(from_node, link->fromsock)); + res.node = graph_->getNode(node_name()); if (res.node) { return res; } @@ -131,12 +139,12 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type sh #define CASE_NODE_TYPE(type, T) \ case type: \ res = \ - T(graph_, depsgraph_, material_, from_node, link->fromsock, shader_type).compute_full(); \ + T(graph_, depsgraph_, material_, from_node, link->fromsock).compute_full(); \ break; switch (from_node->typeinfo->type) { CASE_NODE_TYPE(SH_NODE_BRIGHTCONTRAST, BrightContrastNodeParser) - CASE_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) + //CASE_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) CASE_NODE_TYPE(SH_NODE_COMBINE_COLOR, CombineColorNodeParser) CASE_NODE_TYPE(SH_NODE_COMBXYZ, CombineXYZNodeParser) CASE_NODE_TYPE(SH_NODE_HUE_SAT, HueSatValNodeParser) @@ -164,20 +172,99 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type sh NodeItem NodeParser::get_input_value(const bNodeSocket &socket) { - NodeItem res = get_input_link(socket, NodeItem::Type::None); + NodeItem res = get_input_link(socket); if (!res) { res = get_input_default(socket); } return res; } -NodeItem NodeParser::compute_full() +ShaderNodeParser::ShaderNodeParser(MaterialX::GraphElement *graph, + const Depsgraph *depsgraph, + const Material *material, + const bNode *node, + const bNodeSocket *socket_out, + NodeItem::Type shader_type) + : NodeParser(graph, depsgraph, material, node, socket_out), shader_type_(shader_type) { - CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d]", node_->name, node_->typeinfo->type); - NodeItem res = compute(); - if (res.node) { - res.node->setName(node_name(node_, socket_out_)); +} + +NodeItem ShaderNodeParser::compute() +{ + switch (shader_type_) { + case NodeItem::Type::BSDF: + return compute_bsdf(); + case NodeItem::Type::EDF: + return compute_edf(); + case NodeItem::Type::SurfaceShader: + return compute_surface(); + default: + BLI_assert_unreachable(); } + return empty(); +} + +std::string ShaderNodeParser::node_name() +{ + std::string name = NodeParser::node_name(); + if (shader_type_ != NodeItem::Type::SurfaceShader) { + name += "_" + NodeItem::type(shader_type_); + } + return name; +} + +NodeItem ShaderNodeParser::get_input_shader(const std::string &name, NodeItem::Type shader_type) +{ + return get_input_shader(node_->input_by_identifier(name), shader_type); +} + +NodeItem ShaderNodeParser::get_input_shader(int index, NodeItem::Type shader_type) +{ + return get_input_shader(node_->input_socket(index), shader_type); +} + +NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type) +{ + NodeItem res = empty(); + + const bNodeLink *link = socket.link; + if (!(link && link->is_used())) { + return res; + } + + const bNode *from_node = link->fromnode; + + /* Passing NODE_REROUTE nodes */ + while (from_node->type == NODE_REROUTE) { + link = from_node->input_socket(0).link; + if (!(link && link->is_used())) { + return res; + } + from_node = link->fromnode; + } + + /* Checking if node was already computed */ + res.node = graph_->getNode(node_name()); + if (res.node) { + return res; + } + + /* Computing from_node with required NodeParser object */ +#define CASE_SHADER_NODE_TYPE(type, T) \ + case type: \ + res = T(graph_, depsgraph_, material_, from_node, link->fromsock, shader_type).compute_full(); \ + break; + + switch (from_node->typeinfo->type) { + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) + + default: + CLOG_WARN(LOG_MATERIALX_SHADER, + "Unsupported node: %s [%d]", + from_node->name, + from_node->typeinfo->type); + } + return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index edd9f2e849b7..9fe95e205ebe 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -19,27 +19,25 @@ class NodeParser { const Material *material_; const bNode *node_; const bNodeSocket *socket_out_; - NodeItem::Type shader_type_; public: NodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, const bNode *node, - const bNodeSocket *socket_out, - NodeItem::Type shader_type); + const bNodeSocket *socket_out); virtual ~NodeParser() = default; virtual NodeItem compute() = 0; protected: - static std::string node_name(const bNode *node, const bNodeSocket *socket_out); + NodeItem compute_full(); + virtual std::string node_name(); NodeItem create_node(const std::string &mx_category, const std::string &mx_type); NodeItem get_input_default(const std::string &name); NodeItem get_input_default(int index); - NodeItem get_input_link(const std::string &name, - NodeItem::Type shader_type = NodeItem::Type::None); - NodeItem get_input_link(int index, NodeItem::Type shader_type = NodeItem::Type::None); + NodeItem get_input_link(const std::string &name); + NodeItem get_input_link(int index); NodeItem get_input_value(const std::string &name); NodeItem get_input_value(int index); NodeItem empty() const; @@ -47,9 +45,34 @@ class NodeParser { private: NodeItem get_input_default(const bNodeSocket &socket); - NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type shader_type); + NodeItem get_input_link(const bNodeSocket &socket); NodeItem get_input_value(const bNodeSocket &socket); - NodeItem compute_full(); +}; + +class ShaderNodeParser : public NodeParser { + protected: + NodeItem::Type shader_type_; + + public: + ShaderNodeParser(MaterialX::GraphElement *graph, + const Depsgraph *depsgraph, + const Material *material, + const bNode *node, + const bNodeSocket *socket_out, + NodeItem::Type shader_type); + + NodeItem compute() override; + virtual NodeItem compute_bsdf() = 0; + virtual NodeItem compute_edf() = 0; + virtual NodeItem compute_surface() = 0; + + protected: + std::string node_name() override; + NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type); + NodeItem get_input_shader(int index, NodeItem::Type shader_type); + + private: + NodeItem get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type); }; template NodeItem NodeParser::value(const T &data) const @@ -57,28 +80,38 @@ template NodeItem NodeParser::value(const T &data) const return empty().val(data); } -#define DECLARE_PARSER(T) \ +#define DECLARE_NODE_PARSER(T) \ class T : public NodeParser { \ public: \ using NodeParser::NodeParser; \ NodeItem compute() override; \ }; -DECLARE_PARSER(BrightContrastNodeParser) -DECLARE_PARSER(BSDFPrincipledNodeParser) -DECLARE_PARSER(CombineColorNodeParser) -DECLARE_PARSER(CombineXYZNodeParser) -DECLARE_PARSER(HueSatValNodeParser) -DECLARE_PARSER(InvertNodeParser) -DECLARE_PARSER(MathNodeParser) -DECLARE_PARSER(MixRGBNodeParser) -DECLARE_PARSER(NormalMapNodeParser) -DECLARE_PARSER(SeparateColorNodeParser) -DECLARE_PARSER(SeparateXYZNodeParser) -DECLARE_PARSER(TexCheckerNodeParser) -DECLARE_PARSER(TexEnvironmentNodeParser) -DECLARE_PARSER(TexImageNodeParser) -DECLARE_PARSER(TexNoiseNodeParser) -DECLARE_PARSER(VectorMathNodeParser) +#define DECLARE_SHADER_NODE_PARSER(T) \ + class T : public ShaderNodeParser { \ + public: \ + using ShaderNodeParser::ShaderNodeParser; \ + NodeItem compute_bsdf() override; \ + NodeItem compute_edf() override; \ + NodeItem compute_surface() override; \ + }; + +DECLARE_NODE_PARSER(BrightContrastNodeParser) +DECLARE_NODE_PARSER(CombineColorNodeParser) +DECLARE_NODE_PARSER(CombineXYZNodeParser) +DECLARE_NODE_PARSER(HueSatValNodeParser) +DECLARE_NODE_PARSER(InvertNodeParser) +DECLARE_NODE_PARSER(MathNodeParser) +DECLARE_NODE_PARSER(MixRGBNodeParser) +DECLARE_NODE_PARSER(NormalMapNodeParser) +DECLARE_NODE_PARSER(SeparateColorNodeParser) +DECLARE_NODE_PARSER(SeparateXYZNodeParser) +DECLARE_NODE_PARSER(TexCheckerNodeParser) +DECLARE_NODE_PARSER(TexEnvironmentNodeParser) +DECLARE_NODE_PARSER(TexImageNodeParser) +DECLARE_NODE_PARSER(TexNoiseNodeParser) +DECLARE_NODE_PARSER(VectorMathNodeParser) + +DECLARE_SHADER_NODE_PARSER(BSDFPrincipledNodeParser) } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.cc b/source/blender/nodes/shader/materialx/nodes/output_material.cc index 5703be395dc7..bda6573ef88b 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.cc +++ b/source/blender/nodes/shader/materialx/nodes/output_material.cc @@ -8,9 +8,8 @@ namespace blender::nodes::materialx { OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, - const bNode *node, - NodeItem::Type shader_type) - : NodeParser(graph, depsgraph, material, node, nullptr, shader_type) + const bNode *node) + : ShaderNodeParser(graph, depsgraph, material, node, nullptr, NodeItem::Type::Material) { } @@ -18,18 +17,45 @@ NodeItem OutputMaterialNodeParser::compute() { NodeItem surface = empty(); if (node_) { - surface = get_input_link("Surface", NodeItem::Type::SurfaceShader); + NodeItem bsdf = compute_bsdf(); + NodeItem edf = compute_edf(); + if (bsdf || edf) { + surface = create_node("surface", "surfaceshader"); + if (bsdf) { + surface.set_input("bsdf", bsdf); + } + if (edf) { + surface.set_input("edf", edf); + } + } + else { + surface = compute_surface(); + } } else { surface = create_node("standard_surface", "surfaceshader"); surface.set_input("base_color", value(MaterialX::Color3(1.0f, 0.0f, 1.0f))); } NodeItem res = create_node("surfacematerial", "material"); - res.node->setName(node_name(node_, nullptr)); res.set_input("surfaceshader", surface); return res; } +NodeItem OutputMaterialNodeParser::compute_bsdf() +{ + return get_input_shader("Surface", NodeItem::Type::BSDF); +} + +NodeItem OutputMaterialNodeParser::compute_edf() +{ + return get_input_shader("Surface", NodeItem::Type::EDF); +} + +NodeItem OutputMaterialNodeParser::compute_surface() +{ + return get_input_shader("Surface", NodeItem::Type::SurfaceShader); +} + NodeItem OutputMaterialNodeParser::compute_default() { NodeItem surface = create_node("standard_surface", "surfaceshader"); @@ -51,4 +77,9 @@ NodeItem OutputMaterialNodeParser::compute_default() return res; } +std::string OutputMaterialNodeParser::node_name() +{ + return node_->name; +} + } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.h b/source/blender/nodes/shader/materialx/nodes/output_material.h index cc078c6a1c04..edcf67131d86 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.h +++ b/source/blender/nodes/shader/materialx/nodes/output_material.h @@ -8,15 +8,22 @@ namespace blender::nodes::materialx { -class OutputMaterialNodeParser : public NodeParser { +class OutputMaterialNodeParser : public ShaderNodeParser { public: OutputMaterialNodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, - const bNode *node, - NodeItem::Type shader_type); + const bNode *node); NodeItem compute() override; + NodeItem compute_bsdf() override; + NodeItem compute_edf() override; + NodeItem compute_surface() override; + + using ShaderNodeParser::compute_full; NodeItem compute_default(); + + protected: + std::string node_name() override; }; } // namespace blender::nodes::materialx -- 2.30.2 From de75c7bf6e33c70a679bb7567b990a25ba70637a Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 18:37:55 +0300 Subject: [PATCH 05/11] Fixes after merge. Fixed checking already computed node. --- .../nodes/shader/materialx/nodes/node_item.cc | 12 +++--- .../nodes/shader/materialx/nodes/node_item.h | 4 +- .../shader/materialx/nodes/node_parser.cc | 40 +++++++++++-------- .../shader/materialx/nodes/output_material.cc | 2 +- .../shader/materialx/nodes/tex_checker.cc | 4 +- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index 5dbd09eef3c1..8768e6e342bc 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -54,7 +54,7 @@ NodeItem::Type NodeItem::type(const std::string &type_str) return Type::Material; } BLI_assert_unreachable(); - return Type::None; + return Type::Empty; } std::string NodeItem::type(Type type) @@ -151,7 +151,7 @@ bool NodeItem::operator==(const NodeItem &other) const NodeItem item1 = *this; NodeItem item2 = other; Type to_type = adjust_types(item1, item2); - if (to_type == Type::None) { + if (to_type == Type::Empty) { return false; } return item1.value->getValueString() == item2.value->getValueString(); @@ -529,7 +529,7 @@ NodeItem NodeItem::if_else(CompareOp op, auto item1 = if_val; auto item2 = else_val; Type to_type = adjust_types(item1, item2); - if (to_type == Type::None) { + if (to_type == Type::Empty) { return res; } @@ -579,7 +579,7 @@ NodeItem::Type NodeItem::type() const if (node) { return type(node->getType()); } - return Type::None; + return Type::Empty; } void NodeItem::set_input(const std::string &name, @@ -647,7 +647,7 @@ NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) return t1; } if (!is_arithmetic(t1) || !is_arithmetic(t2)) { - return Type::None; + return Type::Empty; } if (t1 < t2) { item1 = item1.convert(t2); @@ -728,7 +728,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other, NodeItem item1 = *this; NodeItem item2 = other; Type to_type = adjust_types(item1, item2); - if (to_type == Type::None) { + if (to_type == Type::Empty) { return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.h b/source/blender/nodes/shader/materialx/nodes/node_item.h index 28c5fd257cf3..ded16bcd139c 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.h +++ b/source/blender/nodes/shader/materialx/nodes/node_item.h @@ -11,7 +11,8 @@ namespace blender::nodes::materialx { class NodeItem { public: enum class Type { - None = 0, + Empty = 0, + Any, /* Value types */ String, @@ -30,7 +31,6 @@ class NodeItem { EDF, SurfaceShader, Material, - }; enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq }; diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index ea166b196eeb..4c2ed3189c6f 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -129,22 +129,17 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket) from_node = link->fromnode; } - /* Checking if node was already computed */ - res.node = graph_->getNode(node_name()); - if (res.node) { - return res; - } + /* Creating required NodeParser object */ + std::unique_ptr parser; - /* Computing from_node with required NodeParser object */ #define CASE_NODE_TYPE(type, T) \ case type: \ - res = \ - T(graph_, depsgraph_, material_, from_node, link->fromsock).compute_full(); \ + parser = \ + std::make_unique(graph_, depsgraph_, material_, from_node, link->fromsock); \ break; switch (from_node->typeinfo->type) { CASE_NODE_TYPE(SH_NODE_BRIGHTCONTRAST, BrightContrastNodeParser) - //CASE_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) CASE_NODE_TYPE(SH_NODE_COMBINE_COLOR, CombineColorNodeParser) CASE_NODE_TYPE(SH_NODE_COMBXYZ, CombineXYZNodeParser) CASE_NODE_TYPE(SH_NODE_HUE_SAT, HueSatValNodeParser) @@ -167,6 +162,14 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket) from_node->typeinfo->type); } + /* Checking if node was already computed */ + res.node = graph_->getNode(parser->node_name()); + if (res.node) { + return res; + } + + /* Computing */ + res = parser->compute_full(); return res; } @@ -243,16 +246,13 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: from_node = link->fromnode; } - /* Checking if node was already computed */ - res.node = graph_->getNode(node_name()); - if (res.node) { - return res; - } + /* Creating required ShaderNodeParser object */ + std::unique_ptr parser; - /* Computing from_node with required NodeParser object */ #define CASE_SHADER_NODE_TYPE(type, T) \ case type: \ - res = T(graph_, depsgraph_, material_, from_node, link->fromsock, shader_type).compute_full(); \ + parser = std::make_unique( \ + graph_, depsgraph_, material_, from_node, link->fromsock, shader_type); \ break; switch (from_node->typeinfo->type) { @@ -265,6 +265,14 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: from_node->typeinfo->type); } + /* Checking if node was already computed */ + res.node = graph_->getNode(parser->node_name()); + if (res.node) { + return res; + } + + /* Computing */ + res = parser->compute_full(); return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.cc b/source/blender/nodes/shader/materialx/nodes/output_material.cc index bda6573ef88b..9e07a87290b1 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.cc +++ b/source/blender/nodes/shader/materialx/nodes/output_material.cc @@ -79,7 +79,7 @@ NodeItem OutputMaterialNodeParser::compute_default() std::string OutputMaterialNodeParser::node_name() { - return node_->name; + return NodeParser::node_name(); } } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc index 26d87e0b0d1c..6c0f176f2669 100644 --- a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc +++ b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc @@ -20,8 +20,8 @@ NodeItem TexCheckerNodeParser::compute() NodeItem mix = (vector.extract(0).floor() + vector.extract(1).floor()) .if_else(NodeItem::CompareOp::Eq, value(1.0f), value(1.0f), value(0.0f)); NodeItem res = create_node("mix", "color3"); - res.set_input("fg", color1, NodeItem::Type::Color3); - res.set_input("bg", color2, NodeItem::Type::Color3); + res.set_input("fg", color1); + res.set_input("bg", color2); res.set_input("mix", mix); return res; } -- 2.30.2 From a4086cd79a604c6b6023073e906d0f60c2cbaa15 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 19:07:42 +0300 Subject: [PATCH 06/11] Added NodeItem::Type to get_input_default and get_input_link --- .../shader/materialx/nodes/bsdf_principled.cc | 6 +- .../nodes/shader/materialx/nodes/math.cc | 6 +- .../nodes/shader/materialx/nodes/node_item.cc | 2 +- .../shader/materialx/nodes/node_parser.cc | 55 ++++++++++++------- .../shader/materialx/nodes/node_parser.h | 23 ++++---- .../shader/materialx/nodes/tex_checker.cc | 2 +- .../shader/materialx/nodes/vector_math.cc | 6 +- 7 files changed, 57 insertions(+), 43 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc index da2101e6df92..617f6f8df873 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc @@ -50,9 +50,9 @@ NodeItem BSDFPrincipledNodeParser::compute_surface() NodeItem alpha = get_input_value("Alpha", NodeItem::Type::Float); // transparency = 1.0 - alpha - NodeItem normal = get_input_link("Normal"); - NodeItem clearcoat_normal = get_input_link("Clearcoat Normal"); - NodeItem tangent = get_input_link("Tangent"); + NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); + NodeItem clearcoat_normal = get_input_link("Clearcoat Normal", NodeItem::Type::Vector3); + NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); /* Creating standard_surface */ NodeItem res = create_node("standard_surface", "surfaceshader"); diff --git a/source/blender/nodes/shader/materialx/nodes/math.cc b/source/blender/nodes/shader/materialx/nodes/math.cc index c974f9d46a30..b038733c6ab2 100644 --- a/source/blender/nodes/shader/materialx/nodes/math.cc +++ b/source/blender/nodes/shader/materialx/nodes/math.cc @@ -14,7 +14,7 @@ NodeItem MathNodeParser::compute() NodeItem res = empty(); /* Single operand operations */ - NodeItem x = get_input_value(0, NodeItem::Type::Empty); + NodeItem x = get_input_value(0, NodeItem::Type::Any); switch (op) { case NODE_MATH_SINE: res = x.sin(); @@ -82,7 +82,7 @@ NodeItem MathNodeParser::compute() default: { /* 2-operand operations */ - NodeItem y = get_input_value(1, NodeItem::Type::Empty); + NodeItem y = get_input_value(1, NodeItem::Type::Any); switch (op) { case NODE_MATH_ADD: res = x + y; @@ -132,7 +132,7 @@ NodeItem MathNodeParser::compute() default: { /* 3-operand operations */ - NodeItem z = get_input_value(2, NodeItem::Type::Empty); + NodeItem z = get_input_value(2, NodeItem::Type::Any); switch (op) { case NODE_MATH_WRAP: CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op); diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index 8768e6e342bc..ebed94013165 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -328,7 +328,7 @@ NodeItem NodeItem::extract(const int index) const NodeItem NodeItem::convert(Type to_type) const { Type from_type = type(); - if (from_type == to_type) { + if (from_type == Type::Empty || from_type == to_type || to_type == Type::Any) { return *this; } if (!is_arithmetic(from_type) || !is_arithmetic(to_type)) { diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index 4c2ed3189c6f..a08f4ea1eb34 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -46,34 +46,34 @@ NodeItem NodeParser::create_node(const std::string &mx_category, const std::stri return res; } -NodeItem NodeParser::get_input_default(const std::string &name) +NodeItem NodeParser::get_input_default(const std::string &name, NodeItem::Type to_type) { - return get_input_default(node_->input_by_identifier(name)); + return get_input_default(node_->input_by_identifier(name), to_type); } -NodeItem NodeParser::get_input_default(int index) +NodeItem NodeParser::get_input_default(int index, NodeItem::Type to_type) { - return get_input_default(node_->input_socket(index)); + return get_input_default(node_->input_socket(index), to_type); } -NodeItem NodeParser::get_input_link(const std::string &name) +NodeItem NodeParser::get_input_link(const std::string &name, NodeItem::Type to_type) { - return get_input_link(node_->input_by_identifier(name)); + return get_input_link(node_->input_by_identifier(name), to_type); } -NodeItem NodeParser::get_input_link(int index) +NodeItem NodeParser::get_input_link(int index, NodeItem::Type to_type) { - return get_input_link(node_->input_socket(index)); + return get_input_link(node_->input_socket(index), to_type); } -NodeItem NodeParser::get_input_value(const std::string &name, const NodeItem::Type type) +NodeItem NodeParser::get_input_value(const std::string &name, NodeItem::Type to_type) { - return get_input_value(node_->input_by_identifier(name), type); + return get_input_value(node_->input_by_identifier(name), to_type); } -NodeItem NodeParser::get_input_value(int index, const NodeItem::Type type) +NodeItem NodeParser::get_input_value(int index, NodeItem::Type to_type) { - return get_input_value(node_->input_socket(index), type); + return get_input_value(node_->input_socket(index), to_type); } NodeItem NodeParser::empty() const @@ -81,7 +81,7 @@ NodeItem NodeParser::empty() const return NodeItem(graph_); } -NodeItem NodeParser::get_input_default(const bNodeSocket &socket) +NodeItem NodeParser::get_input_default(const bNodeSocket &socket, NodeItem::Type to_type) { NodeItem res = empty(); switch (socket.type) { @@ -106,10 +106,10 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket) CLOG_WARN(LOG_MATERIALX_SHADER, "Unsupported socket type: %d", socket.type); } } - return res; + return res.convert(to_type); } -NodeItem NodeParser::get_input_link(const bNodeSocket &socket) +NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to_type) { NodeItem res = empty(); @@ -170,16 +170,16 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket) /* Computing */ res = parser->compute_full(); - return res; + return res.convert(to_type); } -NodeItem NodeParser::get_input_value(const bNodeSocket &socket, const NodeItem::Type type) +NodeItem NodeParser::get_input_value(const bNodeSocket &socket, NodeItem::Type to_type) { - NodeItem res = get_input_link(socket); + NodeItem res = get_input_link(socket, to_type); if (!res) { - res = get_input_default(socket); + res = get_input_default(socket, to_type); } - return type == NodeItem::Type::Empty ? res : res.convert(type); + return res; } ShaderNodeParser::ShaderNodeParser(MaterialX::GraphElement *graph, @@ -207,6 +207,21 @@ NodeItem ShaderNodeParser::compute() return empty(); } +NodeItem ShaderNodeParser::compute_full() +{ + CLOG_INFO(LOG_MATERIALX_SHADER, + 1, + "%s [%d] - %s", + node_->name, + node_->typeinfo->type, + NodeItem::type(shader_type_).c_str()); + NodeItem res = compute(); + if (res.node) { + res.node->setName(node_name()); + } + return res; +} + std::string ShaderNodeParser::node_name() { std::string name = NodeParser::node_name(); diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index cdfa24d11b6e..7a7f2cf25160 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -31,24 +31,22 @@ class NodeParser { virtual NodeItem compute() = 0; protected: - NodeItem compute_full(); + virtual NodeItem compute_full(); virtual std::string node_name(); NodeItem create_node(const std::string &mx_category, const std::string &mx_type); - NodeItem get_input_default(const std::string &name); - NodeItem get_input_default(int index); - NodeItem get_input_link(const std::string &name); - NodeItem get_input_link(int index); - NodeItem get_input_value(const std::string &name, - const NodeItem::Type type); - NodeItem get_input_value(int index, const NodeItem::Type type); + NodeItem get_input_default(const std::string &name, NodeItem::Type to_type); + NodeItem get_input_default(int index, NodeItem::Type to_type); + NodeItem get_input_link(const std::string &name, NodeItem::Type to_type); + NodeItem get_input_link(int index, NodeItem::Type to_type); + NodeItem get_input_value(const std::string &name, NodeItem::Type to_type); + NodeItem get_input_value(int index, NodeItem::Type to_type); NodeItem empty() const; template NodeItem value(const T &data) const; private: - NodeItem get_input_default(const bNodeSocket &socket); - NodeItem get_input_link(const bNodeSocket &socket); - NodeItem get_input_value(const bNodeSocket &socket, - const NodeItem::Type type); + NodeItem get_input_default(const bNodeSocket &socket, NodeItem::Type to_type); + NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type to_type); + NodeItem get_input_value(const bNodeSocket &socket, NodeItem::Type to_type); }; class ShaderNodeParser : public NodeParser { @@ -69,6 +67,7 @@ class ShaderNodeParser : public NodeParser { virtual NodeItem compute_surface() = 0; protected: + NodeItem compute_full() override; std::string node_name() override; NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type); NodeItem get_input_shader(int index, NodeItem::Type shader_type); diff --git a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc index 6c0f176f2669..f039a6336e43 100644 --- a/source/blender/nodes/shader/materialx/nodes/tex_checker.cc +++ b/source/blender/nodes/shader/materialx/nodes/tex_checker.cc @@ -8,7 +8,7 @@ namespace blender::nodes::materialx { NodeItem TexCheckerNodeParser::compute() { - NodeItem vector = get_input_link("Vector"); + NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2); NodeItem color1 = get_input_value("Color1", NodeItem::Type::Color3); NodeItem color2 = get_input_value("Color2", NodeItem::Type::Color3); NodeItem scale = get_input_value("Scale", NodeItem::Type::Float); diff --git a/source/blender/nodes/shader/materialx/nodes/vector_math.cc b/source/blender/nodes/shader/materialx/nodes/vector_math.cc index df4764609699..a3267565772a 100644 --- a/source/blender/nodes/shader/materialx/nodes/vector_math.cc +++ b/source/blender/nodes/shader/materialx/nodes/vector_math.cc @@ -14,7 +14,7 @@ NodeItem VectorMathNodeParser::compute() NodeItem res = empty(); /* Single operand operations */ - NodeItem x = get_input_value(0, NodeItem::Type::Empty); + NodeItem x = get_input_value(0, NodeItem::Type::Any); switch (op) { case NODE_VECTOR_MATH_SINE: res = x.sin(); @@ -46,7 +46,7 @@ NodeItem VectorMathNodeParser::compute() default: { /* 2-operand operations */ - NodeItem y = get_input_value(1, NodeItem::Type::Empty); + NodeItem y = get_input_value(1, NodeItem::Type::Any); switch (op) { case NODE_VECTOR_MATH_ADD: res = x + y; @@ -93,7 +93,7 @@ NodeItem VectorMathNodeParser::compute() default: { /* 3-operand operations */ - NodeItem z = get_input_value(2, NodeItem::Type::Empty); + NodeItem z = get_input_value(2, NodeItem::Type::Any); switch (op) { case NODE_VECTOR_MATH_MULTIPLY_ADD: res = x * y + z; -- 2.30.2 From fc91be3e313c95b879f231a0f0c32394b371c9aa Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 19:46:34 +0300 Subject: [PATCH 07/11] Added templates for new shaders --- source/blender/nodes/shader/CMakeLists.txt | 12 +++++++++ .../shader/materialx/nodes/add_shader.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/bsdf_diffuse.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/bsdf_glass.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/bsdf_glossy.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/bsdf_refraction.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/bsdf_sheen.cc | 27 +++++++++++++++++++ .../nodes/shader/materialx/nodes/bsdf_toon.cc | 27 +++++++++++++++++++ .../materialx/nodes/bsdf_translucent.cc | 27 +++++++++++++++++++ .../materialx/nodes/bsdf_transparent.cc | 27 +++++++++++++++++++ .../nodes/shader/materialx/nodes/emission.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/mix_shader.cc | 27 +++++++++++++++++++ .../shader/materialx/nodes/node_parser.cc | 12 +++++++++ .../shader/materialx/nodes/node_parser.h | 13 +++++++++ .../materialx/nodes/subsurface_scattering.cc | 27 +++++++++++++++++++ 15 files changed, 361 insertions(+) create mode 100644 source/blender/nodes/shader/materialx/nodes/add_shader.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/emission.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/mix_shader.cc create mode 100644 source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc diff --git a/source/blender/nodes/shader/CMakeLists.txt b/source/blender/nodes/shader/CMakeLists.txt index 9a3b77502fbf..1d75ad77a3df 100644 --- a/source/blender/nodes/shader/CMakeLists.txt +++ b/source/blender/nodes/shader/CMakeLists.txt @@ -147,18 +147,30 @@ set(LIB if(WITH_MATERIALX) list(APPEND SRC materialx/material.cc + materialx/nodes/add_shader.cc materialx/nodes/brightness.cc + materialx/nodes/bsdf_diffuse.cc + materialx/nodes/bsdf_glass.cc + materialx/nodes/bsdf_glossy.cc materialx/nodes/bsdf_principled.cc + materialx/nodes/bsdf_refraction.cc + materialx/nodes/bsdf_sheen.cc + materialx/nodes/bsdf_toon.cc + materialx/nodes/bsdf_translucent.cc + materialx/nodes/bsdf_transparent.cc + materialx/nodes/emission.cc materialx/nodes/huesatval.cc materialx/nodes/invert.cc materialx/nodes/math.cc materialx/nodes/mix_rgb.cc + materialx/nodes/mix_shader.cc materialx/nodes/node_item.cc materialx/nodes/node_parser.cc materialx/nodes/normal_map.cc materialx/nodes/output_material.cc materialx/nodes/sepcomb_color.cc materialx/nodes/sepcomb_xyz.cc + materialx/nodes/subsurface_scattering.cc materialx/nodes/tex_checker.cc materialx/nodes/tex_environment.cc materialx/nodes/tex_image.cc diff --git a/source/blender/nodes/shader/materialx/nodes/add_shader.cc b/source/blender/nodes/shader/materialx/nodes/add_shader.cc new file mode 100644 index 000000000000..1a78ed4485f0 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/add_shader.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem AddShaderNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem AddShaderNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem AddShaderNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc new file mode 100644 index 000000000000..a742f8232b1b --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFDiffuseNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFDiffuseNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFDiffuseNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc new file mode 100644 index 000000000000..f50d5cb1db22 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFGlassNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFGlassNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFGlassNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc new file mode 100644 index 000000000000..5e621595647a --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFGlossyNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFGlossyNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFGlossyNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc new file mode 100644 index 000000000000..53aac90fa335 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFRefractionNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFRefractionNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFRefractionNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc new file mode 100644 index 000000000000..a22b9d53ec62 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFSheenNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFSheenNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFSheenNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc new file mode 100644 index 000000000000..1c37fc914b2c --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFToonNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFToonNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFToonNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc new file mode 100644 index 000000000000..1c7ff789abc8 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFTranslucentNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFTranslucentNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFTranslucentNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc new file mode 100644 index 000000000000..6354ffecdd4d --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem BSDFTransparentNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFTransparentNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem BSDFTransparentNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/emission.cc b/source/blender/nodes/shader/materialx/nodes/emission.cc new file mode 100644 index 000000000000..24afaf339845 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/emission.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem EmissionNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem EmissionNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem EmissionNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc new file mode 100644 index 000000000000..61032252e130 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem MixShaderNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem MixShaderNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem MixShaderNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index a08f4ea1eb34..d27513a025e8 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -271,7 +271,19 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: break; switch (from_node->typeinfo->type) { + CASE_SHADER_NODE_TYPE(SH_NODE_ADD_SHADER, AddShaderNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_DIFFUSE, BSDFDiffuseNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLASS, BSDFGlassNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLOSSY, BSDFGlossyNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_REFRACTION, BSDFRefractionNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_SHEEN, BSDFSheenNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TOON, BSDFToonNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSLUCENT, BSDFTranslucentNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSPARENT, BSDFTransparentNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_EMISSION, EmissionNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_MIX_SHADER, MixShaderNodeParser) + CASE_SHADER_NODE_TYPE(SH_NODE_SUBSURFACE_SCATTERING, SubsurfaceScatteringNodeParser) default: CLOG_WARN(LOG_MATERIALX_SHADER, diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index 7a7f2cf25160..b2c87512ae07 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -113,6 +113,19 @@ DECLARE_NODE_PARSER(TexImageNodeParser) DECLARE_NODE_PARSER(TexNoiseNodeParser) DECLARE_NODE_PARSER(VectorMathNodeParser) +DECLARE_SHADER_NODE_PARSER(AddShaderNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFDiffuseNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFGlassNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFGlossyNodeParser) DECLARE_SHADER_NODE_PARSER(BSDFPrincipledNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFRefractionNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFSheenNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFToonNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFTranslucentNodeParser) +DECLARE_SHADER_NODE_PARSER(BSDFTransparentNodeParser) +DECLARE_SHADER_NODE_PARSER(EmissionNodeParser) +DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser) +DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser) + } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc b/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc new file mode 100644 index 000000000000..4c3425610a81 --- /dev/null +++ b/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc @@ -0,0 +1,27 @@ +/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "node_parser.h" + +namespace blender::nodes::materialx { + +NodeItem SubsurfaceScatteringNodeParser::compute_bsdf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem SubsurfaceScatteringNodeParser::compute_edf() +{ + /* TODO: implement */ + return empty(); +} + +NodeItem SubsurfaceScatteringNodeParser::compute_surface() +{ + /* TODO: implement */ + return empty(); +} + +} // namespace blender::nodes::materialx -- 2.30.2 From c093aa52037736192a99b09defccdcf9e40bce70 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 20:28:44 +0300 Subject: [PATCH 08/11] Implemented add_shader, bsdf_diffuse, emission, mix_shader. make format --- .../shader/materialx/nodes/add_shader.cc | 43 ++++++++++++++--- .../shader/materialx/nodes/bsdf_diffuse.cc | 15 ++++-- .../shader/materialx/nodes/bsdf_principled.cc | 2 + .../nodes/shader/materialx/nodes/emission.cc | 10 ++-- .../shader/materialx/nodes/mix_shader.cc | 47 ++++++++++++++++--- .../shader/materialx/nodes/node_parser.cc | 5 +- .../shader/materialx/nodes/node_parser.h | 1 - 7 files changed, 99 insertions(+), 24 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/add_shader.cc b/source/blender/nodes/shader/materialx/nodes/add_shader.cc index 1a78ed4485f0..c2de40a5bf09 100644 --- a/source/blender/nodes/shader/materialx/nodes/add_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/add_shader.cc @@ -8,20 +8,51 @@ namespace blender::nodes::materialx { NodeItem AddShaderNodeParser::compute_bsdf() { - /* TODO: implement */ - return empty(); + NodeItem shader1 = get_input_shader(0, NodeItem::Type::BSDF); + NodeItem shader2 = get_input_shader(1, NodeItem::Type::BSDF); + + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1; + } + else if (!shader1 && shader2) { + res = shader2; + } + else if (shader1 && shader2) { + res = create_node("add", "BSDF"); + res.set_input("in1", shader1); + res.set_input("in2", shader2); + } + return res; } NodeItem AddShaderNodeParser::compute_edf() { - /* TODO: implement */ - return empty(); + NodeItem shader1 = get_input_shader(0, NodeItem::Type::EDF); + NodeItem shader2 = get_input_shader(1, NodeItem::Type::EDF); + + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1; + } + else if (!shader1 && shader2) { + res = shader2; + } + else if (shader1 && shader2) { + res = create_node("add", "EDF"); + res.set_input("in1", shader1); + res.set_input("in2", shader2); + } + return res; } NodeItem AddShaderNodeParser::compute_surface() { - /* TODO: implement */ - return empty(); + NodeItem res = get_input_shader(0, NodeItem::Type::SurfaceShader); + if (!res) { + res = get_input_shader(1, NodeItem::Type::SurfaceShader); + } + return res; } } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc index a742f8232b1b..c12ea98bee01 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc @@ -8,19 +8,26 @@ namespace blender::nodes::materialx { NodeItem BSDFDiffuseNodeParser::compute_bsdf() { - /* TODO: implement */ - return empty(); + NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float); + NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); + + NodeItem res = create_node("oren_nayar_diffuse_bsdf", "BSDF"); + res.set_input("color", color); + res.set_input("roughness", roughness); + if (normal) { + res.set_input("normal", normal); + } + return res; } NodeItem BSDFDiffuseNodeParser::compute_edf() { - /* TODO: implement */ return empty(); } NodeItem BSDFDiffuseNodeParser::compute_surface() { - /* TODO: implement */ return empty(); } diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc index 617f6f8df873..8bd1ba9b4d70 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc @@ -8,11 +8,13 @@ namespace blender::nodes::materialx { NodeItem BSDFPrincipledNodeParser::compute_bsdf() { + /* TODO: implement */ return empty(); } NodeItem BSDFPrincipledNodeParser::compute_edf() { + /* TODO: implement */ return empty(); } diff --git a/source/blender/nodes/shader/materialx/nodes/emission.cc b/source/blender/nodes/shader/materialx/nodes/emission.cc index 24afaf339845..cef1ebf06d68 100644 --- a/source/blender/nodes/shader/materialx/nodes/emission.cc +++ b/source/blender/nodes/shader/materialx/nodes/emission.cc @@ -8,19 +8,21 @@ namespace blender::nodes::materialx { NodeItem EmissionNodeParser::compute_bsdf() { - /* TODO: implement */ return empty(); } NodeItem EmissionNodeParser::compute_edf() { - /* TODO: implement */ - return empty(); + NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + NodeItem strength = get_input_value("Strength", NodeItem::Type::Float); + + NodeItem res = create_node("uniform_edf", "EDF"); + res.set_input("color", color * strength); + return res; } NodeItem EmissionNodeParser::compute_surface() { - /* TODO: implement */ return empty(); } diff --git a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc index 61032252e130..8e1d7f488ab9 100644 --- a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc @@ -8,20 +8,55 @@ namespace blender::nodes::materialx { NodeItem MixShaderNodeParser::compute_bsdf() { - /* TODO: implement */ - return empty(); + NodeItem fac = get_input_value(0, NodeItem::Type::Float); + NodeItem shader1 = get_input_shader(1, NodeItem::Type::BSDF); + NodeItem shader2 = get_input_shader(2, NodeItem::Type::BSDF); + + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1; + } + else if (!shader1 && shader2) { + res = shader2; + } + else if (shader1 && shader2) { + res = create_node("mix", "BSDF"); + res.set_input("fg", shader1); + res.set_input("bg", shader2); + res.set_input("mix", fac); + } + return res; } NodeItem MixShaderNodeParser::compute_edf() { - /* TODO: implement */ - return empty(); + NodeItem fac = get_input_value(0, NodeItem::Type::Float); + NodeItem shader1 = get_input_shader(1, NodeItem::Type::EDF); + NodeItem shader2 = get_input_shader(2, NodeItem::Type::EDF); + + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1; + } + else if (!shader1 && shader2) { + res = shader2; + } + else if (shader1 && shader2) { + res = create_node("mix", "EDF"); + res.set_input("fg", shader1); + res.set_input("bg", shader2); + res.set_input("mix", fac); + } + return res; } NodeItem MixShaderNodeParser::compute_surface() { - /* TODO: implement */ - return empty(); + NodeItem res = get_input_shader(0, NodeItem::Type::SurfaceShader); + if (!res) { + res = get_input_shader(1, NodeItem::Type::SurfaceShader); + } + return res; } } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index d27513a025e8..91177497d7f3 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -134,8 +134,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to #define CASE_NODE_TYPE(type, T) \ case type: \ - parser = \ - std::make_unique(graph_, depsgraph_, material_, from_node, link->fromsock); \ + parser = std::make_unique(graph_, depsgraph_, material_, from_node, link->fromsock); \ break; switch (from_node->typeinfo->type) { @@ -267,7 +266,7 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: #define CASE_SHADER_NODE_TYPE(type, T) \ case type: \ parser = std::make_unique( \ - graph_, depsgraph_, material_, from_node, link->fromsock, shader_type); \ + graph_, depsgraph_, material_, from_node, link->fromsock, shader_type); \ break; switch (from_node->typeinfo->type) { diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index b2c87512ae07..42fee898b451 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -127,5 +127,4 @@ DECLARE_SHADER_NODE_PARSER(EmissionNodeParser) DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser) DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser) - } // namespace blender::nodes::materialx -- 2.30.2 From e42c1f7607da49aed675f9b8c4a38fc8c082ecda Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 23:14:08 +0300 Subject: [PATCH 09/11] Simplified ShaderNodeParser, adjusted shaders compute() method. Implemented add, multiply shaders to NodeItem. --- .../shader/materialx/nodes/add_shader.cc | 69 +++++++---------- .../shader/materialx/nodes/bsdf_diffuse.cc | 16 ++-- .../shader/materialx/nodes/bsdf_glass.cc | 14 +--- .../shader/materialx/nodes/bsdf_glossy.cc | 14 +--- .../shader/materialx/nodes/bsdf_principled.cc | 17 ++-- .../shader/materialx/nodes/bsdf_refraction.cc | 14 +--- .../shader/materialx/nodes/bsdf_sheen.cc | 14 +--- .../nodes/shader/materialx/nodes/bsdf_toon.cc | 14 +--- .../materialx/nodes/bsdf_translucent.cc | 14 +--- .../materialx/nodes/bsdf_transparent.cc | 14 +--- .../nodes/shader/materialx/nodes/emission.cc | 14 +--- .../shader/materialx/nodes/mix_shader.cc | 77 ++++++++----------- .../nodes/shader/materialx/nodes/node_item.cc | 34 ++++++++ .../shader/materialx/nodes/node_parser.cc | 15 ---- .../shader/materialx/nodes/node_parser.h | 9 +-- .../shader/materialx/nodes/output_material.cc | 21 +---- .../shader/materialx/nodes/output_material.h | 3 - .../materialx/nodes/subsurface_scattering.cc | 14 +--- 18 files changed, 118 insertions(+), 269 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/add_shader.cc b/source/blender/nodes/shader/materialx/nodes/add_shader.cc index c2de40a5bf09..28e095a9b2a9 100644 --- a/source/blender/nodes/shader/materialx/nodes/add_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/add_shader.cc @@ -6,51 +6,36 @@ namespace blender::nodes::materialx { -NodeItem AddShaderNodeParser::compute_bsdf() +NodeItem AddShaderNodeParser::compute() { - NodeItem shader1 = get_input_shader(0, NodeItem::Type::BSDF); - NodeItem shader2 = get_input_shader(1, NodeItem::Type::BSDF); - NodeItem res = empty(); - if (shader1 && !shader2) { - res = shader1; - } - else if (!shader1 && shader2) { - res = shader2; - } - else if (shader1 && shader2) { - res = create_node("add", "BSDF"); - res.set_input("in1", shader1); - res.set_input("in2", shader2); - } - return res; -} + switch (shader_type_) { + case NodeItem::Type::BSDF: + case NodeItem::Type::EDF: { + NodeItem shader1 = get_input_shader(0, shader_type_); + NodeItem shader2 = get_input_shader(1, shader_type_); -NodeItem AddShaderNodeParser::compute_edf() -{ - NodeItem shader1 = get_input_shader(0, NodeItem::Type::EDF); - NodeItem shader2 = get_input_shader(1, NodeItem::Type::EDF); - - NodeItem res = empty(); - if (shader1 && !shader2) { - res = shader1; - } - else if (!shader1 && shader2) { - res = shader2; - } - else if (shader1 && shader2) { - res = create_node("add", "EDF"); - res.set_input("in1", shader1); - res.set_input("in2", shader2); - } - return res; -} - -NodeItem AddShaderNodeParser::compute_surface() -{ - NodeItem res = get_input_shader(0, NodeItem::Type::SurfaceShader); - if (!res) { - res = get_input_shader(1, NodeItem::Type::SurfaceShader); + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1; + } + else if (!shader1 && shader2) { + res = shader2; + } + else if (shader1 && shader2) { + res = shader1 + shader2; + } + break; + } + case NodeItem::Type::SurfaceShader: { + res = get_input_shader(0, shader_type_); + if (!res) { + res = get_input_shader(1, shader_type_); + } + break; + } + default: + BLI_assert_unreachable(); } return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc index c12ea98bee01..0b975cba89c0 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_diffuse.cc @@ -6,8 +6,12 @@ namespace blender::nodes::materialx { -NodeItem BSDFDiffuseNodeParser::compute_bsdf() +NodeItem BSDFDiffuseNodeParser::compute() { + if (shader_type_ != NodeItem::Type::BSDF) { + return empty(); + } + NodeItem color = get_input_value("Color", NodeItem::Type::Color3); NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float); NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); @@ -21,14 +25,4 @@ NodeItem BSDFDiffuseNodeParser::compute_bsdf() return res; } -NodeItem BSDFDiffuseNodeParser::compute_edf() -{ - return empty(); -} - -NodeItem BSDFDiffuseNodeParser::compute_surface() -{ - return empty(); -} - } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc index f50d5cb1db22..092953e7eed5 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_glass.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFGlassNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFGlassNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFGlassNodeParser::compute_surface() +NodeItem BSDFGlassNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc index 5e621595647a..98a372ec14c8 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_glossy.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFGlossyNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFGlossyNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFGlossyNodeParser::compute_surface() +NodeItem BSDFGlossyNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc index 8bd1ba9b4d70..2a39bd542d81 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_principled.cc @@ -6,20 +6,13 @@ namespace blender::nodes::materialx { -NodeItem BSDFPrincipledNodeParser::compute_bsdf() +NodeItem BSDFPrincipledNodeParser::compute() { - /* TODO: implement */ - return empty(); -} + if (shader_type_ != NodeItem::Type::SurfaceShader) { + /* TODO: implement for BSDF and EDF */ + return empty(); + } -NodeItem BSDFPrincipledNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFPrincipledNodeParser::compute_surface() -{ NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc index 53aac90fa335..2818cb52c298 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_refraction.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFRefractionNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFRefractionNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFRefractionNodeParser::compute_surface() +NodeItem BSDFRefractionNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc index a22b9d53ec62..7672813a63fb 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_sheen.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFSheenNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFSheenNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFSheenNodeParser::compute_surface() +NodeItem BSDFSheenNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc index 1c37fc914b2c..0c8fffa57804 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_toon.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFToonNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFToonNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFToonNodeParser::compute_surface() +NodeItem BSDFToonNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc index 1c7ff789abc8..82779b4203b1 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_translucent.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFTranslucentNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFTranslucentNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFTranslucentNodeParser::compute_surface() +NodeItem BSDFTranslucentNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc b/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc index 6354ffecdd4d..c9310de8ac38 100644 --- a/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc +++ b/source/blender/nodes/shader/materialx/nodes/bsdf_transparent.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem BSDFTransparentNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFTransparentNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem BSDFTransparentNodeParser::compute_surface() +NodeItem BSDFTransparentNodeParser::compute() { /* TODO: implement */ return empty(); diff --git a/source/blender/nodes/shader/materialx/nodes/emission.cc b/source/blender/nodes/shader/materialx/nodes/emission.cc index cef1ebf06d68..47d090e4e7fd 100644 --- a/source/blender/nodes/shader/materialx/nodes/emission.cc +++ b/source/blender/nodes/shader/materialx/nodes/emission.cc @@ -6,13 +6,12 @@ namespace blender::nodes::materialx { -NodeItem EmissionNodeParser::compute_bsdf() +NodeItem EmissionNodeParser::compute() { - return empty(); -} + if (shader_type_ != NodeItem::Type::EDF) { + return empty(); + } -NodeItem EmissionNodeParser::compute_edf() -{ NodeItem color = get_input_value("Color", NodeItem::Type::Color3); NodeItem strength = get_input_value("Strength", NodeItem::Type::Float); @@ -21,9 +20,4 @@ NodeItem EmissionNodeParser::compute_edf() return res; } -NodeItem EmissionNodeParser::compute_surface() -{ - return empty(); -} - } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc index 8e1d7f488ab9..264efa883462 100644 --- a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc @@ -6,55 +6,40 @@ namespace blender::nodes::materialx { -NodeItem MixShaderNodeParser::compute_bsdf() +NodeItem MixShaderNodeParser::compute() { - NodeItem fac = get_input_value(0, NodeItem::Type::Float); - NodeItem shader1 = get_input_shader(1, NodeItem::Type::BSDF); - NodeItem shader2 = get_input_shader(2, NodeItem::Type::BSDF); - NodeItem res = empty(); - if (shader1 && !shader2) { - res = shader1; - } - else if (!shader1 && shader2) { - res = shader2; - } - else if (shader1 && shader2) { - res = create_node("mix", "BSDF"); - res.set_input("fg", shader1); - res.set_input("bg", shader2); - res.set_input("mix", fac); - } - return res; -} + switch (shader_type_) { + case NodeItem::Type::BSDF: + case NodeItem::Type::EDF: { + NodeItem fac = get_input_value(0, NodeItem::Type::Float); + NodeItem shader1 = get_input_shader(1, shader_type_); + NodeItem shader2 = get_input_shader(2, shader_type_); -NodeItem MixShaderNodeParser::compute_edf() -{ - NodeItem fac = get_input_value(0, NodeItem::Type::Float); - NodeItem shader1 = get_input_shader(1, NodeItem::Type::EDF); - NodeItem shader2 = get_input_shader(2, NodeItem::Type::EDF); - - NodeItem res = empty(); - if (shader1 && !shader2) { - res = shader1; - } - else if (!shader1 && shader2) { - res = shader2; - } - else if (shader1 && shader2) { - res = create_node("mix", "EDF"); - res.set_input("fg", shader1); - res.set_input("bg", shader2); - res.set_input("mix", fac); - } - return res; -} - -NodeItem MixShaderNodeParser::compute_surface() -{ - NodeItem res = get_input_shader(0, NodeItem::Type::SurfaceShader); - if (!res) { - res = get_input_shader(1, NodeItem::Type::SurfaceShader); + NodeItem res = empty(); + if (shader1 && !shader2) { + res = shader1 * fac; + } + else if (!shader1 && shader2) { + res = shader2 * (value(1.0f) - fac); + } + else if (shader1 && shader2) { + res = create_node("mix", NodeItem::type(shader_type_)); + res.set_input("fg", shader1); + res.set_input("bg", shader2); + res.set_input("mix", fac); + } + break; + } + case NodeItem::Type::SurfaceShader: { + res = get_input_shader(1, shader_type_); + if (!res) { + res = get_input_shader(2, shader_type_); + } + break; + } + default: + BLI_assert_unreachable(); } return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index ebed94013165..94bbc30a386a 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -99,6 +99,20 @@ NodeItem::operator bool() const NodeItem NodeItem::operator+(const NodeItem &other) const { + Type type = this->type(); + if (ELEM(type, Type::BSDF, Type::EDF)) { + NodeItem res = empty(); + if (other.type() == type) { + res.node = graph_->addNode("add", MaterialX::EMPTY_STRING, this->type(type)); + res.set_input("in1", *this); + res.set_input("in2", other); + } + else { + BLI_assert_unreachable(); + } + return res; + } + return arithmetic(other, "add", [](float a, float b) { return a + b; }); } @@ -114,6 +128,20 @@ NodeItem NodeItem::operator-() const NodeItem NodeItem::operator*(const NodeItem &other) const { + Type type = this->type(); + if (ELEM(type, Type::BSDF, Type::EDF)) { + NodeItem res = empty(); + if (ELEM(other.type(), Type::Float, Type::Color3)) { + res.node = graph_->addNode("multiply", MaterialX::EMPTY_STRING, this->type(type)); + res.set_input("in1", *this); + res.set_input("in2", other); + } + else { + BLI_assert_unreachable(); + } + return res; + } + return arithmetic(other, "multiply", [](float a, float b) { return a * b; }); } @@ -332,6 +360,10 @@ NodeItem NodeItem::convert(Type to_type) const return *this; } if (!is_arithmetic(from_type) || !is_arithmetic(to_type)) { + CLOG_WARN(LOG_MATERIALX_SHADER, + "Cannot convert: %s -> %s", + type(from_type).c_str(), + type(to_type).c_str()); return empty(); } @@ -647,6 +679,8 @@ NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) return t1; } if (!is_arithmetic(t1) || !is_arithmetic(t2)) { + CLOG_WARN( + LOG_MATERIALX_SHADER, "Can't adjust types: %s <-> %s", type(t1).c_str(), type(t2).c_str()); return Type::Empty; } if (t1 < t2) { diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index 91177497d7f3..a9a1ec2d7f07 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -191,21 +191,6 @@ ShaderNodeParser::ShaderNodeParser(MaterialX::GraphElement *graph, { } -NodeItem ShaderNodeParser::compute() -{ - switch (shader_type_) { - case NodeItem::Type::BSDF: - return compute_bsdf(); - case NodeItem::Type::EDF: - return compute_edf(); - case NodeItem::Type::SurfaceShader: - return compute_surface(); - default: - BLI_assert_unreachable(); - } - return empty(); -} - NodeItem ShaderNodeParser::compute_full() { CLOG_INFO(LOG_MATERIALX_SHADER, diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.h b/source/blender/nodes/shader/materialx/nodes/node_parser.h index 42fee898b451..eb4502d6cd06 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.h +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.h @@ -61,11 +61,6 @@ class ShaderNodeParser : public NodeParser { const bNodeSocket *socket_out, NodeItem::Type shader_type); - NodeItem compute() override; - virtual NodeItem compute_bsdf() = 0; - virtual NodeItem compute_edf() = 0; - virtual NodeItem compute_surface() = 0; - protected: NodeItem compute_full() override; std::string node_name() override; @@ -92,9 +87,7 @@ template NodeItem NodeParser::value(const T &data) const class T : public ShaderNodeParser { \ public: \ using ShaderNodeParser::ShaderNodeParser; \ - NodeItem compute_bsdf() override; \ - NodeItem compute_edf() override; \ - NodeItem compute_surface() override; \ + NodeItem compute() override; \ }; DECLARE_NODE_PARSER(BrightContrastNodeParser) diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.cc b/source/blender/nodes/shader/materialx/nodes/output_material.cc index 9e07a87290b1..99d071973c82 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.cc +++ b/source/blender/nodes/shader/materialx/nodes/output_material.cc @@ -17,8 +17,8 @@ NodeItem OutputMaterialNodeParser::compute() { NodeItem surface = empty(); if (node_) { - NodeItem bsdf = compute_bsdf(); - NodeItem edf = compute_edf(); + NodeItem bsdf = get_input_shader("Surface", NodeItem::Type::BSDF); + NodeItem edf = get_input_shader("Surface", NodeItem::Type::EDF); if (bsdf || edf) { surface = create_node("surface", "surfaceshader"); if (bsdf) { @@ -29,7 +29,7 @@ NodeItem OutputMaterialNodeParser::compute() } } else { - surface = compute_surface(); + surface = get_input_shader("Surface", NodeItem::Type::SurfaceShader); } } else { @@ -41,21 +41,6 @@ NodeItem OutputMaterialNodeParser::compute() return res; } -NodeItem OutputMaterialNodeParser::compute_bsdf() -{ - return get_input_shader("Surface", NodeItem::Type::BSDF); -} - -NodeItem OutputMaterialNodeParser::compute_edf() -{ - return get_input_shader("Surface", NodeItem::Type::EDF); -} - -NodeItem OutputMaterialNodeParser::compute_surface() -{ - return get_input_shader("Surface", NodeItem::Type::SurfaceShader); -} - NodeItem OutputMaterialNodeParser::compute_default() { NodeItem surface = create_node("standard_surface", "surfaceshader"); diff --git a/source/blender/nodes/shader/materialx/nodes/output_material.h b/source/blender/nodes/shader/materialx/nodes/output_material.h index edcf67131d86..9f75d8761f87 100644 --- a/source/blender/nodes/shader/materialx/nodes/output_material.h +++ b/source/blender/nodes/shader/materialx/nodes/output_material.h @@ -15,9 +15,6 @@ class OutputMaterialNodeParser : public ShaderNodeParser { const Material *material, const bNode *node); NodeItem compute() override; - NodeItem compute_bsdf() override; - NodeItem compute_edf() override; - NodeItem compute_surface() override; using ShaderNodeParser::compute_full; NodeItem compute_default(); diff --git a/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc b/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc index 4c3425610a81..7efddb150a67 100644 --- a/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc +++ b/source/blender/nodes/shader/materialx/nodes/subsurface_scattering.cc @@ -6,19 +6,7 @@ namespace blender::nodes::materialx { -NodeItem SubsurfaceScatteringNodeParser::compute_bsdf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem SubsurfaceScatteringNodeParser::compute_edf() -{ - /* TODO: implement */ - return empty(); -} - -NodeItem SubsurfaceScatteringNodeParser::compute_surface() +NodeItem SubsurfaceScatteringNodeParser::compute() { /* TODO: implement */ return empty(); -- 2.30.2 From c6d2b9ed0ef44fa6be9856138dafc4ca5671d839 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 6 Sep 2023 23:48:40 +0300 Subject: [PATCH 10/11] Fixes, hided unimplemented shaders --- .../shader/materialx/nodes/add_shader.cc | 1 - .../shader/materialx/nodes/mix_shader.cc | 5 ++--- .../shader/materialx/nodes/node_parser.cc | 22 ++++++++++++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/add_shader.cc b/source/blender/nodes/shader/materialx/nodes/add_shader.cc index 28e095a9b2a9..8ac9d7ab4c24 100644 --- a/source/blender/nodes/shader/materialx/nodes/add_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/add_shader.cc @@ -15,7 +15,6 @@ NodeItem AddShaderNodeParser::compute() NodeItem shader1 = get_input_shader(0, shader_type_); NodeItem shader2 = get_input_shader(1, shader_type_); - NodeItem res = empty(); if (shader1 && !shader2) { res = shader1; } diff --git a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc index 264efa883462..fc27dc1a92ae 100644 --- a/source/blender/nodes/shader/materialx/nodes/mix_shader.cc +++ b/source/blender/nodes/shader/materialx/nodes/mix_shader.cc @@ -16,12 +16,11 @@ NodeItem MixShaderNodeParser::compute() NodeItem shader1 = get_input_shader(1, shader_type_); NodeItem shader2 = get_input_shader(2, shader_type_); - NodeItem res = empty(); if (shader1 && !shader2) { - res = shader1 * fac; + res = shader1 * (value(1.0f) - fac); } else if (!shader1 && shader2) { - res = shader2 * (value(1.0f) - fac); + res = shader2 * fac; } else if (shader1 && shader2) { res = create_node("mix", NodeItem::type(shader_type_)); diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index a9a1ec2d7f07..bb9914a2b164 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -160,6 +160,9 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to from_node->name, from_node->typeinfo->type); } + if (!parser) { + return res; + } /* Checking if node was already computed */ res.node = graph_->getNode(parser->node_name()); @@ -257,17 +260,17 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: switch (from_node->typeinfo->type) { CASE_SHADER_NODE_TYPE(SH_NODE_ADD_SHADER, AddShaderNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_DIFFUSE, BSDFDiffuseNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLASS, BSDFGlassNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLOSSY, BSDFGlossyNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLASS, BSDFGlassNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLOSSY, BSDFGlossyNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_REFRACTION, BSDFRefractionNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_SHEEN, BSDFSheenNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TOON, BSDFToonNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSLUCENT, BSDFTranslucentNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSPARENT, BSDFTransparentNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_REFRACTION, BSDFRefractionNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_SHEEN, BSDFSheenNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TOON, BSDFToonNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSLUCENT, BSDFTranslucentNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSPARENT, BSDFTransparentNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_EMISSION, EmissionNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_MIX_SHADER, MixShaderNodeParser) - CASE_SHADER_NODE_TYPE(SH_NODE_SUBSURFACE_SCATTERING, SubsurfaceScatteringNodeParser) + //CASE_SHADER_NODE_TYPE(SH_NODE_SUBSURFACE_SCATTERING, SubsurfaceScatteringNodeParser) default: CLOG_WARN(LOG_MATERIALX_SHADER, @@ -275,6 +278,9 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: from_node->name, from_node->typeinfo->type); } + if (!parser) { + return res; + } /* Checking if node was already computed */ res.node = graph_->getNode(parser->node_name()); -- 2.30.2 From fa280cbbfa7d11fe59b15b73736913828e96b0f6 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 7 Sep 2023 12:00:48 +0300 Subject: [PATCH 11/11] Added comments to NodeItem::Type, renamed adjust_types() -> cast_types() as more related. make format --- .../nodes/shader/materialx/nodes/node_item.cc | 13 ++++++++----- .../nodes/shader/materialx/nodes/node_item.h | 12 +++++++----- .../nodes/shader/materialx/nodes/node_parser.cc | 16 ++++++++-------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.cc b/source/blender/nodes/shader/materialx/nodes/node_item.cc index 94bbc30a386a..8c62b0bf4267 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_item.cc @@ -101,6 +101,7 @@ NodeItem NodeItem::operator+(const NodeItem &other) const { Type type = this->type(); if (ELEM(type, Type::BSDF, Type::EDF)) { + /* Special case: add BSDF/EDF shaders */ NodeItem res = empty(); if (other.type() == type) { res.node = graph_->addNode("add", MaterialX::EMPTY_STRING, this->type(type)); @@ -130,8 +131,10 @@ NodeItem NodeItem::operator*(const NodeItem &other) const { Type type = this->type(); if (ELEM(type, Type::BSDF, Type::EDF)) { + /* Special case: multiple BSDF/EDF shader by Float or Color3 */ NodeItem res = empty(); - if (ELEM(other.type(), Type::Float, Type::Color3)) { + Type other_type = other.type(); + if (ELEM(other_type, Type::Float, Type::Color3)) { res.node = graph_->addNode("multiply", MaterialX::EMPTY_STRING, this->type(type)); res.set_input("in1", *this); res.set_input("in2", other); @@ -178,7 +181,7 @@ bool NodeItem::operator==(const NodeItem &other) const NodeItem item1 = *this; NodeItem item2 = other; - Type to_type = adjust_types(item1, item2); + Type to_type = cast_types(item1, item2); if (to_type == Type::Empty) { return false; } @@ -560,7 +563,7 @@ NodeItem NodeItem::if_else(CompareOp op, auto item1 = if_val; auto item2 = else_val; - Type to_type = adjust_types(item1, item2); + Type to_type = cast_types(item1, item2); if (to_type == Type::Empty) { return res; } @@ -671,7 +674,7 @@ bool NodeItem::is_arithmetic(Type type) return type >= Type::Float && type <= Type::Color4; } -NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) +NodeItem::Type NodeItem::cast_types(NodeItem &item1, NodeItem &item2) { Type t1 = item1.type(); Type t2 = item2.type(); @@ -761,7 +764,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other, NodeItem res = empty(); NodeItem item1 = *this; NodeItem item2 = other; - Type to_type = adjust_types(item1, item2); + Type to_type = cast_types(item1, item2); if (to_type == Type::Empty) { return res; } diff --git a/source/blender/nodes/shader/materialx/nodes/node_item.h b/source/blender/nodes/shader/materialx/nodes/node_item.h index ded16bcd139c..6a223a4d8501 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_item.h +++ b/source/blender/nodes/shader/materialx/nodes/node_item.h @@ -11,22 +11,24 @@ namespace blender::nodes::materialx { class NodeItem { public: enum class Type { - Empty = 0, - Any, + Any = 0, + Empty, /* Value types */ String, Filename, Integer, + /* Block of arithmetic types. Ordered by type cast */ Float, Vector2, Vector3, - Vector4, Color3, + Vector4, Color4, + /* End of arithmetic types */ /* Shader types - * Note: There are only supported types */ + * NOTE: There are only supported types */ BSDF, EDF, SurfaceShader, @@ -106,7 +108,7 @@ class NodeItem { private: static bool is_arithmetic(Type type); - static Type adjust_types(NodeItem &item1, NodeItem &item2); + static Type cast_types(NodeItem &item1, NodeItem &item2); bool is_arithmetic() const; NodeItem arithmetic(const std::string &category, std::function func) const; diff --git a/source/blender/nodes/shader/materialx/nodes/node_parser.cc b/source/blender/nodes/shader/materialx/nodes/node_parser.cc index bb9914a2b164..b1987955cd1b 100644 --- a/source/blender/nodes/shader/materialx/nodes/node_parser.cc +++ b/source/blender/nodes/shader/materialx/nodes/node_parser.cc @@ -260,17 +260,17 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem: switch (from_node->typeinfo->type) { CASE_SHADER_NODE_TYPE(SH_NODE_ADD_SHADER, AddShaderNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_DIFFUSE, BSDFDiffuseNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLASS, BSDFGlassNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLOSSY, BSDFGlossyNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLASS, BSDFGlassNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_GLOSSY, BSDFGlossyNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_REFRACTION, BSDFRefractionNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_SHEEN, BSDFSheenNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TOON, BSDFToonNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSLUCENT, BSDFTranslucentNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSPARENT, BSDFTransparentNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_REFRACTION, BSDFRefractionNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_SHEEN, BSDFSheenNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TOON, BSDFToonNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSLUCENT, BSDFTranslucentNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_BSDF_TRANSPARENT, BSDFTransparentNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_EMISSION, EmissionNodeParser) CASE_SHADER_NODE_TYPE(SH_NODE_MIX_SHADER, MixShaderNodeParser) - //CASE_SHADER_NODE_TYPE(SH_NODE_SUBSURFACE_SCATTERING, SubsurfaceScatteringNodeParser) + // CASE_SHADER_NODE_TYPE(SH_NODE_SUBSURFACE_SCATTERING, SubsurfaceScatteringNodeParser) default: CLOG_WARN(LOG_MATERIALX_SHADER, -- 2.30.2