HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlslProgram.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_GLSLPROGRAM_H
7 #define MATERIALX_GLSLPROGRAM_H
8 
9 /// @file
10 /// GLSL Program interfaces
11 
13 
14 #include <MaterialXRender/Camera.h>
18 
20 
22 
23 // Shared pointer to a GlslProgram
24 using GlslProgramPtr = std::shared_ptr<class GlslProgram>;
25 
26 /// @class GlslProgram
27 /// A class representing an executable GLSL program.
28 ///
29 /// There are two main interfaces which can be used. One which takes in a HwShader and one which
30 /// allows for explicit setting of shader stage code.
32 {
33  public:
34  /// Create a GLSL program instance
36  {
37  return GlslProgramPtr(new GlslProgram());
38  }
39 
40  /// Destructor
41  virtual ~GlslProgram();
42 
43  /// @name Shader code setup
44  /// @{
45 
46  /// Set up code stages to validate based on an input hardware shader.
47  /// @param shader Hardware shader to use
48  void setStages(ShaderPtr shader);
49 
50  /// Set the code stages based on a list of stage strings.
51  /// Refer to the ordering of stages as defined by a HwShader.
52  /// @param stage Name of the shader stage.
53  /// @param sourcCode Source code of the shader stage.
54  void addStage(const string& stage, const string& sourcCode);
55 
56  /// Get source code string for a given stage.
57  /// @return Shader stage string. String is empty if not found.
58  const string& getStageSourceCode(const string& stage) const;
59 
60  /// Clear out any existing stages
61  void clearStages();
62 
63  /// Return the shader, if any, used to generate this program.
65  {
66  return _shader;
67  }
68 
69  /// @}
70  /// @name Program validation and introspection
71  /// @{
72 
73  /// Create the shader program from stages specified
74  /// An exception is thrown if the program cannot be created.
75  /// The exception will contain a list of program creation errors.
76  /// @return Program identifier.
77  unsigned int build();
78 
79  /// Structure to hold information about program inputs.
80  /// The structure is populated by directly scanning the program so may not contain
81  /// some inputs listed on any associated HwShader as those inputs may have been
82  /// optimized out if they are unused.
84  {
85  static int INVALID_OPENGL_TYPE;
86 
87  /// Program location. -1 means an invalid location
88  int location;
89  /// OpenGL type of the input. -1 means an invalid type
90  int gltype;
91  /// Size.
92  int size;
93  /// Input type string. Will only be non-empty if initialized stages with a HwShader
95  /// Input value. Will only be non-empty if initialized stages with a HwShader and a value was set during
96  /// shader generation.
98  /// Is this a constant
99  bool isConstant;
100  /// Element path (if any)
101  string path;
102  /// Unit
103  string unit;
104  /// Colorspace
105  string colorspace;
106 
107  /// Program input constructor
108  Input(int inputLocation, int inputType, int inputSize, string inputPath)
109  : location(inputLocation)
110  , gltype(inputType)
111  , size(inputSize)
112  , isConstant(false)
113  , path(inputPath)
114  { }
115  };
116  /// Program input structure shared pointer type
117  using InputPtr = std::shared_ptr<Input>;
118  /// Program input shaded pointer map type
119  using InputMap = std::unordered_map<std::string, InputPtr>;
120 
121  /// Get list of program input uniforms.
122  /// The program must have been created successfully first.
123  /// An exception is thrown if the parsing of the program for uniforms cannot be performed.
124  /// @return Program uniforms list.
125  const InputMap& getUniformsList();
126 
127  /// Get list of program input attributes.
128  /// The program must have been created successfully first.
129  /// An exception is thrown if the parsing of the program for attribute cannot be performed.
130  /// @return Program attributes list.
131  const InputMap& getAttributesList();
132 
133  /// Find the locations in the program which starts with a given variable name
134  /// @param variable Variable to search for
135  /// @param variableList List of program inputs to search
136  /// @param foundList Returned list of found program inputs. Empty if none found.
137  /// @param exactMatch Search for exact variable name match.
138  void findInputs(const std::string& variable,
139  const InputMap& variableList,
140  InputMap& foundList,
141  bool exactMatch);
142 
143  /// @}
144  /// @name Program activation
145  /// @{
146 
147  /// Bind the program.
148  /// @return False if failed
149  bool bind();
150 
151  /// Return true if the program has active attributes.
152  bool hasActiveAttributes() const;
153 
154  /// Return true if a uniform with the given name is present.
155  bool hasUniform(const string& name);
156 
157  /// Bind a value to the uniform with the given name.
158  void bindUniform(const string& name, ConstValuePtr value, bool errorIfMissing = true);
159 
160  /// Bind attribute buffers to attribute inputs.
161  /// A hardware buffer of the given attribute type is created and bound to the program locations
162  /// for the input attribute.
163  /// @param inputs Attribute inputs to bind to
164  /// @param mesh Mesh containing streams to bind
165  void bindAttribute(const GlslProgram::InputMap& inputs, MeshPtr mesh);
166 
167  /// Bind input geometry partition (indexing)
168  void bindPartition(MeshPartitionPtr partition);
169 
170  /// Bind input geometry streams
171  void bindMesh(MeshPtr mesh);
172 
173  /// Unbind any bound geometry
174  void unbindGeometry();
175 
176  /// Bind any input textures
177  void bindTextures(ImageHandlerPtr imageHandler);
178 
179  /// Bind lighting
180  void bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr imageHandler);
181 
182  /// Bind view information
183  void bindViewInformation(CameraPtr camera);
184 
185  /// Bind time and frame
186  void bindTimeAndFrame(float time = 1.0f, float frame = 1.0f);
187 
188  /// Unbind the program. Equivalent to binding no program
189  void unbind() const;
190 
191  /// @}
192  /// @name Utilities
193  /// @{
194 
195  /// Print all uniforms to the given stream.
196  void printUniforms(std::ostream& outputStream);
197 
198  /// Print all attributes to the given stream.
199  void printAttributes(std::ostream& outputStream);
200 
201  /// @}
202 
203  public:
204  static unsigned int UNDEFINED_OPENGL_RESOURCE_ID;
206 
207  protected:
208  GlslProgram();
209 
210  // Update a list of program input uniforms
211  const InputMap& updateUniformsList();
212 
213  // Update a list of program input attributes
214  const InputMap& updateAttributesList();
215 
216  // Clear out any cached input lists
217  void clearInputLists();
218 
219  // Utility to find a uniform value in an uniform list.
220  // If uniform cannot be found a null pointer will be return.
221  MaterialX::ValuePtr findUniformValue(const std::string& uniformName, const InputMap& uniformList);
222 
223  // Bind an individual texture to a program uniform location
224  ImagePtr bindTexture(unsigned int uniformType, int uniformLocation, const FilePath& filePath,
225  ImageHandlerPtr imageHandler, const ImageSamplingProperties& imageProperties);
226 
227  // Delete any currently created shader program
228  void deleteProgram();
229 
230  // Utility to map a MaterialX type to an OpenGL type
231  static int mapTypeToOpenGLType(const TypeDesc* type);
232 
233  // Bind a value to the uniform at the given location.
234  void bindUniformLocation(int location, ConstValuePtr value);
235 
236  private:
237  // Stages used to create program
238  // Map of stage name and its source code
239  StringMap _stages;
240 
241  // Generated program. A non-zero number indicates a valid shader program.
242  unsigned int _programId;
243 
244  // List of program input uniforms
245  InputMap _uniformList;
246  // List of program input attributes
247  InputMap _attributeList;
248 
249  // Hardware shader (if any) used for program creation
250  ShaderPtr _shader;
251 
252  // Attribute buffer resource handles
253  // for each attribute identifier in the program
254  std::unordered_map<std::string, unsigned int> _attributeBufferIds;
255 
256  // Attribute indexing buffer handle
257  std::map<MeshPartitionPtr, unsigned int> _indexBufferIds;
258 
259  // Attribute vertex array handle
260  unsigned int _vertexArray;
261 
262  // Program texture map
263  std::unordered_map<std::string, unsigned int> _programTextures;
264 
265  // Enabled vertex stream program locations
266  std::set<int> _enabledStreamLocations;
267 };
268 
270 
271 #endif
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:221
static int INVALID_OPENGL_TYPE
Definition: GlslProgram.h:85
std::shared_ptr< class Camera > CameraPtr
Shared pointer to a Camera.
Definition: Camera.h:14
string colorspace
Colorspace.
Definition: GlslProgram.h:105
GLint location
Definition: glcorearb.h:805
Definition: File.h:26
void partition(I begin, I middle, I end, const Pred &pred, I *out_eqbeg, I *out_eqend)
Definition: pugixml.cpp:7255
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:137
GT_API const UT_StringHolder time
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
static GlslProgramPtr create()
Create a GLSL program instance.
Definition: GlslProgram.h:35
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int gltype
OpenGL type of the input. -1 means an invalid type.
Definition: GlslProgram.h:90
GLuint const GLchar * name
Definition: glcorearb.h:786
Input(int inputLocation, int inputType, int inputSize, string inputPath)
Program input constructor.
Definition: GlslProgram.h:108
int location
Program location. -1 means an invalid location.
Definition: GlslProgram.h:88
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:24
std::string typeString
Input type string. Will only be non-empty if initialized stages with a HwShader.
Definition: GlslProgram.h:94
GLsizeiptr size
Definition: glcorearb.h:664
MaterialX::ValuePtr value
Definition: GlslProgram.h:97
std::shared_ptr< class GlslProgram > GlslProgramPtr
Definition: GlslProgram.h:24
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
string unit
Unit.
Definition: GlslProgram.h:103
#define MX_RENDERGLSL_API
Definition: Export.h:18
GLenum GLenum variable
Definition: glew.h:14162
GLuint shader
Definition: glcorearb.h:785
std::unordered_map< std::string, InputPtr > InputMap
Program input shaded pointer map type.
Definition: GlslProgram.h:119
bool isConstant
Is this a constant.
Definition: GlslProgram.h:99
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
GLfloat f
Definition: glcorearb.h:1926
static unsigned int UNDEFINED_OPENGL_RESOURCE_ID
Definition: GlslProgram.h:204
std::shared_ptr< Input > InputPtr
Program input structure shared pointer type.
Definition: GlslProgram.h:117
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
ShaderPtr getShader() const
Return the shader, if any, used to generate this program.
Definition: GlslProgram.h:64
string path
Element path (if any)
Definition: GlslProgram.h:101
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
type
Definition: core.h:1059
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
static int UNDEFINED_OPENGL_PROGRAM_LOCATION
Definition: GlslProgram.h:205
GLuint GLsizei GLsizei GLchar * uniformName
Definition: glcorearb.h:1464