Skip to content

Commit

Permalink
Cleanup build setup for MaterialXViewer module and pull out utilities (
Browse files Browse the repository at this point in the history
…AcademySoftwareFoundation#342)

* Build fixes
- Make Viewer conditionally require render builds
- Remove duplicate installs of resources and libraries by fixing up search path logic to allow for current path as well as parent path.
Current install puts the resources/ and libraries/ under the same directory as bin/.
- Add in pop() method on FilePath to allow to get parent.

* Create Util file and pull out some Material class logic into it.

* Movement of UI / introspection utilities the render utils.

* - Fix up extension storage and usage for image file dialog so it's not fixed.
- Fix so that we use v-flip for textures so texture are not flipped.

* Review fixes. Add in VariantBlock search function based on generic predicate function.

* Review fixes.
  • Loading branch information
bernardkwok committed Apr 1, 2019
1 parent 14fa9b4 commit 050db41
Show file tree
Hide file tree
Showing 25 changed files with 456 additions and 292 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ set(MATERIALX_OIIO_DIR "" CACHE PATH "Path to the root folder of the OpenImageIO
mark_as_advanced(MATERIALX_OIIO_DIR)
option(MATERIALX_BUILD_CONTRIB "Build contribution folder." OFF)
mark_as_advanced(MATERIALX_BUILD_CONTRIB)

# Viewer building option. If viewer is desired then we require
# render and GLSL modules.
option(MATERIALX_BUILD_VIEWER "Build the MaterialX Sample Viewer. " ON)
if (MATERIALX_BUILD_VIEWER)
set( MATERIALX_BUILD_RENDER ON)
set (MATERIALX_BUILD_RENDERGLSL ON)
endif()

if (NOT MATERIALX_BUILD_RENDER)
set (MATERIALX_BUILD_RENDEROSL OFF)
set (MATERIALX_BUILD_RENDERGLSL OFF)
Expand All @@ -78,9 +87,6 @@ if (MATERIALX_BUILD_RENDEROSL)
add_definitions(-DMATERIALX_OSL_INCLUDE_PATH=\"${MATERIALX_OSL_INCLUDE_PATH}\")
endif()

# Viewer options
option(MATERIALX_BUILD_VIEWER "Build the MaterialX Viewer. " ON)

# Adjust the default installation path
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/installed" CACHE PATH "Default install path" FORCE)
Expand Down
8 changes: 8 additions & 0 deletions source/MaterialXFormat/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ FilePath FilePath::operator/(const FilePath& rhs) const
return combined;
}

void FilePath::pop()
{
if (!isEmpty())
{
_vec.pop_back();
}
}

bool FilePath::exists() const
{
#if defined(_WIN32)
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXFormat/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class FilePath
/// combined path.
FilePath operator/(const FilePath& rhs) const;

/// Set the path to the parent directory if one exists.
void pop();

/// @}
/// @name File System Operations
/// @{
Expand Down
12 changes: 12 additions & 0 deletions source/MaterialXGenShader/ShaderStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ const ShaderPort* VariableBlock::find(const string& name) const
return const_cast<VariableBlock*>(this)->find(name);
}

ShaderPort* VariableBlock::find(const ShaderPortPredicate& predicate)
{
for (ShaderPort* port : getVariableOrder())
{
if (predicate(port))
{
return port;
}
}
return nullptr;
}

ShaderPort* VariableBlock::add(const TypeDesc* type, const string& name, ValuePtr value)
{
auto it = _variableMap.find(name);
Expand Down
6 changes: 5 additions & 1 deletion source/MaterialXGenShader/ShaderStage.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class VariableBlock;
using VariableBlockPtr = std::shared_ptr<VariableBlock>;
/// Shared pointer to a map between string identifiers and VariableBlocks
using VariableBlockMap = std::unordered_map<string, VariableBlockPtr>;

/// A standard function predicate taking an ShaderPort pointer and returning a boolean.
using ShaderPortPredicate = std::function<bool(ShaderPort*)>;

/// @class VariableBlock
/// A block of variables in a shader stage
Expand Down Expand Up @@ -98,6 +99,9 @@ class VariableBlock
/// no variable is found by the given name.
const ShaderPort* find(const string& name) const;

/// Find a port based on a predicate
ShaderPort* find(const ShaderPortPredicate& predicate);

/// Add a new shader port to this block.
ShaderPort* add(const TypeDesc* type, const string& name, ValuePtr value = nullptr);

Expand Down
97 changes: 0 additions & 97 deletions source/MaterialXGenShader/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,101 +699,4 @@ ValueElementPtr findNodeDefChild(const string& path, DocumentPtr doc, const stri
return valueElement;
}

unsigned int getUIProperties(const ValueElementPtr nodeDefElement, UIProperties& uiProperties)
{
if (!nodeDefElement)
{
return 0;
}

unsigned int propertyCount = 0;
uiProperties.uiName = nodeDefElement->getAttribute(ValueElement::UI_NAME_ATTRIBUTE);
if (!uiProperties.uiName.empty())
propertyCount++;

uiProperties.uiFolder = nodeDefElement->getAttribute(ValueElement::UI_FOLDER_ATTRIBUTE);
if (!uiProperties.uiFolder.empty())
propertyCount++;

if (nodeDefElement->isA<Parameter>())
{
string enumString = nodeDefElement->getAttribute(ValueElement::ENUM_ATTRIBUTE);
if (!enumString.empty())
{
uiProperties.enumeration = splitString(enumString, ",");
if (uiProperties.enumeration.size())
propertyCount++;
}

const string& enumerationValues = nodeDefElement->getAttribute(ValueElement::ENUM_VALUES_ATTRIBUTE);
if (!enumerationValues.empty())
{
const string& elemType = nodeDefElement->getType();
const TypeDesc* typeDesc = TypeDesc::get(elemType);
if (typeDesc->isScalar() || typeDesc->isFloat2() || typeDesc->isFloat3() ||
typeDesc->isFloat4())
{
StringVec stringValues = splitString(enumerationValues, ",");
string valueString;
size_t elementCount = typeDesc->getSize();
elementCount--;
size_t count = 0;
for (size_t i = 0; i < stringValues.size(); i++)
{
if (count == elementCount)
{
valueString += stringValues[i];
uiProperties.enumerationValues.push_back(Value::createValueFromStrings(valueString, elemType));
valueString.clear();
count = 0;
}
else
{
valueString += stringValues[i] + ",";
count++;
}
}
}
else
{
uiProperties.enumerationValues.push_back(Value::createValue(enumerationValues));
}
propertyCount++;
}
}

const string& uiMinString = nodeDefElement->getAttribute(ValueElement::UI_MIN_ATTRIBUTE);
if (!uiMinString.empty())
{
ValuePtr value = Value::createValueFromStrings(uiMinString, nodeDefElement->getType());
if (value)
{
uiProperties.uiMin = value;
propertyCount++;
}
}

const string& uiMaxString = nodeDefElement->getAttribute(ValueElement::UI_MAX_ATTRIBUTE);
if (!uiMaxString.empty())
{
ValuePtr value = Value::createValueFromStrings(uiMaxString, nodeDefElement->getType());
if (value)
{
uiProperties.uiMax = value;
propertyCount++;
}
}
return propertyCount;
}

unsigned int getUIProperties(const string& path, DocumentPtr doc, const string& target, UIProperties& uiProperties)
{
ValueElementPtr valueElement = findNodeDefChild(path, doc, target);
if (valueElement)
{
return getUIProperties(valueElement, uiProperties);
}
return 0;
}

} // namespace MaterialX
31 changes: 0 additions & 31 deletions source/MaterialXGenShader/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,6 @@ void findRenderableElements(const DocumentPtr& doc, std::vector<TypedElementPtr>
/// if the path is to a Node as definitions for Nodes can be target specific.
ValueElementPtr findNodeDefChild(const string& path, DocumentPtr doc, const string& target);

/// Set of possible UI properties for an element
struct UIProperties
{
/// UI name
string uiName;

/// UI folder
string uiFolder;

/// Enumeration
StringVec enumeration;

/// Enumeration Values
vector<ValuePtr> enumerationValues;

/// UI minimum value
ValuePtr uiMin;

/// UI maximum value
ValuePtr uiMax;
};

/// Get the UI properties for a given nodedef element.
/// Returns the number of properties found.
unsigned int getUIProperties(const ValueElementPtr nodeDefElement, UIProperties& uiProperties);

/// Get the UI properties for a given element path. If the path is to a node, a target
/// identifier can be provided.
/// Returns the number of properties found.
unsigned int getUIProperties(const string& path, DocumentPtr doc, const string& target, UIProperties& uiProperties);

} // namespace MaterialX

#endif
2 changes: 1 addition & 1 deletion source/MaterialXRender/Handlers/GeometryHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MaterialX
{
void GeometryHandler::addLoader(GeometryLoaderPtr loader)
{
const StringVec& extensions = loader->supportedExtensions();
const StringSet& extensions = loader->supportedExtensions();
for (auto extension : extensions)
{
_geometryLoaders.insert(std::pair<std::string, GeometryLoaderPtr>(extension, loader));
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXRender/Handlers/GeometryHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GeometryLoader

/// Returns a list of supported extensions
/// @return List of support extensions
const StringVec& supportedExtensions()
const StringSet& supportedExtensions()
{
return _extensions;
}
Expand All @@ -47,7 +47,7 @@ class GeometryLoader

protected:
/// List of supported string extensions
StringVec _extensions;
StringSet _extensions;
};

/// Shared pointer to an GeometryHandler
Expand Down
12 changes: 11 additions & 1 deletion source/MaterialXRender/Handlers/ImageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@ void ImageHandler::addLoader(ImageLoaderPtr loader)
{
if (loader)
{
const StringVec& extensions = loader->supportedExtensions();
const StringSet& extensions = loader->supportedExtensions();
for (auto extension : extensions)
{
_imageLoaders.insert(std::pair<string, ImageLoaderPtr>(extension, loader));
}
}
}

void ImageHandler::supportedExtensions(StringSet& extensions)
{
extensions.clear();
for (auto loader : _imageLoaders)
{
const StringSet& loaderExtensions = loader.second->supportedExtensions();
extensions.insert(loaderExtensions.begin(), loaderExtensions.end());
}
}

bool ImageHandler::saveImage(const FilePath& filePath,
const ImageDesc &imageDesc)
{
Expand Down
7 changes: 5 additions & 2 deletions source/MaterialXRender/Handlers/ImageHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ImageLoader

/// Returns a list of supported extensions
/// @return List of support extensions
const StringVec& supportedExtensions()
const StringSet& supportedExtensions()
{
return _extensions;
}
Expand All @@ -118,7 +118,7 @@ class ImageLoader

protected:
/// List of supported string extensions
StringVec _extensions;
StringSet _extensions;
};

/// Shared pointer to an ImageHandler
Expand Down Expand Up @@ -153,6 +153,9 @@ class ImageHandler
/// Default destructor
virtual ~ImageHandler() {}

/// Get a list of extensions supported by the handler
void supportedExtensions(StringSet& extensions);

/// Save image to disk. This method must be implemented by derived classes.
/// The first image loader which supports the file name extension will be used.
/// @param filePath Name of file to save image to
Expand Down
28 changes: 14 additions & 14 deletions source/MaterialXRender/Handlers/OiioImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class OiioImageLoader : public ImageLoader
/// Default constructor. Set all extensions supported by stb
OiioImageLoader()
{
_extensions.push_back(BMP_EXTENSION);
_extensions.push_back(GIF_EXTENSION);
_extensions.push_back(HDR_EXTENSION);
_extensions.push_back(JPG_EXTENSION);
_extensions.push_back(JPEG_EXTENSION);
_extensions.push_back(PIC_EXTENSION);
_extensions.push_back(PNG_EXTENSION);
_extensions.push_back(PSD_EXTENSION);
_extensions.push_back(TGA_EXTENSION);
_extensions.push_back(EXR_EXTENSION);
_extensions.push_back(TIF_EXTENSION);
_extensions.push_back(TIFF_EXTENSION);
_extensions.push_back(TXT_EXTENSION);
_extensions.push_back(TXR_EXTENSION);
_extensions.insert(BMP_EXTENSION);
_extensions.insert(GIF_EXTENSION);
_extensions.insert(HDR_EXTENSION);
_extensions.insert(JPG_EXTENSION);
_extensions.insert(JPEG_EXTENSION);
_extensions.insert(PIC_EXTENSION);
_extensions.insert(PNG_EXTENSION);
_extensions.insert(PSD_EXTENSION);
_extensions.insert(TGA_EXTENSION);
_extensions.insert(EXR_EXTENSION);
_extensions.insert(TIF_EXTENSION);
_extensions.insert(TIFF_EXTENSION);
_extensions.insert(TXT_EXTENSION);
_extensions.insert(TXR_EXTENSION);
}

/// Default destructor
Expand Down
18 changes: 9 additions & 9 deletions source/MaterialXRender/Handlers/StbImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class StbImageLoader : public ImageLoader
/// Default constructor. Set all extensions supported by stb
StbImageLoader()
{
_extensions.push_back(BMP_EXTENSION);
_extensions.push_back(GIF_EXTENSION);
_extensions.push_back(HDR_EXTENSION);
_extensions.push_back(JPG_EXTENSION);
_extensions.push_back(JPEG_EXTENSION);
_extensions.push_back(PIC_EXTENSION);
_extensions.push_back(PNG_EXTENSION);
_extensions.push_back(PSD_EXTENSION);
_extensions.push_back(TGA_EXTENSION);
_extensions.insert(BMP_EXTENSION);
_extensions.insert(GIF_EXTENSION);
_extensions.insert(HDR_EXTENSION);
_extensions.insert(JPG_EXTENSION);
_extensions.insert(JPEG_EXTENSION);
_extensions.insert(PIC_EXTENSION);
_extensions.insert(PNG_EXTENSION);
_extensions.insert(PSD_EXTENSION);
_extensions.insert(TGA_EXTENSION);
}

/// Default destructor
Expand Down
Loading

0 comments on commit 050db41

Please sign in to comment.