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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Blender specification
use gfx_hal::pso;

#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum BlendFactor {
    Zero,
    One,
    SrcColor,
    OneMinusSrcColor,
    DstColor,
    OneMinusDstColor,
    SrcAlpha,
    OneMinusSrcAlpha,
    DstAlpha,
    OneMinusDstAlpha,
    ConstColor,
    OneMinusConstColor,
    ConstAlpha,
    OneMinusConstAlpha,
    SrcAlphaSaturate,
    Src1Color,
    OneMinusSrc1Color,
    Src1Alpha,
    OneMinusSrc1Alpha,
}

impl BlendFactor {
    fn to_gfx_blend_factor(self) -> pso::Factor {
        match self {
            BlendFactor::Zero => pso::Factor::Zero,
            BlendFactor::One => pso::Factor::One,
            BlendFactor::SrcColor => pso::Factor::SrcColor,
            BlendFactor::OneMinusSrcColor => pso::Factor::OneMinusSrcColor,
            BlendFactor::DstColor => pso::Factor::DstColor,
            BlendFactor::OneMinusDstColor => pso::Factor::OneMinusDstColor,
            BlendFactor::SrcAlpha => pso::Factor::SrcAlpha,
            BlendFactor::OneMinusSrcAlpha => pso::Factor::OneMinusSrcAlpha,
            BlendFactor::DstAlpha => pso::Factor::DstAlpha,
            BlendFactor::OneMinusDstAlpha => pso::Factor::OneMinusDstAlpha,
            BlendFactor::ConstColor => pso::Factor::ConstColor,
            BlendFactor::OneMinusConstColor => pso::Factor::OneMinusConstColor,
            BlendFactor::ConstAlpha => pso::Factor::ConstAlpha,
            BlendFactor::OneMinusConstAlpha => pso::Factor::OneMinusConstAlpha,
            BlendFactor::SrcAlphaSaturate => pso::Factor::SrcAlphaSaturate,
            BlendFactor::Src1Color => pso::Factor::Src1Color,
            BlendFactor::OneMinusSrc1Color => pso::Factor::OneMinusSrc1Color,
            BlendFactor::Src1Alpha => pso::Factor::Src1Alpha,
            BlendFactor::OneMinusSrc1Alpha => pso::Factor::OneMinusSrc1Alpha,
        }
    }
}

#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum LogicOp {
    Clear,
    And,
    AndReverse,
    Copy,
    AndInverted,
    NoOp,
    Xor,
    Or,
    Nor,
    Equivalent,
    Invert,
    OrReverse,
    CopyInverted,
    OrInverted,
    Nand,
    Set,
}

impl LogicOp {
    fn to_gfx_logic_op(self) -> pso::LogicOp {
        match self {
            LogicOp::Clear => pso::LogicOp::Clear,
            LogicOp::And => pso::LogicOp::And,
            LogicOp::AndReverse => pso::LogicOp::AndReverse,
            LogicOp::Copy => pso::LogicOp::Copy,
            LogicOp::AndInverted => pso::LogicOp::AndInverted,
            LogicOp::NoOp => pso::LogicOp::NoOp,
            LogicOp::Xor => pso::LogicOp::Xor,
            LogicOp::Or => pso::LogicOp::Or,
            LogicOp::Nor => pso::LogicOp::Nor,
            LogicOp::Equivalent => pso::LogicOp::Equivalent,
            LogicOp::Invert => pso::LogicOp::Invert,
            LogicOp::OrReverse => pso::LogicOp::OrReverse,
            LogicOp::CopyInverted => pso::LogicOp::CopyInverted,
            LogicOp::OrInverted => pso::LogicOp::OrInverted,
            LogicOp::Nand => pso::LogicOp::Nand,
            LogicOp::Set => pso::LogicOp::Set,
        }
    }
}

/// Specify the blend operation for a color attachment
#[derive(Clone, Copy, Debug)]
pub enum BlendOp {
    /// Adds the source and destination colors, both multiplied by factors
    Add {
        /// Source multiplied by a factor
        src: BlendFactor,
        /// Destination (attachment) multiplied by factor
        dst: BlendFactor,
    },
    /// Subtracts destination from source, both multiplied by factors
    Sub {
        /// Source multiplied by a factor
        src: BlendFactor,
        /// Destination (attachment) multiplied by factor
        dst: BlendFactor,
    },
    /// Subtracts source from destination, both multiplied by factors
    RevSub {
        /// Source multiplied by a factor
        src: BlendFactor,
        /// Destination (attachment) multiplied by factor
        dst: BlendFactor,
    },
    /// Minimum value of either src or dst
    Min,
    /// Maximum value of either src or dst
    Max,
}

impl BlendOp {
    fn to_gfx_blend_op(self) -> pso::BlendOp {
        match self {
            BlendOp::Add { src, dst } => pso::BlendOp::Add {
                src: src.to_gfx_blend_factor(),
                dst: dst.to_gfx_blend_factor(),
            },
            BlendOp::Sub { src, dst } => pso::BlendOp::Sub {
                src: src.to_gfx_blend_factor(),
                dst: dst.to_gfx_blend_factor(),
            },
            BlendOp::RevSub { src, dst } => pso::BlendOp::RevSub {
                src: src.to_gfx_blend_factor(),
                dst: dst.to_gfx_blend_factor(),
            },
            BlendOp::Min => pso::BlendOp::Min,
            BlendOp::Max => pso::BlendOp::Max,
        }
    }
}

/// Specify whether blending be on or off
#[derive(Clone, Copy, Debug)]
pub struct BlendState {
    /// The blend operations for the color channels
    color: BlendOp,
    /// The blend operations for the alpha channel
    alpha: BlendOp,
}

impl BlendState {
    fn to_gfx_blend_state(self) -> pso::BlendState {
        pso::BlendState {
            color: self.color.to_gfx_blend_op(),
            alpha: self.alpha.to_gfx_blend_op(),
        }
    }
}

/// Main blender access point for setting variables
///
/// The default blender is the default opacity-blender, background objects are blended according to
/// foreground opacity.
#[derive(Clone, Debug)]
pub struct Blender {
    /// Logic ops are ONLY supported for signed and unsigned integer and normalized integer
    /// framebuffers. Not applied to floating point or sRGB color attachments.
    logic_op: Option<pso::LogicOp>,
    targets: Vec<pso::ColorBlendDesc>,
    /// Used to clear the targets if a blend operation is specified.
    is_default: bool,
}

impl Blender {
    pub(crate) fn into_gfx_blender(self) -> pso::BlendDesc {
        pso::BlendDesc {
            logic_op: self.logic_op,
            targets: self.targets,
        }
    }

    /// Set logical operation on the blender
    ///
    /// Logic ops are ONLY supported for signed and unsigned integer and normalized integer
    /// framebuffers. Not applied to floating point or sRGB color attachments.
    /// Defaulted to None
    pub fn logic_op(mut self, op: LogicOp) -> Self {
        self.logic_op = Some(op.to_gfx_logic_op());
        self
    }

    fn add_blend_state(&mut self, state: BlendState, mask: pso::ColorMask) {
        let state = pso::ColorBlendDesc {
            mask,
            blend: Some(state.to_gfx_blend_state()),
        };
        if self.is_default {
            self.targets = vec![state];
            self.is_default = false;
        } else {
            self.targets.push(state);
        }
    }

    /// Set the blender for all channels
    pub fn all(mut self, state: BlendState) -> Self {
        self.add_blend_state(state, pso::ColorMask::ALL);
        self
    }

    /// Set the blender for all color channels
    pub fn colors(mut self, state: BlendOp) -> Self {
        self.add_blend_state(
            BlendState {
                color: state,
                alpha: BlendOp::Min,
            },
            pso::ColorMask::COLOR,
        );
        self
    }

    /// Set the blender for the alpha channel
    pub fn alpha(mut self, state: BlendOp) -> Self {
        self.add_blend_state(
            BlendState {
                color: BlendOp::Min,
                alpha: state,
            },
            pso::ColorMask::ALPHA,
        );
        self
    }

    /// Set the red color blender
    pub fn red(mut self, state: BlendOp) -> Self {
        self.add_blend_state(
            BlendState {
                color: state,
                alpha: BlendOp::Min,
            },
            pso::ColorMask::RED,
        );
        self
    }

    /// Set the green color blender
    pub fn green(mut self, state: BlendOp) -> Self {
        self.add_blend_state(
            BlendState {
                color: state,
                alpha: BlendOp::Min,
            },
            pso::ColorMask::GREEN,
        );
        self
    }

    /// Set the blue color blender
    pub fn blue(mut self, state: BlendOp) -> Self {
        self.add_blend_state(
            BlendState {
                color: state,
                alpha: BlendOp::Min,
            },
            pso::ColorMask::BLUE,
        );
        self
    }

    /// Turn the color blender off
    pub fn none(mut self) -> Self {
        self.targets = vec![pso::ColorBlendDesc {
            mask: pso::ColorMask::NONE,
            blend: None,
        }];
        self
    }
}

impl Default for Blender {
    fn default() -> Self {
        let blend_state = pso::BlendState {
            color: pso::BlendOp::Add {
                src: pso::Factor::SrcAlpha,
                dst: pso::Factor::OneMinusSrcAlpha,
            },
            alpha: pso::BlendOp::Max,
        };
        Self {
            logic_op: None,
            targets: vec![pso::ColorBlendDesc {
                mask: pso::ColorMask::ALL,
                blend: Some(blend_state),
            }],
            is_default: true,
        }
    }
}