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 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
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 sourceCode Source code of the shader stage.
54  void addStage(const string& stage, const string& sourceCode);
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  /// Return the shader, if any, used to generate this program.
62  {
63  return _shader;
64  }
65 
66  /// @}
67  /// @name Program building
68  /// @{
69 
70  /// Build shader program data from the source code set for
71  /// each shader stage.
72  ///
73  /// An exception is thrown if the program cannot be built.
74  /// The exception will contain a list of compilation errors.
75  void build();
76 
77  /// Return true if built shader program data is present.
78  bool hasBuiltData();
79 
80  // Clear built shader program data, if any.
81  void clearBuiltData();
82 
83  /// @}
84  /// @name Program introspection
85  /// @{
86 
87  /// Structure to hold information about program inputs.
88  /// The structure is populated by directly scanning the program so may not contain
89  /// some inputs listed on any associated HwShader as those inputs may have been
90  /// optimized out if they are unused.
92  {
93  static int INVALID_OPENGL_TYPE;
94 
95  /// Program location. -1 means an invalid location
96  int location;
97  /// OpenGL type of the input. -1 means an invalid type
98  int gltype;
99  /// Size.
100  int size;
101  /// Input type string. Will only be non-empty if initialized stages with a HwShader
102  string typeString;
103  /// Input value. Will only be non-empty if initialized stages with a HwShader and a value was set during
104  /// shader generation.
106  /// Is this a constant
108  /// Element path (if any)
109  string path;
110  /// Unit
111  string unit;
112  /// Colorspace
113  string colorspace;
114 
115  /// Program input constructor
116  Input(int inputLocation, int inputType, int inputSize, const string& inputPath) :
117  location(inputLocation),
118  gltype(inputType),
119  size(inputSize),
120  isConstant(false),
121  path(inputPath)
122  { }
123  };
124  /// Program input structure shared pointer type
125  using InputPtr = std::shared_ptr<Input>;
126  /// Program input shaded pointer map type
127  using InputMap = std::unordered_map<string, InputPtr>;
128 
129  /// Get list of program input uniforms.
130  /// The program must have been created successfully first.
131  /// An exception is thrown if the parsing of the program for uniforms cannot be performed.
132  /// @return Program uniforms list.
133  const InputMap& getUniformsList();
134 
135  /// Get list of program input attributes.
136  /// The program must have been created successfully first.
137  /// An exception is thrown if the parsing of the program for attribute cannot be performed.
138  /// @return Program attributes list.
139  const InputMap& getAttributesList();
140 
141  /// Find the locations in the program which starts with a given variable name
142  /// @param variable Variable to search for
143  /// @param variableList List of program inputs to search
144  /// @param foundList Returned list of found program inputs. Empty if none found.
145  /// @param exactMatch Search for exact variable name match.
146  void findInputs(const string& variable,
147  const InputMap& variableList,
148  InputMap& foundList,
149  bool exactMatch);
150 
151  /// @}
152  /// @name Program activation
153  /// @{
154 
155  /// Bind the program.
156  /// @return False if failed
157  bool bind();
158 
159  /// Return true if the program has active attributes.
160  bool hasActiveAttributes() const;
161 
162  /// Return true if a uniform with the given name is present.
163  bool hasUniform(const string& name);
164 
165  /// Bind a value to the uniform with the given name.
166  void bindUniform(const string& name, ConstValuePtr value, bool errorIfMissing = true);
167 
168  /// Bind attribute buffers to attribute inputs.
169  /// A hardware buffer of the given attribute type is created and bound to the program locations
170  /// for the input attribute.
171  /// @param inputs Attribute inputs to bind to
172  /// @param mesh Mesh containing streams to bind
173  void bindAttribute(const GlslProgram::InputMap& inputs, MeshPtr mesh);
174 
175  /// Bind input geometry partition (indexing)
176  void bindPartition(MeshPartitionPtr partition);
177 
178  /// Bind input geometry streams
179  void bindMesh(MeshPtr mesh);
180 
181  /// Unbind any bound geometry
182  void unbindGeometry();
183 
184  /// Bind any input textures
185  void bindTextures(ImageHandlerPtr imageHandler);
186 
187  /// Bind lighting
188  void bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr imageHandler);
189 
190  /// Bind view information
191  void bindViewInformation(CameraPtr camera);
192 
193  /// Bind time and frame
194  void bindTimeAndFrame(float time = 1.0f, float frame = 1.0f);
195 
196  /// Unbind the program. Equivalent to binding no program
197  void unbind() const;
198 
199  /// @}
200  /// @name Utilities
201  /// @{
202 
203  /// Print all uniforms to the given stream.
204  void printUniforms(std::ostream& outputStream);
205 
206  /// Print all attributes to the given stream.
207  void printAttributes(std::ostream& outputStream);
208 
209  /// @}
210 
211  public:
212  static unsigned int UNDEFINED_OPENGL_RESOURCE_ID;
214 
215  protected:
216  GlslProgram();
217 
218  // Update a list of program input uniforms
219  const InputMap& updateUniformsList();
220 
221  // Update a list of program input attributes
222  const InputMap& updateAttributesList();
223 
224  // Utility to find a uniform value in an uniform list.
225  // If uniform cannot be found a null pointer will be return.
226  ValuePtr findUniformValue(const string& uniformName, const InputMap& uniformList);
227 
228  // Bind an individual texture to a program uniform location
229  ImagePtr bindTexture(unsigned int uniformType, int uniformLocation, const FilePath& filePath,
230  ImageHandlerPtr imageHandler, const ImageSamplingProperties& imageProperties);
231 
232  // Utility to map a MaterialX type to an OpenGL type
233  static int mapTypeToOpenGLType(const TypeDesc* type);
234 
235  // Bind a value to the uniform at the given location.
236  void bindUniformLocation(int location, ConstValuePtr value);
237 
238  private:
239  // Stages used to create program
240  // Map of stage name and its source code
241  StringMap _stages;
242 
243  // Generated program. A non-zero number indicates a valid shader program.
244  unsigned int _programId;
245 
246  // List of program input uniforms
247  InputMap _uniformList;
248  // List of program input attributes
249  InputMap _attributeList;
250 
251  // Hardware shader (if any) used for program creation
252  ShaderPtr _shader;
253 
254  // Attribute buffer resource handles
255  // for each attribute identifier in the program
256  std::unordered_map<string, unsigned int> _attributeBufferIds;
257 
258  // Attribute indexing buffer handle
259  std::map<MeshPartitionPtr, unsigned int> _indexBufferIds;
260 
261  // Attribute vertex array handle
262  unsigned int _vertexArray;
263 
264  // Currently bound mesh
265  MeshPtr _boundMesh;
266 
267  // Program texture map
268  std::unordered_map<string, unsigned int> _programTextures;
269 
270  // Enabled vertex stream program locations
271  std::set<int> _enabledStreamLocations;
272 };
273 
275 
276 #endif
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:230
static int INVALID_OPENGL_TYPE
Definition: GlslProgram.h:93
std::shared_ptr< class Camera > CameraPtr
Shared pointer to a Camera.
Definition: Camera.h:14
string colorspace
Colorspace.
Definition: GlslProgram.h:113
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:25
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:146
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
std::unordered_map< string, InputPtr > InputMap
Program input shaded pointer map type.
Definition: GlslProgram.h:127
int gltype
OpenGL type of the input. -1 means an invalid type.
Definition: GlslProgram.h:98
string typeString
Input type string. Will only be non-empty if initialized stages with a HwShader.
Definition: GlslProgram.h:102
int location
Program location. -1 means an invalid location.
Definition: GlslProgram.h:96
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:25
MaterialX::ValuePtr value
Definition: GlslProgram.h:105
GLfloat f
Definition: glcorearb.h:1926
std::shared_ptr< class GlslProgram > GlslProgramPtr
Definition: GlslProgram.h:24
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
GLint location
Definition: glcorearb.h:805
string unit
Unit.
Definition: GlslProgram.h:111
GLuint const GLchar * name
Definition: glcorearb.h:786
#define MX_RENDERGLSL_API
Definition: Export.h:18
GLsizeiptr size
Definition: glcorearb.h:664
Input(int inputLocation, int inputType, int inputSize, const string &inputPath)
Program input constructor.
Definition: GlslProgram.h:116
GLuint shader
Definition: glcorearb.h:785
bool isConstant
Is this a constant.
Definition: GlslProgram.h:107
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
static unsigned int UNDEFINED_OPENGL_RESOURCE_ID
Definition: GlslProgram.h:212
std::shared_ptr< Input > InputPtr
Program input structure shared pointer type.
Definition: GlslProgram.h:125
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
ShaderPtr getShader() const
Return the shader, if any, used to generate this program.
Definition: GlslProgram.h:61
string path
Element path (if any)
Definition: GlslProgram.h:109
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
static int UNDEFINED_OPENGL_PROGRAM_LOCATION
Definition: GlslProgram.h:213
type
Definition: core.h:1059
GLuint GLsizei GLsizei GLchar * uniformName
Definition: glcorearb.h:1464