From 08cdc5c6e8310890b1e7102b1fcb68dd5f6009a9 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 2 Jun 2019 12:36:14 +0200 Subject: Send OffscreenCanvas to graphics worker --- WebInterface/wasm/webhogg/Cargo.toml | 1 + WebInterface/wasm/webhogg/game_logic.js | 29 ++++++++++++++---------- WebInterface/wasm/webhogg/graphics.js | 40 +++++++++++++++++++++------------ WebInterface/wasm/webhogg/index.html | 2 +- WebInterface/wasm/webhogg/loader.js | 5 +++++ WebInterface/wasm/webhogg/src/lib.rs | 5 +++-- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/WebInterface/wasm/webhogg/Cargo.toml b/WebInterface/wasm/webhogg/Cargo.toml index 60cee51..9d6e2ac 100644 --- a/WebInterface/wasm/webhogg/Cargo.toml +++ b/WebInterface/wasm/webhogg/Cargo.toml @@ -26,6 +26,7 @@ features = [ 'HtmlCanvasElement', 'WorkerGlobalScope', 'DedicatedWorkerGlobalScope', + 'OffscreenCanvas', 'WebGl2RenderingContext', 'WebGlShader', 'WebGlProgram', diff --git a/WebInterface/wasm/webhogg/game_logic.js b/WebInterface/wasm/webhogg/game_logic.js index 842c3c8..d023228 100644 --- a/WebInterface/wasm/webhogg/game_logic.js +++ b/WebInterface/wasm/webhogg/game_logic.js @@ -28,10 +28,11 @@ __exports.game_logic_entry = game_logic_entry /** * @param {any} worker +* @param {any} canvas * @returns {void} */ -function graphics_entry(worker) { - return wasm.graphics_entry(addHeapObject(worker)); +function graphics_entry(worker, canvas) { + return wasm.graphics_entry(addHeapObject(worker), addHeapObject(canvas)); } __exports.graphics_entry = graphics_entry @@ -359,9 +360,9 @@ function __wbindgen_throw(ptr, len) { } __exports.__wbindgen_throw = __wbindgen_throw -function __wbindgen_closure_wrapper61(a, b, _ignored) { - const f = wasm.__wbg_function_table.get(22); - const d = wasm.__wbg_function_table.get(23); +function __wbindgen_closure_wrapper59(a, b, _ignored) { + const f = wasm.__wbg_function_table.get(25); + const d = wasm.__wbg_function_table.get(26); const cb = function(arg0) { this.cnt++; let a = this.a; @@ -382,7 +383,7 @@ function __wbindgen_closure_wrapper61(a, b, _ignored) { real.original = cb; return addHeapObject(real); } -__exports.__wbindgen_closure_wrapper61 = __wbindgen_closure_wrapper61 +__exports.__wbindgen_closure_wrapper59 = __wbindgen_closure_wrapper59 function __wbindgen_object_clone_ref(idx) { return addHeapObject(getObject(idx)); @@ -397,11 +398,15 @@ const WASM_URL = './pkg/webhogg_bg.wasm'; const imports = { './webhogg': __exports }; -let graphics = new Worker('./graphics.js', {type: 'module', credentials: 'include'}); +onmessage = function (e) { + console.log('transport canvas'); -let res = WebAssembly.instantiateStreaming(fetch(WASM_URL), imports); + let res = WebAssembly.instantiateStreaming(fetch(WASM_URL), imports); -res.then(result => { - wasm = result.instance.exports; - game_logic_entry(graphics); -}); + res.then(result => { + wasm = result.instance.exports; + game_logic_entry(graphics); + }); + let graphics = new Worker('./graphics.js', {type: 'module', credentials: 'include'}); + graphics.postMessage(e.data, [e.data.canvas]); +} diff --git a/WebInterface/wasm/webhogg/graphics.js b/WebInterface/wasm/webhogg/graphics.js index 3ec753b..5f817fd 100644 --- a/WebInterface/wasm/webhogg/graphics.js +++ b/WebInterface/wasm/webhogg/graphics.js @@ -28,10 +28,11 @@ __exports.game_logic_entry = game_logic_entry /** * @param {any} worker +* @param {any} canvas * @returns {void} */ -function graphics_entry(worker) { - return wasm.graphics_entry(addHeapObject(worker)); +function graphics_entry(worker, canvas) { + return wasm.graphics_entry(addHeapObject(worker), addHeapObject(canvas)); } __exports.graphics_entry = graphics_entry @@ -359,9 +360,9 @@ function __wbindgen_throw(ptr, len) { } __exports.__wbindgen_throw = __wbindgen_throw -function __wbindgen_closure_wrapper61(a, b, _ignored) { - const f = wasm.__wbg_function_table.get(22); - const d = wasm.__wbg_function_table.get(23); +function __wbindgen_closure_wrapper59(a, b, _ignored) { + const f = wasm.__wbg_function_table.get(25); + const d = wasm.__wbg_function_table.get(26); const cb = function(arg0) { this.cnt++; let a = this.a; @@ -382,7 +383,7 @@ function __wbindgen_closure_wrapper61(a, b, _ignored) { real.original = cb; return addHeapObject(real); } -__exports.__wbindgen_closure_wrapper61 = __wbindgen_closure_wrapper61 +__exports.__wbindgen_closure_wrapper59 = __wbindgen_closure_wrapper59 function __wbindgen_object_clone_ref(idx) { return addHeapObject(getObject(idx)); @@ -396,13 +397,24 @@ __exports.__wbindgen_object_drop_ref = __wbindgen_object_drop_ref const WASM_URL = './pkg/webhogg_bg.wasm'; const imports = { './webhogg': __exports }; -let res = WebAssembly.instantiateStreaming(fetch(WASM_URL), imports); -res.then(result => { - wasm = result.instance.exports; - graphics_entry(self); -}); +let res = WebAssembly.instantiateStreaming(fetch(WASM_URL), imports); -/*onmessage = function (e) { - console.log('gooot messaaage', e.data); -}*/ +let first = true; +let msg = null; +onmessage = function (e) { + if (first) { + first = false; + console.log('got context: ', e.data); + msg = e.data; + } +} +while (msg === null) { + console.log('yay'); + res.then(result => { + wasm = result.instance.exports; + console.log('context sndng: ', msg); + graphics_entry(self, msg); + }); + break; +} diff --git a/WebInterface/wasm/webhogg/index.html b/WebInterface/wasm/webhogg/index.html index ced7e94..ad951a6 100644 --- a/WebInterface/wasm/webhogg/index.html +++ b/WebInterface/wasm/webhogg/index.html @@ -3,7 +3,6 @@ webhogg -