From 3537f7b58819e8b091853d42be073dee0ccf209a Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Wed, 6 Dec 2017 18:41:51 -0800 Subject: [PATCH] Add a validation rule for shader references to non-shader nodedefs --- source/MaterialXCore/Definition.cpp | 5 ++++- source/MaterialXCore/Definition.h | 3 +++ source/MaterialXCore/Material.cpp | 14 +++++++++++++- source/MaterialXCore/Material.h | 10 +++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/source/MaterialXCore/Definition.cpp b/source/MaterialXCore/Definition.cpp index 7a1f2d5d52..b130f086aa 100644 --- a/source/MaterialXCore/Definition.cpp +++ b/source/MaterialXCore/Definition.cpp @@ -3,13 +3,16 @@ // All rights reserved. See LICENSE.txt for license. // -#include +#include #include 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"; diff --git a/source/MaterialXCore/Definition.h b/source/MaterialXCore/Definition.h index c2d42cebaf..87446c9904 100644 --- a/source/MaterialXCore/Definition.h +++ b/source/MaterialXCore/Definition.h @@ -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; /// A shared pointer to a const NodeDef diff --git a/source/MaterialXCore/Material.cpp b/source/MaterialXCore/Material.cpp index c3e9537b7c..09a2d5f58d 100644 --- a/source/MaterialXCore/Material.cpp +++ b/source/MaterialXCore/Material.cpp @@ -189,7 +189,7 @@ OutputPtr BindInput::getConnectedOutput() const // ShaderRef methods // -NodeDefPtr ShaderRef::getNodeDef() +NodeDefPtr ShaderRef::getNodeDef() const { if (hasNodeDefString()) { @@ -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 // diff --git a/source/MaterialXCore/Material.h b/source/MaterialXCore/Material.h index 207465833c..a7b6b27edd 100644 --- a/source/MaterialXCore/Material.h +++ b/source/MaterialXCore/Material.h @@ -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 @@ -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;