forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MaterialXOCIO backported to MaterialX
- Loading branch information
1 parent
9750509
commit 73df1c6
Showing
8 changed files
with
478 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// | ||
// Copyright Contributors to the MaterialXOCIO Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include <MaterialXGenShader/Nodes/OpenColorIONode.h> | ||
#include <MaterialXGenShader/OpenColorIOManagementSystem.h> | ||
|
||
#include <MaterialXCore/Interface.h> | ||
#include <MaterialXGenGlsl/GlslShaderGenerator.h> | ||
#include <MaterialXGenShader/GenContext.h> | ||
#include <MaterialXGenShader/Library.h> | ||
#include <MaterialXGenShader/ShaderNode.h> | ||
#include <MaterialXGenShader/Shader.h> | ||
#include <MaterialXGenShader/ShaderStage.h> | ||
|
||
#include <OpenColorIO/OpenColorIO.h> | ||
#include <OpenColorIO/OpenColorTypes.h> | ||
|
||
#include <cstring> | ||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
|
||
MATERIALX_NAMESPACE_BEGIN | ||
|
||
namespace | ||
{ | ||
// Internal OCIO strings: | ||
constexpr const char OCIO_COLOR3[] = "color3"; | ||
|
||
// Lengths where needed: | ||
constexpr auto OCIO_COLOR3_LEN = sizeof(OCIO_COLOR3) / sizeof(OCIO_COLOR3[0]); | ||
|
||
} // namespace | ||
|
||
ShaderNodeImplPtr OpenColorIONode::create() | ||
{ | ||
return std::make_shared<OpenColorIONode>(); | ||
} | ||
|
||
void OpenColorIONode::initialize(const InterfaceElement& element, GenContext& context) | ||
{ | ||
ShaderNodeImpl::initialize(element, context); | ||
|
||
// Single function shared between color3 and color4 nodes, use a custom hash with only the function name. | ||
_hash = std::hash<std::string>{}(getFunctionName()); | ||
} | ||
|
||
void OpenColorIONode::emitFunctionDefinition( | ||
const ShaderNode& /*node*/, | ||
GenContext& context, | ||
ShaderStage& stage) const | ||
{ | ||
if (stage.getName() == Stage::PIXEL) | ||
{ | ||
auto ocioManager = std::dynamic_pointer_cast<OpenColorIOManagementSystem>(context.getShaderGenerator().getColorManagementSystem()); | ||
|
||
auto gpuProcessor = ocioManager->getGpuProcessor(getName()); | ||
OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc(); | ||
|
||
// TODO: Extend to essl and MDL and possibly SLang. | ||
if (context.getShaderGenerator().getTarget() == "genglsl") { | ||
shaderDesc->setLanguage(OCIO::GPU_LANGUAGE_GLSL_4_0); | ||
} else if (context.getShaderGenerator().getTarget() == "genmsl") { | ||
shaderDesc->setLanguage(OCIO::GPU_LANGUAGE_MSL_2_0); | ||
} else if (context.getShaderGenerator().getTarget() == "genosl") { | ||
shaderDesc->setLanguage(OCIO::LANGUAGE_OSL_1); | ||
} | ||
|
||
auto functionName = getFunctionName(); | ||
|
||
shaderDesc->setFunctionName(functionName.c_str()); | ||
|
||
gpuProcessor->extractGpuShaderInfo(shaderDesc); | ||
|
||
stage.addString(shaderDesc->getShaderText()); | ||
stage.endLine(false); | ||
} | ||
} | ||
|
||
void OpenColorIONode::emitFunctionCall( | ||
const ShaderNode& node, | ||
GenContext& context, | ||
ShaderStage& stage) const | ||
{ | ||
if (stage.getName() == Stage::PIXEL) | ||
{ | ||
auto functionName = getFunctionName(); | ||
|
||
// TODO: Adjust syntax for other languages. OSL definitely does not have a native color4 type | ||
// and requires an add-on struct to be used. | ||
|
||
// The OCIO function uses a vec4 parameter, so: | ||
// Function call for color4: vec4 res = func(in); | ||
// Function call for color3: vec3 res = func(vec4(in, 1.0)).rgb; | ||
// TODO: Handle LUT samplers. | ||
bool isColor3 = getName().back() == '3'; | ||
|
||
const auto& shadergen = context.getShaderGenerator(); | ||
shadergen.emitLineBegin(stage); | ||
|
||
const auto* output = node.getOutput(); | ||
const auto* colorInput = node.getInput(0); | ||
|
||
shadergen.emitOutput(output, true, false, context, stage); | ||
shadergen.emitString(" = ", stage); | ||
|
||
shadergen.emitString(functionName + "(", stage); | ||
if (isColor3) | ||
{ | ||
shadergen.emitString("vec4(", stage); | ||
} | ||
shadergen.emitInput(colorInput, context, stage); | ||
if (isColor3) | ||
{ | ||
shadergen.emitString(", 1.0)", stage); | ||
} | ||
|
||
shadergen.emitString(")", stage); | ||
|
||
if (isColor3) | ||
{ | ||
shadergen.emitString(".rgb", stage); | ||
} | ||
|
||
shadergen.emitLineEnd(stage); | ||
} | ||
} | ||
|
||
std::string OpenColorIONode::getFunctionName() const | ||
{ | ||
auto name = getName(); | ||
|
||
// Strip _color3 and _color4 suffixes and impl prefix: | ||
size_t startPos = OpenColorIOManagementSystem::IMPL_PREFIX.size(); | ||
size_t length = name.size() - OCIO_COLOR3_LEN - 1 - startPos; | ||
|
||
return name.substr(startPos, length); | ||
} | ||
|
||
MATERIALX_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Copyright Contributors to the MaterialX Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#ifndef MATERIALX_OCIO_NODE_H | ||
#define MATERIALX_OCIO_NODE_H | ||
|
||
#ifdef MATERIALX_BUILD_OCIO | ||
/// @file | ||
/// OCIO node implementation | ||
|
||
#include <MaterialXGenShader/ShaderNodeImpl.h> | ||
#include <MaterialXCore/Interface.h> | ||
#include <MaterialXGenShader/GenContext.h> | ||
#include <MaterialXGenShader/Library.h> | ||
#include <MaterialXGenShader/ShaderNode.h> | ||
#include <MaterialXGenShader/ShaderStage.h> | ||
|
||
#include <string> | ||
|
||
namespace mx = MaterialX; | ||
|
||
MATERIALX_NAMESPACE_BEGIN | ||
|
||
/// GLSL OCIO node implementation. Takes a Maya OCIO shader fragment and | ||
/// makes it compatible with the shadergen | ||
class OpenColorIONode : public ShaderNodeImpl | ||
{ | ||
public: | ||
static ShaderNodeImplPtr create(); | ||
|
||
void initialize(const InterfaceElement& element, GenContext& context) override; | ||
|
||
void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) | ||
const override; | ||
|
||
void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) | ||
const override; | ||
|
||
private: | ||
std::string getFunctionName() const; | ||
}; | ||
|
||
MATERIALX_NAMESPACE_END | ||
|
||
#endif | ||
#endif |
Oops, something went wrong.