diff --git a/source/MaterialXRenderGlsl/TextureBaker.cpp b/source/MaterialXRenderGlsl/TextureBaker.cpp index 8cd6155557..149f75ebb9 100644 --- a/source/MaterialXRenderGlsl/TextureBaker.cpp +++ b/source/MaterialXRenderGlsl/TextureBaker.cpp @@ -12,6 +12,9 @@ #include +#include +#include + namespace MaterialX { @@ -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) @@ -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{}(bakedImageName); + bakedImageName = hashStream.str(); + } + return FilePath(bakedImageName + "." + _extension); } void TextureBaker::bakeShaderInputs(NodePtr material, NodePtr shader, GenContext& context, const string& udim) diff --git a/source/MaterialXRenderGlsl/TextureBaker.h b/source/MaterialXRenderGlsl/TextureBaker.h index 45c0227c2a..8e6a0a2501 100644 --- a/source/MaterialXRenderGlsl/TextureBaker.h +++ b/source/MaterialXRenderGlsl/TextureBaker.h @@ -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); @@ -234,6 +249,7 @@ class TextureBaker : public GlslRenderer FileSearchPath _codeSearchPath; std::ostream* _outputStream; bool _autoTextureResolution; + bool _hashImageNames; ShaderGeneratorPtr _generator; ConstNodePtr _material; diff --git a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp index a4921e1e56..f9f4d7200d 100644 --- a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp +++ b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp @@ -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 { diff --git a/source/PyMaterialX/PyMaterialXRenderGlsl/PyTextureBaker.cpp b/source/PyMaterialX/PyMaterialXRenderGlsl/PyTextureBaker.cpp index 6d48abc79d..d54e3a50a1 100644 --- a/source/PyMaterialX/PyMaterialXRenderGlsl/PyTextureBaker.cpp +++ b/source/PyMaterialX/PyMaterialXRenderGlsl/PyTextureBaker.cpp @@ -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)