FastUIDraw
shader_source.hpp
Go to the documentation of this file.
1 /*!
2  * \file shader_source.hpp
3  * \brief file shader_source.hpp
4  *
5  * Adapted from: WRATHGLProgram.hpp of WRATH:
6  *
7  * Copyright 2013 by Nomovok Ltd.
8  * Contact: info@nomovok.com
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@nomovok.com>
16  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
17  *
18  */
19 
20 #ifndef FASTUIDRAW_SHADER_SOURCE_HPP
21 #define FASTUIDRAW_SHADER_SOURCE_HPP
22 
23 #include <fastuidraw/util/util.hpp>
24 #include <fastuidraw/util/vecN.hpp>
26 
27 namespace fastuidraw {
28 namespace glsl {
29 /*!\addtogroup GLSL
30  * @{
31  */
32 
33 /*!
34  * \brief
35  * A ShaderSource represents the source code
36  * to a GLSL shader, specifying blocks of source
37  * code and macros to use.
38  */
40 {
41 public:
42  /*!
43  * \brief
44  * Enumeration to indiciate the source for a shader.
45  */
46  enum source_t
47  {
48  /*!
49  * Shader source code is taken from the file whose
50  * name is the passed string.
51  */
53 
54  /*!
55  * The passed string is the shader source code.
56  */
58 
59  /*!
60  * The passed string is label for a string of text
61  * fetched with fastuidraw::fetch_static_resource().
62  * The resource will be IGNORED if the last byte of
63  * resource is not 0 (which indicates end-of-string).
64  */
66  };
67 
68  /*!
69  * \brief
70  * Enumeration to determine if source
71  * code or a macro
72  */
74  {
75  /*!
76  * add the source code or macro
77  * to the back.
78  */
80 
81  /*!
82  * add the source code or macro
83  * to the front.
84  */
86  };
87 
88  /*!
89  * \brief
90  * Enumeration to indicate extension
91  * enable flags.
92  */
94  {
95  /*!
96  * Requires the named GLSL extension,
97  * i.e. will add <B>"#extension extension_name: require"</B>
98  * to GLSL source code.
99  */
101 
102  /*!
103  * Enables the named GLSL extension,
104  * i.e. will add <B>"#extension extension_name: enable"</B>
105  * to GLSL source code.
106  */
108 
109  /*!
110  * Enables the named GLSL extension,
111  * but request that the GLSL compiler
112  * issues warning when the extension
113  * is used, i.e. will add
114  * <B>"#extension extension_name: warn"</B>
115  * to GLSL source code.
116  */
118 
119  /*!
120  * Disables the named GLSL extension,
121  * i.e. will add <B>"#extension extension_name: disable"</B>
122  * to GLSL source code.
123  */
125  };
126 
127  /*!
128  * A MacroSet represents a set of macros.
129  */
130  class MacroSet
131  {
132  public:
133  /*!
134  * Ctor.
135  */
136  MacroSet(void);
137 
138  /*!
139  * Copy ctor.
140  * \param obj value from which to copy
141  */
142  MacroSet(const MacroSet &obj);
143 
144  ~MacroSet();
145 
146  /*!
147  * Assignment operator.
148  * \param obj value from which to copy
149  */
150  MacroSet&
151  operator=(const MacroSet &obj);
152 
153  /*!
154  * Swap operation
155  * \param obj object with which to swap
156  */
157  void
158  swap(MacroSet &obj);
159 
160  /*!
161  * Add a macro to this MacroSet.
162  * \param macro_name name of macro
163  * \param macro_value value to which macro is given
164  */
165  MacroSet&
166  add_macro(c_string macro_name, c_string macro_value = "");
167 
168  /*!
169  * Add a macro to this MacroSet.
170  * \param macro_name name of macro
171  * \param macro_value value to which macro is given
172  */
173  MacroSet&
174  add_macro(c_string macro_name, uint32_t macro_value);
175 
176  /*!
177  * Add a macro to this MacroSet.
178  * \param macro_name name of macro
179  * \param macro_value value to which macro is given
180  */
181  MacroSet&
182  add_macro(c_string macro_name, int32_t macro_value);
183 
184  /*!
185  * Add a macro to this MacroSet.
186  * \param macro_name name of macro
187  * \param macro_value value to which macro is given
188  */
189  MacroSet&
190  add_macro(c_string macro_name, float macro_value);
191 
192  /*!
193  * Add a macro to this MacroSet for casted to uint32_t
194  * \param macro_name name of macro
195  * \param macro_value value to which macro is given
196  */
197  template<typename T>
198  MacroSet&
199  add_macro_u32(c_string macro_name, T macro_value)
200  {
201  uint32_t v(macro_value);
202  return add_macro(macro_name, v);
203  }
204 
205  /*!
206  * Add a macro to this MacroSet for casted to int32_t
207  * \param macro_name name of macro
208  * \param macro_value value to which macro is given
209  */
210  template<typename T>
211  MacroSet&
212  add_macro_i32(c_string macro_name, T macro_value)
213  {
214  int32_t v(macro_value);
215  return add_macro(macro_name, v);
216  }
217 
218  /*!
219  * Add a macro to this MacroSet for casted to float
220  * \param macro_name name of macro
221  * \param macro_value value to which macro is given
222  */
223  template<typename T>
224  MacroSet&
225  add_macro_float(c_string macro_name, T macro_value)
226  {
227  float v(macro_value);
228  return add_macro(macro_name, v);
229  }
230 
231  private:
232  friend class ShaderSource;
233  void *m_d;
234  };
235 
236  /*!
237  * Ctor.
238  */
239  ShaderSource(void);
240 
241  /*!
242  * Copy ctor.
243  * \param obj value from which to copy
244  */
245  ShaderSource(const ShaderSource &obj);
246 
247  ~ShaderSource();
248 
249  /*!
250  * Assignment operator.
251  * \param obj value from which to copy
252  */
253  ShaderSource&
254  operator=(const ShaderSource &obj);
255 
256  /*!
257  * Swap operation
258  * \param obj object with which to swap
259  */
260  void
261  swap(ShaderSource &obj);
262 
263  /*!
264  * Specifies the version of GLSL to which to
265  * declare the shader. An empty string indicates
266  * to not have a "#version" directive in the shader.
267  * String is -copied-.
268  */
269  ShaderSource&
271 
272  /*!
273  * Returns the value set by specify_version().
274  * Returned pointer is only valid until the
275  * next time that specify_version() is called.
276  */
277  c_string
278  version(void) const;
279 
280  /*!
281  * Add shader source code to this ShaderSource.
282  * \param str string that is a filename, GLSL source or a resource name
283  * \param tp interpretation of str, i.e. determines if
284  * str is a filename, raw GLSL source or a resource
285  * \param loc location to add source
286  */
287  ShaderSource&
288  add_source(c_string str, enum source_t tp = from_file,
289  enum add_location_t loc = push_back);
290 
291  /*!
292  * Add the sources from another ShaderSource object.
293  * \param obj ShaderSource object from which to absorb
294  */
295  ShaderSource&
296  add_source(const ShaderSource &obj);
297 
298  /*!
299  * Add a macro to this ShaderSource.
300  * Functionally, will insert \#define macro_name macro_value
301  * in the GLSL source code.
302  * \param macro_name name of macro
303  * \param macro_value value to which macro is given
304  * \param loc location to add macro within code
305  */
306  ShaderSource&
307  add_macro(c_string macro_name, c_string macro_value = "",
308  enum add_location_t loc = push_back);
309 
310  /*!
311  * Add a macro to this ShaderSource.
312  * Functionally, will insert \#define macro_name macro_value
313  * in the GLSL source code.
314  * \param macro_name name of macro
315  * \param macro_value value to which macro is given
316  * \param loc location to add macro within code
317  */
318  ShaderSource&
319  add_macro(c_string macro_name, uint32_t macro_value,
320  enum add_location_t loc = push_back);
321 
322  /*!
323  * Add a macro to this ShaderSource.
324  * Functionally, will insert \#define macro_name macro_value
325  * in the GLSL source code.
326  * \param macro_name name of macro
327  * \param macro_value value to which macro is given
328  * \param loc location to add macro within code
329  */
330  ShaderSource&
331  add_macro(c_string macro_name, int32_t macro_value,
332  enum add_location_t loc = push_back);
333 
334  /*!
335  * Add a macro to this ShaderSource.
336  * Functionally, will insert \#define macro_name macro_value
337  * in the GLSL source code.
338  * \param macro_name name of macro
339  * \param macro_value value to which macro is given
340  * \param loc location to add macro within code
341  */
342  ShaderSource&
343  add_macro(c_string macro_name, float macro_value,
344  enum add_location_t loc = push_back);
345 
346  /*!
347  * Add a macro to this MacroSet for casted to uint32_t
348  * \param macro_name name of macro
349  * \param macro_value value to which macro is given
350  */
351  template<typename T>
352  ShaderSource&
353  add_macro_u32(c_string macro_name, T macro_value)
354  {
355  uint32_t v(macro_value);
356  return add_macro(macro_name, v);
357  }
358 
359  /*!
360  * Add a macro to this MacroSet for casted to int32_t
361  * \param macro_name name of macro
362  * \param macro_value value to which macro is given
363  */
364  template<typename T>
365  ShaderSource&
366  add_macro_i32(c_string macro_name, T macro_value)
367  {
368  int32_t v(macro_value);
369  return add_macro(macro_name, v);
370  }
371 
372  /*!
373  * Add a macro to this MacroSet for casted to float
374  * \param macro_name name of macro
375  * \param macro_value value to which macro is given
376  */
377  template<typename T>
378  ShaderSource&
379  add_macro_float(c_string macro_name, T macro_value)
380  {
381  float v(macro_value);
382  return add_macro(macro_name, v);
383  }
384 
385  /*!
386  * Add macros of a MacroSet to this ShaderSource.
387  * Functionally, will insert \#define macro_name macro_value
388  * in the GLSL source code for each macro in the
389  * \ref MacroSet.
390  * \param macros set of macros to add
391  * \param loc location to add macro within code
392  */
393  ShaderSource&
394  add_macros(const MacroSet &macros,
395  enum add_location_t loc = push_back);
396 
397  /*!
398  * Functionally, will insert \#undef macro_name
399  * in the GLSL source code.
400  * \param macro_name name of macro
401  * \param loc location to add macro within code
402  */
403  ShaderSource&
404  remove_macro(c_string macro_name,
405  enum add_location_t loc = push_back);
406 
407  /*!
408  * Remove macros of a MacroSet to this ShaderSource.
409  * Functionally, will insert \#undef macro_name
410  * in the GLSL source code for each macro in the
411  * \ref MacroSet.
412  * \param macros set of macros to remove
413  * \param loc location to add macro within code
414  */
415  ShaderSource&
416  remove_macros(const MacroSet &macros,
417  enum add_location_t loc = push_back);
418 
419  /*!
420  * Specifiy an extension and usage.
421  * \param ext_name name of GL extension
422  * \param tp usage of extension
423  */
424  ShaderSource&
425  specify_extension(c_string ext_name,
427 
428  /*!
429  * Add all the extension specifacation from another
430  * ShaderSource object to this ShaderSource objects.
431  * Extension already set in this ShaderSource that
432  * are specified in obj are overwritten to the values
433  * specified in obj.
434  * \param obj ShaderSource object from which to take
435  */
436  ShaderSource&
437  specify_extensions(const ShaderSource &obj);
438 
439  /*!
440  * Returns the GLSL code assembled. The returned string is only
441  * gauranteed to be valid up until the ShaderSource object
442  * is modified.
443  * \param code_only if true only, return the GLSL code without
444  * the additions of version, extension and
445  * FastUIDraw convenience functions and macros.
446  */
447  c_string
448  assembled_code(bool code_only = false) const;
449 
450 private:
451  void *m_d;
452 };
453 /*! @} */
454 
455 } //namespace glsl
456 } //namespace fastuidraw
457 
458 #endif
A ShaderSource represents the source code to a GLSL shader, specifying blocks of source code and macr...
ShaderSource & remove_macro(c_string macro_name, enum add_location_t loc=push_back)
ShaderSource & add_macros(const MacroSet &macros, enum add_location_t loc=push_back)
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
MacroSet & add_macro_float(c_string macro_name, T macro_value)
add_location_t
Enumeration to determine if source code or a macro.
ShaderSource & add_macro_float(c_string macro_name, T macro_value)
MacroSet & add_macro_i32(c_string macro_name, T macro_value)
ShaderSource & specify_extension(c_string ext_name, enum extension_enable_t tp=enable_extension)
source_t
Enumeration to indiciate the source for a shader.
ShaderSource & specify_version(c_string v)
file c_array.hpp
MacroSet & add_macro(c_string macro_name, c_string macro_value="")
c_string assembled_code(bool code_only=false) const
file util.hpp
ShaderSource & add_macro_u32(c_string macro_name, T macro_value)
ShaderSource & specify_extensions(const ShaderSource &obj)
MacroSet & add_macro_u32(c_string macro_name, T macro_value)
file vecN.hpp
ShaderSource & add_source(c_string str, enum source_t tp=from_file, enum add_location_t loc=push_back)
c_string version(void) const
extension_enable_t
Enumeration to indicate extension enable flags.
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
MacroSet & operator=(const MacroSet &obj)
ShaderSource & add_macro_i32(c_string macro_name, T macro_value)
ShaderSource & remove_macros(const MacroSet &macros, enum add_location_t loc=push_back)