diff --git a/source/blender/nodes/shader/materialx/group_nodes.cc b/source/blender/nodes/shader/materialx/group_nodes.cc index 6b7d67154913..dac98d674dab 100644 --- a/source/blender/nodes/shader/materialx/group_nodes.cc +++ b/source/blender/nodes/shader/materialx/group_nodes.cc @@ -3,13 +3,27 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ #include "group_nodes.h" -#include "node_item.h" #include "node_parser.h" #include "BLI_vector.hh" namespace blender::nodes::materialx { +GroupNodeParser::GroupNodeParser(MaterialX::GraphElement *graph, + const Depsgraph *depsgraph, + const Material *material, + const bNode *node, + const bNodeSocket *socket_out, + NodeItem::Type to_type, + GroupNodeParser *group_parser, + ExportImageFunction export_image_fn, + bool use_group_default) + : NodeParser( + graph, depsgraph, material, node, socket_out, to_type, group_parser, export_image_fn), + use_group_default_(use_group_default) +{ +} + NodeItem GroupNodeParser::compute() { NodeItem res = empty(); @@ -32,16 +46,16 @@ NodeItem GroupNodeParser::compute() graph = group_graph.get(); #endif - NodeItem out = - GroupOutputNodeParser(graph, + NodeItem out = GroupOutputNodeParser(graph, depsgraph_, material_, node_out, socket_out_, to_type_, this, - export_image_fn_) - .compute_full(); + export_image_fn_, + use_group_default_) + .compute_full(); #ifdef USE_MATERIALX_NODEGRAPH /* We have to be in NodeParser's graph_, therefore copying output */ @@ -66,7 +80,8 @@ NodeItem GroupOutputNodeParser::compute() #ifdef USE_MATERIALX_NODEGRAPH Vector values; for (auto socket_in : node_->input_sockets()) { - NodeItem value = get_input_value(socket_in->index(), NodeItem::Type::Any); + NodeItem value = get_input_value( + socket_in->index(), NodeItem::is_arithmetic(to_type_) ? NodeItem::Type::Any : to_type_); if (value.value) { value = create_node("constant", value.type(), {{"value", value}}); } @@ -75,12 +90,12 @@ NodeItem GroupOutputNodeParser::compute() Vector outputs; for (int i = 0; i < values.size(); ++i) { if (values[i]) { - outputs.append(create_output("output" + std::to_string(i + 1), values[i])); + outputs.append(create_output(out_name(node_->input_sockets()[i]), values[i])); } } return outputs[socket_out_->index()]; #else - if (NodeItem::is_arithmetic(to_type_)) { + if (use_group_default_) { return get_input_value(socket_out_->index(), to_type_); } return get_input_link(socket_out_->index(), to_type_); @@ -89,15 +104,6 @@ NodeItem GroupOutputNodeParser::compute() NodeItem GroupOutputNodeParser::compute_full() { -#ifdef USE_MATERIALX_NODEGRAPH - NodeItem res = empty(); - - /* Checking if output was already computed */ - res.output = graph_->getOutput("output" + std::to_string(socket_out_->index() + 1)); - if (res.output) { - return res; - } - CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d] => %s", @@ -105,6 +111,15 @@ NodeItem GroupOutputNodeParser::compute_full() node_->typeinfo->type, NodeItem::type(to_type_).c_str()); +#ifdef USE_MATERIALX_NODEGRAPH + NodeItem res = empty(); + + /* Checking if output was already computed */ + res.output = graph_->getOutput(out_name(socket_out_)); + if (res.output) { + return res; + } + res = compute(); return res; #else @@ -112,6 +127,11 @@ NodeItem GroupOutputNodeParser::compute_full() #endif } +std::string GroupOutputNodeParser::out_name(const bNodeSocket *out_socket) +{ + return MaterialX::createValidName(std::string("out_") + out_socket->name); +} + NodeItem GroupInputNodeParser::compute() { #ifdef USE_MATERIALX_NODEGRAPH @@ -121,11 +141,11 @@ NodeItem GroupInputNodeParser::compute() } if (value.value) { - value = create_node("constant", value.type(), {{"value", value}}); + value = group_parser_->create_node("constant", value.type(), {{"value", value}}); } - return create_input("input" + std::to_string(socket_out_->index() + 1), value); + return create_input(in_name(), value); #else - if (NodeItem::is_arithmetic(to_type_)) { + if (use_group_default_) { return group_parser_->get_input_value(socket_out_->index(), to_type_); } return group_parser_->get_input_link(socket_out_->index(), to_type_); @@ -134,15 +154,6 @@ NodeItem GroupInputNodeParser::compute() NodeItem GroupInputNodeParser::compute_full() { -#ifdef USE_MATERIALX_NODEGRAPH - NodeItem res = empty(); - - /* Checking if output was already computed */ - res.input = graph_->getInput("input" + std::to_string(socket_out_->index() + 1)); - if (res.input) { - return res; - } - CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d] => %s", @@ -150,6 +161,15 @@ NodeItem GroupInputNodeParser::compute_full() node_->typeinfo->type, NodeItem::type(to_type_).c_str()); +#ifdef USE_MATERIALX_NODEGRAPH + NodeItem res = empty(); + + /* Checking if input was already computed */ + res.input = graph_->getInput(in_name()); + if (res.input) { + return res; + } + res = compute(); return res; #else @@ -157,4 +177,9 @@ NodeItem GroupInputNodeParser::compute_full() #endif } +std::string GroupInputNodeParser::in_name() const +{ + return MaterialX::createValidName(std::string("in_") + socket_out_->name); +} + } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/group_nodes.h b/source/blender/nodes/shader/materialx/group_nodes.h index a486a4af5fd6..6072f6aa9062 100644 --- a/source/blender/nodes/shader/materialx/group_nodes.h +++ b/source/blender/nodes/shader/materialx/group_nodes.h @@ -15,27 +15,43 @@ namespace blender::nodes::materialx { class GroupInputNodeParser; class GroupNodeParser : public NodeParser { - friend NodeParser; friend GroupInputNodeParser; + protected: + bool use_group_default_; + public: - using NodeParser::NodeParser; + GroupNodeParser(MaterialX::GraphElement *graph, + const Depsgraph *depsgraph, + const Material *material, + const bNode *node, + const bNodeSocket *socket_out, + NodeItem::Type to_type, + GroupNodeParser *group_parser, + ExportImageFunction export_image_fn, + bool use_group_default); NodeItem compute() override; NodeItem compute_full() override; }; -class GroupOutputNodeParser : public NodeParser { +class GroupOutputNodeParser : public GroupNodeParser { public: - using NodeParser::NodeParser; + using GroupNodeParser::GroupNodeParser; NodeItem compute() override; NodeItem compute_full() override; + + private: + static std::string out_name(const bNodeSocket *out_socket); }; -class GroupInputNodeParser : public NodeParser { +class GroupInputNodeParser : public GroupNodeParser { public: - using NodeParser::NodeParser; + using GroupNodeParser::GroupNodeParser; NodeItem compute() override; NodeItem compute_full() override; + + private: + std::string in_name() const; }; } // namespace blender::nodes::materialx diff --git a/source/blender/nodes/shader/materialx/node_item.cc b/source/blender/nodes/shader/materialx/node_item.cc index bcb7bdf52f9d..d65332fdb02d 100644 --- a/source/blender/nodes/shader/materialx/node_item.cc +++ b/source/blender/nodes/shader/materialx/node_item.cc @@ -174,12 +174,12 @@ NodeItem NodeItem::operator/(const NodeItem &other) const NodeItem NodeItem::operator%(const NodeItem &other) const { return arithmetic( - other, "modulo", [](float a, float b) { return b == 0.0f ? 0.0f : std::fmodf(a, b); }); + other, "modulo", [](float a, float b) { return b == 0.0f ? 0.0f : std::fmod(a, b); }); } NodeItem NodeItem::operator^(const NodeItem &other) const { - return arithmetic(other, "power", [](float a, float b) { return std::powf(a, b); }); + return arithmetic(other, "power", [](float a, float b) { return std::pow(a, b); }); } NodeItem NodeItem::operator[](int index) const @@ -245,17 +245,17 @@ bool NodeItem::operator!=(const NodeItem &other) const NodeItem NodeItem::abs() const { - return arithmetic("absval", [](float a) { return std::fabsf(a); }); + return arithmetic("absval", [](float a) { return std::abs(a); }); } NodeItem NodeItem::floor() const { - return arithmetic("floor", [](float a) { return std::floorf(a); }); + return arithmetic("floor", [](float a) { return std::floor(a); }); } NodeItem NodeItem::ceil() const { - return arithmetic("ceil", [](float a) { return std::ceilf(a); }); + return arithmetic("ceil", [](float a) { return std::ceil(a); }); } NodeItem NodeItem::length() const @@ -400,38 +400,37 @@ NodeItem NodeItem::rotate(const NodeItem &angle_xyz, bool invert) NodeItem NodeItem::sin() const { - return to_vector().arithmetic("sin", [](float a) { return std::sinf(a); }); + return to_vector().arithmetic("sin", [](float a) { return std::sin(a); }); } NodeItem NodeItem::cos() const { - return to_vector().arithmetic("cos", [](float a) { return std::cosf(a); }); + return to_vector().arithmetic("cos", [](float a) { return std::cos(a); }); } NodeItem NodeItem::tan() const { - return to_vector().arithmetic("tan", [](float a) { return std::tanf(a); }); + return to_vector().arithmetic("tan", [](float a) { return std::tan(a); }); } NodeItem NodeItem::asin() const { - return to_vector().arithmetic("asin", [](float a) { return std::asinf(a); }); + return to_vector().arithmetic("asin", [](float a) { return std::asin(a); }); } NodeItem NodeItem::acos() const { - return to_vector().arithmetic("acos", [](float a) { return std::acosf(a); }); + return to_vector().arithmetic("acos", [](float a) { return std::acos(a); }); } NodeItem NodeItem::atan() const { - return to_vector().arithmetic("atan", [](float a) { return std::atanf(a); }); + return to_vector().arithmetic("atan", [](float a) { return std::atan(a); }); } NodeItem NodeItem::atan2(const NodeItem &other) const { - return to_vector().arithmetic( - other, "atan2", [](float a, float b) { return std::atan2f(a, b); }); + return to_vector().arithmetic(other, "atan2", [](float a, float b) { return std::atan2(a, b); }); } NodeItem NodeItem::sinh() const @@ -456,12 +455,12 @@ NodeItem NodeItem::tanh() const NodeItem NodeItem::ln() const { - return to_vector().arithmetic("ln", [](float a) { return std::logf(a); }); + return to_vector().arithmetic("ln", [](float a) { return std::log(a); }); } NodeItem NodeItem::sqrt() const { - return to_vector().arithmetic("sqrt", [](float a) { return std::sqrtf(a); }); + return to_vector().arithmetic("sqrt", [](float a) { return std::sqrt(a); }); } NodeItem NodeItem::sign() const @@ -471,7 +470,7 @@ NodeItem NodeItem::sign() const NodeItem NodeItem::exp() const { - return to_vector().arithmetic("exp", [](float a) { return std::expf(a); }); + return to_vector().arithmetic("exp", [](float a) { return std::exp(a); }); } NodeItem NodeItem::convert(Type to_type) const diff --git a/source/blender/nodes/shader/materialx/node_parser.cc b/source/blender/nodes/shader/materialx/node_parser.cc index 2f14a971ecb6..beedcab34710 100644 --- a/source/blender/nodes/shader/materialx/node_parser.cc +++ b/source/blender/nodes/shader/materialx/node_parser.cc @@ -115,12 +115,12 @@ NodeItem NodeParser::get_input_default(int index, NodeItem::Type to_type) NodeItem NodeParser::get_input_link(const std::string &name, NodeItem::Type to_type) { - return get_input_link(node_->input_by_identifier(name), to_type); + return get_input_link(node_->input_by_identifier(name), to_type, false); } NodeItem NodeParser::get_input_link(int index, NodeItem::Type to_type) { - return get_input_link(node_->input_socket(index), to_type); + return get_input_link(node_->input_socket(index), to_type, false); } NodeItem NodeParser::get_input_value(const std::string &name, NodeItem::Type to_type) @@ -167,6 +167,10 @@ NodeItem NodeParser::texcoord_node(NodeItem::Type type) NodeItem NodeParser::get_default(const bNodeSocket &socket, NodeItem::Type to_type) { NodeItem res = empty(); + if (!NodeItem::is_arithmetic(to_type) && to_type != NodeItem::Type::Any) { + return res; + } + switch (socket.type) { case SOCK_CUSTOM: /* Return empty */ @@ -195,7 +199,9 @@ NodeItem NodeParser::get_default(const bNodeSocket &socket, NodeItem::Type to_ty return res.convert(to_type); } -NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to_type) +NodeItem NodeParser::get_input_link(const bNodeSocket &socket, + NodeItem::Type to_type, + bool use_group_default) { const bNodeLink *link = socket.link; if (!(link && link->is_used())) { @@ -221,7 +227,8 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to link->fromsock, to_type, group_parser_, - export_image_fn_) + export_image_fn_, + use_group_default) .compute_full(); } if (from_node->is_group_input()) { @@ -232,7 +239,8 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to link->fromsock, to_type, group_parser_, - export_image_fn_) + export_image_fn_, + use_group_default) .compute_full(); } @@ -252,7 +260,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to NodeItem NodeParser::get_input_value(const bNodeSocket &socket, NodeItem::Type to_type) { - NodeItem res = get_input_link(socket, to_type); + NodeItem res = get_input_link(socket, to_type, true); if (!res) { res = get_default(socket, to_type); } diff --git a/source/blender/nodes/shader/materialx/node_parser.h b/source/blender/nodes/shader/materialx/node_parser.h index 6a93ad38f0fd..83a09aa5b767 100644 --- a/source/blender/nodes/shader/materialx/node_parser.h +++ b/source/blender/nodes/shader/materialx/node_parser.h @@ -18,7 +18,7 @@ extern struct CLG_LogRef *LOG_MATERIALX_SHADER; class GroupNodeParser; -using ExportImageFunction = std::function; +using ExportImageFunction = std::function; /** * This is base abstraction class for parsing Blender nodes into MaterialX nodes. @@ -71,7 +71,9 @@ class NodeParser { private: NodeItem get_default(const bNodeSocket &socket, NodeItem::Type to_type); - NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type to_type); + NodeItem get_input_link(const bNodeSocket &socket, + NodeItem::Type to_type, + bool use_group_default); NodeItem get_input_value(const bNodeSocket &socket, NodeItem::Type to_type); }; @@ -123,8 +125,7 @@ struct NodeParserData { void node_shader_materialx(void *data, struct bNode *node, struct bNodeSocket *out) \ { \ materialx::NodeParserData *d = reinterpret_cast(data); \ - d->result = MaterialXNodeParser( \ - d->graph, \ + d->result = MaterialXNodeParser(d->graph, \ d->depsgraph, \ d->material, \ node, \