learn-wgpu icon indicating copy to clipboard operation
learn-wgpu copied to clipboard

Alpha mode

Open Jammyjamjamman opened this issue 10 months ago • 2 comments

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.)

Jammyjamjamman avatar Oct 02 '23 23:10 Jammyjamjamman

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

sotrh avatar Oct 11 '23 19:10 sotrh

Thanks. I'll try to write up something about it. (I'm still going through your great tutorial to improve my understanding :). )

Jammyjamjamman avatar Oct 17 '23 23:10 Jammyjamjamman

Seems like this is fixed so closing this for now

sotrh avatar May 18 '24 16:05 sotrh