diff --git a/resources/Materials/TestSuite/stdlib/color_management/minimal_config.ocio b/resources/Materials/TestSuite/stdlib/color_management/minimal_config.ocio index 559ff5a593..add0a0e1af 100644 --- a/resources/Materials/TestSuite/stdlib/color_management/minimal_config.ocio +++ b/resources/Materials/TestSuite/stdlib/color_management/minimal_config.ocio @@ -18,7 +18,7 @@ colorspaces: - ! name: sRGB (Scene-referred) - aliases: [srgb_texture, srgb_rec709_scene] + aliases: [srgb_texture, srgb_rec709_scene, _sRGB__scene, srgb - scene_, srgb___dot., ((srgb/tex))] description: This is in both the cg-config and the DefaultColorManagementSystem, but has a different name. isdata: false from_scene_reference: ! diff --git a/resources/Materials/TestSuite/stdlib/color_management/ocio_color_management.mtlx b/resources/Materials/TestSuite/stdlib/color_management/ocio_color_management.mtlx index 69ce5023d1..63eb77277d 100644 --- a/resources/Materials/TestSuite/stdlib/color_management/ocio_color_management.mtlx +++ b/resources/Materials/TestSuite/stdlib/color_management/ocio_color_management.mtlx @@ -126,11 +126,11 @@ - - - - - + + + + + diff --git a/source/MaterialXGenShader/OcioColorManagementSystem.cpp b/source/MaterialXGenShader/OcioColorManagementSystem.cpp index fd9ea35623..bb8ec2ae74 100644 --- a/source/MaterialXGenShader/OcioColorManagementSystem.cpp +++ b/source/MaterialXGenShader/OcioColorManagementSystem.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -227,34 +228,35 @@ NodeDefPtr OcioColorManagementSystemImpl::getNodeDef(const ColorSpaceTransform& } // Build a function name from the source/target color space names and the first - // six characters of the cache ID. Each name component is sanitized individually: - // invalid characters are replaced with underscores and consecutive underscores are - // collapsed to one so that OCIO's own identifier normalizer cannot produce a - // different name than the one we register on the node def. - auto sanitizeName = [](const string& s) -> string + // six characters of the cache ID. OCIO collapses double underscores in the function + // name it emits, so the name registered on the node def must not contain any, or the + // generated call site would not match the generated function definition. + auto sanitizeName = [](const string& name) -> string { - string result = createValidName(s); - string collapsed; - bool prevUnderscore = false; - for (char c : result) + // Replace invalid characters with underscores, collapse runs of underscores to + // one, and strip leading and trailing underscores. The colon is a valid + // MaterialX name character, so createValidName doesn't replace it, but is not + // legal in a shader identifier, so replace that separately. + string colonFree = name; + std::replace(colonFree.begin(), colonFree.end(), ':', '_'); + string result; + for (char c : createValidName(colonFree)) { - if (c == '_') - { - if (!prevUnderscore) - collapsed += c; - prevUnderscore = true; - } - else - { - collapsed += c; - prevUnderscore = false; - } + if (c == '_' && (result.empty() || result.back() == '_')) + continue; + result += c; } - return collapsed; + if (!result.empty() && result.back() == '_') + result.pop_back(); + return result; }; static const auto NODE_NAME = string{ "ocio_color_conversion" }; const string cacheID = processor->getCacheID(); - const auto functionName = NODE_NAME + "_" + sanitizeName(sourceColorSpace) + "_to_" + sanitizeName(targetColorSpace) + "_" + cacheID.substr(0, 6); + const auto functionName = sanitizeName( NODE_NAME + "_" + + sourceColorSpace + "_to_" + targetColorSpace + // The cacheID prevents the character substitutions from causing two different Processors + // to collapse to the same functionName. + + "_" + cacheID.substr(0, 6) ); const auto implName = OcioColorManagementSystem::IMPL_PREFIX + functionName + "_" + transform.type.getName(); const auto nodeDefName = ND_PREFIX + functionName + "_" + transform.type.getName(); auto nodeDef = document->getNodeDef(nodeDefName); diff --git a/source/MaterialXTest/MaterialXGenShader/GenShader.cpp b/source/MaterialXTest/MaterialXGenShader/GenShader.cpp index 3518244f32..d224450d64 100644 --- a/source/MaterialXTest/MaterialXGenShader/GenShader.cpp +++ b/source/MaterialXTest/MaterialXGenShader/GenShader.cpp @@ -37,6 +37,8 @@ #include #endif +#include +#include #include #include #include @@ -807,3 +809,93 @@ TEST_CASE("GenShader: User-Facing Color Space Names", "[genshader]") } #endif } + +#ifdef MATERIALX_BUILD_OCIO +TEST_CASE("GenShader: OCIO Function Names", "[genshader]") +{ + // Color space names from the config can contain characters that are not legal in + // shader identifiers, or that begin or end with underscores. The function name + // generated for the transform must be a legal identifier without double underscores + // (which OCIO would collapse in the function definition, leaving the call site + // referring to a function that does not exist). + const std::vector colorSpaceNames = + { + "sRGB AP1", + "sRGB (Scene-referred)", + "_sRGB__scene", + "srgb - scene_", + "srgb___dot.", + "((srgb/tex))", + "ocio:arrilogc4_awg4_scene" + }; + const std::string referenceSpace = "ACES2065-1"; + + try + { + mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); + mx::FilePath minimalConfigFile = searchPath.find( + "resources/Materials/TestSuite/stdlib/color_management/minimal_config.ocio"); + REQUIRE(minimalConfigFile.exists()); + + mx::OcioColorManagementSystemPtr cms = + mx::OcioColorManagementSystem::createFromFile(minimalConfigFile.asString(), "genglsl"); + + mx::DocumentPtr document = mx::createDocument(); + mx::loadLibraries({ "libraries" }, searchPath, document); + cms->loadLibrary(document); + + // Request each color space as both the source and the target of a transform. + for (const std::string& name : colorSpaceNames) + { + INFO(name); + CHECK(cms->supportsTransform(mx::ColorSpaceTransform(name, referenceSpace, mx::Type::COLOR3))); + CHECK(cms->supportsTransform(mx::ColorSpaceTransform(referenceSpace, name, mx::Type::COLOR3))); + } + + // Examine every node def that the OCIO color management system created. + size_t ocioNodeDefCount = 0; + for (mx::NodeDefPtr nodeDef : document->getNodeDefs()) + { + if (nodeDef->getSourceUri() != mx::OcioColorManagementSystem::OCIO_SOURCE_URI) + { + continue; + } + ocioNodeDefCount++; + + const std::string& nodeDefName = nodeDef->getName(); + const std::string& functionName = nodeDef->getNodeString(); + INFO(nodeDefName); + + // Names must be legal MaterialX names and legal shader identifiers, and + // must not contain double underscores. + CHECK(mx::isValidName(nodeDefName)); + CHECK(nodeDefName.find("__") == std::string::npos); + CHECK(!functionName.empty()); + CHECK(std::all_of(functionName.begin(), functionName.end(), + [](char c) { return std::isalnum(static_cast(c)) || c == '_'; })); + CHECK(!std::isdigit(static_cast(functionName[0]))); + CHECK(functionName.find("__") == std::string::npos); + + // The function that OCIO generates must be defined using exactly the name that + // the generated shader will call. + bool foundImplementation = false; + for (mx::ImplementationPtr impl : document->getImplementations()) + { + if (impl->getNodeDefString() == nodeDefName) + { + foundImplementation = true; + CHECK(cms->hasImplementation(impl->getName())); + const std::string code = cms->getGpuProcessorCode(impl->getName(), functionName); + CHECK(code.find(" " + functionName + "(") != std::string::npos); + } + } + CHECK(foundImplementation); + } + CHECK(ocioNodeDefCount > 0); + } + catch (const std::exception& e) + { + WARN(std::string("Could not create OcioColorManagementSystem from minimal_config.ocio: ") + e.what()); + } +} +#endif