]> git.tdb.fi Git - libs/gl.git/blob - source/core/blend.h
3edebc8c7f4bdde90e8d7aa14c2ab87cc300ff04
[libs/gl.git] / source / core / blend.h
1 #ifndef MSP_GL_BLEND_H_
2 #define MSP_GL_BLEND_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include <msp/strings/lexicalcast.h>
6 #include "color.h"
7
8 namespace Msp {
9 namespace GL {
10
11 enum BlendEquation: std::uint8_t
12 {
13         ADD,
14         SUBTRACT,
15         REVERSE_SUBTRACT,
16         MIN,
17         MAX
18 };
19
20 enum BlendFactor: std::uint8_t
21 {
22         ZERO,
23         ONE,
24         SRC_COLOR,
25         ONE_MINUS_SRC_COLOR,
26         SRC_ALPHA,
27         ONE_MINUS_SRC_ALPHA,
28         DST_COLOR,
29         ONE_MINUS_DST_COLOR,
30         DST_ALPHA,
31         ONE_MINUS_DST_ALPHA,
32         CONSTANT_COLOR,
33         ONE_MINUS_CONSTANT_COLOR,
34         CONSTANT_ALPHA,
35         ONE_MINUS_CONSTANT_ALPHA
36 };
37
38 enum ColorWriteMask: std::uint8_t
39 {
40         WRITE_NONE = 0,
41         WRITE_RED = 1,
42         WRITE_GREEN = 2,
43         WRITE_BLUE = 4,
44         WRITE_ALPHA = 8,
45         WRITE_ALL = 15
46 };
47
48 /**
49 Blends incoming fragment color values with those already in the framebuffer.
50 */
51 struct Blend
52 {
53         class Loader: public DataFile::ObjectLoader<Blend>
54         {
55         public:
56                 Loader(Blend &);
57
58         private:
59                 void constant(float, float, float, float);
60                 void equation(BlendEquation);
61                 void factors(BlendFactor, BlendFactor);
62         };
63
64         bool enabled = false;
65         BlendEquation equation = ADD;
66         BlendFactor src_factor = ONE;
67         BlendFactor dst_factor = ZERO;
68         Color constant = { 0.0f, 0.0f, 0.0f, 0.0f };
69         ColorWriteMask write_mask = WRITE_ALL;
70
71         Blend() = default;
72         Blend(BlendFactor, BlendFactor);
73         Blend(BlendEquation, BlendFactor, BlendFactor);
74
75         bool operator==(const Blend &) const;
76         bool operator!=(const Blend &b) const { return !operator==(b); }
77 };
78
79 inline bool Blend::operator==(const Blend &other) const
80 {
81         return enabled==other.enabled && equation==other.equation && src_factor==other.src_factor &&
82                 dst_factor==other.dst_factor && constant==other.constant && write_mask==other.write_mask;
83 }
84
85
86 inline ColorWriteMask operator|(ColorWriteMask m1, ColorWriteMask m2)
87 { return static_cast<ColorWriteMask>(static_cast<int>(m1)|static_cast<int>(m2)); }
88
89 void operator>>(const LexicalConverter &, BlendEquation &);
90 void operator<<(LexicalConverter &, BlendEquation);
91
92 void operator>>(const LexicalConverter &, BlendFactor &);
93 void operator<<(LexicalConverter &, BlendFactor);
94
95 void operator>>(const LexicalConverter &, ColorWriteMask &);
96 void operator<<(LexicalConverter &, ColorWriteMask);
97
98 } // namespace GL
99 } // namespace Msp
100
101 #include "blend_backend.h"
102
103 #endif