forked from zeux/meshoptimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Emscripten runtime with manual .wasm loading
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
Showing
4 changed files
with
136 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 $@ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.