Skip to content

Commit

Permalink
Misc merges from ILM master and viewer fork (AcademySoftwareFoundatio…
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardkwok authored Mar 29, 2019
1 parent 9f82057 commit 58f6acc
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 82 deletions.
4 changes: 2 additions & 2 deletions libraries/pbrlib/pbrlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<input name="tangent" type="vector3" defaultgeomprop="Tworld"/>
<input name="distribution" type="string" value="ggx" enum="ggx"/>
<input name="base" type="BSDF"/>
</nodedef>
</nodedef>

<!--
Node: <subsurface_brdf>
Expand Down Expand Up @@ -262,7 +262,7 @@
Calculates anisotropic surface roughness from a scalar roughness and anisotropy parameterization.
-->
<nodedef name="ND_roughness_anisotropy" node="roughness_anisotropy" type="roughnessinfo" nodegroup="pbr"
doc="Calculates anisotropic surface roughness from a scalar rougness/anisotropy parameterization.">
doc="Calculates anisotropic surface roughness from a scalar roughness/anisotropy parameterization.">
<input name="roughness" type="float" value="0.0"/>
<input name="anisotropy" type="float" value="0.0"/>
</nodedef>
Expand Down
1 change: 1 addition & 0 deletions libraries/pbrlib/pbrlib_ng.mtlx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
TM & (c) 2018 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
All rights reserved. See LICENSE.txt for license.
Expand Down
14 changes: 10 additions & 4 deletions source/MaterialXCore/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,6 @@ bool Element::validate(string* message) const
{
bool res = true;
validateRequire(isValidName(getName()), res, message, "Invalid element name");
if (hasColorSpace())
{
validateRequire(getDocument()->hasColorManagementSystem(), res, message, "Colorspace set without color management system");
}
if (hasInheritString())
{
bool validInherit = getInheritsFrom() && getInheritsFrom()->getCategory() == getCategory();
Expand Down Expand Up @@ -582,6 +578,16 @@ bool ValueElement::validate(string* message) const
{
validateRequire(getValue() != nullptr, res, message, "Invalid value");
}
if (hasInterfaceName())
{
ConstNodeGraphPtr nodeGraph = getAncestorOfType<NodeGraph>();
NodeDefPtr nodeDef = nodeGraph ? nodeGraph->getNodeDef() : nullptr;
if (nodeDef)
{
ValueElementPtr valueElem = nodeDef->getActiveValueElement(getInterfaceName());
validateRequire(valueElem != nullptr, res, message, "Interface name not found in referenced nodedef");
}
}
return TypedElement::validate(message) && res;
}

Expand Down
21 changes: 18 additions & 3 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ class Element : public std::enable_shared_from_this<Element>
return _childOrder;
}

/// Return a vector of all child elements that are instances of the given type,
/// optionally filtered by the given category string. The returned vector
/// maintains the order in which children were added.
/// Return a vector of all child elements that are instances of the given
/// subclass, optionally filtered by the given category string. The returned
/// vector maintains the order in which children were added.
template<class T> vector< shared_ptr<T> > getChildrenOfType(const string& category = EMPTY_STRING) const
{
vector< shared_ptr<T> > children;
Expand Down Expand Up @@ -606,6 +606,21 @@ class Element : public std::enable_shared_from_this<Element>
return getRoot()->asA<Document>();
}

/// Return the first ancestor of the given subclass, or an empty shared
/// pointer if no ancestor of this subclass is found.
template<class T> shared_ptr<const T> getAncestorOfType() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
{
shared_ptr<const T> typedElem = elem->asA<T>();
if (typedElem)
{
return typedElem;
}
}
return nullptr;
}

/// @}
/// @name Traversal
/// @{
Expand Down
81 changes: 40 additions & 41 deletions source/MaterialXCore/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,35 @@

namespace MaterialX
{

const string PortElement::NODE_NAME_ATTRIBUTE = "nodename";
const string PortElement::OUTPUT_ATTRIBUTE = "output";
const string PortElement::CHANNELS_ATTRIBUTE = "channels";
const string InterfaceElement::NODE_DEF_ATTRIBUTE = "nodedef";
const string Input::DEFAULT_GEOM_PROP_ATTRIBUTE = "defaultgeomprop";

//
// PortElement static data members
//

// Mapping from a type to the acceptable set of characters in a swizzle pattern
std::unordered_map<string, std::set<char>> PortElement::_swizzlePatterns =
// Map from type strings to swizzle pattern character sets.
const std::unordered_map<string, CharSet> PortElement::CHANNELS_CHARACTER_SET =
{
{ "float", { '0', '1', 'r', 'x' }},
{"color2", { '0', '1', 'r', 'a' }},
{"color3", { '0', '1', 'r', 'g', 'b' }},
{"color4", { '0', '1', 'r', 'g', 'b', 'a' }},
{"vector2", { '0', '1', 'x', 'y' }},
{"vector3", { '0', '1', 'x', 'y', 'z' }},
{"vector4", { '0', '1', 'x', 'y', 'z', 'w' }}
{ "float", { '0', '1', 'r', 'x' } },
{ "color2", { '0', '1', 'r', 'a' } },
{ "color3", { '0', '1', 'r', 'g', 'b' } },
{ "color4", { '0', '1', 'r', 'g', 'b', 'a' } },
{ "vector2", { '0', '1', 'x', 'y' } },
{ "vector3", { '0', '1', 'x', 'y', 'z' } },
{ "vector4", { '0', '1', 'x', 'y', 'z', 'w' } }
};

// Mapping from a type to the acceptable swizzle pattern size
std::unordered_map<string, size_t> PortElement::_swizzlePatternSizes =
// Map from type strings to swizzle pattern lengths.
const std::unordered_map<string, size_t> PortElement::CHANNELS_PATTERN_LENGTH =
{
{"float", 1},
{"color2", 2},
{"color3", 3},
{"color4", 4},
{"vector2", 2},
{"vector3", 3},
{"vector4", 4}
{ "float", 1 },
{ "color2", 2 },
{ "color3", 3 },
{ "color4", 4 },
{ "vector2", 2 },
{ "vector3", 3 },
{ "vector4", 4 }
};

//
Expand All @@ -64,13 +61,10 @@ void PortElement::setConnectedNode(NodePtr node)

NodePtr PortElement::getConnectedNode() const
{
for (ConstElementPtr elem = getSelf(); elem; elem = elem->getParent())
ConstGraphElementPtr graph = getAncestorOfType<GraphElement>();
if (graph)
{
ConstGraphElementPtr graph = elem->asA<GraphElement>();
if (graph)
{
return graph->getNode(getNodeName());
}
return graph->getNode(getNodeName());
}
return NodePtr();
}
Expand Down Expand Up @@ -99,7 +93,7 @@ bool PortElement::validate(string* message) const
const string& outputType = output->getType();
if (hasChannels())
{
validateRequire(supportsSwizzle(outputType, getType(), getChannels()), res, message, "Invalid channels attribute");
validateRequire(validChannelsString(getChannels(), outputType, getType()), res, message, "Invalid channels attribute");
}
else
{
Expand All @@ -110,7 +104,7 @@ bool PortElement::validate(string* message) const
}
else if (hasChannels())
{
validateRequire(supportsSwizzle(connectedNode->getType(), getType(), getChannels()), res, message, "Invalid channels attribute");
validateRequire(validChannelsString(getChannels(), connectedNode->getType(), getType()), res, message, "Invalid channels attribute");
}
else if(!hasChannels())
{
Expand All @@ -120,31 +114,36 @@ bool PortElement::validate(string* message) const
return ValueElement::validate(message) && res;
}

bool PortElement::validSwizzlePattern(const string &type, const string &pattern)
bool PortElement::validChannelsCharacters(const string& channels, const string& sourceType)
{
if (!_swizzlePatterns.count(type))
if (!CHANNELS_CHARACTER_SET.count(sourceType))
{
return false;
}
const std::set<char>& supportedChannels = _swizzlePatterns[type];
for (const char& channel : pattern)
const CharSet& validCharSet = CHANNELS_CHARACTER_SET.at(sourceType);
for (const char& channelChar : channels)
{
if (supportedChannels.count(channel) == 0)
if (!validCharSet.count(channelChar))
{
return false;
}
}
return true;
}

bool PortElement::validSwizzleSize(const string &type, const string &pattern)
bool PortElement::validChannelsString(const string& channels, const string& sourceType, const string& destinationType)
{
return (_swizzlePatternSizes.count(type) == 0) ? false : (pattern.size() == _swizzlePatternSizes[type]);
}
if (!validChannelsCharacters(channels, sourceType))
{
return false;
}
if (!CHANNELS_PATTERN_LENGTH.count(destinationType) ||
CHANNELS_PATTERN_LENGTH.at(destinationType) != channels.size())
{
return false;
}

bool PortElement::supportsSwizzle(const string &sourceType, const string& destinationType, const string &pattern)
{
return validSwizzlePattern(sourceType, pattern) && validSwizzleSize(destinationType, pattern);
return true;
}

//
Expand Down
21 changes: 10 additions & 11 deletions source/MaterialXCore/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ using InterfaceElementPtr = shared_ptr<InterfaceElement>;
/// A shared pointer to a const InterfaceElement
using ConstInterfaceElementPtr = shared_ptr<const InterfaceElement>;

using CharSet = std::set<char>;

/// @class Parameter
/// A parameter element within a Node or NodeDef.
///
Expand Down Expand Up @@ -170,16 +172,13 @@ class PortElement : public ValueElement
return getAttribute(CHANNELS_ATTRIBUTE);
}

/// Utility method which returns whether a given swizzle pattern
/// is valid for a given source type and destination type.
static bool supportsSwizzle(const string &sourceType, const string& destinationType, const string &pattern);

/// Utility that returns if a swizzle pattern string is acceptable for a given type.
static bool validSwizzlePattern(const string &type, const string &pattern);

/// Utility that returns if a swizzle pattern size is acceptable for a given type.
static bool validSwizzleSize(const string &type, const string &pattern);
/// Return true if the given channels characters are valid for the given
/// source type string.
static bool validChannelsCharacters(const string &channels, const string &sourceType);

/// Return true if the given swizzle pattern is valid for the given source
/// and destination type strings.
static bool validChannelsString(const string& channels, const string& sourceType, const string& destinationType);

/// @}
/// @name Connections
Expand Down Expand Up @@ -208,8 +207,8 @@ class PortElement : public ValueElement
static const string CHANNELS_ATTRIBUTE;

private:
static std::unordered_map<string, std::set<char>> _swizzlePatterns;
static std::unordered_map<string, size_t> _swizzlePatternSizes;
static const std::unordered_map<string, CharSet> CHANNELS_CHARACTER_SET;
static const std::unordered_map<string, size_t> CHANNELS_PATTERN_LENGTH;
};

/// @class Input
Expand Down
36 changes: 16 additions & 20 deletions source/MaterialXGenShader/Syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ const string Syntax::SINGLE_LINE_COMMENT = "// ";
const string Syntax::BEGIN_MULTI_LINE_COMMENT = "/* ";
const string Syntax::END_MULTI_LINE_COMMENT = " */";

namespace {

const std::unordered_map<char, size_t> CHANNELS_MAPPING =
{
{ 'r', 0 }, { 'x', 0 },
{ 'g', 1 }, { 'y', 1 },
{ 'b', 2 }, { 'z', 2 },
{ 'a', 3 }, { 'w', 3 }
};

} // anonymous namespace

//
// Syntax methods
//
Expand Down Expand Up @@ -106,14 +118,6 @@ const string& Syntax::getTypeDefinition(const TypeDesc* type) const

string Syntax::getSwizzledVariable(const string& srcName, const TypeDesc* srcType, const string& channels, const TypeDesc* dstType) const
{
static const std::unordered_map<char, size_t> s_channelsMapping =
{
{ 'r', 0 },{ 'x', 0 },
{ 'g', 1 },{ 'y', 1 },
{ 'b', 2 },{ 'z', 2 },
{ 'a', 3 },{ 'w', 3 }
};

const TypeSyntax& srcSyntax = getTypeSyntax(srcType);
const TypeSyntax& dstSyntax = getTypeSyntax(dstType);

Expand All @@ -130,8 +134,8 @@ string Syntax::getSwizzledVariable(const string& srcName, const TypeDesc* srcTyp
continue;
}

auto it = s_channelsMapping.find(ch);
if (it == s_channelsMapping.end())
auto it = CHANNELS_MAPPING.find(ch);
if (it == CHANNELS_MAPPING.end())
{
throw ExceptionShaderGenError("Invalid channel pattern '" + channels + "'.");
}
Expand All @@ -156,14 +160,6 @@ string Syntax::getSwizzledVariable(const string& srcName, const TypeDesc* srcTyp

ValuePtr Syntax::getSwizzledValue(ValuePtr value, const TypeDesc* srcType, const string& channels, const TypeDesc* dstType) const
{
static const std::unordered_map<char, size_t> s_channelsMapping =
{
{ 'r', 0 },{ 'x', 0 },
{ 'g', 1 },{ 'y', 1 },
{ 'b', 2 },{ 'z', 2 },
{ 'a', 3 },{ 'w', 3 }
};

const TypeSyntax& srcSyntax = getTypeSyntax(srcType);
const vector<string>& srcMembers = srcSyntax.getMembers();

Expand All @@ -184,8 +180,8 @@ ValuePtr Syntax::getSwizzledValue(ValuePtr value, const TypeDesc* srcType, const
continue;
}

auto it = s_channelsMapping.find(ch);
if (it == s_channelsMapping.end())
auto it = CHANNELS_MAPPING.find(ch);
if (it == CHANNELS_MAPPING.end())
{
throw ExceptionShaderGenError("Invalid channel pattern '" + channels + "'.");
}
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXRender/Handlers/ImageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ string ImageLoader::TGA_EXTENSION = "tga";
string ImageLoader::TIF_EXTENSION = "tif";
string ImageLoader::TIFF_EXTENSION = "tiff";
string ImageLoader::TXT_EXTENSION = "txt";
string ImageLoader::TXR_EXTENSION = "txr";

ImageHandler::ImageHandler(ImageLoaderPtr imageLoader)
{
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXRender/Handlers/ImageHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class ImageLoader
static string TIF_EXTENSION;
static string TIFF_EXTENSION;
static string TXT_EXTENSION;
static string TXR_EXTENSION;

/// Returns a list of supported extensions
/// @return List of support extensions
Expand Down
9 changes: 8 additions & 1 deletion source/MaterialXRender/Handlers/OiioImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ bool OiioImageLoader::acquireImage(const FilePath& filePath,
return false;
}

const OIIO::ImageSpec& imageSpec = imageInput->spec();
OIIO::ImageSpec imageSpec = imageInput->spec();
if (imageSpec.format == OIIO::TypeDesc::HALF)
{
// Due to display issue with 16-bit tiled exrs,
// treat as 32-bit float.
imageSpec.set_format(OIIO::TypeDesc::FLOAT);
}

if (imageSpec.format != OIIO::TypeDesc::UINT8 &&
imageSpec.format != OIIO::TypeDesc::FLOAT)
{
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXRender/Handlers/OiioImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class OiioImageLoader : public ImageLoader
_extensions.push_back(TIF_EXTENSION);
_extensions.push_back(TIFF_EXTENSION);
_extensions.push_back(TXT_EXTENSION);
_extensions.push_back(TXR_EXTENSION);
}

/// Default destructor
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXView/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ elseif(UNIX AND NOT APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options(-Wno-misleading-indentation)
add_compile_options(-Wno-format-truncation)
add_compile_options(-Wno-implicit-fallthrough)
add_compile_options(-Wno-int-in-bool-context)
add_compile_options(-Wno-maybe-uninitialized) # GCC-8 only
endif()

Expand Down

0 comments on commit 58f6acc

Please sign in to comment.