Skip to content

Commit

Permalink
Add option to has image filenames. (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardkwok authored Feb 18, 2021
1 parent 1018ba4 commit 715b581
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions source/MaterialXRenderGlsl/TextureBaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#include <MaterialXGenShader/DefaultColorManagementSystem.h>

#include <unordered_map>
#include <sstream>

namespace MaterialX
{

Expand Down Expand Up @@ -66,6 +69,7 @@ TextureBaker::TextureBaker(unsigned int width, unsigned int height, Image::BaseT
_bakedGeomInfoName("GI_baked"),
_outputStream(&std::cout),
_autoTextureResolution(false),
_hashImageNames(false),
_generator(GlslShaderGenerator::create())
{
if (baseType == Image::BaseType::UINT8)
Expand Down Expand Up @@ -94,8 +98,15 @@ FilePath TextureBaker::generateTextureFilename(OutputPtr output, const string& s
string outputName = createValidName(output->getNamePath());
string shaderSuffix = shaderName.empty() ? EMPTY_STRING : "_" + shaderName;
string udimSuffix = udim.empty() ? EMPTY_STRING : "_" + udim;

return FilePath(outputName + shaderSuffix + BAKED_POSTFIX + udimSuffix + "." + _extension);
std::string bakedImageName;
bakedImageName = outputName + shaderSuffix + BAKED_POSTFIX + udimSuffix;
if (_hashImageNames)
{
std::stringstream hashStream;
hashStream << std::hash<std::string>{}(bakedImageName);
bakedImageName = hashStream.str();
}
return FilePath(bakedImageName + "." + _extension);
}

void TextureBaker::bakeShaderInputs(NodePtr material, NodePtr shader, GenContext& context, const string& udim)
Expand Down
16 changes: 16 additions & 0 deletions source/MaterialXRenderGlsl/TextureBaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ class TextureBaker : public GlslRenderer
return _autoTextureResolution;
}

/// Set whether to create a short name for baked images by hashing the baked image filenames
/// This is useful for file systems which may have a maximum limit on filename size.
/// By default names are not hashed.
void setHashImageNames(bool enable)
{
_hashImageNames = enable;
}

/// Return whether automatic baked texture resolution is set.
bool getHashImageNames() const
{
return _hashImageNames;
}


/// Set up the unit definitions to be used in baking.
void setupUnitSystem(DocumentPtr unitDefinitions);

Expand Down Expand Up @@ -234,6 +249,7 @@ class TextureBaker : public GlslRenderer
FileSearchPath _codeSearchPath;
std::ostream* _outputStream;
bool _autoTextureResolution;
bool _hashImageNames;

ShaderGeneratorPtr _generator;
ConstNodePtr _material;
Expand Down
2 changes: 2 additions & 0 deletions source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ void GlslShaderRenderTester::runBake(mx::DocumentPtr doc, const mx::FileSearchPa
baker->setupUnitSystem(doc);
baker->setImageHandler(_renderer->getImageHandler());
baker->setOptimizeConstants(true);
baker->setAutoTextureResolution(true);
baker->setHashImageNames(true);

try
{
Expand Down
2 changes: 2 additions & 0 deletions source/PyMaterialX/PyMaterialXRenderGlsl/PyTextureBaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void bindPyTextureBaker(py::module& mod)
.def("getBakedGeomInfoName", &mx::TextureBaker::getBakedGeomInfoName)
.def("setAutoTextureResolution", &mx::TextureBaker::setAutoTextureResolution)
.def("getAutoTextureResolution", &mx::TextureBaker::getAutoTextureResolution)
.def("setHashImageNames", &mx::TextureBaker::setHashImageNames)
.def("getHashImageNames", &mx::TextureBaker::getHashImageNames)
.def("setupUnitSystem", &mx::TextureBaker::setupUnitSystem)
.def("bakeMaterial", &mx::TextureBaker::bakeMaterial)
.def("createBakeDocuments", &mx::TextureBaker::createBakeDocuments)
Expand Down

0 comments on commit 715b581

Please sign in to comment.