HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlslShaderGenerator.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_GLSLSHADERGENERATOR_H
7 #define MATERIALX_GLSLSHADERGENERATOR_H
8 
9 /// @file
10 /// GLSL shader generator
11 
13 
15 
17 
18 using GlslShaderGeneratorPtr = shared_ptr<class GlslShaderGenerator>;
19 
20 /// Base class for GLSL (OpenGL Shading Language) code generation.
21 /// A generator for a specific GLSL target should be derived from this class.
23 {
24  public:
25  /// Constructor.
27 
28  /// Creator function.
29  /// If a TypeSystem is not provided it will be created internally.
30  /// Optionally pass in an externally created TypeSystem here,
31  /// if you want to keep type descriptions alive after the lifetime
32  /// of the shader generator.
33  static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
34  {
35  return std::make_shared<GlslShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
36  }
37 
38  /// Generate a shader starting from the given element, translating
39  /// the element and all dependencies upstream into shader code.
40  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
41 
42  /// Return a unique identifier for the target this generator is for
43  const string& getTarget() const override { return TARGET; }
44 
45  /// Return the version string for the GLSL version this generator is for
46  virtual const string& getVersion() const { return VERSION; }
47 
48  /// Emit a shader variable.
49  void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
50  bool assignValue = true) const override;
51 
52  /// Return a registered shader node implementation given an implementation element.
53  /// The element must be an Implementation or a NodeGraph acting as implementation.
54  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) 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 emitDirectives(GenContext& context, ShaderStage& stage) const;
71  virtual void emitConstants(GenContext& context, ShaderStage& stage) const;
72  virtual void emitUniforms(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 HwResourceBindingContextPtr getResourceBindingContext(GenContext& context) const;
78 
79  /// Logic to indicate whether code to support direct lighting should be emitted.
80  /// By default if the graph is classified as a shader, or BSDF node then lighting is assumed to be required.
81  /// Derived classes can override this logic.
82  virtual bool requiresLighting(const ShaderGraph& graph) const;
83 
84  /// Emit specular environment lookup code
85  virtual void emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const;
86 
87  /// Emit transmission rendering code
88  virtual void emitTransmissionRender(GenContext& context, ShaderStage& stage) const;
89 
90  /// Emit function definitions for lighting code
91  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
92 
93  static void toVec4(TypeDesc type, string& variable);
94  [[deprecated]] static void toVec4(const TypeDesc* type, string& variable) { toVec4(*type, variable); }
95 
96  /// Nodes used internally for light sampling.
97  vector<ShaderNodePtr> _lightSamplingNodes;
98 };
99 
100 /// Base class for common GLSL node implementations
102 {
103  public:
104  const string& getTarget() const override;
105 
106  protected:
108 };
109 
111 
112 #endif
vector< ShaderNodePtr > _lightSamplingNodes
Nodes used internally for light sampling.
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
shared_ptr< class GlslShaderGenerator > GlslShaderGeneratorPtr
Base class for common GLSL node implementations.
static const string TARGET
Unique identifier for this generator target.
static TypeSystemPtr create()
Create a new type system.
static ShaderGeneratorPtr create(TypeSystemPtr typeSystem=nullptr)
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
shared_ptr< class TypeSystem > TypeSystemPtr
Definition: TypeDesc.h:219
static const string VERSION
Version string for the generator target.
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
virtual const string & getVersion() const
Return the version string for the GLSL version this generator is for.
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
GLuint const GLchar * name
Definition: glcorearb.h:786
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
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
static void toVec4(const TypeDesc *type, string &variable)
#define MX_GENGLSL_API
Definition: Export.h:18