Ocean
Loading...
Searching...
No Matches
GLESShaderProgram.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_RENDERING_GLES_SHADER_PROGRAM_H
9#define META_OCEAN_RENDERING_GLES_SHADER_PROGRAM_H
10
14
16
20
22
23#include <unordered_map>
24
25namespace Ocean
26{
27
28namespace Rendering
29{
30
31namespace GLESceneGraph
32{
33
34// Forward declaration.
35class GLESShaderProgram;
36
37// Forward declaration.
38class GLESFramebuffer;
39
40/**
41 * Definition of an object reference for shader program containers.
42 * @ingroup renderinggles
43 */
45
46/**
47 * This class implements a container for an OpenGL ES shader program.
48 * @ingroup renderinggles
49 */
50class OCEAN_RENDERING_GLES_EXPORT GLESShaderProgram :
51 public ShaderProgram,
52 public GLESAttribute
53{
54 public:
55
56 /**
57 * Definition of a pair combining a GL shader type and shader code parts.
58 */
59 typedef std::pair<GLenum, std::vector<const char*>> ShaderCodePair;
60
61 /**
62 * Definition of a vector holding shader code pairs.
63 */
64 typedef std::vector<ShaderCodePair> ShaderCodePairs;
65
66 protected:
67
68 /**
69 * This class implements a temporary store for shader parameters.
70 * The temporary storage is necessary to allow setting parameters from a different thread than the rendering thread.
71 * @tparam T The data type of the parameters
72 */
73 template <typename T>
75 {
76 public:
77
78 /**
79 * Definition of a map mapping parameter names to parameter values.
80 */
81 typedef std::unordered_map<std::string, T> ValueMap;
82
83 public:
84
85 /**
86 * Setting the value of a parameter.
87 * @param name The name of the parameter, must be valid
88 * @param value The value to be set
89 */
90 void setValue(const std::string& name, T&& value);
91
92 /**
93 * Setting the value of a parameter.
94 * @param name The name of the parameter, must be valid
95 * @param value The value to be set
96 */
97 void setValue(const std::string& name, const T& value);
98
99 /**
100 * Binds all parameters to a shader program.
101 * @param id The id of the shader program to which the values bill be bound, must be valid
102 */
103 void bindValues(const GLuint id) const;
104
105 protected:
106
107 /// All values.
109 };
110
111 /**
112 * Definition of a map mapping sampler indices to textures.
113 */
114 typedef std::unordered_map<SamplerIndex, TextureRef> SamplerMap;
115
116 public:
117
118 /**
119 * Creates a new OpenGL ES shader program container.
120 */
122
123 /**
124 * Destructs an OpenGL ES shader program container.
125 */
127
128 /**
129 * Returns the OpenGL ES shader program id.
130 * @return OpenGL ES program id
131 */
132 inline GLuint id() const;
133
134 /**
135 * Links a vertex and a fragment shader.
136 * @param programType The type of the shader program, must be valid
137 * @param vertexShader Vertex shader to link
138 * @param fragmentShader Fragment shader to link
139 * @param message Returning linker error or warning message.
140 * @return True, if succeeded
141 */
142 bool link(const ProgramType programType, const GLESShaderRef& vertexShader, const GLESShaderRef& fragmentShader, std::string& message);
143
144 /**
145 * Links several shaders.
146 * @param programType The type of the shader program, must be valid
147 * @param shaders The shaders to link
148 * @param message Returning linker error or warning message.
149 * @return True, if succeeded
150 */
151 bool link(const ProgramType programType, const std::vector<GLESShaderRef>& shaders, std::string& message);
152
153 /**
154 * Compiles and links a vertex and a fragment shader.
155 * @param programType The type of the shader program, must be valid
156 * @param vertexShaderCode Vertex shader code to compile and link, can be composed of several individual code blocks
157 * @param fragmentShaderCode Fragment shader code to compile and link, can be composed of several individual code blocks
158 * @param message Returning compiler or linker error or warning message.
159 * @return True, if succeeded
160 */
161 bool compileAndLink(const ProgramType programType, const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& fragmentShaderCode, std::string& message);
162
163 /**
164 * Compiles and links shaders.
165 * @param programType The type of the shader program, must be valid
166 * @param shaderCodePairs The pairs of shader types and shader codes, the shader codes can be composed of several individual code blocks
167 * @param message Returning compiler or linker error or warning message.
168 * @return True, if succeeded
169 */
170 bool compileAndLink(const ProgramType programType, const ShaderCodePairs& shaderCodePairs, std::string& message);
171
172 /**
173 * Uses the shader and binds the given projection and model matrices as OpenGL uniforms.
174 * @param projection The projection matrix used for this node
175 * @param camera_T_model The transformation between model and camera (aka Modelview matrix), must be valid
176 * @param camera_T_world The transformation between world and camera,(aka View matrix) must be valid
177 * @param normalMatrix Normal transformation matrix with is the transposed inverse of the upper 3x3 model view matrix
178 */
179 void bind(const SquareMatrix4& projection, const HomogenousMatrix4& camera_T_model, const HomogenousMatrix4& camera_T_world, const SquareMatrix3& normalMatrix) const;
180
181 /**
182 * Uses the shader and binds the given projection and model matrices as OpenGL uniforms.
183 * @param framebuffer The framebuffer which initiated the rendering process
184 * @param projection The projection matrix used for this node
185 * @param camera_T_model The transformation between model and camera (aka Modelview matrix), must be valid
186 * @param camera_T_world The transformation between world and camera,(aka View matrix) must be valid
187 * @param normalMatrix Normal transformation matrix with is the transposed inverse of the upper 3x3 model view matrix
188 */
189 void bind(const GLESFramebuffer& framebuffer, const SquareMatrix4& projection, const HomogenousMatrix4& camera_T_model, const HomogenousMatrix4& camera_T_world, const SquareMatrix3& normalMatrix);
190
191 /**
192 * Returns the shader type necessary to render an object with this attribute.
193 * @see GLESAttribute::necessaryShader().
194 */
195 ProgramType necessaryShader() const override;
196
197 /**
198 * Binds all (custom) attributes of this shader program.
199 * @see GLESAttribute::bindAttribute().
200 */
201 void bindAttribute(const GLESFramebuffer& framebuffer, GLESShaderProgram& shaderProgram) override;
202
203 /**
204 * Sets the shading code (a custom shader, not a shader from GLESProgramManager).
205 * @see ShaderProgram::setShader().
206 */
207 bool setShader(const ShaderLanguage shaderLanguage, const FilenamePairs& filenamePairs, std::string& errorMessage) override;
208
209 /**
210 * Sets the shading code (a custom shader, not a shader from GLESProgramManager).
211 * @see ShaderProgram::setShader().
212 */
213 bool setShader(const ShaderLanguage shaderLanguage, const std::string& vertexShaderCode, const std::string& fragmentShaderCode, std::string& errorMessage) override;
214
215 /**
216 * Sets the shading code.
217 * @see ShaderProgram::setShader().
218 */
219 bool setShader(const ShaderLanguage shaderLanguage, const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& fragmentShaderCode, std::string& errorMessage) override;
220
221 /**
222 * Sets the shader code.
223 * @see ShaderProgram::setShader().
224 */
225 bool setShader(const ShaderLanguage shaderLanguage, const CodePairs& codePairs, std::string& errorMessage) override;
226
227 /**
228 * Returns whether this shader program has a specific parameter.
229 * @see ShaderProgram::existParameter().
230 */
231 bool existParameter(const std::string& name) const override;
232
233 /**
234 * Returns the type of a specific parameter.
235 * @see ShaderProgram::parameterType().
236 */
237 ParameterType parameterType(const std::string& name) const override;
238
239 /**
240 * Returns the number of sampler parameters.
241 * @see ShaderProgram::samplerNumber().
242 */
243 unsigned int samplerNumber() const override;
244
245 /**
246 * Returns the index of a registered texture sample object.
247 * @see ShaderProgram::samplerIndex().
248 */
249 SamplerIndex samplerIndex(const TextureRef& sampler) const override;
250
251 /**
252 * Returns the number of elements of a specific parameter.
253 * @see ShaderProgram::parameterElements().
254 */
255 unsigned int parameterElements(const std::string& name) const override;
256
257 /**
258 * Sets the texture of a specified sampler.
259 * @see ShaderProgram::setSampler().
260 */
261 bool setSampler(const SamplerIndex index, const TextureRef& texture) override;
262
263 /**
264 * Sets a parameter by a given parameter name.
265 * @see ShaderProgram::setParameter().
266 */
267 bool setParameter(const std::string& name, const double value) override;
268
269 /**
270 * Sets a parameter by a given parameter name.
271 * @see ShaderProgram::setParameter().
272 */
273 bool setParameter(const std::string& name, const float value) override;
274
275 /**
276 * Sets a parameter by a given parameter name.
277 * @see ShaderProgram::setParameter().
278 */
279 bool setParameter(const std::string& name, const float* value, const unsigned int elements) override;
280
281 /**
282 * Sets a parameter by a given parameter name.
283 * @see ShaderProgram::setParameter().
284 */
285 bool setParameter(const std::string& name, const int value) override;
286
287 /**
288 * Sets a parameter by a given parameter name.
289 * @see ShaderProgram::setParameter().
290 */
291 bool setParameter(const std::string& name, const unsigned int value) override;
292
293 /**
294 * Sets a parameter by a given parameter name.
295 * @see ShaderProgram::setParameter().
296 */
297 bool setParameter(const std::string& name, const HomogenousMatrix4& value) override;
298
299 /**
300 * Sets a parameter by a given parameter name.
301 * @see ShaderProgram::setParameter().
302 */
303 bool setParameter(const std::string& name, const SquareMatrix3& value) override;
304
305 /**
306 * Sets a parameter by a given parameter name.
307 * @see ShaderProgram::setParameter().
308 */
309 bool setParameter(const std::string& name, const SquareMatrix4& value) override;
310
311 /**
312 * Sets a parameter by a given parameter name.
313 * @see ShaderProgram::setParameter().
314 */
315 bool setParameter(const std::string& name, const Vector2& value) override;
316
317 /**
318 * Sets a parameter by a given parameter name.
319 * @see ShaderProgram::setParameter().
320 */
321 bool setParameter(const std::string& name, const Vector3& value) override;
322
323 /**
324 * Sets a parameter by a given parameter name.
325 * @see ShaderProgram::setParameter().
326 */
327 bool setParameter(const std::string& name, const Vector4& value) override;
328
329 /**
330 * Sets a parameter by a given parameter name.
331 * @see ShaderProgram::setParameter().
332 */
333 bool setParameter(const std::string& name, const HomogenousMatrices4& value) override;
334
335 /**
336 * Sets a parameter by a given parameter name.
337 * @see ShaderProgram::setParameter().
338 */
339 bool setParameter(const std::string& name, const SquareMatrices3& value) override;
340
341 /**
342 * Returns whether this program holds a valid and successfully compiled and linked shader code.
343 * @see ShaderProgram::isCompiled().
344 */
345 bool isCompiled() const override;
346
347 /**
348 * Translates the OpenGL shader type to a readable string.
349 * @param shaderType The shader type to translate
350 * @return The readable string
351 */
352 static std::string translateShaderType(const GLenum shaderType);
353
354 protected:
355
356 /**
357 * Releases the shader program.
358 */
359 void release();
360
361 protected:
362
363 /// OpenGL ES shader program id.
364 GLuint id_;
365
366 /// The type of the shader program.
368
369 /// The shaders.
370 std::vector<GLESShaderRef> shaders_;
371
372 /// The map of texture samplers.
374
375 /// The map for floating point values.
377
378 /// The map for arrays of floating point value.
380
381 /// The map of integer values.
383
384 /// The map of integer values.
386
387 /// The map of HomogenousMatrix4 values.
389
390 /// The map of SquareMatrix3 values.
392
393 /// The map of SquareMatrix4 values.
395
396 /// The map of Vector2 values.
398
399 /// The map of Vector3 values.
401
402 /// The map of Vector4 values.
404
405 /// The map of HomogenousMatrices4 values.
407
408 /// The map of SquareMatrices3 values.
410};
411
412template <typename T>
413void GLESShaderProgram::Parameters<T>::setValue(const std::string& name, const T& value)
414{
415 valueMap_[name] = value;
416}
417
418template <typename T>
419void GLESShaderProgram::Parameters<T>::setValue(const std::string& name, T&& value)
420{
421 valueMap_.emplace(name, std::move(value));
422}
423
424template <>
425inline void GLESShaderProgram::Parameters<std::vector<float>>::bindValues(const GLuint id) const
426{
427 for (typename ValueMap::const_iterator i = valueMap_.cbegin(); i != valueMap_.cend(); ++i)
428 {
429 const std::string& name = i->first;
430 const std::vector<float>& value = i->second;
431
432 const GLint locationId = glGetUniformLocation(id, name.c_str());
433 if (locationId != -1)
434 {
435 setUniform(locationId, value.data(), value.size());
436 }
437 }
438}
439
440template <typename T>
442{
443 for (typename ValueMap::const_iterator i = valueMap_.cbegin(); i != valueMap_.cend(); ++i)
444 {
445 const std::string& name = i->first;
446 const T& value = i->second;
447
448 const GLint locationId = glGetUniformLocation(id, name.c_str());
449 if (locationId != -1)
450 {
451 setUniform(locationId, value);
452 }
453 }
454}
455
456inline GLuint GLESShaderProgram::id() const
457{
458 return id_;
459}
460
461}
462
463}
464
465}
466
467#endif // META_OCEAN_RENDERING_GLES_SHADER_PROGRAM_H
This template class implements a object reference with an internal reference counter.
Definition base/ObjectRef.h:58
This class wraps a GLESceneGraph attribute object.
Definition GLESAttribute.h:36
ProgramType
Definition of different shader functionalities.
Definition GLESAttribute.h:43
This class implements a base for all GLESceneGraph framebuffers.
Definition rendering/glescenegraph/GLESFramebuffer.h:34
This class implements a temporary store for shader parameters.
Definition GLESShaderProgram.h:75
std::unordered_map< std::string, T > ValueMap
Definition of a map mapping parameter names to parameter values.
Definition GLESShaderProgram.h:81
void setValue(const std::string &name, const T &value)
Setting the value of a parameter.
Definition GLESShaderProgram.h:413
void setValue(const std::string &name, T &&value)
Setting the value of a parameter.
Definition GLESShaderProgram.h:419
ValueMap valueMap_
All values.
Definition GLESShaderProgram.h:108
void bindValues(const GLuint id) const
Binds all parameters to a shader program.
Definition GLESShaderProgram.h:441
This class implements a container for an OpenGL ES shader program.
Definition GLESShaderProgram.h:53
std::vector< GLESShaderRef > shaders_
The shaders.
Definition GLESShaderProgram.h:370
bool setParameter(const std::string &name, const SquareMatrices3 &value) override
Sets a parameter by a given parameter name.
ParameterType parameterType(const std::string &name) const override
Returns the type of a specific parameter.
bool setParameter(const std::string &name, const float value) override
Sets a parameter by a given parameter name.
void bind(const SquareMatrix4 &projection, const HomogenousMatrix4 &camera_T_model, const HomogenousMatrix4 &camera_T_world, const SquareMatrix3 &normalMatrix) const
Uses the shader and binds the given projection and model matrices as OpenGL uniforms.
bool setParameter(const std::string &name, const int value) override
Sets a parameter by a given parameter name.
Parameters< Vector2 > parametersVector2_
The map of Vector2 values.
Definition GLESShaderProgram.h:397
bool setParameter(const std::string &name, const Vector2 &value) override
Sets a parameter by a given parameter name.
bool setShader(const ShaderLanguage shaderLanguage, const std::string &vertexShaderCode, const std::string &fragmentShaderCode, std::string &errorMessage) override
Sets the shading code (a custom shader, not a shader from GLESProgramManager).
bool isCompiled() const override
Returns whether this program holds a valid and successfully compiled and linked shader code.
GLuint id() const
Returns the OpenGL ES shader program id.
Definition GLESShaderProgram.h:456
Parameters< float > parametersFloat_
The map for floating point values.
Definition GLESShaderProgram.h:376
SamplerMap samplers_
The map of texture samplers.
Definition GLESShaderProgram.h:373
bool setParameter(const std::string &name, const double value) override
Sets a parameter by a given parameter name.
Parameters< Vector3 > parametersVector3_
The map of Vector3 values.
Definition GLESShaderProgram.h:400
void bindAttribute(const GLESFramebuffer &framebuffer, GLESShaderProgram &shaderProgram) override
Binds all (custom) attributes of this shader program.
std::pair< GLenum, std::vector< const char * > > ShaderCodePair
Definition of a pair combining a GL shader type and shader code parts.
Definition GLESShaderProgram.h:59
bool setShader(const ShaderLanguage shaderLanguage, const FilenamePairs &filenamePairs, std::string &errorMessage) override
Sets the shading code (a custom shader, not a shader from GLESProgramManager).
void release()
Releases the shader program.
Parameters< unsigned int > parametersUnsignedInt_
The map of integer values.
Definition GLESShaderProgram.h:385
bool setParameter(const std::string &name, const unsigned int value) override
Sets a parameter by a given parameter name.
bool setParameter(const std::string &name, const float *value, const unsigned int elements) override
Sets a parameter by a given parameter name.
std::vector< ShaderCodePair > ShaderCodePairs
Definition of a vector holding shader code pairs.
Definition GLESShaderProgram.h:64
bool setShader(const ShaderLanguage shaderLanguage, const std::vector< const char * > &vertexShaderCode, const std::vector< const char * > &fragmentShaderCode, std::string &errorMessage) override
Sets the shading code.
bool link(const ProgramType programType, const std::vector< GLESShaderRef > &shaders, std::string &message)
Links several shaders.
Parameters< int > parametersInt_
The map of integer values.
Definition GLESShaderProgram.h:382
Parameters< HomogenousMatrix4 > parametersHomogenousMatrix4_
The map of HomogenousMatrix4 values.
Definition GLESShaderProgram.h:388
bool setParameter(const std::string &name, const SquareMatrix3 &value) override
Sets a parameter by a given parameter name.
bool setParameter(const std::string &name, const HomogenousMatrices4 &value) override
Sets a parameter by a given parameter name.
~GLESShaderProgram() override
Destructs an OpenGL ES shader program container.
GLuint id_
OpenGL ES shader program id.
Definition GLESShaderProgram.h:364
bool setParameter(const std::string &name, const SquareMatrix4 &value) override
Sets a parameter by a given parameter name.
bool existParameter(const std::string &name) const override
Returns whether this shader program has a specific parameter.
void bind(const GLESFramebuffer &framebuffer, const SquareMatrix4 &projection, const HomogenousMatrix4 &camera_T_model, const HomogenousMatrix4 &camera_T_world, const SquareMatrix3 &normalMatrix)
Uses the shader and binds the given projection and model matrices as OpenGL uniforms.
unsigned int parameterElements(const std::string &name) const override
Returns the number of elements of a specific parameter.
bool setShader(const ShaderLanguage shaderLanguage, const CodePairs &codePairs, std::string &errorMessage) override
Sets the shader code.
bool setParameter(const std::string &name, const Vector3 &value) override
Sets a parameter by a given parameter name.
Parameters< SquareMatrices3 > parametersSquareMatrices3_
The map of SquareMatrices3 values.
Definition GLESShaderProgram.h:409
bool compileAndLink(const ProgramType programType, const std::vector< const char * > &vertexShaderCode, const std::vector< const char * > &fragmentShaderCode, std::string &message)
Compiles and links a vertex and a fragment shader.
unsigned int samplerNumber() const override
Returns the number of sampler parameters.
bool setParameter(const std::string &name, const HomogenousMatrix4 &value) override
Sets a parameter by a given parameter name.
static std::string translateShaderType(const GLenum shaderType)
Translates the OpenGL shader type to a readable string.
Parameters< Vector4 > parametersVector4_
The map of Vector4 values.
Definition GLESShaderProgram.h:403
bool link(const ProgramType programType, const GLESShaderRef &vertexShader, const GLESShaderRef &fragmentShader, std::string &message)
Links a vertex and a fragment shader.
bool setParameter(const std::string &name, const Vector4 &value) override
Sets a parameter by a given parameter name.
Parameters< std::vector< float > > parametersFloats_
The map for arrays of floating point value.
Definition GLESShaderProgram.h:379
Parameters< SquareMatrix4 > parametersSquareMatrix4_
The map of SquareMatrix4 values.
Definition GLESShaderProgram.h:394
Parameters< SquareMatrix3 > parametersSquareMatrix3_
The map of SquareMatrix3 values.
Definition GLESShaderProgram.h:391
bool setSampler(const SamplerIndex index, const TextureRef &texture) override
Sets the texture of a specified sampler.
ProgramType necessaryShader() const override
Returns the shader type necessary to render an object with this attribute.
std::unordered_map< SamplerIndex, TextureRef > SamplerMap
Definition of a map mapping sampler indices to textures.
Definition GLESShaderProgram.h:114
GLESShaderProgram()
Creates a new OpenGL ES shader program container.
Parameters< HomogenousMatrices4 > parametersHomogenousMatrices4_
The map of HomogenousMatrices4 values.
Definition GLESShaderProgram.h:406
bool compileAndLink(const ProgramType programType, const ShaderCodePairs &shaderCodePairs, std::string &message)
Compiles and links shaders.
ProgramType programType_
The type of the shader program.
Definition GLESShaderProgram.h:367
SamplerIndex samplerIndex(const TextureRef &sampler) const override
Returns the index of a registered texture sample object.
This class implements a shader program attribute.
Definition rendering/ShaderProgram.h:44
unsigned int SamplerIndex
Definition of a sampler index.
Definition rendering/ShaderProgram.h:141
ShaderLanguage
Definition of individual shader languages.
Definition rendering/ShaderProgram.h:51
ParameterType
Definition of a parameter types.
Definition rendering/ShaderProgram.h:107
std::vector< FilenamePair > FilenamePairs
Definition of a vector holding pairs combining filenames with shader types.
Definition rendering/ShaderProgram.h:91
std::vector< CodePair > CodePairs
Definition of a vector holding pairs combining shader codes with shader types.
Definition rendering/ShaderProgram.h:101
This class implements a smart rendering object reference.
Definition rendering/ObjectRef.h:34
This class implements a vector with four elements.
Definition Vector4.h:97
std::vector< HomogenousMatrix4 > HomogenousMatrices4
Definition of a vector holding HomogenousMatrix4 objects.
Definition HomogenousMatrix4.h:73
std::vector< SquareMatrix3 > SquareMatrices3
Definition of a vector holding SquareMatrix3 objects.
Definition SquareMatrix3.h:71
SmartObjectRef< GLESShaderProgram > GLESShaderProgramRef
Definition of an object reference for shader program containers.
Definition GLESShaderProgram.h:44
The namespace covering the entire Ocean framework.
Definition Accessor.h:15