@ -43,7 +43,7 @@ async function init_wasm(state) {
@@ -43,7 +43,7 @@ async function init_wasm(state) {
env : { 'memory' : memory }
} ) ;
const nworkers = navigator . hardwareConcurrency ;
const nworkers = 1 ; //navigator.hardwareConcurrency;
state . wasm . exports = master _wasm . instance . exports ;
state . wasm . heap _base = state . wasm . exports . alloc _static ( 0 ) ;
@ -80,12 +80,17 @@ async function init_wasm(state) {
@@ -80,12 +80,17 @@ async function init_wasm(state) {
'used' : 0 ,
'cap' : initial
} ,
'width' : {
'used' : 0 ,
'cap' : initial
}
} ;
state . wasm . buffers [ 'xs' ] . offset = state . wasm . exports . alloc _static ( initial ) ;
state . wasm . buffers [ 'ys' ] . offset = state . wasm . exports . alloc _static ( initial ) ;
state . wasm . buffers [ 'pressures' ] . offset = state . wasm . exports . alloc _static ( initial ) ;
state . wasm . buffers [ 'coords_from' ] . offset = state . wasm . exports . alloc _static ( initial ) ;
state . wasm . buffers [ 'width' ] . offset = state . wasm . exports . alloc _static ( initial ) ;
const mem = state . wasm . memory . buffer ;
@ -97,6 +102,8 @@ async function init_wasm(state) {
@@ -97,6 +102,8 @@ async function init_wasm(state) {
mem , state . wasm . buffers [ 'pressures' ] . offset ) ;
state . wasm . buffers [ 'coords_from' ] . tv = tv _create _on ( Uint32Array , initial / 4 ,
mem , state . wasm . buffers [ 'coords_from' ] . offset ) ;
state . wasm . buffers [ 'width' ] . tv = tv _create _on ( Uint32Array , initial / 4 ,
mem , state . wasm . buffers [ 'width' ] . offset ) ;
tv _add ( state . wasm . buffers [ 'coords_from' ] . tv , 0 ) ;
state . wasm . buffers [ 'coords_from' ] . used = 4 ;
@ -108,6 +115,7 @@ function wasm_ensure_by(state, nstrokes, ncoords) {
@@ -108,6 +115,7 @@ function wasm_ensure_by(state, nstrokes, ncoords) {
const old _ys _offset = buffers [ 'ys' ] . offset ;
const old _coords _from _offset = buffers [ 'coords_from' ] . offset ;
const old _pressures _offset = buffers [ 'pressures' ] . offset ;
const old _width _offset = buffers [ 'width' ] . offset ;
let realloc = false ;
let coords _bytes = buffers [ 'xs' ] . cap ;
@ -135,23 +143,31 @@ function wasm_ensure_by(state, nstrokes, ncoords) {
@@ -135,23 +143,31 @@ function wasm_ensure_by(state, nstrokes, ncoords) {
buffers [ 'ys' ] . offset = state . wasm . exports . alloc _static ( coords _bytes ) ;
buffers [ 'pressures' ] . offset = state . wasm . exports . alloc _static ( coords _bytes ) ;
buffers [ 'coords_from' ] . offset = state . wasm . exports . alloc _static ( stroke _bytes ) ;
buffers [ 'width' ] . offset = state . wasm . exports . alloc _static ( stroke _bytes ) ;
buffers [ 'xs' ] . tv = tv _create _on ( Float32Array , coords _bytes / 4 , mem , buffers [ 'xs' ] . offset ) ;
buffers [ 'ys' ] . tv = tv _create _on ( Float32Array , coords _bytes / 4 , mem , buffers [ 'ys' ] . offset ) ;
buffers [ 'pressures' ] . tv = tv _create _on ( Uint8Array , coords _bytes , mem , buffers [ 'pressures' ] . offset ) ;
buffers [ 'coords_from' ] . tv = tv _create _on ( Uint32Array , stroke _bytes / 4 , mem , buffers [ 'coords_from' ] . offset ) ;
buffers [ 'width' ] . tv = tv _create _on ( Uint32Array , stroke _bytes / 4 , mem , buffers [ 'width' ] . offset ) ;
// TODO: this should have been automatic maybe?
buffers [ 'xs' ] . tv . size = buffers [ 'xs' ] . used / 4 ;
buffers [ 'ys' ] . tv . size = buffers [ 'ys' ] . used / 4 ;
buffers [ 'pressures' ] . tv . size = buffers [ 'pressures' ] . used ;
buffers [ 'coords_from' ] . tv . size = buffers [ 'coords_from' ] . used / 4 ;
buffers [ 'width' ] . tv . size = buffers [ 'width' ] . used / 4 ;
// TODO: this is SUS, should all the caps really be coords_bytes?
buffers [ 'xs' ] . cap = buffers [ 'ys' ] . cap = buffers [ 'pressures' ] . cap = coords _bytes ;
buffers [ 'coords_from' ] . cap = stroke _bytes ;
buffers [ 'coords_from' ] . cap = buffers [ 'width' ] . cap = stroke _bytes ;
const tmp = new Uint8Array ( Math . max ( coords _bytes , stroke _bytes ) ) ;
// Copy from back to front (otherwise we will overwrite)
tmp . set ( new Uint8Array ( mem , old _width _offset , buffers [ 'width' ] . used ) ) ;
memv . set ( new Uint8Array ( tmp . buffer , 0 , buffers [ 'width' ] . used ) , buffers [ 'width' ] . offset ) ;
tmp . set ( new Uint8Array ( mem , old _coords _from _offset , buffers [ 'coords_from' ] . used ) ) ;
memv . set ( new Uint8Array ( tmp . buffer , 0 , buffers [ 'coords_from' ] . used ) , buffers [ 'coords_from' ] . offset ) ;
@ -169,6 +185,7 @@ async function do_lod(state, context) {
@@ -169,6 +185,7 @@ async function do_lod(state, context) {
const buffers = state . wasm . buffers ;
const result _buffers = state . wasm . exports . alloc _dynamic ( state . wasm . workers . length * 4 ) ;
const result _counts = state . wasm . exports . alloc _dynamic ( state . wasm . workers . length * 4 ) ;
const result _batch _counts = state . wasm . exports . alloc _dynamic ( state . wasm . workers . length * 4 ) ;
const clipped _indices = state . wasm . exports . alloc _dynamic ( context . clipped _indices . size * 4 ) ;
const mem = new Uint8Array ( state . wasm . memory . buffer ) ;
@ -180,11 +197,13 @@ async function do_lod(state, context) {
@@ -180,11 +197,13 @@ async function do_lod(state, context) {
const indices _per _thread = Math . floor ( context . clipped _indices . size / state . wasm . workers . length ) ;
const offsets = {
'coords_from' : buffers [ 'coords_from' ] . offset ,
'width' : buffers [ 'width' ] . offset ,
'xs' : buffers [ 'xs' ] . offset ,
'ys' : buffers [ 'ys' ] . offset ,
'pressures' : buffers [ 'pressures' ] . offset ,
'result_buffers' : result _buffers ,
'result_counts' : result _counts ,
'result_batch_counts' : result _batch _counts ,
} ;
const jobs = [ ] ;
@ -209,11 +228,13 @@ async function do_lod(state, context) {
@@ -209,11 +228,13 @@ async function do_lod(state, context) {
const result _offset = state . wasm . exports . merge _results (
result _counts ,
result _batch _counts ,
result _buffers ,
state . wasm . workers . length
) ;
const segment _count = new Int32Array ( state . wasm . memory . buffer , result _counts , 1 ) [ 0 ] ; // by convention
const batch _count = new Int32Array ( state . wasm . memory . buffer , result _batch _counts , 1 ) [ 0 ] ; // by convention
// Use results without copying from WASM memory
const wasm _points = new Float32Array ( state . wasm . memory . buffer ,
@ -221,16 +242,25 @@ async function do_lod(state, context) {
@@ -221,16 +242,25 @@ async function do_lod(state, context) {
const wasm _ids = new Uint32Array ( state . wasm . memory . buffer ,
result _offset + segment _count * 2 * 4 , segment _count ) ;
const wasm _pressures = new Uint8Array ( state . wasm . memory . buffer ,
result _offset + segment _count * 2 * 4 + segment _count * 4 , segment _count ) ;
result _offset + segment _count * 3 * 4 , segment _count ) ;
const wasm _batches = new Int32Array ( state . wasm . memory . buffer ,
result _offset + round _to _pow2 ( segment _count * ( 3 * 4 + 1 ) , 4 ) , batch _count * 2 ) ;
context . instance _data _points . data = wasm _points ;
context . instance _data _points . size = segment _count * 2 ;
context . instance _data _points . capacity = segment _count * 2 ;
context . instance _data _ids . data = wasm _ids ;
context . instance _data _ids . size = segment _count ;
context . instance _data _ids . capacity = segment _count ;
context . instance _data _pressures . data = wasm _pressures ;
context . instance _data _pressures . size = segment _count ;
context . instance _data _pressures . capacity = segment _count ;
context . instance _data _batches . data = wasm _batches ;
context . instance _data _batches . size = batch _count * 2 ;
context . instance _data _batches . capacity = batch _count * 2 ;
return segment _count ;
}