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 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
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 const 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 function definitions for lighting code
79  virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
80 
81  static void toVec4(const TypeDesc* type, string& variable);
82 
83  /// Nodes used internally for light sampling.
84  vector<ShaderNodePtr> _lightSamplingNodes;
85 };
86 
87 
88 /// Base class for common GLSL node implementations
90 {
91  public:
92  const string& getTarget() const override;
93 
94  bool isEditable(const ShaderInput& input) const override;
95 
96  protected:
98 
99  // Integer identifiers for coordinate spaces.
100  // The order must match the order given for
101  // the space enum string in stdlib.
102  enum Space
103  {
104  MODEL_SPACE = 0,
105  OBJECT_SPACE = 1,
106  WORLD_SPACE = 2
107  };
108 
109  /// Internal string constants
110  static const string SPACE;
111  static const string TO_SPACE;
112  static const string FROM_SPACE;
113  static const string WORLD;
114  static const string OBJECT;
115  static const string MODEL;
116  static const string INDEX;
117  static const string GEOMPROP;
118 };
119 
121 
122 #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:23
virtual void emitVariableDeclaration(const ShaderPort *variable, const string &qualifier, GenContext &context, ShaderStage &stage, bool assignValue=true) const
shared_ptr< class GlslShaderGenerator > GlslShaderGeneratorPtr
GLenum GLenum GLenum input
Definition: glew.h:14162
Base class for common GLSL node implementations.
static const string TARGET
Unique identifier for this generator target.
virtual bool isEditable(const ShaderInput &) const
GLuint const GLchar * name
Definition: glcorearb.h:786
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.
virtual ShaderPtr generate(const string &name, ElementPtr element, GenContext &context) const =0
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
static const string FROM_SPACE
GLenum GLenum variable
Definition: glew.h:14162
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:30
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
static ShaderGeneratorPtr create()
type
Definition: core.h:1059
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
static const string GEOMPROP
#define MX_GENGLSL_API
Definition: Export.h:18