Crate blend_formula

Source
Expand description

Provides two macros, blend_formula! and blend_equation!, used to evaluate an arbitrary formula to its equivalent GPU blend mode. If none exist, it’s a compile-time error.

Valid formulae are equivalent to some Term*Factor Op Term*Factor, where:

If a formula is for a color or alpha equation, non-scalar terms must be “converted” using the appropriate accessor (.rgb or .a).

For convenience, any valid operator can be used to combine the source and destination without a factor (+, -, <, >, *).

§Examples

use blend_formula::*;
 
 // Formulae:
assert_eq!(blend_formula!(src*src.a + dst*(1 - src.a)), BlendFormula {
    src_factor: BlendFactor::SrcAlpha,
    dst_factor: BlendFactor::OneMinusSrcAlpha,
    operation: BlendOperation::Add
});
assert_eq!(blend_formula!(-src), BlendFormula {
    src_factor: BlendFactor::One,
    dst_factor: BlendFactor::Zero,
    operation: BlendOperation::RevSub
});
assert_eq!(blend_formula!(dst < src*c), BlendFormula {
    src_factor: BlendFactor::Constant,
    dst_factor: BlendFactor::One,
    operation: BlendOperation::Min
});
 
 // Equations:
assert_eq!(blend_equation!(src + dst*(1-src.a)), 
    BlendEquation::PREMULTIPLIED_ALPHA_BLENDING
);
assert_eq!(blend_equation!((dst+(src-dst)*src.a).rgb, (dst+src-dst*src).a),
    BlendEquation::ALPHA_BLENDING
);
 
 // Shortcuts:
assert_eq!(blend_formula!(+), blend_formula!(src+dst));
assert_eq!(blend_formula!(*), blend_formula!(src*dst));
assert_eq!(blend_equation!(-, dst.a), BlendEquation {
    color: blend_formula!(src-dst),
    alpha: blend_formula!(dst),
});

§Conversion

If the feature wgpu is enabled, conversion traits are implemented for the corresponding types of the wgpu crate.

  • blend_formula::BlendFactor -> wgpu::BlendFactor
  • blend_formula::BlendOperation -> wgpu::BlendOperation
  • blend_formula::BlendComponent -> wgpu::BlendComponent
  • blend_formula::BlendState -> wgpu::BlendState

Macros§

blend_equation
Produces a BlendEquation equivalent to some formulae, if one exists.
blend_formula
Produces a BlendFormula equivalent to some formula, if one exists.

Structs§

BlendEquation
Corresponds to wgpu::BlendState.
BlendFormula
Corresponds to wgpu::BlendComponent.

Enums§

BlendFactor
Corresponds to wgpu::BlendFactor.
BlendOperation
Corresponds to wgpu::BlendOperation.