FastUIDraw
painter_brush_shader_glsl.hpp
Go to the documentation of this file.
1 /*!
2  * \file painter_brush_shader_glsl.hpp
3  * \brief file painter_brush_shader_glsl.hpp
4  *
5  * Copyright 2019 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_BRUSH_SHADER_GLSL_HPP
21 #define FASTUIDRAW_PAINTER_BRUSH_SHADER_GLSL_HPP
22 
23 #include <fastuidraw/util/vecN.hpp>
28 
29 namespace fastuidraw
30 {
31  namespace glsl
32  {
33 /*!\addtogroup GLSL
34  * @{
35  */
36  /*!
37  * \brief
38  * A PainterBrushShaderGLSL is a PainterBrushShader whose
39  * shader code fragment is via GLSL.The vertex shader code needs to implement the function:
40  * \code
41  * void
42  * fastuidraw_gl_vert_brush_main(in uint sub_shader,
43  * inout uint shader_data_block,
44  * in vec2 brush_p)
45  * \endcode
46  * where
47  * - sub_shader corresponds to PainterBrushShader::sub_shader()
48  * - brush_p is the brush position emitted by the item shader and
49  * - shader_data_block is what block in the data store for
50  * the data packed by PainterBrushShaderData::pack_data()
51  * of the PainterBrushShaderData in the \ref Painter call;
52  * use the macro fastuidraw_fetch_data() to read the data.
53  *
54  * The fragment shader code needs to implement the function:
55  * \code
56  * vec4
57  * fastuidraw_gl_frag_brush_main(in uint sub_shader,
58  * inout uint shader_data_block)
59  * \endcode
60  * which returns the color value, pre-multiplied by alpha, by which
61  * to modulate the outgoing fragment color.
62  *
63  * Available to only the vertex shader are the following:
64  * - the GLSL elements in the module \ref GLSLVertCode
65  *
66  * Available to only the fragment shader are the following:
67  * - the GLSL elements in the module \ref GLSLFragCode
68  *
69  * Available to both the vertex and fragment shader are the following:
70  * - the GLSL elements in the module \ref GLSLVertFragCode
71  *
72  * For both stages, the value of the argument of shader_data_block is
73  * which 128-bit block into the data store (PainterDraw::m_store) of the
74  * shader data to be read with the GLSL macro \ref fastuidraw_fetch_data.
75  * On exit, this value must be updated to the location just past the
76  * shader data of the shader.
77  *
78  * For both stages, the value of the argument of sub_shader() is the
79  * value of \ref PainterShader::sub_shader() of the active shader.
80  *
81  * Also, if one defines macros in any of the passed ShaderSource objects,
82  * those macros MUST be undefined at the end. In addition, if one
83  * has local helper functions, to avoid global name collision, those
84  * function names should be wrapped in the macro FASTUIDRAW_LOCAL()
85  * to make sure that the function is given a unique global name within
86  * the uber-shader.
87  *
88  * Lastly, one can use the class \ref UnpackSourceGenerator to generate
89  * shader code to unpack values from the data in the data store buffer.
90  * That machine generated code uses the macro fastuidraw_fetch_data().
91  */
93  {
94  public:
95  /*!
96  * \brief
97  * If one wishes to make use of other \ref PainterBrushShaderGLSL
98  * fastuidraw_gl_vert_brush_main()/fastuidraw_gl_frag_brush_main()
99  * of other shaders (for example to have a simple shader that adds
100  * on to a previous shader), a DependencyList provides the means to do so.
101  *
102  * Each such used shader is given a name by which the caller will use it.
103  * In addition, the caller has access to the varyings and shared symbols
104  * of the callee as well. A varying or shareable V of an element in the \ref
105  * DependencyList is accessed from the parent shader with dep::V where dep
106  * is the argument value of name to \ref add_shader(). Note that it is
107  * accessed with the scope-resolution operator; the uber-shader assember
108  * will convert the scope-resolution operator into acceptable GLSL code.
109  *
110  * By using the values of the shareables (embodied in fields \ref
111  * symbol_list::m_vert_shareable_values and \ref symbol_list::m_frag_shareable_values),
112  * reading and potentially modifying the values of the varyings, one can
113  * create effects building off of the built-in shaders of the GLSL module.
114  *
115  * Lastly, the uber-shader assembler gives a means to avoid collision
116  * in using context textures. To avoid re-using the same context
117  * textures, the macros fastuidraw_brush_start_context_texture,
118  * and fastuidraw_brush_context_texture(X) are provided where
119  * - fastuidraw_brush_start_context_texture is the first texture for
120  * the brush shader to use.
121  * - fastuidraw_brush_context_texture(X) is just
122  * fastuidraw_brush_context_texture[X + fastuidraw_brush_start_context_texture]
123  *
124  * The Uber-shader assembler fills the textures depth-first. Thus the first
125  * dependency uses slots [0, 1, .., N - 1] where N is number_context_textures()
126  * of the first dependency, then the second dependency uses [N, N + 1, ..., N + M - 1]
127  * where M is number_context_textures() of the second dependency and so on.
128  */
130  {
131  public:
132  /*!
133  * Ctor.
134  */
135  DependencyList(void);
136 
137  /*!
138  * Copy ctor.
139  * \param obj value from which to copy
140  */
141  DependencyList(const DependencyList &obj);
142 
143  ~DependencyList();
144 
145  /*!
146  * Assignment operator
147  * \param rhs value from which to copy
148  */
150  operator=(const DependencyList &rhs);
151 
152  /*!
153  * Swap operation
154  * \param obj object with which to swap
155  */
156  void
157  swap(DependencyList &obj);
158 
159  /*!
160  * Add a shader to the DependencyList's list.
161  * \param name name by which to call the shader
162  * \param shader shader to add to this DependencyList
163  */
165  add_shader(c_string name,
167 
168  private:
169  friend class PainterBrushShaderGLSL;
170  void *m_d;
171  };
172 
173  /*!
174  * Ctor.
175  * \param number_context_textures number of context textures the shader
176  * specified in vertex_src/fragment_src uses;
177  * i.e., this value does NOT include the
178  * number of context textures any dependencies
179  * use
180  * \param vertex_src GLSL source holding vertex shader routine
181  * \param fragment_src GLSL source holding fragment shader routine
182  * \param symbols list of symbols of the shader
183  * \param num_sub_shaders the number of sub-shaders it supports
184  * \param dependencies list of other \ref PainterBrushShaderGLSL
185  * that are used directly.
186  */
188  const ShaderSource &vertex_src,
189  const ShaderSource &fragment_src,
190  const symbol_list &symbols,
191  unsigned int num_sub_shaders = 1,
192  const DependencyList &dependencies = DependencyList());
193 
194  /*!
195  * Ctor.
196  * \param number_context_textures number of context textures the shader
197  * specified in vertex_src/fragment_src uses;
198  * i.e., this value does NOT include the
199  * number of context textures any dependencies
200  * use
201  * \param vertex_src GLSL source holding vertex shader routine
202  * \param fragment_src GLSL source holding fragment shader routine
203  * \param symbols list of symbols of the shader
204  * \param num_sub_shaders the number of sub-shaders it supports
205  * \param dependencies list of other \ref PainterBrushShaderGLSL
206  * that are used directly.
207  */
208  PainterBrushShaderGLSL(unsigned int number_context_textures,
209  const ShaderSource &vertex_src,
210  const ShaderSource &fragment_src,
211  const symbol_list &symbols,
212  const DependencyList &dependencies,
213  unsigned int num_sub_shaders = 1);
214 
215  ~PainterBrushShaderGLSL(void);
216 
217  /*!
218  * Number of external textures the custum brush uses.
219  */
220  unsigned int
221  number_context_textures(void) const;
222 
223  /*!
224  * This is the sum across \ref dependency_list_shaders()
225  * of \ref number_context_textures() which in turn gives
226  * the value of fastuidraw_brush_start_context_texture
227  * for the shader code passed in the ctor.
228  */
229  unsigned int
230  context_texture_start(void) const;
231 
232  /*!
233  * Returns the symbol of the shader
234  */
235  const symbol_list&
236  symbols(void) const;
237 
238  /*!
239  * Returns the varyings of the shader, equivalent to
240  * \code
241  * symbols().m_varying_list;
242  * \endcode
243  */
244  const varying_list&
245  varyings(void) const
246  {
247  return symbols().m_varying_list;
248  }
249 
250  /*!
251  * Return the GLSL source of the vertex shader
252  */
253  const ShaderSource&
254  vertex_src(void) const;
255 
256  /*!
257  * Return the GLSL source of the fragment shader
258  */
259  const ShaderSource&
260  fragment_src(void) const;
261 
262  /*!
263  * Return the list of shaders on which this shader is dependent.
264  */
266  dependency_list_shaders(void) const;
267 
268  /*!
269  * Returns the names that each shader listed in \ref
270  * dependency_list_shaders() is referenced by, i.e.
271  * the i'th element of dependency_list_shaders() is
272  * referenced as the i'th element of \ref
273  * dependency_list_names().
274  */
276  dependency_list_names(void) const;
277 
278  private:
279  void *m_d;
280  };
281 /*! @} */
282 
283  }
284 }
285 
286 #endif
A ShaderSource represents the source code to a GLSL shader, specifying blocks of source code and macr...
A PainterBrushShaderGLSL is a PainterBrushShader whose shader code fragment is via GLSL...
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
const ShaderSource & fragment_src(void) const
A wrapper over a pointer to implement reference counting.
file painter_brush_shader.hpp
file blend_mode.hpp
file shader_source.hpp
unsigned int context_texture_start(void) const
DependencyList & operator=(const DependencyList &rhs)
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 & add_shader(c_string name, const reference_counted_ptr< const PainterBrushShaderGLSL > &shader)
unsigned int number_context_textures(void) const
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
file vecN.hpp
If one wishes to make use of other PainterBrushShaderGLSL fastuidraw_gl_vert_brush_main()/fastuidraw_...
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
A PainterBrushShader represents a shader for performing a custom brush coloring.
file symbol_list.hpp
const symbol_list & symbols(void) const
const ShaderSource & vertex_src(void) const
c_array< const c_string > dependency_list_names(void) const
c_array< const reference_counted_ptr< const PainterBrushShaderGLSL > > dependency_list_shaders(void) const