Skip to content

Commit

Permalink
Switch from Emscripten to clang+WASI SDK for building decoder
Browse files Browse the repository at this point in the history
To make it easier to build gltfpack as a library, we're going to switch
to clang and WASI SDK for building Wasm code in this codebase.

This change starts the process by converting meshopt_decoder.js to this.
Since WASI SDK doesn't provide a small enough memcpy/memset by default,
or a version of sbrk() that actually works with byte aligned
allocations, we provide our own versions for now to keep the size minimal.

As a result, the cumulative size of .js.gz actually decreases by 337
bytes.

To build the decoder, you now need to specify paths to clang-11 (unless
clang is clang-11 on the target system) and WASI SDK:

	make WASMCC=clang-11 WASI_SDK=../wasi js/meshopt_decoder.js
  • Loading branch information
zeux committed Oct 8, 2020
1 parent a507623 commit ff4308d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 12 deletions.
21 changes: 13 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MAKEFLAGS+=-r -j
COMMA=,

config=debug
files=demo/pirate.obj
Expand All @@ -23,11 +24,15 @@ CFLAGS=-g -Wall -Wextra -Werror -std=c89
CXXFLAGS=-g -Wall -Wextra -Wshadow -Wno-missing-field-initializers -Werror -std=c++98
LDFLAGS=

WASM_SOURCES=src/vertexcodec.cpp src/indexcodec.cpp src/vertexfilter.cpp
WASM_EXPORTS="__initialize","_sbrk"
WASM_EXPORTS+=,"_meshopt_decodeVertexBuffer","_meshopt_decodeIndexBuffer","_meshopt_decodeIndexSequence"
WASM_EXPORTS+=,"_meshopt_decodeFilterOct","_meshopt_decodeFilterQuat","_meshopt_decodeFilterExp"
WASM_FLAGS=-O3 -DNDEBUG -s EXPORTED_FUNCTIONS='[$(WASM_EXPORTS)]' -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=24576 -s TOTAL_MEMORY=65536 --no-entry
WASMCC=clang
WASI_SDK=

WASM_SOURCES=src/vertexcodec.cpp src/indexcodec.cpp src/vertexfilter.cpp tools/wasmstubs.cpp
WASM_EXPORTS=meshopt_decodeVertexBuffer meshopt_decodeIndexBuffer meshopt_decodeIndexSequence meshopt_decodeFilterOct meshopt_decodeFilterQuat meshopt_decodeFilterExp sbrk __wasm_call_ctors
WASM_FLAGS=--target=wasm32-wasi --sysroot=$(WASI_SDK)
WASM_FLAGS+=$(patsubst %,-Wl$(COMMA)--export=%,$(WASM_EXPORTS))
WASM_FLAGS+=-O3 -DNDEBUG -nostartfiles -nostdlib -Wl,--no-entry -Wl,-s
WASM_FLAGS+=-Wl,-z -Wl,stack-size=24576 -Wl,--initial-memory=65536

ifeq ($(config),iphone)
IPHONESDK=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
Expand Down Expand Up @@ -88,14 +93,14 @@ gltf/bin/gltfpack.js: ${LIBRARY_SOURCES} ${GLTFPACK_SOURCES} tools/meshloader.cp

build/decoder_base.wasm: $(WASM_SOURCES)
@mkdir -p build
emcc $^ $(WASM_FLAGS) -o $@
$(WASMCC) $^ $(WASM_FLAGS) -o $@

build/decoder_simd.wasm: $(WASM_SOURCES)
@mkdir -p build
emcc $^ $(WASM_FLAGS) -o $@ -msimd128 -mbulk-memory
$(WASMCC) $^ $(WASM_FLAGS) -o $@ -msimd128 -mbulk-memory

js/meshopt_decoder.js: build/decoder_base.wasm build/decoder_simd.wasm
sed -i "s#Built with emcc.*#Built with $$(emcc --version | head -n 1)#" $@
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1)#" $@
sed -i "s#\(var wasm_base = \)\".*\";#\\1\"$$(cat build/decoder_base.wasm | python3 tools/wasmpack.py)\";#" $@
sed -i "s#\(var wasm_simd = \)\".*\";#\\1\"$$(cat build/decoder_simd.wasm | python3 tools/wasmpack.py)\";#" $@

Expand Down
Loading

0 comments on commit ff4308d

Please sign in to comment.