Skip to content

Commit

Permalink
Replace Emscripten runtime with manual .wasm loading
Browse files Browse the repository at this point in the history
This is an experimental change that replaces Emscripten with custom
runtime. Instead of compiling sbrk into wasm we implement sbrk in JS,
which requires less plumbing.

As a result, we no longer need Closure since our source is so small.

We may need to replace .wasm loading with something other than fetch to
be compatible with non-browser environments? To be determined.

Before:
decoder.js 16654 bytes
decoder.js.gz 7008 bytes

After:
decoder.js 8338 bytes
decoder.js.gz 3557 bytes
  • Loading branch information
zeux committed Mar 8, 2019
1 parent 84dcff6 commit 5f9d7b7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 78 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ format:
meshencoder: $(ENCODER_OBJECTS) $(LIBRARY)
$(CXX) $^ $(LDFLAGS) -o $@

js/decoder.js: src/vertexcodec.cpp src/indexcodec.cpp js/decoder-post.js
emcc $(filter %.cpp,$^) -O3 -DNDEBUG -s EXPORTED_FUNCTIONS='["_meshopt_decodeVertexBuffer", "_meshopt_decodeIndexBuffer", "_sbrk"]' -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s MALLOC=emmalloc -s TOTAL_STACK=32768 -s TOTAL_MEMORY=65536 -s MODULARIZE=1 -s SINGLE_FILE=1 -s EXPORT_NAME=MeshoptDecoder --closure 1 --post-js js/decoder-post.js -o $@
sed -i '1s;^;// This file is part of meshoptimizer library and is distributed under the terms of MIT License.\n// Copyright (C) 2016-2019, by Arseny Kapoulkine ([email protected]);' $@
js/decoder.js: src/vertexcodec.cpp src/indexcodec.cpp js/decoder-src.js
@mkdir -p build
emcc $(filter %.cpp,$^) -O3 -DNDEBUG -s EXPORTED_FUNCTIONS='["_meshopt_decodeVertexBuffer", "_meshopt_decodeIndexBuffer"]' -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=32768 -s TOTAL_MEMORY=65536 -o build/decoder.wasm
sed "s#WASMHERE#$$(cat build/decoder.wasm | base64 -w 0)#" js/decoder-src.js >$@

$(EXECUTABLE): $(DEMO_OBJECTS) $(LIBRARY)
$(CXX) $^ $(LDFLAGS) -o $@
Expand Down
33 changes: 0 additions & 33 deletions js/decoder-post.js

This file was deleted.

67 changes: 67 additions & 0 deletions js/decoder-src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is part of meshoptimizer library and is distributed under the terms of MIT License.
// Copyright (C) 2016-2019, by Arseny Kapoulkine ([email protected])
function MeshoptDecoder() {
var Module = {}

var wasm = "WASMHERE";

var page = 65536;
var memory = new WebAssembly.Memory({
initial: 1
});
var heap = new Uint8Array(memory.buffer);
var brk = 32768;

var sbrk = function(size) {
var old = brk;
brk += size;
if (brk > heap.length) {
memory.grow(Math.ceil((brk - heap.length) / page));
heap = new Uint8Array(memory.buffer);
}
return old;
};

var imports = {
env: {
memory: memory,
_emscripten_memcpy_big: function(d, s, n) {
heap.set(heap.subarray(s, s + n), d);
},
}
};

var exports = {};

var source = fetch('data:application/octet-stream;base64,' + wasm);
var instance = source
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports))
.then(result => exports = result.instance.exports);

Module.then = function(callback) {
instance.then(callback);
};

Module.decodeVertexBuffer = function(target, count, size, source) {
var tp = sbrk(count * size);
var sp = sbrk(source.length);
heap.set(source, sp);
var res = exports["_meshopt_decodeVertexBuffer"](tp, count, size, sp, source.length);
target.set(heap.subarray(tp, tp + count * size), 0);
sbrk(tp - sbrk(0));
if (res != 0) throw new Error("Malformed vertex buffer data");
};

Module.decodeIndexBuffer = function(target, count, size, source) {
var tp = sbrk(count * size);
var sp = sbrk(source.length);
heap.set(source, sp);
var res = exports["_meshopt_decodeIndexBuffer"](tp, count, size, sp, source.length);
target.set(heap.subarray(tp, tp + count * size), 0);
sbrk(tp - sbrk(0));
if (res != 0) throw new Error("Malformed index buffer data");
};

return Module;
}
Loading

0 comments on commit 5f9d7b7

Please sign in to comment.