Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ colorspaces:

- !<ColorSpace>
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: !<GroupTransform>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@
<input name="in3" type="color3" value="0.6533, 0.7210, 0.8070" colorspace="Utility - sRGB - Texture" />
<input name="in4" type="color3" value="0.6190, 0.6825, 0.5495" colorspace="Utility - sRGB - Texture" />
<input name="in5" type="color3" value="0.7494, 0.7366, 0.8511" colorspace="Utility - sRGB - Texture" />
<input name="in6" type="color3" value="0.6681, 0.8762, 0.8360" colorspace="Utility - sRGB - Texture" />
<input name="in7" type="color3" value="0.9257, 0.7315, 0.4523" colorspace="Utility - sRGB - Texture" />
<input name="in8" type="color3" value="0.5959, 0.6317, 0.8272" colorspace="Utility - sRGB - Texture" />
<input name="in9" type="color3" value="0.8844, 0.6286, 0.6563" colorspace="Utility - sRGB - Texture" />
<input name="in10" type="color3" value="0.6411, 0.5223, 0.6825" colorspace="Utility - sRGB - Texture" />
<input name="in6" type="color3" value="0.6681, 0.8762, 0.8360" colorspace="sRGB Encoded Rec.709 (sRGB)" />
<input name="in7" type="color3" value="0.9257, 0.7315, 0.4523" colorspace="sRGB Encoded Rec.709 (sRGB)" />
<input name="in8" type="color3" value="0.5959, 0.6317, 0.8272" colorspace="sRGB Encoded Rec.709 (sRGB)" />
<input name="in9" type="color3" value="0.8844, 0.6286, 0.6563" colorspace="sRGB Encoded Rec.709 (sRGB)" />
<input name="in10" type="color3" value="0.6411, 0.5223, 0.6825" colorspace="sRGB Encoded Rec.709 (sRGB)" />
<input name="which" type="float" nodename="column_select" />
</switch>
<switch name="switch6" type="color3" nodedef="ND_switch_color3">
Expand Down
46 changes: 24 additions & 22 deletions source/MaterialXGenShader/OcioColorManagementSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <OpenColorIO/OpenColorIO.h>
#include <OpenColorIO/OpenColorTypes.h>

#include <algorithm>
#include <exception>
#include <stdexcept>
#include <map>
Expand Down Expand Up @@ -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);
Expand Down
92 changes: 92 additions & 0 deletions source/MaterialXTest/MaterialXGenShader/GenShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <MaterialXGenSlang/SlangShaderGenerator.h>
#endif

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <vector>
Expand Down Expand Up @@ -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<std::string> 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<unsigned char>(c)) || c == '_'; }));
CHECK(!std::isdigit(static_cast<unsigned char>(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
Loading