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
//! Various options to specify how to combine pixels with what they draw over
//!
//! GPU blending follows the equation:
//!
//! `Output = Operation(SourceFunction(Source), DestFunction(Destination))`
//!
//! The `Operation` is controlled by [`BlendEquation`], and the
//! `SourceFunction` and `DestFunction` are controlled by [`BlendFunction`]. Both of these are
//! combined to form a [`BlendMode`], which can be applied by [`Context::set_blend_mode`].
//!
//! The default [`BlendMode`] produces the result of:
//!
//! `Output = Source.Alpha * Source + (1 - Source.Alpha) * Destination`
//!
//! and looks like:
//!
//! ```no_run
//! # use golem::blend::{
//! #   BlendMode, BlendInput, BlendFactor, BlendChannel, BlendOperation, BlendEquation,
//! #   BlendFunction,
//! # };
//! # fn test() -> BlendMode {
//! BlendMode {
//!     equation: BlendEquation::Same(BlendOperation::Add),
//!     function: BlendFunction::Same {
//!         source: BlendFactor::Color {
//!             input: BlendInput::Source,
//!             channel: BlendChannel::Alpha,
//!             is_inverse: false,
//!         },
//!         destination: BlendFactor::Color {
//!             input: BlendInput::Source,
//!             channel: BlendChannel::Alpha,
//!             is_inverse: true,
//!         },
//!     },
//!     global_color: [0.0; 4]
//! }
//! # }
//! ```
//!
//! For more information, see the documentation for the individual Blend enums.
//!
//! [`Context::set_blend_mode`]: crate::Context::set_blend_mode

/// The state of the blend pipeline
///
/// See [`Context::set_blend_mode`]
///
/// [`Context::set_blend_mode`]: crate::Context::set_blend_mode
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct BlendMode {
    /// How to combine the source and destination
    pub equation: BlendEquation,
    /// How to transform the inputs to the `equation`
    pub function: BlendFunction,
    /// A color in blending that's neither the source nor destination, specified as [R, G, B, A]
    ///
    /// This provides the value for [`BlendInput::GlobalBlend`]
    pub global_color: [f32; 4],
}

impl Default for BlendMode {
    fn default() -> BlendMode {
        BlendMode {
            equation: BlendEquation::default(),
            function: BlendFunction::default(),
            global_color: [0.0; 4],
        }
    }
}

/// How to combine the values when blending
///
/// Almost all the time you'll want `BlendEquation::Same(BlendOperation::Add)`, but there are cases
/// where other blend equatiosn come in handy.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendEquation {
    /// Apply the same equation to the color and alpha of the blended pixels
    Same(BlendOperation),
    /// Apply a different equation to the color and alpha channel of the blended pixels
    Separate {
        color: BlendOperation,
        alpha: BlendOperation,
    },
}

impl Default for BlendEquation {
    fn default() -> BlendEquation {
        BlendEquation::Same(BlendOperation::Add)
    }
}

/// The operation to apply to the pixels during blending
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendOperation {
    /// Output = Source + Destination
    Add,
    /// Output = Source - Destination
    Subtract,
    /// Output = Destination - Source
    ReverseSubtract,
    /// Output = Max(Source, Destination)
    Max,
    /// Output = Min(Source, Destination)
    Min,
}

impl BlendOperation {
    pub(crate) fn to_gl(self) -> u32 {
        use BlendOperation::*;
        match self {
            Add => glow::FUNC_ADD,
            Subtract => glow::FUNC_SUBTRACT,
            ReverseSubtract => glow::FUNC_REVERSE_SUBTRACT,
            Max => glow::MAX,
            Min => glow::MIN,
        }
    }
}

/// The blend function controls how the source and destination are transformed
///
/// Before being passed to the [`BlendEquation`], the source and destination are multiplied by the
/// value determined by `BlendFunction`.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendFunction {
    /// Use the same [`BlendFactor`] on the color and alpha channels
    Same {
        source: BlendFactor,
        destination: BlendFactor,
    },
    /// Use different [`BlendFactor`]s on the color and alpha channels
    Separate {
        source_color: BlendFactor,
        source_alpha: BlendFactor,
        destination_color: BlendFactor,
        destination_alpha: BlendFactor,
    },
}

impl Default for BlendFunction {
    fn default() -> BlendFunction {
        BlendFunction::Same {
            source: BlendFactor::Color {
                input: BlendInput::Source,
                channel: BlendChannel::Alpha,
                is_inverse: false,
            },
            destination: BlendFactor::Color {
                input: BlendInput::Source,
                channel: BlendChannel::Alpha,
                is_inverse: true,
            },
        }
    }
}

/// The various coefficients to multiply the color inputs by
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendFactor {
    Zero,
    One,
    Color {
        /// Which source of color
        input: BlendInput,
        /// Which channel of the `input` to use
        channel: BlendChannel,
        /// Whether to use (1 - Value) instead of Value
        is_inverse: bool,
    },
}

impl BlendFactor {
    pub(crate) fn to_gl(self) -> u32 {
        use BlendChannel::*;
        use BlendFactor::{Color as Col, One, Zero};
        use BlendInput::*;

        match self {
            Zero => glow::ZERO,
            One => glow::ONE,
            Col {
                input: Source,
                channel: Color,
                is_inverse: false,
            } => glow::SRC_COLOR,
            Col {
                input: Source,
                channel: Color,
                is_inverse: true,
            } => glow::ONE_MINUS_SRC_COLOR,
            Col {
                input: Source,
                channel: Alpha,
                is_inverse: false,
            } => glow::SRC_ALPHA,
            Col {
                input: Source,
                channel: Alpha,
                is_inverse: true,
            } => glow::ONE_MINUS_SRC_ALPHA,
            Col {
                input: Destination,
                channel: Color,
                is_inverse: false,
            } => glow::DST_COLOR,
            Col {
                input: Destination,
                channel: Color,
                is_inverse: true,
            } => glow::ONE_MINUS_DST_COLOR,
            Col {
                input: Destination,
                channel: Alpha,
                is_inverse: false,
            } => glow::DST_ALPHA,
            Col {
                input: Destination,
                channel: Alpha,
                is_inverse: true,
            } => glow::ONE_MINUS_DST_ALPHA,
            Col {
                input: GlobalBlend,
                channel: Color,
                is_inverse: false,
            } => glow::CONSTANT_COLOR,
            Col {
                input: GlobalBlend,
                channel: Color,
                is_inverse: true,
            } => glow::ONE_MINUS_CONSTANT_COLOR,
            Col {
                input: GlobalBlend,
                channel: Alpha,
                is_inverse: false,
            } => glow::CONSTANT_ALPHA,
            Col {
                input: GlobalBlend,
                channel: Alpha,
                is_inverse: true,
            } => glow::ONE_MINUS_CONSTANT_ALPHA,
        }
    }
}

/// A color to pull from when blending
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendInput {
    /// The pixel that is being drawn
    Source,
    /// The pixel that is being replaced
    Destination,
    /// The color supplied to [`BlendMode::global_color`]
    GlobalBlend,
}

/// Which part of the [`BlendInput`] to pull from
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlendChannel {
    /// The RGB component when using separate functions, or RGBA otherwise
    Color,
    /// The Alpha component
    Alpha,
}