1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#![deny(missing_docs, missing_copy_implementations)]
#[macro_use]
extern crate bitflags;
pub mod state;
pub mod target;
use state::{BlendValue, CullFace, Equation, RasterMethod, StencilOp, FrontFace};
use target::{Mask, Rect, Stencil};
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct DrawState {
pub primitive: state::Primitive,
pub multi_sample: Option<state::MultiSample>,
pub scissor: Option<Rect>,
pub stencil: Option<state::Stencil>,
pub depth: Option<state::Depth>,
pub blend: Option<state::Blend>,
pub color_mask: state::ColorMask,
}
#[derive(Copy, Clone, PartialEq, Debug, Eq, Hash)]
pub enum BlendPreset {
Add,
Multiply,
Alpha,
Invert,
}
impl DrawState {
pub fn new() -> DrawState {
DrawState {
primitive: state::Primitive {
front_face: FrontFace::CounterClockwise,
method: RasterMethod::Fill(CullFace::Back),
offset: None,
},
multi_sample: None,
scissor: None,
stencil: None,
depth: None,
blend: None,
color_mask: state::MASK_ALL,
}
}
pub fn get_target_mask(&self) -> Mask {
use target as t;
(if self.stencil.is_some() {t::STENCIL} else {Mask::empty()}) |
(if self.depth.is_some() {t::DEPTH} else {Mask::empty()}) |
(if self.blend.is_some() {t::COLOR} else {Mask::empty()})
}
pub fn multi_sample(mut self) -> DrawState {
self.multi_sample = Some(state::MultiSample);
self
}
pub fn stencil(mut self, fun: state::Comparison, value: Stencil) -> DrawState {
let side = state::StencilSide {
fun: fun,
value: value,
mask_read: Stencil::max_value(),
mask_write: Stencil::max_value(),
op_fail: StencilOp::Keep,
op_depth_fail: StencilOp::Keep,
op_pass: StencilOp::Keep,
};
self.stencil = Some(state::Stencil {
front: side,
back: side,
});
self
}
pub fn depth(mut self, fun: state::Comparison, write: bool) -> DrawState {
self.depth = Some(state::Depth {
fun: fun,
write: write,
});
self
}
pub fn scissor(mut self, x: u16, y: u16, w: u16, h: u16) -> DrawState {
self.scissor = Some(target::Rect { x: x, y: y, w: w, h: h });
self
}
pub fn blend(mut self, preset: BlendPreset) -> DrawState {
self.blend = Some(match preset {
BlendPreset::Add => state::Blend {
color: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::One,
destination: state::Factor::One,
},
alpha: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::One,
destination: state::Factor::One,
},
value: [0.0, 0.0, 0.0, 0.0],
},
BlendPreset::Multiply => state::Blend {
color: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::ZeroPlus(BlendValue::DestColor),
destination: state::Factor::Zero,
},
alpha: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::ZeroPlus(BlendValue::DestAlpha),
destination: state::Factor::Zero,
},
value: [0.0, 0.0, 0.0, 0.0],
},
BlendPreset::Alpha => state::Blend {
color: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::ZeroPlus(BlendValue::SourceAlpha),
destination: state::Factor::OneMinus(BlendValue::SourceAlpha),
},
alpha: state::BlendChannel {
equation: Equation::Add,
source: state::Factor::One,
destination: state::Factor::One,
},
value: [0.0, 0.0, 0.0, 0.0],
},
BlendPreset::Invert => state::Blend {
color: state::BlendChannel {
equation: Equation::Sub,
source: state::Factor::ZeroPlus(state::BlendValue::ConstColor),
destination: state::Factor::ZeroPlus(state::BlendValue::SourceColor),
},
alpha: state::BlendChannel {
equation: state::Equation::Add,
source: state::Factor::Zero,
destination: state::Factor::One,
},
value: [1.0, 1.0, 1.0, 1.0],
},
});
self
}
}