Skip to content

Commit 2992308

Browse files
authored
Override aggregated struct generation for MDL (#2261)
Add a custom StructTypeSyntax implementation for MDL.
1 parent f70d55c commit 2992308

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

source/MaterialXGenMdl/MdlShaderGenerator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,19 @@ void MdlShaderGenerator::emitMdlVersionFilenameSuffix(GenContext& context, Shade
816816
emitString(getMdlVersionFilenameSuffix(context), stage);
817817
}
818818

819+
void MdlShaderGenerator::emitTypeDefinitions(GenContext&, ShaderStage& stage) const
820+
{
821+
// Emit typedef statements for all data types that have an alias
822+
for (const auto& syntax : _syntax->getTypeSyntaxes())
823+
{
824+
if (!syntax->getTypeDefinition().empty())
825+
{
826+
stage.addLine("export " + syntax->getTypeDefinition(), false);
827+
}
828+
}
829+
stage.newLine();
830+
}
831+
819832
namespace MDL
820833
{
821834
// Identifiers for MDL variable blocks

source/MaterialXGenMdl/MdlShaderGenerator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class MX_GENMDL_API MdlShaderGenerator : public ShaderGenerator
100100
/// Get the version number suffix appended to MDL modules that use versions.
101101
const string& getMdlVersionFilenameSuffix(GenContext& context) const;
102102

103+
/// Emit type definitions for all data types that need them.
104+
void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const override;
105+
103106
protected:
104107
// Create and initialize a new MDL shader for shader generation.
105108
ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;

source/MaterialXGenMdl/MdlSyntax.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,41 @@ const string& MdlSyntax::getMdlVersionSuffixMarker() const
615615
return MARKER_MDL_VERSION_SUFFIX;
616616
}
617617

618+
StructTypeSyntaxPtr MdlSyntax::createStructSyntax(const string& structTypeName, const string& defaultValue,
619+
const string& uniformDefaultValue, const string& typeAlias,
620+
const string& typeDefinition) const
621+
{
622+
return std::make_shared<MdlStructTypeSyntax>(
623+
this,
624+
structTypeName,
625+
defaultValue,
626+
uniformDefaultValue,
627+
typeAlias,
628+
typeDefinition);
629+
}
630+
631+
string MdlStructTypeSyntax::getValue(const Value& value, bool /* uniform */) const
632+
{
633+
const AggregateValue& aggValue = static_cast<const AggregateValue&>(value);
634+
635+
string result = aggValue.getTypeString() + "(";
636+
637+
string separator = "";
638+
for (const auto& memberValue : aggValue.getMembers())
639+
{
640+
result += separator;
641+
separator = ", ";
642+
643+
const string& memberTypeName = memberValue->getTypeString();
644+
const TypeDesc memberTypeDesc = _parent->getType(memberTypeName);
645+
646+
// Recursively use the syntax to generate the output, so we can supported nested structs.
647+
result += _parent->getValue(memberTypeDesc, *memberValue, true);
648+
}
649+
650+
result += ")";
651+
652+
return result;
653+
}
654+
618655
MATERIALX_NAMESPACE_END

source/MaterialXGenMdl/MdlSyntax.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class MX_GENMDL_API MdlSyntax : public Syntax
2929

3030
static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared<MdlSyntax>(typeSystem); }
3131

32+
StructTypeSyntaxPtr createStructSyntax(const string& structTypeName, const string& defaultValue,
33+
const string& uniformDefaultValue, const string& typeAlias,
34+
const string& typeDefinition) const override;
35+
3236
const string& getConstantQualifier() const override { return CONST_QUALIFIER; };
3337
const string& getUniformQualifier() const override { return UNIFORM_QUALIFIER; };
3438
const string& getSourceFileExtension() const override { return SOURCE_FILE_EXTENSION; };
@@ -77,6 +81,16 @@ class MX_GENMDL_API MdlSyntax : public Syntax
7781
const string& getMdlVersionSuffixMarker() const;
7882
};
7983

84+
/// @class MdlStructTypeSyntax
85+
/// Specialization of TypeSyntax for aggregate types.
86+
class MX_GENMDL_API MdlStructTypeSyntax : public StructTypeSyntax
87+
{
88+
public:
89+
using StructTypeSyntax::StructTypeSyntax;
90+
91+
string getValue(const Value& value, bool uniform) const override;
92+
};
93+
8094
namespace Type
8195
{
8296

0 commit comments

Comments
 (0)