bevy_hanabi icon indicating copy to clipboard operation
bevy_hanabi copied to clipboard

WASM compatibility

Open ZettaScript opened this issue 3 years ago • 38 comments

Using bevy_hanabi fails in WASM because VERTEX_WRITABLE_STORAGE is not supported (even if WGPU doc says it is supported by all platforms).

Is it planned to make this crate WASM-compatible?

ZettaScript avatar Aug 16 '22 13:08 ZettaScript

No, bevy_hanabi uses compute shaders which are not currently supported on Web.

djeedai avatar Aug 16 '22 13:08 djeedai

The docs says:

This is a native-only feature.

djeedai avatar Aug 16 '22 17:08 djeedai

I'm going to close this issue. I fully appreciate the value of wasm, and I'm kind of sad that this crate doesn't work with it, but it would be such a large amount of work not only to create a separate backend dedicated to wasm that doesn't use compute shaders, but also to maintain two backends in parallel, that I realistically don't have the time to do this.

I hear some Bevy users are working on a CPU particle solution, which would work with wasm (see bevyengine/rfcs#28) so I encourage anyone who needs a particle system that works in wasm to contribute to that RFC and workstream.

If you're familiar with Unity, and to hazard an over-flattering comparison that is probably not realistic (yet?), 🎆 Bevy Hanabi is to Unity's VFX Graph what that CPU solution and RFC for Bevy is to Unity's built-in particle system, with the same advantages and drawbacks. CPU solutions are more versatile, easier to support on all platforms, but limited in terms of performance; on the other hand GPU solutions are vastly more performant but require modern API and hardware features (compute shaders) which are not available everywhere.

djeedai avatar Sep 11 '22 13:09 djeedai

too, bad, but I totally undestand your decision. I guess we can only hope that webgpu will solve this in the future...

maybe add a small note in the readme that wasm is not supported?

johanhelsing avatar Nov 12 '22 11:11 johanhelsing

maybe add a small note in the readme that wasm is not supported?

I thought I already had but apparently not. Fixed now, it's in the README.

djeedai avatar Nov 14 '22 23:11 djeedai

AFAIK compute shaders are now supported by WebGPU. @djeedai would you consider reopening this issue?

janhohenheim avatar Feb 22 '23 01:02 janhohenheim

@janhohenheim I would be more than happy to add wasm support to 🎆 Hanabi provided Bevy itself supports compute shaders on wasm. I don't believe this is the case yet though? And I can really spearhead that myself.

djeedai avatar Feb 23 '23 22:02 djeedai

@djeedai hmm, I tried running the compute_shader_game_of_life example on wasm on Bevy's main branch and forced the wgpu backend to WebGPU by calling the following on the DefaultPlugin setup:

.set(RenderPlugin {
    wgpu_settings: WgpuSettings {
        backends: Some(Backends::BROWSER_WEBGPU),
        ..default()
    },
})

However, I cannot test this further, since I fail to activate WebGPU on my browsers; I cannot render any WebGPU examples on the internet right now. Maybe someone else has better luck? The steps needed to enable WebGPU are described here.

I double checked, and yes, WebGPU does indeed support compute shaders.

janhohenheim avatar Feb 23 '23 23:02 janhohenheim

I couldn't get webgpu to work at all in firefox, but on chrome, this example worked: https://cx20.github.io/webgpu-test/examples/rust/triangle/index.html

However, when I tried patching wgpu compute_shader_game_of_life like you suggested, @janhohenheim , I get a panic:

panicked at 'Unable to find a GPU! Make sure you have installed required drivers!', crates\bevy_render\src\renderer\mod.rs:125:10

This was on windows 11 with nvidia gtx 970. May give it another try on linux.

johanhelsing avatar Feb 24 '23 16:02 johanhelsing

@johanhelsing ah heck! I checked Discord and found this: image image I deduce from this that you might get webgpu running if you enable the respective feature on wgpu. But it seems this is not yet part of any public facing Bevy API.

janhohenheim avatar Feb 24 '23 16:02 janhohenheim

So yes, that's my understanding, and happy to reopen this once Bevy supports it but until then no luck in afraid.

djeedai avatar Feb 24 '23 17:02 djeedai

Seems like there is official support for WebGPU in bevy's main branch, should this issue be reopened?

Dimchikkk avatar May 11 '23 19:05 Dimchikkk

I ported bevy_hanabi to main, and with a few fixes for WebGPU it works in the browser

https://github.com/djeedai/bevy_hanabi/assets/8672791/fa5979ca-3008-42c0-9ae9-00752645bd9d

mockersf avatar May 19 '23 08:05 mockersf

@mockersf great, do you have a branch to try? :)

Dimchikkk avatar May 19 '23 08:05 Dimchikkk

just pushed it here: https://github.com/mockersf/bevy_hanabi/tree/with-webgpu, started from https://github.com/djeedai/bevy_hanabi/pull/179 for some of the Bevy update needed

Building for WebGPU: RUSTFLAGS=--cfg=web_sys_unstable_apis cargo build --target wasm32-unknown-unknown --example firework --features="bevy/bevy_winit bevy/bevy_pbr bevy/png 3d"

mockersf avatar May 19 '23 10:05 mockersf

Reopening the issue now that Bevy has official support.

djeedai avatar May 19 '23 17:05 djeedai

I did some initial work and I can build and run all examples. However there's a major bug where only the first effect is visible, and I'm having difficulties debugging on Web (notably, at the minute logs don't show up in console like they should, and I don't know of any way to make GPU captures). So this likely won't make it for the next release unfortunately (next few days, waiting on bevy-inspector-egui to upgrade to Bevy 0.11).

djeedai avatar Jul 10 '23 21:07 djeedai

@djeedai could you create a draft PR with that you have? So me and others can maybe help with debugging

adamtomecek avatar Aug 23 '23 15:08 adamtomecek

@adamtomecek See the u/wasm branch, I think it has my latest try.

djeedai avatar Aug 23 '23 15:08 djeedai

Unfortunately no luck running it. It panicks on me with Current version, u/wasm branch and mockersf WASM support branch.

Caused by:
    In Device::create_bind_group_layout
      note: label = `hanabi:bind_group_layout:dispatch_indirect_dispatch_indirect`
    Too many bindings of type StorageBuffers in Stage ShaderStages(COMPUTE), limit is 0, count was 3

adamtomecek avatar Aug 24 '23 14:08 adamtomecek

Looks like a bug on Hanabi side though, unless that 0 limit somehow means something is not supported on wasm? Not sure...

djeedai avatar Aug 24 '23 16:08 djeedai

Are you building for WebGL2 or WebGPU?

mockersf avatar Aug 24 '23 16:08 mockersf

Good question. Not sure. I'm pretty new to Rust/Bevy but I think it's safe to say I'm using default values?

RUSTFLAGS=--cfg=web_sys_unstable_apis cargo run --target wasm32-unknown-unknown this is a command I'm using to run it. On M2 Mac

adamtomecek avatar Aug 24 '23 19:08 adamtomecek

I take it back. I finally figured out how to build Bevy with WebGPU and it works spot on with your u/wasm branch.

adamtomecek avatar Aug 31 '23 19:08 adamtomecek

Can you run all examples? At the time I made the branch I could only run examples with a single effect. As soon as there were more, only the first rendered.

djeedai avatar Aug 31 '23 19:08 djeedai

...works spot on with my game, but I use multiple effects. I create all effects while loading the game, save Handles and run effect when needed.

I'll give examples here a try and let you know

adamtomecek avatar Aug 31 '23 19:08 adamtomecek

I tested on latest, there's no change. See for example the spawn example, it's missing the second and third effects. I have no idea how to debug the GPU on WASM in the browser, I don't think there's any tool like RenderDoc. So it might take a while to figure out what the browser is not liking.

image

djeedai avatar Sep 02 '23 07:09 djeedai

@djeedai Screenshot 2023-09-04 at 16 21 55

I see all three effects. I couldn't compile examples from your u/wasm branch, I use my custom rebased with my Cargo.toml. But I think removing WorldInspectorPlugin did the trick. With the plugin, I wasn't able to see any effects.

I'll make a working example and send you a link.

adamtomecek avatar Sep 04 '23 14:09 adamtomecek

Yes I had to remove the inspector too, but I still don't see the other effects beyond the first one.

I'm using the latest Chrome 116 to run the examples on Windows. That looks more and more like a platform bug in Bevy or Wgpu.

djeedai avatar Sep 04 '23 16:09 djeedai

@djeedai code that's working for me. Sadly can't see any major changes compared to your version. Running it like this RUSTFLAGS=--cfg=web_sys_unstable_apis cargo run --example spawn --features="bevy/bevy_winit bevy/bevy_pbr 3d" --target wasm32-unknown-unknown with added Cargo config file for a WASM runner.

adamtomecek avatar Sep 04 '23 18:09 adamtomecek