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 
16 
17 #define TEXTURE_NAME(t) ((t) + "_tex")
18 #define SAMPLER_NAME(t) ((t) + "_sampler")
19 
21 
22 using MslShaderGeneratorPtr = shared_ptr<class MslShaderGenerator>;
23 
24 /// Base class for MSL (OpenGL Shading Language) code generation.
25 /// A generator for a specific MSL target should be derived from this class.
27 {
28  public:
29  /// Constructor.
31 
32  /// Creator function.
33  /// If a TypeSystem is not provided it will be created internally.
34  /// Optionally pass in an externally created TypeSystem here,
35  /// if you want to keep type descriptions alive after the lifetime
36  /// of the shader generator.
37  static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
38  {
39  return std::make_shared<MslShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
40  }
41 
42  /// Generate a shader starting from the given element, translating
43  /// the element and all dependencies upstream into shader code.
44  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
45 
46  /// Return a unique identifier for the target this generator is for
47  const string& getTarget() const override { return TARGET; }
48 
49  /// Return the version string for the MSL version this generator is for
50  virtual const string& getVersion() const { return VERSION; }
51 
52  /// Emit a shader variable.
53  void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
54  bool assignValue = true) const override;
55 
56  /// Determine the prefix of vertex data variables.
57  string getVertexDataPrefix(const VariableBlock& vertexData) const override;
58 
59  public:
60  /// Unique identifier for this generator target
61  static const string TARGET;
62 
63  /// Version string for the generator target
64  static const string VERSION;
65 
66  protected:
67  virtual void emitVertexStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
68  virtual void emitPixelStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
69 
70  virtual void emitMetalTextureClass(GenContext& context, ShaderStage& stage) const;
71  virtual void emitDirectives(GenContext& context, ShaderStage& stage) const;
72  virtual void emitConstants(GenContext& context, ShaderStage& stage) const;
73  virtual void emitLightData(GenContext& context, ShaderStage& stage) const;
74  virtual void emitInputs(GenContext& context, ShaderStage& stage) const;
75  virtual void emitOutputs(GenContext& context, ShaderStage& stage) const;
76 
77  virtual void emitMathMatrixScalarMathOperators(GenContext& context, ShaderStage& stage) const;
78  virtual void MetalizeGeneratedShader(ShaderStage& shaderStage) const;
79 
80  void emitConstantBufferDeclarations(GenContext& context,
81  HwResourceBindingContextPtr resourceBindingCtx,
82  ShaderStage& stage) const;
83 
85  {
86  EMIT_GLOBAL_SCOPE_CONTEXT_ENTRY_FUNCTION_RESOURCES = 0,
87  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT = 1,
88  EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_DECL = 2,
89  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_ARGS = 3,
90  EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_INIT = 4
91  };
92 
93  void emitGlobalVariables(GenContext& context, ShaderStage& stage,
94  EmitGlobalScopeContext situation,
95  bool isVertexShader,
96  bool needsLightData) const;
97 
98  void emitInputs(GenContext& context, ShaderStage& stage,
99  const VariableBlock& inputs) const;
100 
101  virtual HwResourceBindingContextPtr getResourceBindingContext(GenContext& context) const;
102 
103  /// Emit specular environment lookup code
104  virtual void emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const;
105 
106  /// Emit transmission rendering code
107  virtual void emitTransmissionRender(GenContext& context, ShaderStage& stage) const;
108 
109  /// Emit function definitions for lighting code
110  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
111 
112  /// Nodes used internally for light sampling.
113  vector<ShaderNodePtr> _lightSamplingNodes;
114 };
115 
117 
118 #endif
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
#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
virtual string getVertexDataPrefix(const VariableBlock &vertexData) const =0
Determine the prefix of vertex data variables.
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 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