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

WebGPU support detection doesn't handle failure at getContext() #5332

Open
kpreid opened this issue Mar 2, 2024 · 8 comments
Open

WebGPU support detection doesn't handle failure at getContext() #5332

kpreid opened this issue Mar 2, 2024 · 8 comments
Labels
api: webgpu Issues with direct interface with WebGPU type: bug Something isn't working

Comments

@kpreid
Copy link
Contributor

kpreid commented Mar 2, 2024

Description
Enabling the "webgpu" backend can cause WebGL support to be lost.

Repro steps

  1. Use Firefox 123.0 on macOS.
  2. Set dom.webgpu.enabled to true.
  3. Visit https://wgpu.rs/examples/?backend=webgl2&example=bunnymark

Expected vs observed behavior
Expected: The demo should display, using either webgpu or webgl2.
Observed: it fails to initialize:
CreateSurfaceError { inner: Web("canvas.getContext() returned null; webgpu not available or canvas already in use") }

WebGPU detection is presumably concluding WebGPU is supported because navigator.gpu exists, but that is evidently not sufficient.

(Note that it is also possible for a canvas to return null for the context simply because it was previously used as "webgl2" rather than "webgpu". That would be nice to support too, but since it would require a simultaneous WebGPU-or-WebGL2 backend, understandable to skip.)

Platform

  • Firefox 123.0
  • macOS
  • wgpu 0.19.3
kpreid added a commit to kpreid/all-is-cubes that referenced this issue Mar 3, 2024
I would like to also enable WebGPU support, as that no longer needs
special unstable flags, but doing so breaks WebGL on Firefox:
<gfx-rs/wgpu#5332>
@Wumpf
Copy link
Member

Wumpf commented Mar 3, 2024

As I understand navigator.gpu existence is supposed to signify webgpu support, but I ran into the same issue and had to do an elaborate workaround.
The whole situation is surprisingly hard to solve with the way things are set up right now: In order to check whether the Instance should be a WebGPU instance, we have to first instantiate the adapter and if that fails turn around and create a different instance. So fixing this likely needs us to step back a bit and rethink how all of this works.

@Wumpf Wumpf added type: bug Something isn't working api: webgpu Issues with direct interface with WebGPU labels Mar 3, 2024
@Wumpf
Copy link
Member

Wumpf commented Mar 3, 2024

@kpreid
Copy link
Contributor Author

kpreid commented Mar 4, 2024

In order to check whether the Instance should be a WebGPU instance, we have to first instantiate the adapter and if that fails turn around and create a different instance. So fixing this likely needs us to step back a bit and rethink how all of this works.

Oh, this is actually a feasible workaround in applications which create the instance and adapter together, since it can be done purely using the wgpu API! I'll have to try that. Would be nice to have it mentioned in wgpu's docs until this bug can be fixed (or Firefox can be ?more conformant?).

@kpreid
Copy link
Contributor Author

kpreid commented Mar 9, 2024

I tried implementing the workaround: creating an instance, surface, and adapter first with Backends::BROWSER_WEBGPU, and then wgpu::Backends::GL if that fails. But, I got a panic when WebGPU isn't supported/enabled in the browser at all:

panicked at /Users/kpreid/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.19.3/src/instance.rs:521:39:
called Option::unwrap() on a None value

This appears to be the bug already fixed by #5166. But it can be avoided by using Backends::all() in the first attempt, and then initialization succeeds in all cases I've tested.

@dv29
Copy link
Contributor

dv29 commented Jul 19, 2024

I'm getting this as well. Tried running it locally and https://wgpu.rs/examples/?backend=webgpu&example=bunnymark both give the same error. I'm going to investigate it unless a fix is already in the works

panicked at examples/src/framework.rs:148:65:
called `Result::unwrap()` on an `Err` value: CreateSurfaceError { inner: Web("canvas.getContext() returned null; webgpu not available or canvas already in use") }

@cwfitzgerald
Copy link
Member

There is not, go ahead :)

@dv29
Copy link
Contributor

dv29 commented Jul 19, 2024

So it seems that firefox only supports WebGPU API on Nightly, I tried it with nightly and its works.
I'm not sure why dom.webgpu.enabled did not work though, i'm guessing that it only affects on Nightly builds.
I'm going to try to fall back on WebGL as you suggested on #5142 (btw this is my first time with this project)

@Wumpf
Copy link
Member

Wumpf commented Jul 21, 2024

I for one observed that issue on Linux Chrome where they (afaik as of writing) still don't support WebGPU. wgpu incorrectly picks webgpu over webgl there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: webgpu Issues with direct interface with WebGPU type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants