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:
26 
27  static ShaderGeneratorPtr create() { return std::make_shared<GlslShaderGenerator>(); }
28 
29  /// Generate a shader starting from the given element, translating
30  /// the element and all dependencies upstream into shader code.
31  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
32 
33  /// Return a unique identifier for the target this generator is for
34  const string& getTarget() const override { return TARGET; }
35 
36  /// Return the version string for the GLSL version this generator is for
37  virtual const string& getVersion() const { return VERSION; }
38 
39  /// Emit a shader variable.
40  void emitVariableDeclaration(const ShaderPort* variable, const string& qualifier, GenContext& context, ShaderStage& stage,
41  bool assignValue = true) const override;
42 
43  /// Return a registered shader node implementation given an implementation element.
44  /// The element must be an Implementation or a NodeGraph acting as implementation.
45  ShaderNodeImplPtr getImplementation(const NodeDef& nodedef, GenContext& context) const override;
46 
47  /// Determine the prefix of vertex data variables.
48  string getVertexDataPrefix(const VariableBlock& vertexData) const override;
49 
50  public:
51  /// Unique identifier for this generator target
52  static const string TARGET;
53 
54  /// Version string for the generator target
55  static const string VERSION;
56 
57  protected:
58  virtual void emitVertexStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
59  virtual void emitPixelStage(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
60 
61  virtual void emitDirectives(GenContext& context, ShaderStage& stage) const;
62  virtual void emitConstants(GenContext& context, ShaderStage& stage) const;
63  virtual void emitUniforms(GenContext& context, ShaderStage& stage) const;
64  virtual void emitLightData(GenContext& context, ShaderStage& stage) const;
65  virtual void emitInputs(GenContext& context, ShaderStage& stage) const;
66  virtual void emitOutputs(GenContext& context, ShaderStage& stage) const;
67 
68  virtual HwResourceBindingContextPtr getResourceBindingContext(GenContext& context) const;
69 
70  /// Logic to indicate whether code to support direct lighting should be emitted.
71  /// By default if the graph is classified as a shader, or BSDF node then lighting is assumed to be required.
72  /// Derived classes can override this logic.
73  virtual bool requiresLighting(const ShaderGraph& graph) const;
74 
75  /// Emit specular environment lookup code
76  virtual void emitSpecularEnvironment(GenContext& context, ShaderStage& stage) const;
77 
78  /// Emit transmission rendering code
79  virtual void emitTransmissionRender(GenContext& context, ShaderStage& stage) const;
80 
81  /// Emit function definitions for lighting code
82  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
83 
84  static void toVec4(const TypeDesc* type, string& variable);
85 
86  /// Nodes used internally for light sampling.
87  vector<ShaderNodePtr> _lightSamplingNodes;
88 };
89 
90 /// Base class for common GLSL node implementations
92 {
93  public:
94  const string& getTarget() const override;
95 
96  protected:
98 };
99 
101 
102 #endif
vector< ShaderNodePtr > _lightSamplingNodes
Nodes used internally for light sampling.
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
shared_ptr< class GlslShaderGenerator > GlslShaderGeneratorPtr
Base class for common GLSL node implementations.
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.
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.
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:34
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
static ShaderGeneratorPtr create()
type
Definition: core.h:1059
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const
#define MX_GENGLSL_API
Definition: Export.h:18