1use crate::pso::{graphics::StencilValue, State};
6
7#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub enum Comparison {
11 Never = 0,
13 Less = 1,
15 Equal = 2,
17 LessEqual = 3,
19 Greater = 4,
21 NotEqual = 5,
23 GreaterEqual = 6,
25 Always = 7,
27}
28
29bitflags!(
30 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32 pub struct ColorMask: u8 {
33 const RED = 0x1;
35 const GREEN = 0x2;
37 const BLUE = 0x4;
39 const ALPHA = 0x8;
41 const COLOR = 0x7;
43 const ALL = 0xF;
45 const NONE = 0x0;
47 }
48);
49
50impl Default for ColorMask {
51 fn default() -> Self {
52 Self::ALL
53 }
54}
55
56#[allow(missing_docs)]
60#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
62pub enum Factor {
63 Zero = 0,
64 One = 1,
65 SrcColor = 2,
66 OneMinusSrcColor = 3,
67 DstColor = 4,
68 OneMinusDstColor = 5,
69 SrcAlpha = 6,
70 OneMinusSrcAlpha = 7,
71 DstAlpha = 8,
72 OneMinusDstAlpha = 9,
73 ConstColor = 10,
74 OneMinusConstColor = 11,
75 ConstAlpha = 12,
76 OneMinusConstAlpha = 13,
77 SrcAlphaSaturate = 14,
78 Src1Color = 15,
79 OneMinusSrc1Color = 16,
80 Src1Alpha = 17,
81 OneMinusSrc1Alpha = 18,
82}
83
84#[allow(missing_docs)]
86#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
87#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
88pub enum BlendOp {
89 Add { src: Factor, dst: Factor },
92 Sub { src: Factor, dst: Factor },
95 RevSub { src: Factor, dst: Factor },
98 Min,
100 Max,
102}
103
104impl BlendOp {
105 pub const REPLACE: Self = BlendOp::Add {
107 src: Factor::One,
108 dst: Factor::Zero,
109 };
110 pub const ADD: Self = BlendOp::Add {
112 src: Factor::One,
113 dst: Factor::One,
114 };
115 pub const ALPHA: Self = BlendOp::Add {
117 src: Factor::SrcAlpha,
118 dst: Factor::OneMinusSrcAlpha,
119 };
120 pub const PREMULTIPLIED_ALPHA: Self = BlendOp::Add {
122 src: Factor::One,
123 dst: Factor::OneMinusSrcAlpha,
124 };
125}
126
127#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
130#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
131pub struct BlendState {
132 pub color: BlendOp,
134 pub alpha: BlendOp,
136}
137
138impl BlendState {
139 pub const REPLACE: Self = BlendState {
141 color: BlendOp::REPLACE,
142 alpha: BlendOp::REPLACE,
143 };
144 pub const ADD: Self = BlendState {
146 color: BlendOp::ADD,
147 alpha: BlendOp::ADD,
148 };
149 pub const MULTIPLY: Self = BlendState {
151 color: BlendOp::Add {
152 src: Factor::Zero,
153 dst: Factor::SrcColor,
154 },
155 alpha: BlendOp::Add {
156 src: Factor::Zero,
157 dst: Factor::SrcAlpha,
158 },
159 };
160 pub const ALPHA: Self = BlendState {
162 color: BlendOp::ALPHA,
163 alpha: BlendOp::PREMULTIPLIED_ALPHA,
164 };
165 pub const PREMULTIPLIED_ALPHA: Self = BlendState {
167 color: BlendOp::PREMULTIPLIED_ALPHA,
168 alpha: BlendOp::PREMULTIPLIED_ALPHA,
169 };
170}
171
172#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
174#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
175pub struct ColorBlendDesc {
176 pub mask: ColorMask,
178 pub blend: Option<BlendState>,
180}
181
182impl ColorBlendDesc {
183 pub const EMPTY: Self = ColorBlendDesc {
186 mask: ColorMask::ALL,
187 blend: None,
188 };
189}
190
191#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
193#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
194pub struct DepthTest {
195 pub fun: Comparison,
197 pub write: bool,
199}
200
201impl DepthTest {
202 pub const FAIL: Self = DepthTest {
204 fun: Comparison::Never,
205 write: false,
206 };
207 pub const PASS_TEST: Self = DepthTest {
211 fun: Comparison::Always,
212 write: false,
213 };
214 pub const PASS_WRITE: Self = DepthTest {
217 fun: Comparison::Always,
218 write: true,
219 };
220}
221
222#[repr(u8)]
224#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
225#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
226pub enum StencilOp {
227 Keep = 0,
229 Zero = 1,
231 Replace = 2,
233 IncrementClamp = 3,
235 DecrementClamp = 4,
237 Invert = 5,
239 IncrementWrap = 6,
241 DecrementWrap = 7,
243}
244
245#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
247#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
248pub struct StencilFace {
249 pub fun: Comparison,
251 pub op_fail: StencilOp,
253 pub op_depth_fail: StencilOp,
255 pub op_pass: StencilOp,
257}
258
259impl Default for StencilFace {
260 fn default() -> StencilFace {
261 StencilFace {
262 fun: Comparison::Never,
263 op_fail: StencilOp::Keep,
264 op_depth_fail: StencilOp::Keep,
265 op_pass: StencilOp::Keep,
266 }
267 }
268}
269
270#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
272#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
273pub struct Sided<T> {
274 pub front: T,
276 pub back: T,
278}
279
280impl<T: Copy> Sided<T> {
281 pub fn new(value: T) -> Self {
284 Sided {
285 front: value,
286 back: value,
287 }
288 }
289}
290
291pub type StencilValues = State<Sided<StencilValue>>;
294
295#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
301#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
302pub struct StencilTest {
303 pub faces: Sided<StencilFace>,
305 pub read_masks: StencilValues,
308 pub write_masks: StencilValues,
310 pub reference_values: StencilValues,
312}
313
314impl Default for StencilTest {
315 fn default() -> Self {
316 StencilTest {
317 faces: Sided::default(),
318 read_masks: State::Static(Sided::new(!0)),
319 write_masks: State::Static(Sided::new(!0)),
320 reference_values: State::Static(Sided::new(0)),
321 }
322 }
323}
324
325#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
327#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
328pub struct DepthStencilDesc {
329 pub depth: Option<DepthTest>,
331 pub depth_bounds: bool,
333 pub stencil: Option<StencilTest>,
335}
336
337impl DepthStencilDesc {
338 pub fn uses_depth(&self) -> bool {
340 self.depth.is_some() || self.depth_bounds
341 }
342 pub fn uses_stencil(&self) -> bool {
344 self.stencil.is_some()
345 }
346}
347
348bitflags!(
349 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
351 pub struct Face: u32 {
352 const NONE = 0x0;
354 const FRONT = 0x1;
356 const BACK = 0x2;
358 }
359);