Skip to content

Commit

Permalink
bash -> python for parallel raytracing example
Browse files Browse the repository at this point in the history
  • Loading branch information
jthemphill committed Sep 13, 2023
1 parent 51e89eb commit e148618
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
4 changes: 1 addition & 3 deletions examples/raytrace-parallel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ online][compiled]
You can build the example locally with:

```
$ ./run.sh
$ python3 run.py
```

(or running the commands on Windows manually)

and then visiting http://localhost:8080 in a browser should run the example!
57 changes: 57 additions & 0 deletions examples/raytrace-parallel/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
import os
import subprocess

root_dir = os.path.dirname(__file__)

# A couple of steps are necessary to get this build working which makes it slightly
# nonstandard compared to most other builds.
#
# * First, the Rust standard library needs to be recompiled with atomics
# enabled. to do that we use Cargo's unstable `-Zbuild-std` feature.
#
# * Next we need to compile everything with the `atomics` and `bulk-memory`
# features enabled, ensuring that LLVM will generate atomic instructions,
# shared memory, passive segments, etc.

os.environ.update(
{"RUSTFLAGS": "-C target-feature=+atomics,+bulk-memory,+mutable-globals"}
)

subprocess.run(
[
"cargo",
"build",
"--target",
"wasm32-unknown-unknown",
"--release",
"-Zbuild-std=std,panic_abort",
],
cwd=root_dir,
).check_returncode()

# Note the usage of `--target no-modules` here which is required for passing
# the memory import to each wasm module.
subprocess.run(
[
"cargo",
"run",
"-p",
"wasm-bindgen-cli",
"--",
os.path.join(
root_dir,
"..",
"..",
"target",
"wasm32-unknown-unknown",
"release",
"raytrace_parallel.wasm",
),
"--out-dir",
root_dir,
"--target",
"no-modules",
],
cwd=root_dir,
).check_returncode()
20 changes: 1 addition & 19 deletions examples/raytrace-parallel/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,4 @@

set -ex

# A couple of steps are necessary to get this build working which makes it slightly
# nonstandard compared to most other builds.
#
# * First, the Rust standard library needs to be recompiled with atomics
# enabled. to do that we use Cargo's unstable `-Zbuild-std` feature.
#
# * Next we need to compile everything with the `atomics` and `bulk-memory`
# features enabled, ensuring that LLVM will generate atomic instructions,
# shared memory, passive segments, etc.

RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
cargo build --target wasm32-unknown-unknown --release -Z build-std=std,panic_abort

# Note the usage of `--target no-modules` here which is required for passing
# the memory import to each wasm module.
cargo run -p wasm-bindgen-cli -- \
../../target/wasm32-unknown-unknown/release/raytrace_parallel.wasm \
--out-dir . \
--target no-modules
python3 build.py
9 changes: 9 additions & 0 deletions examples/raytrace-parallel/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
import os
import subprocess

root_dir = os.path.dirname(__file__)

subprocess.run(["python3", "build.py"], cwd=root_dir).check_returncode()

subprocess.run(["python3", "server.py"], cwd=root_dir).check_returncode()
2 changes: 1 addition & 1 deletion examples/raytrace-parallel/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -ex

./build.sh
python3 build.py

python3 server.py

0 comments on commit e148618

Please sign in to comment.