FastUIDraw
painter_item_shader_glsl.hpp
Go to the documentation of this file.
1 /*!
2  * \file painter_item_shader_glsl.hpp
3  * \brief file painter_item_shader_glsl.hpp
4  *
5  * Copyright 2016 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
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@gmail.com>
16  *
17  */
18 
19 
20 #ifndef FASTUIDRAW_PAINTER_ITEM_SHADER_GLSL_HPP
21 #define FASTUIDRAW_PAINTER_ITEM_SHADER_GLSL_HPP
22 
26 
27 namespace fastuidraw
28 {
29  namespace glsl
30  {
31 /*!\addtogroup GLSL
32  * @{
33  */
34  /*!
35  * \brief
36  * A PainterItemCoverageShaderGLSL is a collection of GLSL source code
37  * fragments for a \ref PainterShaderRegistrarGLSL.
38  *
39  * The vertex shader code needs to implement the function:
40  * \code
41  * void
42  * fastuidraw_gl_vert_main(in uint sub_shader,
43  * in uvec4 attrib0,
44  * in uvec4 attrib1,
45  * in uvec4 attrib2,
46  * inout uint shader_data_block,
47  * out vec3 clip_p)
48  * \endcode
49  * where
50  * - sub_shader corresponds to PainterItemCoverageShader::sub_shader()
51  * - attrib0 corresponds to PainterAttribute::m_attrib0,
52  * - attrib1 corresponds to PainterAttribute::m_attrib1,
53  * - attrib2 corresponds to PainterAttribute::m_attrib2 and
54  * - shader_data_block is what block in the data store for
55  * the data packed by PainterItemCoverageShaderData::pack_data()
56  * of the PainterItemCoverageShaderData in the \ref Painter call;
57  * use the macro fastuidraw_fetch_data() to read the data.
58  *
59  * The output clip_p is to hold the clip-coordinate of the vertex.
60  *
61  * The fragment shader code needs to implement the function:
62  * \code
63  * float
64  * fastuidraw_gl_frag_main(in uint sub_shader,
65  * inout uint shader_data_block)
66  * \endcode
67  *
68  * which returns the value to write to the coverage buffer
69  * from the fragment for the item.
70  *
71  * Available to only the vertex shader are the following:
72  * - the GLSL elements in the module \ref GLSLVertCode
73  *
74  * Available to only the fragment shader are the following:
75  * - the GLSL elements in the module \ref GLSLFragCode
76  *
77  * Available to both the vertex and fragment shader are the following:
78  * - the GLSL elements in the module \ref GLSLVertFragCode
79  *
80  * For both stages, the value of the argument of shader_data_block is
81  * which 128-bit block into the data store (PainterDraw::m_store) of the
82  * shader data to be read with the GLSL macro \ref fastuidraw_fetch_data.
83  * On exit, this value must be updated to the location just past the
84  * shader data of the shader.
85  *
86  * For both stages, the value of the argument of sub_shader() is the
87  * value of \ref PainterShader::sub_shader() of the active shader.
88  *
89  * Also, if one defines macros in any of the passed ShaderSource objects,
90  * those macros MUST be undefined at the end. In addition, if one
91  * has local helper functions, to avoid global name collision, those
92  * function names should be wrapped in the macro FASTUIDRAW_LOCAL()
93  * to make sure that the function is given a unique global name within
94  * the uber-shader.
95  *
96  * Lastly, one can use the class \ref UnpackSourceGenerator to generate
97  * shader code to unpack values from the data in the data store buffer.
98  * That machine generated code uses the macro fastuidraw_fetch_data().
99  */
101  {
102  public:
103  /*!
104  * \brief
105  * If one wishes to make use of other \ref PainterItemCoverageShaderGLSL
106  * fastuidraw_gl_vert_main()/fastuidraw_gl_frag_main() of other shaders
107  * (for example to have a simple shader that adds on to a previous shader),
108  * a DependencyList provides the means to do so.
109  *
110  * Each such used shader is given a name by which the caller will use it.
111  * In addition, the caller has access to the varyings and shared symbols
112  * of the callee as well. A varying or shareable V of an element in the \ref
113  * DependencyList is accessed from the parent shader with dep::V where dep
114  * is the argument value of name to \ref add_shader(). Note that it is
115  * accessed with the scope-resolution operator; the uber-shader assember
116  * will convert the scope-resolution operator into acceptable GLSL code.
117  *
118  * By using the values of the shareables (embodied in fields \ref
119  * symbol_list::m_vert_shareable_values and \ref symbol_list::m_frag_shareable_values),
120  * reading and potentially modifying the values of the varyings, one can
121  * create effects building off of the built-in shaders of the GLSL module.
122  */
124  {
125  public:
126  /*!
127  * Ctor.
128  */
129  DependencyList(void);
130 
131  /*!
132  * Copy ctor.
133  * \param obj value from which to copy
134  */
135  DependencyList(const DependencyList &obj);
136 
137  ~DependencyList();
138 
139  /*!
140  * Assignment operator
141  * \param rhs value from which to copy
142  */
144  operator=(const DependencyList &rhs);
145 
146  /*!
147  * Swap operation
148  * \param obj object with which to swap
149  */
150  void
151  swap(DependencyList &obj);
152 
153  /*!
154  * Add a shader to the DependencyList's list.
155  * \param name name by which to call the shader
156  * \param shader shader to add to this DependencyList
157  */
159  add_shader(c_string name,
161 
162  private:
163  friend class PainterItemCoverageShaderGLSL;
164  void *m_d;
165  };
166 
167  /*!
168  * Ctor.
169  * \param vertex_src GLSL source holding vertex shader routine
170  * \param fragment_src GLSL source holding fragment shader routine
171  * \param symbols list of symbols of the shader
172  * \param num_sub_shaders the number of sub-shaders it supports
173  * \param dependencies list of other \ref PainterItemCoverageShaderGLSL
174  * that are used directly.
175  */
177  const ShaderSource &fragment_src,
178  const symbol_list &symbols,
179  unsigned int num_sub_shaders = 1,
180  const DependencyList &dependencies = DependencyList());
181 
182  /*!
183  * Ctor.
184  * \param vertex_src GLSL source holding vertex shader routine
185  * \param fragment_src GLSL source holding fragment shader routine
186  * \param symbols list of symbols of the shader
187  * \param num_sub_shaders the number of sub-shaders it supports
188  * \param dependencies list of other \ref PainterItemCoverageShaderGLSL
189  * that are used directly.
190  */
192  const ShaderSource &fragment_src,
193  const symbol_list &symbols,
194  const DependencyList &dependencies,
195  unsigned int num_sub_shaders = 1);
196 
198 
199  /*!
200  * Returns the symbol of the shader
201  */
202  const symbol_list&
203  symbols(void) const;
204 
205  /*!
206  * Returns the varyings of the shader, equivalent to
207  * \code
208  * symbols().m_varying_list;
209  * \endcode
210  */
211  const varying_list&
212  varyings(void) const
213  {
214  return symbols().m_varying_list;
215  }
216 
217  /*!
218  * Return the GLSL source of the vertex shader
219  */
220  const ShaderSource&
221  vertex_src(void) const;
222 
223  /*!
224  * Return the GLSL source of the fragment shader
225  */
226  const ShaderSource&
227  fragment_src(void) const;
228 
229  /*!
230  * Return the list of shaders on which this shader is dependent.
231  */
233  dependency_list_shaders(void) const;
234 
235  /*!
236  * Returns the names that each shader listed in \ref
237  * dependency_list_shaders() is referenced by, i.e.
238  * the i'th element of dependency_list_shaders() is
239  * referenced as the i'th element of \ref
240  * dependency_list_names().
241  */
243  dependency_list_names(void) const;
244 
245  private:
246  void *m_d;
247  };
248 
249  /*!
250  * \brief
251  * A PainterItemShaderGLSL is a collection of GLSL source code
252  * fragments for a \ref PainterShaderRegistrarGLSL.
253  *
254  * The vertex shader code needs to implement the function:
255  * \code
256  * void
257  * fastuidraw_gl_vert_main(in uint sub_shader,
258  * in uvec4 attrib0,
259  * in uvec4 attrib1,
260  * in uvec4 attrib2,
261  * inout uint shader_data_block,
262  * out uint z_add,
263  * out vec2 brush_p,
264  * out vec3 clip_p)
265  * \endcode
266  * where
267  * - sub_shader corresponds to PainterItemShader::sub_shader()
268  * - attrib0 corresponds to PainterAttribute::m_attrib0,
269  * - attrib1 corresponds to PainterAttribute::m_attrib1,
270  * - attrib2 corresponds to PainterAttribute::m_attrib2 and
271  * - shader_data_block is what block in the data store for
272  * the data packed by PainterItemShaderData::pack_data()
273  * of the PainterItemShaderData in the \ref Painter call;
274  * use the macro fastuidraw_fetch_data() to read the data.
275  *
276  * The output clip_p is to hold the clip-coordinate of the vertex.
277  * The output brush_p is to hold the coordinate for the brush of
278  * the vertex. The out z_add must be written to as well and it
279  * is how much to add to the value in \ref PainterHeader::m_z
280  * for the purpose of intra-item z-occluding. Items that do
281  * not self-occlude should write 0 to z_add.
282  *
283  * The fragment shader code needs to implement the function:
284  * \code
285  * vec4
286  * fastuidraw_gl_frag_main(in uint sub_shader,
287  * inout uint shader_data_block)
288  * \endcode
289  *
290  * which returns the color of the fragment for the item -before-
291  * the color modulation by the pen, brush or having blending
292  * applied. In addition, the color value returned MUST be
293  * pre-multiplied by alpha.
294  *
295  * Available to only the vertex shader are the following:
296  * - the GLSL elements in the module \ref GLSLVertCode
297  *
298  * Available to only the fragment shader are the following:
299  * - the GLSL elements in the module \ref GLSLFragCode
300  *
301  * Available to both the vertex and fragment shader are the following:
302  * - the GLSL elements in the module \ref GLSLVertFragCode
303  *
304  * For both stages, the value of the argument of shader_data_block is
305  * which 128-bit block into the data store (PainterDraw::m_store) of the
306  * shader data to be read with the GLSL macro \ref fastuidraw_fetch_data.
307  * On exit, this value must be updated to the location just past the
308  * shader data of the shader.
309  *
310  * For both stages, the value of the argument of sub_shader() is the
311  * value of \ref PainterShader::sub_shader() of the active shader.
312  *
313  * Also, if one defines macros in any of the passed ShaderSource objects,
314  * those macros MUST be undefined at the end. In addition, if one
315  * has local helper functions, to avoid global name collision, those
316  * function names should be wrapped in the macro FASTUIDRAW_LOCAL()
317  * to make sure that the function is given a unique global name within
318  * the uber-shader.
319  *
320  * Lastly, one can use the class \ref UnpackSourceGenerator to generate
321  * shader code to unpack values from the data in the data store buffer.
322  * That machine generated code uses the macro fastuidraw_fetch_data().
323  */
325  {
326  public:
327  /*!
328  * \brief
329  * If one wishes to make use of other \ref PainterItemShaderGLSL
330  * fastuidraw_gl_vert_main()/fastuidraw_gl_frag_main() of other shaders
331  * (for example to have a simple shader that adds on to a previous shader),
332  * a DependencyList provides the means to do so.
333  *
334  * Each such used shader is given a name by which the caller will use it.
335  * In addition, the caller has access to the varyings and shared symbols
336  * of the callee as well. A varying or shareable V of an element in the \ref
337  * DependencyList is accessed from the parent shader with dep::V where dep
338  * is the argument value of name to \ref add_shader(). Note that it is
339  * accessed with the scope-resolution operator; the uber-shader assember
340  * will convert the scope-resolution operator into acceptable GLSL code.
341  *
342  * By using the values of the shareables (embodied in fields \ref
343  * symbol_list::m_vert_shareable_values and \ref symbol_list::m_frag_shareable_values),
344  * reading and potentially modifying the values of the varyings, one can
345  * create effects building off of the built-in shaders of the GLSL module.
346  */
348  {
349  public:
350  /*!
351  * Ctor.
352  */
353  DependencyList(void);
354 
355  /*!
356  * Copy ctor.
357  * \param obj value from which to copy
358  */
359  DependencyList(const DependencyList &obj);
360 
361  ~DependencyList();
362 
363  /*!
364  * Assignment operator
365  * \param rhs value from which to copy
366  */
368  operator=(const DependencyList &rhs);
369 
370  /*!
371  * Swap operation
372  * \param obj object with which to swap
373  */
374  void
375  swap(DependencyList &obj);
376 
377  /*!
378  * Add a shader to the DependencyList's list.
379  * \param name name by which to call the shader
380  * \param shader shader to add to this DependencyList
381  */
383  add_shader(c_string name,
385 
386  private:
387  friend class PainterItemShaderGLSL;
388  void *m_d;
389  };
390 
391  /*!
392  * Ctor.
393  * \param puses_discard set to true if and only if the shader code
394  * will use discard. Discard should be used
395  * in the GLSL code via the macro FASTUIDRAW_DISCARD.
396  * \param vertex_src GLSL source holding vertex shader routine
397  * \param fragment_src GLSL source holding fragment shader routine
398  * \param symbols list of symbols of the shader
399  * \param num_sub_shaders the number of sub-shaders it supports
400  * \param cvg the coverage shader (if any) to be used by the item shader
401  * \param dependencies list of other \ref PainterItemShaderGLSL that are
402  * used directly.
403  */
404  PainterItemShaderGLSL(bool puses_discard,
405  const ShaderSource &vertex_src,
406  const ShaderSource &fragment_src,
407  const symbol_list &symbols,
408  unsigned int num_sub_shaders = 1,
411  const DependencyList &dependencies = DependencyList());
412 
413  /*!
414  * Ctor.
415  * \param puses_discard set to true if and only if the shader code
416  * will use discard. Discard should be used
417  * in the GLSL code via the macro FASTUIDRAW_DISCARD.
418  * \param vertex_src GLSL source holding vertex shader routine
419  * \param fragment_src GLSL source holding fragment shader routine
420  * \param symbols list of symbols of the shader
421  * \param num_sub_shaders the number of sub-shaders it supports
422  * \param cvg the coverage shader (if any) to be used by the item shader
423  * \param dependencies list of other \ref PainterItemShaderGLSL that are
424  * used directly.
425  */
426  PainterItemShaderGLSL(bool puses_discard,
427  const ShaderSource &vertex_src,
428  const ShaderSource &fragment_src,
429  const symbol_list &symbols,
431  const DependencyList &dependencies = DependencyList(),
432  unsigned int num_sub_shaders = 1);
433 
434  /*!
435  * Ctor.
436  * \param puses_discard set to true if and only if the shader code
437  * will use discard. Discard should be used
438  * in the GLSL code via the macro FASTUIDRAW_DISCARD.
439  * \param vertex_src GLSL source holding vertex shader routine
440  * \param fragment_src GLSL source holding fragment shader routine
441  * \param symbols list of symbols of the shader
442  * \param num_sub_shaders the number of sub-shaders it supports
443  * \param cvg the coverage shader (if any) to be used by the item shader
444  * \param dependencies list of other \ref PainterItemShaderGLSL that are
445  * used directly.
446  */
447  PainterItemShaderGLSL(bool puses_discard,
448  const ShaderSource &vertex_src,
449  const ShaderSource &fragment_src,
450  const symbol_list &symbols,
451  const DependencyList &dependencies,
454  unsigned int num_sub_shaders = 1);
455 
456  /*!
457  * Ctor.
458  * \param puses_discard set to true if and only if the shader code
459  * will use discard. Discard should be used
460  * in the GLSL code via the macro FASTUIDRAW_DISCARD.
461  * \param vertex_src GLSL source holding vertex shader routine
462  * \param fragment_src GLSL source holding fragment shader routine
463  * \param symbols list of symbols of the shader
464  * \param num_sub_shaders the number of sub-shaders it supports
465  * \param cvg the coverage shader (if any) to be used by the item shader
466  * \param dependencies list of other \ref PainterItemShaderGLSL that are
467  * used directly.
468  */
469  PainterItemShaderGLSL(bool puses_discard,
470  const ShaderSource &vertex_src,
471  const ShaderSource &fragment_src,
472  const symbol_list &symbols,
473  const DependencyList &dependencies,
474  unsigned int num_sub_shaders,
477 
479 
480  /*!
481  * Returns the symbol of the shader
482  */
483  const symbol_list&
484  symbols(void) const;
485 
486  /*!
487  * Returns the varyings of the shader, equivalent to
488  * \code
489  * symbols().m_varying_list;
490  * \endcode
491  */
492  const varying_list&
493  varyings(void) const
494  {
495  return symbols().m_varying_list;
496  }
497 
498  /*!
499  * Return the GLSL source of the vertex shader
500  */
501  const ShaderSource&
502  vertex_src(void) const;
503 
504  /*!
505  * Return the GLSL source of the fragment shader
506  */
507  const ShaderSource&
508  fragment_src(void) const;
509 
510  /*!
511  * Returns true if the fragment shader uses discard
512  */
513  bool
514  uses_discard(void) const;
515 
516  /*!
517  * Return the list of shaders on which this shader is dependent.
518  */
520  dependency_list_shaders(void) const;
521 
522  /*!
523  * Returns the names that each shader listed in \ref
524  * dependency_list_shaders() is referenced by, i.e.
525  * the i'th element of dependency_list_shaders() is
526  * referenced as the i'th element of \ref
527  * dependency_list_names().
528  */
530  dependency_list_names(void) const;
531 
532  private:
533  void *m_d;
534  };
535 /*! @} */
536 
537  }
538 }
539 
540 #endif
A ShaderSource represents the source code to a GLSL shader, specifying blocks of source code and macr...
const ShaderSource & vertex_src(void) const
const ShaderSource & fragment_src(void) const
const symbol_list & symbols(void) const
A PainterItemShaderGLSL is a collection of GLSL source code fragments for a PainterShaderRegistrarGLS...
A varying_list lists all the in&#39;s of a frag shader (and their names) which is the same as the out&#39;s o...
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
A wrapper over a pointer to implement reference counting.
c_array< const reference_counted_ptr< const PainterItemCoverageShaderGLSL > > dependency_list_shaders(void) const
file shader_source.hpp
If one wishes to make use of other PainterItemShaderGLSL fastuidraw_gl_vert_main()/fastuidraw_gl_frag...
A symbol_list embodies a varying_list along with a set of shareable values for the vertex and fragmen...
Definition: symbol_list.hpp:39
DependencyList & operator=(const DependencyList &rhs)
c_array< const c_string > dependency_list_names(void) const
file painter_item_shader.hpp
A c_array is a wrapper over a C pointer with a size parameter to facilitate bounds checking and provi...
Definition: c_array.hpp:43
A PainterItemCoverageShader represents a shader to draw an item to a coverage buffer (see PainterSurf...
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
file symbol_list.hpp
A PainterItemShader represents a shader to draw an item (typically a vertex and fragment shader pair)...
If one wishes to make use of other PainterItemCoverageShaderGLSL fastuidraw_gl_vert_main()/fastuidraw...
DependencyList & add_shader(c_string name, const reference_counted_ptr< const PainterItemCoverageShaderGLSL > &shader)
A PainterItemCoverageShaderGLSL is a collection of GLSL source code fragments for a PainterShaderRegi...