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

How to get a transparent Canvas? #521

Open
panxinmiao opened this issue Jun 11, 2024 · 2 comments
Open

How to get a transparent Canvas? #521

panxinmiao opened this issue Jun 11, 2024 · 2 comments

Comments

@panxinmiao
Copy link
Contributor

I want to render some scenes to the desktop using a transparent window. This requires setting the alpha_mode to premultiplied in the CanvasContext.config() method.

I have added a flag in the wgpu glfw backend code with a line like this:
glfw.window_hint(glfw.TRANSPARENT_FRAMEBUFFER, True).

However, the alpha_mode="premultiplied" setting in the CanvasContext.config() method still seems to be unavailable.

The capable_alpha_modes here are always ['opaque'].

capable_alpha_modes = []
for i in range(capabilities.alphaModeCount):
int_val = capabilities.alphaModes[i]
str_val = enum_int2str["CompositeAlphaMode"][int_val]
capable_alpha_modes.append(str_val.lower())

Am I missing something? How can we get a canvas that supports alpha_mode="premultiplied"?

@almarklein
Copy link
Member

Interesting. I just tried with this:

import glfw
import wgpu
from wgpu.gui.glfw import WgpuCanvas

glfw.init()
glfw.window_hint(glfw.TRANSPARENT_FRAMEBUFFER, True)
canvas = WgpuCanvas()
c = canvas.get_context()
c.configure(
    device=wgpu.utils.get_default_device(),
    format=wgpu.TextureFormat.bgra8unorm_srgb, 
    alpha_mode=wgpu.CanvasAlphaMode.premultiplied,
)

I get this error:

ValueError: Given format 'premultiplied' is not in supported formats ['opaque', 'unpremultiplied']

Interestingly, it does support unpremultiplied? which is not in the CanvasAlphaMode enum.

I found gfx-rs/wgpu#3486 which relates to this issue. Following some links to referred issues suggests that others have similar issue. E.g. Appears to be an issue with wgpu not correctly detecting surface capabilities. SurfaceCapabilities::alpha_modes only contains Opaque on my machine, Windows 10, nvidia gpu.

@panxinmiao
Copy link
Contributor Author

Ah, it seems that many people are encountering the same issue. This looks like it might be a problem with wgpu or something upstream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants