Skip to content

Commit e437b2b

Browse files
chellmuthChris Hellmuth
authored andcommitted
Refactor: Remove ShaderNode's nodeDefName in favor of new OsoNode Impl class
1 parent 08e2d19 commit e437b2b

File tree

7 files changed

+81
-30
lines changed

7 files changed

+81
-30
lines changed

source/MaterialXGenOslNodes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
file(GLOB_RECURSE materialx_source
22
"${CMAKE_CURRENT_SOURCE_DIR}/OslNodesShaderGenerator.cpp"
33
"${CMAKE_CURRENT_SOURCE_DIR}/OslNodesSyntax.cpp"
4+
"${CMAKE_CURRENT_SOURCE_DIR}/Nodes/OsoNode.cpp"
45
)
56
file(GLOB_RECURSE materialx_headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h*")
67

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright Contributors to the MaterialX Project
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
#include <MaterialXGenOslNodes/Nodes/OsoNode.h>
7+
#include <MaterialXGenShader/GenContext.h>
8+
9+
MATERIALX_NAMESPACE_BEGIN
10+
11+
ShaderNodeImplPtr OsoNode::create()
12+
{
13+
return std::make_shared<OsoNode>();
14+
}
15+
16+
void OsoNode::initialize(const InterfaceElement& element, GenContext& context)
17+
{
18+
ShaderNodeImpl::initialize(element, context);
19+
20+
if (!element.isA<Implementation>())
21+
{
22+
throw ExceptionShaderGenError("Element '" + element.getName() + "' is not an Implementation element");
23+
}
24+
const Implementation& impl = static_cast<const Implementation&>(element);
25+
26+
// Implementation's function attr is the oso filename, file attr is its directory
27+
_osoName = impl.getFunction();
28+
_osoPath = impl.getFile();
29+
30+
// Set hash using the oso name.
31+
_hash = std::hash<string>{}(_osoName);
32+
}
33+
34+
MATERIALX_NAMESPACE_END
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Copyright Contributors to the MaterialX Project
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
#ifndef MATERIALX_OSONODE_H
7+
#define MATERIALX_OSONODE_H
8+
9+
#include <MaterialXGenOslNodes/Export.h>
10+
11+
#include <MaterialXGenShader/ShaderNodeImpl.h>
12+
13+
MATERIALX_NAMESPACE_BEGIN
14+
15+
class MX_GENOSLNODES_API OsoNode : public ShaderNodeImpl
16+
{
17+
public:
18+
static ShaderNodeImplPtr create();
19+
20+
void initialize(const InterfaceElement& element, GenContext& context) override;
21+
22+
const string& getOsoName() const { return _osoName; }
23+
const string& getOsoPath() const { return _osoPath; }
24+
25+
private:
26+
string _osoName;
27+
string _osoPath;
28+
};
29+
30+
MATERIALX_NAMESPACE_END
31+
32+
#endif

source/MaterialXGenOslNodes/OslNodesShaderGenerator.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
#include <MaterialXGenOslNodes/OslNodesShaderGenerator.h>
77
#include <MaterialXGenOslNodes/OslNodesSyntax.h>
8+
#include <MaterialXGenOslNodes/Nodes/OsoNode.h>
89

910
#include <MaterialXGenShader/GenContext.h>
1011
#include <MaterialXGenShader/Shader.h>
1112
#include <MaterialXGenShader/TypeDesc.h>
1213
#include <MaterialXGenShader/ShaderStage.h>
13-
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
1414

1515

1616
MATERIALX_NAMESPACE_BEGIN
@@ -26,6 +26,11 @@ OslNodesShaderGenerator::OslNodesShaderGenerator(TypeSystemPtr typeSystem) :
2626
{
2727
}
2828

29+
ShaderNodeImplPtr OslNodesShaderGenerator::createShaderNodeImplForImplementation(const NodeDef& /* nodedef */) const
30+
{
31+
return OsoNode::create();
32+
}
33+
2934
static string paramString(const string& paramType, const string& paramName, const string& paramValue)
3035
{
3136
return "param " + paramType + " " + paramName + " " + paramValue + " ;";
@@ -106,21 +111,13 @@ ShaderPtr OslNodesShaderGenerator::generate(const string& name, ElementPtr eleme
106111
// Keep track of the root output, so we can connect it to our setCi node
107112
lastOutput = node->getOutput(0);
108113

109-
NodeDefPtr nodeDef = document->getNodeDef(node->getNodeDefName());
110-
ImplementationPtr impl = nodeDef->getImplementation("genoslnodes")->asA<Implementation>();
111-
112-
if (!impl)
113-
{
114-
printf("Skipping test due to missing implementation\n");
115-
return nullptr;
116-
}
117-
118-
string osoName = impl->getFunction();
114+
const ShaderNodeImpl& impl = node->getImplementation();
115+
const OsoNode& osoNodeImpl = dynamic_cast<const OsoNode&>(impl);
119116

120-
string osoPath = impl->getFile();
117+
const string osoPath = osoNodeImpl.getOsoPath();
121118
osoPaths.insert(osoPath);
122119

123-
emitLine("shader " + osoName + " " + nodeName + " ;", stage, false);
120+
emitLine("shader " + osoNodeImpl.getOsoName() + " " + nodeName + " ;", stage, false);
124121
lastNodeName = nodeName;
125122
}
126123

source/MaterialXGenOslNodes/OslNodesShaderGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class MX_GENOSLNODES_API OslNodesShaderGenerator : public ShaderGenerator
3636
return std::make_shared<OslNodesShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
3737
}
3838

39+
ShaderNodeImplPtr createShaderNodeImplForImplementation(const NodeDef& nodedef) const override;
40+
3941
/// Return a unique identifier for the target this generator is for
4042
const string& getTarget() const override { return TARGET; }
4143

source/MaterialXGenShader/ShaderNode.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,16 @@ const string ShaderNode::GEOMETRIC_GROUPNAME = "geometric";
161161
//
162162

163163
ShaderNode::ShaderNode(const ShaderGraph* parent, const string& name) :
164-
ShaderNode(parent, name, "")
165-
{
166-
}
167-
168-
ShaderNode::ShaderNode(const ShaderGraph* parent, const string& name, const string& nodeDefName) :
169164
_parent(parent),
170165
_name(name),
171166
_classification(0),
172-
_impl(nullptr),
173-
_nodeDefName(nodeDefName)
167+
_impl(nullptr)
174168
{
175169
}
176170

177171
ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef, GenContext& context)
178172
{
179-
ShaderNodePtr newNode = std::make_shared<ShaderNode>(parent, name, nodeDef.getName());
173+
ShaderNodePtr newNode = std::make_shared<ShaderNode>(parent, name);
180174

181175
const ShaderGenerator& shadergen = context.getShaderGenerator();
182176

source/MaterialXGenShader/ShaderNode.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ class MX_GENSHADER_API ShaderNode
377377
public:
378378
/// Constructor.
379379
ShaderNode(const ShaderGraph* parent, const string& name);
380-
ShaderNode(const ShaderGraph* parent, const string& name, const string& nodeDefName);
381380

382381
/// Create a new node from a nodedef.
383382
static ShaderNodePtr create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef,
@@ -435,12 +434,6 @@ class MX_GENSHADER_API ShaderNode
435434
return *_impl;
436435
}
437436

438-
// Return the NodeDef name associated with this node.
439-
const string& getNodeDefName() const
440-
{
441-
return _nodeDefName;
442-
}
443-
444437
/// Initialize this shader node with all required data
445438
/// from the given node and nodedef.
446439
void initialize(const Node& node, const NodeDef& nodeDef, GenContext& context);
@@ -511,8 +504,6 @@ class MX_GENSHADER_API ShaderNode
511504
ShaderNodeImplPtr _impl;
512505
ShaderMetadataVecPtr _metadata;
513506

514-
string _nodeDefName;
515-
516507
friend class ShaderGraph;
517508
};
518509

0 commit comments

Comments
 (0)