Run cache loop in web

This commit is contained in:
Maximilian Ammann 2022-01-01 16:55:15 +01:00
parent ce788619a1
commit 5cde7afe27
8 changed files with 72 additions and 36 deletions

View File

@ -19,5 +19,5 @@ fn main() {
cache_io.run_loop();
});
pollster::block_on(main_loop::setup(window, event_loop, cache_main));
pollster::block_on(main_loop::setup(window, event_loop, Box::new(cache_main)));
}

View File

@ -7,7 +7,7 @@ use crate::io::cache::Cache;
use crate::platform::Instant;
use crate::render::state::State;
pub async fn setup(window: winit::window::Window, event_loop: EventLoop<()>, cache: Cache) {
pub async fn setup(window: winit::window::Window, event_loop: EventLoop<()>, cache: Box<Cache>) {
info!("== mapr ==");
for x in 0..6 {

View File

@ -1 +0,0 @@

View File

@ -1,22 +1,24 @@
use console_error_panic_hook;
use std::cell::RefCell;
use std::panic;
use std::rc::Rc;
use js_sys::Array;
use log::{info, warn, Level};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{MessageEvent, Window as WebWindow};
use winit::dpi::{LogicalSize, Size};
use winit::event_loop::EventLoop;
use winit::platform::web::WindowBuilderExtWebSys;
use winit::window::{Window, WindowBuilder};
mod io;
mod wasm_experiment;
use console_error_panic_hook;
pub use instant::Instant;
use js_sys::Array;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use web_sys::{MessageEvent, Window as WebWindow};
use crate::io::cache::Cache;
mod wasm_experiment;
// WebGPU
#[cfg(not(feature = "web-webgl"))]
@ -28,14 +30,16 @@ pub const COLOR_TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8
#[wasm_bindgen(start)]
pub fn start() {
if let Err(_) = console_log::init_with_level(Level::Trace) {
if let Err(_) = console_log::init_with_level(Level::Info) {
// Failed to initialize logging. No need to log a message.
}
panic::set_hook(Box::new(console_error_panic_hook::hook));
}
#[wasm_bindgen]
pub async fn run() {
pub async fn run(cache_ptr: *mut Cache) {
let cache: Box<Cache> = unsafe { Box::from_raw(cache_ptr) };
let event_loop = EventLoop::new();
let web_window: WebWindow = web_sys::window().unwrap();
let document = web_window.document().unwrap();
@ -57,5 +61,29 @@ pub async fn run() {
height: body.client_height(),
});
crate::main_loop::setup(window, event_loop).await;
/* for x in 0..6 {
for y in 0..6 {
cache.fetch((2178 + x, 1421 + y, 12).into())
}
}*/
// Either call forget or the main loop to keep cache alive
//std::mem::forget(cache);
crate::main_loop::setup(window, event_loop, cache).await;
}
#[wasm_bindgen]
pub fn create_cache() -> *mut Cache {
let mut cache = Box::new(Cache::new());
let ptr = Box::into_raw(cache);
return ptr;
}
#[wasm_bindgen]
pub async fn run_cache_loop(cache_ptr: *mut Cache) {
let mut cache: Box<Cache> = unsafe { Box::from_raw(cache_ptr) };
// Either call forget or the cache loop to keep cache alive
// std::mem::forget(cache);
cache.run_loop();
}

View File

@ -21,7 +21,7 @@ pub fn test_fetch(web_window: &Window) {
#[wasm_bindgen]
pub fn test_shared_mem(memory: &JsValue) {
let worker = web_sys::Worker::new_with_options(
"./fetch-worker.js",
"./cache-worker.js",
// Works only on chrome
&web_sys::WorkerOptions::new().type_(web_sys::WorkerType::Module),
)

18
web/cache-worker.js Normal file
View File

@ -0,0 +1,18 @@
import init from "./dist/libs/mapr";
let initialized = false;
onmessage = async m => {
let msg = m.data;
if (msg.type === "init") {
if (initialized) {
return;
}
initialized = true;
const module = await init(undefined, msg.memory);
console.log("Started cache-worker: " + msg.cache_address)
module.run_cache_loop(msg.cache_address);
}
};

View File

@ -1,13 +0,0 @@
import init from "./dist/libs/mapr";
onmessage = async m => {
let msg = m.data;
//await fetch("http://localhost:8080/mapr.html")
if (msg.type === "init") {
const init_output = await init(undefined, msg.memory);
console.log(msg.address)
postMessage(init_output.get54(msg.address));
}
};

View File

@ -1,20 +1,24 @@
import init from "./dist/libs/mapr";
const start = async () => {
const memory = new WebAssembly.Memory({initial: 1024, maximum: 10 * 1024, shared: true});
const init_output = await init(undefined, memory);
let MEMORY = 16 * 1024;
const memory = new WebAssembly.Memory({initial: 1024, maximum: MEMORY, shared: true});
const module = await init(undefined, memory);
const fetch_worker = new Worker(new URL('./fetch-worker.js', import.meta.url), {
const worker = new Worker(new URL('./cache-worker.js', import.meta.url), {
type: "module",
});
fetch_worker.postMessage({type: "init", memory, address: init_output.test_alloc()});
let cache_address = module.create_cache();
fetch_worker.onmessage = (e) => {
console.log("Starting cache-worker")
worker.postMessage({type: "init", memory, cache_address});
/* worker.onmessage = (e) => {
console.log(e)
}
}*/
await init_output.run();
await module.run(cache_address);
}
start();
start().then(r => console.log("started via wasm"));