bevyengine / bevyengine/bevy

Support WebGL2 and WebGPU in the same WASM file

Open
#13,168 9 comments 2 reactions 0 assignees View on GitHub
A-Rendering C-Feature C-Usability D-Complex M-Release-Note O-Web S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

Bevy currently [forces you to choose](https://github.com/bevyengine/bevy/tree/96b9d0a7e203bc55aff4f8b14ac8a51a6945fea5/examples#webgl2-and-webgpu) either WebGL2 or WebGPU when building a project. This is one extra thing that developers need to think about when deploying their games, which is already a complicated enough process.

Since the web is a large, incredibly portable platform, we should attempt to make targeting it as painless as possible.

## What solution would you like?

In https://github.com/gfx-rs/wgpu/pull/5044, WGPU added support for detecting WebGL2 and WebGPU at runtime. I think integrating this into Bevy would be incredibly useful, and give WebGPU more exposure. This would be achieved through the existing `webgl2` and `webgpu` features.

|`webgl2` enabled?|`webgpu` enabled?|Behavior|
|-|-|-|
|✅|✅|Detect API at runtime, preferring WebGPU and falling back to WebGL2 if not available|
|✅|❌|Use WebGL2, panicking if not available at runtime|
|❌|✅|Use WebGPU, panicking if not available at runtime|
|❌|❌|Fail to compile for WASM|

## Questions

- What crate(s) will need to implement this functionality? Is there a way we can abstract over both APIs to reduce the amount of logic necessary?
- Does WebGPU still need `--cfg=web_sys_unstable_apis`, and do we handle this already?
- How difficult will this change be? Is it something we can plan on finishing by Bevy 0.14?

## What alternative(s) have you considered?

- Instead of panicking, using the `error!(...)` macro may be preferred because it will be visible in the Javascript console.
- Do we have a panic handler for WASM that logs panics? If not, then `error!` may be preferred.

## Additional context

- [Discord message that inspired this feature request](https://discord.com/channels/691052431525675048/1233486877307179188/1234656132824436748)
- https://github.com/bevyengine/bevy/pull/11280#issuecomment-1894914591

cc @Elabajaba, who originally migrated Bevy to the WGPU version that introduced this feature.

Contributor guide

Open the contributing guide

Research direction

Start by tracing Bevy's existing `webgl2` and `webgpu` feature definitions and the WASM renderer initialization path, then compare them with the runtime detection support in gfx-rs/wgpu PR 5044. Done means combined-feature WASM builds prefer WebGPU and fall back to WebGL2, while single-feature builds retain their specified behavior and unsupported combinations fail as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
computer-graphics, game-dev, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.