Skip to content

Commit

Permalink
Support for --target wasm32-unknown-emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Dec 7, 2021
1 parent d51f70e commit 8eb6fa8
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 86 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ jobs:
tool: clippy
kind: web

# Emscripten WebGL
- name: WebAssembly emscripten
os: ubuntu-20.04
channel: stable
target: wasm32-unknown-emscripten
tool: clippy
kind: webgl-em

name: Check ${{ matrix.name }}
runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -164,6 +172,15 @@ jobs:
# build docs
cargo doc --target ${{ matrix.target }} -p wgpu --no-deps
- name: check web-em
if: matrix.kind == 'webgl-em'
run: |
# check with no features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-core
# build docs
cargo doc --target ${{ matrix.target }} -p wgpu -p wgpu-core --no-deps
- name: check native
if: matrix.kind == 'local' || matrix.kind == 'other'
run: |
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions run-wasm-emscripten-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -e

set -- hello-triangle
echo "Compiling..."
EMMAKEN_CFLAGS="-g -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry -s FULL_ES3=1" cargo build --example $1 --target wasm32-unknown-emscripten

mkdir -p target/wasm-examples/$1/
cp target/wasm32-unknown-emscripten/debug/examples/* target/wasm-examples/$1
cat wasm-resources/index.em.template.html | sed "s/{{example}}/$1/g" > target/wasm-examples/$1/index.html

# Find a serving tool to host the example
SERVE_CMD=""
SERVE_ARGS=""
if which basic-http-server; then
SERVE_CMD="basic-http-server"
SERVE_ARGS="target/wasm-examples/$1 -a 127.0.0.1:1234"
elif which miniserve && python3 -m http.server --help > /dev/null; then
SERVE_CMD="miniserve"
SERVE_ARGS="target/wasm-examples/$1 -p 1234 --index index.html"
elif python3 -m http.server --help > /dev/null; then
SERVE_CMD="python3"
SERVE_ARGS="-m http.server --directory target/wasm-examples/$1 1234"
fi

# Exit if we couldn't find a tool to serve the example with
if [ "$SERVE_CMD" = "" ]; then
echo "Couldn't find a utility to use to serve the example web page. You can serve the `target/wasm-examples/$1` folder yourself using any simple static http file server."
fi

echo "Serving example with $SERVE_CMD at http://localhost:1234"
$SERVE_CMD $SERVE_ARGS
15 changes: 15 additions & 0 deletions wasm-resources/index.em.template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<canvas id="canvas" width="640" height="400"></canvas>
<script>
var Module = {
canvas: document.getElementById("canvas"),
};
</script>
<script src="{{example}}.js"></script>
</body>
</html>
10 changes: 8 additions & 2 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gpu-descriptor = { version = "0.2", optional = true }
inplace_it = { version ="0.3.3", optional = true }

# backend: Gles
glow = { version = "0.11", optional = true }
glow = { git = "https://github.com/grovesNL/glow", rev = "b6eb0ba", optional = true }

# backend: Dx12
bit-set = { version = "0.5", optional = true }
Expand All @@ -59,6 +59,10 @@ egl = { package = "khronos-egl", version = "4.1", features = ["dynamic"], option
#Note: it's only unused on Apple platforms
libloading = { version = "0.7", optional = true }

[target.'cfg(target_os = "emscripten")'.dependencies]
egl = { package = "khronos-egl", version = "4.1", features = ["static", "no-pkg-config"], optional = true }
libloading = { version = "0.7", optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["libloaderapi", "windef", "winuser"] }
native = { package = "d3d12", version = "0.4.1", features = ["libloading"], optional = true }
Expand All @@ -68,7 +72,7 @@ mtl = { package = "metal", version = "0.23.1" }
objc = "0.2.5"
core-graphics-types = "0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["Window", "HtmlCanvasElement", "WebGl2RenderingContext"] }
js-sys = { version = "0.3" }
Expand All @@ -86,4 +90,6 @@ features = ["wgsl-in"]

[dev-dependencies]
env_logger = "0.8"

[target.'cfg(not(target_os = "emscripten"))'.dev-dependencies]
winit = "0.26"
8 changes: 8 additions & 0 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ impl crate::Device<super::Api> for super::Device {
#[cfg_attr(target_arch = "wasm32", allow(clippy::needless_borrow))]
Ok(fence.get_latest(&self.shared.context.lock()))
}

#[cfg_attr(target_os = "emscripten", allow(unreachable_code, unused_variables))]
unsafe fn wait(
&self,
fence: &super::Fence,
Expand All @@ -1114,6 +1116,12 @@ impl crate::Device<super::Api> for super::Device {
.iter()
.find(|&&(value, _)| value >= wait_value)
.unwrap();

// for some reason signature of glow's client_wait_sync didn't match to emscripten's client_wait_sync
// however it should be noop in emscripten
#[cfg(target_os = "emscripten")]
return Ok(true);

match gl.client_wait_sync(sync, glow::SYNC_FLUSH_COMMANDS_BIT, timeout_ns as i32) {
// for some reason firefox returns WAIT_FAILED, to investigate
#[cfg(target_arch = "wasm32")]
Expand Down
Loading

0 comments on commit 8eb6fa8

Please sign in to comment.