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)]
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    Alpha,
18
19    /// Premultiplied alpha blending.
20    ///
21    /// Formula: `src.rgb + dst.rgb * (1 - src.a)`
22    ///
23    /// Use for: Blitting framebuffers with premultiplied alpha, compositing.
24    PremultipliedAlpha,
25
26    /// Additive blending - colors are added together.
27    ///
28    /// Formula: `src.rgb + dst.rgb`
29    ///
30    /// Use for: Glow effects, particles, light sources.
31    Additive,
32
33    /// Multiplicative blending.
34    ///
35    /// Formula: `src.rgb * dst.rgb`
36    ///
37    /// Use for: Shadows, color tinting.
38    Multiply,
39
40    /// Custom blend state for advanced use cases.
41    Custom(wgpu::BlendState),
42}
43
44impl BlendMode {
45    /// Convert to wgpu BlendState.
46    pub fn to_blend_state(self) -> Option<wgpu::BlendState> {
47        match self {
48            BlendMode::Replace => Some(wgpu::BlendState::REPLACE),
49            BlendMode::Alpha => Some(wgpu::BlendState::ALPHA_BLENDING),
50            BlendMode::PremultipliedAlpha => Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
51            BlendMode::Additive => Some(wgpu::BlendState {
52                color: wgpu::BlendComponent {
53                    src_factor: wgpu::BlendFactor::SrcAlpha,
54                    dst_factor: wgpu::BlendFactor::One,
55                    operation: wgpu::BlendOperation::Add,
56                },
57                alpha: wgpu::BlendComponent {
58                    src_factor: wgpu::BlendFactor::One,
59                    dst_factor: wgpu::BlendFactor::One,
60                    operation: wgpu::BlendOperation::Add,
61                },
62            }),
63            BlendMode::Multiply => Some(wgpu::BlendState {
64                color: wgpu::BlendComponent {
65                    src_factor: wgpu::BlendFactor::Dst,
66                    dst_factor: wgpu::BlendFactor::Zero,
67                    operation: wgpu::BlendOperation::Add,
68                },
69                alpha: wgpu::BlendComponent {
70                    src_factor: wgpu::BlendFactor::DstAlpha,
71                    dst_factor: wgpu::BlendFactor::Zero,
72                    operation: wgpu::BlendOperation::Add,
73                },
74            }),
75            BlendMode::Custom(state) => Some(state),
76        }
77    }
78
79    /// Create a color target state with this blend mode.
80    pub fn to_color_target_state(self, format: wgpu::TextureFormat) -> wgpu::ColorTargetState {
81        wgpu::ColorTargetState {
82            format,
83            blend: self.to_blend_state(),
84            write_mask: wgpu::ColorWrites::ALL,
85        }
86    }
87}
88
89impl Default for BlendMode {
90    fn default() -> Self {
91        BlendMode::Alpha
92    }
93}
94
95impl From<BlendMode> for Option<wgpu::BlendState> {
96    fn from(mode: BlendMode) -> Self {
97        mode.to_blend_state()
98    }
99}
100
101impl From<wgpu::BlendState> for BlendMode {
102    fn from(state: wgpu::BlendState) -> Self {
103        BlendMode::Custom(state)
104    }
105}