Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alpha blending for wgpu msaa #1367

Merged
merged 1 commit into from
Jun 17, 2022

Conversation

tarkah
Copy link
Member

@tarkah tarkah commented Jun 16, 2022

Alpha is not blended correctly when using MSAA on wgpu. When the fragments are stored to the multisampled color attachment, the color components are multiplied by alpha. When they are sampled and then stored to the non-multisampled resolve attachment, they are getting multiplied by alpha again. Using PREMULTIPLIED_ALPHA_BLENDING for the resolve attachment fixes this since it uses a src_factor of One for color components.

This is evident when adding a background color to the solar_system example and removing the frame.fill call to cover the canvas in a background color. The orbit, which has a small alpha value to begin with, becomes nearly fully transparent due to the double alpha blending. This fixes it so the orbit looks identical in alpha to the example without anti-aliasing enabled.

blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
}),
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ALPHA_BLENDING is equal to this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I see they added new useful variants 👌

@hecrj hecrj added bug Something isn't working rendering wgpu labels Jun 17, 2022
@hecrj hecrj added this to the 0.4.3 milestone Jun 17, 2022
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you and good catch!

I'm not sure if this is something they changed in wgpu, I remember changing these to match the glow backend.

It's great to see the fix consists in removing a bunch of code! 🥳

blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
}),
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I see they added new useful variants 👌

@hecrj hecrj merged commit 9b46176 into iced-rs:master Jun 17, 2022
@tarkah tarkah deleted the fix/wgpu-msaa-alpha-blending branch June 17, 2022 14:12
@hecrj hecrj modified the milestones: 0.4.3, 0.5.0 Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rendering wgpu
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants