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  /// Generate a shader starting from the given element, translating
74  /// the element and all dependencies upstream into shader code.
75  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
76 
77  /// Return a registered shader node implementation given an implementation element.
78  /// The element must be an Implementation or a NodeGraph acting as implementation.
79  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const override;
80 
81  /// Return the result of an upstream connection or value for an input.
82  string getUpstreamResult(const ShaderInput* input, GenContext& context) const override;
83 
84  /// Unique identifier for this generator target
85  static const string TARGET;
86 
87  /// Map of code snippets for geomprops in MDL.
88  static const std::unordered_map<string, string> GEOMPROP_DEFINITIONS;
89 
90  /// Get the selected MDL target language version number from the context option.
91  /// If not set, the latest version supported by GenMdl is returned.
92  GenMdlOptions::MdlVersion getMdlVersion(GenContext& context) const;
93 
94  /// Add the MDL file header containing the version number of the generated module.
95  void emitMdlVersionNumber(GenContext& context, ShaderStage& stage) const;
96 
97  /// Add the version number suffix appended to MDL modules that use versions.
98  void emitMdlVersionFilenameSuffix(GenContext& context, ShaderStage& stage) const;
99 
100  /// Get the version number suffix appended to MDL modules that use versions.
101  const string& getMdlVersionFilenameSuffix(GenContext& context) const;
102 
103  protected:
104  // Create and initialize a new MDL shader for shader generation.
105  ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
106 
107  // Emit a block of shader inputs.
108  void emitShaderInputs(ConstDocumentPtr doc, const VariableBlock& inputs, ShaderStage& stage) const;
109 };
110 
111 namespace MDL
112 {
113 
114 // Identifiers for MDL variable blocks
115 extern MX_GENMDL_API const string INPUTS;
116 extern MX_GENMDL_API const string OUTPUTS;
117 
118 } // namespace MDL
119 
121 
122 #endif
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.
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.
virtual ShaderNodeImplPtr getImplementation(const NodeDef &nodedef, GenContext &context) const
Return a registered shader node implementation for the given nodedef.
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
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
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
MdlVersion
MDL Versions supported by the Code Generator.
shared_ptr< class MdlShaderGenerator > MdlShaderGeneratorPtr
Shared pointer to an MdlShaderGenerator.