MaterialX 1.39.1
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
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
134
140
146 void findInputs(const string& variable,
147 const InputMap& variableList,
148 InputMap& foundList,
149 bool exactMatch);
150
154
157 bool bind();
158
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
177
179 void bindMesh(MeshPtr mesh);
180
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(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:57
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
const InputMap & getAttributesList()
Get list of program input attributes.
std::shared_ptr< Input > InputPtr
Program input structure shared pointer type.
Definition: GlslProgram.h:125
void printAttributes(std::ostream &outputStream)
Print all attributes to the given stream.
bool hasActiveAttributes() const
Return true if the program has active attributes.
void bindMesh(MeshPtr mesh)
Bind input geometry streams.
bool bind()
Bind the program.
bool hasBuiltData()
Return true if built shader program data is present.
void bindTextures(ImageHandlerPtr imageHandler)
Bind any input textures.
void printUniforms(std::ostream &outputStream)
Print all uniforms to the given stream.
void bindAttribute(const GlslProgram::InputMap &inputs, MeshPtr mesh)
Bind attribute buffers to attribute inputs.
std::unordered_map< string, InputPtr > InputMap
Program input shaded pointer map type.
Definition: GlslProgram.h:127
void bindUniform(const string &name, ConstValuePtr value, bool errorIfMissing=true)
Bind a value to the uniform with the given name.
static GlslProgramPtr create()
Create a GLSL program instance.
Definition: GlslProgram.h:35
void setStages(ShaderPtr shader)
Set up code stages to validate based on an input hardware shader.
void build()
Build shader program data from the source code set for each shader stage.
void bindPartition(MeshPartitionPtr partition)
Bind input geometry partition (indexing)
void unbindGeometry()
Unbind any bound geometry.
const string & getStageSourceCode(const string &stage) const
Get source code string for a given stage.
void bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr imageHandler)
Bind lighting.
void unbind() const
Unbind the program. Equivalent to binding no program.
void findInputs(const string &variable, const InputMap &variableList, InputMap &foundList, bool exactMatch)
Find the locations in the program which starts with a given variable name.
ShaderPtr getShader() const
Return the shader, if any, used to generate this program.
Definition: GlslProgram.h:61
const InputMap & getUniformsList()
Get list of program input uniforms.
virtual ~GlslProgram()
Destructor.
void bindViewInformation(CameraPtr camera)
Bind view information.
void addStage(const string &stage, const string &sourceCode)
Set the code stages based on a list of stage strings.
void bindTimeAndFrame(float time=1.0f, float frame=1.0f)
Bind time and frame.
bool hasUniform(const string &name)
Return true if a uniform with the given name is present.
Interface to describe sampling properties for images.
Definition: ImageHandler.h:43
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:35
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