Skip to content

Commit

Permalink
instead of SIMD/standard, call them deluxe/compat. compat disables SI…
Browse files Browse the repository at this point in the history
…MD and pthreads. the reasoning being that if you don't support WASM SIMD, your device probably can't support pthreads either.
  • Loading branch information
Birch-san committed Jan 8, 2025
1 parent fd74cec commit 8e76067
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,35 @@ jobs:
else
echo "target_type=Release" >> $GITHUB_OUTPUT
fi
- name: "SIMD: build makefile"
- name: "deluxe: build makefile"
working-directory: box2d3-wasm
run: shell/0_build_makefile.sh
env:
FLAVOUR: simd
FLAVOUR: deluxe
TARGET_TYPE: ${{ steps.build-type.outputs.target_type }}
- name: "SIMD: emmake"
- name: "deluxe: emmake"
working-directory: box2d3-wasm
run: emmake make -j8 -C cmake-build-simd
- name: "SIMD: build wasm"
run: emmake make -j8 -C cmake-build-deluxe
- name: "deluxe: build wasm"
working-directory: box2d3-wasm
run: shell/1_build_wasm.sh
env:
FLAVOUR: simd
FLAVOUR: deluxe
TARGET_TYPE: ${{ steps.build-type.outputs.target_type }}
- name: "Standard: build makefile"
- name: "compat: build makefile"
working-directory: box2d3-wasm
run: shell/0_build_makefile.sh
env:
FLAVOUR: standard
FLAVOUR: compat
TARGET_TYPE: ${{ steps.build-type.outputs.target_type }}
- name: "Standard: emmake"
- name: "compat: emmake"
working-directory: box2d3-wasm
run: emmake make -j8 -C cmake-build-standard
- name: "Standard: build wasm"
run: emmake make -j8 -C cmake-build-compat
- name: "compat: build wasm"
working-directory: box2d3-wasm
run: shell/1_build_wasm.sh
env:
FLAVOUR: standard
FLAVOUR: compat
TARGET_TYPE: ${{ steps.build-type.outputs.target_type }}
# Pack mode: Create prerelease version without git tags
- name: version for pack
Expand Down
18 changes: 9 additions & 9 deletions box2d3-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ git clone --recurse-submodules https://github.com/Birch-san/box2d3-wasm.git
Run the following commands:

```bash
FLAVOUR=simd TARGET_TYPE=Debug ./shell/0_build_makefile.sh
FLAVOUR=deluxe TARGET_TYPE=Debug ./shell/0_build_makefile.sh
emmake make -j8 -C cmake-build
FLAVOUR=simd TARGET_TYPE=Debug ./shell/1_build_wasm.sh
FLAVOUR=deluxe TARGET_TYPE=Debug ./shell/1_build_wasm.sh
```

## Run Demos
Expand Down Expand Up @@ -90,15 +90,15 @@ node integration-test/index.mjs
From `box2d3-wasm` package's directory,

```bash
git clean -dfx build cmake-build-simd cmake-build-standard
git clean -dfx build cmake-build-deluxe cmake-build-compat

FLAVOUR=simd TARGET_TYPE=Release ./shell/0_build_makefile.sh
emmake make -j8 -C cmake-build-simd
FLAVOUR=simd TARGET_TYPE=Release ./shell/1_build_wasm.sh
FLAVOUR=deluxe TARGET_TYPE=Release ./shell/0_build_makefile.sh
emmake make -j8 -C cmake-build-deluxe
FLAVOUR=deluxe TARGET_TYPE=Release ./shell/1_build_wasm.sh

FLAVOUR=standard TARGET_TYPE=Release ./shell/0_build_makefile.sh
emmake make -j8 -C cmake-build-standard
FLAVOUR=standard TARGET_TYPE=Release ./shell/1_build_wasm.sh
FLAVOUR=compat TARGET_TYPE=Release ./shell/0_build_makefile.sh
emmake make -j8 -C cmake-build-compat
FLAVOUR=compat TARGET_TYPE=Release ./shell/1_build_wasm.sh

# e.g. major | minor | patch, see https://docs.npmjs.com/cli/v10/commands/npm-version
npm version minor
Expand Down
2 changes: 1 addition & 1 deletion box2d3-wasm/build/dist/es/entry.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// these typings are provided in case you wish to bypass the NodeJS module specifier
// (i.e. 'box2d-wasm') and import the entrypoint directly.
import type Box2D from './simd/Box2D.simd'
import type Box2D from './deluxe/Box2D.deluxe'
export = Box2D;
6 changes: 3 additions & 3 deletions box2d3-wasm/build/dist/es/entry.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @import { MainModule, ModuleOptions } from './simd/Box2D.simd'
* @import { MainModule, ModuleOptions } from './deluxe/Box2D.deluxe'
* @param {ModuleOptions} options
* @return {Promise<MainModule>}
*/
Expand All @@ -23,8 +23,8 @@ export default async (options, ...args) => {
/** @type {{ 'default': (options?: unknown) => MainModule) }} */
const Box2DModule = await (
hasSIMD
? import('./simd/Box2D.simd.mjs')
: import('./standard/Box2D.standard.mjs')
? import('./deluxe/Box2D.deluxe.mjs')
: import('./compat/Box2D.compat.mjs')
);
const { 'default': Box2DFactory } = Box2DModule;
// awaiting gives us a better stack trace (at the cost of an extra microtask)
Expand Down
21 changes: 10 additions & 11 deletions box2d3-wasm/shell/0_build_makefile.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# FLAVOUR=simd TARGET_TYPE=Debug ./shell/0_build_makefile.sh
# FLAVOUR=deluxe TARGET_TYPE=Debug ./shell/0_build_makefile.sh
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BOX2D_DIR="$(realpath "$DIR/../../box2d")"
Expand All @@ -18,23 +18,22 @@ NC='\033[0m' # No Color
# fi

CMAKE_OPTS=()
CFLAGS=(-pthread -s USE_PTHREADS=1)
CXXFLAGS=(-pthread -s USE_PTHREADS=1)
CFLAGS=()
CXXFLAGS=()
case "$FLAVOUR" in
standard)
# maybe we don't need to disable SIMD? seems to compile fine
# CMAKE_OPTS=(${CMAKE_OPTS[@]} -DBOX2D_ENABLE_SIMD=OFF)
compat)
CMAKE_OPTS=(${CMAKE_OPTS[@]} -DBOX2D_ENABLE_SIMD=OFF)
;;
simd)
deluxe)
# this is ON by default but let's be explicit
CMAKE_OPTS=(${CMAKE_OPTS[@]} -DBOX2D_ENABLE_SIMD=ON)
CFLAGS=(${CFLAGS[@]} -msimd128 -msse2)
CXXFLAGS=(${CXXFLAGS[@]} -msimd128 -msse2)
CFLAGS=(${CFLAGS[@]} -msimd128 -msse2 -pthread -s USE_PTHREADS=1)
CXXFLAGS=(${CXXFLAGS[@]} -msimd128 -msse2 -pthread -s USE_PTHREADS=1)
;;
*)
>&2 echo -e "${Red}FLAVOUR not set.${NC}"
>&2 echo -e "Please set FLAVOUR to 'standard' or 'simd'. For example, with:"
>&2 echo -e "${Purple}export FLAVOUR='simd'${NC}"
>&2 echo -e "Please set FLAVOUR to 'compat' or 'deluxe'. For example, with:"
>&2 echo -e "${Purple}export FLAVOUR='deluxe'${NC}"
exit 1
;;
esac
Expand Down
48 changes: 32 additions & 16 deletions box2d3-wasm/shell/1_build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,34 @@ Blue='\033[0;34m'
Purple='\033[0;35m'
NC='\033[0m' # No Color

BASENAME=Box2D
BASENAME="Box2D.$FLAVOUR"
FLAVOUR_EMCC_OPTS=()
FLAVOUR_LINK_OPTS=()
case "$FLAVOUR" in
standard)
BASENAME="$BASENAME.standard"
compat)
;;
simd)
BASENAME="$BASENAME.simd"
FLAVOUR_EMCC_OPTS=(${FLAVOUR_EMCC_OPTS[@]} -msimd128)
deluxe)
FLAVOUR_EMCC_OPTS=(
${FLAVOUR_EMCC_OPTS[@]}
# SIMD
-msimd128
-msse2 # probably not necessary now that we're past emmake, unless our glue code adds more SSE
# threading
-pthread
-s USE_PTHREADS=1
-s PTHREAD_POOL_SIZE=pthreadCount
)
FLAVOUR_LINK_OPTS=(
${FLAVOUR_LINK_OPTS[@]}
-pthread
-s USE_PTHREADS=1
-s PTHREAD_POOL_SIZE='_emscripten_num_logical_cores()'
)
;;
*)
>&2 echo -e "${Red}FLAVOUR not set.${NC}"
>&2 echo -e "Please set FLAVOUR to 'standard' or 'simd'. For example, with:"
>&2 echo -e "${Purple}export FLAVOUR='simd'${NC}"
>&2 echo -e "Please set FLAVOUR to 'compat' or 'deluxe'. For example, with:"
>&2 echo -e "${Purple}export FLAVOUR='deluxe'${NC}"
exit 1
;;
esac
Expand All @@ -46,10 +60,6 @@ EMCC_OPTS=(
# -s SUPPORT_LONGJMP=0 # this causes 'undefined symbol: _emscripten_stack_restore'
-s EXPORTED_FUNCTIONS=_malloc,_free
-s ALLOW_MEMORY_GROWTH=1
# threading
-pthread
-s USE_PTHREADS=1
-s PTHREAD_POOL_SIZE=pthreadCount
${FLAVOUR_EMCC_OPTS[@]}
)
DEBUG_OPTS=(
Expand Down Expand Up @@ -127,11 +137,9 @@ mkdir -p "$ES_DIR"

LINK_OPTS=(
${DEBUG_OPTS[@]}
${FLAVOUR_LINK_OPTS[@]}
-lembind
-pthread
-s USE_PTHREADS=1
-s ALLOW_MEMORY_GROWTH=1
-s PTHREAD_POOL_SIZE='_emscripten_num_logical_cores()'
--post-link "$BARE_WASM"
)

Expand All @@ -145,7 +153,15 @@ emcc "${LINK_OPTS[@]}" -s EXPORT_ES6=1 -o "$ES_FILE" --emit-tsd "$ES_TSD"
cp "$ES_FILE" "$ES_PRECURSOR"
cp "$ES_TSD" "$ES_TSD_PRECURSOR"

awk -f "$DIR/modify_emscripten_mjs.awk" "$ES_PRECURSOR" > "$ES_FILE"
# TODO: make awk error if any of the text replacements fail to match anything, to protect us when emscripten updates.
case "$FLAVOUR" in
compat)
# all of our text replacements are just for disabling/tuning deluxe functionality. compat flavour doesn't need any text replacements.
;;
deluxe)
awk -f "$DIR/modify_emscripten_mjs.$FLAVOUR.awk" "$ES_PRECURSOR" > "$ES_FILE"
;;
esac
awk -f "$DIR/modify_emscripten_dts.awk" -v template="$BUILD_DIR/Box2D.template.d.ts" "$ES_TSD_PRECURSOR" > "$ES_TSD"

set +x
Expand Down
File renamed without changes.

0 comments on commit 8e76067

Please sign in to comment.