MaterialX 1.39.5
Loading...
Searching...
No Matches
MdlShaderGenerator.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_MDLSHADERGENERATOR_H
7#define MATERIALX_MDLSHADERGENERATOR_H
8
11
13
15
16MATERIALX_NAMESPACE_BEGIN
17
19class MX_GENMDL_API GenMdlOptions : public GenUserData
20{
21 public:
23 enum class MdlVersion
24 {
25 MDL_1_6,
26 MDL_1_7,
27 MDL_1_8,
28 MDL_1_9,
29 MDL_1_10,
30 MDL_LATEST = MDL_1_10
31 };
32
35 targetVersion(MdlVersion::MDL_LATEST) { }
36
38 static const string GEN_CONTEXT_USER_DATA_KEY;
39
44};
45
47using GenMdlOptionsPtr = shared_ptr<class GenMdlOptions>;
48
50using MdlShaderGeneratorPtr = shared_ptr<class MdlShaderGenerator>;
51
54class MX_GENMDL_API MdlShaderGenerator : public ShaderGenerator
55{
56 public:
58 MdlShaderGenerator(TypeSystemPtr typeSystem);
59
65 static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
66 {
67 return std::make_shared<MdlShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
68 }
69
71 const string& getTarget() const override { return TARGET; }
72
74 void applyDefaultOptions(GenOptions& options) const override;
75
78 ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
79
82
85
87 string getUpstreamResult(const ShaderInput* input, GenContext& context) const override;
88
90 static const string TARGET;
91
93 static const std::unordered_map<string, string> GEOMPROP_DEFINITIONS;
94
98
100 void emitMdlVersionNumber(GenContext& context, ShaderStage& stage) const;
101
104
106 const string& getMdlVersionFilenameSuffix(GenContext& context) const;
107
109 void emitTypeDefinitions(GenContext& context, ShaderStage& stage) const override;
110
111 protected:
112 // Create and initialize a new MDL shader for shader generation.
113 ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
114
115 // Emit a block of shader inputs.
116 void emitShaderInputs(const VariableBlock& inputs, ShaderStage& stage) const;
117};
118
119namespace MDL
120{
121
122// Identifiers for MDL variable blocks
123extern MX_GENMDL_API const string INPUTS;
124extern MX_GENMDL_API const string OUTPUTS;
125
126} // namespace MDL
127
128MATERIALX_NAMESPACE_END
129
130#endif
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition Element.h:31
Macros for declaring imported and exported symbols.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition Library.h:33
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition Library.h:39
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition Library.h:37
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.
shared_ptr< class MdlShaderGenerator > MdlShaderGeneratorPtr
Shared pointer to an MdlShaderGenerator.
Definition MdlShaderGenerator.h:50
shared_ptr< class GenMdlOptions > GenMdlOptionsPtr
Shared pointer to GenMdlOptions.
Definition MdlShaderGenerator.h:47
Base shader generator class.
A context class for shader generation.
Definition GenContext.h:30
GenMdlOptions()
Create MDL code generator options with default values.
Definition MdlShaderGenerator.h:34
MdlVersion targetVersion
The MDL version number the generated module will have.
Definition MdlShaderGenerator.h:43
MdlVersion
MDL Versions supported by the Code Generator.
Definition MdlShaderGenerator.h:24
static const string GEN_CONTEXT_USER_DATA_KEY
Unique identifier for the MDL options on the GenContext object.
Definition MdlShaderGenerator.h:38
Class holding options to configure shader generation.
Definition GenOptions.h:76
An implementation element within a Document.
Definition Definition.h:204
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
Definition MdlShaderGenerator.h:71
static ShaderGeneratorPtr create(TypeSystemPtr typeSystem=nullptr)
Creator function.
Definition MdlShaderGenerator.h:65
static const string TARGET
Unique identifier for this generator target.
Definition MdlShaderGenerator.h:90
ShaderPtr generate(const string &name, ElementPtr element, GenContext &context) const override
Generate a shader starting from the given element, translating the element and all dependencies upstr...
ShaderNodeImplPtr createShaderNodeImplForImplementation(const Implementation &implementation) const override
Create the shader node implementation for an mplementation implementation.
void emitTypeDefinitions(GenContext &context, ShaderStage &stage) const override
Emit type definitions for all data types that need them.
void emitMdlVersionNumber(GenContext &context, ShaderStage &stage) const
Add the MDL file header containing the version number of the generated module.
void emitMdlVersionFilenameSuffix(GenContext &context, ShaderStage &stage) const
Add the version number suffix appended to MDL modules that use versions.
static const std::unordered_map< string, string > GEOMPROP_DEFINITIONS
Map of code snippets for geomprops in MDL.
Definition MdlShaderGenerator.h:93
void applyDefaultOptions(GenOptions &options) const override
Apply the default GenOptions for MDL shader generation.
MdlShaderGenerator(TypeSystemPtr typeSystem)
Constructor.
ShaderNodeImplPtr createShaderNodeImplForNodeGraph(const NodeGraph &nodegraph) const override
Create the shader node implementation for a NodeGraph implementation.
GenMdlOptions::MdlVersion getMdlVersion(GenContext &context) const
Get the selected MDL target language version number from the context option.
const string & getMdlVersionFilenameSuffix(GenContext &context) const
Get the version number suffix appended to MDL modules that use versions.
string getUpstreamResult(const ShaderInput *input, GenContext &context) const override
Return the result of an upstream connection or value for an input.
A node graph element within a Document.
Definition Node.h:332
ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax)
Protected constructor.
An input on a ShaderNode.
Definition ShaderNode.h:272
A shader stage, containing the state and resulting source code for the stage.
Definition ShaderStage.h:145
static TypeSystemPtr create()
Create a new type system.
A block of variables in a shader stage.
Definition ShaderStage.h:67