Ocean
platform/gl/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_PLATFORM_GL_SHADER_PROGRAM_H
9 #define META_OCEAN_PLATFORM_GL_SHADER_PROGRAM_H
10 
11 #include "ocean/platform/gl/GL.h"
13 
15 
16 namespace Ocean
17 {
18 
19 namespace Platform
20 {
21 
22 namespace GL
23 {
24 
25 /**
26  * This class implements an OpenGL shader program.
27  * @ingroup platformgl
28  */
29 class OCEAN_PLATFORM_GL_EXPORT ShaderProgram : public ContextAssociated
30 {
31  public:
32 
33  /**
34  * Creates a new shader program object.
35  */
36  inline ShaderProgram();
37 
38  /**
39  * Creates a new shader program with associated context.
40  * @param context The associated context of the shader program
41  */
42  explicit inline ShaderProgram(Context& context);
43 
44  /**
45  * Destructs a shader program object.
46  */
47  virtual ~ShaderProgram();
48 
49  /**
50  * Returns the id of the shader program.
51  * @return The shader program's id, 0 if the shader has not been defined or could not be created
52  */
53  inline GLuint id() const;
54 
55  /**
56  * Creates a new shader program by the code of the vertex shader and fragment shader.
57  * The shader code will be compiled and linked afterwards.<br>
58  * @param vertexCode The code of the vertex shader
59  * @param fragmentCode The code of the fragment shader
60  * @param errorMessage Optional resulting error message
61  * @return True, if succeeded
62  */
63  bool createProgram(const std::string& vertexCode, const std::string& fragmentCode, std::string* errorMessage = nullptr);
64 
65  /**
66  * Binds this shader program.
67  * @return True, if succeeded
68  */
69  bool bind();
70 
71  /**
72  * Sets the value of a uniform parameter of this shader program.
73  * @param uniformName The name of the uniform parameter
74  * @param value The value to set
75  * @return True, if succeeded
76  */
77  bool setUniform(const std::string& uniformName, const int value);
78 
79  /**
80  * Sets the value of a uniform parameter of this shader program.
81  * @param uniformName The name of the uniform parameter
82  * @param value The value to set
83  * @return True, if succeeded
84  */
85  bool setUniform(const std::string& uniformName, const float value);
86 
87  /**
88  * Sets the value of a uniform parameter of this shader program.
89  * @param uniformName The name of the uniform parameter
90  * @param value The value to set
91  * @return True, if succeeded
92  */
93  bool setUniform(const std::string& uniformName, const VectorF2& value);
94 
95  /**
96  * Sets the value of a uniform parameter of this shader program.
97  * @param uniformName The name of the uniform parameter
98  * @param value The value to set
99  * @return True, if succeeded
100  */
101  bool setUniform(const std::string& uniformName, const VectorF3& value);
102 
103  /**
104  * Sets the value of a uniform parameter of this shader program.
105  * @param uniformName The name of the uniform parameter
106  * @param value The value to set
107  * @return True, if succeeded
108  */
109  bool setUniform(const std::string& uniformName, const VectorF4& value);
110 
111  /**
112  * Sets the value of a uniform parameter of this shader program.
113  * @param uniformName The name of the uniform parameter
114  * @param value The value to set
115  * @return True, if succeeded
116  */
117  bool setUniform(const std::string& uniformName, const SquareMatrixF4& value);
118 
119  /**
120  * Releases this shader program.
121  * @return True, if succeeded
122  */
123  bool release();
124 
125  /**
126  * Returns whether this object holds a valid shader program (which could be created, compiled and linked successfully).
127  * @return True, if so
128  */
129  explicit inline operator bool() const;
130 
131  protected:
132 
133  /**
134  * Creates either a vertex or a fragment shader.
135  * @param code The code of the shader
136  * @param shaderId The resulting id of the shader, must be 0
137  * @param shaderType The type of the shader (GL_VERTEX_SHADER or GL_FRAGMENT_SHADER)
138  * @param errorMessage Optional resulting error message
139  * @return True, if succeeded
140  */
141  bool createShader(const std::string& code, GLuint& shaderId, const GLenum shaderType, std::string* errorMessage = nullptr);
142 
143  protected:
144 
145  /// The id of the shader program.
146  GLuint programId;
147 
148  /// The id of the vertex shader.
150 
151  /// The id of the fragment shader.
153 };
154 
157  programId(0u),
158  programVertexShaderId(0u),
159  programFragmentShaderId(0u)
160 {
161  // nothing to do here
162 }
163 
165  ContextAssociated(context),
166  programId(0u),
167  programVertexShaderId(0u),
168  programFragmentShaderId(0u)
169 {
170  // nothing to do here
171 }
172 
173 inline GLuint ShaderProgram::id() const
174 {
175  return programId;
176 }
177 
178 inline ShaderProgram::operator bool() const
179 {
180  ocean_assert(programId == 0u || (programVertexShaderId != 0u || programFragmentShaderId != 0u));
181  return programId != 0u;
182 }
183 
184 }
185 
186 }
187 
188 }
189 
190 #endif // META_OCEAN_PLATFORM_GL_SHADER_PROGRAM_H
This class implements the base class for all object which have an associated context.
Definition: ContextAssociated.h:31
This class encapsulates an OpenGL context.
Definition: platform/gl/Context.h:29
This class implements an OpenGL shader program.
Definition: platform/gl/ShaderProgram.h:30
GLuint programVertexShaderId
The id of the vertex shader.
Definition: platform/gl/ShaderProgram.h:149
GLuint programFragmentShaderId
The id of the fragment shader.
Definition: platform/gl/ShaderProgram.h:152
virtual ~ShaderProgram()
Destructs a shader program object.
bool setUniform(const std::string &uniformName, const int value)
Sets the value of a uniform parameter of this shader program.
bool setUniform(const std::string &uniformName, const float value)
Sets the value of a uniform parameter of this shader program.
bool bind()
Binds this shader program.
bool setUniform(const std::string &uniformName, const VectorF3 &value)
Sets the value of a uniform parameter of this shader program.
bool setUniform(const std::string &uniformName, const VectorF4 &value)
Sets the value of a uniform parameter of this shader program.
bool createShader(const std::string &code, GLuint &shaderId, const GLenum shaderType, std::string *errorMessage=nullptr)
Creates either a vertex or a fragment shader.
GLuint programId
The id of the shader program.
Definition: platform/gl/ShaderProgram.h:146
bool setUniform(const std::string &uniformName, const VectorF2 &value)
Sets the value of a uniform parameter of this shader program.
ShaderProgram()
Creates a new shader program object.
Definition: platform/gl/ShaderProgram.h:155
bool release()
Releases this shader program.
GLuint id() const
Returns the id of the shader program.
Definition: platform/gl/ShaderProgram.h:173
bool createProgram(const std::string &vertexCode, const std::string &fragmentCode, std::string *errorMessage=nullptr)
Creates a new shader program by the code of the vertex shader and fragment shader.
bool setUniform(const std::string &uniformName, const SquareMatrixF4 &value)
Sets the value of a uniform parameter of this shader program.
This class implements a 4x4 square matrix.
Definition: SquareMatrix4.h:85
This class implements a vector with two elements.
Definition: Vector2.h:96
This class implements a vector with three elements.
Definition: Vector3.h:97
This class implements a vector with four elements.
Definition: Vector4.h:97
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15