FastUIDraw
blend_mode.hpp
Go to the documentation of this file.
1 /*!
2  * \file blend_mode.hpp
3  * \brief file blend_mode.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 #ifndef FASTUIDRAW_BLEND_MODE_HPP
20 #define FASTUIDRAW_BLEND_MODE_HPP
21 
22 #include <stdint.h>
23 
24 #include <fastuidraw/util/vecN.hpp>
25 #include <fastuidraw/util/util.hpp>
26 
27 namespace fastuidraw
28 {
29 /*!\addtogroup Utility
30  * @{
31  */
32  /*!
33  * \brief
34  * Class to hold the blend mode as exposed by typical
35  * 3D APIs
36  */
37  class BlendMode
38  {
39  public:
40  /*!
41  * \brief
42  * Enumeration to specify blend equation, i.e. glBlendEquation.
43  */
45  {
46  /*!
47  * Indicates to add the values.
48  */
49  ADD,
50 
51  /*!
52  * Indicates to subtract the values.
53  */
55 
56  /*!
57  * Indicates to reverse-subtract the values.
58  */
60 
61  /*!
62  * Indicates to min the values.
63  */
64  MIN,
65 
66  /*!
67  * Indicates to max the values.
68  */
69  MAX,
70 
71  NUMBER_OPS
72  };
73 
74  /*!
75  * \brief
76  * Enumeration to specify the blend coefficient factor,
77  * i.e. glBlendFunc.
78  */
79  enum func_t
80  {
81  /*!
82  * Indicates the coefficient value of 0 in each channel.
83  */
85 
86  /*!
87  * Indicates the coefficient value of 1 in each channel.
88  */
89  ONE,
90 
91  /*!
92  * Indicates the coefficient where each channel value
93  * is the output of the fragment shader.
94  */
96 
97  /*!
98  * Indicates the coefficient where each channel value
99  * is one minus the output of the fragment shader.
100  */
102 
103  /*!
104  * Indicates the coefficient where each channel value
105  * is the value in the framebuffer.
106  */
108 
109  /*!
110  * Indicates the coefficient where each channel value
111  * is one minus the value in the framebuffer.
112  */
114 
115  /*!
116  * Indicates the coefficient where each channel value
117  * is the alpha value of the output of the fragment shader.
118  */
120 
121  /*!
122  * Indicates the coefficient where each channel value
123  * is one minus the alpha value of the output of the
124  * fragment shader.
125  */
127 
128  /*!
129  * Indicates the coefficient where each channel value
130  * is the alpha value in the framebuffer.
131  */
133 
134  /*!
135  * Indicates the coefficient where each channel value
136  * is one minus the alpha value in the framebuffer.
137  */
139 
140  /*!
141  * Indicates the coefficient where each channel value
142  * comes from the constant color value specified as part
143  * of the 3D API state.
144  */
146 
147  /*!
148  * Indicates the coefficient where each channel value
149  * comes from one minus the constant color value specified
150  * as part of the 3D API state.
151  */
153 
154  /*!
155  * Indicates the coefficient where each channel is
156  * the alpha channel of the constant color value
157  * specified as part of the 3D API state.
158  */
160 
161  /*!
162  * Indicates the coefficient where each channel is
163  * one minus the alpha channel of the constant color
164  * value specified as part of the 3D API state.
165  */
167 
168  /*!
169  * Indicates the coefficient where each channel value
170  * is the alpha value of the output of the fragment shader
171  * clamped to [0, 1].
172  */
174 
175  /*!
176  * Indicates the coefficient where each channel value
177  * is the secondary output of the fragment shader (the
178  * secondary output as present in dual-src blending).
179  */
181 
182  /*!
183  * Indicates the coefficient where each channel value
184  * is one minus the secondary output of the fragment
185  * shader (the secondary output as present in dual-src
186  * blending).
187  */
189 
190  /*!
191  * Indicates the coefficient where each channel value
192  * is the alpha channel of the secondary output of the
193  * fragment shader (the secondary output as present in
194  * dual-src blending).
195  */
197 
198  /*!
199  * Indicates the coefficient where each channel value
200  * is one minus the alpha channel of the secondary output
201  * of the fragment shader (the secondary output as present
202  * in dual-src blending).
203  */
205 
206  NUMBER_FUNCS,
207  };
208 
209  /*!
210  * Ctor. Initializes as valid with blending on,
211  * with blend equation as add in all channels,
212  * with src func in all channels as \ref ONE
213  * and dest func in all channels as \ref ZERO.
214  */
215  BlendMode(void)
216  {
217  m_value = pack_bits(blending_on_bit, 1u, 1u)
218  | pack_bits(equation_rgb_bit0, equation_num_bits, ADD)
219  | pack_bits(equation_alpha_bit0, equation_num_bits, ADD)
220  | pack_bits(src_func_rgb_bit0, func_num_bits, ONE)
221  | pack_bits(src_func_alpha_bit0, func_num_bits, ONE)
222  | pack_bits(dst_func_rgb_bit0, func_num_bits, ZERO)
223  | pack_bits(dst_func_alpha_bit0, func_num_bits, ZERO);
224  }
225 
226  /*!
227  * Equality comparison operator.
228  */
229  bool
231  {
232  return m_value == rhs.m_value;
233  }
234 
235  /*!
236  * Inequality comparison operator.
237  */
238  bool
240  {
241  return m_value != rhs.m_value;
242  }
243 
244  /*!
245  * Set the BlendMode to a value to mark it as invalid.
246  */
247  BlendMode&
249  {
250  m_value |= (1u << invalid_bit);
251  return *this;
252  }
253 
254  /*!
255  * Set the BlendMode to a value to mark it as valid.
256  */
257  BlendMode&
259  {
260  m_value &= ~(1u << invalid_bit);
261  return *this;
262  }
263 
264  /*!
265  * Returns true if the BlendMode has been marked as invalid,
266  * see set_as_invalid() and set_as_valid().
267  */
268  bool
269  is_valid(void) const
270  {
271  return !(m_value & (1u << invalid_bit));
272  }
273 
274  /*!
275  * Set that 3D API blending is on or off.
276  * Default value is true.
277  */
278  BlendMode&
279  blending_on(bool v)
280  {
281  m_value &= ~FASTUIDRAW_MASK(blending_on_bit, 1u);
282  m_value |= pack_bits(blending_on_bit, 1u, uint32_t(v));
283  return *this;
284  }
285 
286  /*!
287  * Return the value as set by blending_on(bool).
288  */
289  bool
290  blending_on(void) const
291  {
292  return unpack_bits(blending_on_bit, 1u, m_value) != 0u;
293  }
294 
295  /*!
296  * Set the blend equation for the RGB channels.
297  * Default value is ADD.
298  */
299  BlendMode&
301  {
302  m_value &= ~FASTUIDRAW_MASK(equation_rgb_bit0, equation_num_bits);
303  m_value |= pack_bits(equation_rgb_bit0, equation_num_bits, v);
304  return *this;
305  }
306 
307  /*!
308  * Return the value as set by equation_rgb(enum equation_t).
309  */
310  enum equation_t
311  equation_rgb(void) const
312  {
313  return static_cast<enum equation_t>(unpack_bits(equation_rgb_bit0, equation_num_bits, m_value));
314  }
315 
316  /*!
317  * Set the blend equation for the Alpha channel.
318  * Default value is ADD.
319  */
320  BlendMode&
322  {
323  m_value &= ~FASTUIDRAW_MASK(equation_alpha_bit0, equation_num_bits);
324  m_value |= pack_bits(equation_alpha_bit0, equation_num_bits, v);
325  return *this;
326  }
327 
328  /*!
329  * Return the value as set by equation_alpha(enum equation_t).
330  */
331  enum equation_t
332  equation_alpha(void) const
333  {
334  return static_cast<enum equation_t>(unpack_bits(equation_alpha_bit0, equation_num_bits, m_value));
335  }
336 
337  /*!
338  * Provided as a conveniance, equivalent to
339  * \code
340  * equation_rgb(v);
341  * equation_alpha(v);
342  * \endcode
343  */
344  BlendMode&
346  {
347  equation_rgb(v);
348  return equation_alpha(v);
349  }
350 
351  /*!
352  * Set the source coefficient for the RGB channels.
353  * Default value is ONE.
354  */
355  BlendMode&
357  {
358  m_value &= ~FASTUIDRAW_MASK(src_func_rgb_bit0, func_num_bits);
359  m_value |= pack_bits(src_func_rgb_bit0, func_num_bits, v);
360  return *this;
361  }
362 
363  /*!
364  * Return the value as set by func_src_rgb(enum t).
365  */
366  enum func_t
367  func_src_rgb(void) const
368  {
369  return static_cast<enum func_t>(unpack_bits(src_func_rgb_bit0, func_num_bits, m_value));
370  }
371 
372  /*!
373  * Set the source coefficient for the Alpha channel.
374  * Default value is ONE.
375  */
376  BlendMode&
378  {
379  m_value &= ~FASTUIDRAW_MASK(src_func_alpha_bit0, func_num_bits);
380  m_value |= pack_bits(src_func_alpha_bit0, func_num_bits, v);
381  return *this;
382  }
383 
384  /*!
385  * Return the value as set by func_src_alpha(enum t).
386  */
387  enum func_t
388  func_src_alpha(void) const
389  {
390  return static_cast<enum func_t>(unpack_bits(src_func_alpha_bit0, func_num_bits, m_value));
391  }
392 
393  /*!
394  * Provided as a conveniance, equivalent to
395  * \code
396  * func_src_rgb(v);
397  * func_src_alpha(v);
398  * \endcode
399  */
400  BlendMode&
401  func_src(enum func_t v)
402  {
403  func_src_rgb(v);
404  return func_src_alpha(v);
405  }
406 
407  /*!
408  * Set the destication coefficient for the RGB channels.
409  * Default value is ZERO.
410  */
411  BlendMode&
413  {
414  m_value &= ~FASTUIDRAW_MASK(dst_func_rgb_bit0, func_num_bits);
415  m_value |= pack_bits(dst_func_rgb_bit0, func_num_bits, v);
416  return *this;
417  }
418 
419  /*!
420  * Return the value as set by func_dst_rgb(enum t).
421  */
422  enum func_t
423  func_dst_rgb(void) const
424  {
425  return static_cast<enum func_t>(unpack_bits(dst_func_rgb_bit0, func_num_bits, m_value));
426  }
427 
428  /*!
429  * Set the destication coefficient for the Alpha channel.
430  * Default value is ZERO.
431  */
432  BlendMode&
434  {
435  m_value &= ~FASTUIDRAW_MASK(dst_func_alpha_bit0, func_num_bits);
436  m_value |= pack_bits(dst_func_alpha_bit0, func_num_bits, v);
437  return *this;
438  }
439 
440  /*!
441  * Return the value as set by func_dst_alpha(enum t).
442  */
443  enum func_t
444  func_dst_alpha(void) const
445  {
446  return static_cast<enum func_t>(unpack_bits(dst_func_alpha_bit0, func_num_bits, m_value));
447  }
448 
449  /*!
450  * Provided as a conveniance, equivalent to
451  * \code
452  * func_dst_rgb(v);
453  * func_dst_alpha(v);
454  * \endcode
455  */
456  BlendMode&
457  func_dst(enum func_t v)
458  {
459  func_dst_rgb(v);
460  return func_dst_alpha(v);
461  }
462 
463  /*!
464  * Provided as a conveniance, equivalent to
465  * \code
466  * func_src(src);
467  * func_dst(dst);
468  * \endcode
469  */
470  BlendMode&
471  func(enum func_t src, enum func_t dst)
472  {
473  func_src(src);
474  return func_dst(dst);
475  }
476 
477  /*!
478  * Returns a \ref c_string for an enumerated value.
479  * \param v value to get the label-string of.
480  */
481  static
482  c_string
483  label(enum equation_t v);
484 
485  /*!
486  * Returns a \ref c_string for an enumerated value.
487  * \param v value to get the label-string of.
488  */
489  static
490  c_string
491  label(enum func_t v);
492 
493  private:
494  enum
495  {
496  equation_num_bits = 3,
497  func_num_bits = 5,
498  };
499 
500  enum
501  {
502  blending_on_bit = 0,
503 
504  equation_rgb_bit0 = 1,
505  equation_alpha_bit0 = equation_rgb_bit0 + equation_num_bits,
506 
507  src_func_rgb_bit0 = equation_alpha_bit0 + equation_num_bits,
508  src_func_alpha_bit0 = src_func_rgb_bit0 + func_num_bits,
509 
510  dst_func_rgb_bit0 = src_func_alpha_bit0 + func_num_bits,
511  dst_func_alpha_bit0 = dst_func_rgb_bit0 + func_num_bits,
512 
513  invalid_bit = dst_func_alpha_bit0 + func_num_bits,
514  };
515 
516  uint32_t m_value;
517  };
518 /*! @} */
519 }
520 
521 #endif
#define FASTUIDRAW_MASK(BIT0, NUMBITS)
Definition: util.hpp:59
uint32_t unpack_bits(uint32_t bit0, uint32_t num_bits, uint32_t value)
Definition: util.hpp:328
bool is_valid(void) const
Definition: blend_mode.hpp:269
bool operator!=(BlendMode rhs) const
Definition: blend_mode.hpp:239
enum func_t func_src_alpha(void) const
Definition: blend_mode.hpp:388
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
enum equation_t equation_rgb(void) const
Definition: blend_mode.hpp:311
bool blending_on(void) const
Definition: blend_mode.hpp:290
BlendMode & equation(enum equation_t v)
Definition: blend_mode.hpp:345
BlendMode & func_src_rgb(enum func_t v)
Definition: blend_mode.hpp:356
enum equation_t equation_alpha(void) const
Definition: blend_mode.hpp:332
BlendMode & func_src_alpha(enum func_t v)
Definition: blend_mode.hpp:377
enum func_t func_dst_rgb(void) const
Definition: blend_mode.hpp:423
file util.hpp
enum func_t func_dst_alpha(void) const
Definition: blend_mode.hpp:444
equation_t
Enumeration to specify blend equation, i.e. glBlendEquation.
Definition: blend_mode.hpp:44
BlendMode & func_dst(enum func_t v)
Definition: blend_mode.hpp:457
BlendMode & func_dst_rgb(enum func_t v)
Definition: blend_mode.hpp:412
BlendMode & func(enum func_t src, enum func_t dst)
Definition: blend_mode.hpp:471
file vecN.hpp
uint32_t pack_bits(uint32_t bit0, uint32_t num_bits, uint32_t value)
Definition: util.hpp:294
static c_string label(enum equation_t v)
BlendMode & equation_alpha(enum equation_t v)
Definition: blend_mode.hpp:321
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
BlendMode & set_as_valid(void)
Definition: blend_mode.hpp:258
BlendMode & func_src(enum func_t v)
Definition: blend_mode.hpp:401
enum func_t func_src_rgb(void) const
Definition: blend_mode.hpp:367
BlendMode & blending_on(bool v)
Definition: blend_mode.hpp:279
BlendMode & set_as_invalid(void)
Definition: blend_mode.hpp:248
bool operator==(BlendMode rhs) const
Definition: blend_mode.hpp:230
BlendMode & func_dst_alpha(enum func_t v)
Definition: blend_mode.hpp:433
Class to hold the blend mode as exposed by typical 3D APIs.
Definition: blend_mode.hpp:37
func_t
Enumeration to specify the blend coefficient factor, i.e. glBlendFunc.
Definition: blend_mode.hpp:79
BlendMode & equation_rgb(enum equation_t v)
Definition: blend_mode.hpp:300