1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum BlendMode {
9 Replace,
11
12 Alpha,
18
19 PremultipliedAlpha,
25
26 Additive,
32
33 Multiply,
39
40 Custom(wgpu::BlendState),
42}
43
44impl BlendMode {
45 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 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}