Skip to content

Commit

Permalink
Merge pull request #53 from jstone-lucasfilm/master
Browse files Browse the repository at this point in the history
Add a validation rule for shader references to non-shader nodedefs
  • Loading branch information
jstone-lucasfilm authored Dec 7, 2017
2 parents 9923d29 + 3537f7b commit 2c093c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
5 changes: 4 additions & 1 deletion source/MaterialXCore/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// All rights reserved. See LICENSE.txt for license.
//

#include <MaterialXCore/Node.h>
#include <MaterialXCore/Definition.h>

#include <MaterialXCore/Document.h>

namespace MaterialX
{

const string COLOR_SEMANTIC = "color";
const string SHADER_SEMANTIC = "shader";

const string NodeDef::NODE_ATTRIBUTE = "node";
const string TypeDef::SEMANTIC_ATTRIBUTE = "semantic";
const string TypeDef::CONTEXT_ATTRIBUTE = "context";
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXCore/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
namespace MaterialX
{

extern const string COLOR_SEMANTIC;
extern const string SHADER_SEMANTIC;

/// A shared pointer to a NodeDef
using NodeDefPtr = shared_ptr<class NodeDef>;
/// A shared pointer to a const NodeDef
Expand Down
14 changes: 13 additions & 1 deletion source/MaterialXCore/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ OutputPtr BindInput::getConnectedOutput() const
// ShaderRef methods
//

NodeDefPtr ShaderRef::getNodeDef()
NodeDefPtr ShaderRef::getNodeDef() const
{
if (hasNodeDefString())
{
Expand All @@ -203,6 +203,18 @@ NodeDefPtr ShaderRef::getNodeDef()
return NodeDefPtr();
}

bool ShaderRef::validate(string * message) const
{
bool res = true;
NodeDefPtr nodeDef = getNodeDef();
TypeDefPtr typeDef = nodeDef ? getDocument()->getTypeDef(nodeDef->getType()) : TypeDefPtr();
if (typeDef)
{
validateRequire(typeDef->getSemantic() == SHADER_SEMANTIC, res, message, "Shader reference to a non-shader nodedef");
}
return Element::validate(message) && res;
}

//
// Override methods
//
Expand Down
10 changes: 9 additions & 1 deletion source/MaterialXCore/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class ShaderRef : public Element
/// @{

/// Return the NodeDef, if any, that this element references.
NodeDefPtr getNodeDef();
NodeDefPtr getNodeDef() const;

/// @}
/// @name Output References
Expand All @@ -508,6 +508,14 @@ class ShaderRef : public Element
}

/// @}
/// @name Validation
/// @{

/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;

/// @}

public:
static const string CATEGORY;
Expand Down

0 comments on commit 2c093c0

Please sign in to comment.