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

feature: emscripten #2422

Merged
merged 1 commit into from
Jan 27, 2022
Merged

feature: emscripten #2422

merged 1 commit into from
Jan 27, 2022

Conversation

caiiiycuk
Copy link
Contributor

This is reimplementation of #2233

--

Added new feature "emscripten", this feature can be used in combination with --target wasm32-unknown-emscripten to build wgpu for emscripten.

raw-gles.rs example now works also with emscripten, to run it:

  1. install emsdk
  2. build this example with cargo:
EMMAKEN_CFLAGS="-g -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry -s FULL_ES3=1" cargo build --example raw-gles --target wasm32-unknown-emscripten --features gles,emscripten,webgl
  1. copy raw-gles.em.html into target/wasm32-unknown-emscripten/debug/examples/ and open it in browser

This PR also adds a workflow to check emscripten build. I think that raw-gles example is only needed example for emscripten integration it also can be a test, but it hard to do it automatically because need to install emcc. I think not worth to doing it.

Typical usage of wgpu and emscripten will be through external gles integration like in raw-gles.rs. Because windows management (like winit) crate not exists for emscripten, so it will be used with some native library like SDL/2+wgpu. SDL will initialize context and controls window/events and wgpu will be used as renderer library.

--

There is 2 identical comments in 2233 that I can't solve:

        #[cfg(feature = "emscripten")]
        let wsi_library = None;

        #[cfg(feature = "emscripten")]
        let wsi_kind = WindowKind::Unknown;

        #[cfg(not(feature = "emscripten"))]
        let (display, wsi_library, wsi_kind) = if let (Some(library), Some(egl)) =
            (wayland_library, egl.upcast::<egl::EGL1_5>())
        {
            log::info!("Using Wayland platform");
...
        } else {
            log::info!("Using default platform");
            let display = egl.get_display(egl::DEFAULT_DISPLAY).unwrap();
            (display, None, WindowKind::Unknown)
        };

        #[cfg(feature = "emscripten")]
        let display = egl.get_display(egl::DEFAULT_DISPLAY).unwrap();

Ideally we can remove this #[cfg because last else is what we actually need. But I can't find good way to jump into last else branch when feature emscripten is set. And egl::EGL1_5 is not defined when feature is emscripten.

raw-gles.rs in browser (tested in Chrome an FF):
изображение

@caiiiycuk caiiiycuk force-pushed the feature-emscripten branch 3 times, most recently from 286f4ac to 1beff75 Compare January 25, 2022 11:23
.github/workflows/ci.yml Outdated Show resolved Hide resolved
wgpu-hal/Cargo.toml Outdated Show resolved Hide resolved
wgpu-hal/examples/raw-gles.rs Show resolved Hide resolved
wgpu-hal/Cargo.toml Show resolved Hide resolved
wgpu-hal/src/gles/egl.rs Outdated Show resolved Hide resolved
@caiiiycuk caiiiycuk marked this pull request as draft January 26, 2022 08:36
@caiiiycuk
Copy link
Contributor Author

Mark it as draft until fully test it on vaners + librusty_vangers

@caiiiycuk caiiiycuk marked this pull request as ready for review January 27, 2022 04:01
@caiiiycuk
Copy link
Contributor Author

Tested, works fine

@kvark kvark merged commit b190003 into gfx-rs:master Jan 27, 2022
@kvark
Copy link
Member

kvark commented Jan 27, 2022

We'll probably need a separate section in README about all the ways this can be compiled for the Web

@caiiiycuk
Copy link
Contributor Author

caiiiycuk commented Jan 30, 2022

@kvark Regarding to emscripten, I think it's will be very handy for projects that use wgpu-native. Since this projects are on C++ and use Rust to render, they can't be ported to web until this PR. Because you can't merge 2 Modules into one (from emscripten, and from wasm32-unknown-unknown). But now, it's super simple: compile both with emscripten and link wgpu-native statically or dynamically and it will work.

So for emscripten, I think we can try to compile examples from wgpu-native. Or I think it's better we can create simple SDL1/2+wgpu-native project and write tutorial how to compile it. Because I think SDL1/2 is widely used and have first class support from emscripten.

@caiiiycuk
Copy link
Contributor Author

caiiiycuk commented Jan 30, 2022

Also I can do announcement in emscripten channels about this template (SDL+wgpu)

@kvark
Copy link
Member

kvark commented Jan 30, 2022

Yes, I'm totally with you on this. C-based projects would be able to target WebGL via webgpu-headers, it's a very cool feature. The example code should live in https://github.com/gfx-rs/wgpu-native/ for this.

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

Successfully merging this pull request may close these issues.

2 participants