learn-wgpu
learn-wgpu copied to clipboard
Alpha mode
In your tutorial here, you wrote this:
alpha_mode is honestly not something I'm familiar with. I believe it has something to do with transparent windows, but feel free to open a pull request. For now we'll just use the first AlphaMode in the list given by surface_caps.
I think I've figured out what those alpha settings are about. The modes are:
#[repr(C)]
pub enum CompositeAlphaMode {
Auto,
Opaque,
PreMultiplied,
PostMultiplied,
Inherit,
}
"Auto" is probably the mode one wants to stick with (if available). The interesting settings are PreMultiplied and PostMultiplied.
There's a good article here: https://developer.nvidia.com/content/alpha-blending-pre-or-not-pre
Out of the "interesting" settings, premultiplied is the option you usually want, if available. When you downsample a texture with transparent pixels, you get incorrect results with "postmultiply". The reason is because downsampling "averages" pixels. But if a pixel has a smaller alpha channel, you want its non-alpha channels to contribute less to the average colour. Premultiply addresses this by "pre-multiplying" the non-alpha channels of pixels by their alpha channel before averaging pixels / downsampling. Postmultiply does not.
If you don't downsample textures, the behaviour of premultiply and postmultiply is exactly the same.
(Not sure if any of this info is of interest. I could try and make it into a PR for your tutorial or feel free to copy-paste it.)
Looking at the spec it seems to be related to outputting the final image to the surface (HTMLCanvas in particular). Feel free to create a PR
Thanks. I'll try to write up something about it. (I'm still going through your great tutorial to improve my understanding :). )
Seems like this is fixed so closing this for now