HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// MDL shading language generator
11 
12 #include <MaterialXGenMdl/Export.h>
13 
15 
17 
18 /// Generator context data class to pass strings.
20 {
21  public:
22  /// MDL Versions supported by the Code Generator
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 
33  /// Create MDL code generator options with default values.
35  targetVersion(MdlVersion::MDL_LATEST) { }
36 
37  /// Unique identifier for the MDL options on the GenContext object.
38  static const string GEN_CONTEXT_USER_DATA_KEY;
39 
40  /// The MDL version number the generated module will have.
41  /// Allows to generate MDL for older applications by limiting support according
42  /// to the corresponding specification. By default this option is MDL_LATEST.
44 };
45 
46 /// Shared pointer to GenMdlOptions
47 using GenMdlOptionsPtr = shared_ptr<class GenMdlOptions>;
48 
49 /// Shared pointer to an MdlShaderGenerator
50 using MdlShaderGeneratorPtr = shared_ptr<class MdlShaderGenerator>;
51 
52 /// @class MdlShaderGenerator
53 /// Shader generator for MDL (Material Definition Language).
55 {
56  public:
57  /// Constructor.
59 
60  /// Creator function.
61  /// If a TypeSystem is not provided it will be created internally.
62  /// Optionally pass in an externally created TypeSystem here,
63  /// if you want to keep type descriptions alive after the lifetime
64  /// of the shader generator.
65  static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
66  {
67  return std::make_shared<MdlShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
68  }
69 
70  /// Return a unique identifier for the target this generator is for
71  const string& getTarget() const override { return TARGET; }
72 
73  /// Apply the default GenOptions for MDL shader generation.
74  void applyDefaultOptions(GenOptions& options) const override;
75 
76  /// Generate a shader starting from the given element, translating
77  /// the element and all dependencies upstream into shader code.
78  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
79 
80  /// Create the shader node implementation for a NodeGraph implementation.
81  ShaderNodeImplPtr createShaderNodeImplForNodeGraph(const NodeGraph& nodegraph) const override;
82 
83  /// Create the shader node implementation for an mplementation implementation.
84  ShaderNodeImplPtr createShaderNodeImplForImplementation(const Implementation& implementation) const override;
85 
86  /// Return the result of an upstream connection or value for an input.
87  string getUpstreamResult(const ShaderInput* input, GenContext& context) const override;
88 
89  /// Unique identifier for this generator target
90  static const string TARGET;
91 
92  /// Map of code snippets for geomprops in MDL.
93  static const std::unordered_map<string, string> GEOMPROP_DEFINITIONS;
94 
95  /// Get the selected MDL target language version number from the context option.
96  /// If not set, the latest version supported by GenMdl is returned.
97  GenMdlOptions::MdlVersion getMdlVersion(GenContext& context) const;
98 
99  /// Add the MDL file header containing the version number of the generated module.
100  void emitMdlVersionNumber(GenContext& context, ShaderStage& stage) const;
101 
102  /// Add the version number suffix appended to MDL modules that use versions.
103  void emitMdlVersionFilenameSuffix(GenContext& context, ShaderStage& stage) const;
104 
105  /// Get the version number suffix appended to MDL modules that use versions.
106  const string& getMdlVersionFilenameSuffix(GenContext& context) const;
107 
108  /// Emit type definitions for all data types that need them.
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 
119 namespace MDL
120 {
121 
122 // Identifiers for MDL variable blocks
123 extern MX_GENMDL_API const string INPUTS;
124 extern MX_GENMDL_API const string OUTPUTS;
125 
126 } // namespace MDL
127 
129 
130 #endif
virtual ShaderNodeImplPtr createShaderNodeImplForNodeGraph(const NodeGraph &nodegraph) const
Create the shader node implementation for a NodeGraph implementation.
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:39
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
#define MX_GENMDL_API
Definition: Export.h:18
static const string GEN_CONTEXT_USER_DATA_KEY
Unique identifier for the MDL options on the GenContext object.
MdlVersion targetVersion
static TypeSystemPtr create()
Create a new type system.
static const string TARGET
Unique identifier for this generator target.
shared_ptr< class TypeSystem > TypeSystemPtr
Definition: TypeDesc.h:219
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
virtual void applyDefaultOptions(GenOptions &options) const
Apply the default GenOptions for this generator's target.
static ShaderGeneratorPtr create(TypeSystemPtr typeSystem=nullptr)
virtual string getUpstreamResult(const ShaderInput *input, GenContext &context) const
Return the result of an upstream connection or value for an input.
MX_GENMDL_API const string OUTPUTS
Generator context data class to pass strings.
GenMdlOptions()
Create MDL code generator options with default values.
GLuint const GLchar * name
Definition: glcorearb.h:786
shared_ptr< class GenMdlOptions > GenMdlOptionsPtr
Shared pointer to GenMdlOptions.
MX_GENMDL_API const string INPUTS
virtual ShaderNodeImplPtr createShaderNodeImplForImplementation(const Implementation &implementation) const
Create the shader node implementation for an Implementation implementation.
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:33
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
static const std::unordered_map< string, string > GEOMPROP_DEFINITIONS
Map of code snippets for geomprops in MDL.
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
virtual void emitTypeDefinitions(GenContext &context, ShaderStage &stage) const
Emit type definitions for all data types that needs it.
MdlVersion
MDL Versions supported by the Code Generator.
shared_ptr< class MdlShaderGenerator > MdlShaderGeneratorPtr
Shared pointer to an MdlShaderGenerator.