HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MslShaderGenerator.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_MSLSHADERGENERATOR_H
7 #define MATERIALX_MSLSHADERGENERATOR_H
8 
9 /// @file
10 /// MSL shader generator
11 
12 #include <MaterialXGenMsl/Export.h>
13 
15 
16 #define TEXTURE_NAME(t) ((t) + "_tex")
17 #define SAMPLER_NAME(t) ((t) + "_sampler")
18 
20 
21 using MslShaderGeneratorPtr = shared_ptr<class MslShaderGenerator>;
22 
23 /// Base class for MSL (OpenGL Shading Language) code generation.
24 /// A generator for a specific MSL target should be derived from this class.
26 {
27  public:
28  /// Constructor.
30 
31  /// Creator function.
32  /// If a TypeSystem is not provided it will be created internally.
33  /// Optionally pass in an externally created TypeSystem here,
34  /// if you want to keep type descriptions alive after the lifetime
35  /// of the shader generator.
36  static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
37  {
38  return std::make_shared<MslShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
39  }
40 
41  /// Generate a shader starting from the given element, translating
42  /// the element and all dependencies upstream into shader code.
43  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
44 
45  /// Return a unique identifier for the target this generator is for
46  const string& getTarget() const override { return TARGET; }
47 
48  /// Return the version string for the MSL version this generator is for
49  virtual const string& getVersion() const { return VERSION; }
50 
51  /// Emit a shader variable.
52  void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
53  bool assignValue = true) const override;
54 
55  /// Return a registered shader node implementation given an implementation element.
56  /// The element must be an Implementation or a NodeGraph acting as implementation.
57  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const override;
58 
59  /// Determine the prefix of vertex data variables.
60  string getVertexDataPrefix(const VariableBlock& vertexData) const override;
61 
62  public:
63  /// Unique identifier for this generator target
64  static const string TARGET;
65 
66  /// Version string for the generator target
67  static const string VERSION;
68 
69  protected:
70  virtual void emitVertexStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
71  virtual void emitPixelStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
72 
73  virtual void emitMetalTextureClass(GenContext& context, ShaderStage& stage) const;
74  virtual void emitDirectives(GenContext& context, ShaderStage& stage) const;
75  virtual void emitConstants(GenContext& context, ShaderStage& stage) const;
76  virtual void emitLightData(GenContext& context, ShaderStage& stage) const;
77  virtual void emitInputs(GenContext& context, ShaderStage& stage) const;
78  virtual void emitOutputs(GenContext& context, ShaderStage& stage) const;
79 
80  virtual void emitMathMatrixScalarMathOperators(GenContext& context, ShaderStage& stage) const;
81  virtual void MetalizeGeneratedShader(ShaderStage& shaderStage) const;
82 
83  void emitConstantBufferDeclarations(GenContext& context,
84  HwResourceBindingContextPtr resourceBindingCtx,
85  ShaderStage& stage) const;
86 
88  {
89  EMIT_GLOBAL_SCOPE_CONTEXT_ENTRY_FUNCTION_RESOURCES = 0,
90  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT = 1,
91  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_DECL = 2,
92  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_ARGS = 3,
93  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_INIT = 4
94  };
95 
96  void emitGlobalVariables(GenContext& context, ShaderStage& stage,
97  EmitGlobalScopeContext situation,
98  bool isVertexShader,
99  bool needsLightData) const;
100 
101  void emitInputs(GenContext& context, ShaderStage& stage,
102  const VariableBlock& inputs) const;
103 
104  virtual HwResourceBindingContextPtr getResourceBindingContext(GenContext& context) const;
105 
106  /// Logic to indicate whether code to support direct lighting should be emitted.
107  /// By default if the graph is classified as a shader, or BSDF node then lighting is assumed to be required.
108  /// Derived classes can override this logic.
109  virtual bool requiresLighting(const ShaderGraph& graph) const;
110 
111  /// Emit specular environment lookup code
112  virtual void emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const;
113 
114  /// Emit transmission rendering code
115  virtual void emitTransmissionRender(GenContext& context, ShaderStage& stage) const;
116 
117  /// Emit function definitions for lighting code
118  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
119 
120  static void toVec4(TypeDesc type, string& variable);
121  [[deprecated]] static void toVec4(const TypeDesc* type, string& variable) { toVec4(*type, variable); }
122 
123  /// Nodes used internally for light sampling.
124  vector<ShaderNodePtr> _lightSamplingNodes;
125 };
126 
127 /// Base class for common MSL node implementations
129 {
130  public:
131  const string& getTarget() const override;
132 
133  protected:
135 };
136 
138 
139 #endif
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:39
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
virtual void emitVariableDeclaration(const ShaderPort *variable, const string &qualifier, GenContext &context, ShaderStage &stage, bool assignValue=true) const
static const string VERSION
Version string for the generator target.
static TypeSystemPtr create()
Create a new type system.
shared_ptr< class TypeSystem > TypeSystemPtr
Definition: TypeDesc.h:219
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
virtual string getVertexDataPrefix(const VariableBlock &vertexData) const =0
Determine the prefix of vertex data variables.
virtual ShaderNodeImplPtr getImplementation(const NodeDef &nodedef, GenContext &context) const
Return a registered shader node implementation for the given nodedef.
virtual const string & getTarget() const
Base class for common MSL node implementations.
virtual const string & getVersion() const
Return the version string for the MSL version this generator is for.
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
GLuint const GLchar * name
Definition: glcorearb.h:786
#define MX_GENMSL_API
Definition: Export.h:18
vector< ShaderNodePtr > _lightSamplingNodes
Nodes used internally for light sampling.
static ShaderGeneratorPtr create(TypeSystemPtr typeSystem=nullptr)
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 void toVec4(const TypeDesc *type, string &variable)
static const string TARGET
Unique identifier for this generator target.
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
shared_ptr< class MslShaderGenerator > MslShaderGeneratorPtr