FastUIDraw
glyph_render_data_banded_rays.hpp
Go to the documentation of this file.
1 /*!
2  * \file glyph_render_data_banded_rays.hpp
3  * \brief file glyph_render_data_banded_rays.hpp
4  *
5  * Copyright 2018 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_GLYPH_RENDER_DATA_BANDED_RAYS_HPP
21 #define FASTUIDRAW_GLYPH_RENDER_DATA_BANDED_RAYS_HPP
22 
23 #include <fastuidraw/util/rect.hpp>
26 
27 namespace fastuidraw
28 {
29 /*!\addtogroup Glyph
30  * @{
31  */
32 
33  /*!
34  * A GlyphRenderDataBandedRays represents the data needed to
35  * build a glyph to render it directly from the geometric data
36  * of the curve. This implementation does NOT use the techinque
37  * as patented by Eric Lengyel that computes by a lookup table
38  * keyed by the 3 conditions of examing q1, q2, q3 against 0.
39  * The Lengyel technique does give a fast shader, but we cannot
40  * use it because of the patent on it. Instead, we enforece the
41  * condition 0 <= t <= 1 of the solutions to 0 = [q1, q2, q3](t)
42  * by the hammer of algebra. Assuming that the leading coefficient
43  * of the quadratic is positive the enforement then becomes
44  * - use t1 if (q1 >= q2 && q1 >= 0.0) && (q3 > q2 || q3 < 0.0)
45  * - use t2 if (q3 > q2 && q3 > 0.0) && (q1 >= q2 || q1 <= 0.0)
46  *
47  * The fragment shader needs to have additional code to handle
48  * the case where the leading coefficient is negative.
49  *
50  * The attribute data for a Glyph has:
51  * - the number of bands horizontally H
52  * - the number of bands vertically V
53  * - the offset to the glyph data
54  *
55  * Each horizontal band is the EXACT same height and each vertical
56  * band is the EXACT same width. The data location of a horizontal
57  * and vertical band is implicitely given from the location of
58  * the glyph data as follows (where O = start of glyph data):
59  *
60  * - horizontal_band_plus_infinity(I) is at I + O
61  * - horizontal_band_negative_infinity(I) is at H + I + O
62  * - vertical_band_plus_infinity(I) is at 2 * H + I + O
63  * - vertical_band_negative_infinity(I) is at V + 2 * H + I + O
64  *
65  * The value I for horizontal bands is computed as I = ny * V and
66  * the value I for vertical bands is computed as I = nx * H where
67  * nx is the glyph x-coordinate normalized to [0, 1] and ny is the
68  * glyph y-coordinate normalized to [0, 1].
69  *
70  * A band is encoded by \ref band_t (which gives the number of
71  * curves and offset to the curves).
72  *
73  * Rather than encoding where in the glyph the horizontal and
74  * vertical bands split, we just say that the bands are split
75  * in the middle always (TODO: is this wise really?)
76  */
78  {
79  public:
80  /*!
81  * A band_t represents the header for listing the curves of a band.
82  * Each curve of a band is THREE points (see \ref point_packing_t
83  * how points are packed). The packing of the curves is done as follows.
84  * Let the band have curves c1, c2, ..., cN were the curve cI has points
85  * (pI_0, pI_1, pI_2). The packing of points is:
86  *
87  * p1_0, p1_1, p1_2, p2_0, p2_1, p2_2, .... , pN_0, pN_1, pN_2
88  */
89  enum band_t
90  {
91  /*
92  * Number of bits used to encode the number
93  * of curves; note that the maximum number
94  * of allowed curves in a band is 256 curves.
95  */
96  band_numcurves_numbits = 8,
97 
98  /*
99  * The number of bits to encode the offset to
100  * where the curves are located RELATIVE to
101  * the location of the glyph data.
102  */
103  band_curveoffset_numbits = 32 - band_numcurves_numbits,
104 
105  /*!
106  * first bit used to encode the number of curves
107  * in a band
108  */
110 
111  /*!
112  * first bit used to encode the offset to the curves
113  */
114  band_curveoffset_bit0 = band_numcurves_bit0 + band_numcurves_numbits
115  };
116 
117  /*!
118  * Points are packed as (fp16, fp16) pairs.
119  */
121  {
122  };
123 
124  enum
125  {
126  /*!
127  * The glyph coordinate value in each coordiante varies
128  * from -\ref glyph_coord_value to +\ref glyph_coord_value,
129  * i.e. the glyph is drawn as rect with min-corner
130  * (-\ref glyph_coord_value, -\ref glyph_coord_value)
131  * and max-corner (+\ref glyph_coord_value, +\ref glyph_coord_value)
132  */
134  };
135 
136  /*!
137  * This enumeration describes the meaning of the
138  * attributes. The data of the glyph is offset so
139  * that a shader can assume that the bottom left
140  * corner has glyph-coordinate (0, 0) and the top
141  * right corner has glyph-coordinate (width, height)
142  * where width and height are the width and height
143  * of the glyph in glyph coordinates.
144  */
146  {
147  /*!
148  * Value is 0 if on min-x side of glyph, value is
149  * 1 if on max-x side of glyph; packed as uint.
150  */
152 
153  /*!
154  * Value is 0 if on min-y side of glyph, value is
155  * 1 if on max-y side of glyph; packed as uint.
156  */
158 
159  /*!
160  * the index into GlyphAttribute::m_data storing
161  * the number of vertical bands in the glyph;
162  * packing as uint.
163  */
165 
166  /*!
167  * the index into GlyphAttribute::m_data storing
168  * the number of horizontal bands in the glyph;
169  * packing as uint.
170  */
172 
173  /*!
174  * the index into GlyphAttribute::m_data storing
175  * the fill rule and the offset into the store for
176  * the glyph data. The offset is encoded as follows
177  * - bits0-bits29 encode the offset
178  * - bit30 indicates to complement fill
179  * - bit31 up indicates odd-even fill rule and
180  * down indicates non-zero fill rule.
181  */
183 
184  /*!
185  * Number attribute values needed.
186  */
188  };
189 
190  /*!
191  * A query_info holds data about a \ref GlyphRenderDataBandedRays
192  * value (after its finalized method).
193  */
195  {
196  public:
197  /*!
198  * Default ctor, initializing the value as empty
199  */
200  query_info(void):
203  {}
204 
205  /*!
206  * Set the value of a \ref vecN of \ref GlyphAttribute values
207  * derived from this query_info object
208  * \param out_attribs location to which to write GlyphAttribute values
209  * \param fill_rule fill rule with which to fill the glyphs
210  * \param offset location of glyph data
211  */
212  void
214  enum PainterEnums::fill_rule_t fill_rule,
215  uint32_t offset);
216 
217  /*!
218  * The GPU data of the queried \ref GlyphRenderDataBandedRays
219  * object; the data pointed to by the array is backed
220  * internally by the queried \ref GlyphRenderDataBandedRays;
221  * thus, if the point becomes invalid once the queried
222  * \ref GlyphRenderDataBandedRays goes out of scope.
223  */
225 
226  /*!
227  * The number of vertical bands of the queried
228  * \ref GlyphRenderDataBandedRays
229  */
231 
232  /*!
233  * The number of horizontal bands of the queried
234  * \ref GlyphRenderDataBandedRays
235  */
237  };
238 
239  /*!
240  * Ctor.
241  */
243 
245 
246  /*!
247  * Start a contour. Before starting a new contour
248  * the previous contour must be closed by calling
249  * line_to() or quadratic_to() connecting to the
250  * start point of the previous contour.
251  * \param pt start point of the new contour
252  */
253  void
254  move_to(vec2 pt);
255 
256  /*!
257  * Add a line segment connecting the end point of the
258  * last curve or line segment of the current contour to
259  * a given point.
260  * \param pt end point of the new line segment
261  */
262  void
263  line_to(vec2 pt);
264 
265  /*!
266  * Add a quadratic curveconnecting the end point of the
267  * last curve or line segment of the current contour
268  * \param ct control point of the quadratic curve
269  * \param pt end point of the quadratic curve
270  */
271  void
272  quadratic_to(vec2 ct, vec2 pt);
273 
274  /*!
275  * Finalize the input data after which no more contours or curves may be added;
276  * all added contours must be closed before calling finalize(). Once finalize()
277  * is called, no further data can be added. All contours added must be closed as
278  * well.
279  * \param f fill rule to use for rendering, must be one of
280  * PainterEnums::nonzero_fill_rule or \ref
281  * PainterEnums::odd_even_fill_rule.
282  * \param glyph_rect the rect of the glpyh
283  * \param max_recursion maximum level of recursion to employ to reduce the
284  * complexity of each band. The number of bands that are
285  * generated in a dimension is 2^N where N is the number
286  * of levels of recursion used to generate bands.
287  * \param avg_num_curves_thresh when a band is generated, the average number of
288  * curves needed to perform the coverage computation
289  * is computed. When that value is lower than this
290  * parameter, the band is considered that is does
291  * not need to be reduced to smaller bands.
292  */
293  void
294  finalize(enum PainterEnums::fill_rule_t f, const Rect &glyph_rect,
295  int max_recursion, float avg_num_curves_thresh);
296 
297  /*!
298  * Finalize the input data after which no more contours or curves may be added;
299  * all added contours must be closed before calling finalize(). Once finalize()
300  * is called, no further data can be added. All contours added must be closed as
301  * well. Provided as a conveniance, equivalent to
302  * \code
303  * finalize(f, glyph_rect, GlyphGenerateParams::banded_rays_max_recursion(),
304  * GlyphGenerateParams::banded_rays_average_number_curves_thresh());
305  * \endcode
306  * \param f fill rule to use for rendering, must be one of
307  * PainterEnums::nonzero_fill_rule or \ref
308  * PainterEnums::odd_even_fill_rule.
309  * \param glyph_rect the rect of the glpyh
310  */
311  void
312  finalize(enum PainterEnums::fill_rule_t f, const Rect &glyph_rect);
313 
314  /*!
315  * Query the data; may only be called after finalize(). Returns
316  * \ref routine_fail if finalize() has not yet been called.
317  * \param out_info location to which to write information about
318  * this object.
319  */
320  enum return_code
321  query(query_info *out_info) const;
322 
323  virtual
325  render_info_labels(void) const;
326 
327  virtual
329  upload_to_atlas(GlyphAtlasProxy &atlas_proxy,
330  GlyphAttribute::Array &attributes,
331  c_array<float> render_costs) const;
332 
333  private:
334  void *m_d;
335  };
336 /*! @} */
337 }
338 
339 #endif
Represents an opaque array of GlyphAttribute values.
GlyphRenderData provides an interface to specify data used for rendering glyphs and to pack that data...
file glyph_render_data.hpp
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
void finalize(enum PainterEnums::fill_rule_t f, const Rect &glyph_rect, int max_recursion, float avg_num_curves_thresh)
enum return_code query(query_info *out_info) const
return_code
Enumeration for simple return codes for functions for success or failure.
Definition: util.hpp:142
vecN is a simple static array class with no virtual functions and no memory overhead. Supports runtim array index checking and STL style iterators via pointer iterators.
Definition: vecN.hpp:42
An GlyphAtlasProxy is a proxy for a GlyphAtlas; one can allocate through it. Internally it tracks all...
void quadratic_to(vec2 ct, vec2 pt)
void set_glyph_attributes(vecN< GlyphAttribute, glyph_num_attributes > *out_attribs, enum PainterEnums::fill_rule_t fill_rule, uint32_t offset)
file painter_enums.hpp
fill_rule_t
Enumerations specifying common fill rules.
virtual c_array< const c_string > render_info_labels(void) const
virtual enum fastuidraw::return_code upload_to_atlas(GlyphAtlasProxy &atlas_proxy, GlyphAttribute::Array &attributes, c_array< float > render_costs) const