wgpu icon indicating copy to clipboard operation
wgpu copied to clipboard

Expose alpha_mode, support non-opaque mode on metal vk and gl backends

Open jinleili opened this issue 3 years ago • 2 comments
trafficstars

Checklist

  • [x] Run cargo clippy.
  • [X] Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if 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 set window_builder.with_transparent(true) ;
  • If use custom SurfaceView on 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_layer on 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-sys documentation states that GpuCanvasAlphaMode is supported, but it is not possible to configure this feature in .toml. nor to call the alpha_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 Dawn and it is hard coded swapChainDesc.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).

macOS android

jinleili avatar Jul 02 '22 10:07 jinleili

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

geertbleyen avatar Aug 10 '22 06:08 geertbleyen

No, just my highly split attention. I'll take a look soon. Thank you for reviewing!

cwfitzgerald avatar Aug 10 '22 15:08 cwfitzgerald

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

Hoverbear avatar Aug 24 '22 05:08 Hoverbear

@jinleili , do you intend to continue work on this PR?

geertbleyen avatar Aug 29 '22 06:08 geertbleyen

@jinleili , do you intend to continue work on this PR?

Yes, I still want to get it done.

jinleili avatar Aug 29 '22 09:08 jinleili

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?

jinleili avatar Aug 29 '22 09:08 jinleili

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

jinleili avatar Sep 03 '22 10:09 jinleili

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.

jinleili avatar Sep 04 '22 00:09 jinleili

@cwfitzgerald , these changes are now ok to merge?

geertbleyen avatar Sep 07 '22 07:09 geertbleyen

Great work @jinleili Looking forward this change in the next release👍

codeart1st avatar Sep 19 '22 04:09 codeart1st