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:
29 
30  static ShaderGeneratorPtr create() { return std::make_shared<MslShaderGenerator>(); }
31 
32  /// Generate a shader starting from the given element, translating
33  /// the element and all dependencies upstream into shader code.
34  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
35 
36  /// Return a unique identifier for the target this generator is for
37  const string& getTarget() const override { return TARGET; }
38 
39  /// Return the version string for the MSL version this generator is for
40  virtual const string& getVersion() const { return VERSION; }
41 
42  /// Emit a shader variable.
43  void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
44  bool assignValue = true) const override;
45 
46  /// Return a registered shader node implementation given an implementation element.
47  /// The element must be an Implementation or a NodeGraph acting as implementation.
48  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const override;
49 
50  /// Determine the prefix of vertex data variables.
51  virtual string getVertexDataPrefix(const VariableBlock& vertexData) const;
52 
53  public:
54  /// Unique identifier for this generator target
55  static const string TARGET;
56 
57  /// Version string for the generator target
58  static const string VERSION;
59 
60  protected:
61  virtual void emitVertexStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
62  virtual void emitPixelStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
63 
64  virtual void emitMetalTextureClass(GenContext& context, ShaderStage& stage) const;
65  virtual void emitDirectives(GenContext& context, ShaderStage& stage) const;
66  virtual void emitConstants(GenContext& context, ShaderStage& stage) const;
67  virtual void emitLightData(GenContext& context, ShaderStage& stage) const;
68  virtual void emitInputs(GenContext& context, ShaderStage& stage) const;
69  virtual void emitOutputs(GenContext& context, ShaderStage& stage) const;
70 
71  virtual void emitMathMatrixScalarMathOperators(GenContext& context, ShaderStage& stage) const;
72  virtual void MetalizeGeneratedShader(ShaderStage& shaderStage) const;
73 
74  void emitConstantBufferDeclarations(GenContext& context,
75  HwResourceBindingContextPtr resourceBindingCtx,
76  ShaderStage& stage) const;
77 
79  {
80  EMIT_GLOBAL_SCOPE_CONTEXT_ENTRY_FUNCTION_RESOURCES = 0,
81  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT = 1,
82  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_DECL = 2,
83  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_ARGS = 3,
84  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_INIT = 4
85  };
86 
87  void emitGlobalVariables(GenContext& context, ShaderStage& stage,
88  EmitGlobalScopeContext situation,
89  bool isVertexShader,
90  bool needsLightData) const;
91 
92  void emitInputs(GenContext& context, ShaderStage& stage,
93  const VariableBlock& inputs) const;
94 
95  virtual HwResourceBindingContextPtr getResourceBindingContext(GenContext& context) const;
96 
97  /// Logic to indicate whether code to support direct lighting should be emitted.
98  /// By default if the graph is classified as a shader, or BSDF node then lighting is assumed to be required.
99  /// Derived classes can override this logic.
100  virtual bool requiresLighting(const ShaderGraph& graph) const;
101 
102  /// Emit specular environment lookup code
103  virtual void emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const;
104 
105  /// Emit transmission rendering code
106  virtual void emitTransmissionRender(GenContext& context, ShaderStage& stage) const;
107 
108  /// Emit function definitions for lighting code
109  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
110 
111  static void toVec4(const TypeDesc* type, string& variable);
112 
113  /// Nodes used internally for light sampling.
114  vector<ShaderNodePtr> _lightSamplingNodes;
115 };
116 
117 /// Base class for common MSL node implementations
119 {
120  public:
121  const string& getTarget() const override;
122 
123  bool isEditable(const ShaderInput& input) const override;
124 
125  protected:
127 
128  // Integer identifiers for coordinate spaces.
129  // The order must match the order given for
130  // the space enum string in stdlib.
131  enum Space
132  {
133  MODEL_SPACE = 0,
134  OBJECT_SPACE = 1,
135  WORLD_SPACE = 2
136  };
137 
138  /// Internal string constants
139  static const string SPACE;
140  static const string TO_SPACE;
141  static const string FROM_SPACE;
142  static const string WORLD;
143  static const string OBJECT;
144  static const string MODEL;
145  static const string INDEX;
146  static const string GEOMPROP;
147 };
148 
150 
151 #endif
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
#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.
virtual bool isEditable(const ShaderInput &) const
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
static const string FROM_SPACE
static const string MODEL
static const string GEOMPROP
static const string INDEX
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
static const string OBJECT
vector< ShaderNodePtr > _lightSamplingNodes
Nodes used internally for light sampling.
static const string SPACE
Internal string constants.
static const string WORLD
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 string TARGET
Unique identifier for this generator target.
type
Definition: core.h:1059
static const string TO_SPACE
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
static ShaderGeneratorPtr create()
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
shared_ptr< class MslShaderGenerator > MslShaderGeneratorPtr