Skip to content
Closed
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
1 change: 1 addition & 0 deletions python/MaterialXTest/genshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_ShaderInterface(self):
self.assertTrue(foundTarget)
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(searchPath)
shadergen.registerTypeDefs(doc, context)

# Test generator with complete mode
context.getOptions().shaderInterfaceType = mx_gen_shader.ShaderInterfaceType.SHADER_INTERFACE_COMPLETE;
Expand Down
1 change: 1 addition & 0 deletions python/Scripts/generateshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def main():
codeSearchPath.append(os.path.dirname(inputFilename))
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(codeSearchPath)
shadergen.registerTypeDefs(doc, context)

# If we're generating Vulkan-compliant GLSL then set the binding context
if opts.vulkanCompliantGlsl:
Expand Down
39 changes: 39 additions & 0 deletions resources/Materials/TestSuite/stdlib/structs/struct_texcoord.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- TODO - Decide if perhaps this structs test folder belongs in a different parent folder
its not really testing any stdlib node.
Its more like testing Core MaterialX features -->

<typedef name="texcoord_struct">
<member name="ss" type="float" value="0.5"/>
<member name="tt" type="float" value="0.5"/>
</typedef>

<nodedef name="ND_extract_s_texcoord" node="extracts" nodegroup="shader" >
<input name="in" type="texcoord_struct" value="{0.1;0.1}" />
<output name="out" type="float" value="0.0" />
</nodedef>

<implementation name="IM_extract_s_texcoord_genmsl" nodedef="ND_extract_s_texcoord" target="genmsl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genosl" nodedef="ND_extract_s_texcoord" target="genosl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genmdl" nodedef="ND_extract_s_texcoord" target="genmdl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genglsl" nodedef="ND_extract_s_texcoord" target="genglsl" sourcecode="{{in}}.ss" />

<nodegraph name="test_struct_texcoord">
<extracts name="extracts" type="float">
<input name="in" type="texcoord_struct" value="{0.01;0.5}"/>
</extracts>
<convert name="float_to_color3" type="color3">
<input name="in" type="float" nodename="extracts"/>
</convert>
<oren_nayar_diffuse_bsdf name="diffuse_brdf1" type="BSDF">
<input name="color" type="color3" nodename="float_to_color3" />
</oren_nayar_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="diffuse_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>
<output name="out" type="surfaceshader" nodename="surface1" />
</nodegraph>
</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<materialx version="1.38">

<typedef name="texcoord_struct">
<member name="ss" type="float" value="0.5"/>
<member name="tt" type="float" value="0.5"/>
</typedef>
<typedef name="texcoordGroup_struct">
<member name="st_0" type="texcoord_struct" value="{0.1;0.1}"/>
<member name="st_1" type="texcoord_struct" value="{0.5;0.5}"/>
<member name="st_2" type="texcoord_struct" value="{0.9;0.9}"/>
</typedef>

<nodedef name="ND_extract_first_s_texcoordGroup" node="extractfirstsgroup" nodegroup="shader" >
<input name="in" type="texcoordGroup_struct" value="{{0.1;0.2};{0.3;0.4};{0.5;0.6}}"/>
<output name="out" type="float" value="0.0" />
</nodedef>

<implementation name="IM_extract_first_s_texcoordGroup_genglsl" nodedef="ND_extract_first_s_texcoordGroup" target="genglsl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genmdl" nodedef="ND_extract_first_s_texcoordGroup" target="genmdl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genmsl" nodedef="ND_extract_first_s_texcoordGroup" target="genmsl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genosl" nodedef="ND_extract_first_s_texcoordGroup" target="genosl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>

<nodegraph name="test_struct_texcoordgroup">
<extractfirstsgroup name="extractfirstsgroup" type="float">
<input name="in" type="texcoordGroup_struct" value="{{0.1;0.2};{0.3;0.4};{0.5;0.6}}"/>
</extractfirstsgroup>
<convert name="float_to_color3" type="color3">
<input name="in" type="float" nodename="extractfirstsgroup"/>
</convert>
<oren_nayar_diffuse_bsdf name="diffuse_brdf1" type="BSDF">
<input name="color" type="color3" nodename="float_to_color3" />
</oren_nayar_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="diffuse_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>
<output name="out" type="surfaceshader" nodename="surface1" />
</nodegraph>
</materialx>
1 change: 1 addition & 0 deletions source/JsMaterialX/JsMaterialXGenShader/JsTypeDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

#include <MaterialXGenShader/TypeDesc.h>
#include <MaterialXGenShader/GenContext.h>

#include <emscripten/bind.h>

Expand Down
8 changes: 4 additions & 4 deletions source/MaterialXGenGlsl/GlslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& c
else
{
string outputValue = outputSocket->getValue() ?
_syntax->getValue(outputSocket->getType(), *outputSocket->getValue()) :
_syntax->getValue(outputSocket->getType(), *outputSocket->getValue(), context) :
_syntax->getDefaultValue(outputSocket->getType());
if (!outputSocket->getType().isFloat4())
{
Expand Down Expand Up @@ -689,7 +689,7 @@ void GlslShaderGenerator::toVec4(TypeDesc type, string& variable)
}

void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, const string& qualifier,
GenContext&, ShaderStage& stage,
GenContext& context, ShaderStage& stage,
bool assignValue) const
{
// A file texture input needs special handling on GLSL
Expand Down Expand Up @@ -724,7 +724,7 @@ void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, co
if (assignValue)
{
const string valueStr = (variable->getValue() ?
_syntax->getValue(variable->getType(), *variable->getValue(), true) :
_syntax->getValue(variable->getType(), *variable->getValue(), context, true) :
_syntax->getDefaultValue(variable->getType(), true));
str += valueStr.empty() ? EMPTY_STRING : " = " + valueStr;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ ShaderNodeImplPtr GlslShaderGenerator::getImplementation(const NodeDef& nodedef,
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}

const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
const TypeDesc outputType = context.getTypeDesc(outputs[0]->getType());

if (implElement->isA<NodeGraph>())
{
Expand Down
11 changes: 6 additions & 5 deletions source/MaterialXGenGlsl/GlslSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <MaterialXGenGlsl/GlslSyntax.h>

#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderGenerator.h>

MATERIALX_NAMESPACE_BEGIN
Expand All @@ -20,7 +21,7 @@ class GlslStringTypeSyntax : public StringTypeSyntax
GlslStringTypeSyntax() :
StringTypeSyntax("int", "0", "0") { }

string getValue(const Value& /*value*/, bool /*uniform*/) const override
string getValue(const Value& /*value*/, const GenContext& /*context*/, bool /*uniform*/) const override
{
return "0";
}
Expand All @@ -34,7 +35,7 @@ class GlslArrayTypeSyntax : public ScalarTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
size_t arraySize = getSize(value);
if (arraySize > 0)
Expand Down Expand Up @@ -388,7 +389,7 @@ StructTypeSyntaxPtr GlslSyntax::createStructSyntax(const string& structTypeName,
typeDefinition);
}

string GlslStructTypeSyntax::getValue(const Value& value, bool /* uniform */) const
string GlslStructTypeSyntax::getValue(const Value& value, const GenContext& context, bool /*uniform*/) const
{
const AggregateValue& aggValue = static_cast<const AggregateValue&>(value);

Expand All @@ -401,10 +402,10 @@ string GlslStructTypeSyntax::getValue(const Value& value, bool /* uniform */) co
separator = ",";

const string& memberTypeName = memberValue->getTypeString();
TypeDesc memberTypeDesc = TypeDesc::get(memberTypeName);
TypeDesc memberTypeDesc = context.getTypeDesc(memberTypeName);

// Recursively use the syntax to generate the output, so we can supported nested structs.
result += _parentSyntax->getValue(memberTypeDesc, *memberValue, true);
result += _parentSyntax->getValue(memberTypeDesc, *memberValue, context, true);
}

result += ")";
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenGlsl/GlslSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MX_GENGLSL_API GlslStructTypeSyntax : public StructTypeSyntax
public:
using StructTypeSyntax::StructTypeSyntax;

string getValue(const Value& value, bool uniform) const override;
string getValue(const Value& value, const GenContext& context, bool uniform) const override;
};

MATERIALX_NAMESPACE_END
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void LightCompoundNodeGlsl::initialize(const InterfaceElement& element, GenConte
NodeDefPtr nodeDef = graph.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
_lightUniforms.add(TypeDesc::get(input->getType()), input->getName());
_lightUniforms.add(context.getTypeDesc(input->getType()), input->getName());
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void LightShaderNodeGlsl::initialize(const InterfaceElement& element, GenContext
NodeDefPtr nodeDef = impl.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
_lightUniforms.add(TypeDesc::get(input->getType()), input->getName(), input->getValue());
_lightUniforms.add(context.getTypeDesc(input->getType()), input->getName(), input->getValue());
}
}

Expand Down
21 changes: 17 additions & 4 deletions source/MaterialXGenMdl/MdlShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
emitScopeBegin(stage, Syntax::PARENTHESES);

// Emit shader inputs
emitShaderInputs(element->getDocument(), stage.getInputBlock(MDL::INPUTS), stage);
emitShaderInputs(element->getDocument(), stage.getInputBlock(MDL::INPUTS), context, stage);

// End shader signature
emitScopeEnd(stage);
Expand Down Expand Up @@ -333,7 +333,7 @@ ShaderNodeImplPtr MdlShaderGenerator::getImplementation(const NodeDef& nodedef,
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}

const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
const TypeDesc outputType = context.getTypeDesc(outputs[0]->getType());

if (implElement->isA<NodeGraph>())
{
Expand Down Expand Up @@ -658,7 +658,7 @@ void emitInputAnnotations(const MdlShaderGenerator& _this, const DocumentPtr doc

} // anonymous namespace

void MdlShaderGenerator::emitShaderInputs(const DocumentPtr doc, const VariableBlock& inputs, ShaderStage& stage) const
void MdlShaderGenerator::emitShaderInputs(const DocumentPtr doc, const VariableBlock& inputs, const GenContext& context, ShaderStage& stage) const
{
const string uniformPrefix = _syntax->getUniformQualifier() + " ";
for (size_t i = 0; i < inputs.size(); ++i)
Expand All @@ -668,7 +668,7 @@ void MdlShaderGenerator::emitShaderInputs(const DocumentPtr doc, const VariableB
const string& qualifier = input->isUniform() || input->getType() == Type::FILENAME ? uniformPrefix : EMPTY_STRING;
const string& type = _syntax->getTypeName(input->getType());

string value = input->getValue() ? _syntax->getValue(input->getType(), *input->getValue(), true) : EMPTY_STRING;
string value = input->getValue() ? _syntax->getValue(input->getType(), *input->getValue(), context, true) : EMPTY_STRING;
const string& geomprop = input->getGeomProp();
if (!geomprop.empty())
{
Expand Down Expand Up @@ -753,6 +753,19 @@ void MdlShaderGenerator::emitMdlVersionFilenameSuffix(GenContext& context, Shade
emitString(getMdlVersionFilenameSuffix(context), stage);
}

void MdlShaderGenerator::registerBuiltinTypes(GenContext& context)
{
ShaderGenerator::registerBuiltinTypes(context);

// Custom types to handle enumeration output
context.registerTypeDesc(Type::MDL_COORDINATESPACE);
context.registerTypeDesc(Type::MDL_ADDRESSMODE);
context.registerTypeDesc(Type::MDL_FILTERLOOKUPMODE);
context.registerTypeDesc(Type::MDL_FILTERTYPE);
context.registerTypeDesc(Type::MDL_DISTRIBUTIONTYPE);
context.registerTypeDesc(Type::MDL_SCATTER_MODE);
}

namespace MDL
{
// Identifiers for MDL variable blocks
Expand Down
4 changes: 3 additions & 1 deletion source/MaterialXGenMdl/MdlShaderGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ class MX_GENMDL_API MdlShaderGenerator : public ShaderGenerator
/// Get the version number suffix appended to MDL modules that use versions.
const string& getMdlVersionFilenameSuffix(GenContext& context) const;

void registerBuiltinTypes(GenContext& context) override;

protected:
// Create and initialize a new MDL shader for shader generation.
ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;

// Emit a block of shader inputs.
void emitShaderInputs(const DocumentPtr doc, const VariableBlock& inputs, ShaderStage& stage) const;
void emitShaderInputs(const DocumentPtr doc, const VariableBlock& inputs, const GenContext& context, ShaderStage& stage) const;
};

namespace MDL
Expand Down
21 changes: 4 additions & 17 deletions source/MaterialXGenMdl/MdlSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@

MATERIALX_NAMESPACE_BEGIN

// Custom types to handle enumeration output
namespace Type
{

TYPEDESC_REGISTER_TYPE(MDL_COORDINATESPACE, "coordinatespace")
TYPEDESC_REGISTER_TYPE(MDL_ADDRESSMODE, "addressmode")
TYPEDESC_REGISTER_TYPE(MDL_FILTERLOOKUPMODE, "filterlookup")
TYPEDESC_REGISTER_TYPE(MDL_FILTERTYPE, "filtertype")
TYPEDESC_REGISTER_TYPE(MDL_DISTRIBUTIONTYPE, "distributiontype")
TYPEDESC_REGISTER_TYPE(MDL_SCATTER_MODE, "scatter_mode")

} // namespace Type

namespace
{

Expand All @@ -37,7 +24,7 @@ class MdlFilenameTypeSyntax : public ScalarTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
const string outputValue = value.getValueString();
if (outputValue.empty() || outputValue == "/")
Expand Down Expand Up @@ -84,7 +71,7 @@ class MdlArrayTypeSyntax : public ScalarTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
if (!isEmpty(value))
{
Expand Down Expand Up @@ -145,7 +132,7 @@ class MdlColor4TypeSyntax : public AggregateTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
StringStream ss;

Expand All @@ -171,7 +158,7 @@ class MdlEnumSyntax : public AggregateTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
return _name + "_" + value.getValueString();
}
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenMdl/Nodes/BlurNodeMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void BlurNodeMdl::emitFunctionCall(const ShaderNode& node, GenContext& context,
string emitValue = "\"" + GAUSSIAN_FILTER + "\"";
if (syntax.remapEnumeration(GAUSSIAN_FILTER, Type::STRING, FILTER_LIST, result))
{
emitValue = syntax.getValue(result.first, *(result.second));
emitValue = syntax.getValue(result.first, *(result.second), context);
}
shadergen.emitString(" == " + emitValue + " ? ", stage);
{
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenMdl/Nodes/ClosureLayerNodeMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ ShaderNodeImplPtr LayerableNodeMdl::create()
return std::make_shared<LayerableNodeMdl>();
}

void LayerableNodeMdl::addInputs(ShaderNode& node, GenContext& /*context*/) const
void LayerableNodeMdl::addInputs(ShaderNode& node, GenContext&) const
{
// Add the input to hold base layer BSDF.
node.addInput(StringConstantsMdl::BASE, Type::BSDF);
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenMdl/Nodes/CompoundNodeMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void CompoundNodeMdl::emitFunctionSignature(const ShaderNode&, GenContext& conte
const string& qualifier = input->isUniform() || input->getType() == Type::FILENAME ? uniformPrefix : EMPTY_STRING;
const string& type = syntax.getTypeName(input->getType());

string value = input->getValue() ? syntax.getValue(input->getType(), *input->getValue(), true) : EMPTY_STRING;
string value = input->getValue() ? syntax.getValue(input->getType(), *input->getValue(), context, true) : EMPTY_STRING;
const string& geomprop = input->getGeomProp();
if (!geomprop.empty())
{
Expand Down
Loading