MaterialX 1.38.2
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
11
13
18
20
21namespace MaterialX
22{
23
24// Shared pointer to a GlslProgram
25using GlslProgramPtr = std::shared_ptr<class GlslProgram>;
26
32class MX_RENDERGLSL_API GlslProgram
33{
34 public:
36 static GlslProgramPtr create()
37 {
38 return GlslProgramPtr(new GlslProgram());
39 }
40
42 virtual ~GlslProgram();
43
46
49 void setStages(ShaderPtr shader);
50
55 void addStage(const string& stage, const string& sourcCode);
56
59 const string& getStageSourceCode(const string& stage) const;
60
62 void clearStages();
63
67
72 unsigned int build();
73
78 struct MX_RENDERGLSL_API Input
79 {
80 static int INVALID_OPENGL_TYPE;
81
85 int gltype;
87 int size;
89 std::string typeString;
96 string path;
98 string unit;
99
101 Input(int inputLocation, int inputType, int inputSize, string inputPath)
102 : location(inputLocation)
103 , gltype(inputType)
104 , size(inputSize)
105 , isConstant(false)
106 , path(inputPath)
107 { }
108 };
110 using InputPtr = std::shared_ptr<Input>;
112 using InputMap = std::unordered_map<std::string, InputPtr>;
113
118 const InputMap& getUniformsList();
119
124 const InputMap& getAttributesList();
125
131 void findInputs(const std::string& variable,
132 const InputMap& variableList,
133 InputMap& foundList,
134 bool exactMatch);
135
139
142 bool bind();
143
145 void bindInputs(ViewHandlerPtr viewHandler,
146 GeometryHandlerPtr geometryHandler,
147 ImageHandlerPtr imageHandler,
148 LightHandlerPtr lightHandler);
149
151 void unbindInputs(ImageHandlerPtr imageHandler);
152
154 bool hasActiveAttributes() const;
155
157 bool hasUniform(const string& name);
158
160 void bindUniform(const string& name, ConstValuePtr value, bool errorIfMissing = true);
161
167 void bindAttribute(const GlslProgram::InputMap& inputs, MeshPtr mesh);
168
170 void bindPartition(MeshPartitionPtr partition);
171
173 void bindMesh(MeshPtr mesh);
174
176 void unbindGeometry();
177
179 void bindTextures(ImageHandlerPtr imageHandler);
180
182 void bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr imageHandler);
183
185 void bindViewInformation(ViewHandlerPtr viewHandler);
186
188 void bindTimeAndFrame();
189
191 void unbind() const;
192
196
198 void printUniforms(std::ostream& outputStream);
199
201 void printAttributes(std::ostream& outputStream);
202
204
205 public:
206 static unsigned int UNDEFINED_OPENGL_RESOURCE_ID;
207 static int UNDEFINED_OPENGL_PROGRAM_LOCATION;
208
209 protected:
210 GlslProgram();
211
212 // Update a list of program input uniforms
213 const InputMap& updateUniformsList();
214
215 // Update a list of program input attributes
216 const InputMap& updateAttributesList();
217
218 // Clear out any cached input lists
219 void clearInputLists();
220
221 // Utility to find a uniform value in an uniform list.
222 // If uniform cannot be found a null pointer will be return.
223 MaterialX::ValuePtr findUniformValue(const std::string& uniformName, const InputMap& uniformList);
224
225 // Bind an individual texture to a program uniform location
226 ImagePtr bindTexture(unsigned int uniformType, int uniformLocation, const FilePath& filePath,
227 ImageHandlerPtr imageHandler, const ImageSamplingProperties& imageProperties);
228
229 // Delete any currently created shader program
230 void deleteProgram();
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<std::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 // Program texture map
265 std::unordered_map<std::string, unsigned int> _programTextures;
266
267 // Enabled vertex stream program locations
268 std::set<int> _enabledStreamLocations;
269
270 std::string _lastGeometryName;
271};
272
273} // namespace MaterialX
274
275#endif
Geometry loader interfaces.
std::shared_ptr< class GeometryHandler > GeometryHandlerPtr
Shared pointer to an GeometryHandler.
Definition: GeometryHandler.h:55
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:24
Image handler interfaces.
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:33
Handler for hardware lights.
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:23
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:58
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
Macros for declaring imported and exported symbols.
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:138
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:207
Shader instance class created during shader generation.
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:32
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
Utility for providing view data.
std::shared_ptr< class ViewHandler > ViewHandlerPtr
Shared pointer to a ViewHandler.
Definition: ViewHandler.h:20
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:28
A class representing an executable GLSL program.
Definition: GlslProgram.h:33
std::shared_ptr< Input > InputPtr
Program input structure shared pointer type.
Definition: GlslProgram.h:110
static GlslProgramPtr create()
Create a GLSL program instance.
Definition: GlslProgram.h:36
std::unordered_map< std::string, InputPtr > InputMap
Program input shaded pointer map type.
Definition: GlslProgram.h:112
Interface to describe sampling properties for images.
Definition: ImageHandler.h:44
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:29
Structure to hold information about program inputs.
Definition: GlslProgram.h:79
int gltype
OpenGL type of the input. -1 means an invalid type.
Definition: GlslProgram.h:85
int size
Size.
Definition: GlslProgram.h:87
string path
Element path (if any)
Definition: GlslProgram.h:96
string unit
Unit.
Definition: GlslProgram.h:98
int location
Program location. -1 means an invalid location.
Definition: GlslProgram.h:83
MaterialX::ValuePtr value
Input value.
Definition: GlslProgram.h:92
std::string typeString
Input type string. Will only be non-empty if initialized stages with a HwShader.
Definition: GlslProgram.h:89
Input(int inputLocation, int inputType, int inputSize, string inputPath)
Program input constructor.
Definition: GlslProgram.h:101
bool isConstant
Is this a constant.
Definition: GlslProgram.h:94