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