HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TextureBaker.inl
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
8 #include <MaterialXRender/Util.h>
9 
11 
12 #include <MaterialXFormat/XmlIo.h>
13 
15 
16 namespace
17 {
18 
19 const string SRGB_TEXTURE = "srgb_texture";
20 const string LIN_REC709 = "lin_rec709";
21 const string SHADER_PREFIX = "SR_";
22 const string DEFAULT_UDIM_PREFIX = "_";
23 
24 } // anonymous namespace
25 
26 template <typename Renderer, typename ShaderGen>
28 {
29  if (type == "color4" || type == "vector4")
30  {
31  return toValueString(color);
32  }
33  if (type == "color3" || type == "vector3")
34  {
35  return toValueString(Vector3(color[0], color[1], color[2]));
36  }
37  if (type == "vector2")
38  {
39  return toValueString(Vector2(color[0], color[1]));
40  }
41  if (type == "float")
42  {
43  return toValueString(color[0]);
44  }
45  return EMPTY_STRING;
46 }
47 
48 template <typename Renderer, typename ShaderGen>
49 TextureBaker<Renderer, ShaderGen>::TextureBaker(unsigned int width, unsigned int height, Image::BaseType baseType, bool flipSavedImage) :
50  Renderer(width, height, baseType),
51  _distanceUnit("meter"),
52  _averageImages(false),
53  _optimizeConstants(true),
54  _bakedGraphName("NG_baked"),
55  _bakedGeomInfoName("GI_baked"),
56  _textureFilenameTemplate("$MATERIAL_$SHADINGMODEL_$INPUT$UDIMPREFIX$UDIM.$EXTENSION"),
57  _outputStream(&std::cout),
58  _hashImageNames(false),
59  _textureSpaceMin(0.0f),
60  _textureSpaceMax(1.0f),
61  _generator(ShaderGen::create()),
62  _permittedOverrides({ "$ASSET", "$MATERIAL", "$UDIMPREFIX" }),
63  _flipSavedImage(flipSavedImage),
66 {
67  if (baseType == Image::BaseType::UINT8)
68  {
69 #if MATERIALX_BUILD_OIIO
70  _extension = ImageLoader::TIFF_EXTENSION;
71 #else
72  _extension = ImageLoader::PNG_EXTENSION;
73 #endif
74  _colorSpace = SRGB_TEXTURE;
75  }
76  else
77  {
78 #if MATERIALX_BUILD_OIIO
79  _extension = ImageLoader::EXR_EXTENSION;
80 #else
81  _extension = ImageLoader::HDR_EXTENSION;
82 #endif
83  _colorSpace = LIN_REC709;
84  }
85 
86  // Initialize our base renderer.
88 
89  // Initialize our image handler.
90  Renderer::_imageHandler = Renderer::createImageHandler(StbImageLoader::create());
91 #if MATERIALX_BUILD_OIIO
92  Renderer::_imageHandler->addLoader(OiioImageLoader::create());
93 #endif
94 
95  // Create our dedicated frame capture image.
96  _frameCaptureImage = Image::create(width, height, 4, baseType);
97  _frameCaptureImage->createResourceBuffer();
98 }
99 
100 template <typename Renderer, typename ShaderGen>
101 size_t TextureBaker<Renderer, ShaderGen>::findVarInTemplate(const string& filename, const string& var, size_t start)
102 {
103  size_t i = filename.find(var, start);
104  if (var == "$UDIM" && i != string::npos)
105  {
106  size_t udimPrefix = filename.find("$UDIMPREFIX", start);
107  if (i == udimPrefix)
108  {
109  i = filename.find(var, i + 1);
110  }
111  }
112  return i;
113 }
114 
115 template <typename Renderer, typename ShaderGen>
117 {
118  string bakedImageName = _textureFilenameTemplate;
119 
120  for (auto& pair : filenameTemplateMap)
121  {
122  string replacement = (_texTemplateOverrides.count(pair.first)) ? _texTemplateOverrides[pair.first] : pair.second;
123  replacement = (filenameTemplateMap.at("$UDIM").empty() && pair.first == "$UDIMPREFIX") ? EMPTY_STRING : replacement;
124 
125  for (size_t i = 0; (i = findVarInTemplate(bakedImageName, pair.first, i)) != string::npos; i++)
126  {
127  bakedImageName.replace(i, pair.first.length(), replacement);
128  }
129  }
130 
131  if (_hashImageNames)
132  {
133  std::stringstream hashStream;
134  hashStream << std::hash<std::string>{}(bakedImageName);
135  hashStream << "." + getExtension();
136  bakedImageName = hashStream.str();
137  }
138  return _outputImagePath / bakedImageName;
139 }
140 
141 template <typename Renderer, typename ShaderGen>
143 {
144  FilePath assetPath = FilePath(shader->getActiveSourceUri());
145  assetPath.removeExtension();
146  StringMap filenameTemplateMap;
147  filenameTemplateMap["$ASSET"] = assetPath.getBaseName();
148  filenameTemplateMap["$INPUT"] = _bakedInputMap[input->getName()];
149  filenameTemplateMap["$EXTENSION"] = _extension;
150  filenameTemplateMap["$MATERIAL"] = _material->getName();
151  filenameTemplateMap["$SHADINGMODEL"] = shader->getCategory();
152  filenameTemplateMap["$UDIM"] = udim;
153  filenameTemplateMap["$UDIMPREFIX"] = DEFAULT_UDIM_PREFIX;
154  return filenameTemplateMap;
155 }
156 
157 template <typename Renderer, typename ShaderGen>
159 {
160  if (!Renderer::_imageHandler->saveImage(baked.filename, image, _flipSavedImage))
161  {
162  if (_outputStream)
163  {
164  *_outputStream << "Failed to write baked image: " << baked.filename.asString() << std::endl;
165  }
166  return false;
167  }
168 
169  if (_outputStream)
170  {
171  *_outputStream << "Wrote baked image: " << baked.filename.asString() << std::endl;
172  }
173 
174  return true;
175 }
176 
177 template <typename Renderer, typename ShaderGen>
179 {
180  _material = material;
181 
182  if (!shader)
183  {
184  return;
185  }
186 
187  std::unordered_map<OutputPtr, InputPtr> bakedOutputMap;
188  for (InputPtr input : shader->getInputs())
189  {
190  OutputPtr output = input->getConnectedOutput();
191  if (output && !bakedOutputMap.count(output))
192  {
193  bakedOutputMap[output] = input;
194  _bakedInputMap[input->getName()] = input->getName();
195 
196  // When possible, nodes with world-space outputs are applied outside of the baking process.
197  NodePtr worldSpaceNode = connectsToWorldSpaceNode(output);
198  if (worldSpaceNode)
199  {
200  output->setConnectedNode(worldSpaceNode->getConnectedNode("in"));
201  _worldSpaceNodes[input->getName()] = worldSpaceNode;
202  }
203  StringMap filenameTemplateMap = initializeFileTemplateMap(input, shader, udim);
204  bakeGraphOutput(output, context, filenameTemplateMap);
205  }
206  else if (bakedOutputMap.count(output))
207  {
208  // When the input shares the same output as a previously baked input, we use the already baked input.
209  _bakedInputMap[input->getName()] = bakedOutputMap[output]->getName();
210  }
211  }
212 
213  // Release all images used to generate this set of shader inputs.
214  Renderer::_imageHandler->clearImageCache();
215 }
216 
217 template <typename Renderer, typename ShaderGen>
218 void TextureBaker<Renderer, ShaderGen>::bakeGraphOutput(OutputPtr output, GenContext& context, const StringMap& filenameTemplateMap)
219 {
220  if (!output)
221  {
222  return;
223  }
224 
225  bool encodeSrgb = _colorSpace == SRGB_TEXTURE && output->isColorType();
226  Renderer::getFramebuffer()->setEncodeSrgb(encodeSrgb);
227 
228  ShaderPtr shader = _generator->generate("BakingShader", output, context);
229  Renderer::createProgram(shader);
230 
231  // Render and capture the requested image.
232  Renderer::renderTextureSpace(getTextureSpaceMin(), getTextureSpaceMax());
233  string texturefilepath = generateTextureFilename(filenameTemplateMap);
234  Renderer::captureImage(_frameCaptureImage);
235 
236  // Construct a baked image record.
237  BakedImage baked;
238  baked.filename = texturefilepath;
239  if (_averageImages)
240  {
241  baked.uniformColor = _frameCaptureImage->getAverageColor();
242  baked.isUniform = true;
243  }
244  else if (_frameCaptureImage->isUniformColor(&baked.uniformColor))
245  {
246  baked.isUniform = true;
247  }
248  _bakedImageMap[output].push_back(baked);
249 
250  // TODO: Write images to memory rather than to disk.
251  // Write non-uniform images to disk.
252  if (!baked.isUniform)
253  {
254  writeBakedImage(baked, _frameCaptureImage);
255  }
256 }
257 
258 template <typename Renderer, typename ShaderGen>
260 {
261  if (!shader)
262  {
263  return;
264  }
265 
266  // Check for fully uniform outputs.
267  for (auto& pair : _bakedImageMap)
268  {
269  bool outputIsUniform = true;
270  for (BakedImage& baked : pair.second)
271  {
272  if (!baked.isUniform || baked.uniformColor != pair.second[0].uniformColor)
273  {
274  outputIsUniform = false;
275  continue;
276  }
277  }
278  if (outputIsUniform)
279  {
280  BakedConstant bakedConstant;
281  bakedConstant.color = pair.second[0].uniformColor;
282  _bakedConstantMap[pair.first] = bakedConstant;
283  }
284  }
285 
286  // Check for uniform outputs at their default values.
287  NodeDefPtr shaderNodeDef = shader->getNodeDef();
288  if (shaderNodeDef)
289  {
290  for (InputPtr shaderInput : shader->getInputs())
291  {
292  OutputPtr output = shaderInput->getConnectedOutput();
293  if (output && _bakedConstantMap.count(output))
294  {
295  InputPtr input = shaderNodeDef->getInput(shaderInput->getName());
296  if (input)
297  {
298  Color4 uniformColor = _bakedConstantMap[output].color;
299  string uniformColorString = getValueStringFromColor(uniformColor, input->getType());
300  string defaultValueString = input->hasValue() ? input->getValue()->getValueString() : EMPTY_STRING;
301  if (uniformColorString == defaultValueString)
302  {
303  _bakedConstantMap[output].isDefault = true;
304  }
305  }
306  }
307  }
308  }
309 
310  // Remove baked images that have been replaced by constant values.
311  for (auto& pair : _bakedConstantMap)
312  {
313  if (pair.second.isDefault || _optimizeConstants || _averageImages)
314  {
315  _bakedImageMap.erase(pair.first);
316  }
317  }
318 }
319 
320 template <typename Renderer, typename ShaderGen>
322 {
323  if (!shader)
324  {
325  return nullptr;
326  }
327 
328  // Create document.
330  {
331  _bakedTextureDoc = createDocument();
332  }
333  if (shader->getDocument()->hasColorSpace())
334  {
335  _bakedTextureDoc->setColorSpace(shader->getDocument()->getColorSpace());
336  }
337 
338  // Create node graph and geometry info.
339  NodeGraphPtr bakedNodeGraph;
340  if (!_bakedImageMap.empty())
341  {
342  _bakedGraphName = _bakedTextureDoc->createValidChildName(_bakedGraphName);
343  bakedNodeGraph = _bakedTextureDoc->addNodeGraph(_bakedGraphName);
344  bakedNodeGraph->setColorSpace(_colorSpace);
345  }
346  _bakedGeomInfoName = _bakedTextureDoc->createValidChildName(_bakedGeomInfoName);
347  GeomInfoPtr bakedGeom = !udimSet.empty() ? _bakedTextureDoc->addGeomInfo(_bakedGeomInfoName) : nullptr;
348  if (bakedGeom)
349  {
350  bakedGeom->setGeomPropValue(UDIM_SET_PROPERTY, udimSet, "stringarray");
351  }
352 
353  // Create a shader node.
354  NodePtr bakedShader = _bakedTextureDoc->addNode(shader->getCategory(), shader->getName(), shader->getType());
355 
356  // Optionally create a material node, connecting it to the new shader node.
357  if (_material)
358  {
359  string materialName = (_texTemplateOverrides.count("$MATERIAL")) ? _texTemplateOverrides["$MATERIAL"] : _material->getName();
360  NodePtr bakedMaterial = _bakedTextureDoc->addNode(_material->getCategory(), materialName, _material->getType());
361  for (auto sourceMaterialInput : _material->getInputs())
362  {
363  const string& sourceMaterialInputName = sourceMaterialInput->getName();
364  NodePtr upstreamShader = sourceMaterialInput->getConnectedNode();
365  if (upstreamShader && (upstreamShader->getNamePath() == shader->getNamePath()))
366  {
367  InputPtr bakedMaterialInput = bakedMaterial->getInput(sourceMaterialInputName);
368  if (!bakedMaterialInput)
369  {
370  bakedMaterialInput = bakedMaterial->addInput(sourceMaterialInputName, sourceMaterialInput->getType());
371  }
372  bakedMaterialInput->setNodeName(bakedShader->getName());
373  }
374  }
375  }
376 
377  // Create and connect inputs on the new shader node.
378  for (ValueElementPtr valueElem : shader->getChildrenOfType<ValueElement>())
379  {
380  // Get the source input and its connected output.
381  InputPtr sourceInput = valueElem->asA<Input>();
382  if (!sourceInput)
383  {
384  continue;
385  }
386 
387  OutputPtr output = sourceInput->getConnectedOutput();
388 
389  // Skip uniform outputs at their default values.
390  if (output && _bakedConstantMap.count(output) && _bakedConstantMap[output].isDefault)
391  {
392  continue;
393  }
394 
395  // Find or create the baked input.
396  const string& sourceName = sourceInput->getName();
397  const string& sourceType = sourceInput->getType();
398  InputPtr bakedInput = bakedShader->getInput(sourceName);
399  if (!bakedInput)
400  {
401  bakedInput = bakedShader->addInput(sourceName, sourceType);
402  }
403 
404  // Assign image or constant data to the baked input.
405  if (output)
406  {
407  // Store a constant value for uniform outputs.
408  if (_optimizeConstants && _bakedConstantMap.count(output))
409  {
410  Color4 uniformColor = _bakedConstantMap[output].color;
411  string uniformColorString = getValueStringFromColor(uniformColor, bakedInput->getType());
412  bakedInput->setValueString(uniformColorString);
413  if (bakedInput->isColorType())
414  {
415  bakedInput->setColorSpace(_colorSpace);
416  }
417  continue;
418  }
419 
420  if (!_bakedImageMap.empty())
421  {
422  // Add the image node.
423  NodePtr bakedImage = bakedNodeGraph->addNode("image", sourceName, sourceType);
424  InputPtr input = bakedImage->addInput("file", "filename");
425  StringMap filenameTemplateMap = initializeFileTemplateMap(bakedInput, shader, udimSet.empty() ? EMPTY_STRING : UDIM_TOKEN);
426  input->setValueString(generateTextureFilename(filenameTemplateMap));
427 
428  // Reconstruct any world-space nodes that were excluded from the baking process.
429  auto worldSpacePair = _worldSpaceNodes.find(sourceInput->getName());
430  if (worldSpacePair != _worldSpaceNodes.end())
431  {
432  NodePtr origWorldSpaceNode = worldSpacePair->second;
433  if (origWorldSpaceNode)
434  {
435  NodePtr newWorldSpaceNode = bakedNodeGraph->addNode(origWorldSpaceNode->getCategory(), sourceName + "_map", sourceType);
436  newWorldSpaceNode->copyContentFrom(origWorldSpaceNode);
437  InputPtr mapInput = newWorldSpaceNode->getInput("in");
438  if (mapInput)
439  {
440  mapInput->setNodeName(bakedImage->getName());
441  }
442  bakedImage = newWorldSpaceNode;
443  }
444  }
445 
446  // Add the graph output.
447  OutputPtr bakedOutput = bakedNodeGraph->addOutput(sourceName + "_output", sourceType);
448  bakedOutput->setConnectedNode(bakedImage);
449  bakedInput->setConnectedOutput(bakedOutput);
450  }
451  }
452  else
453  {
454  bakedInput->copyContentFrom(sourceInput);
455  }
456  }
457 
458  // Generate uniform images and write to disk.
459  ImagePtr uniformImage = createUniformImage(4, 4, 4, Renderer::_baseType, Color4());
460  for (const auto& pair : _bakedImageMap)
461  {
462  for (const BakedImage& baked : pair.second)
463  {
464  if (baked.isUniform)
465  {
466  uniformImage->setUniformColor(baked.uniformColor);
467  writeBakedImage(baked, uniformImage);
468  }
469  }
470  }
471 
472  // Clear cached information after each material bake
473  _bakedImageMap.clear();
474  _bakedConstantMap.clear();
475  _worldSpaceNodes.clear();
476  _bakedInputMap.clear();
477  _material = nullptr;
478 
479  // Return the baked document on success.
480  return _bakedTextureDoc;
481 }
482 
483 template <typename Renderer, typename ShaderGen>
485  const StringVec& udimSet, string& documentName)
486 {
487  if (_outputStream)
488  {
489  *_outputStream << "Processing material: " << materialPath << std::endl;
490  }
491 
492  // Set up generator context for material
493  GenContext genContext(_generator);
495  genContext.getOptions().fileTextureVerticalFlip = true;
496  genContext.getOptions().targetDistanceUnit = _distanceUnit;
497 
499  cms->loadLibrary(doc);
500  genContext.registerSourceCodeSearchPath(searchPath);
502 
503  // Compute the material tag set.
504  StringVec materialTags = udimSet;
505  if (materialTags.empty())
506  {
507  materialTags.push_back(EMPTY_STRING);
508  }
509 
510  ElementPtr elem = doc->getDescendant(materialPath);
511  if (!elem || !elem->isA<Node>())
512  {
513  return nullptr;
514  }
515  NodePtr materialNode = elem->asA<Node>();
516 
517  vector<NodePtr> shaderNodes = getShaderNodes(materialNode);
518  NodePtr shaderNode = shaderNodes.empty() ? nullptr : shaderNodes[0];
519  if (!shaderNode)
520  {
521  return nullptr;
522  }
523 
525 
526  // Iterate over material tags.
527  for (const string& tag : materialTags)
528  {
529  // Always clear any cached implementations before generation.
530  genContext.clearNodeImplementations();
531 
532  ShaderPtr hwShader = createShader("Shader", genContext, shaderNode);
533  if (!hwShader)
534  {
535  continue;
536  }
537  Renderer::_imageHandler->setSearchPath(searchPath);
538  resolver->setUdimString(tag);
539  Renderer::_imageHandler->setFilenameResolver(resolver);
540  bakeShaderInputs(materialNode, shaderNode, genContext, tag);
541 
542  // Optimize baked textures.
543  optimizeBakedTextures(shaderNode);
544  }
545 
546  // Link the baked material and textures in a MaterialX document.
547  documentName = shaderNode->getName();
548  return generateNewDocumentFromShader(shaderNode, udimSet);
549 }
550 
551 template <typename Renderer, typename ShaderGen>
553 {
554  if (_outputImagePath.isEmpty())
555  {
556  _outputImagePath = outputFilename.getParentPath();
557  if (!_outputImagePath.exists())
558  {
559  _outputImagePath.createDirectory();
560  }
561  }
562 
563  std::vector<TypedElementPtr> renderableMaterials = findRenderableElements(doc);
564 
565  // Compute the UDIM set.
566  ValuePtr udimSetValue = doc->getGeomPropValue(UDIM_SET_PROPERTY);
567  StringVec udimSet;
568  if (udimSetValue && udimSetValue->isA<StringVec>())
569  {
570  udimSet = udimSetValue->asA<StringVec>();
571  }
572 
573  // Bake all materials in documents to memory.
574  BakedDocumentVec bakedDocuments;
575  for (size_t i = 0; i < renderableMaterials.size(); i++)
576  {
577  if (_outputStream && i > 0)
578  {
579  *_outputStream << std::endl;
580  }
581 
582  const TypedElementPtr& element = renderableMaterials[i];
583  string documentName;
584  DocumentPtr bakedMaterialDoc = bakeMaterialToDoc(doc, searchPath, element->getNamePath(), udimSet, documentName);
585  if (_writeDocumentPerMaterial && bakedMaterialDoc)
586  {
587  bakedDocuments.push_back(make_pair(documentName, bakedMaterialDoc));
588  }
589  }
590 
592  {
593  // Write documents in memory to disk.
594  size_t bakeCount = bakedDocuments.size();
595  for (size_t i = 0; i < bakeCount; i++)
596  {
597  if (bakedDocuments[i].second)
598  {
599  FilePath writeFilename = outputFilename;
600 
601  // Add additional filename decorations if there are multiple documents.
602  if (bakedDocuments.size() > 1)
603  {
604  const string extension = writeFilename.getExtension();
605  writeFilename.removeExtension();
606  string filenameSeparator = writeFilename.isDirectory() ? EMPTY_STRING : "_";
607  writeFilename = FilePath(writeFilename.asString() + filenameSeparator + bakedDocuments[i].first + "." + extension);
608  }
609 
610  writeToXmlFile(bakedDocuments[i].second, writeFilename);
611  if (_outputStream)
612  {
613  *_outputStream << "Wrote baked document: " << writeFilename.asString() << std::endl;
614  }
615  }
616  }
617  }
618  else if (_bakedTextureDoc)
619  {
620  writeToXmlFile(_bakedTextureDoc, outputFilename);
621  if (_outputStream)
622  {
623  *_outputStream << "Wrote baked document: " << outputFilename.asString() << std::endl;
624  }
625  }
626 }
627 
628 template <typename Renderer, typename ShaderGen>
630 {
631  UnitTypeDefPtr distanceTypeDef = unitDefinitions ? unitDefinitions->getUnitTypeDef("distance") : nullptr;
632  UnitTypeDefPtr angleTypeDef = unitDefinitions ? unitDefinitions->getUnitTypeDef("angle") : nullptr;
633  if (!distanceTypeDef && !angleTypeDef)
634  {
635  return;
636  }
637 
638  UnitSystemPtr unitSystem = UnitSystem::create(_generator->getTarget());
639  if (!unitSystem)
640  {
641  return;
642  }
643  _generator->setUnitSystem(unitSystem);
645  registry->addUnitConverter(distanceTypeDef, LinearUnitConverter::create(distanceTypeDef));
646  registry->addUnitConverter(angleTypeDef, LinearUnitConverter::create(angleTypeDef));
647  _generator->getUnitSystem()->loadLibrary(unitDefinitions);
648  _generator->getUnitSystem()->setUnitConverterRegistry(registry);
649 }
650 
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
const string DEFAULT_UDIM_PREFIX
GT_API const UT_StringHolder filename
static const string EXR_EXTENSION
Definition: ImageHandler.h:117
IMATH_HOSTDEVICE void getValue(S &a, S &b, S &c, S &d) const IMATH_NOEXCEPT
Return the value.
Definition: ImathColor.h:544
Definition: File.h:26
_writeDocumentPerMaterial(true)
void setColorManagementSystem(ColorManagementSystemPtr colorManagementSystem)
Sets the color management system.
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Definition: Node.h:52
bool fileTextureVerticalFlip
Definition: GenOptions.h:111
GLuint start
Definition: glcorearb.h:475
MX_FORMAT_API void writeToXmlFile(DocumentPtr doc, const FilePath &filename, const XmlWriteOptions *writeOptions=nullptr)
void clearNodeImplementations()
Clear all cached shader node implementation.
shared_ptr< TypedElement > TypedElementPtr
A shared pointer to a TypedElement.
Definition: Element.h:36
shared_ptr< class UnitSystem > UnitSystemPtr
A shared pointer to a UnitSystem.
Definition: UnitSystem.h:26
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
static StringResolverPtr create()
Create a new string resolver.
Definition: Element.h:1215
static UnitConverterRegistryPtr create()
Creator.
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
const string & getBaseName() const
Definition: File.h:105
static const string TIFF_EXTENSION
Definition: ImageHandler.h:127
shared_ptr< GeomInfo > GeomInfoPtr
A shared pointer to a GeomInfo.
Definition: Geom.h:38
string targetColorSpaceOverride
Definition: GenOptions.h:116
bool writeBakedImage(const BakedImage &baked, ImagePtr image)
std::vector< std::pair< std::string, DocumentPtr >> BakedDocumentVec
A vector of baked documents with their associated names.
Definition: TextureBaker.h:24
MX_GENSHADER_API vector< TypedElementPtr > findRenderableElements(ConstDocumentPtr doc)
bool isDirectory() const
Return true if the given path is a directory on the file system.
void createDirectory() const
Create a directory on the file system at the given path.
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER IMFUTIL_EXPORT void saveImage(const std::string &fileName, const Header &hdr, const Image &img, DataWindowSource dws=USE_IMAGE_DATA_WINDOW)
shared_ptr< UnitTypeDef > UnitTypeDefPtr
A shared pointer to a UnitTypeDef.
Definition: Definition.h:67
ShaderGenerator & getShaderGenerator()
Return shader generatior.
Definition: GenContext.h:37
string getValueStringFromColor(const Color4 &color, const string &type)
StringMap initializeFileTemplateMap(InputPtr input, NodePtr shader, const string &udim=EMPTY_STRING)
void bakeAllMaterials(DocumentPtr doc, const FileSearchPath &searchPath, const FilePath &outputFileName)
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
static const string PNG_EXTENSION
Definition: ImageHandler.h:123
BaseType
Definition: Image.h:48
GLfloat f
Definition: glcorearb.h:1926
void removeExtension()
Remove the file extension, if any, from the given path.
Definition: File.h:141
virtual OutputPtr getConnectedOutput() const
Return the output, if any, to which this input is connected.
MATERIALX_NAMESPACE_BEGIN MX_CORE_API vector< NodePtr > getShaderNodes(NodePtr materialNode, const string &nodeType=SURFACE_SHADER_TYPE_STRING, const string &target=EMPTY_STRING)
static LinearUnitConverterPtr create(UnitTypeDefPtr UnitDef)
Creator.
void bakeGraphOutput(OutputPtr output, GenContext &context, const StringMap &filenameTemplateMap)
Bake a texture for the given graph output.
TextureBaker(unsigned int width, unsigned int height, Image::BaseType baseType, bool flipSavedImage)
static ImagePtr create(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType=BaseType::UINT8)
Create an empty image with the given properties.
Definition: Image.h:60
static DefaultColorManagementSystemPtr create(const string &target)
Create a new DefaultColorManagementSystem.
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
Definition: Types.h:285
string getExtension() const
Return the file extension of the given path.
Definition: File.h:127
MX_RENDER_API ImagePtr createUniformImage(unsigned int width, unsigned int height, unsigned int channelCount, Image::BaseType baseType, const Color4 &color)
Create a uniform-color image with the given properties.
void optimizeBakedTextures(NodePtr shader)
Optimize baked textures before writing.
DocumentPtr bakeMaterialToDoc(DocumentPtr doc, const FileSearchPath &searchPath, const string &materialPath, const StringVec &udimSet, std::string &documentName)
Bake material to document in memory and write baked textures to disk.
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
_flipSavedImage(flipSavedImage)
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:31
size_t findVarInTemplate(const string &filename, const string &var, size_t start=0)
virtual const string & getTarget() const
Return the name of the target this generator is for.
string asString(Format format=FormatNative) const
Return this path as a standard string with the given format.
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
Definition: Element.h:66
GLuint shader
Definition: glcorearb.h:785
shared_ptr< UnitConverterRegistry > UnitConverterRegistryPtr
A shared pointer to a UnitConverterRegistry.
Definition: Unit.h:33
FilePath generateTextureFilename(const StringMap &fileTemplateMap)
MX_GENSHADER_API NodePtr connectsToWorldSpaceNode(OutputPtr output)
GLuint color
Definition: glcorearb.h:1261
GenOptions & getOptions()
Return shader generation options.
Definition: GenContext.h:43
static UnitSystemPtr create(const string &target)
Create a new UnitSystem.
Definition: Types.h:305
DocumentPtr generateNewDocumentFromShader(NodePtr shader, const StringVec &udimSet)
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.
GLint GLsizei width
Definition: glcorearb.h:103
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext &context, const string &udim=EMPTY_STRING)
Bake textures for all graph inputs of the given shader.
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
Definition: Element.h:41
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
string targetDistanceUnit
Definition: GenOptions.h:121
static OiioImageLoaderPtr create()
Create a new OpenImageIO image loader.
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
static const string HDR_EXTENSION
Definition: ImageHandler.h:119
FilePath getParentPath() const
Definition: File.h:116
MX_CORE_API const string UDIM_TOKEN
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
type
Definition: core.h:1059
shared_ptr< NodeGraph > NodeGraphPtr
A shared pointer to a NodeGraph.
Definition: Node.h:34
MX_CORE_API const string UDIM_SET_PROPERTY
void setupUnitSystem(DocumentPtr unitDefinitions)
Set up the unit definitions to be used in baking.
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
shared_ptr< class DefaultColorManagementSystem > DefaultColorManagementSystemPtr
A shared pointer to a DefaultColorManagementSystem.
static StbImageLoaderPtr create()
Create a new stb image loader.
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24
void registerSourceCodeSearchPath(const FilePath &path)
Definition: GenContext.h:56
shared_ptr< NodeDef > NodeDefPtr
A shared pointer to a NodeDef.
Definition: Definition.h:32
_bakedTextureDoc(nullptr)