]> git.tdb.fi Git - libs/gl.git/blob - source/core/blend.cpp
Make it possible to query if a framebuffer can be presented
[libs/gl.git] / source / core / blend.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
3 #include "blend.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Blend::Blend(BlendFactor sf, BlendFactor df):
11         enabled(true),
12         src_factor(sf),
13         dst_factor(df)
14 { }
15
16 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
17         enabled(true),
18         equation(e),
19         src_factor(sf),
20         dst_factor(df)
21 { }
22
23
24 Blend::Loader::Loader(Blend &b):
25         ObjectLoader<Blend>(b)
26 {
27         add("alpha_to_coverage", &Blend::alpha_to_coverage);
28         add("equation", &Loader::equation);
29         add("factors", &Loader::factors);
30         add("constant", &Loader::constant);
31 }
32
33 void Blend::Loader::constant(float r, float g, float b, float a)
34 {
35         obj.constant = Color(r, g, b, a);
36 }
37
38 void Blend::Loader::equation(BlendEquation eq)
39 {
40         obj.enabled = true;
41         obj.equation = eq;
42 }
43
44 void Blend::Loader::factors(BlendFactor sf, BlendFactor df)
45 {
46         obj.enabled = true;
47         obj.src_factor = sf;
48         obj.dst_factor = df;
49 }
50
51
52 void operator>>(const LexicalConverter &conv, BlendEquation &eq)
53 {
54         const string &str = conv.get();
55         if(str=="ADD")
56                 eq = ADD;
57         else if(str=="SUBTRACT")
58                 eq = SUBTRACT;
59         else if(str=="REVERSE_SUBTRACT")
60                 eq = REVERSE_SUBTRACT;
61         else if(str=="MIN")
62                 eq = MIN;
63         else if(str=="MAX")
64                 eq = MAX;
65         else
66                 throw lexical_error(format("conversion of '%s' to BlendEquation", str));
67 }
68
69 void operator<<(LexicalConverter &conv, BlendEquation eq)
70 {
71         switch(eq)
72         {
73         case ADD: conv.result("ADD"); break;
74         case SUBTRACT: conv.result("SUBTRACT"); break;
75         case REVERSE_SUBTRACT: conv.result("REVERSE_SUBTRACT"); break;
76         case MIN: conv.result("MIN"); break;
77         case MAX: conv.result("MAX"); break;
78         default: conv.result(format("BlendEquation(%#x)", static_cast<int>(eq)));
79         }
80 }
81
82 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
83 {
84         const string &str = conv.get();
85         if(str=="ZERO")
86                 factor = ZERO;
87         else if(str=="ONE")
88                 factor = ONE;
89         else if(str=="SRC_COLOR")
90                 factor = SRC_COLOR;
91         else if(str=="ONE_MINUS_SRC_COLOR")
92                 factor = ONE_MINUS_SRC_COLOR;
93         else if(str=="SRC_ALPHA")
94                 factor = SRC_ALPHA;
95         else if(str=="ONE_MINUS_SRC_ALPHA")
96                 factor = ONE_MINUS_SRC_ALPHA;
97         else if(str=="DST_COLOR")
98                 factor = DST_COLOR;
99         else if(str=="ONE_MINUS_DST_COLOR")
100                 factor = ONE_MINUS_DST_COLOR;
101         else if(str=="DST_ALPHA")
102                 factor = DST_ALPHA;
103         else if(str=="ONE_MINUS_DST_ALPHA")
104                 factor = ONE_MINUS_DST_ALPHA;
105         else if(str=="CONSTANT_COLOR")
106                 factor = CONSTANT_COLOR;
107         else if(str=="ONE_MINUS_CONSTANT_COLOR")
108                 factor = ONE_MINUS_CONSTANT_COLOR;
109         else if(str=="CONSTANT_ALPHA")
110                 factor = CONSTANT_ALPHA;
111         else if(str=="ONE_MINUS_CONSTANT_ALPHA")
112                 factor = ONE_MINUS_CONSTANT_ALPHA;
113         else
114                 throw lexical_error(format("conversion of '%s' to BlendFactor", str));
115 }
116
117 void operator<<(LexicalConverter &conv, BlendFactor factor)
118 {
119         switch(factor)
120         {
121         case ZERO: conv.result("ZERO"); break;
122         case ONE: conv.result("ONE"); break;
123         case SRC_COLOR: conv.result("SRC_COLOR"); break;
124         case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
125         case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
126         case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
127         case DST_COLOR: conv.result("DST_COLOR"); break;
128         case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
129         case DST_ALPHA: conv.result("DST_ALPHA"); break;
130         case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
131         case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
132         case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
133         case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
134         case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
135         default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
136         }
137 }
138
139 void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
140 {
141         ColorWriteMask result = WRITE_NONE;
142         for(const string &p: split(conv.get(), '_'))
143         {
144                 if(p=="ALL")
145                         result = result|WRITE_ALL;
146                 else if(p=="RED")
147                         result = result|WRITE_RED;
148                 else if(p=="GREEN")
149                         result = result|WRITE_GREEN;
150                 else if(p=="BLUE")
151                         result = result|WRITE_BLUE;
152                 else if(p=="ALPHA")
153                         result = result|WRITE_ALPHA;
154                 else
155                         throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
156         }
157         mask = result;
158 }
159
160 void operator<<(LexicalConverter &conv, ColorWriteMask mask)
161 {
162         if(mask==WRITE_ALL)
163                 conv.result("ALL");
164         else if(mask&~WRITE_ALL)
165                 conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
166         else
167         {
168                 string result;
169                 if(mask&WRITE_RED)
170                         result = "RED";
171                 if(mask&WRITE_GREEN)
172                         append(result, "_", "GREEN");
173                 if(mask&WRITE_BLUE)
174                         append(result, "_", "BLUE");
175                 if(mask&WRITE_ALPHA)
176                         append(result, "_", "ALPHA");
177                 conv.result(result);
178         }
179 }
180
181 } // namespace GL
182 } // namespace Msp