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..cac1d91e47 100644
--- a/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenEssl/JsEsslShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr EsslShaderGenerator_create()
+ {
+ return mx::EsslShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(EsslShaderGenerator)
{
- ems::class_>("EsslShaderGenerator")
- .smart_ptr_constructor("EsslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("EsslShaderGenerator")
+ .class_function("create", &EsslShaderGenerator_create);
}
diff --git a/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
index 311296790f..3c0e6c3585 100644
--- a/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenGlsl/JsGlslShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr GlslShaderGenerator_create()
+ {
+ return mx::GlslShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(GlslShaderGenerator)
{
- ems::class_>("GlslShaderGenerator")
- .smart_ptr_constructor("GlslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("GlslShaderGenerator")
+ .class_function("create", &GlslShaderGenerator_create);
}
diff --git a/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
index 9f075a3457..a817660383 100644
--- a/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenMdl/JsMdlShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr MdlShaderGenerator_create()
+ {
+ return mx::MdlShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(MdlShaderGenerator)
{
ems::class_>("MdlShaderGenerator")
- .smart_ptr_constructor("MdlShaderGenerator", &std::make_shared)
- ;
+ .class_function("create", &MdlShaderGenerator_create);
}
diff --git a/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
index 38e0649ac9..285ce68686 100644
--- a/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenMsl/JsMslShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr MslShaderGenerator_create()
+ {
+ return mx::MslShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(MslShaderGenerator)
{
- ems::class_>("MslShaderGenerator")
- .smart_ptr_constructor("MslShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("MslShaderGenerator")
+ .class_function("create", &MslShaderGenerator_create);
}
diff --git a/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
index ebafa25363..7700b7be48 100644
--- a/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenOsl/JsOslShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr OslShaderGenerator_create()
+ {
+ return mx::OslShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(OslShaderGenerator)
{
ems::class_>("OslShaderGenerator")
- .smart_ptr_constructor("OslShaderGenerator", &std::make_shared)
- ;
+ .class_function("create", &OslShaderGenerator_create);
}
diff --git a/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp b/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
index 191f62664c..e55b64a397 100644
--- a/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
+++ b/source/JsMaterialX/JsMaterialXGenVk/JsVkShaderGenerator.cpp
@@ -11,9 +11,17 @@
namespace ems = emscripten;
namespace mx = MaterialX;
+namespace
+{
+ // Creator wrapper to avoid having to expose the TypeSystem class in JavaScript
+ mx::ShaderGeneratorPtr VkShaderGenerator_create()
+ {
+ return mx::VkShaderGenerator::create();
+ }
+}
+
EMSCRIPTEN_BINDINGS(VkShaderGenerator)
{
- ems::class_>("VkShaderGenerator")
- .smart_ptr_constructor("VkShaderGenerator", &std::make_shared)
- ;
+ ems::class_>("VkShaderGenerator")
+ .class_function("create", &VkShaderGenerator_create);
}
diff --git a/source/MaterialXGenGlsl/EsslShaderGenerator.cpp b/source/MaterialXGenGlsl/EsslShaderGenerator.cpp
index 5b59221039..cb9939be8b 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 c75d123700..faaa547a79 100644
--- a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp
+++ b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp
@@ -41,8 +41,8 @@ const string GlslSamplingIncludeFilename = "stdlib/genglsl/lib/mx_sampling.glsl"
// GlslShaderGenerator methods
//
-GlslShaderGenerator::GlslShaderGenerator() :
- HwShaderGenerator(GlslSyntax::create())
+GlslShaderGenerator::GlslShaderGenerator(TypeSystemPtr typeSystem) :
+ HwShaderGenerator(typeSystem, GlslSyntax::create(typeSystem))
{
//
// Register all custom node implementation classes
@@ -734,7 +734,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 8bccb82255..5e65c4a477 100644
--- a/source/MaterialXGenGlsl/GlslSyntax.cpp
+++ b/source/MaterialXGenGlsl/GlslSyntax.cpp
@@ -18,8 +18,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
{
@@ -30,8 +30,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)
{
}
@@ -52,8 +52,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)
{
}
@@ -68,8 +68,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)
{
}
@@ -97,7 +97,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(
@@ -158,6 +159,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -165,11 +167,13 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -177,11 +181,13 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -189,6 +195,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -199,6 +206,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::COLOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -209,6 +217,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"vec2",
"vec2(0.0)",
"vec2(0.0)",
@@ -219,6 +228,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -229,6 +239,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -239,6 +250,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"mat3",
"mat3(1.0)",
"mat3(1.0)"));
@@ -246,17 +258,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));
@@ -264,6 +278,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"BSDF",
"BSDF(vec3(0.0),vec3(1.0))",
EMPTY_STRING,
@@ -273,6 +288,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"EDF",
"EDF(0.0)",
"EDF(0.0)",
@@ -282,6 +298,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"BSDF",
"BSDF(vec3(0.0),vec3(1.0))",
EMPTY_STRING));
@@ -289,6 +306,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"surfaceshader",
"surfaceshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -298,6 +316,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"volumeshader",
"volumeshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -307,6 +326,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"displacementshader",
"displacementshader(vec3(0.0),1.0)",
EMPTY_STRING,
@@ -316,6 +336,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"lightshader",
"lightshader(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -325,6 +346,7 @@ GlslSyntax::GlslSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"material",
"material(vec3(0.0),vec3(0.0))",
EMPTY_STRING,
@@ -402,10 +424,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 eb1737e796..1d4fe7d564 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 516b314771..c227cf56d4 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);
@@ -354,7 +362,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 c47dba864e..18a68cce39 100644
--- a/source/MaterialXGenMdl/MdlShaderGenerator.h
+++ b/source/MaterialXGenMdl/MdlShaderGenerator.h
@@ -54,9 +54,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 c013e7785f..15e10354da 100644
--- a/source/MaterialXGenMdl/MdlSyntax.cpp
+++ b/source/MaterialXGenMdl/MdlSyntax.cpp
@@ -13,20 +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")
-TYPEDESC_REGISTER_TYPE(MDL_SHEEN_MODE, "mode")
-
-} // namespace Type
-
namespace
{
@@ -35,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()")
{
}
@@ -82,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)
{
}
@@ -103,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)
{
}
@@ -119,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)
{
}
@@ -142,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)
{
}
@@ -169,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)
{
}
@@ -205,7 +191,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
@@ -255,6 +241,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -262,11 +249,13 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -274,11 +263,13 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -286,6 +277,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"color",
"color(0.0)",
"color(0.0)",
@@ -295,11 +287,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)",
@@ -310,6 +303,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"float3",
"float3(0.0)",
"float3(0.0)",
@@ -320,6 +314,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"float4",
"float4(0.0)",
"float4(0.0)",
@@ -330,6 +325,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"float3x3",
"float3x3(1.0)",
"float3x3(1.0)"));
@@ -337,6 +333,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATRIX44,
std::make_shared(
+ this,
"float4x4",
"float4x4(1.0)",
"float4x4(1.0)"));
@@ -344,17 +341,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()"));
@@ -362,6 +361,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -369,6 +369,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -376,6 +377,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -383,6 +385,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -390,6 +393,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -397,6 +401,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -404,6 +409,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MATERIAL,
std::make_shared(
+ this,
"material",
"material()",
"material()"));
@@ -411,6 +417,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_ADDRESSMODE,
std::make_shared(
+ this,
"mx_addressmode_type",
"mx_addressmode_type_periodic",
"mx_addressmode_type_periodic",
@@ -419,6 +426,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_COORDINATESPACE,
std::make_shared(
+ this,
"mx_coordinatespace_type",
"mx_coordinatespace_type_model",
"mx_coordinatespace_type_model",
@@ -427,6 +435,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_FILTERLOOKUPMODE,
std::make_shared(
+ this,
"mx_filterlookup_type",
"mx_filterlookup_type_linear",
"mx_filterlookup_type_linear",
@@ -435,6 +444,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_FILTERTYPE,
std::make_shared(
+ this,
"mx_filter_type",
"mx_filter_type_gaussian",
"mx_filter_type_gaussian",
@@ -443,6 +453,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_DISTRIBUTIONTYPE,
std::make_shared(
+ this,
"mx_distribution_type",
"mx_distribution_type_ggx",
"mx_distribution_type_ggx",
@@ -451,6 +462,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_SCATTER_MODE,
std::make_shared(
+ this,
"mx_scatter_mode",
"mx_scatter_mode_R",
"mx_scatter_mode_R",
@@ -459,6 +471,7 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::MDL_SHEEN_MODE,
std::make_shared(
+ this,
"mx_sheen_mode",
"mx_sheen_mode_conty_kulla",
"mx_sheen_mode_conty_kulla",
diff --git a/source/MaterialXGenMdl/MdlSyntax.h b/source/MaterialXGenMdl/MdlSyntax.h
index 7bf4cd7300..c3a32537bf 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 71ca907f87..f714ce1cd2 100644
--- a/source/MaterialXGenMsl/MslShaderGenerator.cpp
+++ b/source/MaterialXGenMsl/MslShaderGenerator.cpp
@@ -45,8 +45,8 @@ const string MslSamplingIncludeFilename = "stdlib/genmsl/lib/mx_sampling.metal";
// MslShaderGenerator methods
//
-MslShaderGenerator::MslShaderGenerator() :
- HwShaderGenerator(MslSyntax::create())
+MslShaderGenerator::MslShaderGenerator(TypeSystemPtr typeSystem) :
+ HwShaderGenerator(typeSystem, MslSyntax::create(typeSystem))
{
//
// Register all custom node implementation classes
@@ -1248,7 +1248,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 9cc9fecb96..a231a9bc6d 100644
--- a/source/MaterialXGenMsl/MslSyntax.cpp
+++ b/source/MaterialXGenMsl/MslSyntax.cpp
@@ -18,8 +18,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
{
@@ -30,8 +30,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)
{
}
@@ -52,8 +52,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)
{
}
@@ -68,8 +68,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)
{
}
@@ -98,7 +98,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(
@@ -140,6 +140,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::FLOAT,
std::make_shared(
+ this,
"float",
"0.0",
"0.0"));
@@ -147,11 +148,13 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::FLOATARRAY,
std::make_shared(
+ this,
"float"));
registerTypeSyntax(
Type::INTEGER,
std::make_shared(
+ this,
"int",
"0",
"0"));
@@ -159,11 +162,13 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::INTEGERARRAY,
std::make_shared(
+ this,
"int"));
registerTypeSyntax(
Type::BOOLEAN,
std::make_shared(
+ this,
"bool",
"false",
"false"));
@@ -171,6 +176,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::COLOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -181,6 +187,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::COLOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -191,6 +198,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR2,
std::make_shared(
+ this,
"vec2",
"vec2(0.0)",
"vec2(0.0)",
@@ -201,6 +209,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR3,
std::make_shared(
+ this,
"vec3",
"vec3(0.0)",
"vec3(0.0)",
@@ -211,6 +220,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VECTOR4,
std::make_shared(
+ this,
"vec4",
"vec4(0.0)",
"vec4(0.0)",
@@ -221,6 +231,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::MATRIX33,
std::make_shared(
+ this,
"mat3",
"mat3(1.0)",
"mat3(1.0)"));
@@ -228,17 +239,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));
@@ -246,6 +259,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::BSDF,
std::make_shared(
+ this,
"BSDF",
"BSDF{float3(0.0),float3(1.0)}",
EMPTY_STRING,
@@ -255,6 +269,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::EDF,
std::make_shared(
+ this,
"EDF",
"EDF(0.0)",
"EDF(0.0)",
@@ -264,6 +279,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VDF,
std::make_shared(
+ this,
"BSDF",
"BSDF{float3(0.0),float3(1.0)}",
EMPTY_STRING));
@@ -271,6 +287,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::SURFACESHADER,
std::make_shared(
+ this,
"surfaceshader",
"surfaceshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -280,6 +297,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::VOLUMESHADER,
std::make_shared(
+ this,
"volumeshader",
"volumeshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -289,6 +307,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared(
+ this,
"displacementshader",
"displacementshader{float3(0.0),1.0}",
EMPTY_STRING,
@@ -298,6 +317,7 @@ MslSyntax::MslSyntax()
registerTypeSyntax(
Type::LIGHTSHADER,
std::make_shared(
+ this,
"lightshader",
"lightshader{float3(0.0),float3(0.0)}",
EMPTY_STRING,
@@ -307,6 +327,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 05249911ad..74a133538c 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 c12b37579e..311660ab09 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 3eafbadf69..0e5e87804b 100644
--- a/source/MaterialXGenShader/GenContext.h
+++ b/source/MaterialXGenShader/GenContext.h
@@ -14,6 +14,7 @@
#include
#include
#include
+#include
#include
@@ -49,6 +50,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 1e994714ea..58faebc6c6 100644
--- a/source/MaterialXGenShader/HwShaderGenerator.cpp
+++ b/source/MaterialXGenShader/HwShaderGenerator.cpp
@@ -164,7 +164,7 @@ const string USER_DATA_CLOSURE_CONTEXT = "udcc";
const string USER_DATA_LIGHT_SHADERS = "udls";
const string USER_DATA_BINDING_CONTEXT = "udbinding";
-const TypeDesc ClosureDataType = TypeDesc("ClosureData", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_NONE, 1);
+const TypeDesc ClosureDataType = TypeDesc("ClosureData", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_NONE, 1, 0);
} // namespace HW
namespace Stage
@@ -178,8 +178,8 @@ const string VERTEX = "vertex";
// HwShaderGenerator methods
//
-HwShaderGenerator::HwShaderGenerator(SyntaxPtr syntax) :
- ShaderGenerator(syntax)
+HwShaderGenerator::HwShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax) :
+ ShaderGenerator(typeSystem, syntax)
{
// Assign default identifiers names for all tokens.
// Derived generators can override these names.
@@ -468,7 +468,7 @@ ShaderPtr HwShaderGenerator::createShader(const string& name, ElementPtr element
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 c12d31dba5..2e21dfcee4 100644
--- a/source/MaterialXGenShader/HwShaderGenerator.h
+++ b/source/MaterialXGenShader/HwShaderGenerator.h
@@ -332,7 +332,7 @@ class MX_GENSHADER_API HwShaderGenerator : public ShaderGenerator
};
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 068d5718bd..e261096be9 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)
{
}
@@ -296,7 +297,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())
{
@@ -340,45 +341,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
@@ -432,7 +460,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 c885341b31..73a31f09cb 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 52424cba56..bc16ebfb40 100644
--- a/source/MaterialXGenShader/ShaderNode.cpp
+++ b/source/MaterialXGenShader/ShaderNode.cpp
@@ -185,7 +185,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