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  virtual string getVertexDataPrefix(const VariableBlock& vertexData) const;
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  bool isEditable(const ShaderInput& input) const override;
97 
98  protected:
100 
101  // Integer identifiers for coordinate spaces.
102  // The order must match the order given for
103  // the space enum string in stdlib.
104  enum Space
105  {
106  MODEL_SPACE = 0,
107  OBJECT_SPACE = 1,
108  WORLD_SPACE = 2
109  };
110 
111  /// Internal string constants
112  static const string SPACE;
113  static const string TO_SPACE;
114  static const string FROM_SPACE;
115  static const string WORLD;
116  static const string OBJECT;
117  static const string MODEL;
118  static const string INDEX;
119  static const string GEOMPROP;
120 };
121 
123 
124 #endif
vector< ShaderNodePtr > _lightSamplingNodes
Nodes used internally for light sampling.
static const string TO_SPACE
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
static const string SPACE
Internal string constants.
#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.
virtual bool isEditable(const ShaderInput &) const
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 ShaderNodeImplPtr getImplementation(const NodeDef &nodedef, GenContext &context) const
Return a registered shader node implementation for the given nodedef.
virtual const string & getTarget() const
static const string OBJECT
GLuint const GLchar * name
Definition: glcorearb.h:786
static const string FROM_SPACE
static const string INDEX
static const string MODEL
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 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
static const string GEOMPROP
#define MX_GENGLSL_API
Definition: Export.h:18