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_LATEST = MDL_1_8
29  };
30 
31  /// Create MDL code generator options with default values.
33  targetVersion(MdlVersion::MDL_LATEST) { }
34 
35  /// Unique identifier for the MDL options on the GenContext object.
36  static const string GEN_CONTEXT_USER_DATA_KEY;
37 
38  /// The MDL version number the generated module will have.
39  /// Allows to generate MDL for older applications by limiting support according
40  /// to the corresponding specification. By default this option is MDL_LATEST.
42 };
43 
44 /// Shared pointer to GenMdlOptions
45 using GenMdlOptionsPtr = shared_ptr<class GenMdlOptions>;
46 
47 /// Shared pointer to an MdlShaderGenerator
48 using MdlShaderGeneratorPtr = shared_ptr<class MdlShaderGenerator>;
49 
50 /// @class MdlShaderGenerator
51 /// Shader generator for MDL (Material Definition Language).
53 {
54  public:
56 
57  static ShaderGeneratorPtr create() { return std::make_shared<MdlShaderGenerator>(); }
58 
59  /// Return a unique identifier for the target this generator is for
60  const string& getTarget() const override { return TARGET; }
61 
62  /// Generate a shader starting from the given element, translating
63  /// the element and all dependencies upstream into shader code.
64  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
65 
66  /// Return a registered shader node implementation given an implementation element.
67  /// The element must be an Implementation or a NodeGraph acting as implementation.
68  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const override;
69 
70  /// Return the result of an upstream connection or value for an input.
71  string getUpstreamResult(const ShaderInput* input, GenContext& context) const override;
72 
73  /// Unique identifier for this generator target
74  static const string TARGET;
75 
76  /// Map of code snippets for geomprops in MDL.
77  static const std::unordered_map<string, string> GEOMPROP_DEFINITIONS;
78 
79  /// Add the MDL file header containing the version number of the generated module..
80  void emitMdlVersionNumber(GenContext& context, ShaderStage& stage) const;
81 
82  /// Add the version number suffix appended to MDL modules that use versions.
83  void emitMdlVersionFilenameSuffix(GenContext& context, ShaderStage& stage) const;
84 
85  /// Get the version number suffix appended to MDL modules that use versions.
86  const string& getMdlVersionFilenameSuffix(GenContext& context) const;
87 
88  protected:
89  // Create and initialize a new MDL shader for shader generation.
90  ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
91 
92  // Emit a block of shader inputs.
93  void emitShaderInputs(const DocumentPtr doc, const VariableBlock& inputs, ShaderStage& stage) const;
94 };
95 
96 namespace MDL
97 {
98 
99 // Identifiers for MDL variable blocks
100 extern MX_GENMDL_API const string INPUTS;
101 extern MX_GENMDL_API const string OUTPUTS;
102 
103 } // namespace MDL
104 
106 
107 #endif
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
#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 const string TARGET
Unique identifier for this generator target.
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
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.
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
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:34
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:38
static ShaderGeneratorPtr create()
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.