diff --git a/javascript/MaterialXTest/browser/shaderGenerator.spec.js b/javascript/MaterialXTest/browser/shaderGenerator.spec.js
index df44297d46..7ca1d74b32 100644
--- a/javascript/MaterialXTest/browser/shaderGenerator.spec.js
+++ b/javascript/MaterialXTest/browser/shaderGenerator.spec.js
@@ -34,17 +34,17 @@ describe('Generate Shaders', function ()
const generators = []
if (typeof mx.EsslShaderGenerator != 'undefined')
- generators.push(new mx.EsslShaderGenerator());
+ generators.push(mx.EsslShaderGenerator.create());
if (typeof mx.GlslShaderGenerator != 'undefined')
- generators.push(new mx.GlslShaderGenerator());
+ generators.push(mx.GlslShaderGenerator.create());
if (typeof mx.MslShaderGenerator != 'undefined')
- generators.push(new mx.MslShaderGenerator());
+ generators.push(mx.MslShaderGenerator.create());
if (typeof mx.OslShaderGenerator != 'undefined')
- generators.push(new mx.OslShaderGenerator());
+ generators.push(mx.OslShaderGenerator.create());
if (typeof mx.VkShaderGenerator != 'undefined')
- generators.push(new mx.VkShaderGenerator());
+ generators.push(mx.VkShaderGenerator.create());
if (typeof mx.MdlShaderGenerator != 'undefined')
- generators.push(new mx.MdlShaderGenerator());
+ generators.push(mx.MdlShaderGenerator.create());
const elem = mx.findRenderableElement(doc);
for (let gen of generators)
diff --git a/javascript/MaterialXView/source/viewer.js b/javascript/MaterialXView/source/viewer.js
index 02167b8c51..821a848ba2 100644
--- a/javascript/MaterialXView/source/viewer.js
+++ b/javascript/MaterialXView/source/viewer.js
@@ -1486,7 +1486,7 @@ export class Viewer
this.mx = mtlxIn;
// Initialize base document
- this.generator = new this.mx.EsslShaderGenerator();
+ this.generator = this.mx.EsslShaderGenerator.create();
this.genContext = new this.mx.GenContext(this.generator);
this.document = this.mx.createDocument();
diff --git a/javascript/README.md b/javascript/README.md
index 05c4c8a214..0fd3c00214 100644
--- a/javascript/README.md
+++ b/javascript/README.md
@@ -179,7 +179,7 @@ Make sure to consume `JsMaterialXGenShader.js` instead of `JsMaterialXCore.js` a
#### Generating Essl Shader Code & Compiling with WebGL
To generate WebGL 2 compatible shader code a generator context and an instance of the `EsslShaderGenerator` class is required.
```javascript
-const gen = new mx.EsslShaderGenerator();
+const gen = mx.EsslShaderGenerator.create();
const genContext = new mx.GenContext(gen);
```
The standard libraries need to be loaded and imported into the document. This step is required as the standard libraries contain all the definitions and snippets needed for assembly of the shader code.
diff --git a/python/MaterialXTest/genshader.py b/python/MaterialXTest/genshader.py
index 0b541df24d..b6a81ba4b5 100644
--- a/python/MaterialXTest/genshader.py
+++ b/python/MaterialXTest/genshader.py
@@ -54,6 +54,7 @@ def test_ShaderInterface(self):
self.assertTrue(foundTarget)
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(searchPath)
+ shadergen.registerTypeDefs(doc);
# Test generator with complete mode
context.getOptions().shaderInterfaceType = mx_gen_shader.ShaderInterfaceType.SHADER_INTERFACE_COMPLETE;
diff --git a/python/Scripts/generateshader.py b/python/Scripts/generateshader.py
index b00311eb2e..9b4cbfee9b 100644
--- a/python/Scripts/generateshader.py
+++ b/python/Scripts/generateshader.py
@@ -107,6 +107,7 @@ def main():
codeSearchPath.append(os.path.dirname(inputFilename))
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(codeSearchPath)
+ shadergen.registerTypeDefs(doc);
# If we're generating Vulkan-compliant GLSL then set the binding context
if opts.vulkanCompliantGlsl:
diff --git a/resources/Materials/TestSuite/stdlib/structs/struct_texcoord.mtlx b/resources/Materials/TestSuite/stdlib/structs/struct_texcoord.mtlx
new file mode 100644
index 0000000000..f4fde221b4
--- /dev/null
+++ b/resources/Materials/TestSuite/stdlib/structs/struct_texcoord.mtlx
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/resources/Materials/TestSuite/stdlib/structs/struct_texcoordGroup.mtlx b/resources/Materials/TestSuite/stdlib/structs/struct_texcoordGroup.mtlx
new file mode 100644
index 0000000000..aa56798831
--- /dev/null
+++ b/resources/Materials/TestSuite/stdlib/structs/struct_texcoordGroup.mtlx
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp
index 96e9dfac59..18f472a871 100644
--- a/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp
@@ -13,7 +13,7 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(EsslShaderGenerator)
{
- ems::class_>("EsslShaderGenerator")
- .smart_ptr_constructor("EsslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("EsslShaderGenerator")
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::EsslShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
index 311296790f..d6da5b35e3 100644
--- a/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
@@ -13,7 +13,7 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(GlslShaderGenerator)
{
- ems::class_>("GlslShaderGenerator")
- .smart_ptr_constructor("GlslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("GlslShaderGenerator")
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::GlslShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
index 9f075a3457..ca00a51c0f 100644
--- a/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
@@ -14,6 +14,6 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(MdlShaderGenerator)
{
ems::class_>("MdlShaderGenerator")
- .smart_ptr_constructor("MdlShaderGenerator", &std::make_shared)
- ;
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::MdlShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
index 38e0649ac9..533eafa3a7 100644
--- a/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
@@ -13,7 +13,7 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(MslShaderGenerator)
{
- ems::class_>("MslShaderGenerator")
- .smart_ptr_constructor("MslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("MslShaderGenerator")
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::MslShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
index ebafa25363..83120954ab 100644
--- a/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
@@ -14,6 +14,6 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(OslShaderGenerator)
{
ems::class_>("OslShaderGenerator")
- .smart_ptr_constructor("OslShaderGenerator", &std::make_shared)
- ;
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::OslShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/JsMaterialX/JsMaterialXGenShader/JsTypeDesc.cpp b/source/JsMaterialX/JsMaterialXGenShader/JsTypeDesc.cpp
index 92e1c27b69..cf16ce82c5 100644
--- a/source/JsMaterialX/JsMaterialXGenShader/JsTypeDesc.cpp
+++ b/source/JsMaterialX/JsMaterialXGenShader/JsTypeDesc.cpp
@@ -27,4 +27,9 @@ EMSCRIPTEN_BINDINGS(TypeDesc)
.function("isAggregate", &mx::TypeDesc::isAggregate)
.function("getName", &mx::TypeDesc::getName)
;
+
+ ems::class_("TypeSystem")
+ .smart_ptr>("TypeSystemPtr")
+ .class_function("create", &mx::TypeSystem::create)
+ ;
}
diff --git a/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
index 191f62664c..6d24e67824 100644
--- a/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
@@ -13,7 +13,7 @@ namespace mx = MaterialX;
EMSCRIPTEN_BINDINGS(VkShaderGenerator)
{
- ems::class_>("VkShaderGenerator")
- .smart_ptr_constructor("VkShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("VkShaderGenerator")
+ .constructor()
+ BIND_CLASS_FUNC("create", mx::VkShaderGenerator, create, 0, 1, mx::TypeSystemPtr);
}
diff --git a/source/MaterialXGenGlsl/EsslShaderGenerator.cpp b/source/MaterialXGenGlsl/EsslShaderGenerator.cpp
index 4e3c8528d7..b9a5780245 100644
--- a/source/MaterialXGenGlsl/EsslShaderGenerator.cpp
+++ b/source/MaterialXGenGlsl/EsslShaderGenerator.cpp
@@ -13,10 +13,10 @@ MATERIALX_NAMESPACE_BEGIN
const string EsslShaderGenerator::TARGET = "essl";
const string EsslShaderGenerator::VERSION = "300 es"; // Current target is WebGL 2.0
-EsslShaderGenerator::EsslShaderGenerator() :
- GlslShaderGenerator()
+EsslShaderGenerator::EsslShaderGenerator(TypeSystemPtr typeSystem) :
+ GlslShaderGenerator(typeSystem)
{
- _syntax = EsslSyntax::create();
+ _syntax = EsslSyntax::create(typeSystem);
// Add in ESSL specific keywords
const StringSet reservedWords = { "precision", "highp", "mediump", "lowp" };
_syntax->registerReservedWords(reservedWords);
diff --git a/source/MaterialXGenGlsl/EsslShaderGenerator.h b/source/MaterialXGenGlsl/EsslShaderGenerator.h
index c4b5703989..f9da816055 100644
--- a/source/MaterialXGenGlsl/EsslShaderGenerator.h
+++ b/source/MaterialXGenGlsl/EsslShaderGenerator.h
@@ -20,9 +20,18 @@ using EsslShaderGeneratorPtr = shared_ptr;
class MX_GENGLSL_API EsslShaderGenerator : public GlslShaderGenerator
{
public:
- EsslShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ EsslShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Return a unique identifier for the target this generator is for
const string& getTarget() const override { return TARGET; }
diff --git a/source/MaterialXGenGlsl/EsslSyntax.cpp b/source/MaterialXGenGlsl/EsslSyntax.cpp
index 8760bf1fc5..fb391645af 100644
--- a/source/MaterialXGenGlsl/EsslSyntax.cpp
+++ b/source/MaterialXGenGlsl/EsslSyntax.cpp
@@ -12,7 +12,7 @@
MATERIALX_NAMESPACE_BEGIN
-EsslSyntax::EsslSyntax()
+EsslSyntax::EsslSyntax(TypeSystemPtr typeSystem) : GlslSyntax(typeSystem)
{
}
diff --git a/source/MaterialXGenGlsl/EsslSyntax.h b/source/MaterialXGenGlsl/EsslSyntax.h
index 21e8929510..309e096477 100644
--- a/source/MaterialXGenGlsl/EsslSyntax.h
+++ b/source/MaterialXGenGlsl/EsslSyntax.h
@@ -17,9 +17,9 @@ MATERIALX_NAMESPACE_BEGIN
class MX_GENGLSL_API EsslSyntax : public GlslSyntax
{
public:
- EsslSyntax();
+ EsslSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
};
MATERIALX_NAMESPACE_END
diff --git a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp
index 3c3a56b1d7..7a083097f7 100644
--- a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp
+++ b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp
@@ -41,8 +41,8 @@ const string GlslShaderGenerator::VERSION = "400";
// GlslShaderGenerator methods
//
-GlslShaderGenerator::GlslShaderGenerator() :
- HwShaderGenerator(GlslSyntax::create())
+GlslShaderGenerator::GlslShaderGenerator(TypeSystemPtr typeSystem) :
+ HwShaderGenerator(typeSystem, GlslSyntax::create(typeSystem))
{
//
// Register all custom node implementation classes
@@ -735,7 +735,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())
{
diff --git a/source/MaterialXGenGlsl/GlslShaderGenerator.h b/source/MaterialXGenGlsl/GlslShaderGenerator.h
index c3dc78b5aa..bbe24d1061 100644
--- a/source/MaterialXGenGlsl/GlslShaderGenerator.h
+++ b/source/MaterialXGenGlsl/GlslShaderGenerator.h
@@ -22,9 +22,18 @@ using GlslShaderGeneratorPtr = shared_ptr;
class MX_GENGLSL_API GlslShaderGenerator : public HwShaderGenerator
{
public:
- GlslShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ GlslShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Generate a shader starting from the given element, translating
/// the element and all dependencies upstream into shader code.
diff --git a/source/MaterialXGenGlsl/GlslSyntax.cpp b/source/MaterialXGenGlsl/GlslSyntax.cpp
index 7054725b76..7abf472fc1 100644
--- a/source/MaterialXGenGlsl/GlslSyntax.cpp
+++ b/source/MaterialXGenGlsl/GlslSyntax.cpp
@@ -17,8 +17,8 @@ namespace
class GlslStringTypeSyntax : public StringTypeSyntax
{
public:
- GlslStringTypeSyntax() :
- StringTypeSyntax("int", "0", "0") { }
+ GlslStringTypeSyntax(const Syntax* parent) :
+ StringTypeSyntax(parent, "int", "0", "0") { }
string getValue(const Value& /*value*/, bool /*uniform*/) const override
{
@@ -29,8 +29,8 @@ class GlslStringTypeSyntax : public StringTypeSyntax
class GlslArrayTypeSyntax : public ScalarTypeSyntax
{
public:
- GlslArrayTypeSyntax(const string& name) :
- ScalarTypeSyntax(name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
+ GlslArrayTypeSyntax(const Syntax* parent, const string& name) :
+ ScalarTypeSyntax(parent, name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
{
}
@@ -51,8 +51,8 @@ class GlslArrayTypeSyntax : public ScalarTypeSyntax
class GlslFloatArrayTypeSyntax : public GlslArrayTypeSyntax
{
public:
- explicit GlslFloatArrayTypeSyntax(const string& name) :
- GlslArrayTypeSyntax(name)
+ explicit GlslFloatArrayTypeSyntax(const Syntax* parent, const string& name) :
+ GlslArrayTypeSyntax(parent, name)
{
}
@@ -67,8 +67,8 @@ class GlslFloatArrayTypeSyntax : public GlslArrayTypeSyntax
class GlslIntegerArrayTypeSyntax : public GlslArrayTypeSyntax
{
public:
- explicit GlslIntegerArrayTypeSyntax(const string& name) :
- GlslArrayTypeSyntax(name)
+ explicit GlslIntegerArrayTypeSyntax(const Syntax* parent, const string& name) :
+ GlslArrayTypeSyntax(parent, name)
{
}
@@ -96,7 +96,8 @@ const StringVec GlslSyntax::VEC4_MEMBERS = { ".x", ".y", ".z", ".w" };
// GlslSyntax methods
//
-GlslSyntax::GlslSyntax()
+GlslSyntax::GlslSyntax(TypeSystemPtr typeSystem) :
+ Syntax(typeSystem)
{
// Add in all reserved words and keywords in GLSL
registerReservedWords(
@@ -157,6 +158,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -164,11 +166,13 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -176,11 +180,13 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -188,6 +194,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -198,6 +205,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::COLOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -208,6 +216,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"vec2",
"vec2(0.0)",
"vec2(0.0)",
@@ -218,6 +227,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -228,6 +238,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -238,6 +249,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"mat3",
"mat3(1.0)",
"mat3(1.0)"));
@@ -245,17 +257,19 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::MATRIX44,
std::make_shared(
+ this,
"mat4",
"mat4(1.0)",
"mat4(1.0)"));
registerTypeSyntax(
Type::STRING,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
Type::FILENAME,
std::make_shared(
+ this,
"sampler2D",
EMPTY_STRING,
EMPTY_STRING));
@@ -263,6 +277,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"BSDF",
"BSDF(vec3(0.0),vec3(1.0))",
EMPTY_STRING,
@@ -272,6 +287,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"EDF",
"EDF(0.0)",
"EDF(0.0)",
@@ -281,6 +297,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"BSDF",
"BSDF(vec3(0.0),vec3(1.0))",
EMPTY_STRING));
@@ -288,6 +305,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"surfaceshader",
"surfaceshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -297,6 +315,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"volumeshader",
"volumeshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -306,6 +325,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"displacementshader",
"displacementshader(vec3(0.0),1.0)",
EMPTY_STRING,
@@ -315,6 +335,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"lightshader",
"lightshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -324,6 +345,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"material",
"material(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -401,10 +423,10 @@ string GlslStructTypeSyntax::getValue(const Value& value, bool /* uniform */) co
separator = ",";
const string& memberTypeName = memberValue->getTypeString();
- TypeDesc memberTypeDesc = TypeDesc::get(memberTypeName);
+ const TypeDesc memberTypeDesc = _parent->getType(memberTypeName);
// Recursively use the syntax to generate the output, so we can supported nested structs.
- result += _parentSyntax->getValue(memberTypeDesc, *memberValue, true);
+ result += _parent->getValue(memberTypeDesc, *memberValue, true);
}
result += ")";
diff --git a/source/MaterialXGenGlsl/GlslSyntax.h b/source/MaterialXGenGlsl/GlslSyntax.h
index 1e82e354bd..4e4327a962 100644
--- a/source/MaterialXGenGlsl/GlslSyntax.h
+++ b/source/MaterialXGenGlsl/GlslSyntax.h
@@ -19,9 +19,9 @@ MATERIALX_NAMESPACE_BEGIN
class MX_GENGLSL_API GlslSyntax : public Syntax
{
public:
- GlslSyntax();
+ GlslSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
const string& getInputQualifier() const override { return INPUT_QUALIFIER; }
const string& getOutputQualifier() const override { return OUTPUT_QUALIFIER; }
@@ -35,6 +35,10 @@ class MX_GENGLSL_API GlslSyntax : public Syntax
/// the shader generator. The enumeration may be converted to a different type than the input.
bool remapEnumeration(const string& value, TypeDesc type, const string& enumNames, std::pair& result) const override;
+ StructTypeSyntaxPtr createStructSyntax(const string& structTypeName, const string& defaultValue,
+ const string& uniformDefaultValue, const string& typeAlias,
+ const string& typeDefinition) const override;
+
static const string INPUT_QUALIFIER;
static const string OUTPUT_QUALIFIER;
static const string UNIFORM_QUALIFIER;
@@ -45,11 +49,6 @@ class MX_GENGLSL_API GlslSyntax : public Syntax
static const StringVec VEC2_MEMBERS;
static const StringVec VEC3_MEMBERS;
static const StringVec VEC4_MEMBERS;
-
- protected:
- StructTypeSyntaxPtr createStructSyntax(const string& structTypeName, const string& defaultValue,
- const string& uniformDefaultValue, const string& typeAlias,
- const string& typeDefinition) const override;
};
/// Specialization of TypeSyntax for aggregate types.
diff --git a/source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp
index 31b1b6758b..b53a1ef7d2 100644
--- a/source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp
+++ b/source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp
@@ -34,7 +34,8 @@ void LightCompoundNodeGlsl::initialize(const InterfaceElement& element, GenConte
NodeDefPtr nodeDef = graph.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
- _lightUniforms.add(TypeDesc::get(input->getType()), input->getName());
+ const TypeDesc type = context.getTypeDesc(input->getType());
+ _lightUniforms.add(type, input->getName());
}
}
diff --git a/source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp
index c7b4df2bb6..0f3fb71ef9 100644
--- a/source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp
+++ b/source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp
@@ -44,7 +44,8 @@ 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());
+ const TypeDesc type = context.getTypeDesc(input->getType());
+ _lightUniforms.add(type, input->getName(), input->getValue());
}
}
diff --git a/source/MaterialXGenGlsl/VkShaderGenerator.cpp b/source/MaterialXGenGlsl/VkShaderGenerator.cpp
index 5a91d96ff5..5ce890ef90 100644
--- a/source/MaterialXGenGlsl/VkShaderGenerator.cpp
+++ b/source/MaterialXGenGlsl/VkShaderGenerator.cpp
@@ -11,10 +11,10 @@ MATERIALX_NAMESPACE_BEGIN
const string VkShaderGenerator::TARGET = "genglsl";
const string VkShaderGenerator::VERSION = "450";
-VkShaderGenerator::VkShaderGenerator() :
- GlslShaderGenerator()
+VkShaderGenerator::VkShaderGenerator(TypeSystemPtr typeSystem) :
+ GlslShaderGenerator(typeSystem)
{
- _syntax = VkSyntax::create();
+ _syntax = VkSyntax::create(typeSystem);
// Add in Vulkan specific keywords
const StringSet reservedWords = { "texture2D", "sampler" };
_syntax->registerReservedWords(reservedWords);
diff --git a/source/MaterialXGenGlsl/VkShaderGenerator.h b/source/MaterialXGenGlsl/VkShaderGenerator.h
index d13957836d..8c0e809600 100644
--- a/source/MaterialXGenGlsl/VkShaderGenerator.h
+++ b/source/MaterialXGenGlsl/VkShaderGenerator.h
@@ -21,9 +21,18 @@ using VkShaderGeneratorPtr = shared_ptr;
class MX_GENGLSL_API VkShaderGenerator : public GlslShaderGenerator
{
public:
- VkShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ VkShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Return a unique identifier for the target this generator is for
const string& getTarget() const override { return TARGET; }
diff --git a/source/MaterialXGenGlsl/VkSyntax.cpp b/source/MaterialXGenGlsl/VkSyntax.cpp
index 6ff52a8648..563190ad51 100644
--- a/source/MaterialXGenGlsl/VkSyntax.cpp
+++ b/source/MaterialXGenGlsl/VkSyntax.cpp
@@ -7,7 +7,7 @@
MATERIALX_NAMESPACE_BEGIN
-VkSyntax::VkSyntax()
+VkSyntax::VkSyntax(TypeSystemPtr typeSystem) : GlslSyntax(typeSystem)
{
}
diff --git a/source/MaterialXGenGlsl/VkSyntax.h b/source/MaterialXGenGlsl/VkSyntax.h
index a7d512ee95..ff0d277638 100644
--- a/source/MaterialXGenGlsl/VkSyntax.h
+++ b/source/MaterialXGenGlsl/VkSyntax.h
@@ -17,9 +17,9 @@ MATERIALX_NAMESPACE_BEGIN
class MX_GENGLSL_API VkSyntax : public GlslSyntax
{
public:
- VkSyntax();
+ VkSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
const string& getInputQualifier() const override { return INPUT_QUALIFIER; }
};
diff --git a/source/MaterialXGenMdl/MdlShaderGenerator.cpp b/source/MaterialXGenMdl/MdlShaderGenerator.cpp
index 0f983cf8f2..566de99277 100644
--- a/source/MaterialXGenMdl/MdlShaderGenerator.cpp
+++ b/source/MaterialXGenMdl/MdlShaderGenerator.cpp
@@ -82,10 +82,18 @@ const std::unordered_map MdlShaderGenerator::GEOMPROP_DEFINITION
// MdlShaderGenerator methods
//
-MdlShaderGenerator::MdlShaderGenerator() :
- ShaderGenerator(MdlSyntax::create())
+MdlShaderGenerator::MdlShaderGenerator(TypeSystemPtr typeSystem) :
+ ShaderGenerator(typeSystem, MdlSyntax::create(typeSystem))
{
- // Register built-in implementations
+ // Register custom types to handle enumeration output
+ _typeSystem->registerType(Type::MDL_COORDINATESPACE);
+ _typeSystem->registerType(Type::MDL_ADDRESSMODE);
+ _typeSystem->registerType(Type::MDL_FILTERLOOKUPMODE);
+ _typeSystem->registerType(Type::MDL_FILTERTYPE);
+ _typeSystem->registerType(Type::MDL_DISTRIBUTIONTYPE);
+ _typeSystem->registerType(Type::MDL_SCATTER_MODE);
+
+ // Register build-in implementations
//
registerImplementation("IM_surfacematerial_" + MdlShaderGenerator::TARGET, MaterialNodeMdl::create);
@@ -352,7 +360,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 = _typeSystem->getType(outputs[0]->getType());
if (implElement->isA())
{
diff --git a/source/MaterialXGenMdl/MdlShaderGenerator.h b/source/MaterialXGenMdl/MdlShaderGenerator.h
index ac64e7c882..da54636178 100644
--- a/source/MaterialXGenMdl/MdlShaderGenerator.h
+++ b/source/MaterialXGenMdl/MdlShaderGenerator.h
@@ -53,9 +53,18 @@ using MdlShaderGeneratorPtr = shared_ptr;
class MX_GENMDL_API MdlShaderGenerator : public ShaderGenerator
{
public:
- MdlShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ MdlShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Return a unique identifier for the target this generator is for
const string& getTarget() const override { return TARGET; }
diff --git a/source/MaterialXGenMdl/MdlSyntax.cpp b/source/MaterialXGenMdl/MdlSyntax.cpp
index f8c99bb286..c08556cc04 100644
--- a/source/MaterialXGenMdl/MdlSyntax.cpp
+++ b/source/MaterialXGenMdl/MdlSyntax.cpp
@@ -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
{
@@ -34,8 +21,8 @@ const string MARKER_MDL_VERSION_SUFFIX = "MDL_VERSION_SUFFIX";
class MdlFilenameTypeSyntax : public ScalarTypeSyntax
{
public:
- MdlFilenameTypeSyntax() :
- ScalarTypeSyntax("texture_2d", "texture_2d()", "texture_2d()")
+ MdlFilenameTypeSyntax(const Syntax* parent) :
+ ScalarTypeSyntax(parent, "texture_2d", "texture_2d()", "texture_2d()")
{
}
@@ -81,8 +68,8 @@ class MdlFilenameTypeSyntax : public ScalarTypeSyntax
class MdlArrayTypeSyntax : public ScalarTypeSyntax
{
public:
- MdlArrayTypeSyntax(const string& name) :
- ScalarTypeSyntax(name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
+ MdlArrayTypeSyntax(const Syntax* parent, const string& name) :
+ ScalarTypeSyntax(parent, name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
{
}
@@ -102,8 +89,8 @@ class MdlArrayTypeSyntax : public ScalarTypeSyntax
class MdlFloatArrayTypeSyntax : public MdlArrayTypeSyntax
{
public:
- explicit MdlFloatArrayTypeSyntax(const string& name) :
- MdlArrayTypeSyntax(name)
+ explicit MdlFloatArrayTypeSyntax(const Syntax* parent, const string& name) :
+ MdlArrayTypeSyntax(parent, name)
{
}
@@ -118,8 +105,8 @@ class MdlFloatArrayTypeSyntax : public MdlArrayTypeSyntax
class MdlIntegerArrayTypeSyntax : public MdlArrayTypeSyntax
{
public:
- explicit MdlIntegerArrayTypeSyntax(const string& name) :
- MdlArrayTypeSyntax(name)
+ explicit MdlIntegerArrayTypeSyntax(const Syntax* parent, const string& name) :
+ MdlArrayTypeSyntax(parent, name)
{
}
@@ -141,8 +128,8 @@ class MdlIntegerArrayTypeSyntax : public MdlArrayTypeSyntax
class MdlColor4TypeSyntax : public AggregateTypeSyntax
{
public:
- MdlColor4TypeSyntax() :
- AggregateTypeSyntax("color4", "mk_color4(0.0)", "mk_color4(0.0)",
+ MdlColor4TypeSyntax(const Syntax* parent) :
+ AggregateTypeSyntax(parent, "color4", "mk_color4(0.0)", "mk_color4(0.0)",
EMPTY_STRING, EMPTY_STRING, MdlSyntax::COLOR4_MEMBERS)
{
}
@@ -168,8 +155,8 @@ class MdlColor4TypeSyntax : public AggregateTypeSyntax
class MdlEnumSyntax : public AggregateTypeSyntax
{
public:
- MdlEnumSyntax(const string& name, const string& defaultValue, const string& defaultUniformValue, const StringVec& members) :
- AggregateTypeSyntax(name, defaultValue, defaultUniformValue, EMPTY_STRING, EMPTY_STRING, members)
+ MdlEnumSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& defaultUniformValue, const StringVec& members) :
+ AggregateTypeSyntax(parent, name, defaultValue, defaultUniformValue, EMPTY_STRING, EMPTY_STRING, members)
{
}
@@ -203,7 +190,7 @@ const string MdlSyntax::PORT_NAME_PREFIX = "mxp_";
// MdlSyntax methods
//
-MdlSyntax::MdlSyntax()
+MdlSyntax::MdlSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
{
// Add in all reserved words and keywords in MDL
// Formatted as in the MDL Specification 1.9.2 for easy comparing
@@ -253,6 +240,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -260,11 +248,13 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -272,11 +262,13 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -284,6 +276,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"color",
"color(0.0)",
"color(0.0)",
@@ -293,11 +286,12 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::COLOR4,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"float2",
"float2(0.0)",
"float2(0.0)",
@@ -308,6 +302,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"float3",
"float3(0.0)",
"float3(0.0)",
@@ -318,6 +313,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"float4",
"float4(0.0)",
"float4(0.0)",
@@ -328,6 +324,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"float3x3",
"float3x3(1.0)",
"float3x3(1.0)"));
@@ -335,6 +332,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATRIX44,
std::make_shared(
+ this,
"float4x4",
"float4x4(1.0)",
"float4x4(1.0)"));
@@ -342,17 +340,19 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::STRING,
std::make_shared(
+ this,
"string",
"\"\"",
"\"\""));
registerTypeSyntax(
Type::FILENAME,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -360,6 +360,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -367,6 +368,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -374,6 +376,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -381,6 +384,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -388,6 +392,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -395,6 +400,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -402,6 +408,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -409,6 +416,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_ADDRESSMODE,
std::make_shared(
+ this,
"mx_addressmode_type",
"mx_addressmode_type_periodic",
"mx_addressmode_type_periodic",
@@ -417,6 +425,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_COORDINATESPACE,
std::make_shared(
+ this,
"mx_coordinatespace_type",
"mx_coordinatespace_type_model",
"mx_coordinatespace_type_model",
@@ -425,6 +434,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_FILTERLOOKUPMODE,
std::make_shared(
+ this,
"mx_filterlookup_type",
"mx_filterlookup_type_linear",
"mx_filterlookup_type_linear",
@@ -433,6 +443,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_FILTERTYPE,
std::make_shared(
+ this,
"mx_filter_type",
"mx_filter_type_gaussian",
"mx_filter_type_gaussian",
@@ -441,6 +452,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_DISTRIBUTIONTYPE,
std::make_shared(
+ this,
"mx_distribution_type",
"mx_distribution_type_ggx",
"mx_distribution_type_ggx",
@@ -449,6 +461,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_SCATTER_MODE,
std::make_shared(
+ this,
"mx_scatter_mode",
"mx_scatter_mode_R",
"mx_scatter_mode_R",
diff --git a/source/MaterialXGenMdl/MdlSyntax.h b/source/MaterialXGenMdl/MdlSyntax.h
index a3a7aee6e1..d12ddb230e 100644
--- a/source/MaterialXGenMdl/MdlSyntax.h
+++ b/source/MaterialXGenMdl/MdlSyntax.h
@@ -25,9 +25,9 @@ using MdlSyntaxPtr = shared_ptr;
class MX_GENMDL_API MdlSyntax : public Syntax
{
public:
- MdlSyntax();
+ MdlSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
const string& getConstantQualifier() const override { return CONST_QUALIFIER; };
const string& getUniformQualifier() const override { return UNIFORM_QUALIFIER; };
diff --git a/source/MaterialXGenMsl/MslShaderGenerator.cpp b/source/MaterialXGenMsl/MslShaderGenerator.cpp
index b61f62131e..b5a6177a1c 100644
--- a/source/MaterialXGenMsl/MslShaderGenerator.cpp
+++ b/source/MaterialXGenMsl/MslShaderGenerator.cpp
@@ -45,8 +45,8 @@ const string MslShaderGenerator::VERSION = "2.3";
// MslShaderGenerator methods
//
-MslShaderGenerator::MslShaderGenerator() :
- HwShaderGenerator(MslSyntax::create())
+MslShaderGenerator::MslShaderGenerator(TypeSystemPtr typeSystem) :
+ HwShaderGenerator(typeSystem, MslSyntax::create(typeSystem))
{
//
// Register all custom node implementation classes
@@ -1250,7 +1250,7 @@ ShaderNodeImplPtr MslShaderGenerator::getImplementation(const NodeDef& nodedef,
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}
- const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
+ const TypeDesc outputType = _typeSystem->getType(outputs[0]->getType());
if (implElement->isA())
{
diff --git a/source/MaterialXGenMsl/MslShaderGenerator.h b/source/MaterialXGenMsl/MslShaderGenerator.h
index 6796f545d3..62d2dc4f9c 100644
--- a/source/MaterialXGenMsl/MslShaderGenerator.h
+++ b/source/MaterialXGenMsl/MslShaderGenerator.h
@@ -25,9 +25,18 @@ using MslShaderGeneratorPtr = shared_ptr;
class MX_GENMSL_API MslShaderGenerator : public HwShaderGenerator
{
public:
- MslShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ MslShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Generate a shader starting from the given element, translating
/// the element and all dependencies upstream into shader code.
diff --git a/source/MaterialXGenMsl/MslSyntax.cpp b/source/MaterialXGenMsl/MslSyntax.cpp
index 30f44c5d1c..81b427259c 100644
--- a/source/MaterialXGenMsl/MslSyntax.cpp
+++ b/source/MaterialXGenMsl/MslSyntax.cpp
@@ -17,8 +17,8 @@ namespace
class MslStringTypeSyntax : public StringTypeSyntax
{
public:
- MslStringTypeSyntax() :
- StringTypeSyntax("int", "0", "0") { }
+ MslStringTypeSyntax(const Syntax* parent) :
+ StringTypeSyntax(parent, "int", "0", "0") { }
string getValue(const Value& /*value*/, bool /*uniform*/) const override
{
@@ -29,8 +29,8 @@ class MslStringTypeSyntax : public StringTypeSyntax
class MslArrayTypeSyntax : public ScalarTypeSyntax
{
public:
- MslArrayTypeSyntax(const string& name) :
- ScalarTypeSyntax(name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
+ MslArrayTypeSyntax(const Syntax* parent, const string& name) :
+ ScalarTypeSyntax(parent, name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
{
}
@@ -51,8 +51,8 @@ class MslArrayTypeSyntax : public ScalarTypeSyntax
class MslFloatArrayTypeSyntax : public MslArrayTypeSyntax
{
public:
- explicit MslFloatArrayTypeSyntax(const string& name) :
- MslArrayTypeSyntax(name)
+ explicit MslFloatArrayTypeSyntax(const Syntax* parent, const string& name) :
+ MslArrayTypeSyntax(parent, name)
{
}
@@ -67,8 +67,8 @@ class MslFloatArrayTypeSyntax : public MslArrayTypeSyntax
class MslIntegerArrayTypeSyntax : public MslArrayTypeSyntax
{
public:
- explicit MslIntegerArrayTypeSyntax(const string& name) :
- MslArrayTypeSyntax(name)
+ explicit MslIntegerArrayTypeSyntax(const Syntax* parent, const string& name) :
+ MslArrayTypeSyntax(parent, name)
{
}
@@ -97,7 +97,7 @@ const StringVec MslSyntax::VEC4_MEMBERS = { ".x", ".y", ".z", ".w" };
// MslSyntax methods
//
-MslSyntax::MslSyntax()
+MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
{
// Add in all reserved words and keywords in MSL
registerReservedWords(
@@ -139,6 +139,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -146,11 +147,13 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -158,11 +161,13 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -170,6 +175,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -180,6 +186,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::COLOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -190,6 +197,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"vec2",
"vec2(0.0)",
"vec2(0.0)",
@@ -200,6 +208,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -210,6 +219,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -220,6 +230,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"mat3",
"mat3(1.0)",
"mat3(1.0)"));
@@ -227,17 +238,19 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::MATRIX44,
std::make_shared(
+ this,
"mat4",
"mat4(1.0)",
"mat4(1.0)"));
registerTypeSyntax(
Type::STRING,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
Type::FILENAME,
std::make_shared(
+ this,
"MetalTexture",
EMPTY_STRING,
EMPTY_STRING));
@@ -245,6 +258,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"BSDF",
"BSDF{float3(0.0),float3(1.0)}",
EMPTY_STRING,
@@ -254,6 +268,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"EDF",
"EDF(0.0)",
"EDF(0.0)",
@@ -263,6 +278,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"BSDF",
"BSDF{float3(0.0),float3(1.0)}",
EMPTY_STRING));
@@ -270,6 +286,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"surfaceshader",
"surfaceshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -279,6 +296,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"volumeshader",
"volumeshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -288,6 +306,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"displacementshader",
"displacementshader{float3(0.0),1.0}",
EMPTY_STRING,
@@ -297,6 +316,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"lightshader",
"lightshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -306,6 +326,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"material",
"material{float3(0.0),float3(0.0)}",
EMPTY_STRING,
diff --git a/source/MaterialXGenMsl/MslSyntax.h b/source/MaterialXGenMsl/MslSyntax.h
index 22469e2193..1836a7503e 100644
--- a/source/MaterialXGenMsl/MslSyntax.h
+++ b/source/MaterialXGenMsl/MslSyntax.h
@@ -19,9 +19,9 @@ MATERIALX_NAMESPACE_BEGIN
class MX_GENMSL_API MslSyntax : public Syntax
{
public:
- MslSyntax();
+ MslSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
const string& getInputQualifier() const override { return INPUT_QUALIFIER; }
const string& getOutputQualifier() const override { return OUTPUT_QUALIFIER; }
diff --git a/source/MaterialXGenMsl/Nodes/LightCompoundNodeMsl.cpp b/source/MaterialXGenMsl/Nodes/LightCompoundNodeMsl.cpp
index d7d5d8ceaa..d0ee8a4dee 100644
--- a/source/MaterialXGenMsl/Nodes/LightCompoundNodeMsl.cpp
+++ b/source/MaterialXGenMsl/Nodes/LightCompoundNodeMsl.cpp
@@ -34,7 +34,7 @@ void LightCompoundNodeMsl::initialize(const InterfaceElement& element, GenContex
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());
}
}
diff --git a/source/MaterialXGenMsl/Nodes/LightShaderNodeMsl.cpp b/source/MaterialXGenMsl/Nodes/LightShaderNodeMsl.cpp
index b556415dae..1c3bd40a21 100644
--- a/source/MaterialXGenMsl/Nodes/LightShaderNodeMsl.cpp
+++ b/source/MaterialXGenMsl/Nodes/LightShaderNodeMsl.cpp
@@ -44,7 +44,8 @@ void LightShaderNodeMsl::initialize(const InterfaceElement& element, GenContext&
NodeDefPtr nodeDef = impl.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
- _lightUniforms.add(TypeDesc::get(input->getType()), input->getName(), input->getValue());
+ const TypeDesc type = context.getTypeDesc(input->getType());
+ _lightUniforms.add(type, input->getName(), input->getValue());
}
}
diff --git a/source/MaterialXGenOsl/OslShaderGenerator.cpp b/source/MaterialXGenOsl/OslShaderGenerator.cpp
index 75cfd47f3b..774c15bd41 100644
--- a/source/MaterialXGenOsl/OslShaderGenerator.cpp
+++ b/source/MaterialXGenOsl/OslShaderGenerator.cpp
@@ -24,8 +24,8 @@ const string OslShaderGenerator::TARGET = "genosl";
// OslShaderGenerator methods
//
-OslShaderGenerator::OslShaderGenerator() :
- ShaderGenerator(OslSyntax::create())
+OslShaderGenerator::OslShaderGenerator(TypeSystemPtr typeSystem) :
+ ShaderGenerator(typeSystem, OslSyntax::create(typeSystem))
{
// Register built-in implementations
diff --git a/source/MaterialXGenOsl/OslShaderGenerator.h b/source/MaterialXGenOsl/OslShaderGenerator.h
index e5cf13977e..d81c19e2c0 100644
--- a/source/MaterialXGenOsl/OslShaderGenerator.h
+++ b/source/MaterialXGenOsl/OslShaderGenerator.h
@@ -23,9 +23,18 @@ using OslShaderGeneratorPtr = shared_ptr;
class MX_GENOSL_API OslShaderGenerator : public ShaderGenerator
{
public:
- OslShaderGenerator();
-
- static ShaderGeneratorPtr create() { return std::make_shared(); }
+ /// Constructor.
+ OslShaderGenerator(TypeSystemPtr typeSystem);
+
+ /// Creator function.
+ /// If a TypeSystem is not provided it will be created internally.
+ /// Optionally pass in an externally created TypeSystem here,
+ /// if you want to keep type descriptions alive after the lifetime
+ /// of the shader generator.
+ static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
+ {
+ return std::make_shared(typeSystem ? typeSystem : TypeSystem::create());
+ }
/// Return a unique identifier for the target this generator is for
const string& getTarget() const override { return TARGET; }
diff --git a/source/MaterialXGenOsl/OslSyntax.cpp b/source/MaterialXGenOsl/OslSyntax.cpp
index a85821c4fd..4d5651d375 100644
--- a/source/MaterialXGenOsl/OslSyntax.cpp
+++ b/source/MaterialXGenOsl/OslSyntax.cpp
@@ -17,8 +17,8 @@ namespace
class OslBooleanTypeSyntax : public ScalarTypeSyntax
{
public:
- OslBooleanTypeSyntax() :
- ScalarTypeSyntax("int", "0", "0", EMPTY_STRING, "#define true 1\n#define false 0")
+ OslBooleanTypeSyntax(const Syntax* parent) :
+ ScalarTypeSyntax(parent, "int", "0", "0", EMPTY_STRING, "#define true 1\n#define false 0")
{
}
@@ -31,8 +31,8 @@ class OslBooleanTypeSyntax : public ScalarTypeSyntax
class OslArrayTypeSyntax : public ScalarTypeSyntax
{
public:
- OslArrayTypeSyntax(const string& name) :
- ScalarTypeSyntax(name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
+ OslArrayTypeSyntax(const Syntax* parent, const string& name) :
+ ScalarTypeSyntax(parent, name, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING)
{
}
@@ -57,8 +57,8 @@ class OslArrayTypeSyntax : public ScalarTypeSyntax
class OslFloatArrayTypeSyntax : public OslArrayTypeSyntax
{
public:
- explicit OslFloatArrayTypeSyntax(const string& name) :
- OslArrayTypeSyntax(name)
+ explicit OslFloatArrayTypeSyntax(const Syntax* parent, const string& name) :
+ OslArrayTypeSyntax(parent, name)
{
}
@@ -73,8 +73,8 @@ class OslFloatArrayTypeSyntax : public OslArrayTypeSyntax
class OslIntegerArrayTypeSyntax : public OslArrayTypeSyntax
{
public:
- explicit OslIntegerArrayTypeSyntax(const string& name) :
- OslArrayTypeSyntax(name)
+ explicit OslIntegerArrayTypeSyntax(const Syntax* parent, const string& name) :
+ OslArrayTypeSyntax(parent, name)
{
}
@@ -91,10 +91,10 @@ class OslIntegerArrayTypeSyntax : public OslArrayTypeSyntax
class OslStructTypeSyntax : public AggregateTypeSyntax
{
public:
- OslStructTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
+ OslStructTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
const StringVec& members = EMPTY_MEMBERS) :
- AggregateTypeSyntax(name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
+ AggregateTypeSyntax(parent, name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
{
}
@@ -121,8 +121,8 @@ class OslStructTypeSyntax : public AggregateTypeSyntax
class OslColor4TypeSyntax : public OslStructTypeSyntax
{
public:
- OslColor4TypeSyntax() :
- OslStructTypeSyntax("color4", "color4(color(0.0), 0.0)", "{color(0.0), 0.0}", EMPTY_STRING, EMPTY_STRING, OslSyntax::COLOR4_MEMBERS)
+ OslColor4TypeSyntax(const Syntax* parent) :
+ OslStructTypeSyntax(parent, "color4", "color4(color(0.0), 0.0)", "{color(0.0), 0.0}", EMPTY_STRING, EMPTY_STRING, OslSyntax::COLOR4_MEMBERS)
{
}
@@ -155,10 +155,10 @@ class OslColor4TypeSyntax : public OslStructTypeSyntax
class OSLMatrix3TypeSyntax : public AggregateTypeSyntax
{
public:
- OSLMatrix3TypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
+ OSLMatrix3TypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
const StringVec& members = EMPTY_MEMBERS) :
- AggregateTypeSyntax(name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
+ AggregateTypeSyntax(parent, name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
{
}
@@ -193,10 +193,10 @@ class OSLMatrix3TypeSyntax : public AggregateTypeSyntax
class OSLFilenameTypeSyntax : public AggregateTypeSyntax
{
public:
- OSLFilenameTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
+ OSLFilenameTypeSyntax(const Syntax* parent, const string& name, const string& defaultValue, const string& uniformDefaultValue,
const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
const StringVec& members = EMPTY_MEMBERS) :
- AggregateTypeSyntax(name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
+ AggregateTypeSyntax(parent, name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
{
}
@@ -234,7 +234,7 @@ const StringVec OslSyntax::COLOR4_MEMBERS = { ".rgb[0]", ".rgb[1]", ".rgb[2]", "
// OslSyntax methods
//
-OslSyntax::OslSyntax()
+OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
{
// Add in all reserved words and keywords in OSL
registerReservedWords(
@@ -268,6 +268,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -275,11 +276,13 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -287,17 +290,19 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
// Note: the color type in OSL is a built in type and
// should not use the custom OslStructTypeSyntax.
Type::COLOR3,
std::make_shared(
+ this,
"color",
"color(0.0)",
"color(0.0)",
@@ -307,11 +312,12 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::COLOR4,
- std::make_shared());
+ std::make_shared(this));
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"vector2",
"vector2(0.0, 0.0)",
"{0.0, 0.0}",
@@ -324,6 +330,7 @@ OslSyntax::OslSyntax()
// should not use the custom OslStructTypeSyntax.
Type::VECTOR3,
std::make_shared(
+ this,
"vector",
"vector(0.0)",
"vector(0.0)",
@@ -334,6 +341,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"vector4",
"vector4(0.0, 0.0, 0.0, 0.0)",
"{0.0, 0.0, 0.0, 0.0}",
@@ -344,6 +352,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"matrix",
"matrix(1.0)",
"matrix(1.0)"));
@@ -351,6 +360,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::MATRIX44,
std::make_shared(
+ this,
"matrix",
"matrix(1.0)",
"matrix(1.0)"));
@@ -358,6 +368,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::STRING,
std::make_shared(
+ this,
"string",
"\"\"",
"\"\""));
@@ -365,6 +376,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::FILENAME,
std::make_shared(
+ this,
"textureresource ",
"textureresource (\"\", \"\")",
"(\"\", \"\")",
@@ -374,6 +386,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"BSDF",
"null_closure",
"0",
@@ -383,6 +396,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"EDF",
"null_closure",
"0",
@@ -392,6 +406,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"VDF",
"null_closure",
"0",
@@ -401,6 +416,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"surfaceshader",
"surfaceshader(null_closure, null_closure, 1.0)",
"{ 0, 0, 1.0 }",
@@ -410,6 +426,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"volumeshader",
"null_closure",
"0",
@@ -419,6 +436,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"displacementshader",
"vector(0.0)",
"vector(0.0)",
@@ -428,6 +446,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"lightshader",
"null_closure",
"0",
@@ -437,6 +456,7 @@ OslSyntax::OslSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"MATERIAL",
"null_closure",
"0",
diff --git a/source/MaterialXGenOsl/OslSyntax.h b/source/MaterialXGenOsl/OslSyntax.h
index 5d33524c10..aad280a5cd 100644
--- a/source/MaterialXGenOsl/OslSyntax.h
+++ b/source/MaterialXGenOsl/OslSyntax.h
@@ -20,9 +20,9 @@ MATERIALX_NAMESPACE_BEGIN
class MX_GENOSL_API OslSyntax : public Syntax
{
public:
- OslSyntax();
+ OslSyntax(TypeSystemPtr typeSystem);
- static SyntaxPtr create() { return std::make_shared(); }
+ static SyntaxPtr create(TypeSystemPtr typeSystem) { return std::make_shared(typeSystem); }
const string& getOutputQualifier() const override;
const string& getConstantQualifier() const override { return EMPTY_STRING; };
diff --git a/source/MaterialXGenShader/GenContext.h b/source/MaterialXGenShader/GenContext.h
index 0f565ded6e..733d8b98ce 100644
--- a/source/MaterialXGenShader/GenContext.h
+++ b/source/MaterialXGenShader/GenContext.h
@@ -14,6 +14,7 @@
#include
#include
#include
+#include
#include
@@ -51,6 +52,12 @@ class MX_GENSHADER_API GenContext
return _options;
}
+ /// Return a TypeDesc for the given type name.
+ TypeDesc getTypeDesc(const string& name) const
+ {
+ return _sg->getTypeSystem()->getType(name);
+ }
+
/// Register a user search path for finding source code during
/// code generation.
void registerSourceCodeSearchPath(const FilePath& path)
diff --git a/source/MaterialXGenShader/HwShaderGenerator.cpp b/source/MaterialXGenShader/HwShaderGenerator.cpp
index d9862650e3..b3a3f3e132 100644
--- a/source/MaterialXGenShader/HwShaderGenerator.cpp
+++ b/source/MaterialXGenShader/HwShaderGenerator.cpp
@@ -181,8 +181,8 @@ const string HwShaderGenerator::CLOSURE_CONTEXT_SUFFIX_REFLECTION("_reflection")
const string HwShaderGenerator::CLOSURE_CONTEXT_SUFFIX_TRANSMISSION("_transmission");
const string HwShaderGenerator::CLOSURE_CONTEXT_SUFFIX_INDIRECT("_indirect");
-HwShaderGenerator::HwShaderGenerator(SyntaxPtr syntax) :
- ShaderGenerator(syntax),
+HwShaderGenerator::HwShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax) :
+ ShaderGenerator(typeSystem, syntax),
_defDefault(HwShaderGenerator::ClosureContextType::DEFAULT),
_defReflection(HwShaderGenerator::ClosureContextType::REFLECTION),
_defTransmission(HwShaderGenerator::ClosureContextType::TRANSMISSION),
@@ -544,7 +544,7 @@ void HwShaderGenerator::emitFunctionCall(const ShaderNode& node, GenContext& con
void HwShaderGenerator::bindLightShader(const NodeDef& nodeDef, unsigned int lightTypeId, GenContext& context)
{
- if (TypeDesc::get(nodeDef.getType()) != Type::LIGHTSHADER)
+ if (context.getTypeDesc(nodeDef.getType()) != Type::LIGHTSHADER)
{
throw ExceptionShaderGenError("Error binding light shader. Given nodedef '" + nodeDef.getName() + "' is not of lightshader type");
}
diff --git a/source/MaterialXGenShader/HwShaderGenerator.h b/source/MaterialXGenShader/HwShaderGenerator.h
index d78aaf917a..8d54cd39bb 100644
--- a/source/MaterialXGenShader/HwShaderGenerator.h
+++ b/source/MaterialXGenShader/HwShaderGenerator.h
@@ -330,7 +330,7 @@ class MX_GENSHADER_API HwShaderGenerator : public ShaderGenerator
static const string CLOSURE_CONTEXT_SUFFIX_INDIRECT;
protected:
- HwShaderGenerator(SyntaxPtr syntax);
+ HwShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax);
/// Create and initialize a new HW shader for shader generation.
virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
diff --git a/source/MaterialXGenShader/ShaderGenerator.cpp b/source/MaterialXGenShader/ShaderGenerator.cpp
index 398232bf39..e62fd1e1a6 100644
--- a/source/MaterialXGenShader/ShaderGenerator.cpp
+++ b/source/MaterialXGenShader/ShaderGenerator.cpp
@@ -29,7 +29,8 @@ const string ShaderGenerator::T_FILE_TRANSFORM_UV = "$fileTransformUv";
// ShaderGenerator methods
//
-ShaderGenerator::ShaderGenerator(SyntaxPtr syntax) :
+ShaderGenerator::ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax) :
+ _typeSystem(typeSystem),
_syntax(syntax)
{
}
@@ -300,7 +301,7 @@ ShaderNodeImplPtr ShaderGenerator::getImplementation(const NodeDef& nodedef, Gen
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}
- const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
+ const TypeDesc outputType = _typeSystem->getType(outputs[0]->getType());
if (implElement->isA())
{
@@ -344,45 +345,72 @@ ShaderNodeImplPtr ShaderGenerator::getImplementation(const NodeDef& nodedef, Gen
return impl;
}
-/// Load any struct type definitions from the document in to the type cache.
-void ShaderGenerator::loadStructTypeDefs(const DocumentPtr& doc)
+void ShaderGenerator::registerTypeDefs(const DocumentPtr& doc)
{
+ /// Load any struct type definitions from the document.
for (const auto& mxTypeDef : doc->getTypeDefs())
{
- const auto& typeDefName = mxTypeDef->getName();
+ const string& typeName = mxTypeDef->getName();
const auto& members = mxTypeDef->getMembers();
// If we don't have any member children then we're not going to consider ourselves a struct.
if (members.empty())
+ {
continue;
+ }
- StructTypeDesc newStructTypeDesc;
+ auto structMembers = std::make_shared();
for (const auto& member : members)
{
- auto memberName = member->getName();
- auto memberTypeName = member->getType();
- auto memberType = TypeDesc::get(memberTypeName);
- auto memberDefaultValue = member->getValueString();
+ const auto memberType = _typeSystem->getType(member->getType());
+ const auto memberName = member->getName();
+ const auto memberDefaultValue = member->getValueString();
+ structMembers->emplace_back(StructMemberDesc(memberType, memberName, memberDefaultValue));
+ }
+
+ _typeSystem->registerType(typeName, TypeDesc::BASETYPE_STRUCT, TypeDesc::SEMANTIC_NONE, 1, structMembers);
+ }
- newStructTypeDesc.addMember(memberName, memberType, memberDefaultValue);
+ // Create a type syntax for all struct types loaded above.
+ for (TypeDesc typeDesc : _typeSystem->getTypes())
+ {
+ if (!typeDesc.isStruct())
+ {
+ continue;
}
- auto structIndex = StructTypeDesc::emplace_back(newStructTypeDesc);
+ const string& structTypeName = typeDesc.getName();
+ string defaultValue = structTypeName + "( ";
+ string uniformDefaultValue = EMPTY_STRING;
+ string typeAlias = EMPTY_STRING;
+ string typeDefinition = "struct " + structTypeName + " { ";
- TypeDesc structTypeDesc(typeDefName, TypeDesc::BASETYPE_STRUCT, TypeDesc::SEMANTIC_NONE, 1, structIndex);
+ auto structMembers = typeDesc.getStructMembers();
+ if (structMembers)
+ {
+ for (const auto& structMember : *structMembers)
+ {
+ const string& memberType = structMember.getType().getName();
+ const string& memberName = structMember.getName();
+ const string& memberDefaultValue = structMember.getDefaultValueStr();
- TypeDescRegistry(structTypeDesc, typeDefName);
+ defaultValue += memberDefaultValue + ", ";
+ typeDefinition += memberType + " " + memberName + "; ";
+ }
+ }
- StructTypeDesc::get(structIndex).setTypeDesc(TypeDesc::get(typeDefName));
- }
+ typeDefinition += " };";
+ defaultValue += " )";
- _syntax->registerStructTypeDescSyntax();
-}
+ StructTypeSyntaxPtr structTypeSyntax = _syntax->createStructSyntax(
+ structTypeName,
+ defaultValue,
+ uniformDefaultValue,
+ typeAlias,
+ typeDefinition);
-/// Clear any struct type definitions loaded
-void ShaderGenerator::clearStructTypeDefs()
-{
- StructTypeDesc::clear();
+ _syntax->registerTypeSyntax(typeDesc, structTypeSyntax);
+ }
}
namespace
@@ -436,7 +464,7 @@ void ShaderGenerator::registerShaderMetadata(const DocumentPtr& doc, GenContext&
if (def->getExportable())
{
const string& attrName = def->getAttrName();
- const TypeDesc type = TypeDesc::get(def->getType());
+ const TypeDesc type = _typeSystem->getType(def->getType());
if (!attrName.empty() && type != Type::NONE)
{
registry->addMetadata(attrName, type, def->getValue());
diff --git a/source/MaterialXGenShader/ShaderGenerator.h b/source/MaterialXGenShader/ShaderGenerator.h
index a2f54fec2e..997ee3772e 100644
--- a/source/MaterialXGenShader/ShaderGenerator.h
+++ b/source/MaterialXGenShader/ShaderGenerator.h
@@ -185,17 +185,20 @@ class MX_GENSHADER_API ShaderGenerator
return _unitSystem;
}
+ /// Returns the type system
+ TypeSystemPtr getTypeSystem() const
+ {
+ return _typeSystem;
+ }
+
/// Return the map of token substitutions used by the generator.
const StringMap& getTokenSubstitutions() const
{
return _tokenSubstitutions;
}
- /// Load any struct type definitions from the document in to the type cache.
- void loadStructTypeDefs(const DocumentPtr& doc);
-
- /// Clear any struct type definitions loaded
- void clearStructTypeDefs();
+ /// Register type definitions from the document.
+ virtual void registerTypeDefs(const DocumentPtr& doc);
/// Register metadata that should be exported to the generated shaders.
/// Supported metadata includes standard UI attributes like "uiname", "uifolder",
@@ -210,7 +213,7 @@ class MX_GENSHADER_API ShaderGenerator
protected:
/// Protected constructor
- ShaderGenerator(SyntaxPtr syntax);
+ ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax);
/// Create a new stage in a shader.
virtual ShaderStagePtr createStage(const string& name, Shader& shader) const;
@@ -231,6 +234,7 @@ class MX_GENSHADER_API ShaderGenerator
protected:
static const string T_FILE_TRANSFORM_UV;
+ TypeSystemPtr _typeSystem;
SyntaxPtr _syntax;
Factory _implFactory;
ColorManagementSystemPtr _colorManagementSystem;
diff --git a/source/MaterialXGenShader/ShaderGraph.cpp b/source/MaterialXGenShader/ShaderGraph.cpp
index 0c9f80cf8e..eee15cda3e 100644
--- a/source/MaterialXGenShader/ShaderGraph.cpp
+++ b/source/MaterialXGenShader/ShaderGraph.cpp
@@ -38,7 +38,7 @@ void ShaderGraph::addInputSockets(const InterfaceElement& elem, GenContext& cont
const string& portValueString = portValue ? portValue->getValueString() : EMPTY_STRING;
std::pair enumResult;
const string& enumNames = input->getAttribute(ValueElement::ENUM_ATTRIBUTE);
- const TypeDesc portType = TypeDesc::get(input->getType());
+ const TypeDesc portType = context.getTypeDesc(input->getType());
if (context.getShaderGenerator().getSyntax().remapEnumeration(portValueString, portType, enumNames, enumResult))
{
inputSocket = addInputSocket(input->getName(), enumResult.first);
@@ -64,15 +64,15 @@ void ShaderGraph::addInputSockets(const InterfaceElement& elem, GenContext& cont
}
}
-void ShaderGraph::addOutputSockets(const InterfaceElement& elem)
+void ShaderGraph::addOutputSockets(const InterfaceElement& elem, GenContext& context)
{
for (const OutputPtr& output : elem.getActiveOutputs())
{
- addOutputSocket(output->getName(), TypeDesc::get(output->getType()));
+ addOutputSocket(output->getName(), context.getTypeDesc(output->getType()));
}
if (numOutputSockets() == 0)
{
- addOutputSocket("out", TypeDesc::get(elem.getType()));
+ addOutputSocket("out", context.getTypeDesc(elem.getType()));
}
}
@@ -246,7 +246,7 @@ void ShaderGraph::addDefaultGeomNode(ShaderInput* input, const GeomPropDef& geom
{
std::pair enumResult;
const string& enumNames = nodeDefSpaceInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
- const TypeDesc portType = TypeDesc::get(nodeDefSpaceInput->getType());
+ const TypeDesc portType = context.getTypeDesc(nodeDefSpaceInput->getType());
if (context.getShaderGenerator().getSyntax().remapEnumeration(space, portType, enumNames, enumResult))
{
spaceInput->setValue(enumResult.second);
@@ -452,7 +452,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const NodeGraph& n
graph->addInputSockets(*nodeDef, context);
// Create output sockets from the nodegraph
- graph->addOutputSockets(nodeGraph);
+ graph->addOutputSockets(nodeGraph, context);
// Traverse all outputs and create all internal nodes
for (OutputPtr graphOutput : nodeGraph.getActiveOutputs())
@@ -511,7 +511,8 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
graph->addInputSockets(*interface, context);
// Create the given output socket
- ShaderGraphOutputSocket* outputSocket = graph->addOutputSocket(output->getName(), TypeDesc::get(output->getType()));
+ const TypeDesc outputType = context.getTypeDesc(output->getType());
+ ShaderGraphOutputSocket* outputSocket = graph->addOutputSocket(output->getName(), outputType);
outputSocket->setPath(output->getNamePath());
const string& outputUnit = output->getUnit();
if (!outputUnit.empty())
@@ -543,7 +544,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
graph->addInputSockets(*nodeDef, context);
// Create output sockets
- graph->addOutputSockets(*nodeDef);
+ graph->addOutputSockets(*nodeDef, context);
// Create this shader node in the graph.
ShaderNodePtr newNode = ShaderNode::create(graph.get(), node->getName(), *nodeDef, context);
@@ -579,7 +580,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
{
const string& valueString = value->getValueString();
std::pair enumResult;
- const TypeDesc type = TypeDesc::get(nodedefInput->getType());
+ const TypeDesc type = context.getTypeDesc(nodedefInput->getType());
const string& enumNames = nodedefInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
if (context.getShaderGenerator().getSyntax().remapEnumeration(valueString, type, enumNames, enumResult))
{
diff --git a/source/MaterialXGenShader/ShaderGraph.h b/source/MaterialXGenShader/ShaderGraph.h
index 3ad126923b..9187ce03ff 100644
--- a/source/MaterialXGenShader/ShaderGraph.h
+++ b/source/MaterialXGenShader/ShaderGraph.h
@@ -136,7 +136,7 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode
void addInputSockets(const InterfaceElement& elem, GenContext& context);
/// Add output sockets from an interface element (nodedef, nodegraph or node)
- void addOutputSockets(const InterfaceElement& elem);
+ void addOutputSockets(const InterfaceElement& elem, GenContext& context);
/// Traverse from the given root element and add all dependencies upstream.
/// The traversal is done in the context of a material, if given, to include
diff --git a/source/MaterialXGenShader/ShaderNode.cpp b/source/MaterialXGenShader/ShaderNode.cpp
index 12b9bbf548..43539b5d9b 100644
--- a/source/MaterialXGenShader/ShaderNode.cpp
+++ b/source/MaterialXGenShader/ShaderNode.cpp
@@ -186,7 +186,7 @@ ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name,
// Create interface from nodedef
for (const ValueElementPtr& port : nodeDef.getActiveValueElements())
{
- const TypeDesc portType = TypeDesc::get(port->getType());
+ const TypeDesc portType = context.getTypeDesc(port->getType());
if (port->isA