Ocean
rendering/ShaderProgram.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_SHADER_PROGRAM_H
9 #define META_OCEAN_RENDERING_SHADER_PROGRAM_H
10 
15 
19 #include "ocean/math/Vector2.h"
20 #include "ocean/math/Vector3.h"
21 #include "ocean/math/Vector4.h"
22 
23 namespace Ocean
24 {
25 
26 namespace Rendering
27 {
28 
29 // Forward declaration
30 class ShaderProgram;
31 
32 /**
33  * Definition of a smart object reference holding a shader program object.
34  * @see SmartObjectRef, ShaderProgram.
35  * @ingroup rendering
36  */
38 
39 /**
40  * This class implements a shader program attribute.
41  * @ingroup rendering
42  */
43 class OCEAN_RENDERING_EXPORT ShaderProgram : virtual public Attribute
44 {
45  public:
46 
47  /**
48  * Definition of individual shader languages.
49  */
50  enum ShaderLanguage : uint32_t
51  {
52  /// An invalid shader language.
53  SL_INVALID = 0u,
54  /// The GLSL shader language (e.g., used by OpenGL and OpenGL ES).
56  /// The Cg shader language (e.g., used by NVIDIA).
57  SL_CG
58  };
59 
60  /**
61  * Definition of individual shader types.
62  */
63  enum ShaderType : uint32_t
64  {
65  /// An invalid shader type.
66  ST_INVALID = 0u,
67  /// A compute shader.
69  /// A fragment shader.
71  /// A geometry shader.
73  /// A Tessellation control shader.
75  /// A Tessellation evaluation shader.
77  /// A vertex shader.
79  //// A unified shader (e.g., combining vertex and fragment shader).
80  ST_UNIFIED
81  };
82 
83  /**
84  * Definition of a pair combining a filename with a shader type with.
85  */
86  typedef std::pair<std::string, ShaderType> FilenamePair;
87 
88  /**
89  * Definition of a vector holding pairs combining filenames with shader types.
90  */
91  typedef std::vector<FilenamePair> FilenamePairs;
92 
93  /**
94  * Definition of a pair combining shader code parts with a shader type with.
95  */
96  typedef std::pair<std::vector<const char*>, ShaderType> CodePair;
97 
98  /**
99  * Definition of a vector holding pairs combining shader codes with shader types.
100  */
101  typedef std::vector<CodePair> CodePairs;
102 
103  /**
104  * Definition of a parameter types.
105  */
106  enum ParameterType : uint32_t
107  {
108  /// Invalid parameter.
109  TYPE_INVALID = 0u,
110  /// Unsupported parameter.
112  /// Boolean parameter.
114  /// Float parameter.
116  /// Integer parameter.
118  /// 3x3 matrix parameter.
120  /// 4x4 matrix parameter.
122  /// 1D sample parameter.
124  /// 2D sample parameter.
126  /// 3D sample parameter.
128  /// Struct parameter.
130  /// 2D vector parameter.
132  /// 3D vector parameter.
134  /// 4D vector parameter.
135  TYPE_VECTOR4
136  };
137 
138  /**
139  * Definition of a sampler index.
140  */
141  typedef unsigned int SamplerIndex;
142 
143  /**
144  * Definition of an invalid sampler index.
145  */
146  static constexpr SamplerIndex invalidSamplerIndex = SamplerIndex(-1);
147 
148  protected:
149 
150  /**
151  * Definition of a vector holding sample objects.
152  */
153  typedef std::vector<TextureRef> Textures;
154 
155  public:
156 
157  /**
158  * Sets the shader code.
159  * @param shaderLanguage The language of the given shaders.
160  * @param filenamePairs The pairs of filenames and shader types which are necessary to defined the shader, at least one
161  * @param errorMessage Returning error message if the code is not valid
162  * @return True, if the shader could be compiled successfully
163  */
164  virtual bool setShader(const ShaderLanguage shaderLanguage, const FilenamePairs& filenamePairs, std::string& errorMessage);
165 
166  /**
167  * Sets the shader code.
168  * @param shaderLanguage The language of the given shaders.
169  * @param vertexShaderCode The code of the vertex shader, must be valid
170  * @param fragmentShaderCode The code of the fragment shader, must be valid
171  * @param errorMessage Returning error message if the code is not valid
172  * @return True, if the shader could be compiled successfully
173  */
174  virtual bool setShader(const ShaderLanguage shaderLanguage, const std::string& vertexShaderCode, const std::string& fragmentShaderCode, std::string& errorMessage);
175 
176  /**
177  * Sets the shader code.
178  * @param shaderLanguage The language of the given shaders.
179  * @param vertexShaderCode The code of the vertex shader, can be composed of several individual code blocks
180  * @param fragmentShaderCode The code of the fragment shader, can be composed of several individual code blocks
181  * @param errorMessage Returning error message if the code is not valid
182  * @return True, if the shader could be compiled successfully
183  */
184  virtual bool setShader(const ShaderLanguage shaderLanguage, const std::vector<const char*>& vertexShaderCode, const std::vector<const char*>& fragmentShaderCode, std::string& errorMessage);
185 
186  /**
187  * Sets the shader code.
188  * @param shaderLanguage The language of the given shaders.
189  * @param codePairs The codes of the shader, each shader can be composed of several individual code blocks
190  * @param errorMessage Returning error message if the code is not valid
191  * @return True, if the shader could be compiled successfully
192  */
193  virtual bool setShader(const ShaderLanguage shaderLanguage, const CodePairs& codePairs, std::string& errorMessage);
194 
195  /**
196  * Returns whether this shader program has a specific parameter.
197  * @param name The name of the parameter to check
198  * @return True, if so
199  */
200  virtual bool existParameter(const std::string& name) const;
201 
202  /**
203  * Returns the type of a specific parameter.
204  * @param name The name of the parameter to return the type for
205  * @return Parameter type
206  */
207  virtual ParameterType parameterType(const std::string& name) const;
208 
209  /**
210  * Returns the number of sampler parameters.
211  * @return Number of samplers
212  */
213  virtual unsigned int samplerNumber() const;
214 
215  /**
216  * Returns the index of a registered texture sample object.
217  * @param sampler The sampler to return the index for
218  * @return Index of the specified sampler, an invalid index is returned if the sampler is not registered
219  */
220  virtual SamplerIndex samplerIndex(const TextureRef& sampler) const;
221 
222  /**
223  * Returns the number of elements of a specific parameter.
224  * @param name The name of the parameter to return the number of elements for
225  * @return Number of elements
226  */
227  virtual unsigned int parameterElements(const std::string& name) const;
228 
229  /**
230  * Sets the texture of a specified sampler.
231  * @param index The index of the shader sampler to set
232  * @param texture The texture object to set
233  * @return True, if succeeded
234  */
235  virtual bool setSampler(const SamplerIndex index, const TextureRef& texture);
236 
237  /**
238  * Sets a parameter by a given parameter name.
239  * @param name The name of the parameter to set
240  * @param value The value to set
241  * @return True, if succeeded
242  */
243  virtual bool setParameter(const std::string& name, const double value);
244 
245  /**
246  * Sets a parameter by a given parameter name.
247  * @param name The name of the parameter to set
248  * @param value The value to set
249  * @return True, if succeeded
250  */
251  virtual bool setParameter(const std::string& name, const float value);
252 
253  /**
254  * Sets a parameter by a given parameter name.
255  * @param name The name of the parameter to set
256  * @param value Array value to set
257  * @param elements Number of elements in the array
258  * @return True, if succeeded
259  */
260  virtual bool setParameter(const std::string& name, const float* value, const unsigned int elements);
261 
262  /**
263  * Sets a parameter by a given parameter name.
264  * @param name The name of the parameter to set
265  * @param value The value to set
266  * @return True, if succeeded
267  */
268  virtual bool setParameter(const std::string& name, const int value);
269 
270  /**
271  * Sets a parameter by a given parameter name.
272  * @param name The name of the parameter to set
273  * @param value The value to set
274  * @return True, if succeeded
275  */
276  virtual bool setParameter(const std::string& name, const unsigned int value);
277 
278  /**
279  * Sets a parameter by a given parameter name.
280  * @param name The name of the parameter to set
281  * @param value The value to set
282  * @return True, if succeeded
283  */
284  virtual bool setParameter(const std::string& name, const HomogenousMatrix4& value);
285 
286  /**
287  * Sets a parameter by a given parameter name.
288  * @param name The name of the parameter to set
289  * @param value The value to set
290  * @return True, if succeeded
291  */
292  virtual bool setParameter(const std::string& name, const SquareMatrix3& value);
293 
294  /**
295  * Sets a parameter by a given parameter name.
296  * @param name The name of the parameter to set
297  * @param value The value to set
298  * @return True, if succeeded
299  */
300  virtual bool setParameter(const std::string& name, const SquareMatrix4& value);
301 
302  /**
303  * Sets a parameter by a given parameter name.
304  * @param name The name of the parameter to set
305  * @param value The value to set
306  * @return True, if succeeded
307  */
308  virtual bool setParameter(const std::string& name, const Vector2& value);
309 
310  /**
311  * Sets a parameter by a given parameter name.
312  * @param name The name of the parameter to set
313  * @param value The value to set
314  * @return True, if succeeded
315  */
316  virtual bool setParameter(const std::string& name, const Vector3& value);
317 
318  /**
319  * Sets a parameter by a given parameter name.
320  * @param name The name of the parameter to set
321  * @param value The value to set
322  * @return True, if succeeded
323  */
324  virtual bool setParameter(const std::string& name, const Vector4& value);
325 
326  /**
327  * Sets a parameter by a given parameter name.
328  * @param name The name of the parameter to set
329  * @param value The value to set
330  * @return True, if succeeded
331  */
332  virtual bool setParameter(const std::string& name, const HomogenousMatrices4& value);
333 
334  /**
335  * Sets a parameter by a given parameter name.
336  * @param name The name of the parameter to set
337  * @param value The value to set
338  * @return True, if succeeded
339  */
340  virtual bool setParameter(const std::string& name, const SquareMatrices3& value);
341 
342  /**
343  * Returns whether this program holds a valid and successfully compiled and linked shader code.
344  * @return True, if so
345  */
346  virtual bool isCompiled() const;
347 
348  /**
349  * Returns the type of this object.
350  * @see Object::type().
351  */
352  ObjectType type() const override;
353 
354  protected:
355 
356  /**
357  * Creates a new shader program object.
358  */
360 
361  /**
362  * Destructs a shader program object.
363  */
364  ~ShaderProgram() override;
365 
366  protected:
367 
368  /// Vector holding all registered texture samples.
370 };
371 
372 }
373 
374 }
375 
376 #endif // META_OCEAN_RENDERING_SHADER_PROGRAM_H
This class is the base class for all rendering attribute objects.
Definition: Attribute.h:39
ObjectType
Definition of different object type.
Definition: Object.h:63
This class implements a shader program attribute.
Definition: rendering/ShaderProgram.h:44
virtual bool setParameter(const std::string &name, const double value)
Sets a parameter by a given parameter name.
unsigned int SamplerIndex
Definition of a sampler index.
Definition: rendering/ShaderProgram.h:141
virtual unsigned int parameterElements(const std::string &name) const
Returns the number of elements of a specific parameter.
virtual bool setParameter(const std::string &name, const HomogenousMatrix4 &value)
Sets a parameter by a given parameter name.
virtual unsigned int samplerNumber() const
Returns the number of sampler parameters.
virtual bool setParameter(const std::string &name, const int value)
Sets a parameter by a given parameter name.
virtual bool setParameter(const std::string &name, const Vector2 &value)
Sets a parameter by a given parameter name.
virtual bool setShader(const ShaderLanguage shaderLanguage, const FilenamePairs &filenamePairs, std::string &errorMessage)
Sets the shader code.
ShaderLanguage
Definition of individual shader languages.
Definition: rendering/ShaderProgram.h:51
@ SL_GLSL
The GLSL shader language (e.g., used by OpenGL and OpenGL ES).
Definition: rendering/ShaderProgram.h:55
virtual bool setShader(const ShaderLanguage shaderLanguage, const std::vector< const char * > &vertexShaderCode, const std::vector< const char * > &fragmentShaderCode, std::string &errorMessage)
Sets the shader code.
virtual bool setParameter(const std::string &name, const SquareMatrix3 &value)
Sets a parameter by a given parameter name.
virtual ParameterType parameterType(const std::string &name) const
Returns the type of a specific parameter.
virtual bool setParameter(const std::string &name, const float value)
Sets a parameter by a given parameter name.
virtual bool setShader(const ShaderLanguage shaderLanguage, const std::string &vertexShaderCode, const std::string &fragmentShaderCode, std::string &errorMessage)
Sets the shader code.
ParameterType
Definition of a parameter types.
Definition: rendering/ShaderProgram.h:107
@ TYPE_MATRIX3
3x3 matrix parameter.
Definition: rendering/ShaderProgram.h:119
@ TYPE_SAMPLE1
1D sample parameter.
Definition: rendering/ShaderProgram.h:123
@ TYPE_VECTOR2
2D vector parameter.
Definition: rendering/ShaderProgram.h:131
@ TYPE_UNSUPPORTED
Unsupported parameter.
Definition: rendering/ShaderProgram.h:111
@ TYPE_SAMPLE2
2D sample parameter.
Definition: rendering/ShaderProgram.h:125
@ TYPE_SAMPLE3
3D sample parameter.
Definition: rendering/ShaderProgram.h:127
@ TYPE_VECTOR3
3D vector parameter.
Definition: rendering/ShaderProgram.h:133
@ TYPE_FLOAT
Float parameter.
Definition: rendering/ShaderProgram.h:115
@ TYPE_STRUCT
Struct parameter.
Definition: rendering/ShaderProgram.h:129
@ TYPE_MATRIX4
4x4 matrix parameter.
Definition: rendering/ShaderProgram.h:121
@ TYPE_BOOL
Boolean parameter.
Definition: rendering/ShaderProgram.h:113
@ TYPE_INTEGER
Integer parameter.
Definition: rendering/ShaderProgram.h:117
Textures shaderProgramTextures
Vector holding all registered texture samples.
Definition: rendering/ShaderProgram.h:369
std::vector< FilenamePair > FilenamePairs
Definition of a vector holding pairs combining filenames with shader types.
Definition: rendering/ShaderProgram.h:91
virtual bool setParameter(const std::string &name, const HomogenousMatrices4 &value)
Sets a parameter by a given parameter name.
virtual bool isCompiled() const
Returns whether this program holds a valid and successfully compiled and linked shader code.
std::pair< std::string, ShaderType > FilenamePair
Definition of a pair combining a filename with a shader type with.
Definition: rendering/ShaderProgram.h:86
virtual bool existParameter(const std::string &name) const
Returns whether this shader program has a specific parameter.
ShaderType
Definition of individual shader types.
Definition: rendering/ShaderProgram.h:64
@ ST_GEOMETRY
A geometry shader.
Definition: rendering/ShaderProgram.h:72
@ ST_TESSELLATION_EVALUATION
A Tessellation evaluation shader.
Definition: rendering/ShaderProgram.h:76
@ ST_VERTEX
A vertex shader.
Definition: rendering/ShaderProgram.h:78
@ ST_TESSELLATION_CONTROL
A Tessellation control shader.
Definition: rendering/ShaderProgram.h:74
@ ST_FRAGMENT
A fragment shader.
Definition: rendering/ShaderProgram.h:70
@ ST_COMPUTE
A compute shader.
Definition: rendering/ShaderProgram.h:68
ObjectType type() const override
Returns the type of this object.
virtual bool setSampler(const SamplerIndex index, const TextureRef &texture)
Sets the texture of a specified sampler.
std::vector< TextureRef > Textures
Definition of a vector holding sample objects.
Definition: rendering/ShaderProgram.h:153
std::pair< std::vector< const char * >, ShaderType > CodePair
Definition of a pair combining shader code parts with a shader type with.
Definition: rendering/ShaderProgram.h:96
virtual bool setParameter(const std::string &name, const float *value, const unsigned int elements)
Sets a parameter by a given parameter name.
~ShaderProgram() override
Destructs a shader program object.
virtual bool setParameter(const std::string &name, const unsigned int value)
Sets a parameter by a given parameter name.
virtual bool setParameter(const std::string &name, const SquareMatrices3 &value)
Sets a parameter by a given parameter name.
virtual bool setParameter(const std::string &name, const Vector3 &value)
Sets a parameter by a given parameter name.
ShaderProgram()
Creates a new shader program object.
std::vector< CodePair > CodePairs
Definition of a vector holding pairs combining shader codes with shader types.
Definition: rendering/ShaderProgram.h:101
virtual bool setParameter(const std::string &name, const SquareMatrix4 &value)
Sets a parameter by a given parameter name.
virtual SamplerIndex samplerIndex(const TextureRef &sampler) const
Returns the index of a registered texture sample object.
virtual bool setParameter(const std::string &name, const Vector4 &value)
Sets a parameter by a given parameter name.
virtual bool setShader(const ShaderLanguage shaderLanguage, const CodePairs &codePairs, std::string &errorMessage)
Sets the shader code.
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< ShaderProgram > ShaderProgramRef
Definition of a smart object reference holding a shader program object.
Definition: rendering/ShaderProgram.h:30
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15