From 8f80c831821c52bac1aade4471080bb1ace701e5 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Mon, 27 May 2024 16:38:23 -0400 Subject: [PATCH 1/2] Propagate added inputs created inside a NodeGraph Custom image nodes like ND_gltf_colorimage and ND_UsdUVTexture did not produce uv_scale and uv_offset inputs. Fix that by propagating the added inputs found inside a CompoundNode which represents a NodeGraph implementation of a NodeDef. Fixes https://github.com/AcademySoftwareFoundation/MaterialX/issues/1716 --- .../Nodes/ClosureLayerNodeMdl.cpp | 5 +++ .../Nodes/ClosureLayerNodeMdl.h | 1 + source/MaterialXGenMdl/Nodes/ImageNodeMdl.cpp | 7 ++++ source/MaterialXGenMdl/Nodes/ImageNodeMdl.h | 2 ++ .../MaterialXGenShader/Nodes/CompoundNode.cpp | 15 +++++++++ .../MaterialXGenShader/Nodes/CompoundNode.h | 2 ++ .../MaterialXGenShader/Nodes/HwImageNode.cpp | 5 +++ source/MaterialXGenShader/Nodes/HwImageNode.h | 3 ++ source/MaterialXGenShader/ShaderGraph.cpp | 33 +++++++++++++++++++ source/MaterialXGenShader/ShaderGraph.h | 10 ++++++ source/MaterialXGenShader/ShaderNode.h | 6 ++++ source/MaterialXGenShader/ShaderNodeImpl.cpp | 5 +++ source/MaterialXGenShader/ShaderNodeImpl.h | 3 ++ 13 files changed, 97 insertions(+) diff --git a/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.cpp b/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.cpp index 1dfa45317f..0a506abf0f 100644 --- a/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.cpp +++ b/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.cpp @@ -172,4 +172,9 @@ void LayerableNodeMdl::addInputs(ShaderNode& node, GenContext& /*context*/) cons node.addInput(StringConstantsMdl::BASE, Type::BSDF); } +StringVec LayerableNodeMdl::addedInputNames() const +{ + return {1, StringConstantsMdl::BASE}; +} + MATERIALX_NAMESPACE_END diff --git a/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.h b/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.h index bced5cfac1..4870d0a086 100644 --- a/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.h +++ b/source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.h @@ -45,6 +45,7 @@ class MX_GENMDL_API LayerableNodeMdl : public SourceCodeNodeMdl static ShaderNodeImplPtr create(); void addInputs(ShaderNode& node, GenContext&) const override; + StringVec addedInputNames() const override; }; MATERIALX_NAMESPACE_END diff --git a/source/MaterialXGenMdl/Nodes/ImageNodeMdl.cpp b/source/MaterialXGenMdl/Nodes/ImageNodeMdl.cpp index 502711fe01..adb0f3d13d 100644 --- a/source/MaterialXGenMdl/Nodes/ImageNodeMdl.cpp +++ b/source/MaterialXGenMdl/Nodes/ImageNodeMdl.cpp @@ -23,6 +23,13 @@ void ImageNodeMdl::addInputs(ShaderNode& node, GenContext& context) const node.addInput(ImageNodeMdl::FLIP_V, Type::BOOLEAN)->setUniform(); } +StringVec ImageNodeMdl::addedInputNames() const +{ + auto retVal = BASE::addedInputNames(); + retVal.push_back(ImageNodeMdl::FLIP_V); + return retVal; +} + bool ImageNodeMdl::isEditable(const ShaderInput& input) const { if (input.getName() == ImageNodeMdl::FLIP_V) diff --git a/source/MaterialXGenMdl/Nodes/ImageNodeMdl.h b/source/MaterialXGenMdl/Nodes/ImageNodeMdl.h index fe88b2ce09..7bf8671af0 100644 --- a/source/MaterialXGenMdl/Nodes/ImageNodeMdl.h +++ b/source/MaterialXGenMdl/Nodes/ImageNodeMdl.h @@ -24,6 +24,8 @@ class MX_GENMDL_API ImageNodeMdl : public SourceCodeNodeMdl void addInputs(ShaderNode& node, GenContext& context) const override; + StringVec addedInputNames() const override; + bool isEditable(const ShaderInput& input) const override; void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override; diff --git a/source/MaterialXGenShader/Nodes/CompoundNode.cpp b/source/MaterialXGenShader/Nodes/CompoundNode.cpp index a7903d437d..d0cdcec031 100644 --- a/source/MaterialXGenShader/Nodes/CompoundNode.cpp +++ b/source/MaterialXGenShader/Nodes/CompoundNode.cpp @@ -45,6 +45,21 @@ void CompoundNode::initialize(const InterfaceElement& element, GenContext& conte _hash = std::hash{}(_functionName); } +void CompoundNode::addInputs(ShaderNode& node, GenContext& context) const +{ + // Propagate inputs added to the ShaderGraph + for (auto const& name: _rootGraph->getPropagatedAddedInputs()) { + const auto* inputSocket = _rootGraph->getInputSocket(name); + if (inputSocket) + { + ShaderInput* input = node.addInput(name, inputSocket->getType()); + input->setValue(inputSocket->getValue()); + } + } +} + +// Note: No addedInputNames here. Not necessary unless we start nesting ShaderGraphs. + void CompoundNode::createVariables(const ShaderNode&, GenContext& context, Shader& shader) const { // Gather shader inputs from all child nodes diff --git a/source/MaterialXGenShader/Nodes/CompoundNode.h b/source/MaterialXGenShader/Nodes/CompoundNode.h index 128701db80..047e783059 100644 --- a/source/MaterialXGenShader/Nodes/CompoundNode.h +++ b/source/MaterialXGenShader/Nodes/CompoundNode.h @@ -20,6 +20,8 @@ class MX_GENSHADER_API CompoundNode : public ShaderNodeImpl void initialize(const InterfaceElement& element, GenContext& context) override; + void addInputs(ShaderNode& node, GenContext& context) const override; + void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const override; void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override; diff --git a/source/MaterialXGenShader/Nodes/HwImageNode.cpp b/source/MaterialXGenShader/Nodes/HwImageNode.cpp index e8eddd6e1a..95a6ffa8c2 100644 --- a/source/MaterialXGenShader/Nodes/HwImageNode.cpp +++ b/source/MaterialXGenShader/Nodes/HwImageNode.cpp @@ -28,6 +28,11 @@ void HwImageNode::addInputs(ShaderNode& node, GenContext&) const input->setValue(Value::createValue(Vector2(0.0f, 0.0f))); } +StringVec HwImageNode::addedInputNames() const +{ + return {UV_SCALE, UV_OFFSET}; +} + void HwImageNode::setValues(const Node& node, ShaderNode& shaderNode, GenContext& context) const { // Remap uvs to normalized 0..1 space if the original UDIMs in a UDIM set diff --git a/source/MaterialXGenShader/Nodes/HwImageNode.h b/source/MaterialXGenShader/Nodes/HwImageNode.h index 58ddb9305c..8eeef0eab5 100644 --- a/source/MaterialXGenShader/Nodes/HwImageNode.h +++ b/source/MaterialXGenShader/Nodes/HwImageNode.h @@ -17,6 +17,9 @@ class MX_GENSHADER_API HwImageNode : public SourceCodeNode static ShaderNodeImplPtr create(); void addInputs(ShaderNode& node, GenContext& context) const override; + + StringVec addedInputNames() const override; + void setValues(const Node& node, ShaderNode& shaderNode, GenContext& context) const override; }; diff --git a/source/MaterialXGenShader/ShaderGraph.cpp b/source/MaterialXGenShader/ShaderGraph.cpp index f06c2496ff..76eecce71c 100644 --- a/source/MaterialXGenShader/ShaderGraph.cpp +++ b/source/MaterialXGenShader/ShaderGraph.cpp @@ -93,6 +93,7 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement, if (!newNode) { newNode = createNode(upstreamNode, context); + propagateAddedInputs(*newNode, newNode->addedInputNames()); } // @@ -424,6 +425,9 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const NodeGraph& n // Clear classification graph->_classification = 0; + // Allow added input propagation since this ShaderGraph is for a CompoundNode + graph->_propagateAddedInputs = true; + // Create input sockets from the nodedef graph->addInputSockets(*nodeDef, context); @@ -442,6 +446,35 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const NodeGraph& n return graph; } +void ShaderGraph::propagateAddedInputs(ShaderNode& node, const StringVec& addedInputs) +{ + if (!_propagateAddedInputs) { + return; + } + + for (auto const& name: addedInputs) { + auto* nodeInput = node.getInput(name); + if (nodeInput) { + auto* inputSocket = getInputSocket(name); + if (!inputSocket) { + // Only add an input socket once. This means that a NodeGraph + // with three embedded image nodes will only get a single + // pair of uv_scale and uv_offset inputs shared between + // all three nodes. + // TODO: Have as many uv_scale inputs as there are embedded image + // nodes. Requires a scheme to make their names unique. Bonus + // points if that name can be related to the filename input. + // NOTE: This is enough to have UDIMs working with ND_gltf_colorimage + // and ND_UsdUVTexture, so we stop here at this time. + inputSocket = addInputSocket(name, nodeInput->getType()); + _propagatedAddedInputs.push_back(name); + } + inputSocket->makeConnection(nodeInput); + inputSocket->setValue(nodeInput->getValue()); + } + } +} + ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name, ElementPtr element, GenContext& context) { ShaderGraphPtr graph; diff --git a/source/MaterialXGenShader/ShaderGraph.h b/source/MaterialXGenShader/ShaderGraph.h index 2b1d3d5d0b..fed30e5182 100644 --- a/source/MaterialXGenShader/ShaderGraph.h +++ b/source/MaterialXGenShader/ShaderGraph.h @@ -58,6 +58,9 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode static ShaderGraphPtr create(const ShaderGraph* parent, const NodeGraph& nodeGraph, GenContext& context); + /// Get the list of added inputs to propagate further up + const StringVec& getPropagatedAddedInputs() const { return _propagatedAddedInputs; } + /// Return true if this node is a graph. bool isAGraph() const override { return true; } @@ -132,6 +135,9 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode /// Add a node to the graph void addNode(ShaderNodePtr node); + /// Propagate additional inputs added on a node. + void propagateAddedInputs(ShaderNode& node, const StringVec& addedInputs); + /// Add input sockets from an interface element (nodedef, nodegraph or node) void addInputSockets(const InterfaceElement& elem, GenContext& context); @@ -188,6 +194,10 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode std::vector _nodeOrder; IdentifierMap _identifiers; + // Propagate or not the extra inputs added by nodes + bool _propagateAddedInputs = false; + StringVec _propagatedAddedInputs; + // Temporary storage for inputs that require color transformations std::unordered_map _inputColorTransformMap; // Temporary storage for inputs that require unit transformations diff --git a/source/MaterialXGenShader/ShaderNode.h b/source/MaterialXGenShader/ShaderNode.h index 4f2f44e27b..3f09f33b39 100644 --- a/source/MaterialXGenShader/ShaderNode.h +++ b/source/MaterialXGenShader/ShaderNode.h @@ -484,6 +484,12 @@ class MX_GENSHADER_API ShaderNode return (!_impl || _impl->isEditable(input)); } + /// Return input names that are added by the implementation of this node + virtual StringVec addedInputNames() const + { + return _impl ? _impl->addedInputNames() : StringVec{}; + } + protected: /// Create metadata from the nodedef according to registered metadata. void createMetadata(const NodeDef& nodeDef, GenContext& context); diff --git a/source/MaterialXGenShader/ShaderNodeImpl.cpp b/source/MaterialXGenShader/ShaderNodeImpl.cpp index 7cd929a8e0..8e02170f81 100644 --- a/source/MaterialXGenShader/ShaderNodeImpl.cpp +++ b/source/MaterialXGenShader/ShaderNodeImpl.cpp @@ -38,6 +38,11 @@ void ShaderNodeImpl::addInputs(ShaderNode&, GenContext&) const { } +StringVec ShaderNodeImpl::addedInputNames() const +{ + return {}; +} + void ShaderNodeImpl::setValues(const Node&, ShaderNode&, GenContext&) const { } diff --git a/source/MaterialXGenShader/ShaderNodeImpl.h b/source/MaterialXGenShader/ShaderNodeImpl.h index b9fd2a5148..7a816c276c 100644 --- a/source/MaterialXGenShader/ShaderNodeImpl.h +++ b/source/MaterialXGenShader/ShaderNodeImpl.h @@ -62,6 +62,9 @@ class MX_GENSHADER_API ShaderNodeImpl /// Add additional inputs on a node. virtual void addInputs(ShaderNode& node, GenContext& context) const; + /// Return input names that are added by this implementation + virtual StringVec addedInputNames() const; + /// Set values for additional inputs on a node. virtual void setValues(const Node& node, ShaderNode& shaderNode, GenContext& context) const; From ad8b2250336660cb191c0d4b4b222d07fd361d25 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Mon, 27 May 2024 17:05:49 -0400 Subject: [PATCH 2/2] Fix Windows preflight builds --- source/MaterialXGenShader/Nodes/CompoundNode.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/MaterialXGenShader/Nodes/CompoundNode.cpp b/source/MaterialXGenShader/Nodes/CompoundNode.cpp index d0cdcec031..46ed06630a 100644 --- a/source/MaterialXGenShader/Nodes/CompoundNode.cpp +++ b/source/MaterialXGenShader/Nodes/CompoundNode.cpp @@ -45,7 +45,7 @@ void CompoundNode::initialize(const InterfaceElement& element, GenContext& conte _hash = std::hash{}(_functionName); } -void CompoundNode::addInputs(ShaderNode& node, GenContext& context) const +void CompoundNode::addInputs(ShaderNode& node, GenContext&) const { // Propagate inputs added to the ShaderGraph for (auto const& name: _rootGraph->getPropagatedAddedInputs()) {