NGL  6.5
The NCCA Graphics Library
ShaderProgram.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHADER_PROGRAM_H_
19 #define SHADER_PROGRAM_H_
20 #include "Shader.h"
21 #include "Types.h"
22 #include "Util.h"
23 #include <vector>
24 #include <unordered_map>
25 
26 //----------------------------------------------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------------------------------------------
34 namespace ngl
35 {
36 
38 {
39 public :
40  //----------------------------------------------------------------------------------------------------------------------
44  //----------------------------------------------------------------------------------------------------------------------
45  ShaderProgram(std::string _name ) noexcept;
46  //----------------------------------------------------------------------------------------------------------------------
48  //----------------------------------------------------------------------------------------------------------------------
49  ~ShaderProgram();
50  //----------------------------------------------------------------------------------------------------------------------
53  //----------------------------------------------------------------------------------------------------------------------
54  void use() noexcept;
55  //----------------------------------------------------------------------------------------------------------------------
60  //----------------------------------------------------------------------------------------------------------------------
61  void unbind() noexcept;
62  //----------------------------------------------------------------------------------------------------------------------
66  //----------------------------------------------------------------------------------------------------------------------
67  void attachShader( Shader *_shader ) noexcept;
68  //----------------------------------------------------------------------------------------------------------------------
72  //----------------------------------------------------------------------------------------------------------------------
73  void bindAttribute(GLuint index, const std::string &_attribName ) noexcept;
74  //----------------------------------------------------------------------------------------------------------------------
78  //----------------------------------------------------------------------------------------------------------------------
79  void bindFragDataLocation(GLuint index, const std::string &_attribName ) noexcept;
80 
81  //----------------------------------------------------------------------------------------------------------------------
85  //----------------------------------------------------------------------------------------------------------------------
86  void link() noexcept;
87  //----------------------------------------------------------------------------------------------------------------------
89  //----------------------------------------------------------------------------------------------------------------------
90  GLuint getID() const noexcept{return m_programID;}
91 
92  //----------------------------------------------------------------------------------------------------------------------
95  //----------------------------------------------------------------------------------------------------------------------
96  GLint getUniformLocation( const char* _name) const noexcept;
97 
98  //----------------------------------------------------------------------------------------------------------------------
101  //----------------------------------------------------------------------------------------------------------------------
102  void printActiveUniforms() const noexcept;
103  //----------------------------------------------------------------------------------------------------------------------
106  //----------------------------------------------------------------------------------------------------------------------
107  void printActiveAttributes() const noexcept;
108  //----------------------------------------------------------------------------------------------------------------------
111  //----------------------------------------------------------------------------------------------------------------------
112  void printProperties() const noexcept;
113  //----------------------------------------------------------------------------------------------------------------------
115  //----------------------------------------------------------------------------------------------------------------------
116 
117  //----------------------------------------------------------------------------------------------------------------------
121  //----------------------------------------------------------------------------------------------------------------------
122  void setUniform1f(const char* _varname, float __v0 ) const noexcept;
123 
124  //----------------------------------------------------------------------------------------------------------------------
128  //----------------------------------------------------------------------------------------------------------------------
129  void setRegisteredUniform1f(const std::string &_varname, float _v0 ) const noexcept;
130 
131  //----------------------------------------------------------------------------------------------------------------------
136  //----------------------------------------------------------------------------------------------------------------------
137  void setUniform2f( const char* _varname, float _v0, float _v1 ) const noexcept;
138  //----------------------------------------------------------------------------------------------------------------------
143  //----------------------------------------------------------------------------------------------------------------------
144  void setRegisteredUniform2f(const std::string &_varname, float _v0, float _v1 ) const noexcept;
145 
146  //----------------------------------------------------------------------------------------------------------------------
152  //----------------------------------------------------------------------------------------------------------------------
153  void setUniform3f( const char* _varname, float _v0,float _v1, float _v2 ) const noexcept;
154  //----------------------------------------------------------------------------------------------------------------------
160  //----------------------------------------------------------------------------------------------------------------------
161  void setRegisteredUniform3f( const std::string &_varname,float _v0, float _v1, float _v2 ) const noexcept;
162  //----------------------------------------------------------------------------------------------------------------------
169  //----------------------------------------------------------------------------------------------------------------------
170  void setUniform4f( const char* _varname,float _v0, float _v1, float _v2, float _v3 ) const noexcept;
171  //----------------------------------------------------------------------------------------------------------------------
179  void setRegisteredUniform4f(const std::string &_varname, float _v0,float _v1,float _v2, float _v3 ) const noexcept;
180  //----------------------------------------------------------------------------------------------------------------------
185  //----------------------------------------------------------------------------------------------------------------------
186  void setUniform1fv(const char* _varname, size_t _count, const float* _value ) const noexcept;
187 
188  //----------------------------------------------------------------------------------------------------------------------
193  //----------------------------------------------------------------------------------------------------------------------
194  void setUniform2fv( const char* _varname, size_t _count, const float* _value ) const noexcept;
195 
196  //----------------------------------------------------------------------------------------------------------------------
201  //----------------------------------------------------------------------------------------------------------------------
202  void setUniform3fv( const char* _varname,size_t _count, const float* _value ) const noexcept;
203 
204  //----------------------------------------------------------------------------------------------------------------------
209  //----------------------------------------------------------------------------------------------------------------------
210  void setUniform4fv( const char* _varname, size_t _count, const float* _value ) const noexcept;
211 
212  //----------------------------------------------------------------------------------------------------------------------
217  //----------------------------------------------------------------------------------------------------------------------
218  void setUniform1i(const char* _varname, GLint _v0 ) const noexcept;
219 
220  //----------------------------------------------------------------------------------------------------------------------
224  //----------------------------------------------------------------------------------------------------------------------
225  void setRegisteredUniform1i(const std::string &_varname, int _v0 ) const noexcept;
226  //----------------------------------------------------------------------------------------------------------------------
231 
232  //----------------------------------------------------------------------------------------------------------------------
233  void setRegisteredUniform2i(const std::string &_varname, int _v0, int _v1 ) const noexcept;
234  //----------------------------------------------------------------------------------------------------------------------
240  //----------------------------------------------------------------------------------------------------------------------
241  void setRegisteredUniform3i(const std::string &_varname, int _v0, int _v1,int _v2 ) const noexcept;
242  //----------------------------------------------------------------------------------------------------------------------
249 
250  //----------------------------------------------------------------------------------------------------------------------
251  void setRegisteredUniform4i( const std::string &_varname,int _v0, int _v1, int _v2, int _v3 ) const noexcept;
252 
253  //----------------------------------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------------------------------
259  void setUniform2i(const char* _varname, GLint _v0, GLint _v1 ) const noexcept;
260 
261  //----------------------------------------------------------------------------------------------------------------------
267  //----------------------------------------------------------------------------------------------------------------------
268  void setUniform3i( const char* _varname, GLint _v0, GLint _v1, GLint _v2 ) const noexcept;
269 
270  //----------------------------------------------------------------------------------------------------------------------
277  //----------------------------------------------------------------------------------------------------------------------
278  void setUniform4i(const char* _varname, GLint _v0, GLint _v1, GLint _v2, GLint _v3 ) const noexcept;
279 
280  //----------------------------------------------------------------------------------------------------------------------
285  //----------------------------------------------------------------------------------------------------------------------
286  void setUniform1iv( const char* _varname, size_t _count, const GLint* _value ) const noexcept;
287 
288  //----------------------------------------------------------------------------------------------------------------------
293  //----------------------------------------------------------------------------------------------------------------------
294  void setUniform2iv( const char* _varname, size_t _count, const GLint* _value) const noexcept;
295 
296  //----------------------------------------------------------------------------------------------------------------------
301  //----------------------------------------------------------------------------------------------------------------------
302  void setUniform3iv( const char* _varname, size_t _count, const GLint* _value ) const noexcept;
303 
304  //----------------------------------------------------------------------------------------------------------------------
309  //----------------------------------------------------------------------------------------------------------------------
310  void setUniform4iv( const char* _varname, size_t _count,const GLint* _value ) const noexcept;
311 
312 
313  //----------------------------------------------------------------------------------------------------------------------
315  //----------------------------------------------------------------------------------------------------------------------
316 
317  //----------------------------------------------------------------------------------------------------------------------
323  //----------------------------------------------------------------------------------------------------------------------
324  void setUniformMatrix2fv(const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
325 
326  //----------------------------------------------------------------------------------------------------------------------
332  //----------------------------------------------------------------------------------------------------------------------
333  void setUniformMatrix3fv( const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
334  //----------------------------------------------------------------------------------------------------------------------
340  //----------------------------------------------------------------------------------------------------------------------
341  void setRegisteredUniformMatrix3fv( const std::string &_varname,size_t _count, bool _transpose, const float* _value ) const noexcept;
342  //----------------------------------------------------------------------------------------------------------------------
348  //----------------------------------------------------------------------------------------------------------------------
349  void setUniformMatrix4fv( const char* _varname,size_t _count, bool _transpose, const float* _value) const noexcept;
350 
351  //----------------------------------------------------------------------------------------------------------------------
357  //----------------------------------------------------------------------------------------------------------------------
358  void setRegisteredUniformMatrix4fv( const std::string &_varname,size_t _count, bool _transpose,const float* _value ) const noexcept;
359 
360 
361  //----------------------------------------------------------------------------------------------------------------------
363  //----------------------------------------------------------------------------------------------------------------------
364 
365  //----------------------------------------------------------------------------------------------------------------------
371  //----------------------------------------------------------------------------------------------------------------------
372  void setUniformMatrix2x3fv( const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
373 
374  //----------------------------------------------------------------------------------------------------------------------
380  //----------------------------------------------------------------------------------------------------------------------
381  void setUniformMatrix2x4fv(const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
382 
383  //----------------------------------------------------------------------------------------------------------------------
389  //----------------------------------------------------------------------------------------------------------------------
390  void setUniformMatrix3x2fv( const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
391 
392  //----------------------------------------------------------------------------------------------------------------------
398  //----------------------------------------------------------------------------------------------------------------------
399  void setUniformMatrix3x4fv(const char* _varname, size_t _count, bool _transpose, const float* _value) const noexcept;
400 
401  //----------------------------------------------------------------------------------------------------------------------
407  //----------------------------------------------------------------------------------------------------------------------
408  void setUniformMatrix4x2fv(const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
409 
410  //----------------------------------------------------------------------------------------------------------------------
416  //----------------------------------------------------------------------------------------------------------------------
417  void setUniformMatrix4x3fv(const char* _varname, size_t _count, bool _transpose, const float* _value ) const noexcept;
418 
419  //----------------------------------------------------------------------------------------------------------------------
421  //----------------------------------------------------------------------------------------------------------------------
422 
423  //----------------------------------------------------------------------------------------------------------------------
427  //----------------------------------------------------------------------------------------------------------------------
428  void getUniformfv( const char* _name, float* o_values ) const noexcept;
429 
430  //----------------------------------------------------------------------------------------------------------------------
434  //----------------------------------------------------------------------------------------------------------------------
435  void getUniformiv( const char* _name, int* o_values ) const noexcept;
436 
437  //----------------------------------------------------------------------------------------------------------------------
441  //----------------------------------------------------------------------------------------------------------------------
442 
443  //----------------------------------------------------------------------------------------------------------------------
446  //----------------------------------------------------------------------------------------------------------------------
447  void enableAttribArray(const char* _name ) const noexcept;
448 
449  //----------------------------------------------------------------------------------------------------------------------
452  //----------------------------------------------------------------------------------------------------------------------
453  void disableAttribArray(const char* _name) const noexcept;
454 
455  //----------------------------------------------------------------------------------------------------------------------
457  //----------------------------------------------------------------------------------------------------------------------
458  void autoRegisterUniforms() noexcept;
459  //----------------------------------------------------------------------------------------------------------------------
461  //----------------------------------------------------------------------------------------------------------------------
462  void printRegisteredUniforms()const noexcept;
463 
464  //----------------------------------------------------------------------------------------------------------------------
468  //----------------------------------------------------------------------------------------------------------------------
469  void bindFragDataLocation(GLuint _colourNumber, const char *_name) noexcept;
470  //----------------------------------------------------------------------------------------------------------------------
474  //----------------------------------------------------------------------------------------------------------------------
475 
476  GLuint getUniformBlockIndex( const std::string &_uniformBlockName )const noexcept;
477 
478 private :
479  //----------------------------------------------------------------------------------------------------------------------
481  //----------------------------------------------------------------------------------------------------------------------
482  bool m_linked;
483  //----------------------------------------------------------------------------------------------------------------------
485  //----------------------------------------------------------------------------------------------------------------------
486  std::vector <Shader *> m_shaders;
487  //----------------------------------------------------------------------------------------------------------------------
489  //----------------------------------------------------------------------------------------------------------------------
491  //----------------------------------------------------------------------------------------------------------------------
493  //----------------------------------------------------------------------------------------------------------------------
495  //----------------------------------------------------------------------------------------------------------------------
497  //----------------------------------------------------------------------------------------------------------------------
499  //----------------------------------------------------------------------------------------------------------------------
501  //----------------------------------------------------------------------------------------------------------------------
502  std::unordered_map <std::string, GLuint > m_attribs;
503  //----------------------------------------------------------------------------------------------------------------------
506  //----------------------------------------------------------------------------------------------------------------------
507 
508  struct uniformData
509  {
513  };
514 
515  //----------------------------------------------------------------------------------------------------------------------
518  //----------------------------------------------------------------------------------------------------------------------
519  std::unordered_map <std::string, uniformData> m_registeredUniforms;
520  //----------------------------------------------------------------------------------------------------------------------
522  //----------------------------------------------------------------------------------------------------------------------
523  bool m_active;
524  //----------------------------------------------------------------------------------------------------------------------
525 
526 };
527 } // end NGL namespace
528 #endif
529 //----------------------------------------------------------------------------------------------------------------------
530 
531 //----------------------------------------------------------------------------------------------------------------------
some useful definitions and functions
unsigned int GLuint
Definition: glew.h:280
std::vector< Shader * > m_shaders
a list of the shader objects attached to the program
main definition of types and namespace
GLuint m_programID
the program id for this program object
bool m_active
indicate if this program is the current active program
#define NGL_DLLEXPORT
Definition: Types.h:65
this structure holds information about the registered uniforms in the program id is the uniform locat...
unsigned int GLenum
Definition: glew.h:278
implementation files for RibExport class
Definition: AABB.cpp:22
std::string m_programName
text name of the current program used in lookup and debugging
bool m_debugState
debug mode flag
bool m_linked
flag to indicate if the current Program has been linked
std::unordered_map< std::string, GLuint > m_attribs
a list of attributes for this object, mapping name to ID number
GLuint getID() const noexcept
get the ProgramID for the Program
Definition: ShaderProgram.h:90
GLuint index
Definition: glew.h:1817
and encapsulation of an OpenGL Shader object with associations for source code, etc. Used in conjunction with the ShaderProgram class
Definition: Shader.h:40
int GLint
Definition: glew.h:281
std::unordered_map< std::string, uniformData > m_registeredUniforms
a list of uniforms for this object, mapping name to ID number must be added by the user using the reg...
GLsizei const GLchar *const * string
Definition: glew.h:1847