Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ShaderProgramPool.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 /*
26  * ShaderProgramPool.hh
27  *
28  * Created on: Mar 6, 2009
29  * Author: africk
30  */
31 
32 #ifndef __SHADER_PROGRAMPOOL_HH__
33 #define __SHADER_PROGRAMPOOL_HH__
34 
35 #include <OpenGLFramework/Base/glfShaderProgram.hh>
36 #include <OpenGLFramework/Base/glfMatrix.hh>
37 #include <OpenGLFramework/Base/glfException.hh>
38 #include <bias_config.h>
39 #include <map>
40 #include <list>
41 
42 namespace BIAS
43 {
44 
45  /**
46  * this class represents a set of shader programs
47  * one can add and remove shader programs to and from the pool
48  * all programs have unique names and can be accessed by the name
49  * uniform variables of the shader programs can be set by addressing a program by its name
50  * by setting a uniform variable it is checked, if a uniform variable of this type and with this name exists, if not an glfException is thrown
51  * before using a shader program call : LinkAll();
52  *
53  * \ingroup g_openglframework
54  * \author frick
55  **/
56  class BIASOpenGLFramework_EXPORT ShaderProgramPool
57  {
58  public:
59 
60  //key: uniform type - list of uniform names
61  typedef std::map<std::string, std::list<std::string> > uniforms_map_t;
62  //pair of shader pointer - shader source
63  typedef std::pair<glfShader*, std::string> shader_data_t;
64  //key: shader_name - value: shader_data
65  typedef std::map<std::string, shader_data_t> shader_map_t;
66  //pair: program pointer - program uniform data
67  typedef std::pair<glfShaderProgram*, uniforms_map_t> program_data_t;
68  //key: program name - program_data
69  typedef std::map<std::string, program_data_t> program_map_t;
70 
71  //###############################################################
74 
75  //###############################################################
76  /**
77  * adds a shader program with name shader_program_name to the shader pool
78  * throws an glf exception if a program with this name already exists
79  * @author frick
80  */
81  void
82  AddShaderProgram(const std::string& shader_program_name);
83 
84  /**
85  * adds a fragment shader with name shader_name and source code shader_source to the program with the name shader_program_name
86  * throws an glf exception if a shader with this name already exists
87  * @author frick
88  */
89  void
90  AddFragmentShader(const std::string& shader_source,
91  const std::string& shader_name, const std::string& shader_program_name);
92 
93  /**
94  * adds a vertex shader with name shader_name and source code shader_source to the program with the name shader_program_name
95  * throws an glf exception if a shader with this name already exists
96  * @author frick
97  */
98  void
99  AddVertexShader(const std::string& shader_source,
100  const std::string& shader_name, const std::string& shader_program_name);
101 
102  /**
103  * adds a fragment shader with name shader_name and source code in a file (path) to the program with the name shader_program_name
104  * throws an glf exception if a shader with this name already exists
105  * @author frick
106  */
107  void
108  AddFragmentShaderFromFile(const std::string& path_to_shader,
109  const std::string& shader_name, const std::string& shader_program_name);
110 
111  /**
112  * adds a vertex shader with name shader_name and source code in a file (path) to the program with the name shader_program_name
113  * throws an glf exception if a shader with this name already exists
114  * @author frick
115  */
116  void
117  AddVertexShaderFromFile(const std::string& path_to_shader,
118  const std::string& shader_name, const std::string& shader_program_name);
119 
120  /**
121  * removes a shader program with name shader_program_name to the shader pool
122  * @author frick
123  */
124  void
125  RemoveShaderProgram(const std::string& shader_program_name);
126 
127  /**
128  * returns a vector with pairs (shader program name, shader program pointer) for all shader programs in the shader pool
129  * @author frick
130  */
131  std::vector<std::pair<std::string, glfShaderProgram*> >
132  GetShaderPrograms();
133 
134  /**
135  * returns the pointer to the shader program with the name shader_program_name
136  * if a program with this name does not exist NULL is returned
137  * @author frick
138  */
140  GetShaderProgram(const std::string& shader_program_name);
141 
142  /**
143  * Links all shader program
144  * after that the programs are ready to be bind and executed and uniform variables may be set
145  * @author frick
146  */
147  void
148  LinkAll();
149 
150  /**
151  * returns true if all programs are linked, false otherwise
152  * @author frick
153  *
154  */
155  bool
156  IsLinked();
157 
158  /**
159  * Binds shader program with the given name
160  * @author frick
161  */
162  void
163  BindShaderProgram(const std::string& shader_program_name);
164 
165  /**
166  * fills the vector shader_names with names of all shaders attached to the program with the name shader_program_name
167  */
168  void
169  GetShaderNames(const std::string& shader_program_name, std::vector<
170  std::string>& shader_names);
171 
172  /**
173  * sets uniform variable varName for shader program shader_program_name
174  * glfException is thrown if no uniform variables with such type and name exist in the shader program
175  */
176  template<class StorageType>
177  void
178  SetUniform(const std::string& shader_program_name,
179  const std::string& varName, StorageType value);
180 
181  template<class StorageType>
182  void
183  SetUniformArray(const std::string& shader_program_name,
184  const std::string& varName, StorageType values, int arg_number);
185 
186  /**
187  * returns the type of uniform variable with name varName if such variable exists in the shader_program
188  * otherwise an empty string is returned
189  */
190  std::string
191  GetUniformType(const std::string& shader_program_name,
192  const std::string& varName);
193 
194  /**
195  * resets shader pool
196  */
197  void
198  ClearAll();
199 
200  private:
201 
202  void
203  AddShader_(GLenum type, const std::string& shader_source,
204  const std::string& shader_name, const std::string& shader_program_name);
205 
206  shader_map_t*
207  GetShadersMap_(const std::string& shader_program_name);
208 
210  GetShaderData_(shader_map_t& shader_map, const std::string& shader_name);
211 
213  GetShaderProgramData_(const std::string& shader_program_name);
214 
216  GetUniformMap_(program_data_t& program_data);
217 
219  GetShaderProgram_(program_data_t& program_data);
220 
222  GetUniformMap_(const std::string& shader_program_name);
223 
224  std::string*
225  GetShaderSource_(shader_data_t& shader_data);
226 
227  glfShader*
228  GetShader_(shader_data_t& shader_data);
229 
230  glfShader*
231  GetShader_(const std::string& shader_program_name,
232  const std::string& shader_name);
233 
234  void
235  AddUniformDataFromSource_(program_data_t& shader_data,
236  std::string shader_source);
237 
238  void
239  AddUniformVariable_(program_data_t& shader_data, const std::string& type,
240  const std::string& name);
241 
242  void
243  LoadShaderFromFile_(const std::string& shader_path,
244  std::string& shader_source);
245 
246  std::string RemoveCommentariesFromSource_(const std::string& source);
247 
248  //###############################################################
249 
250 
251  //key: program name - value: pair program pointer - uniforms_map
252  program_map_t program_map_;
253  //key: program name - shader_map
254  std::map<std::string, shader_map_t> shaders_;
255 
256  bool all_program_linked_;
257  /* indicates if the programm requires linking.
258  * This is required since linking erases all set uniforms.
259  * A strategy that relinks everything after a new program has been
260  * added would also require the ability to reset all set uniforms to
261  * all unchanged but relinked programms. This would cause some overhead.
262  * It is better to only relink when realy necessary, this is generally
263  * correlated with a new set of uniforms and the user will provide
264  * the required interaction.
265  */
266  std::map<std::string, bool> validitlyLinked_;
267 
268  };
269 }
270 
271 #endif /* __SHADER_PROGRAMPOOL_HH__ */
this class represents a set of shader programs one can add and remove shader programs to and from the...
std::map< std::string, std::list< std::string > > uniforms_map_t
std::pair< glfShaderProgram *, uniforms_map_t > program_data_t
std::map< std::string, program_data_t > program_map_t
A shader program composed of several shaders.
A GLSL vertex shader or fragment shader, which must be linked in a shader program.
Definition: glfShader.hh:39
std::pair< glfShader *, std::string > shader_data_t
std::map< std::string, shader_data_t > shader_map_t