MaterialX 1.38.10
Loading...
Searching...
No Matches
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
11
13
14#include <MaterialXRender/Camera.h>
18
20
21MATERIALX_NAMESPACE_BEGIN
22
23// Shared pointer to a GlslProgram
24using GlslProgramPtr = std::shared_ptr<class GlslProgram>;
25
31class MX_RENDERGLSL_API GlslProgram
32{
33 public:
35 static GlslProgramPtr create()
36 {
37 return GlslProgramPtr(new GlslProgram());
38 }
39
41 virtual ~GlslProgram();
42
45
48 void setStages(ShaderPtr shader);
49
54 void addStage(const string& stage, const string& sourceCode);
55
58 const string& getStageSourceCode(const string& stage) const;
59
62 {
63 return _shader;
64 }
65
69
75 void build();
76
78 bool hasBuiltData();
79
80 // Clear built shader program data, if any.
81 void clearBuiltData();
82
86
91 struct MX_RENDERGLSL_API Input
92 {
93 static int INVALID_OPENGL_TYPE;
94
98 int gltype;
100 int size;
105 MaterialX::ValuePtr value;
109 string path;
111 string unit;
114
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 };
125 using InputPtr = std::shared_ptr<Input>;
127 using InputMap = std::unordered_map<string, InputPtr>;
128
133 const InputMap& getUniformsList();
134
139 const InputMap& getAttributesList();
140
146 void findInputs(const string& variable,
147 const InputMap& variableList,
148 InputMap& foundList,
149 bool exactMatch);
150
154
157 bool bind();
158
160 bool hasActiveAttributes() const;
161
163 bool hasUniform(const string& name);
164
166 void bindUniform(const string& name, ConstValuePtr value, bool errorIfMissing = true);
167
173 void bindAttribute(const GlslProgram::InputMap& inputs, MeshPtr mesh);
174
176 void bindPartition(MeshPartitionPtr partition);
177
179 void bindMesh(MeshPtr mesh);
180
182 void unbindGeometry();
183
185 void bindTextures(ImageHandlerPtr imageHandler);
186
188 void bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr imageHandler);
189
191 void bindViewInformation(CameraPtr camera);
192
194 void bindTimeAndFrame(float time = 1.0f, float frame = 1.0f);
195
197 void unbind() const;
198
202
204 void printUniforms(std::ostream& outputStream);
205
207 void printAttributes(std::ostream& outputStream);
208
210
211 public:
212 static unsigned int UNDEFINED_OPENGL_RESOURCE_ID;
213 static int UNDEFINED_OPENGL_PROGRAM_LOCATION;
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
274MATERIALX_NAMESPACE_END
275
276#endif
Geometry loader interfaces.
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
Image handler interfaces.
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
Handler for hardware lights.
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:25
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
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:146
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:230
Shader instance class created during shader generation.
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
A class representing an executable GLSL program.
Definition: GlslProgram.h:32
std::shared_ptr< Input > InputPtr
Program input structure shared pointer type.
Definition: GlslProgram.h:125
std::unordered_map< string, InputPtr > InputMap
Program input shaded pointer map type.
Definition: GlslProgram.h:127
static GlslProgramPtr create()
Create a GLSL program instance.
Definition: GlslProgram.h:35
ShaderPtr getShader() const
Return the shader, if any, used to generate this program.
Definition: GlslProgram.h:61
Interface to describe sampling properties for images.
Definition: ImageHandler.h:43
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:28
Structure to hold information about program inputs.
Definition: GlslProgram.h:92
int gltype
OpenGL type of the input. -1 means an invalid type.
Definition: GlslProgram.h:98
string colorspace
Colorspace.
Definition: GlslProgram.h:113
string typeString
Input type string. Will only be non-empty if initialized stages with a HwShader.
Definition: GlslProgram.h:102
int size
Size.
Definition: GlslProgram.h:100
string path
Element path (if any)
Definition: GlslProgram.h:109
Input(int inputLocation, int inputType, int inputSize, const string &inputPath)
Program input constructor.
Definition: GlslProgram.h:116
string unit
Unit.
Definition: GlslProgram.h:111
int location
Program location. -1 means an invalid location.
Definition: GlslProgram.h:96
MaterialX::ValuePtr value
Input value.
Definition: GlslProgram.h:105
bool isConstant
Is this a constant.
Definition: GlslProgram.h:107