wgpu
wgpu copied to clipboard
Expose alpha_mode, support non-opaque mode on metal vk and gl backends
Checklist
- [x] Run
cargo clippy. - [X] Run
RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknownif applicable. - [X] Add change to CHANGELOG.md. See simple instructions inside file.
Connections https://github.com/gfx-rs/wgpu/issues/687
Description
To get a transparent background Canvas, there are different cases on the user side:
- If use
winit, have to setwindow_builder.with_transparent(true); - If use custom
SurfaceViewon Android, the only way to set SurfaceView background color to transparent is:
val sv = WGPUSurfaceView(ctx)
sv.setZOrderOnTop(true)
sv.holder.setFormat(PixelFormat.TRANSPARENT)
- If call
create_surface_from_core_animation_layeron metal to create surface, have to ensure that the background color of the layer is nil or clearColor
Known issues:
- wasm target cannot update alpha_mode,
web-sysdocumentation states thatGpuCanvasAlphaModeis supported, but it is not possible to configure this feature in.toml. nor to call thealpha_mode() - dx12 setting non-opaque alpha_mode on Windows 11 causes crash(running on Parallels Desktop), haven't found a solution yet, checked out the source code for
Dawnand it is hard codedswapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE: https://dawn.googlesource.com/dawn/+/24239fcc4786bc231eacc065eaa49fd484546984/src/dawn/native/d3d12/SwapChainD3D12.cpp#243
Testing Tested on macOS(metal, vulkan-portability, angle), iOS, Nexus 5x(Android 8.1, vk and gl backends).
![]() |
![]() |
|---|
@cwfitzgerald, is something preventing this PR from being completed (besides the merge conflicts)? This particular change has been under discussion for quite a while now in #687 We've been doing private patches for this for a while now, and having the alpha mode exposed means we can stop using a fork.
No, just my highly split attention. I'll take a look soon. Thank you for reviewing!
I had the opportunity to test this patch atop v0.13.1 in Bevy as part of exploring the cause of https://github.com/bevyengine/bevy/issues/5779. It seemed functional and working in x86_64 Linux on a Vulkan backend with a AMD RADV NAVI10 adapter running inside Wayland (Gnome).
@jinleili , do you intend to continue work on this PR?
@jinleili , do you intend to continue work on this PR?
Yes, I still want to get it done.
Code generally looks good - one comment. We should also add a function to query what modes are actually available and enforce that the user only uses an available mode (similar to how we do present modes now).
Is it acceptable to simply match the WebGPU Spec and provide only normal Opaque and PreMultiplied options?
@cwfitzgerald Added surface_get_supported_alpha_modes fn, extract the same code from surface_get_supported_xxx into fetch_adapter_and_surface fn, also renamed get_supported_modes to get_supported_present_modes.
Since alpha_mode is now strictly validated, Opaque is no longer guaranteed to be supported, because the vk backend on Android devices tends to only support Inherit.
So, use get_supported_alpha_modes to set the alpha_mode to keep examples portability.
@cwfitzgerald , these changes are now ok to merge?
Great work @jinleili Looking forward this change in the next release👍

