|
| 1 | +//! Graphics draw state. |
| 2 | +
|
| 3 | +/// Graphics draw state used for blending, clipping and stencil rendering. |
| 4 | +#[derive(Copy, Clone, PartialEq, Debug, PartialOrd)] |
| 5 | +pub struct DrawState { |
| 6 | + /// Scissor mask to use. If set, no pixel outside of this |
| 7 | + /// rectangle (in screen space) will be written to as a result of rendering. |
| 8 | + pub scissor: Option<[u32; 4]>, |
| 9 | + /// Stencil test to use. If None, no stencil testing is done. |
| 10 | + pub stencil: Option<Stencil>, |
| 11 | + /// Blend function to use. If None, blending is disabled. |
| 12 | + pub blend: Option<Blend>, |
| 13 | +} |
| 14 | + |
| 15 | +impl Default for DrawState { |
| 16 | + fn default() -> Self { |
| 17 | + DrawState::new_alpha() |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl DrawState { |
| 22 | + /// Uses alpha blending. |
| 23 | + pub fn new_alpha() -> DrawState { |
| 24 | + DrawState { |
| 25 | + blend: Some(Blend::Alpha), |
| 26 | + stencil: None, |
| 27 | + scissor: None, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Draws to stencil buffer with value 255. |
| 32 | + /// This can be used for clipping. |
| 33 | + pub fn new_clip() -> DrawState { |
| 34 | + DrawState { |
| 35 | + blend: Some(Blend::Alpha), |
| 36 | + stencil: Some(Stencil::Clip(255)), |
| 37 | + scissor: None, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Tests against stencil buffer with value 255. |
| 42 | + /// Draws inside the shape defined by stencil buffer. |
| 43 | + pub fn new_inside() -> DrawState { |
| 44 | + DrawState { |
| 45 | + blend: Some(Blend::Alpha), |
| 46 | + stencil: Some(Stencil::Inside(255)), |
| 47 | + scissor: None, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Tests against stencil buffer with value 255. |
| 52 | + /// Draws outside the shape defined by stencil buffer. |
| 53 | + pub fn new_outside() -> DrawState { |
| 54 | + DrawState { |
| 55 | + blend: Some(Blend::Alpha), |
| 56 | + stencil: Some(Stencil::Outside(255)), |
| 57 | + scissor: None, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Sets blending. |
| 62 | + pub fn blend(mut self, blend: Blend) -> DrawState { |
| 63 | + self.blend = Some(blend); |
| 64 | + self |
| 65 | + } |
| 66 | + |
| 67 | + /// Sets scissor `[x, y, w, h]`. |
| 68 | + pub fn scissor(mut self, scissor: [u32; 4]) -> DrawState { |
| 69 | + self.scissor = Some(scissor); |
| 70 | + self |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// The blend setting to use when drawing. |
| 75 | +/// |
| 76 | +/// Using presets since some backends need one pipeline state object instance |
| 77 | +/// per blending technique. |
| 78 | +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)] |
| 79 | +pub enum Blend { |
| 80 | + /// Alpha blending (allows semi-transparent pixels). |
| 81 | + /// |
| 82 | + /// ```ignore |
| 83 | + /// dest_color' = src_color * src_alpha + dest_color * (1 - src_alpha) |
| 84 | + /// dest_alpha' = src_alpha + dest_alpha |
| 85 | + /// ``` |
| 86 | + Alpha, |
| 87 | + /// Additive blending. |
| 88 | + /// |
| 89 | + /// ```ignore |
| 90 | + /// dest_color' = src_color + dest_color |
| 91 | + /// dest_alpha' = src_alpha + dest_alpha |
| 92 | + /// ``` |
| 93 | + Add, |
| 94 | + /// Multiply color components. |
| 95 | + /// |
| 96 | + /// ```ignore |
| 97 | + /// dest_color' = src_color * dest_color |
| 98 | + /// dest_alpha' = src_alpha * dest_alpha |
| 99 | + /// ``` |
| 100 | + Multiply, |
| 101 | + /// Invert colors when rendering a white shape. |
| 102 | + /// |
| 103 | + /// ```ignore |
| 104 | + /// dest_color' = ref_color - src_color |
| 105 | + /// dest_alpha' = dest_alpha |
| 106 | + /// ``` |
| 107 | + /// |
| 108 | + /// When combining two fragments, subtract the destination color from a constant color |
| 109 | + /// using the source color as weight. Has an invert effect with the constant color |
| 110 | + /// as base and source color controlling displacement from the base color. |
| 111 | + /// A white source color and a white value results in plain invert. |
| 112 | + /// The output alpha is same as destination alpha. |
| 113 | + Invert, |
| 114 | +} |
| 115 | + |
| 116 | +/// Stencil buffer settings. |
| 117 | +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)] |
| 118 | +pub enum Stencil { |
| 119 | + /// Draw to stencil buffer. |
| 120 | + Clip(u8), |
| 121 | + /// Draw pixels that have stencil value. |
| 122 | + Inside(u8), |
| 123 | + /// Draw pixels that does not have stencil value. |
| 124 | + Outside(u8), |
| 125 | +} |
0 commit comments