-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathOcioColorManagementSystem.cpp
More file actions
365 lines (302 loc) · 11.1 KB
/
Copy pathOcioColorManagementSystem.cpp
File metadata and controls
365 lines (302 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#ifdef MATERIALX_BUILD_OCIO
#include "MaterialXCore/Library.h"
#include <MaterialXGenShader/OcioColorManagementSystem.h>
#include <MaterialXCore/Definition.h>
#include <MaterialXGenShader/ColorManagementSystem.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/Nodes/OcioNode.h>
#include <OpenColorIO/OpenColorABI.h>
#include <OpenColorIO/OpenColorIO.h>
#include <OpenColorIO/OpenColorTypes.h>
#include <exception>
#include <stdexcept>
#include <map>
namespace OCIO = OCIO_NAMESPACE;
MATERIALX_NAMESPACE_BEGIN
const string OcioColorManagementSystem::IMPL_PREFIX = "IMPL_MXOCIO_";
const string OcioColorManagementSystem::OCIO_SOURCE_URI = "materialx://OcioColorManagementSystem.cpp";
const string ND_PREFIX = "ND_MXOCIO_";
namespace
{
const string CMS_NAME = "OpenColorIO";
// Remap from legacy color space names to their ACES 1.3 equivalents.
const std::map<string, string> COLOR_SPACE_REMAP =
{
{ "gamma18", "Gamma 1.8 Rec.709 - Texture" },
{ "gamma22", "Gamma 2.2 Rec.709 - Texture" },
{ "gamma24", "Gamma 2.4 Rec.709 - Texture" },
// TODO: Add support for adobergb and lin_adobergb
};
} // anonymous namespace
//
// OcioColorManagementSystemImpl class and methods
//
class OcioColorManagementSystemImpl
{
public:
OcioColorManagementSystemImpl(OCIO::ConstConfigRcPtr config, string target) :
_config(std::move(config)), _target(std::move(target)) { }
const char* getSupportedColorSpaceName(const char* colorSpace) const;
NodeDefPtr getNodeDef(const ColorSpaceTransform& transform, const DocumentPtr& document) const;
bool hasImplementation(const string& implName) const
{
return _implementations.count(implName);
}
ShaderNodeImplPtr createImplementation(const string& implName) const
{
if (_implementations.count(implName))
{
return OcioNode::create();
}
return {};
}
string getGpuProcessorCode(const string& implName, const string& functionName) const;
const string& target() const { return _target; }
private:
OCIO::ConstConfigRcPtr _config;
string _target;
mutable std::map<string, OCIO::ConstGPUProcessorRcPtr> _implementations;
};
const char* OcioColorManagementSystemImpl::getSupportedColorSpaceName(const char* colorSpace) const
{
if (_config->getColorSpace(colorSpace))
{
return colorSpace;
}
auto remap = COLOR_SPACE_REMAP.find(colorSpace);
if (remap != COLOR_SPACE_REMAP.end())
{
return getSupportedColorSpaceName(remap->second.c_str());
}
auto cgConfig = OCIO::Config::CreateFromBuiltinConfig("ocio://studio-config-latest");
try
{
// Throws on failure:
return OCIO::Config::IdentifyBuiltinColorSpace(_config, cgConfig, colorSpace);
}
catch (const std::exception& /*e*/)
{
return nullptr;
}
}
NodeDefPtr OcioColorManagementSystemImpl::getNodeDef(const ColorSpaceTransform& transform, const DocumentPtr& document) const
{
OCIO::ConstProcessorRcPtr processor;
// Check if directly supported in the config:
const char* sourceColorSpace = getSupportedColorSpaceName(transform.sourceSpace.c_str());
const char* targetColorSpace = getSupportedColorSpaceName(transform.targetSpace.c_str());
if (!sourceColorSpace || !targetColorSpace)
{
return {};
}
try
{
processor = _config->getProcessor(sourceColorSpace, targetColorSpace);
}
catch (const std::exception& /*e*/)
{
return {};
}
if (!processor)
{
return {};
}
auto gpuProcessor = processor->getDefaultGPUProcessor();
if (!gpuProcessor)
{
return {};
}
if (gpuProcessor->isNoOp())
{
return document->getNodeDef("ND_dot_" + transform.type.getName());
}
// Reject transforms requiring textures (1D and 3D LUTs)
OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc();
gpuProcessor->extractGpuShaderInfo(shaderDesc);
if (shaderDesc->getNum3DTextures() || shaderDesc->getNumTextures())
{
// TODO: Support LUTs
return {};
}
// 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
{
string result = createValidName(s);
string collapsed;
bool prevUnderscore = false;
for (char c : result)
{
if (c == '_')
{
if (!prevUnderscore)
collapsed += c;
prevUnderscore = true;
}
else
{
collapsed += c;
prevUnderscore = false;
}
}
return collapsed;
};
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 implName = OcioColorManagementSystem::IMPL_PREFIX + functionName + "_" + transform.type.getName();
const auto nodeDefName = ND_PREFIX + functionName + "_" + transform.type.getName();
auto nodeDef = document->getNodeDef(nodeDefName);
if (!nodeDef)
{
nodeDef = document->addNodeDef(nodeDefName, "", functionName);
nodeDef->setNodeGroup("colortransform");
nodeDef->setSourceUri(OcioColorManagementSystem::OCIO_SOURCE_URI);
nodeDef->addInput("in", transform.type.getName());
nodeDef->addOutput("out", transform.type.getName());
auto implementation = document->addImplementation(implName);
implementation->setTarget(_target);
implementation->setNodeDef(nodeDef);
implementation->setSourceUri(OcioColorManagementSystem::OCIO_SOURCE_URI);
}
_implementations.emplace(implName, gpuProcessor);
return nodeDef;
}
string OcioColorManagementSystemImpl::getGpuProcessorCode(const string& implName, const string& functionName) const
{
auto it = _implementations.find(implName);
if (it == _implementations.end())
{
return {};
}
auto gpuProcessor = it->second;
OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc();
// TODO: Extend to essl and MDL and possibly SLang.
bool isOSL = false;
if (_target == "genglsl")
{
shaderDesc->setLanguage(OCIO::GPU_LANGUAGE_GLSL_4_0);
}
else if (_target == "genmsl")
{
shaderDesc->setLanguage(OCIO::GPU_LANGUAGE_MSL_2_0);
}
else if (_target == "genosl")
{
shaderDesc->setLanguage(OCIO::LANGUAGE_OSL_1);
isOSL = true;
}
shaderDesc->setFunctionName(functionName.c_str());
gpuProcessor->extractGpuShaderInfo(shaderDesc);
string shaderText = shaderDesc->getShaderText();
// For OSL, we need to extract the function from the shader OCIO creates.
if (isOSL)
{
auto startpos = shaderText.find(string{ "color4 " } + shaderDesc->getFunctionName());
if (startpos != string::npos)
{
auto endpos = shaderText.find(string{ "outColor = " } + shaderDesc->getFunctionName(), startpos);
if (endpos != string::npos)
{
shaderText = shaderText.substr(startpos, endpos - startpos);
}
}
#if OCIO_VERSION_HEX <= 0x02040100
// Need to transpose the matrix if we have an OCIO < 2.4.2 see:
// https://github.com/AcademySoftwareFoundation/OpenColorIO/pull/2121
startpos = shaderText.find(string{ "matrix(" });
if (startpos != string::npos)
{
auto endpos = shaderText.find(string{ ")" }, startpos);
if (endpos != string::npos)
{
shaderText.insert(endpos, ")");
shaderText.insert(startpos, "transpose(");
}
}
#endif
}
return shaderText;
}
//
// OcioColorManagementSystem methods
//
OcioColorManagementSystemPtr OcioColorManagementSystem::createFromEnv(string target)
{
if (target != "genglsl" && target != "genmsl" && target != "genosl")
{
throw std::runtime_error("OCIO does not support this target");
}
auto config = OCIO::Config::CreateFromEnv();
return OcioColorManagementSystemPtr(new OcioColorManagementSystem(new OcioColorManagementSystemImpl(config, target)));
}
OcioColorManagementSystemPtr OcioColorManagementSystem::createFromFile(const string& filename, string target)
{
if (target != "genglsl" && target != "genmsl" && target != "genosl")
{
throw std::runtime_error("OCIO does not support this target");
}
auto config = OCIO::Config::CreateFromFile(filename.c_str());
return OcioColorManagementSystemPtr(new OcioColorManagementSystem(new OcioColorManagementSystemImpl(config, target)));
}
OcioColorManagementSystemPtr OcioColorManagementSystem::createFromBuiltinConfig(const string& configName, string target)
{
if (target != "genglsl" && target != "genmsl" && target != "genosl")
{
throw std::runtime_error("OCIO does not support this target");
}
auto config = OCIO::Config::CreateFromBuiltinConfig(configName.c_str());
return OcioColorManagementSystemPtr(new OcioColorManagementSystem(new OcioColorManagementSystemImpl(config, target)));
}
OcioColorManagementSystem::OcioColorManagementSystem(OcioColorManagementSystemImpl* impl) :
DefaultColorManagementSystem(impl->target()),
_impl(impl)
{
}
OcioColorManagementSystem::~OcioColorManagementSystem() = default;
const string& OcioColorManagementSystem::getName() const
{
return CMS_NAME;
}
const char* OcioColorManagementSystem::getSupportedColorSpaceName(const char* colorSpace) const
{
return _impl->getSupportedColorSpaceName(colorSpace);
}
NodeDefPtr OcioColorManagementSystem::getNodeDef(const ColorSpaceTransform& transform) const
{
// See if the default color management system already handles this:
if (auto cmNodeDef = DefaultColorManagementSystem::getNodeDef(transform))
{
return cmNodeDef;
}
return _impl->getNodeDef(transform, _document);
}
bool OcioColorManagementSystem::hasImplementation(const string& implName) const
{
if (DefaultColorManagementSystem::hasImplementation(implName))
{
return true;
}
return _impl->hasImplementation(implName);
}
ShaderNodeImplPtr OcioColorManagementSystem::createImplementation(const string& implName) const
{
if (auto impl = DefaultColorManagementSystem::createImplementation(implName))
{
return impl;
}
return _impl->createImplementation(implName);
}
string OcioColorManagementSystem::getGpuProcessorCode(const string& implName, const string& functionName) const
{
return _impl->getGpuProcessorCode(implName, functionName);
}
MATERIALX_NAMESPACE_END
#endif // MATERIALX_BUILD_OCIO