Skip to main content

astrelis_render/
blend.rs

1//! Blend mode presets for common rendering scenarios.
2
3/// Predefined blend modes for common use cases.
4///
5/// Use these to configure how source and destination colors are combined
6/// during rendering.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
8pub enum BlendMode {
9    /// No blending - source completely replaces destination.
10    Replace,
11
12    /// Standard alpha blending for transparent content.
13    ///
14    /// Formula: `src.rgb * src.a + dst.rgb * (1 - src.a)`
15    ///
16    /// Use for: Transparent UI over game content, sprites with transparency.
17    #[default]
18    Alpha,
19
20    /// Premultiplied alpha blending.
21    ///
22    /// Formula: `src.rgb + dst.rgb * (1 - src.a)`
23    ///
24    /// Use for: Blitting framebuffers with premultiplied alpha, compositing.
25    PremultipliedAlpha,
26
27    /// Additive blending - colors are added together.
28    ///
29    /// Formula: `src.rgb + dst.rgb`
30    ///
31    /// Use for: Glow effects, particles, light sources.
32    Additive,
33
34    /// Multiplicative blending.
35    ///
36    /// Formula: `src.rgb * dst.rgb`
37    ///
38    /// Use for: Shadows, color tinting.
39    Multiply,
40
41    /// Custom blend state for advanced use cases.
42    Custom(wgpu::BlendState),
43}
44
45impl BlendMode {
46    /// Convert to wgpu BlendState.
47    pub fn to_blend_state(self) -> Option<wgpu::BlendState> {
48        match self {
49            BlendMode::Replace => Some(wgpu::BlendState::REPLACE),
50            BlendMode::Alpha => Some(wgpu::BlendState::ALPHA_BLENDING),
51            BlendMode::PremultipliedAlpha => Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
52            BlendMode::Additive => Some(wgpu::BlendState {
53                color: wgpu::BlendComponent {
54                    src_factor: wgpu::BlendFactor::SrcAlpha,
55                    dst_factor: wgpu::BlendFactor::One,
56                    operation: wgpu::BlendOperation::Add,
57                },
58                alpha: wgpu::BlendComponent {
59                    src_factor: wgpu::BlendFactor::One,
60                    dst_factor: wgpu::BlendFactor::One,
61                    operation: wgpu::BlendOperation::Add,
62                },
63            }),
64            BlendMode::Multiply => Some(wgpu::BlendState {
65                color: wgpu::BlendComponent {
66                    src_factor: wgpu::BlendFactor::Dst,
67                    dst_factor: wgpu::BlendFactor::Zero,
68                    operation: wgpu::BlendOperation::Add,
69                },
70                alpha: wgpu::BlendComponent {
71                    src_factor: wgpu::BlendFactor::DstAlpha,
72                    dst_factor: wgpu::BlendFactor::Zero,
73                    operation: wgpu::BlendOperation::Add,
74                },
75            }),
76            BlendMode::Custom(state) => Some(state),
77        }
78    }
79
80    /// Create a color target state with this blend mode.
81    pub fn to_color_target_state(self, format: wgpu::TextureFormat) -> wgpu::ColorTargetState {
82        wgpu::ColorTargetState {
83            format,
84            blend: self.to_blend_state(),
85            write_mask: wgpu::ColorWrites::ALL,
86        }
87    }
88}
89
90impl From<BlendMode> for Option<wgpu::BlendState> {
91    fn from(mode: BlendMode) -> Self {
92        mode.to_blend_state()
93    }
94}
95
96impl From<wgpu::BlendState> for BlendMode {
97    fn from(state: wgpu::BlendState) -> Self {
98        BlendMode::Custom(state)
99    }
100}